Skip to content

Commit

Permalink
save on key hashing: lump code size update with first code chunk group (
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet authored Feb 7, 2023
1 parent 4dc00e6 commit 8e29b6c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
22 changes: 13 additions & 9 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
trieUtils "github.com/ethereum/go-ethereum/trie/utils"
"github.com/ethereum/go-ethereum/trie/utils"
"github.com/gballet/go-verkle"
"github.com/holiman/uint256"
)
Expand Down Expand Up @@ -523,26 +523,30 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
s.setError(fmt.Errorf("updateStateObject (%x) error: %w", addr[:], err))
}
if s.trie.IsVerkle() {
cs := make([]byte, 32)
binary.LittleEndian.PutUint64(cs, uint64(len(obj.code)))
key := trieUtils.GetTreeKeyCodeSize(addr[:])
if err := s.trie.TryUpdate(key, cs); err != nil {
s.setError(fmt.Errorf("updateStateObject (%x) error: %w", addr[:], err))
}
var (
chunks = trie.ChunkifyCode(obj.code)
values [][]byte
key []byte
)
for i, chunknr := 0, uint64(0); i < len(chunks); i, chunknr = i+32, chunknr+1 {
groupOffset := (chunknr + 128) % 256
if groupOffset == 128 {
values = make([][]byte, verkle.NodeWidth)
key = trieUtils.GetTreeKeyCodeChunkWithEvaluatedAddress(obj.pointEval, uint256.NewInt(chunknr))
key = utils.GetTreeKeyCodeChunkWithEvaluatedAddress(obj.pointEval, uint256.NewInt(chunknr))
}
values[groupOffset] = chunks[i : i+32]

// Reuse the calculated key to also update the code size.
if i == 0 {
cs := make([]byte, 32)
binary.LittleEndian.PutUint64(cs, uint64(len(obj.code)))
values[utils.CodeSizeLeafKey] = cs
}

if groupOffset == 255 || len(chunks)-i <= 32 {
s.trie.(*trie.VerkleTrie).TryUpdateStem(key[:31], values)
if err := s.trie.(*trie.VerkleTrie).TryUpdateStem(key[:31], values); err != nil {
s.setError(fmt.Errorf("updateStateObject (%x) error: %w", addr[:], err))
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ func (t *VerkleTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error
return nil
}

func (trie *VerkleTrie) TryUpdateStem(key []byte, values [][]byte) {
func (trie *VerkleTrie) TryUpdateStem(key []byte, values [][]byte) error {
resolver :=
func(h []byte) ([]byte, error) {
return trie.db.diskdb.Get(h)
}
switch root := trie.root.(type) {
case *verkle.InternalNode:
root.InsertStem(key, values, resolver)
return root.InsertStem(key, values, resolver)
case *verkle.StatelessNode:
root.InsertAtStem(key, values, resolver, true)
return root.InsertAtStem(key, values, resolver, true)
default:
panic("invalid root type")
}
Expand Down

0 comments on commit 8e29b6c

Please sign in to comment.