Modify AnteHandler to take a simulate boolean parameter
This commit is contained in:
parent
7e8feec738
commit
76a16ab288
@ -608,7 +608,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
|
||||
|
||||
// run the ante handler
|
||||
if app.anteHandler != nil {
|
||||
newCtx, result, abort := app.anteHandler(ctx, tx)
|
||||
newCtx, result, abort := app.anteHandler(ctx, tx, (mode == runTxModeSimulate))
|
||||
if abort {
|
||||
return result
|
||||
}
|
||||
|
||||
@ -365,7 +365,7 @@ func testTxDecoder(cdc *wire.Codec) sdk.TxDecoder {
|
||||
}
|
||||
|
||||
func anteHandlerTxTest(t *testing.T, capKey *sdk.KVStoreKey, storeKey []byte) sdk.AnteHandler {
|
||||
return func(ctx sdk.Context, tx sdk.Tx) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
store := ctx.KVStore(capKey)
|
||||
msgCounter := tx.(txTest).Counter
|
||||
res = incrementingCounter(t, store, storeKey, msgCounter)
|
||||
@ -595,7 +595,7 @@ func TestSimulateTx(t *testing.T) {
|
||||
gasConsumed := int64(5)
|
||||
|
||||
anteOpt := func(bapp *BaseApp) {
|
||||
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasConsumed))
|
||||
return
|
||||
})
|
||||
@ -659,7 +659,9 @@ func TestSimulateTx(t *testing.T) {
|
||||
|
||||
func TestRunInvalidTransaction(t *testing.T) {
|
||||
anteOpt := func(bapp *BaseApp) {
|
||||
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx) (newCtx sdk.Context, res sdk.Result, abort bool) { return })
|
||||
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
return
|
||||
})
|
||||
}
|
||||
routerOpt := func(bapp *BaseApp) {
|
||||
bapp.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (res sdk.Result) { return })
|
||||
@ -734,7 +736,7 @@ func TestRunInvalidTransaction(t *testing.T) {
|
||||
func TestTxGasLimits(t *testing.T) {
|
||||
gasGranted := int64(10)
|
||||
anteOpt := func(bapp *BaseApp) {
|
||||
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasGranted))
|
||||
|
||||
// NOTE/TODO/XXX:
|
||||
@ -825,7 +827,7 @@ func TestTxGasLimits(t *testing.T) {
|
||||
func TestQuery(t *testing.T) {
|
||||
key, value := []byte("hello"), []byte("goodbye")
|
||||
anteOpt := func(bapp *BaseApp) {
|
||||
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
store := ctx.KVStore(capKey1)
|
||||
store.Set(key, value)
|
||||
return
|
||||
|
||||
@ -4,6 +4,7 @@ import "github.com/spf13/cobra"
|
||||
|
||||
// nolint
|
||||
const (
|
||||
DefaultGasLimit = 200000
|
||||
DefaultGasAdjustment = 1.2
|
||||
|
||||
FlagUseLedger = "ledger"
|
||||
@ -52,7 +53,7 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
|
||||
c.Flags().String(FlagChainID, "", "Chain ID of tendermint node")
|
||||
c.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to tendermint rpc interface for this chain")
|
||||
c.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device")
|
||||
c.Flags().Int64(FlagGas, 0, "gas limit to set per-transaction; set to 0 to calculate required gas automatically")
|
||||
c.Flags().Int64(FlagGas, DefaultGasLimit, "gas limit to set per-transaction; set to 0 to calculate required gas automatically")
|
||||
c.Flags().Float64(FlagGasAdjustment, DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation")
|
||||
c.Flags().Bool(FlagAsync, false, "broadcast transactions asynchronously")
|
||||
c.Flags().Bool(FlagJson, false, "return output in json format")
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tendermint/tendermint/libs/common"
|
||||
)
|
||||
|
||||
func TestParseQueryResponse(t *testing.T) {
|
||||
@ -18,3 +20,39 @@ func TestParseQueryResponse(t *testing.T) {
|
||||
assert.Equal(t, gas, int64(0))
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestCalculateGas(t *testing.T) {
|
||||
cdc := app.MakeCodec()
|
||||
makeQueryFunc := func(gasUsed int64, wantErr bool) func(string, common.HexBytes) ([]byte, error) {
|
||||
return func(string, common.HexBytes) ([]byte, error) {
|
||||
if wantErr {
|
||||
return nil, errors.New("")
|
||||
}
|
||||
return cdc.MustMarshalBinary(sdk.Result{GasUsed: gasUsed}), nil
|
||||
}
|
||||
}
|
||||
type args struct {
|
||||
queryFuncGasUsed int64
|
||||
queryFuncWantErr bool
|
||||
adjustment float64
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantEstimate int64
|
||||
wantAdjusted int64
|
||||
wantErr bool
|
||||
}{
|
||||
{"error", args{0, true, 1.2}, 0, 0, true},
|
||||
{"adjusted gas", args{10, false, 1.2}, 10, 12, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
queryFunc := makeQueryFunc(tt.args.queryFuncGasUsed, tt.args.queryFuncWantErr)
|
||||
gotEstimate, gotAdjusted, err := CalculateGas(queryFunc, cdc, []byte(""), tt.args.adjustment)
|
||||
assert.Equal(t, err != nil, tt.wantErr)
|
||||
assert.Equal(t, gotEstimate, tt.wantEstimate)
|
||||
assert.Equal(t, gotAdjusted, tt.wantAdjusted)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -211,7 +211,7 @@ func tx2Decoder(cdc *wire.Codec) sdk.TxDecoder {
|
||||
|
||||
// Simple anteHandler that ensures msg signers have signed.
|
||||
// Provides no replay protection.
|
||||
func antehandler(ctx sdk.Context, tx sdk.Tx) (_ sdk.Context, _ sdk.Result, abort bool) {
|
||||
func antehandler(ctx sdk.Context, tx sdk.Tx, simulate bool) (_ sdk.Context, _ sdk.Result, abort bool) {
|
||||
appTx, ok := tx.(app2Tx)
|
||||
if !ok {
|
||||
// set abort boolean to true so that we don't continue to process failed tx
|
||||
|
||||
@ -5,4 +5,4 @@ type Handler func(ctx Context, msg Msg) Result
|
||||
|
||||
// AnteHandler authenticates transactions, before their internal messages are handled.
|
||||
// If newCtx.IsZero(), ctx is used instead.
|
||||
type AnteHandler func(ctx Context, tx Tx) (newCtx Context, result Result, abort bool)
|
||||
type AnteHandler func(ctx Context, tx Tx, simulate bool) (newCtx Context, result Result, abort bool)
|
||||
|
||||
@ -25,7 +25,7 @@ const (
|
||||
func NewAnteHandler(am AccountMapper, fck FeeCollectionKeeper) sdk.AnteHandler {
|
||||
|
||||
return func(
|
||||
ctx sdk.Context, tx sdk.Tx,
|
||||
ctx sdk.Context, tx sdk.Tx, simulate bool,
|
||||
) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
||||
|
||||
// This AnteHandler requires Txs to be StdTxs
|
||||
@ -35,7 +35,7 @@ func NewAnteHandler(am AccountMapper, fck FeeCollectionKeeper) sdk.AnteHandler {
|
||||
}
|
||||
|
||||
// set the gas meter
|
||||
if stdTx.Fee.Gas == 0 {
|
||||
if simulate {
|
||||
newCtx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
||||
} else {
|
||||
newCtx = ctx.WithGasMeter(sdk.NewGasMeter(stdTx.Fee.Gas))
|
||||
|
||||
@ -39,16 +39,16 @@ func privAndAddr() (crypto.PrivKey, sdk.AccAddress) {
|
||||
}
|
||||
|
||||
// run the tx through the anteHandler and ensure its valid
|
||||
func checkValidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx) {
|
||||
_, result, abort := anteHandler(ctx, tx)
|
||||
func checkValidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, simulate bool) {
|
||||
_, result, abort := anteHandler(ctx, tx, simulate)
|
||||
require.False(t, abort)
|
||||
require.Equal(t, sdk.ABCICodeOK, result.Code)
|
||||
require.True(t, result.IsOK())
|
||||
}
|
||||
|
||||
// run the tx through the anteHandler and ensure it fails with the given code
|
||||
func checkInvalidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, code sdk.CodeType) {
|
||||
newCtx, result, abort := anteHandler(ctx, tx)
|
||||
func checkInvalidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, simulate bool, code sdk.CodeType) {
|
||||
newCtx, result, abort := anteHandler(ctx, tx, simulate)
|
||||
require.True(t, abort)
|
||||
require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, code), result.Code,
|
||||
fmt.Sprintf("Expected %v, got %v", sdk.ToABCICode(sdk.CodespaceRoot, code), result))
|
||||
@ -140,23 +140,23 @@ func TestAnteHandlerSigErrors(t *testing.T) {
|
||||
require.Equal(t, expectedSigners, stdTx.GetSigners())
|
||||
|
||||
// Check no signatures fails
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeUnauthorized)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnauthorized)
|
||||
|
||||
// test num sigs dont match GetSigners
|
||||
privs, accNums, seqs = []crypto.PrivKey{priv1}, []int64{0}, []int64{0}
|
||||
tx = newTestTx(ctx, msgs, privs, accNums, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeUnauthorized)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnauthorized)
|
||||
|
||||
// test an unrecognized account
|
||||
privs, accNums, seqs = []crypto.PrivKey{priv1, priv2, priv3}, []int64{0, 1, 2}, []int64{0, 0, 0}
|
||||
tx = newTestTx(ctx, msgs, privs, accNums, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeUnknownAddress)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnknownAddress)
|
||||
|
||||
// save the first account, but second is still unrecognized
|
||||
acc1 := mapper.NewAccountWithAddress(ctx, addr1)
|
||||
acc1.SetCoins(fee.Amount)
|
||||
mapper.SetAccount(ctx, acc1)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeUnknownAddress)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnknownAddress)
|
||||
}
|
||||
|
||||
// Test logic around account number checking with one signer and many signers.
|
||||
@ -192,17 +192,17 @@ func TestAnteHandlerAccountNumbers(t *testing.T) {
|
||||
// test good tx from one signer
|
||||
privs, accnums, seqs := []crypto.PrivKey{priv1}, []int64{0}, []int64{0}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
// new tx from wrong account number
|
||||
seqs = []int64{1}
|
||||
tx = newTestTx(ctx, msgs, privs, []int64{1}, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidSequence)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidSequence)
|
||||
|
||||
// from correct account number
|
||||
seqs = []int64{1}
|
||||
tx = newTestTx(ctx, msgs, privs, []int64{0}, seqs, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
// new tx with another signer and incorrect account numbers
|
||||
msg1 := newTestMsg(addr1, addr2)
|
||||
@ -210,12 +210,12 @@ func TestAnteHandlerAccountNumbers(t *testing.T) {
|
||||
msgs = []sdk.Msg{msg1, msg2}
|
||||
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []int64{1, 0}, []int64{2, 0}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidSequence)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidSequence)
|
||||
|
||||
// correct account numbers
|
||||
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []int64{0, 1}, []int64{2, 0}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
}
|
||||
|
||||
// Test logic around sequence checking with one signer and many signers.
|
||||
@ -255,15 +255,15 @@ func TestAnteHandlerSequences(t *testing.T) {
|
||||
// test good tx from one signer
|
||||
privs, accnums, seqs := []crypto.PrivKey{priv1}, []int64{0}, []int64{0}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
// test sending it again fails (replay protection)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidSequence)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidSequence)
|
||||
|
||||
// fix sequence, should pass
|
||||
seqs = []int64{1}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
// new tx with another signer and correct sequences
|
||||
msg1 := newTestMsg(addr1, addr2)
|
||||
@ -272,28 +272,28 @@ func TestAnteHandlerSequences(t *testing.T) {
|
||||
|
||||
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2, priv3}, []int64{0, 1, 2}, []int64{2, 0, 0}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
// replay fails
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidSequence)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidSequence)
|
||||
|
||||
// tx from just second signer with incorrect sequence fails
|
||||
msg = newTestMsg(addr2)
|
||||
msgs = []sdk.Msg{msg}
|
||||
privs, accnums, seqs = []crypto.PrivKey{priv2}, []int64{1}, []int64{0}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidSequence)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidSequence)
|
||||
|
||||
// fix the sequence and it passes
|
||||
tx = newTestTx(ctx, msgs, []crypto.PrivKey{priv2}, []int64{1}, []int64{1}, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
// another tx from both of them that passes
|
||||
msg = newTestMsg(addr1, addr2)
|
||||
msgs = []sdk.Msg{msg}
|
||||
privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []int64{0, 1}, []int64{3, 2}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
}
|
||||
|
||||
// Test logic around fee deduction.
|
||||
@ -323,17 +323,17 @@ func TestAnteHandlerFees(t *testing.T) {
|
||||
|
||||
// signer does not have enough funds to pay the fee
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInsufficientFunds)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds)
|
||||
|
||||
acc1.SetCoins(sdk.Coins{sdk.NewInt64Coin("atom", 149)})
|
||||
mapper.SetAccount(ctx, acc1)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInsufficientFunds)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds)
|
||||
|
||||
require.True(t, feeCollector.GetCollectedFees(ctx).IsEqual(emptyCoins))
|
||||
|
||||
acc1.SetCoins(sdk.Coins{sdk.NewInt64Coin("atom", 150)})
|
||||
mapper.SetAccount(ctx, acc1)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
require.True(t, feeCollector.GetCollectedFees(ctx).IsEqual(sdk.Coins{sdk.NewInt64Coin("atom", 150)}))
|
||||
}
|
||||
@ -360,26 +360,26 @@ func TestAnteHandlerMemoGas(t *testing.T) {
|
||||
var tx sdk.Tx
|
||||
msg := newTestMsg(addr1)
|
||||
privs, accnums, seqs := []crypto.PrivKey{priv1}, []int64{0}, []int64{0}
|
||||
fee := NewStdFee(1, sdk.NewInt64Coin("atom", 0))
|
||||
fee := NewStdFee(0, sdk.NewInt64Coin("atom", 0))
|
||||
|
||||
// tx does not have enough gas
|
||||
tx = newTestTx(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeOutOfGas)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeOutOfGas)
|
||||
|
||||
// tx with memo doesn't have enough gas
|
||||
fee = NewStdFee(801, sdk.NewInt64Coin("atom", 0))
|
||||
tx = newTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, "abcininasidniandsinasindiansdiansdinaisndiasndiadninsd")
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeOutOfGas)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeOutOfGas)
|
||||
|
||||
// memo too large
|
||||
fee = NewStdFee(2001, sdk.NewInt64Coin("atom", 0))
|
||||
tx = newTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, "abcininasidniandsinasindiansdiansdinaisndiasndiadninsdabcininasidniandsinasindiansdiansdinaisndiasndiadninsdabcininasidniandsinasindiansdiansdinaisndiasndiadninsd")
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeMemoTooLarge)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeMemoTooLarge)
|
||||
|
||||
// tx with memo has enough gas
|
||||
fee = NewStdFee(1100, sdk.NewInt64Coin("atom", 0))
|
||||
tx = newTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, "abcininasidniandsinasindiansdiansdinaisndiasndiadninsd")
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
}
|
||||
|
||||
func TestAnteHandlerMultiSigner(t *testing.T) {
|
||||
@ -420,17 +420,17 @@ func TestAnteHandlerMultiSigner(t *testing.T) {
|
||||
privs, accnums, seqs := []crypto.PrivKey{priv1, priv2, priv3}, []int64{0, 1, 2}, []int64{0, 0, 0}
|
||||
tx = newTestTxWithMemo(ctx, msgs, privs, accnums, seqs, fee, "Check signers are in expected order and different account numbers works")
|
||||
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
// change sequence numbers
|
||||
tx = newTestTx(ctx, []sdk.Msg{msg1}, []crypto.PrivKey{priv1, priv2}, []int64{0, 1}, []int64{1, 1}, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
tx = newTestTx(ctx, []sdk.Msg{msg2}, []crypto.PrivKey{priv3, priv1}, []int64{2, 0}, []int64{1, 2}, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
// expected seqs = [3, 2, 2]
|
||||
tx = newTestTxWithMemo(ctx, msgs, privs, accnums, []int64{3, 2, 2}, fee, "Check signers are in expected order and different account numbers and sequence numbers works")
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
}
|
||||
|
||||
func TestAnteHandlerBadSignBytes(t *testing.T) {
|
||||
@ -467,7 +467,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
|
||||
// test good tx and signBytes
|
||||
privs, accnums, seqs := []crypto.PrivKey{priv1}, []int64{0}, []int64{0}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
chainID := ctx.ChainID()
|
||||
chainID2 := chainID + "somemorestuff"
|
||||
@ -497,20 +497,20 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
|
||||
StdSignBytes(cs.chainID, cs.accnum, cs.seq, cs.fee, cs.msgs, ""),
|
||||
"",
|
||||
)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, cs.code)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, cs.code)
|
||||
}
|
||||
|
||||
// test wrong signer if public key exist
|
||||
privs, accnums, seqs = []crypto.PrivKey{priv2}, []int64{0}, []int64{1}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeUnauthorized)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnauthorized)
|
||||
|
||||
// test wrong signer if public doesn't exist
|
||||
msg = newTestMsg(addr2)
|
||||
msgs = []sdk.Msg{msg}
|
||||
privs, accnums, seqs = []crypto.PrivKey{priv1}, []int64{1}, []int64{0}
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidPubKey)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidPubKey)
|
||||
|
||||
}
|
||||
|
||||
@ -544,7 +544,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
|
||||
privs, accnums, seqs := []crypto.PrivKey{priv1}, []int64{0}, []int64{0}
|
||||
fee := newStdFee()
|
||||
tx = newTestTx(ctx, msgs, privs, accnums, seqs, fee)
|
||||
checkValidTx(t, anteHandler, ctx, tx)
|
||||
checkValidTx(t, anteHandler, ctx, tx, false)
|
||||
|
||||
acc1 = mapper.GetAccount(ctx, addr1)
|
||||
require.Equal(t, acc1.GetPubKey(), priv1.PubKey())
|
||||
@ -555,14 +555,14 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
|
||||
tx = newTestTx(ctx, msgs, privs, []int64{1}, seqs, fee)
|
||||
sigs := tx.(StdTx).GetSignatures()
|
||||
sigs[0].PubKey = nil
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidPubKey)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidPubKey)
|
||||
|
||||
acc2 = mapper.GetAccount(ctx, addr2)
|
||||
require.Nil(t, acc2.GetPubKey())
|
||||
|
||||
// test invalid signature and public key
|
||||
tx = newTestTx(ctx, msgs, privs, []int64{1}, seqs, fee)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, sdk.CodeInvalidPubKey)
|
||||
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInvalidPubKey)
|
||||
|
||||
acc2 = mapper.GetAccount(ctx, addr2)
|
||||
require.Nil(t, acc2.GetPubKey())
|
||||
|
||||
@ -24,7 +24,7 @@ func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) {
|
||||
// NewAnteHandler returns an AnteHandler that checks
|
||||
// whether msg type is activate or not
|
||||
func NewAnteHandler(k Keeper) sdk.AnteHandler {
|
||||
return func(ctx sdk.Context, tx sdk.Tx) (sdk.Context, sdk.Result, bool) {
|
||||
return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, sdk.Result, bool) {
|
||||
for _, msg := range tx.GetMsgs() {
|
||||
ok := k.Getter().GetBoolWithDefault(ctx, ActivatedParamKey(msg.Type()), false)
|
||||
if !ok {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user