Skip to content

Commit

Permalink
Improve secp256k1fx + merkledb error handling (#1402)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Apr 22, 2023
1 parent dd572a4 commit a152e62
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 102 deletions.
20 changes: 10 additions & 10 deletions vms/secp256k1fx/fx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func TestFxVerifyTransferInvalidOutput(t *testing.T) {
},
}

require.ErrorIs(fx.VerifyTransfer(tx, in, cred, out), errOutputUnoptimized)
require.ErrorIs(fx.VerifyTransfer(tx, in, cred, out), ErrOutputUnoptimized)
}

func TestFxVerifyTransferWrongAmounts(t *testing.T) {
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestFxVerifyTransferWrongAmounts(t *testing.T) {
},
}

require.Error(fx.VerifyTransfer(tx, in, cred, out))
require.ErrorIs(fx.VerifyTransfer(tx, in, cred, out), ErrMismatchedAmounts)
}

func TestFxVerifyTransferTimelocked(t *testing.T) {
Expand Down Expand Up @@ -479,7 +479,7 @@ func TestFxVerifyTransferInvalidSignature(t *testing.T) {

require.NoError(fx.VerifyTransfer(tx, in, cred, out))
require.NoError(fx.Bootstrapped())
require.Error(fx.VerifyTransfer(tx, in, cred, out), errAddrsNotSortedUnique)
require.ErrorIs(fx.VerifyTransfer(tx, in, cred, out), secp256k1.ErrInvalidSig)
}

func TestFxVerifyTransferWrongSigner(t *testing.T) {
Expand Down Expand Up @@ -518,7 +518,7 @@ func TestFxVerifyTransferWrongSigner(t *testing.T) {

require.NoError(fx.VerifyTransfer(tx, in, cred, out))
require.NoError(fx.Bootstrapped())
require.Error(fx.VerifyTransfer(tx, in, cred, out))
require.ErrorIs(fx.VerifyTransfer(tx, in, cred, out), ErrWrongSig)
}

func TestFxVerifyTransferSigIndexOOB(t *testing.T) {
Expand Down Expand Up @@ -881,7 +881,7 @@ func TestFxVerifyOperationInvalidOperationVerify(t *testing.T) {
}

utxos := []interface{}{utxo}
require.ErrorIs(fx.VerifyOperation(tx, op, cred, utxos), errOutputUnspendable)
require.ErrorIs(fx.VerifyOperation(tx, op, cred, utxos), ErrOutputUnspendable)
}

func TestFxVerifyOperationMismatchedMintOutputs(t *testing.T) {
Expand Down Expand Up @@ -962,7 +962,7 @@ func TestVerifyPermission(t *testing.T) {
Threshold: 0,
Addrs: []ids.ShortID{addr},
},
errOutputUnoptimized,
ErrOutputUnoptimized,
},
{
"threshold 0, no sigs, no addrs",
Expand Down Expand Up @@ -995,7 +995,7 @@ func TestVerifyPermission(t *testing.T) {
Threshold: 0,
Addrs: []ids.ShortID{addr},
},
errOutputUnoptimized,
ErrOutputUnoptimized,
},
{
"threshold 1, 0 sigs (too few sigs)",
Expand Down Expand Up @@ -1028,7 +1028,7 @@ func TestVerifyPermission(t *testing.T) {
Threshold: 2,
Addrs: []ids.ShortID{addr, addr2},
},
errNotSortedUnique,
ErrInputIndicesNotSortedUnique,
},
{
"threshold 2, repeated address and repeated sig",
Expand All @@ -1039,7 +1039,7 @@ func TestVerifyPermission(t *testing.T) {
Threshold: 2,
Addrs: []ids.ShortID{addr, addr},
},
errAddrsNotSortedUnique,
ErrAddrsNotSortedUnique,
},
{
"threshold 2, 2 sigs",
Expand All @@ -1061,7 +1061,7 @@ func TestVerifyPermission(t *testing.T) {
Threshold: 2,
Addrs: []ids.ShortID{addr, addr2},
},
errNotSortedUnique,
ErrInputIndicesNotSortedUnique,
},
{
"threshold 1, 1 sig, index out of bounds",
Expand Down
8 changes: 4 additions & 4 deletions vms/secp256k1fx/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
)

var (
errNilInput = errors.New("nil input")
errNotSortedUnique = errors.New("signatures not sorted and unique")
ErrNilInput = errors.New("nil input")
ErrInputIndicesNotSortedUnique = errors.New("address indices not sorted and unique")
)

type Input struct {
Expand All @@ -35,9 +35,9 @@ func (in *Input) Cost() (uint64, error) {
func (in *Input) Verify() error {
switch {
case in == nil:
return errNilInput
return ErrNilInput
case !utils.IsSortedAndUniqueOrdered(in.SigIndices):
return errNotSortedUnique
return ErrInputIndicesNotSortedUnique
default:
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions vms/secp256k1fx/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ func TestInputVerifyNil(t *testing.T) {
{
name: "nil input",
in: nil,
expectedErr: errNilInput,
expectedErr: ErrNilInput,
},
{
name: "not sorted",
in: &Input{SigIndices: []uint32{2, 1}},
expectedErr: errNotSortedUnique,
expectedErr: ErrInputIndicesNotSortedUnique,
},
{
name: "not unique",
in: &Input{SigIndices: []uint32{2, 2}},
expectedErr: errNotSortedUnique,
expectedErr: ErrInputIndicesNotSortedUnique,
},
{
name: "passes verification",
Expand Down
10 changes: 5 additions & 5 deletions vms/secp256k1fx/mint_operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestMintOperationVerify(t *testing.T) {
MintOutput: validMintOutput,
TransferOutput: validTransferOutput,
},
expectedErr: errNotSortedUnique,
expectedErr: ErrInputIndicesNotSortedUnique,
},
{
name: "invalid mint output",
Expand All @@ -63,7 +63,7 @@ func TestMintOperationVerify(t *testing.T) {
},
TransferOutput: validTransferOutput,
},
expectedErr: errOutputUnspendable,
expectedErr: ErrOutputUnspendable,
},
{
name: "invalid transfer output",
Expand All @@ -78,7 +78,7 @@ func TestMintOperationVerify(t *testing.T) {
},
},
},
expectedErr: errOutputUnoptimized,
expectedErr: ErrOutputUnoptimized,
},
{
name: "addresses not unique",
Expand All @@ -93,7 +93,7 @@ func TestMintOperationVerify(t *testing.T) {
},
},
},
expectedErr: errAddrsNotSortedUnique,
expectedErr: ErrAddrsNotSortedUnique,
},
{
name: "addresses not sorted",
Expand All @@ -108,7 +108,7 @@ func TestMintOperationVerify(t *testing.T) {
},
},
},
expectedErr: errAddrsNotSortedUnique,
expectedErr: ErrAddrsNotSortedUnique,
},
{
name: "passes verification",
Expand Down
2 changes: 1 addition & 1 deletion vms/secp256k1fx/mint_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type MintOutput struct {
func (out *MintOutput) Verify() error {
switch {
case out == nil:
return errNilOutput
return ErrNilOutput
default:
return out.OutputOwners.Verify()
}
Expand Down
4 changes: 2 additions & 2 deletions vms/secp256k1fx/mint_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestMintOutputVerify(t *testing.T) {
{
name: "nil",
out: nil,
expectedErr: errNilOutput,
expectedErr: ErrNilOutput,
},
{
name: "invalid output owners",
Expand All @@ -30,7 +30,7 @@ func TestMintOutputVerify(t *testing.T) {
Addrs: []ids.ShortID{ids.GenerateTestShortID()},
},
},
expectedErr: errOutputUnspendable,
expectedErr: ErrOutputUnspendable,
},
{
name: "passes verification",
Expand Down
22 changes: 11 additions & 11 deletions vms/secp256k1fx/output_owners.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
)

var (
errNilOutput = errors.New("nil output")
errOutputUnspendable = errors.New("output is unspendable")
errOutputUnoptimized = errors.New("output representation should be optimized")
errAddrsNotSortedUnique = errors.New("addresses not sorted and unique")
errMarshal = errors.New("cannot marshal without ctx")
ErrNilOutput = errors.New("nil output")
ErrOutputUnspendable = errors.New("output is unspendable")
ErrOutputUnoptimized = errors.New("output representation should be optimized")
ErrAddrsNotSortedUnique = errors.New("addresses not sorted and unique")
ErrMarshal = errors.New("cannot marshal without ctx")

_ verify.State = (*OutputOwners)(nil)
)
Expand Down Expand Up @@ -57,13 +57,13 @@ func (out *OutputOwners) MarshalJSON() ([]byte, error) {
}

// Fields returns JSON keys in a map that can be used with marshal JSON
// to serialise OutputOwners struct
// to serialize OutputOwners struct
func (out *OutputOwners) Fields() (map[string]interface{}, error) {
addrsLen := len(out.Addrs)

// we need out.ctx to do this, if its absent, throw error
if addrsLen > 0 && out.ctx == nil {
return nil, errMarshal
return nil, ErrMarshal
}

addresses := make([]string, addrsLen)
Expand Down Expand Up @@ -123,13 +123,13 @@ func (out *OutputOwners) Equals(other *OutputOwners) bool {
func (out *OutputOwners) Verify() error {
switch {
case out == nil:
return errNilOutput
return ErrNilOutput
case out.Threshold > uint32(len(out.Addrs)):
return errOutputUnspendable
return ErrOutputUnspendable
case out.Threshold == 0 && len(out.Addrs) > 0:
return errOutputUnoptimized
return ErrOutputUnoptimized
case !utils.IsSortedAndUniqueSortable(out.Addrs):
return errAddrsNotSortedUnique
return ErrAddrsNotSortedUnique
default:
return nil
}
Expand Down
12 changes: 6 additions & 6 deletions vms/secp256k1fx/output_owners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,39 @@ func TestOutputOwnersVerify(t *testing.T) {
{
name: "nil",
out: nil,
expectedErr: errNilOutput,
expectedErr: ErrNilOutput,
},
{
name: "threshold > num addrs",
out: &OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{},
},
expectedErr: errOutputUnspendable,
expectedErr: ErrOutputUnspendable,
},
{
name: "unoptimized",
out: &OutputOwners{
Threshold: 0,
Addrs: []ids.ShortID{ids.GenerateTestShortID()},
},
expectedErr: errOutputUnoptimized,
expectedErr: ErrOutputUnoptimized,
},
{
name: "not sorted",
out: &OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{{2}, {1}},
},
expectedErr: errAddrsNotSortedUnique,
expectedErr: ErrAddrsNotSortedUnique,
},
{
name: "not unique",
out: &OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{{2}, {2}},
},
expectedErr: errAddrsNotSortedUnique,
expectedErr: ErrAddrsNotSortedUnique,
},
{
name: "passes verification",
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestMarshalJSONRequiresCtxWhenAddrsArePresent(t *testing.T) {
}

_, err := out.MarshalJSON()
require.ErrorIs(err, errMarshal)
require.ErrorIs(err, ErrMarshal)
}

func TestMarshalJSONDoesNotRequireCtxWhenAddrsAreAbsent(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion vms/secp256k1fx/transfer_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (in *TransferInput) Amount() uint64 {
func (in *TransferInput) Verify() error {
switch {
case in == nil:
return errNilInput
return ErrNilInput
case in.Amt == 0:
return ErrNoValueInput
default:
Expand Down
6 changes: 3 additions & 3 deletions vms/secp256k1fx/transfer_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestTransferInputVerify(t *testing.T) {
func TestTransferInputVerifyNil(t *testing.T) {
require := require.New(t)
in := (*TransferInput)(nil)
require.ErrorIs(in.Verify(), errNilInput)
require.ErrorIs(in.Verify(), ErrNilInput)
}

func TestTransferInputVerifyNoValue(t *testing.T) {
Expand All @@ -60,7 +60,7 @@ func TestTransferInputVerifyDuplicated(t *testing.T) {
SigIndices: []uint32{0, 0},
},
}
require.ErrorIs(in.Verify(), errNotSortedUnique)
require.ErrorIs(in.Verify(), ErrInputIndicesNotSortedUnique)
}

func TestTransferInputVerifyUnsorted(t *testing.T) {
Expand All @@ -71,7 +71,7 @@ func TestTransferInputVerifyUnsorted(t *testing.T) {
SigIndices: []uint32{1, 0},
},
}
require.ErrorIs(in.Verify(), errNotSortedUnique)
require.ErrorIs(in.Verify(), ErrInputIndicesNotSortedUnique)
}

func TestTransferInputSerialize(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion vms/secp256k1fx/transfer_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (out *TransferOutput) Amount() uint64 {
func (out *TransferOutput) Verify() error {
switch {
case out == nil:
return errNilOutput
return ErrNilOutput
case out.Amt == 0:
return ErrNoValueOutput
default:
Expand Down
Loading

0 comments on commit a152e62

Please sign in to comment.