forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert Capabilities on Failed Tx (cosmos#5999)
Reintroduce memKVStore to keep track of fwd and reverse mappings. On reverse mapping, rather than store a mapping to marshalled capability; we store the index. capability.Keeper and all scopedKeeper have access to a capability map that maps index to the capability pointer. This is done to make sure that all writes to memKVStore get reverted on a fail tx, while also allowing GetCapability to retrieve the original memory pointer from the go map. Go map must be accessed only by first going through the memKVStore. SInce writes to go map cannot be automatically reverted on tx failure, it gets cleaned up on failed GetCapability calls. Closes: cosmos#5965
- Loading branch information
1 parent
4da4bb6
commit d247184
Showing
11 changed files
with
274 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package mem_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/mem" | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
) | ||
|
||
func TestStore(t *testing.T) { | ||
db := mem.NewStore() | ||
key, value := []byte("key"), []byte("value") | ||
|
||
require.Equal(t, types.StoreTypeMemory, db.GetStoreType()) | ||
|
||
require.Nil(t, db.Get(key)) | ||
db.Set(key, value) | ||
require.Equal(t, value, db.Get(key)) | ||
|
||
newValue := []byte("newValue") | ||
db.Set(key, newValue) | ||
require.Equal(t, newValue, db.Get(key)) | ||
|
||
db.Delete(key) | ||
require.Nil(t, db.Get(key)) | ||
} | ||
|
||
func TestCommit(t *testing.T) { | ||
db := mem.NewStore() | ||
key, value := []byte("key"), []byte("value") | ||
|
||
db.Set(key, value) | ||
id := db.Commit() | ||
require.True(t, id.IsZero()) | ||
require.True(t, db.LastCommitID().IsZero()) | ||
require.Equal(t, value, db.Get(key)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package mem | ||
|
||
import ( | ||
"io" | ||
|
||
dbm "github.com/tendermint/tm-db" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/cachekv" | ||
"github.com/cosmos/cosmos-sdk/store/dbadapter" | ||
"github.com/cosmos/cosmos-sdk/store/tracekv" | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
) | ||
|
||
var ( | ||
_ types.KVStore = (*Store)(nil) | ||
_ types.Committer = (*Store)(nil) | ||
) | ||
|
||
// Store implements an in-memory only KVStore. Entries are persisted between | ||
// commits and thus between blocks. State in Memory store is not committed as part of app state but maintained privately by each node | ||
type Store struct { | ||
dbadapter.Store | ||
} | ||
|
||
func NewStore() *Store { | ||
return NewStoreWithDB(dbm.NewMemDB()) | ||
} | ||
|
||
func NewStoreWithDB(db *dbm.MemDB) *Store { // nolint: interfacer | ||
return &Store{Store: dbadapter.Store{DB: db}} | ||
} | ||
|
||
// GetStoreType returns the Store's type. | ||
func (s Store) GetStoreType() types.StoreType { | ||
return types.StoreTypeMemory | ||
} | ||
|
||
// CacheWrap cache wraps the underlying store. | ||
func (s Store) CacheWrap() types.CacheWrap { | ||
return cachekv.NewStore(s) | ||
} | ||
|
||
// CacheWrapWithTrace implements KVStore. | ||
func (s Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap { | ||
return cachekv.NewStore(tracekv.NewStore(s, w, tc)) | ||
} | ||
|
||
// Commit performs a no-op as entries are persistent between commitments. | ||
func (s *Store) Commit() (id types.CommitID) { return } | ||
|
||
// nolint | ||
func (s *Store) SetPruning(pruning types.PruningOptions) {} | ||
func (s Store) LastCommitID() (id types.CommitID) { return } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.