Skip to content

Commit 49852c2

Browse files
committed
Use pkg/errors for all errors
1 parent 318acd2 commit 49852c2

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

go/ops.go

+23-19
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,71 @@ import (
77
_ "crypto/sha256"
88
// adds sha512 capability to crypto.SHA512
99
_ "crypto/sha512"
10-
fmt "fmt"
1110

1211
// adds ripemd160 capability to crypto.RIPEMD160
1312
_ "golang.org/x/crypto/ripemd160"
13+
14+
"github.com/pkg/errors"
1415
)
1516

17+
// Apply will calculate the leaf hash given the key and value being proven
1618
func (op *LeafOp) Apply(key []byte, value []byte) ([]byte, error) {
1719
if len(key) == 0 {
18-
return nil, fmt.Errorf("Leaf op needs key")
20+
return nil, errors.New("Leaf op needs key")
1921
}
2022
if len(value) == 0 {
21-
return nil, fmt.Errorf("Leaf op needs value")
23+
return nil, errors.New("Leaf op needs value")
2224
}
2325
pkey, err := prepareLeafData(op.PrehashKey, op.Length, key)
2426
if err != nil {
25-
return nil, err
27+
return nil, errors.Wrap(err, "prehash key")
2628
}
2729
pvalue, err := prepareLeafData(op.PrehashValue, op.Length, value)
2830
if err != nil {
29-
return nil, err
31+
return nil, errors.Wrap(err, "prehash value")
3032
}
3133
data := append(op.Prefix, pkey...)
3234
data = append(data, pvalue...)
3335
return doHash(op.Hash, data)
3436
}
3537

38+
// CheckAgainstSpec will verify the LeafOp is in the format defined in spec
3639
func (op *LeafOp)CheckAgainstSpec(spec *ProofSpec) error {
3740
lspec := spec.LeafSpec
3841

3942
if op.Hash != lspec.Hash {
40-
return fmt.Errorf("Unexpected HashOp: %d", op.Hash)
43+
return errors.Errorf("Unexpected HashOp: %d", op.Hash)
4144
}
4245
if op.PrehashKey != lspec.PrehashKey {
43-
return fmt.Errorf("Unexpected PrehashKey: %d", op.PrehashKey)
46+
return errors.Errorf("Unexpected PrehashKey: %d", op.PrehashKey)
4447
}
4548
if op.PrehashValue != lspec.PrehashValue {
46-
return fmt.Errorf("Unexpected PrehashValue: %d", op.PrehashValue)
49+
return errors.Errorf("Unexpected PrehashValue: %d", op.PrehashValue)
4750
}
4851
if op.Length != lspec.Length {
49-
return fmt.Errorf("Unexpected LengthOp: %d", op.Length)
52+
return errors.Errorf("Unexpected LengthOp: %d", op.Length)
5053
}
5154
if !bytes.HasPrefix(op.Prefix, lspec.Prefix) {
52-
return fmt.Errorf("Leaf Prefix doesn't start with %X", lspec.Prefix)
55+
return errors.Errorf("Leaf Prefix doesn't start with %X", lspec.Prefix)
5356
}
5457
return nil
5558
}
5659

57-
60+
// Apply will calculate the hash of the next step, given the hash of the previous step
5861
func (op *InnerOp) Apply(child []byte) ([]byte, error) {
5962
if len(child) == 0 {
60-
return nil, fmt.Errorf("Inner op needs child value")
63+
return nil, errors.Errorf("Inner op needs child value")
6164
}
6265
preimage := append(op.Prefix, child...)
6366
preimage = append(preimage, op.Suffix...)
6467
return doHash(op.Hash, preimage)
6568
}
6669

