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 code offset in tree update #157

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,22 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
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
)
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))
}
values[groupOffset] = chunks[i : i+32]

if groupOffset == 255 || len(chunks)-i <= 32 {
s.trie.(*trie.VerkleTrie).TryUpdateStem(key[:31], values)
}
}
}

// If state snapshotting is active, cache the data til commit. Note, this
Expand Down Expand Up @@ -1000,25 +1016,6 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
if obj := s.stateObjects[addr]; !obj.deleted {
// Write any contract code associated with the state object
if obj.code != nil && obj.dirtyCode {
if s.trie.IsVerkle() {
var (
chunks = trie.ChunkifyCode(obj.code)
key []byte
values [][]byte
)
for i := 0; i < len(chunks); i += 32 {
groupOffset := (i / 32) % 256
if groupOffset == 0 {
values = make([][]byte, verkle.NodeWidth)
key = trieUtils.GetTreeKeyCodeChunkWithEvaluatedAddress(obj.pointEval, uint256.NewInt(uint64(i)/32))
}
values[groupOffset] = chunks[i : i+32]

if groupOffset == 255 || len(chunks)-1 <= 32 {
s.trie.(*trie.VerkleTrie).TryUpdateStem(key[:31], values)
}
}
}
rawdb.WriteCode(codeWriter, common.BytesToHash(obj.CodeHash()), obj.code)
obj.dirtyCode = false
}
Expand Down