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: #5965
40 lines
817 B
Go
40 lines
817 B
Go
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))
|
|
}
|