From c934699c0b4379f9d6bbabed9448ef7e7499f4dc Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 6 Jan 2020 21:33:36 +0100 Subject: [PATCH] trie: start deprecating old hasher --- trie/hasher.go | 23 +++-------------------- trie/pure_hasher.go | 20 ++++++++++++++++++++ trie/secure_trie.go | 4 ++-- trie/trie.go | 10 ---------- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/trie/hasher.go b/trie/hasher.go index 210fd81c9c..110ab36e2a 100644 --- a/trie/hasher.go +++ b/trie/hasher.go @@ -17,7 +17,6 @@ package trie import ( - "hash" "sync" "github.com/ethereum/go-ethereum/common" @@ -25,31 +24,15 @@ import ( "golang.org/x/crypto/sha3" ) +// @deprecated +// hasher is the old hash+commit utility, replaced by dedicated +// hasher (pure_hasher) and committer (pure_commit) type hasher struct { tmp sliceBuffer sha keccakState onleaf LeafCallback } -// keccakState wraps sha3.state. In addition to the usual hash methods, it also supports -// Read to get a variable amount of data from the hash state. Read is faster than Sum -// because it doesn't copy the internal state, but also modifies the internal state. -type keccakState interface { - hash.Hash - Read([]byte) (int, error) -} - -type sliceBuffer []byte - -func (b *sliceBuffer) Write(data []byte) (n int, err error) { - *b = append(*b, data...) - return len(data), nil -} - -func (b *sliceBuffer) Reset() { - *b = (*b)[:0] -} - // hashers live in a global db. var hasherPool = sync.Pool{ New: func() interface{} { diff --git a/trie/pure_hasher.go b/trie/pure_hasher.go index f6d536db5c..facfbe1588 100644 --- a/trie/pure_hasher.go +++ b/trie/pure_hasher.go @@ -17,12 +17,32 @@ package trie import ( + "hash" "sync" "github.com/ethereum/go-ethereum/rlp" "golang.org/x/crypto/sha3" ) +// keccakState wraps sha3.state. In addition to the usual hash methods, it also supports +// Read to get a variable amount of data from the hash state. Read is faster than Sum +// because it doesn't copy the internal state, but also modifies the internal state. +type keccakState interface { + hash.Hash + Read([]byte) (int, error) +} + +type sliceBuffer []byte + +func (b *sliceBuffer) Write(data []byte) (n int, err error) { + *b = append(*b, data...) + return len(data), nil +} + +func (b *sliceBuffer) Reset() { + *b = (*b)[:0] +} + // pureHasher is a type used for the trie Hash operation. A pureHasher has some // internal preallocated temp space type pureHasher struct { diff --git a/trie/secure_trie.go b/trie/secure_trie.go index fbc591ed10..abf2df8d58 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -176,11 +176,11 @@ func (t *SecureTrie) NodeIterator(start []byte) NodeIterator { // The caller must not hold onto the return value because it will become // invalid on the next call to hashKey or secKey. func (t *SecureTrie) hashKey(key []byte) []byte { - h := newHasher(nil) + h := newPureHasher() h.sha.Reset() h.sha.Write(key) buf := h.sha.Sum(t.hashKeyBuf[:0]) - returnHasherToPool(h) + returnPureHasherToPool(h) return buf } diff --git a/trie/trie.go b/trie/trie.go index 78e7d724f5..358bcbfc1d 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -451,16 +451,6 @@ func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) { return rootHash, nil } -// oldHashRoot is the old implementation of hashRoot, which uses the regular hasher -func (t *Trie) oldHashRoot(db *Database, onleaf LeafCallback) (node, node, error) { - if t.root == nil { - return hashNode(emptyRoot.Bytes()), nil, nil - } - h := newHasher(onleaf) - defer returnHasherToPool(h) - return h.hash(t.root, db, true) -} - // hashRoot calculates the root hash of the given trie func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (node, node, error) { if t.root == nil {