PR changes
This commit is contained in:
committed by
Ethan Frey
parent
007230e583
commit
3d5cf393b9
@@ -0,0 +1,32 @@
|
||||
//nolint
|
||||
package nonce
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
|
||||
"github.com/tendermint/basecoin/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
errNoNonce = fmt.Errorf("Tx doesn't contain nonce")
|
||||
errNotMember = fmt.Errorf("nonce contains non-permissioned member")
|
||||
errZeroSequence = fmt.Errorf("Sequence number cannot be zero")
|
||||
|
||||
unauthorized = abci.CodeType_Unauthorized
|
||||
)
|
||||
|
||||
func ErrBadNonce(got, expected uint32) errors.TMError {
|
||||
return errors.WithCode(fmt.Errorf("Bad nonce sequence, got %d, expected %d", got, expected), unauthorized)
|
||||
}
|
||||
|
||||
func ErrNoNonce() errors.TMError {
|
||||
return errors.WithCode(errNoNonce, unauthorized)
|
||||
}
|
||||
func ErrNotMember() errors.TMError {
|
||||
return errors.WithCode(errNotMember, unauthorized)
|
||||
}
|
||||
func ErrZeroSequence() errors.TMError {
|
||||
return errors.WithCode(errZeroSequence, unauthorized)
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package nonce
|
||||
|
||||
import (
|
||||
"github.com/tendermint/basecoin"
|
||||
"github.com/tendermint/basecoin/errors"
|
||||
"github.com/tendermint/basecoin/stack"
|
||||
"github.com/tendermint/basecoin/state"
|
||||
)
|
||||
@@ -57,11 +56,16 @@ func (r ReplayCheck) checkIncrementNonceTx(ctx basecoin.Context, store state.KVS
|
||||
// make sure it is a the nonce Tx (Tx from this package)
|
||||
nonceTx, ok := tx.Unwrap().(Tx)
|
||||
if !ok {
|
||||
return tx, errors.ErrNoNonce()
|
||||
return tx, ErrNoNonce()
|
||||
}
|
||||
|
||||
err := nonceTx.ValidateBasic()
|
||||
if err != nil {
|
||||
return tx, err
|
||||
}
|
||||
|
||||
// check the nonce sequence number
|
||||
err := nonceTx.CheckIncrementSeq(ctx, store)
|
||||
err = nonceTx.CheckIncrementSeq(ctx, store)
|
||||
if err != nil {
|
||||
return tx, err
|
||||
}
|
||||
|
||||
+3
-3
@@ -52,7 +52,7 @@ func (n Tx) ValidateBasic() error {
|
||||
case n.Tx.Empty():
|
||||
return errors.ErrTxEmpty()
|
||||
case n.Sequence == 0:
|
||||
return errors.ErrZeroSequence()
|
||||
return ErrZeroSequence()
|
||||
case len(n.Signers) == 0:
|
||||
return errors.ErrNoSigners()
|
||||
}
|
||||
@@ -73,13 +73,13 @@ func (n Tx) CheckIncrementSeq(ctx basecoin.Context, store state.KVStore) error {
|
||||
return err
|
||||
}
|
||||
if n.Sequence != cur+1 {
|
||||
return errors.ErrBadNonce(n.Sequence, cur+1)
|
||||
return ErrBadNonce(n.Sequence, cur+1)
|
||||
}
|
||||
|
||||
// make sure they all signed
|
||||
for _, s := range n.Signers {
|
||||
if !ctx.HasPermission(s) {
|
||||
return errors.ErrNotMember()
|
||||
return ErrNotMember()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,13 +17,25 @@ func TestNonce(t *testing.T) {
|
||||
|
||||
// generic args here...
|
||||
chainID := "my-chain"
|
||||
chain2ID := "woohoo"
|
||||
|
||||
height := uint64(100)
|
||||
ctx := stack.MockContext(chainID, height)
|
||||
store := state.NewMemKVStore()
|
||||
|
||||
act1 := basecoin.Actor{App: "fooz", Address: []byte{1, 2, 3, 4}}
|
||||
act2 := basecoin.Actor{App: "fooz", Address: []byte{1, 1, 1, 1}}
|
||||
act3 := basecoin.Actor{App: "fooz", Address: []byte{3, 3, 3, 3}}
|
||||
appName1 := "fooz"
|
||||
appName2 := "foot"
|
||||
|
||||
//root actors for the tests
|
||||
act1 := basecoin.Actor{ChainID: chainID, App: appName1, Address: []byte{1, 2, 3, 4}}
|
||||
act2 := basecoin.Actor{ChainID: chainID, App: appName1, Address: []byte{1, 1, 1, 1}}
|
||||
act3 := basecoin.Actor{ChainID: chainID, App: appName1, Address: []byte{3, 3, 3, 3}}
|
||||
act1DiffChain := basecoin.Actor{ChainID: chain2ID, App: appName1, Address: []byte{1, 2, 3, 4}}
|
||||
act2DiffChain := basecoin.Actor{ChainID: chain2ID, App: appName1, Address: []byte{1, 1, 1, 1}}
|
||||
act3DiffChain := basecoin.Actor{ChainID: chain2ID, App: appName1, Address: []byte{3, 3, 3, 3}}
|
||||
act1DiffApp := basecoin.Actor{ChainID: chainID, App: appName2, Address: []byte{1, 2, 3, 4}}
|
||||
act2DiffApp := basecoin.Actor{ChainID: chainID, App: appName2, Address: []byte{1, 1, 1, 1}}
|
||||
act3DiffApp := basecoin.Actor{ChainID: chainID, App: appName2, Address: []byte{3, 3, 3, 3}}
|
||||
|
||||
// let's construct some tests to make the table a bit less verbose
|
||||
set0 := []basecoin.Actor{}
|
||||
@@ -34,6 +46,12 @@ func TestNonce(t *testing.T) {
|
||||
set123 := []basecoin.Actor{act1, act2, act3}
|
||||
set321 := []basecoin.Actor{act3, act2, act1}
|
||||
|
||||
//some more test cases for different chains and apps for each actor
|
||||
set123Chain2 := []basecoin.Actor{act1DiffChain, act2DiffChain, act3DiffChain}
|
||||
set123App2 := []basecoin.Actor{act1DiffApp, act2DiffApp, act3DiffApp}
|
||||
set123MixedChains := []basecoin.Actor{act1, act2DiffChain, act3}
|
||||
set123MixedApps := []basecoin.Actor{act1, act2DiffApp, act3}
|
||||
|
||||
testList := []struct {
|
||||
valid bool
|
||||
seq uint32
|
||||
@@ -60,12 +78,18 @@ func TestNonce(t *testing.T) {
|
||||
{false, 2, set12, set12}, // but can't repeat sequence
|
||||
{true, 3, set12, set321}, // no effect from extra sigs
|
||||
|
||||
// tripple sigs also work
|
||||
// triple sigs also work
|
||||
{false, 2, set123, set123}, // must start with seq=1
|
||||
{false, 1, set123, set12}, // all must sign
|
||||
{true, 1, set123, set321}, // this works
|
||||
{true, 2, set321, set321}, // other order is the same
|
||||
{false, 2, set321, set321}, // no repetition
|
||||
|
||||
// signers from different chain and apps
|
||||
{false, 3, set123, set123Chain2}, // sign with different chain actors
|
||||
{false, 3, set123, set123App2}, // sign with different app actors
|
||||
{false, 3, set123, set123MixedChains}, // sign with mixed chain actor
|
||||
{false, 3, set123, set123MixedApps}, // sign with mixed app actors
|
||||
}
|
||||
|
||||
raw := stack.NewRawTx([]byte{42})
|
||||
|
||||
Reference in New Issue
Block a user