67-
func (inner *InnerOp)CheckAgainstSpec(spec *ProofSpec) error {
70+
// CheckAgainstSpec will verify the InnerOp is in the format defined in spec
71+
func (op *InnerOp)CheckAgainstSpec(spec *ProofSpec) error {
6872
leafPrefix := spec.LeafSpec.Prefix
69-
if bytes.HasPrefix(inner.Prefix, leafPrefix) {
70-
return fmt.Errorf("Inner Prefix starts with %X", leafPrefix)
73+
if bytes.HasPrefix(op.Prefix, leafPrefix) {
74+
return errors.Errorf("Inner Prefix starts with %X", leafPrefix)
7175
}
7276
return nil
7377
}
@@ -117,7 +121,7 @@ func doHash(hashOp HashOp, preimage []byte) ([]byte, error) {
117121
hash.Write(tmp)
118122
return hash.Sum(nil), nil
119123
}
120-
return nil, fmt.Errorf("Unsupported hashop: %d", hashOp)
124+
return nil, errors.Errorf("Unsupported hashop: %d", hashOp)
121125
}
122126

123127
// doLengthOp will calculate the proper prefix and return it prepended
@@ -131,12 +135,12 @@ func doLengthOp(lengthOp LengthOp, data []byte) ([]byte, error) {
131135
return res, nil
132136
case LengthOp_REQUIRE_32_BYTES:
133137
if len(data) != 32 {
134-
return nil, fmt.Errorf("Data was %d bytes, not 32", len(data))
138+
return nil, errors.Errorf("Data was %d bytes, not 32", len(data))
135139
}
136140
return data, nil
137141
case LengthOp_REQUIRE_64_BYTES:
138142
if len(data) != 64 {
139-
return nil, fmt.Errorf("Data was %d bytes, not 64", len(data))
143+
return nil, errors.Errorf("Data was %d bytes, not 64", len(data))
140144
}
141145
return data, nil
142146
// TODO
@@ -146,7 +150,7 @@ func doLengthOp(lengthOp LengthOp, data []byte) ([]byte, error) {
146150
// case LengthOp_FIXED32_LITTLE:
147151
// case LengthOp_FIXED64_LITTLE:
148152
}
149-
return nil, fmt.Errorf("Unsupported lengthop: %d", lengthOp)
153+
return nil, errors.Errorf("Unsupported lengthop: %d", lengthOp)
150154
}
151155

152156
func encodeVarintProto(l int) []byte {

go/proof.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package proofs
22

33
import (
44
"bytes"
5-
"fmt"
65

76
"github.com/pkg/errors"
87
)
@@ -53,42 +52,42 @@ func (p *ExistenceProof) Verify(spec *ProofSpec, root CommitmentRoot, key []byte
5352

5453
}
5554

56-
5755
// Calculate determines the root hash that matches the given proof.
5856
// You must validate the result is what you have in a header.
5957
// Returns error if the calculations cannot be performed.
6058
func (p *ExistenceProof) Calculate() (CommitmentRoot, error) {
6159
if p.GetLeaf() == nil {
62-
return nil, fmt.Errorf("Existence Proof needs defined LeafOp")
60+
return nil, errors.New("Existence Proof needs defined LeafOp")
6361
}
6462

6563
// leaf step takes the key and value as input
6664
res, err := p.Leaf.Apply(p.Key, p.Value)
6765
if err != nil {
68-
return nil, err
66+
return nil, errors.WithMessage(err, "leaf")
6967
}
7068

7169
// the rest just take the output of the last step (reducing it)
7270
for _, step := range p.Path {
7371
res, err = step.Apply(res)
7472
if err != nil {
75-
return nil, err
73+
return nil, errors.WithMessage(err, "inner")
7674
}
7775
}
7876
return res, nil
7977
}
8078

79+
// CheckAgainstSpec will verify the leaf and all path steps are in the format defined in spec
8180
func (p *ExistenceProof) CheckAgainstSpec(spec *ProofSpec) error {
8281
if p.GetLeaf() == nil {
83-
return fmt.Errorf("Existence Proof needs defined LeafOp")
82+
return errors.New("Existence Proof needs defined LeafOp")
8483
}
8584
err := p.Leaf.CheckAgainstSpec(spec)
8685
if err != nil {
87-
return err
86+
return errors.WithMessage(err, "leaf")
8887
}
8988
for _, inner := range p.Path {
9089
if err := inner.CheckAgainstSpec(spec); err != nil {
91-
return err
90+
return errors.WithMessage(err, "inner")
9291
}
9392
}
9493
return nil

0 commit comments

Comments
 (0)