trie: support proof generation from the iterator

This commit is contained in:
Péter Szilágyi 2018-05-10 12:49:27 +03:00
parent fbf57d53e2
commit c934c06cc1
No known key found for this signature in database
GPG Key ID: E9AE538CEDF8293D
2 changed files with 150 additions and 48 deletions

View File

@ -22,6 +22,7 @@ import (
"errors" "errors"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
) )
// Iterator is a key-value trie iterator that traverses a Trie. // Iterator is a key-value trie iterator that traverses a Trie.
@ -55,31 +56,50 @@ func (it *Iterator) Next() bool {
return false return false
} }
// Prove generates the Merkle proof for the leaf node the iterator is currently
// positioned on.
func (it *Iterator) Prove() [][]byte {
return it.nodeIt.LeafProof()
}
// NodeIterator is an iterator to traverse the trie pre-order. // NodeIterator is an iterator to traverse the trie pre-order.
type NodeIterator interface { type NodeIterator interface {
// Next moves the iterator to the next node. If the parameter is false, any child // Next moves the iterator to the next node. If the parameter is false, any child
// nodes will be skipped. // nodes will be skipped.
Next(bool) bool Next(bool) bool
// Error returns the error status of the iterator. // Error returns the error status of the iterator.
Error() error Error() error
// Hash returns the hash of the current node. // Hash returns the hash of the current node.
Hash() common.Hash Hash() common.Hash
// Parent returns the hash of the parent of the current node. The hash may be the one // Parent returns the hash of the parent of the current node. The hash may be the one
// grandparent if the immediate parent is an internal node with no hash. // grandparent if the immediate parent is an internal node with no hash.
Parent() common.Hash Parent() common.Hash
// Path returns the hex-encoded path to the current node. // Path returns the hex-encoded path to the current node.
// Callers must not retain references to the return value after calling Next. // Callers must not retain references to the return value after calling Next.
// For leaf nodes, the last element of the path is the 'terminator symbol' 0x10. // For leaf nodes, the last element of the path is the 'terminator symbol' 0x10.
Path() []byte Path() []byte
// Leaf returns true iff the current node is a leaf node. // Leaf returns true iff the current node is a leaf node.
// LeafBlob, LeafKey return the contents and key of the leaf node. These
// method panic if the iterator is not positioned at a leaf.
// Callers must not retain references to their return value after calling Next
Leaf() bool Leaf() bool
LeafBlob() []byte
// LeafKey returns the key of the leaf. The method panics if the iterator is not
// positioned at a leaf. Callers must not retain references to the value after
// calling Next.
LeafKey() []byte LeafKey() []byte
// LeafBlob returns the content of the leaf. The method panics if the iterator
// is not positioned at a leaf. Callers must not retain references to the value
// after calling Next.
LeafBlob() []byte
// LeafProof returns the Merkle proof of the leaf. The method panics if the
// iterator is not positioned at a leaf. Callers must not retain references
// to the value after calling Next.
LeafProof() [][]byte
} }
// nodeIteratorState represents the iteration state at one particular node of the // nodeIteratorState represents the iteration state at one particular node of the
@ -139,6 +159,15 @@ func (it *nodeIterator) Leaf() bool {
return hasTerm(it.path) return hasTerm(it.path)
} }
func (it *nodeIterator) LeafKey() []byte {
if len(it.stack) > 0 {
if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
return hexToKeybytes(it.path)
}
}
panic("not at leaf")
}
func (it *nodeIterator) LeafBlob() []byte { func (it *nodeIterator) LeafBlob() []byte {
if len(it.stack) > 0 { if len(it.stack) > 0 {
if node, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { if node, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
@ -148,10 +177,22 @@ func (it *nodeIterator) LeafBlob() []byte {
panic("not at leaf") panic("not at leaf")
} }
func (it *nodeIterator) LeafKey() []byte { func (it *nodeIterator) LeafProof() [][]byte {
if len(it.stack) > 0 { if len(it.stack) > 0 {
if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
return hexToKeybytes(it.path) hasher := newHasher(0, 0, nil)
proofs := make([][]byte, 0, len(it.stack))
for i, item := range it.stack[:len(it.stack)-1] {
// Gather nodes that end up as hash nodes (or the root)
node, _, _ := hasher.hashChildren(item.node, nil)
hashed, _ := hasher.store(node, nil, false)
if _, ok := hashed.(hashNode); ok || i == 0 {
enc, _ := rlp.EncodeToBytes(node)
proofs = append(proofs, enc)
}
}
return proofs
} }
} }
panic("not at leaf") panic("not at leaf")
@ -361,12 +402,16 @@ func (it *differenceIterator) Leaf() bool {
return it.b.Leaf() return it.b.Leaf()
} }
func (it *differenceIterator) LeafKey() []byte {
return it.b.LeafKey()
}
func (it *differenceIterator) LeafBlob() []byte { func (it *differenceIterator) LeafBlob() []byte {
return it.b.LeafBlob() return it.b.LeafBlob()
} }
func (it *differenceIterator) LeafKey() []byte { func (it *differenceIterator) LeafProof() [][]byte {
return it.b.LeafKey() return it.b.LeafProof()
} }
func (it *differenceIterator) Path() []byte { func (it *differenceIterator) Path() []byte {
@ -464,12 +509,16 @@ func (it *unionIterator) Leaf() bool {
return (*it.items)[0].Leaf() return (*it.items)[0].Leaf()
} }
func (it *unionIterator) LeafKey() []byte {
return (*it.items)[0].LeafKey()
}
func (it *unionIterator) LeafBlob() []byte { func (it *unionIterator) LeafBlob() []byte {
return (*it.items)[0].LeafBlob() return (*it.items)[0].LeafBlob()
} }
func (it *unionIterator) LeafKey() []byte { func (it *unionIterator) LeafProof() [][]byte {
return (*it.items)[0].LeafKey() return (*it.items)[0].LeafProof()
} }
func (it *unionIterator) Path() []byte { func (it *unionIterator) Path() []byte {
@ -509,12 +558,10 @@ func (it *unionIterator) Next(descend bool) bool {
heap.Push(it.items, skipped) heap.Push(it.items, skipped)
} }
} }
if least.Next(descend) { if least.Next(descend) {
it.count++ it.count++
heap.Push(it.items, least) heap.Push(it.items, least)
} }
return len(*it.items) > 0 return len(*it.items) > 0
} }

View File

@ -32,20 +32,46 @@ func init() {
mrand.Seed(time.Now().Unix()) mrand.Seed(time.Now().Unix())
} }
// makeProvers creates Merkle trie provers based on different implementations to
// test all variations.
func makeProvers(trie *Trie) []func(key []byte) *ethdb.MemDatabase {
var provers []func(key []byte) *ethdb.MemDatabase
// Create a direct trie based Merkle prover
provers = append(provers, func(key []byte) *ethdb.MemDatabase {
proof := ethdb.NewMemDatabase()
trie.Prove(key, 0, proof)
return proof
})
// Create a leaf iterator based Merkle prover
provers = append(provers, func(key []byte) *ethdb.MemDatabase {
proof := ethdb.NewMemDatabase()
if it := NewIterator(trie.NodeIterator(key)); it.Next() && bytes.Equal(key, it.Key) {
for _, p := range it.Prove() {
proof.Put(crypto.Keccak256(p), p)
}
}
return proof
})
return provers
}
func TestProof(t *testing.T) { func TestProof(t *testing.T) {
trie, vals := randomTrie(500) trie, vals := randomTrie(500)
root := trie.Hash() root := trie.Hash()
for _, kv := range vals { for i, prover := range makeProvers(trie) {
proofs := ethdb.NewMemDatabase() for _, kv := range vals {
if trie.Prove(kv.k, 0, proofs) != nil { proof := prover(kv.k)
t.Fatalf("missing key %x while constructing proof", kv.k) if proof == nil {
} t.Fatalf("prover %d: missing key %x while constructing proof", i, kv.k)
val, _, err := VerifyProof(root, kv.k, proofs) }
if err != nil { val, _, err := VerifyProof(root, kv.k, proof)
t.Fatalf("VerifyProof error for key %x: %v\nraw proof: %v", kv.k, err, proofs) if err != nil {
} t.Fatalf("prover %d: failed to verify proof for key %x: %v\nraw proof: %x", i, kv.k, err, proof)
if !bytes.Equal(val, kv.v) { }
t.Fatalf("VerifyProof returned wrong value for key %x: got %x, want %x", kv.k, val, kv.v) if !bytes.Equal(val, kv.v) {
t.Fatalf("prover %d: verified value mismatch for key %x: have %x, want %x", i, kv.k, val, kv.v)
}
} }
} }
} }
@ -53,37 +79,66 @@ func TestProof(t *testing.T) {
func TestOneElementProof(t *testing.T) { func TestOneElementProof(t *testing.T) {
trie := new(Trie) trie := new(Trie)
updateString(trie, "k", "v") updateString(trie, "k", "v")
proofs := ethdb.NewMemDatabase() for i, prover := range makeProvers(trie) {
trie.Prove([]byte("k"), 0, proofs) proof := prover([]byte("k"))
if len(proofs.Keys()) != 1 { if proof == nil {
t.Error("proof should have one element") t.Fatalf("prover %d: nil proof", i)
} }
val, _, err := VerifyProof(trie.Hash(), []byte("k"), proofs) if proof.Len() != 1 {
if err != nil { t.Errorf("prover %d: proof should have one element", i)
t.Fatalf("VerifyProof error: %v\nproof hashes: %v", err, proofs.Keys()) }
} val, _, err := VerifyProof(trie.Hash(), []byte("k"), proof)
if !bytes.Equal(val, []byte("v")) { if err != nil {
t.Fatalf("VerifyProof returned wrong value: got %x, want 'k'", val) t.Fatalf("prover %d: failed to verify proof: %v\nraw proof: %x", i, err, proof)
}
if !bytes.Equal(val, []byte("v")) {
t.Fatalf("prover %d: verified value mismatch: have %x, want 'k'", i, val)
}
} }
} }
func TestVerifyBadProof(t *testing.T) { func TestBadProof(t *testing.T) {
trie, vals := randomTrie(800) trie, vals := randomTrie(800)
root := trie.Hash() root := trie.Hash()
for _, kv := range vals { for i, prover := range makeProvers(trie) {
proofs := ethdb.NewMemDatabase() for _, kv := range vals {
trie.Prove(kv.k, 0, proofs) proof := prover(kv.k)
if len(proofs.Keys()) == 0 { if proof == nil {
t.Fatal("zero length proof") t.Fatalf("prover %d: nil proof", i)
}
key := proof.Keys()[mrand.Intn(proof.Len())]
val, _ := proof.Get(key)
proof.Delete(key)
mutateByte(val)
proof.Put(crypto.Keccak256(val), val)
if _, _, err := VerifyProof(root, kv.k, proof); err == nil {
t.Fatalf("prover %d: expected proof to fail for key %x", i, kv.k)
}
} }
keys := proofs.Keys() }
key := keys[mrand.Intn(len(keys))] }
node, _ := proofs.Get(key)
proofs.Delete(key) // Tests that missing keys can also be proven. The test explicitly uses a single
mutateByte(node) // entry trie and checks for missing keys both before and after the single entry.
proofs.Put(crypto.Keccak256(node), node) func TestMissingKeyProof(t *testing.T) {
if _, _, err := VerifyProof(root, kv.k, proofs); err == nil { trie := new(Trie)
t.Fatalf("expected proof to fail for key %x", kv.k) updateString(trie, "k", "v")
for i, key := range []string{"a", "j", "l", "z"} {
proof := ethdb.NewMemDatabase()
trie.Prove([]byte(key), 0, proof)
if proof.Len() != 1 {
t.Errorf("test %d: proof should have one element", i)
}
val, _, err := VerifyProof(trie.Hash(), []byte(key), proof)
if err != nil {
t.Fatalf("test %d: failed to verify proof: %v\nraw proof: %x", i, err, proof)
}
if val != nil {
t.Fatalf("test %d: verified value mismatch: have %x, want nil", i, val)
} }
} }
} }