1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
package merkle
import (
"bytes"
"golang.org/x/crypto/ripemd160"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-wire"
)
type IAVLProof struct {
LeafNode IAVLProofLeafNode
InnerNodes []IAVLProofInnerNode
RootHash []byte
}
func (proof *IAVLProof) Verify(keyBytes, valueBytes, rootHash []byte) bool {
if !bytes.Equal(keyBytes, proof.LeafNode.KeyBytes) {
return false
}
if !bytes.Equal(valueBytes, proof.LeafNode.ValueBytes) {
return false
}
if !bytes.Equal(rootHash, proof.RootHash) {
return false
}
hash := proof.LeafNode.Hash()
// fmt.Printf("leaf hash: %X\n", hash)
for _, branch := range proof.InnerNodes {
hash = branch.Hash(hash)
// fmt.Printf("branch hash: %X\n", hash)
}
// fmt.Printf("root: %X, computed: %X\n", proof.RootHash, hash)
return bytes.Equal(proof.RootHash, hash)
}
type IAVLProofInnerNode struct {
Height int8
Size int
Left []byte
Right []byte
}
func (branch IAVLProofInnerNode) Hash(childHash []byte) []byte {
hasher := ripemd160.New()
buf := new(bytes.Buffer)
n, err := int(0), error(nil)
wire.WriteInt8(branch.Height, buf, &n, &err)
wire.WriteVarint(branch.Size, buf, &n, &err)
if len(branch.Left) == 0 {
wire.WriteByteSlice(childHash, buf, &n, &err)
wire.WriteByteSlice(branch.Right, buf, &n, &err)
} else {
wire.WriteByteSlice(branch.Left, buf, &n, &err)
wire.WriteByteSlice(childHash, buf, &n, &err)
}
if err != nil {
PanicCrisis(Fmt("Failed to hash IAVLProofInnerNode: %v", err))
}
// fmt.Printf("InnerNode hash bytes: %X\n", buf.Bytes())
hasher.Write(buf.Bytes())
return hasher.Sum(nil)
}
type IAVLProofLeafNode struct {
KeyBytes []byte
ValueBytes []byte
}
func (leaf IAVLProofLeafNode) Hash() []byte {
hasher := ripemd160.New()
buf := new(bytes.Buffer)
n, err := int(0), error(nil)
wire.WriteInt8(0, buf, &n, &err)
wire.WriteVarint(1, buf, &n, &err)
wire.WriteByteSlice(leaf.KeyBytes, buf, &n, &err)
wire.WriteByteSlice(leaf.ValueBytes, buf, &n, &err)
if err != nil {
PanicCrisis(Fmt("Failed to hash IAVLProofLeafNode: %v", err))
}
// fmt.Printf("LeafNode hash bytes: %X\n", buf.Bytes())
hasher.Write(buf.Bytes())
return hasher.Sum(nil)
}
func (node *IAVLNode) constructProof(t *IAVLTree, key []byte, proof *IAVLProof) (exists bool) {
if node.height == 0 {
if bytes.Compare(node.key, key) == 0 {
leaf := IAVLProofLeafNode{
KeyBytes: node.key,
ValueBytes: node.value,
}
proof.LeafNode = leaf
return true
} else {
return false
}
} else {
if bytes.Compare(key, node.key) < 0 {
exists := node.getLeftNode(t).constructProof(t, key, proof)
if !exists {
return false
}
branch := IAVLProofInnerNode{
Height: node.height,
Size: node.size,
Left: nil,
Right: node.getRightNode(t).hash,
}
proof.InnerNodes = append(proof.InnerNodes, branch)
return true
} else {
exists := node.getRightNode(t).constructProof(t, key, proof)
if !exists {
return false
}
branch := IAVLProofInnerNode{
Height: node.height,
Size: node.size,
Left: node.getLeftNode(t).hash,
Right: nil,
}
proof.InnerNodes = append(proof.InnerNodes, branch)
return true
}
}
}
// Returns nil if key is not in tree.
func (t *IAVLTree) ConstructProof(key []byte) *IAVLProof {
if t.root == nil {
return nil
}
t.root.hashWithCount(t) // Ensure that all hashes are calculated.
proof := &IAVLProof{
RootHash: t.root.hash,
}
t.root.constructProof(t, key, proof)
return proof
}
|