Skip to content

Commit

Permalink
trie: fix closure error, work around upstream Go issue
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed May 9, 2024
1 parent 9a4e5db commit a397c9b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Trie interface {
GetStorage(addr common.Address, key []byte) ([]byte, error)

// GetStorageBatch is a batched version of GetStorage that simultaneously looks
// up multiple slots. The advantage vs. teh singleton version is the potential
// up multiple slots. The advantage vs. the singleton version is the potential
// for concurrent disk lookups.
GetStorageBatch(addrs []common.Address, keys [][]byte) ([][]byte, error)

Expand Down
4 changes: 2 additions & 2 deletions trie/secure_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
func (t *StateTrie) GetStorageBatch(_ []common.Address, keys [][]byte) ([][]byte, error) {
hashes := make([][]byte, len(keys))
for i, key := range keys {
hashes[i] = t.hashKey(key)
hashes[i] = common.CopyBytes(t.hashKey(key))
}
encs, err := t.trie.GetBatch(hashes)
if err != nil {
Expand Down Expand Up @@ -138,7 +138,7 @@ func (t *StateTrie) GetAccount(address common.Address) (*types.StateAccount, err
func (t *StateTrie) GetAccountBatch(addrs []common.Address) ([]*types.StateAccount, error) {
hashes := make([][]byte, len(addrs))
for i, addr := range addrs {
hashes[i] = t.hashKey(addr.Bytes())
hashes[i] = common.CopyBytes(t.hashKey(addr.Bytes()))
}
encs, err := t.trie.GetBatch(hashes)
if err != nil {
Expand Down
27 changes: 23 additions & 4 deletions trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,29 @@ func (t *Trie) getBatch(origNode node, keys [][]byte, pos int) ([][]byte, node,
n = n.copy()
n.Val = newnode
}
values = append(make([][]byte, first), values...)
values = append(values, make([][]byte, len(keys)-last)...)

return values, n, didResolve, err
if first == 0 && last == len(keys) {
// If all the keys are the same (or there's only 1 key), return the
// result values without padding them on the two sides with nils.
//
// A bit of a weird clause, but this is the default, it is possibly
// insiginificantly faster, and it avoids a rare Go crash prior to
// v1.22.4 per https://github.com/golang/go/issues/67255.
//
// TODO(karalabe): Simplify when Go v1.24.0 is out.
return values, n, didResolve, err
} else {
// Only a subset of the keys are existent in the trie, pad the value
// return slice with nils for the rest.
//
// We could also use append here, but that can potentially hit the
// above Go issue: https://github.com/golang/go/issues/67255.
//
// TODO(karalabe): Simplify when Go v1.24.0 is out.
result := make([][]byte, len(keys))
copy(result[:first], values)

return values, n, didResolve, err
}

case *fullNode:
var (
Expand Down

0 comments on commit a397c9b

Please sign in to comment.