forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cosmos#105 from mossid/joon/generalmerkle
General Merkle Proof
- Loading branch information
Showing
7 changed files
with
221 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package iavl | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tendermint/tendermint/crypto/merkle" | ||
cmn "github.com/tendermint/tmlibs/common" | ||
) | ||
|
||
const ProofOpIAVLAbsence = "iavl:a" | ||
|
||
// IAVLAbsenceOp takes a key as its only argument | ||
// | ||
// If the produced root hash matches the expected hash, the proof | ||
// is good. | ||
type IAVLAbsenceOp struct { | ||
// Encoded in ProofOp.Key. | ||
key []byte | ||
|
||
// To encode in ProofOp.Data. | ||
// Proof is nil for an empty tree. | ||
// The hash of an empty tree is nil. | ||
Proof *RangeProof `json:"proof"` | ||
} | ||
|
||
var _ merkle.ProofOperator = IAVLAbsenceOp{} | ||
|
||
func NewIAVLAbsenceOp(key []byte, proof *RangeProof) IAVLAbsenceOp { | ||
return IAVLAbsenceOp{ | ||
key: key, | ||
Proof: proof, | ||
} | ||
} | ||
|
||
func IAVLAbsenceOpDecoder(pop merkle.ProofOp) (merkle.ProofOperator, error) { | ||
if pop.Type != ProofOpIAVLAbsence { | ||
return nil, cmn.NewError("unexpected ProofOp.Type; got %v, want %v", pop.Type, ProofOpIAVLAbsence) | ||
} | ||
var op IAVLAbsenceOp // a bit strange as we'll discard this, but it works. | ||
err := cdc.UnmarshalBinary(pop.Data, &op) | ||
if err != nil { | ||
return nil, cmn.ErrorWrap(err, "decoding ProofOp.Data into IAVLAbsenceOp") | ||
} | ||
return NewIAVLAbsenceOp(pop.Key, op.Proof), nil | ||
} | ||
|
||
func (op IAVLAbsenceOp) ProofOp() merkle.ProofOp { | ||
bz := cdc.MustMarshalBinary(op) | ||
return merkle.ProofOp{ | ||
Type: ProofOpIAVLAbsence, | ||
Key: op.key, | ||
Data: bz, | ||
} | ||
} | ||
|
||
func (op IAVLAbsenceOp) String() string { | ||
return fmt.Sprintf("IAVLAbsenceOp{%v}", op.GetKey()) | ||
} | ||
|
||
func (op IAVLAbsenceOp) Run(args [][]byte) ([][]byte, error) { | ||
if len(args) != 0 { | ||
return nil, cmn.NewError("expected 0 args, got %v", len(args)) | ||
} | ||
// If the tree is nil, the proof is nil, and all keys are absent. | ||
if op.Proof == nil { | ||
return [][]byte{[]byte(nil)}, nil | ||
} | ||
// Compute the root hash and assume it is valid. | ||
// The caller checks the ultimate root later. | ||
root := op.Proof.ComputeRootHash() | ||
err := op.Proof.Verify(root) | ||
if err != nil { | ||
return nil, cmn.ErrorWrap(err, "computing root hash") | ||
} | ||
// XXX What is the encoding for keys? | ||
// We should decode the key depending on whether it's a string or hex, | ||
// maybe based on quotes and 0x prefix? | ||
err = op.Proof.VerifyAbsence([]byte(op.key)) | ||
if err != nil { | ||
return nil, cmn.ErrorWrap(err, "verifying absence") | ||
} | ||
return [][]byte{root}, nil | ||
} | ||
|
||
func (op IAVLAbsenceOp) GetKey() []byte { | ||
return op.key | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package iavl | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tendermint/tendermint/crypto/merkle" | ||
cmn "github.com/tendermint/tmlibs/common" | ||
) | ||
|
||
const ProofOpIAVLValue = "iavl:v" | ||
|
||
// IAVLValueOp takes a key and a single value as argument and | ||
// produces the root hash. | ||
// | ||
// If the produced root hash matches the expected hash, the proof | ||
// is good. | ||
type IAVLValueOp struct { | ||
// Encoded in ProofOp.Key. | ||
key []byte | ||
|
||
// To encode in ProofOp.Data. | ||
// Proof is nil for an empty tree. | ||
// The hash of an empty tree is nil. | ||
Proof *RangeProof `json:"proof"` | ||
} | ||
|
||
var _ merkle.ProofOperator = IAVLValueOp{} | ||
|
||
func NewIAVLValueOp(key []byte, proof *RangeProof) IAVLValueOp { | ||
return IAVLValueOp{ | ||
key: key, | ||
Proof: proof, | ||
} | ||
} | ||
|
||
func IAVLValueOpDecoder(pop merkle.ProofOp) (merkle.ProofOperator, error) { | ||
if pop.Type != ProofOpIAVLValue { | ||
return nil, cmn.NewError("unexpected ProofOp.Type; got %v, want %v", pop.Type, ProofOpIAVLValue) | ||
} | ||
var op IAVLValueOp // a bit strange as we'll discard this, but it works. | ||
err := cdc.UnmarshalBinary(pop.Data, &op) | ||
if err != nil { | ||
return nil, cmn.ErrorWrap(err, "decoding ProofOp.Data into IAVLValueOp") | ||
} | ||
return NewIAVLValueOp(pop.Key, op.Proof), nil | ||
} | ||
|
||
func (op IAVLValueOp) ProofOp() merkle.ProofOp { | ||
bz := cdc.MustMarshalBinary(op) | ||
return merkle.ProofOp{ | ||
Type: ProofOpIAVLValue, | ||
Key: op.key, | ||
Data: bz, | ||
} | ||
} | ||
|
||
func (op IAVLValueOp) String() string { | ||
return fmt.Sprintf("IAVLValueOp{%v}", op.GetKey()) | ||
} | ||
|
||
func (op IAVLValueOp) Run(args [][]byte) ([][]byte, error) { | ||
if len(args) != 1 { | ||
return nil, cmn.NewError("Value size is not 1") | ||
} | ||
value := args[0] | ||
|
||
// Compute the root hash and assume it is valid. | ||
// The caller checks the ultimate root later. | ||
root := op.Proof.ComputeRootHash() | ||
err := op.Proof.Verify(root) | ||
if err != nil { | ||
return nil, cmn.ErrorWrap(err, "computing root hash") | ||
} | ||
// XXX What is the encoding for keys? | ||
// We should decode the key depending on whether it's a string or hex, | ||
// maybe based on quotes and 0x prefix? | ||
err = op.Proof.VerifyItem([]byte(op.key), value) | ||
if err != nil { | ||
return nil, cmn.ErrorWrap(err, "verifying value") | ||
} | ||
return [][]byte{root}, nil | ||
} | ||
|
||
func (op IAVLValueOp) GetKey() []byte { | ||
return op.key | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.