diff options
author | kiel barry <kiel.j.barry@gmail.com> | 2018-05-22 04:41:31 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2018-05-22 04:41:31 +0800 |
commit | 0fe47e98c4d93f952dc7415f401fd03b2664a21d (patch) | |
tree | ae30e450e853fe8a03760496b87259077a760e68 /trie | |
parent | 415969f534d8122e717e18abafdc8ec7bb913a84 (diff) | |
download | dexon-0fe47e98c4d93f952dc7415f401fd03b2664a21d.tar dexon-0fe47e98c4d93f952dc7415f401fd03b2664a21d.tar.gz dexon-0fe47e98c4d93f952dc7415f401fd03b2664a21d.tar.bz2 dexon-0fe47e98c4d93f952dc7415f401fd03b2664a21d.tar.lz dexon-0fe47e98c4d93f952dc7415f401fd03b2664a21d.tar.xz dexon-0fe47e98c4d93f952dc7415f401fd03b2664a21d.tar.zst dexon-0fe47e98c4d93f952dc7415f401fd03b2664a21d.zip |
trie: fixes to comply with golint (#16771)
Diffstat (limited to 'trie')
-rw-r--r-- | trie/iterator.go | 14 | ||||
-rw-r--r-- | trie/proof.go | 10 | ||||
-rw-r--r-- | trie/proof_test.go | 8 | ||||
-rw-r--r-- | trie/secure_trie.go | 5 |
4 files changed, 21 insertions, 16 deletions
diff --git a/trie/iterator.go b/trie/iterator.go index 3bae8e186..64110c6d9 100644 --- a/trie/iterator.go +++ b/trie/iterator.go @@ -99,8 +99,8 @@ type nodeIterator struct { err error // Failure set in case of an internal error in the iterator } -// iteratorEnd is stored in nodeIterator.err when iteration is done. -var iteratorEnd = errors.New("end of iteration") +// errIteratorEnd is stored in nodeIterator.err when iteration is done. +var errIteratorEnd = errors.New("end of iteration") // seekError is stored in nodeIterator.err if the initial seek has failed. type seekError struct { @@ -162,7 +162,7 @@ func (it *nodeIterator) Path() []byte { } func (it *nodeIterator) Error() error { - if it.err == iteratorEnd { + if it.err == errIteratorEnd { return nil } if seek, ok := it.err.(seekError); ok { @@ -176,7 +176,7 @@ func (it *nodeIterator) Error() error { // sets the Error field to the encountered failure. If `descend` is false, // skips iterating over any subnodes of the current node. func (it *nodeIterator) Next(descend bool) bool { - if it.err == iteratorEnd { + if it.err == errIteratorEnd { return false } if seek, ok := it.err.(seekError); ok { @@ -201,8 +201,8 @@ func (it *nodeIterator) seek(prefix []byte) error { // Move forward until we're just before the closest match to key. for { state, parentIndex, path, err := it.peek(bytes.HasPrefix(key, it.path)) - if err == iteratorEnd { - return iteratorEnd + if err == errIteratorEnd { + return errIteratorEnd } else if err != nil { return seekError{prefix, err} } else if bytes.Compare(path, key) >= 0 { @@ -246,7 +246,7 @@ func (it *nodeIterator) peek(descend bool) (*nodeIteratorState, *int, []byte, er // No more child nodes, move back up. it.pop() } - return nil, nil, nil, iteratorEnd + return nil, nil, nil, errIteratorEnd } func (st *nodeIteratorState) resolve(tr *Trie, path []byte) error { diff --git a/trie/proof.go b/trie/proof.go index 508e4a6cf..6cb8f4d5f 100644 --- a/trie/proof.go +++ b/trie/proof.go @@ -102,28 +102,28 @@ func (t *SecureTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.Putter) err // VerifyProof checks merkle proofs. The given proof must contain the value for // 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 DatabaseReader) (value []byte, err error, nodes int) { +func VerifyProof(rootHash common.Hash, key []byte, proofDb DatabaseReader) (value []byte, nodes int, err error) { key = keybytesToHex(key) wantHash := rootHash for i := 0; ; i++ { buf, _ := proofDb.Get(wantHash[:]) if buf == nil { - return nil, fmt.Errorf("proof node %d (hash %064x) missing", i, wantHash), i + return nil, i, fmt.Errorf("proof node %d (hash %064x) missing", i, wantHash) } n, err := decodeNode(wantHash[:], buf, 0) if err != nil { - return nil, fmt.Errorf("bad proof node %d: %v", i, err), i + return nil, i, fmt.Errorf("bad proof node %d: %v", i, err) } keyrest, cld := get(n, key) switch cld := cld.(type) { case nil: // The trie doesn't contain the key. - return nil, nil, i + return nil, i, nil case hashNode: key = keyrest copy(wantHash[:], cld) case valueNode: - return cld, nil, i + 1 + return cld, i + 1, nil } } } diff --git a/trie/proof_test.go b/trie/proof_test.go index a3537787c..dee6f7d85 100644 --- a/trie/proof_test.go +++ b/trie/proof_test.go @@ -40,7 +40,7 @@ func TestProof(t *testing.T) { if trie.Prove(kv.k, 0, proofs) != nil { t.Fatalf("missing key %x while constructing proof", kv.k) } - val, err, _ := VerifyProof(root, kv.k, proofs) + val, _, err := VerifyProof(root, kv.k, proofs) if err != nil { t.Fatalf("VerifyProof error for key %x: %v\nraw proof: %v", kv.k, err, proofs) } @@ -58,7 +58,7 @@ func TestOneElementProof(t *testing.T) { if len(proofs.Keys()) != 1 { t.Error("proof should have one element") } - val, err, _ := VerifyProof(trie.Hash(), []byte("k"), proofs) + val, _, err := VerifyProof(trie.Hash(), []byte("k"), proofs) if err != nil { t.Fatalf("VerifyProof error: %v\nproof hashes: %v", err, proofs.Keys()) } @@ -82,7 +82,7 @@ func TestVerifyBadProof(t *testing.T) { proofs.Delete(key) mutateByte(node) proofs.Put(crypto.Keccak256(node), node) - if _, err, _ := VerifyProof(root, kv.k, proofs); err == nil { + if _, _, err := VerifyProof(root, kv.k, proofs); err == nil { t.Fatalf("expected proof to fail for key %x", kv.k) } } @@ -131,7 +131,7 @@ func BenchmarkVerifyProof(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { im := i % len(keys) - if _, err, _ := VerifyProof(root, []byte(keys[im]), proofs[im]); err != nil { + if _, _, err := VerifyProof(root, []byte(keys[im]), proofs[im]); err != nil { b.Fatalf("key %x: %v", keys[im], err) } } diff --git a/trie/secure_trie.go b/trie/secure_trie.go index 3881ee18a..6a50cfd5a 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -155,14 +155,19 @@ func (t *SecureTrie) Commit(onleaf LeafCallback) (root common.Hash, err error) { return t.trie.Commit(onleaf) } +// Hash returns the root hash of SecureTrie. It does not write to the +// database and can be used even if the trie doesn't have one. func (t *SecureTrie) Hash() common.Hash { return t.trie.Hash() } +// Root returns the root hash of SecureTrie. +// Deprecated: use Hash instead. func (t *SecureTrie) Root() []byte { return t.trie.Root() } +// Copy returns a copy of SecureTrie. func (t *SecureTrie) Copy() *SecureTrie { cpy := *t return &cpy |