Skip to content
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(state) : Update StorageState to load storage from database. #1486

Merged
merged 7 commits into from
Mar 24, 2021
Merged
78 changes: 44 additions & 34 deletions dot/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,9 @@ func (s *StorageState) LoadFromDB(root common.Hash) (*trie.Trie, error) {

// 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(hash *common.Hash, key []byte) (bool, error) {
if hash == nil {
sr, err := s.blockState.BestBlockStateRoot()
if err != nil {
return false, err
}
hash = &sr
}

s.lock.RLock()
defer s.lock.RUnlock()
val := s.tries[*hash].Get(key)
return val != nil, nil
func (s *StorageState) ExistsStorage(root *common.Hash, key []byte) (bool, error) {
val, err := s.GetStorage(root, key)
return val != nil, err
}

// GetStorage gets the object from the trie using the given key and storage hash
Expand All @@ -233,8 +223,8 @@ func (s *StorageState) GetStorage(root *common.Hash, key []byte) ([]byte, error)
s.lock.RLock()
defer s.lock.RUnlock()

if s.tries[*root] != nil {
val := s.tries[*root].Get(key)
if trie, ok := s.tries[*root]; ok {
val := trie.Get(key)
return val, nil
}

Expand Down Expand Up @@ -270,11 +260,15 @@ func (s *StorageState) StorageRoot() (common.Hash, error) {
s.lock.RLock()
defer s.lock.RUnlock()

if s.tries[sr] == nil {
return common.Hash{}, errTrieDoesNotExist(sr)
t, ok := s.tries[sr]
if !ok {
t, err = s.LoadFromDB(sr)
if err != nil {
return common.Hash{}, errTrieDoesNotExist(sr)
}
}

return s.tries[sr].Hash()
return t.Hash()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function can be updated to just return s.blockState.BestBlockStateRoot() since it should be returning the current state root

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

// EnumeratedTrieRoot not implemented
Expand All @@ -296,13 +290,13 @@ func (s *StorageState) Entries(root *common.Hash) (map[string][]byte, error) {
s.lock.RLock()
defer s.lock.RUnlock()

if s.tries[*root] != nil {
return s.tries[*root].Entries(), nil
}

tr, err := s.LoadFromDB(*root)
if err != nil {
return nil, err
tr, ok := s.tries[*root]
if !ok {
var err error
tr, err = s.LoadFromDB(*root)
if err != nil {
return nil, errTrieDoesNotExist(*root)
}
}

return tr.Entries(), nil
Expand All @@ -317,9 +311,14 @@ func (s *StorageState) GetKeysWithPrefix(hash *common.Hash, prefix []byte) ([][]
}
hash = &sr
}
t := s.tries[*hash]
if t == nil {
return nil, fmt.Errorf("unable to retrieve trie with hash %x", *hash)

t, ok := s.tries[*hash]
if !ok {
var err error
t, err = s.LoadFromDB(*hash)
if err != nil {
return nil, errTrieDoesNotExist(*hash)
}
}
return t.GetKeysWithPrefix(prefix), nil
}
Expand All @@ -337,11 +336,16 @@ func (s *StorageState) GetStorageChild(hash *common.Hash, keyToChild []byte) (*t
s.lock.RLock()
defer s.lock.RUnlock()

if s.tries[*hash] == nil {
return nil, errTrieDoesNotExist(*hash)
t, ok := s.tries[*hash]
if !ok {
var err error
t, err = s.LoadFromDB(*hash)
if err != nil {
return nil, errTrieDoesNotExist(*hash)
}
}

return s.tries[*hash].GetChild(keyToChild)
return t.GetChild(keyToChild)
}

// GetStorageFromChild return GetFromChild from the trie
Expand All @@ -357,10 +361,16 @@ func (s *StorageState) GetStorageFromChild(hash *common.Hash, keyToChild, key []
s.lock.RLock()
defer s.lock.RUnlock()

if s.tries[*hash] == nil {
return nil, errTrieDoesNotExist(*hash)
t, ok := s.tries[*hash]
if !ok {
var err error
t, err = s.LoadFromDB(*hash)
if err != nil {
return nil, errTrieDoesNotExist(*hash)
}
}
return s.tries[*hash].GetFromChild(keyToChild, key)

return t.GetFromChild(keyToChild, key)
}

// LoadCode returns the runtime code (located at :code)
Expand Down