From 04dccfb25a5f2100016de0a9355001617f5c16d6 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Tue, 7 May 2019 18:25:03 +0800 Subject: [PATCH] trie: use hasher instead of allocate keccack256 every time --- trie/proof.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/trie/proof.go b/trie/proof.go index 5ccf4983b1..491c4ec1d5 100644 --- a/trie/proof.go +++ b/trie/proof.go @@ -21,7 +21,6 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" @@ -81,7 +80,7 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.Writer) error { } else { enc, _ := rlp.EncodeToBytes(n) if !ok { - hash = crypto.Keccak256(enc) + hash = hasher.makeHashNode(enc) } proofDb.Put(hash, enc) } @@ -105,6 +104,9 @@ func (t *SecureTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.Writer) err // key in a trie with the given root hash. VerifyProof returns an error if the // proof contains invalid trie nodes or the wrong value. func VerifyProof(rootHash common.Hash, key []byte, proofDb ethdb.Reader) (value []byte, nodes int, err error) { + hasher := newHasher(nil) + defer returnHasherToPool(hasher) + key = keybytesToHex(key) wantHash := rootHash for i := 0; ; i++ { @@ -112,8 +114,8 @@ func VerifyProof(rootHash common.Hash, key []byte, proofDb ethdb.Reader) (value if buf == nil { return nil, i, fmt.Errorf("proof node %d (hash %064x) missing", i, wantHash) } - if bytes.Compare(crypto.Keccak256(buf), wantHash[:]) != 0 { - return nil, i, fmt.Errorf("proof node %d (hash %64x) invalid", i, wantHash) + if !bytes.Equal(hasher.makeHashNode(buf), wantHash.Bytes()) { + return nil, i, fmt.Errorf("proof node %d (hash %064x) invalid", i, wantHash) } n, err := decodeNode(wantHash[:], buf) if err != nil {