Revert "core/state: move slot RLP encoding into the MPT implementation (#27000)"

This reverts commit 797c10068e.
This commit is contained in:
devopsbo3 2023-11-10 12:27:53 -06:00 committed by GitHub
parent 955ff6ee56
commit 74d25c4c98
3 changed files with 21 additions and 35 deletions

View file

@ -185,7 +185,6 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
var (
enc []byte
err error
value common.Hash
)
if s.db.snap != nil {
start := time.Now()
@ -193,13 +192,6 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
if metrics.EnabledExpensive {
s.db.SnapshotStorageReads += time.Since(start)
}
if len(enc) > 0 {
_, content, _, err := rlp.Split(enc)
if err != nil {
s.db.setError(err)
}
value.SetBytes(content)
}
}
// If the snapshot is unavailable or reading from it fails, load from the database.
if s.db.snap == nil || err != nil {
@ -209,7 +201,7 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
s.db.setError(err)
return common.Hash{}
}
val, err := tr.GetStorage(s.address, key.Bytes())
enc, err = tr.GetStorage(s.address, key.Bytes())
if metrics.EnabledExpensive {
s.db.StorageReads += time.Since(start)
}
@ -217,7 +209,14 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
s.db.setError(err)
return common.Hash{}
}
value.SetBytes(val)
}
var value common.Hash
if len(enc) > 0 {
_, content, _, err := rlp.Split(enc)
if err != nil {
s.db.setError(err)
}
value.SetBytes(content)
}
s.originStorage[key] = value
return value
@ -293,8 +292,7 @@ func (s *stateObject) updateTrie(db Database) (Trie, error) {
}
s.originStorage[key] = value
// rlp-encoded value to be used by the snapshot
var snapshotVal []byte
var v []byte
if (value == common.Hash{}) {
if err := tr.DeleteStorage(s.address, key[:]); err != nil {
s.db.setError(err)
@ -302,10 +300,9 @@ func (s *stateObject) updateTrie(db Database) (Trie, error) {
}
s.db.StorageDeleted += 1
} else {
trimmedVal := common.TrimLeftZeroes(value[:])
// Encoding []byte cannot fail, ok to ignore the error.
snapshotVal, _ = rlp.EncodeToBytes(trimmedVal)
if err := tr.UpdateStorage(s.address, key[:], trimmedVal); err != nil {
v, _ = rlp.EncodeToBytes(common.TrimLeftZeroes(value[:]))
if err := tr.UpdateStorage(s.address, key[:], v); err != nil {
s.db.setError(err)
return nil, err
}
@ -320,7 +317,7 @@ func (s *stateObject) updateTrie(db Database) (Trie, error) {
s.db.snapStorage[s.addrHash] = storage
}
}
storage[crypto.HashData(hasher, key[:])] = snapshotVal // will be nil if it's deleted
storage[crypto.HashData(hasher, key[:])] = v // v will be nil if it's deleted
}
usedStorage = append(usedStorage, common.CopyBytes(key[:])) // Copy needed for closure
}

View file

@ -108,16 +108,12 @@ type odrTrie struct {
func (t *odrTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
key = crypto.Keccak256(key)
var enc []byte
var res []byte
err := t.do(key, func() (err error) {
enc, err = t.trie.Get(key)
res, err = t.trie.Get(key)
return err
})
if err != nil || len(enc) == 0 {
return nil, err
}
_, content, _, err := rlp.Split(enc)
return content, err
return res, err
}
func (t *odrTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
@ -149,9 +145,8 @@ func (t *odrTrie) UpdateAccount(address common.Address, acc *types.StateAccount)
func (t *odrTrie) UpdateStorage(_ common.Address, key, value []byte) error {
key = crypto.Keccak256(key)
v, _ := rlp.EncodeToBytes(value)
return t.do(key, func() error {
return t.trie.Update(key, v)
return t.trie.Update(key, value)
})
}

View file

@ -86,12 +86,7 @@ func (t *StateTrie) MustGet(key []byte) []byte {
// If the specified storage slot is not in the trie, nil will be returned.
// If a trie node is not found in the database, a MissingNodeError is returned.
func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
enc, err := t.trie.Get(t.hashKey(key))
if err != nil || len(enc) == 0 {
return nil, err
}
_, content, _, err := rlp.Split(enc)
return content, err
return t.trie.Get(t.hashKey(key))
}
// GetAccount attempts to retrieve an account with provided account address.
@ -153,8 +148,7 @@ func (t *StateTrie) MustUpdate(key, value []byte) {
// If a node is not found in the database, a MissingNodeError is returned.
func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
hk := t.hashKey(key)
v, _ := rlp.EncodeToBytes(value)
err := t.trie.Update(hk, v)
err := t.trie.Update(hk, value)
if err != nil {
return err
}