trie: fixes to comply with golint (#16771)
This commit is contained in:
parent
415969f534
commit
0fe47e98c4
@ -230,7 +230,7 @@ func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error {
|
|||||||
}
|
}
|
||||||
nodeSet := proofs[0].NodeSet()
|
nodeSet := proofs[0].NodeSet()
|
||||||
// Verify the proof and store if checks out
|
// Verify the proof and store if checks out
|
||||||
if _, err, _ := trie.VerifyProof(r.Id.Root, r.Key, nodeSet); err != nil {
|
if _, _, err := trie.VerifyProof(r.Id.Root, r.Key, nodeSet); err != nil {
|
||||||
return fmt.Errorf("merkle proof verification failed: %v", err)
|
return fmt.Errorf("merkle proof verification failed: %v", err)
|
||||||
}
|
}
|
||||||
r.Proof = nodeSet
|
r.Proof = nodeSet
|
||||||
@ -241,7 +241,7 @@ func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error {
|
|||||||
// Verify the proof and store if checks out
|
// Verify the proof and store if checks out
|
||||||
nodeSet := proofs.NodeSet()
|
nodeSet := proofs.NodeSet()
|
||||||
reads := &readTraceDB{db: nodeSet}
|
reads := &readTraceDB{db: nodeSet}
|
||||||
if _, err, _ := trie.VerifyProof(r.Id.Root, r.Key, reads); err != nil {
|
if _, _, err := trie.VerifyProof(r.Id.Root, r.Key, reads); err != nil {
|
||||||
return fmt.Errorf("merkle proof verification failed: %v", err)
|
return fmt.Errorf("merkle proof verification failed: %v", err)
|
||||||
}
|
}
|
||||||
// check if all nodes have been read by VerifyProof
|
// check if all nodes have been read by VerifyProof
|
||||||
@ -400,7 +400,7 @@ func (r *ChtRequest) Validate(db ethdb.Database, msg *Msg) error {
|
|||||||
var encNumber [8]byte
|
var encNumber [8]byte
|
||||||
binary.BigEndian.PutUint64(encNumber[:], r.BlockNum)
|
binary.BigEndian.PutUint64(encNumber[:], r.BlockNum)
|
||||||
|
|
||||||
value, err, _ := trie.VerifyProof(r.ChtRoot, encNumber[:], light.NodeList(proof.Proof).NodeSet())
|
value, _, err := trie.VerifyProof(r.ChtRoot, encNumber[:], light.NodeList(proof.Proof).NodeSet())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -435,7 +435,7 @@ func (r *ChtRequest) Validate(db ethdb.Database, msg *Msg) error {
|
|||||||
binary.BigEndian.PutUint64(encNumber[:], r.BlockNum)
|
binary.BigEndian.PutUint64(encNumber[:], r.BlockNum)
|
||||||
|
|
||||||
reads := &readTraceDB{db: nodeSet}
|
reads := &readTraceDB{db: nodeSet}
|
||||||
value, err, _ := trie.VerifyProof(r.ChtRoot, encNumber[:], reads)
|
value, _, err := trie.VerifyProof(r.ChtRoot, encNumber[:], reads)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("merkle proof verification failed: %v", err)
|
return fmt.Errorf("merkle proof verification failed: %v", err)
|
||||||
}
|
}
|
||||||
@ -529,7 +529,7 @@ func (r *BloomRequest) Validate(db ethdb.Database, msg *Msg) error {
|
|||||||
|
|
||||||
for i, idx := range r.SectionIdxList {
|
for i, idx := range r.SectionIdxList {
|
||||||
binary.BigEndian.PutUint64(encNumber[2:], idx)
|
binary.BigEndian.PutUint64(encNumber[2:], idx)
|
||||||
value, err, _ := trie.VerifyProof(r.BloomTrieRoot, encNumber[:], reads)
|
value, _, err := trie.VerifyProof(r.BloomTrieRoot, encNumber[:], reads)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -99,8 +99,8 @@ type nodeIterator struct {
|
|||||||
err error // Failure set in case of an internal error in the iterator
|
err error // Failure set in case of an internal error in the iterator
|
||||||
}
|
}
|
||||||
|
|
||||||
// iteratorEnd is stored in nodeIterator.err when iteration is done.
|
// errIteratorEnd is stored in nodeIterator.err when iteration is done.
|
||||||
var iteratorEnd = errors.New("end of iteration")
|
var errIteratorEnd = errors.New("end of iteration")
|
||||||
|
|
||||||
// seekError is stored in nodeIterator.err if the initial seek has failed.
|
// seekError is stored in nodeIterator.err if the initial seek has failed.
|
||||||
type seekError struct {
|
type seekError struct {
|
||||||
@ -162,7 +162,7 @@ func (it *nodeIterator) Path() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (it *nodeIterator) Error() error {
|
func (it *nodeIterator) Error() error {
|
||||||
if it.err == iteratorEnd {
|
if it.err == errIteratorEnd {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if seek, ok := it.err.(seekError); ok {
|
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,
|
// sets the Error field to the encountered failure. If `descend` is false,
|
||||||
// skips iterating over any subnodes of the current node.
|
// skips iterating over any subnodes of the current node.
|
||||||
func (it *nodeIterator) Next(descend bool) bool {
|
func (it *nodeIterator) Next(descend bool) bool {
|
||||||
if it.err == iteratorEnd {
|
if it.err == errIteratorEnd {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if seek, ok := it.err.(seekError); ok {
|
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.
|
// Move forward until we're just before the closest match to key.
|
||||||
for {
|
for {
|
||||||
state, parentIndex, path, err := it.peek(bytes.HasPrefix(key, it.path))
|
state, parentIndex, path, err := it.peek(bytes.HasPrefix(key, it.path))
|
||||||
if err == iteratorEnd {
|
if err == errIteratorEnd {
|
||||||
return iteratorEnd
|
return errIteratorEnd
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return seekError{prefix, err}
|
return seekError{prefix, err}
|
||||||
} else if bytes.Compare(path, key) >= 0 {
|
} 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.
|
// No more child nodes, move back up.
|
||||||
it.pop()
|
it.pop()
|
||||||
}
|
}
|
||||||
return nil, nil, nil, iteratorEnd
|
return nil, nil, nil, errIteratorEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (st *nodeIteratorState) resolve(tr *Trie, path []byte) error {
|
func (st *nodeIteratorState) resolve(tr *Trie, path []byte) error {
|
||||||
|
@ -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
|
// 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
|
// key in a trie with the given root hash. VerifyProof returns an error if the
|
||||||
// proof contains invalid trie nodes or the wrong value.
|
// 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)
|
key = keybytesToHex(key)
|
||||||
wantHash := rootHash
|
wantHash := rootHash
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
buf, _ := proofDb.Get(wantHash[:])
|
buf, _ := proofDb.Get(wantHash[:])
|
||||||
if buf == nil {
|
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)
|
n, err := decodeNode(wantHash[:], buf, 0)
|
||||||
if err != nil {
|
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)
|
keyrest, cld := get(n, key)
|
||||||
switch cld := cld.(type) {
|
switch cld := cld.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
// The trie doesn't contain the key.
|
// The trie doesn't contain the key.
|
||||||
return nil, nil, i
|
return nil, i, nil
|
||||||
case hashNode:
|
case hashNode:
|
||||||
key = keyrest
|
key = keyrest
|
||||||
copy(wantHash[:], cld)
|
copy(wantHash[:], cld)
|
||||||
case valueNode:
|
case valueNode:
|
||||||
return cld, nil, i + 1
|
return cld, i + 1, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ func TestProof(t *testing.T) {
|
|||||||
if trie.Prove(kv.k, 0, proofs) != nil {
|
if trie.Prove(kv.k, 0, proofs) != nil {
|
||||||
t.Fatalf("missing key %x while constructing proof", kv.k)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("VerifyProof error for key %x: %v\nraw proof: %v", kv.k, err, proofs)
|
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 {
|
if len(proofs.Keys()) != 1 {
|
||||||
t.Error("proof should have one element")
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("VerifyProof error: %v\nproof hashes: %v", err, proofs.Keys())
|
t.Fatalf("VerifyProof error: %v\nproof hashes: %v", err, proofs.Keys())
|
||||||
}
|
}
|
||||||
@ -82,7 +82,7 @@ func TestVerifyBadProof(t *testing.T) {
|
|||||||
proofs.Delete(key)
|
proofs.Delete(key)
|
||||||
mutateByte(node)
|
mutateByte(node)
|
||||||
proofs.Put(crypto.Keccak256(node), 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)
|
t.Fatalf("expected proof to fail for key %x", kv.k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,7 +131,7 @@ func BenchmarkVerifyProof(b *testing.B) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
im := i % len(keys)
|
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)
|
b.Fatalf("key %x: %v", keys[im], err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -155,14 +155,19 @@ func (t *SecureTrie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
|||||||
return t.trie.Commit(onleaf)
|
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 {
|
func (t *SecureTrie) Hash() common.Hash {
|
||||||
return t.trie.Hash()
|
return t.trie.Hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Root returns the root hash of SecureTrie.
|
||||||
|
// Deprecated: use Hash instead.
|
||||||
func (t *SecureTrie) Root() []byte {
|
func (t *SecureTrie) Root() []byte {
|
||||||
return t.trie.Root()
|
return t.trie.Root()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Copy returns a copy of SecureTrie.
|
||||||
func (t *SecureTrie) Copy() *SecureTrie {
|
func (t *SecureTrie) Copy() *SecureTrie {
|
||||||
cpy := *t
|
cpy := *t
|
||||||
return &cpy
|
return &cpy
|
||||||
|
Loading…
Reference in New Issue
Block a user