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

HashBuilder: fix top node shorter than 32 bytes #6456

Merged
merged 3 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions core/types/withdrawal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package types

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/u256"
)

func TestWithdrawalsHash(t *testing.T) {
w := &Withdrawal{
Index: 0,
Validator: 0,
Address: common.HexToAddress("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
Amount: *u256.Num1,
}
withdrawals := Withdrawals([]*Withdrawal{w})
hash := DeriveSha(withdrawals)
// The only trie node is short (its RLP < 32 bytes).
// Its Keccak should be returned, not the node itself.
assert.Equal(t, common.HexToHash("82cc6fbe74c41496b382fcdf25216c5af7bdbb5a3929e8f2e61bd6445ab66436"), hash)
}
3 changes: 2 additions & 1 deletion tests/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/eth/tracers/logger"
"github.com/ledgerwatch/log/v3"
)

Expand Down Expand Up @@ -86,7 +87,7 @@ func withTrace(t *testing.T, test func(vm.Config) error) {
t.Error(err)
buf := new(bytes.Buffer)
w := bufio.NewWriter(buf)
tracer := vm.NewJSONLogger(&vm.LogConfig{DisableMemory: true}, w)
tracer := logger.NewJSONLogger(&logger.LogConfig{DisableMemory: true}, w)
config.Debug, config.Tracer = true, tracer
err2 := test(config)
if !reflect.DeepEqual(err, err2) {
Expand Down
4 changes: 2 additions & 2 deletions turbo/trie/gen_struct_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func GenStructStep(
send := maxLen == 0 && (hasTree[maxLen] != 0 || hasHash[maxLen] != 0) // account.root - store only if have useful info
send = send || (maxLen == 1 && groups[maxLen] != 0) // first level of trie_account - store in any case
if send {
if err := h(curr[:maxLen], groups[maxLen], hasTree[maxLen], hasHash[maxLen], usefulHashes, e.topHash()); err != nil {
if err := h(curr[:maxLen], groups[maxLen], hasTree[maxLen], hasHash[maxLen], usefulHashes, e.topHash()[1:]); err != nil {
return nil, nil, nil, err
}
}
Expand Down Expand Up @@ -412,7 +412,7 @@ func GenStructStepOld(
}
}
if h != nil {
if err := h(curr[:maxLen], e.topHash()); err != nil {
if err := h(curr[:maxLen], e.topHash()[1:]); err != nil {
return nil, err
}
}
Expand Down
21 changes: 19 additions & 2 deletions turbo/trie/hashbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,12 +598,29 @@ func (hb *HashBuilder) RootHash() (common.Hash, error) {

func (hb *HashBuilder) rootHash() common.Hash {
var hash common.Hash
copy(hash[:], hb.topHash())
top := hb.topHash()
if len(top) == 33 {
copy(hash[:], top[1:])
} else {
hb.sha.Reset()
if _, err := hb.sha.Write(top); err != nil {
panic(err)
}
if _, err := hb.sha.Read(hash[:]); err != nil {
panic(err)
}
}
return hash
}

func (hb *HashBuilder) topHash() []byte {
return hb.hashStack[len(hb.hashStack)-hashStackStride+1:]
pos := len(hb.hashStack) - hashStackStride
len := hb.hashStack[pos] - 0x80
if len > 32 {
// node itself (RLP list), not its hash
len = hb.hashStack[pos] - 0xc0
}
return hb.hashStack[pos : pos+1+int(len)]
}

func (hb *HashBuilder) printTopHashes(prefix []byte, _, children uint16) {
Expand Down