6c8310ebb4
core/types: use stacktrie for derivesha trie: add stacktrie file trie: fix linter core/types: use stacktrie for derivesha rebased: adapt stacktrie to the newer version of DeriveSha Co-authored-by: Martin Holst Swende <martin@swende.se> More linter fixes review feedback: no key offset for nodes converted to hashes trie: use EncodeRLP for full nodes core/types: insert txs in order in derivesha trie: tests for derivesha with stacktrie trie: make stacktrie use pooled hashers trie: make stacktrie reuse tmp slice space trie: minor polishes on stacktrie trie/stacktrie: less rlp dancing core/types: explain the contorsions in DeriveSha ci: fix goimport errors trie: clear mem on subtrie hashing squashme: linter fix stracktrie: use pooling, less allocs (#3) trie: in-place hex prefix, reduce allocs and add rawNode.EncodeRLP Reintroduce the `[]node` method, add the missing `EncodeRLP` implementation for `rawNode` and calculate the hex prefix in place. Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Martin Holst Swende <martin@swende.se>
63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
// Copyright 2014 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package types
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
)
|
|
|
|
type DerivableList interface {
|
|
Len() int
|
|
GetRlp(i int) []byte
|
|
}
|
|
|
|
// Hasher is the tool used to calculate the hash of derivable list.
|
|
type Hasher interface {
|
|
Reset()
|
|
Update([]byte, []byte)
|
|
Hash() common.Hash
|
|
}
|
|
|
|
func DeriveSha(list DerivableList, hasher Hasher) common.Hash {
|
|
hasher.Reset()
|
|
keybuf := new(bytes.Buffer)
|
|
|
|
// StackTrie requires values to be inserted in increasing
|
|
// hash order, which is not the order that `list` provides
|
|
// hashes in. This insertion sequence ensures that the
|
|
// order is correct.
|
|
for i := 1; i < list.Len() && i <= 0x7f; i++ {
|
|
keybuf.Reset()
|
|
rlp.Encode(keybuf, uint(i))
|
|
hasher.Update(keybuf.Bytes(), list.GetRlp(i))
|
|
}
|
|
if list.Len() > 0 {
|
|
keybuf.Reset()
|
|
rlp.Encode(keybuf, uint(0))
|
|
hasher.Update(keybuf.Bytes(), list.GetRlp(0))
|
|
}
|
|
for i := 0x80; i < list.Len(); i++ {
|
|
keybuf.Reset()
|
|
rlp.Encode(keybuf, uint(i))
|
|
hasher.Update(keybuf.Bytes(), list.GetRlp(i))
|
|
}
|
|
return hasher.Hash()
|
|
}
|