Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sei-cosmos/storev2/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,15 @@ func (rs *Store) CacheMultiStoreFromCommitter(snap sctypes.Committer) (types.Cac

// GetStore Implements interface MultiStore
func (rs *Store) GetStore(key types.StoreKey) types.Store {
rs.mtx.RLock()
defer rs.mtx.RUnlock()
return rs.ckvStores[key]
}

// GetKVStore Implements interface MultiStore
func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore {
rs.mtx.RLock()
defer rs.mtx.RUnlock()
return rs.ckvStores[key]
}

Expand Down Expand Up @@ -453,6 +457,8 @@ func (rs *Store) GetCommitStore(key types.StoreKey) types.CommitStore {

// GetCommitKVStore Implements interface CommitMultiStore
func (rs *Store) GetCommitKVStore(key types.StoreKey) types.CommitKVStore {
rs.mtx.RLock()
defer rs.mtx.RUnlock()
Comment thread
cursor[bot] marked this conversation as resolved.
return rs.ckvStores[key]
}

Expand Down
30 changes: 30 additions & 0 deletions sei-cosmos/storev2/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/sei-protocol/sei-chain/sei-cosmos/store/mem"
"github.com/sei-protocol/sei-chain/sei-cosmos/store/types"
"github.com/sei-protocol/sei-chain/sei-cosmos/storev2/state"
"github.com/sei-protocol/sei-chain/sei-db/config"
Expand All @@ -15,6 +16,35 @@ import (
"golang.org/x/time/rate"
)

func TestGetCommitKVStore_ReaderRespectsWriteLock(t *testing.T) {
store := &Store{
storeKeys: map[string]types.StoreKey{},
ckvStores: map[types.StoreKey]types.CommitKVStore{},
}
key := types.NewKVStoreKey("bank")
store.storeKeys[key.Name()] = key
store.ckvStores[key] = mem.NewStore()

store.mtx.Lock()

readDone := make(chan types.CommitKVStore, 1)
go func() {
readDone <- store.GetCommitKVStore(key) //GetCommitKVStore is blocked until store.mtx is unlocked
}()

select {
case <-readDone:
t.Fatal("GetCommitKVStore returned while write lock held — RLock missing")
case <-time.After(50 * time.Millisecond):
}

newVal := mem.NewStore()
store.ckvStores = map[types.StoreKey]types.CommitKVStore{key: newVal}
store.mtx.Unlock()

require.Same(t, newVal, <-readDone)
}

func TestLastCommitID(t *testing.T) {
store := NewStore(t.TempDir(), config.DefaultStateCommitConfig(), config.StateStoreConfig{}, []string{})
require.Equal(t, types.CommitID{}, store.LastCommitID())
Expand Down
Loading