Skip to content

Commit 1907b67

Browse files
committed
Refactor out TypeCastSteps to reduce code duplication
1 parent 7ad9f18 commit 1907b67

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

go/ops.go

-18
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,6 @@ func asInner(op *ProofOp) (*InnerOp, error) {
4343
return inner.Inner, nil
4444
}
4545

46-
func (op *ProofOp) Apply(args ...[]byte) ([]byte, error) {
47-
o := op.Op
48-
switch o.(type) {
49-
case *ProofOp_Leaf:
50-
if len(args) != 2 {
51-
return nil, fmt.Errorf("Need key and value args, got %d", len(args))
52-
}
53-
return op.GetLeaf().Apply(args[0], args[1])
54-
case *ProofOp_Inner:
55-
if len(args) != 1 {
56-
return nil, fmt.Errorf("Need one child hash, got %d", len(args))
57-
}
58-
return op.GetInner().Apply(args[0])
59-
default:
60-
panic("Unknown proof op")
61-
}
62-
}
63-
6446
func (op *LeafOp) Apply(key []byte, value []byte) ([]byte, error) {
6547
if len(key) == 0 {
6648
return nil, fmt.Errorf("Leaf op needs key")

go/proof.go

+25-14
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ var IavlSpec = &ProofSpec{
1818
// You must validate the result is what you have in a header.
1919
// Returns error if the calculations cannot be performed.
2020
func (p *ExistenceProof) Calculate() ([]byte, error) {
21-
if len(p.Steps) == 0 {
22-
return nil, fmt.Errorf("Existence Proof needs at least one step")
21+
leaf, inners, err := p.TypeCastSteps()
22+
if err != nil {
23+
return nil, err
2324
}
2425

25-
leaf, inners := p.Steps[0], p.Steps[1:]
2626
// leaf step takes the key and value as input
2727
res, err := leaf.Apply(p.Key, p.Value)
2828
if err != nil {
@@ -40,31 +40,42 @@ func (p *ExistenceProof) Calculate() ([]byte, error) {
4040
}
4141

4242
func (p *ExistenceProof) CheckAgainstSpec(spec *ProofSpec) error {
43-
if len(p.Steps) == 0 {
44-
return fmt.Errorf("Existence Proof needs at least one step")
45-
}
46-
47-
leafOp, inners := p.Steps[0], p.Steps[1:]
48-
leaf, err := asLeaf(leafOp)
43+
leaf, inners, err := p.TypeCastSteps()
4944
if err != nil {
5045
return err
5146
}
5247
err = checkLeaf(leaf, spec.LeafSpec)
5348
if err != nil {
5449
return err
5550
}
56-
for _, innerOp := range inners {
57-
inner, err := asInner(innerOp)
58-
if err != nil {
59-
return err
60-
}
51+
for _, inner := range inners {
6152
if err := checkInner(inner, spec.LeafSpec.Prefix); err != nil {
6253
return err
6354
}
6455
}
6556
return nil
6657
}
6758

59+
func (p *ExistenceProof) TypeCastSteps() (*LeafOp, []*InnerOp, error) {
60+
if len(p.Steps) == 0 {
61+
return nil, nil, fmt.Errorf("Existence Proof needs at least one step")
62+
}
63+
leafOp, inners := p.Steps[0], p.Steps[1:]
64+
leaf, err := asLeaf(leafOp)
65+
if err != nil {
66+
return nil, nil, err
67+
}
68+
var result []*InnerOp
69+
for _, innerOp := range inners {
70+
inner, err := asInner(innerOp)
71+
if err != nil {
72+
return nil, nil, err
73+
}
74+
result = append(result, inner)
75+
}
76+
return leaf, result, nil
77+
}
78+
6879
func checkLeaf(leaf *LeafOp, spec *LeafOp) error {
6980
if leaf.Hash != spec.Hash {
7081
return fmt.Errorf("Unexpected HashOp: %d", leaf.Hash)

0 commit comments

Comments
 (0)