Skip to content

Commit

Permalink
core/rawdb: allocate database keys with explicit size to avoid slice …
Browse files Browse the repository at this point in the history
…growth (ethereum#27772)
  • Loading branch information
MariusVanDerWijden authored and joeylichang committed Oct 16, 2023
1 parent dda8c00 commit 959b733
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ func accountSnapshotKey(hash common.Hash) []byte {

// storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
buf := make([]byte, len(SnapshotStoragePrefix)+common.HashLength+common.HashLength)
n := copy(buf, SnapshotStoragePrefix)
n += copy(buf[n:], accountHash.Bytes())
copy(buf[n:], storageHash.Bytes())
return buf
}

// storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
Expand Down Expand Up @@ -274,7 +278,11 @@ func accountTrieNodeKey(path []byte) []byte {

// storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath.
func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...)
buf := make([]byte, len(trieNodeStoragePrefix)+common.HashLength+len(path))
n := copy(buf, trieNodeStoragePrefix)
n += copy(buf[n:], accountHash.Bytes())
copy(buf[n:], path)
return buf
}

// IsLegacyTrieNode reports whether a provided database entry is a legacy trie
Expand Down

0 comments on commit 959b733

Please sign in to comment.