Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Sequencing, Error Cleanup, Bash Tests Cleanup #177

Merged
merged 5 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 7 additions & 69 deletions errors/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,17 @@ import (
"fmt"
"reflect"

"github.com/pkg/errors"

abci "github.com/tendermint/abci/types"
)

var (
errDecoding = fmt.Errorf("Error decoding input")
errUnauthorized = fmt.Errorf("Unauthorized")
errInvalidSignature = fmt.Errorf("Invalid Signature")
errTooLarge = fmt.Errorf("Input size too large")
errNoSigners = fmt.Errorf("There are no signers")
errMissingSignature = fmt.Errorf("Signature missing")
errTooManySignatures = fmt.Errorf("Too many signatures")
errNoChain = fmt.Errorf("No chain id provided")
errTxEmpty = fmt.Errorf("The provided Tx is empty")
errWrongChain = fmt.Errorf("Wrong chain for tx")
errUnknownTxType = fmt.Errorf("Tx type unknown")
errInvalidFormat = fmt.Errorf("Invalid format")
errUnknownModule = fmt.Errorf("Unknown module")
errExpired = fmt.Errorf("Tx expired")
errUnknownKey = fmt.Errorf("Unknown key")
errDecoding = fmt.Errorf("Error decoding input")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, very correct with moving the errors

errUnauthorized = fmt.Errorf("Unauthorized")
errTooLarge = fmt.Errorf("Input size too large")
errMissingSignature = fmt.Errorf("Signature missing")
errUnknownTxType = fmt.Errorf("Tx type unknown")
errInvalidFormat = fmt.Errorf("Invalid format")
errUnknownModule = fmt.Errorf("Unknown module")

internalErr = abci.CodeType_InternalError
encodingErr = abci.CodeType_EncodingError
Expand Down Expand Up @@ -70,14 +60,6 @@ func IsUnknownModuleErr(err error) bool {
return IsSameError(errUnknownModule, err)
}

func ErrUnknownKey(mod string) TMError {
w := errors.Wrap(errUnknownKey, mod)
return WithCode(w, abci.CodeType_UnknownRequest)
}
func IsUnknownKeyErr(err error) bool {
return IsSameError(errUnknownKey, err)
}

func ErrInternal(msg string) TMError {
return New(msg, internalErr)
}
Expand All @@ -104,60 +86,16 @@ func IsUnauthorizedErr(err error) bool {
return HasErrorCode(err, unauthorized)
}

func ErrNoSigners() TMError {
return WithCode(errNoSigners, unauthorized)
}

func ErrMissingSignature() TMError {
return WithCode(errMissingSignature, unauthorized)
}
func IsMissingSignatureErr(err error) bool {
return IsSameError(errMissingSignature, err)
}

func ErrTooManySignatures() TMError {
return WithCode(errTooManySignatures, unauthorized)
}
func IsTooManySignaturesErr(err error) bool {
return IsSameError(errTooManySignatures, err)
}

func ErrInvalidSignature() TMError {
return WithCode(errInvalidSignature, unauthorized)
}
func IsInvalidSignatureErr(err error) bool {
return IsSameError(errInvalidSignature, err)
}

func ErrNoChain() TMError {
return WithCode(errNoChain, unauthorized)
}
func IsNoChainErr(err error) bool {
return IsSameError(errNoChain, err)
}

func ErrTxEmpty() TMError {
return WithCode(errTxEmpty, unauthorized)
}

func ErrWrongChain(chain string) TMError {
msg := errors.Wrap(errWrongChain, chain)
return WithCode(msg, unauthorized)
}
func IsWrongChainErr(err error) bool {
return IsSameError(errWrongChain, err)
}

func ErrTooLarge() TMError {
return WithCode(errTooLarge, encodingErr)
}
func IsTooLargeErr(err error) bool {
return IsSameError(errTooLarge, err)
}

func ErrExpired() TMError {
return WithCode(errExpired, unauthorized)
}
func IsExpiredErr(err error) bool {
return IsSameError(errExpired, err)
}
8 changes: 0 additions & 8 deletions errors/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func TestErrorMatches(t *testing.T) {
{errUnauthorized, ErrUnauthorized(), true},
{errMissingSignature, ErrUnauthorized(), false},
{errMissingSignature, ErrMissingSignature(), true},
{errWrongChain, ErrWrongChain("hakz"), true},
{errUnknownTxType, ErrUnknownTxType(holder{}), true},
{errUnknownTxType, ErrUnknownTxType("some text here..."), true},
{errUnknownTxType, ErrUnknownTxType(demoTx{5}.Wrap()), true},
Expand All @@ -66,13 +65,6 @@ func TestChecks(t *testing.T) {
{ErrDecoding(), IsDecodingErr, true},
{ErrUnauthorized(), IsDecodingErr, false},
{ErrUnauthorized(), IsUnauthorizedErr, true},
{ErrInvalidSignature(), IsInvalidSignatureErr, true},
// unauthorized includes InvalidSignature, but not visa versa
{ErrInvalidSignature(), IsUnauthorizedErr, true},
{ErrUnauthorized(), IsInvalidSignatureErr, false},
// make sure WrongChain works properly
{ErrWrongChain("fooz"), IsUnauthorizedErr, true},
{ErrWrongChain("barz"), IsWrongChainErr, true},
// make sure lots of things match InternalErr, but not everything
{ErrInternal("bad db connection"), IsInternalErr, true},
{Wrap(errors.New("wrapped")), IsInternalErr, true},
Expand Down
30 changes: 30 additions & 0 deletions modules/auth/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//nolint
package auth

import (
"fmt"

abci "github.com/tendermint/abci/types"
"github.com/tendermint/basecoin/errors"
)

var (
errInvalidSignature = fmt.Errorf("Invalid Signature") //move auth
errTooManySignatures = fmt.Errorf("Too many signatures") //move auth

unauthorized = abci.CodeType_Unauthorized
)

func ErrTooManySignatures() errors.TMError {
return errors.WithCode(errTooManySignatures, unauthorized)
}
func IsTooManySignaturesErr(err error) bool {
return errors.IsSameError(errTooManySignatures, err)
}

func ErrInvalidSignature() errors.TMError {
return errors.WithCode(errInvalidSignature, unauthorized)
}
func IsInvalidSignatureErr(err error) bool {
return errors.IsSameError(errInvalidSignature, err)
}
29 changes: 29 additions & 0 deletions modules/auth/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package auth

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/tendermint/basecoin/errors"
)

func TestChecks(t *testing.T) {
// TODO: make sure the Is and Err methods match
assert := assert.New(t)

cases := []struct {
err error
check func(error) bool
match bool
}{
// unauthorized includes InvalidSignature, but not visa versa
{ErrInvalidSignature(), IsInvalidSignatureErr, true},
{ErrInvalidSignature(), errors.IsUnauthorizedErr, true},
}

for i, tc := range cases {
match := tc.check(tc.err)
assert.Equal(tc.match, match, "%d", i)
}
}
6 changes: 3 additions & 3 deletions modules/auth/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (s *OneSig) Sign(pubkey crypto.PubKey, sig crypto.Signature) error {
return errors.ErrMissingSignature()
}
if !s.Empty() {
return errors.ErrTooManySignatures()
return ErrTooManySignatures()
}
// set the value once we are happy
s.Signed = signed
Expand All @@ -121,7 +121,7 @@ func (s *OneSig) Signers() ([]crypto.PubKey, error) {
return nil, errors.ErrMissingSignature()
}
if !s.Pubkey.VerifyBytes(s.SignBytes(), s.Sig) {
return nil, errors.ErrInvalidSignature()
return nil, ErrInvalidSignature()
}
return []crypto.PubKey{s.Pubkey}, nil
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func (s *MultiSig) Signers() ([]crypto.PubKey, error) {
for i := range s.Sigs {
ms := s.Sigs[i]
if !ms.Pubkey.VerifyBytes(data, ms.Sig) {
return nil, errors.ErrInvalidSignature()
return nil, ErrInvalidSignature()
}
keys[i] = ms.Pubkey
}
Expand Down
7 changes: 3 additions & 4 deletions modules/base/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package base

import (
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state"
)
Expand Down Expand Up @@ -48,7 +47,7 @@ func (c Chain) checkChainTx(chainID string, height uint64, tx basecoin.Tx) (base
// make sure it is a chaintx
ctx, ok := tx.Unwrap().(ChainTx)
if !ok {
return tx, errors.ErrNoChain()
return tx, ErrNoChain()
}

// basic validation
Expand All @@ -59,10 +58,10 @@ func (c Chain) checkChainTx(chainID string, height uint64, tx basecoin.Tx) (base

// compare against state
if ctx.ChainID != chainID {
return tx, errors.ErrWrongChain(ctx.ChainID)
return tx, ErrWrongChain(ctx.ChainID)
}
if ctx.ExpiresAt != 0 && ctx.ExpiresAt <= height {
return tx, errors.ErrExpired()
return tx, ErrExpired()
}
return ctx.Tx, nil
}
40 changes: 40 additions & 0 deletions modules/base/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//nolint
package base

import (
"fmt"

pkgerrors "github.com/pkg/errors"

abci "github.com/tendermint/abci/types"
"github.com/tendermint/basecoin/errors"
)

var (
errNoChain = fmt.Errorf("No chain id provided") //move base
errWrongChain = fmt.Errorf("Wrong chain for tx") //move base
errExpired = fmt.Errorf("Tx expired") //move base

unauthorized = abci.CodeType_Unauthorized
)

func ErrNoChain() errors.TMError {
return errors.WithCode(errNoChain, unauthorized)
}
func IsNoChainErr(err error) bool {
return errors.IsSameError(errNoChain, err)
}
func ErrWrongChain(chain string) errors.TMError {
msg := pkgerrors.Wrap(errWrongChain, chain)
return errors.WithCode(msg, unauthorized)
}
func IsWrongChainErr(err error) bool {
return errors.IsSameError(errWrongChain, err)
}

func ErrExpired() errors.TMError {
return errors.WithCode(errExpired, unauthorized)
}
func IsExpiredErr(err error) bool {
return errors.IsSameError(errExpired, err)
}
45 changes: 45 additions & 0 deletions modules/base/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package base

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/tendermint/basecoin/errors"
)

func TestErrorMatches(t *testing.T) {
assert := assert.New(t)

cases := []struct {
pattern, err error
match bool
}{
{errWrongChain, ErrWrongChain("hakz"), true},
}

for i, tc := range cases {
same := errors.IsSameError(tc.pattern, tc.err)
assert.Equal(tc.match, same, "%d: %#v / %#v", i, tc.pattern, tc.err)
}
}

func TestChecks(t *testing.T) {
// TODO: make sure the Is and Err methods match
assert := assert.New(t)

cases := []struct {
err error
check func(error) bool
match bool
}{
// make sure WrongChain works properly
{ErrWrongChain("fooz"), errors.IsUnauthorizedErr, true},
{ErrWrongChain("barz"), IsWrongChainErr, true},
}

for i, tc := range cases {
match := tc.check(tc.err)
assert.Equal(tc.match, match, "%d", i)
}
}
4 changes: 2 additions & 2 deletions modules/base/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func (c ChainTx) Wrap() basecoin.Tx {
}
func (c ChainTx) ValidateBasic() error {
if c.ChainID == "" {
return errors.ErrNoChain()
return ErrNoChain()
}
if !chainPattern.MatchString(c.ChainID) {
return errors.ErrWrongChain(c.ChainID)
return ErrWrongChain(c.ChainID)
}
if c.Tx.Empty() {
return errors.ErrUnknownTxType(c.Tx)
Expand Down
6 changes: 3 additions & 3 deletions modules/coin/commands/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

lc "github.com/tendermint/light-client"
lcmd "github.com/tendermint/basecoin/client/commands"
proofcmd "github.com/tendermint/basecoin/client/commands/proofs"
lc "github.com/tendermint/light-client"

"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/coin"
Expand All @@ -17,10 +17,10 @@ import (
var AccountQueryCmd = &cobra.Command{
Use: "account [address]",
Short: "Get details of an account, with proof",
RunE: lcmd.RequireInit(doAccountQuery),
RunE: lcmd.RequireInit(accountQueryCmd),
}

func doAccountQuery(cmd *cobra.Command, args []string) error {
func accountQueryCmd(cmd *cobra.Command, args []string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i forgot the naming scheme

addr, err := proofcmd.ParseHexKey(args, "address")
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions modules/coin/commands/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var SendTxCmd = &cobra.Command{
Use: "send",
Short: "send tokens from one account to another",
RunE: commands.RequireInit(doSendTx),
RunE: commands.RequireInit(sendTxCmd),
}

//nolint
Expand All @@ -31,8 +31,8 @@ func init() {
flags.String(FlagFrom, "", "Address sending coins, if not first signer")
}

// doSendTx is an example of how to make a tx
func doSendTx(cmd *cobra.Command, args []string) error {
// sendTxCmd is an example of how to make a tx
func sendTxCmd(cmd *cobra.Command, args []string) error {
// load data from json or flags
// var tx basecoin.Tx
// found, err := txcmd.LoadJSON(&tx)
Expand Down
Loading