Skip to content

Commit

Permalink
fix code chunk key calculation and storage key calculation (ethereum#161
Browse files Browse the repository at this point in the history
)

* fix codeKey calculation

* Remove

* fix storageOffset

* fix the fix to the fix to the offset fix

* Remove copy/pasted, unused code in test

* fix linter

---------

Co-authored-by: Guillaume Ballet <[email protected]>
  • Loading branch information
tanishqjasoria and gballet authored Feb 8, 2023
1 parent 8e29b6c commit ecf1a20
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
)
for i, chunknr := 0, uint64(0); i < len(chunks); i, chunknr = i+32, chunknr+1 {
groupOffset := (chunknr + 128) % 256
if groupOffset == 128 {
if groupOffset == 0 /* start of new group */ || chunknr == 0 /* first chunk in header group */ {
values = make([][]byte, verkle.NodeWidth)
key = utils.GetTreeKeyCodeChunkWithEvaluatedAddress(obj.pointEval, uint256.NewInt(chunknr))
}
Expand Down
25 changes: 25 additions & 0 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie"
)

// Tests that updating a state trie does not leak any database writes prior to
Expand Down Expand Up @@ -957,3 +958,27 @@ func TestFlushOrderDataLoss(t *testing.T) {
}
}
}

// Test that an account with more than 128 pieces of code overflows
// correctly into the next group.
func TestCodeChunkOverflow(t *testing.T) {
// Create an empty state database
db := rawdb.NewMemoryDatabase()
state, _ := New(common.Hash{}, NewDatabaseWithConfig(db, &trie.Config{UseVerkle: true}), nil)

// Update it with some accounts
addr := common.BytesToAddress([]byte{1})
state.AddBalance(addr, big.NewInt(int64(11)))
state.SetNonce(addr, uint64(42))
state.SetState(addr, common.BytesToHash([]byte{1, 1, 1}), common.BytesToHash([]byte{1, 1, 1, 1}))
code := make([]byte, 31*256)
for i := range code {
code[i] = 1
}
state.SetCode(addr, code)

root := state.IntermediateRoot(false)
if err := state.Database().TrieDB().Commit(root, false, nil); err != nil {
t.Errorf("can not commit trie %v to persistent database", root.Hex())
}
}
17 changes: 8 additions & 9 deletions trie/utils/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ const (
)

var (
zero = uint256.NewInt(0)
HeaderStorageOffset = uint256.NewInt(64)
CodeOffset = uint256.NewInt(128)
MainStorageOffset = new(uint256.Int).Lsh(uint256.NewInt(256), 31)
VerkleNodeWidth = uint256.NewInt(256)
codeStorageDelta = uint256.NewInt(0).Sub(CodeOffset, HeaderStorageOffset)
MainStorageMinusStorageDeltaOffset = new(uint256.Int).Sub(MainStorageOffset, codeStorageDelta)
zero = uint256.NewInt(0)
HeaderStorageOffset = uint256.NewInt(64)
CodeOffset = uint256.NewInt(128)
MainStorageOffset = new(uint256.Int).Lsh(uint256.NewInt(256), 31)
VerkleNodeWidth = uint256.NewInt(256)
codeStorageDelta = uint256.NewInt(0).Sub(CodeOffset, HeaderStorageOffset)

getTreePolyIndex0Point *verkle.Point
)
Expand Down Expand Up @@ -146,7 +145,7 @@ func GetTreeKeyStorageSlot(address []byte, storageKey *uint256.Int) []byte {
if storageKey.Cmp(codeStorageDelta) < 0 {
pos.Add(HeaderStorageOffset, storageKey)
} else {
pos.Add(MainStorageMinusStorageDeltaOffset, storageKey)
pos.Add(MainStorageOffset, storageKey)
}
treeIndex := new(uint256.Int).Div(pos, VerkleNodeWidth)

Expand Down Expand Up @@ -229,7 +228,7 @@ func GetTreeKeyStorageSlotWithEvaluatedAddress(evaluated *verkle.Point, storageK
if storageKey.Cmp(codeStorageDelta) < 0 {
pos.Add(HeaderStorageOffset, storageKey)
} else {
pos.Add(MainStorageMinusStorageDeltaOffset, storageKey)
pos.Add(MainStorageOffset, storageKey)
}
treeIndex := new(uint256.Int).Div(pos, VerkleNodeWidth)
// calculate the sub_index, i.e. the index in the stem tree.
Expand Down

0 comments on commit ecf1a20

Please sign in to comment.