-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(dot/state): add StorageState Lock/Unlock API for usage by babe and sync #1700
Merged
Merged
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
569d955
update gssmr genesis to have pallet_babe::SameAuthoritiesForever
noot f9236b7
update gssmr genesis.json
noot 234ae15
restore auths
noot 8cc3dd6
fix test
noot 41429b3
attempt to fix trie state
noot d93d511
lock entire handleBlock func
noot f4df4c5
snapshot trie in StoreTrie
noot 4563627
attempt to fix by copying branch.children
noot b813973
add trie snapshot concurrency test
noot f332399
fix tests, lint
noot 4f68e4f
use StorageState-wide lock, seems to fix
noot 25cfd53
update logs
noot 226c88a
update build block to return if queue empty
noot b4a9489
fix ordering of SetContextStorage and lock
noot 9148108
re-add StorageState-wide lock
noot c3e8a2c
lock before calling TrieState
noot 35a5b3a
use storageState lock, cleanup
noot 7bad042
cleanup
noot edf6c1b
re-add trie deletion if syncing
noot 071cb67
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot c2a6bee
fix test
noot 9c6026c
fmt
noot c3b38be
Merge branch 'development' into noot/fix-trie-state
noot 0440355
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot 75d2896
Merge branch 'noot/fix-trie-state' of github.com:ChainSafe/gossamer i…
noot f6db69a
fix
noot cc97361
use storageState Lock/Unlock
noot 67a5849
fix
noot 7201436
merge w development
noot 3606069
fix invalid runtime stop (#1705)
noot 5eff1ff
Merge branch 'development' into noot/fix-trie-state
noot e874b01
skip failing test
noot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -44,10 +44,10 @@ func errTrieDoesNotExist(hash common.Hash) error { | |
// StorageState is the struct that holds the trie, db and lock | ||
type StorageState struct { | ||
blockState *BlockState | ||
tries map[common.Hash]*trie.Trie // map of root -> trie | ||
tries *sync.Map // map[common.Hash]*trie.Trie // map of root -> trie | ||
|
||
db chaindb.Database | ||
lock sync.RWMutex | ||
db chaindb.Database | ||
sync.RWMutex | ||
|
||
// change notifiers | ||
changedLock sync.RWMutex | ||
|
@@ -66,8 +66,8 @@ func NewStorageState(db chaindb.Database, blockState *BlockState, t *trie.Trie, | |
return nil, fmt.Errorf("cannot have nil trie") | ||
} | ||
|
||
tries := make(map[common.Hash]*trie.Trie) | ||
tries[t.MustHash()] = t | ||
tries := new(sync.Map) | ||
tries.Store(t.MustHash(), t) | ||
|
||
storageTable := chaindb.NewTable(db, storagePrefix) | ||
|
||
|
@@ -97,25 +97,23 @@ func (s *StorageState) SetSyncing(syncing bool) { | |
} | ||
|
||
func (s *StorageState) pruneKey(keyHeader *types.Header) { | ||
s.lock.Lock() | ||
defer s.lock.Unlock() | ||
|
||
delete(s.tries, keyHeader.StateRoot) | ||
s.tries.Delete(keyHeader.StateRoot) | ||
} | ||
|
||
// StoreTrie stores the given trie in the StorageState and writes it to the database | ||
func (s *StorageState) StoreTrie(ts *rtstorage.TrieState, header *types.Header) error { | ||
s.lock.Lock() | ||
defer s.lock.Unlock() | ||
|
||
root := ts.MustRoot() | ||
|
||
if s.syncing { | ||
// keep only the trie at the head of the chain when syncing | ||
for key := range s.tries { | ||
delete(s.tries, key) | ||
} | ||
// TODO: probably remove this when memory usage improves | ||
s.tries.Range(func(k, _ interface{}) bool { | ||
s.tries.Delete(k) | ||
return true | ||
}) | ||
} | ||
s.tries[root] = ts.Trie() | ||
|
||
_, _ = s.tries.LoadOrStore(root, ts.Trie()) | ||
|
||
if _, ok := s.pruner.(*pruner.FullNode); header == nil && ok { | ||
return fmt.Errorf("block cannot be empty for Full node pruner") | ||
|
@@ -136,7 +134,7 @@ func (s *StorageState) StoreTrie(ts *rtstorage.TrieState, header *types.Header) | |
|
||
logger.Trace("cached trie in storage state", "root", root) | ||
|
||
if err := s.tries[root].WriteDirty(s.db); err != nil { | ||
if err := ts.Trie().WriteDirty(s.db); err != nil { | ||
logger.Warn("failed to write trie to database", "root", root, "error", err) | ||
return err | ||
} | ||
|
@@ -156,20 +154,21 @@ func (s *StorageState) TrieState(root *common.Hash) (*rtstorage.TrieState, error | |
root = &sr | ||
} | ||
|
||
s.lock.RLock() | ||
t := s.tries[*root] | ||
s.lock.RUnlock() | ||
|
||
if t != nil && t.MustHash() != *root { | ||
panic("trie does not have expected root") | ||
} | ||
|
||
if t == nil { | ||
st, has := s.tries.Load(*root) | ||
if !has { | ||
var err error | ||
t, err = s.LoadFromDB(*root) | ||
st, err = s.LoadFromDB(*root) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, _ = s.tries.LoadOrStore(*root, st) | ||
} | ||
|
||
t := st.(*trie.Trie) | ||
|
||
if has && t.MustHash() != *root { | ||
panic("trie does not have expected root") | ||
} | ||
|
||
nextTrie := t.Snapshot() | ||
|
@@ -178,10 +177,20 @@ func (s *StorageState) TrieState(root *common.Hash) (*rtstorage.TrieState, error | |
return nil, err | ||
} | ||
|
||
logger.Trace("returning trie to be modified", "root", root, "next", next.MustRoot()) | ||
logger.Trace("returning trie to be modified", "root", root) | ||
return next, nil | ||
} | ||
|
||
// BeginModifyTrie should be called before the trie is modified | ||
func (s *StorageState) BeginModifyTrie() { | ||
s.Lock() | ||
} | ||
|
||
// FinishModifyTrie should be called after the trie is done being modified | ||
func (s *StorageState) FinishModifyTrie() { | ||
s.Unlock() | ||
} | ||
|
||
// LoadFromDB loads an encoded trie from the DB where the key is `root` | ||
func (s *StorageState) LoadFromDB(root common.Hash) (*trie.Trie, error) { | ||
t := trie.NewEmptyTrie() | ||
|
@@ -190,13 +199,31 @@ func (s *StorageState) LoadFromDB(root common.Hash) (*trie.Trie, error) { | |
return nil, err | ||
} | ||
|
||
s.lock.Lock() | ||
defer s.lock.Unlock() | ||
|
||
s.tries[t.MustHash()] = t | ||
_, _ = s.tries.LoadOrStore(t.MustHash(), t) | ||
return t, nil | ||
} | ||
|
||
func (s *StorageState) loadTrie(root *common.Hash) (*trie.Trie, error) { | ||
if root == nil { | ||
sr, err := s.blockState.BestBlockStateRoot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
root = &sr | ||
} | ||
|
||
if t, has := s.tries.Load(*root); !has && t != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch, updated! |
||
return t.(*trie.Trie), nil | ||
} | ||
|
||
tr, err := s.LoadFromDB(*root) | ||
if err != nil { | ||
return nil, errTrieDoesNotExist(*root) | ||
} | ||
|
||
return tr, nil | ||
} | ||
|
||
// ExistsStorage check if the key exists in the storage trie with the given storage hash | ||
// If no hash is provided, the current chain head is used | ||
func (s *StorageState) ExistsStorage(root *common.Hash, key []byte) (bool, error) { | ||
|
@@ -215,11 +242,8 @@ func (s *StorageState) GetStorage(root *common.Hash, key []byte) ([]byte, error) | |
root = &sr | ||
} | ||
|
||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
|
||
if trie, ok := s.tries[*root]; ok { | ||
val := trie.Get(key) | ||
if t, has := s.tries.Load(*root); has { | ||
val := t.(*trie.Trie).Get(key) | ||
return val, nil | ||
} | ||
|
||
|
@@ -258,109 +282,41 @@ func (s *StorageState) EnumeratedTrieRoot(values [][]byte) { | |
|
||
// Entries returns Entries from the trie with the given state root | ||
func (s *StorageState) Entries(root *common.Hash) (map[string][]byte, error) { | ||
if root == nil { | ||
head, err := s.blockState.BestBlockStateRoot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
root = &head | ||
} | ||
|
||
s.lock.RLock() | ||
tr, ok := s.tries[*root] | ||
s.lock.RUnlock() | ||
|
||
if !ok { | ||
var err error | ||
tr, err = s.LoadFromDB(*root) | ||
if err != nil { | ||
return nil, errTrieDoesNotExist(*root) | ||
} | ||
tr, err := s.loadTrie(root) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
return tr.Entries(), nil | ||
} | ||
|
||
// GetKeysWithPrefix returns all that match the given prefix for the given hash (or best block state root if hash is nil) in lexicographic order | ||
func (s *StorageState) GetKeysWithPrefix(hash *common.Hash, prefix []byte) ([][]byte, error) { | ||
if hash == nil { | ||
sr, err := s.blockState.BestBlockStateRoot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
hash = &sr | ||
} | ||
|
||
s.lock.RLock() | ||
tr, ok := s.tries[*hash] | ||
s.lock.RUnlock() | ||
|
||
if !ok { | ||
var err error | ||
tr, err = s.LoadFromDB(*hash) | ||
if err != nil { | ||
return nil, errTrieDoesNotExist(*hash) | ||
} | ||
func (s *StorageState) GetKeysWithPrefix(root *common.Hash, prefix []byte) ([][]byte, error) { | ||
tr, err := s.loadTrie(root) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
return tr.GetKeysWithPrefix(prefix), nil | ||
} | ||
|
||
// GetStorageChild return GetChild from the trie | ||
func (s *StorageState) GetStorageChild(hash *common.Hash, keyToChild []byte) (*trie.Trie, error) { | ||
if hash == nil { | ||
sr, err := s.blockState.BestBlockStateRoot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
hash = &sr | ||
} | ||
|
||
s.lock.RLock() | ||
tr, ok := s.tries[*hash] | ||
s.lock.RUnlock() | ||
|
||
if !ok { | ||
var err error | ||
tr, err = s.LoadFromDB(*hash) | ||
if err != nil { | ||
return nil, errTrieDoesNotExist(*hash) | ||
} | ||
// GetStorageChild returns a child trie, if it exists | ||
func (s *StorageState) GetStorageChild(root *common.Hash, keyToChild []byte) (*trie.Trie, error) { | ||
tr, err := s.loadTrie(root) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
return tr.GetChild(keyToChild) | ||
} | ||
|
||
// GetStorageFromChild return GetFromChild from the trie | ||
func (s *StorageState) GetStorageFromChild(hash *common.Hash, keyToChild, key []byte) ([]byte, error) { | ||
if hash == nil { | ||
sr, err := s.blockState.BestBlockStateRoot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
hash = &sr | ||
} | ||
|
||
s.lock.RLock() | ||
tr, ok := s.tries[*hash] | ||
s.lock.RUnlock() | ||
|
||
if !ok { | ||
var err error | ||
tr, err = s.LoadFromDB(*hash) | ||
if err != nil { | ||
return nil, errTrieDoesNotExist(*hash) | ||
} | ||
// GetStorageFromChild get a value from a child trie | ||
func (s *StorageState) GetStorageFromChild(root *common.Hash, keyToChild, key []byte) ([]byte, error) { | ||
tr, err := s.loadTrie(root) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
return tr.GetFromChild(keyToChild, key) | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need to be re-added before merging?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll open an issue for this, didn't want to do it here