2019-08-15 08:19:16 -07:00
package ante_test
2018-03-12 17:40:04 -07:00
import (
2019-10-23 07:43:58 -07:00
"encoding/json"
2019-12-27 09:57:54 -08:00
"errors"
2018-06-20 12:27:36 -07:00
"fmt"
2019-01-16 09:30:57 -08:00
"math/rand"
2018-12-10 02:48:19 -08:00
"strings"
2018-03-12 17:40:04 -07:00
"testing"
2018-03-17 11:54:18 -07:00
"github.com/stretchr/testify/require"
2018-06-28 17:54:47 -07:00
"github.com/tendermint/tendermint/crypto"
2018-07-25 13:43:37 -07:00
"github.com/tendermint/tendermint/crypto/ed25519"
2018-11-15 06:30:24 -08:00
"github.com/tendermint/tendermint/crypto/multisig"
2018-08-31 10:04:11 -07:00
"github.com/tendermint/tendermint/crypto/secp256k1"
2018-12-10 06:27:25 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2019-10-10 05:46:09 -07:00
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
2019-08-15 08:19:16 -07:00
"github.com/cosmos/cosmos-sdk/x/auth/ante"
2019-06-05 16:26:17 -07:00
"github.com/cosmos/cosmos-sdk/x/auth/types"
2018-03-12 17:40:04 -07:00
)
// run the tx through the anteHandler and ensure its valid
2018-08-25 12:12:14 -07:00
func checkValidTx ( t * testing . T , anteHandler sdk . AnteHandler , ctx sdk . Context , tx sdk . Tx , simulate bool ) {
2019-10-10 05:46:09 -07:00
_ , err := anteHandler ( ctx , tx , simulate )
require . Nil ( t , err )
2018-03-12 17:40:04 -07:00
}
// run the tx through the anteHandler and ensure it fails with the given code
2019-12-27 09:57:54 -08:00
func checkInvalidTx ( t * testing . T , anteHandler sdk . AnteHandler , ctx sdk . Context , tx sdk . Tx , simulate bool , expErr error ) {
2019-10-10 05:46:09 -07:00
_ , err := anteHandler ( ctx , tx , simulate )
require . NotNil ( t , err )
2019-12-27 09:57:54 -08:00
require . True ( t , errors . Is ( expErr , err ) )
2018-03-12 17:40:04 -07:00
}
2019-10-15 13:21:54 -07:00
// Test that simulate transaction accurately estimates gas cost
func TestSimulateGasCost ( t * testing . T ) {
// setup
app , ctx := createTestApp ( true )
ctx = ctx . WithBlockHeight ( 1 )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
// keys and addresses
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
priv2 , _ , addr2 := types . KeyTestPubAddr ( )
priv3 , _ , addr3 := types . KeyTestPubAddr ( )
// set the accounts
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
require . NoError ( t , acc1 . SetAccountNumber ( 0 ) )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , acc1 . GetAddress ( ) , types . NewTestCoins ( ) )
2019-10-15 13:21:54 -07:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
require . NoError ( t , acc2 . SetAccountNumber ( 1 ) )
app . AccountKeeper . SetAccount ( ctx , acc2 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , acc2 . GetAddress ( ) , types . NewTestCoins ( ) )
2019-10-15 13:21:54 -07:00
acc3 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr3 )
require . NoError ( t , acc3 . SetAccountNumber ( 2 ) )
app . AccountKeeper . SetAccount ( ctx , acc3 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , acc3 . GetAddress ( ) , types . NewTestCoins ( ) )
2019-10-15 13:21:54 -07:00
// set up msgs and fee
var tx sdk . Tx
msg1 := types . NewTestMsg ( addr1 , addr2 )
msg2 := types . NewTestMsg ( addr3 , addr1 )
msg3 := types . NewTestMsg ( addr2 , addr3 )
msgs := [ ] sdk . Msg { msg1 , msg2 , msg3 }
fee := types . NewTestStdFee ( )
// signers in order. accnums are all 0 because it is in genesis block
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 , priv2 , priv3 } , [ ] uint64 { 0 , 1 , 2 } , [ ] uint64 { 0 , 0 , 0 }
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
cc , _ := ctx . CacheContext ( )
newCtx , err := anteHandler ( cc , tx , true )
require . Nil ( t , err , "transaction failed on simulate mode" )
simulatedGas := newCtx . GasMeter ( ) . GasConsumed ( )
fee . Gas = simulatedGas
// update tx with simulated gas estimate
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
_ , err = anteHandler ( ctx , tx , false )
require . Nil ( t , err , "transaction failed with gas estimate" )
}
2018-03-12 17:40:04 -07:00
// Test various error cases in the AnteHandler control flow.
func TestAnteHandlerSigErrors ( t * testing . T ) {
// setup
2019-08-15 08:19:16 -07:00
app , ctx := createTestApp ( true )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-03-12 17:40:04 -07:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
priv2 , _ , addr2 := types . KeyTestPubAddr ( )
priv3 , _ , addr3 := types . KeyTestPubAddr ( )
2018-03-12 17:40:04 -07:00
// msg and signatures
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg1 := types . NewTestMsg ( addr1 , addr2 )
msg2 := types . NewTestMsg ( addr1 , addr3 )
fee := types . NewTestStdFee ( )
2018-03-12 17:40:04 -07:00
2018-06-21 15:05:25 -07:00
msgs := [ ] sdk . Msg { msg1 , msg2 }
2018-03-12 17:40:04 -07:00
// test no signatures
2018-11-26 03:29:21 -08:00
privs , accNums , seqs := [ ] crypto . PrivKey { } , [ ] uint64 { } , [ ] uint64 { }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accNums , seqs , fee )
2018-06-21 15:05:25 -07:00
// tx.GetSigners returns addresses in correct order: addr1, addr2, addr3
2018-07-06 00:06:53 -07:00
expectedSigners := [ ] sdk . AccAddress { addr1 , addr2 , addr3 }
2019-06-05 16:26:17 -07:00
stdTx := tx . ( types . StdTx )
2018-06-29 18:10:15 -07:00
require . Equal ( t , expectedSigners , stdTx . GetSigners ( ) )
2018-06-21 15:05:25 -07:00
// Check no signatures fails
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrNoSignatures )
2018-03-12 17:40:04 -07:00
// test num sigs dont match GetSigners
2018-11-26 03:29:21 -08:00
privs , accNums , seqs = [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accNums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnauthorized )
2018-03-12 17:40:04 -07:00
// test an unrecognized account
2018-11-26 03:29:21 -08:00
privs , accNums , seqs = [ ] crypto . PrivKey { priv1 , priv2 , priv3 } , [ ] uint64 { 0 , 1 , 2 } , [ ] uint64 { 0 , 0 , 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accNums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnknownAddress )
2018-03-12 17:40:04 -07:00
// save the first account, but second is still unrecognized
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , fee . Amount )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnknownAddress )
2018-03-12 17:40:04 -07:00
}
2018-06-11 19:30:54 -07:00
// Test logic around account number checking with one signer and many signers.
func TestAnteHandlerAccountNumbers ( t * testing . T ) {
// setup
2019-10-23 07:43:58 -07:00
app , ctx := createTestApp ( false )
2019-08-15 08:19:16 -07:00
ctx = ctx . WithBlockHeight ( 1 )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-06-11 19:30:54 -07:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
priv2 , _ , addr2 := types . KeyTestPubAddr ( )
2018-06-11 19:30:54 -07:00
// set the accounts
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc1 . SetAccountNumber ( 0 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , types . NewTestCoins ( ) )
2019-08-15 08:19:16 -07:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc2 . SetAccountNumber ( 1 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc2 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr2 , types . NewTestCoins ( ) )
2018-06-11 19:30:54 -07:00
// msg and signatures
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg := types . NewTestMsg ( addr1 )
fee := types . NewTestStdFee ( )
2018-06-11 19:30:54 -07:00
2018-06-21 15:05:25 -07:00
msgs := [ ] sdk . Msg { msg }
2018-06-11 19:30:54 -07:00
// test good tx from one signer
2018-11-26 03:29:21 -08:00
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-06-11 19:30:54 -07:00
// new tx from wrong account number
2018-11-26 03:29:21 -08:00
seqs = [ ] uint64 { 1 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , [ ] uint64 { 1 } , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnauthorized )
2018-06-11 19:30:54 -07:00
// from correct account number
2018-11-26 03:29:21 -08:00
seqs = [ ] uint64 { 1 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , [ ] uint64 { 0 } , seqs , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-06-11 19:30:54 -07:00
// new tx with another signer and incorrect account numbers
2019-06-10 02:57:38 -07:00
msg1 := types . NewTestMsg ( addr1 , addr2 )
msg2 := types . NewTestMsg ( addr2 , addr1 )
2018-06-21 15:05:25 -07:00
msgs = [ ] sdk . Msg { msg1 , msg2 }
2018-11-26 03:29:21 -08:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv1 , priv2 } , [ ] uint64 { 1 , 0 } , [ ] uint64 { 2 , 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnauthorized )
2018-06-11 19:30:54 -07:00
// correct account numbers
2018-11-26 03:29:21 -08:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv1 , priv2 } , [ ] uint64 { 0 , 1 } , [ ] uint64 { 2 , 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-06-11 19:30:54 -07:00
}
2018-10-19 11:00:27 -07:00
// Test logic around account number checking with many signers when BlockHeight is 0.
func TestAnteHandlerAccountNumbersAtBlockHeightZero ( t * testing . T ) {
// setup
2019-10-23 07:43:58 -07:00
app , ctx := createTestApp ( false )
2019-08-15 08:19:16 -07:00
ctx = ctx . WithBlockHeight ( 0 )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-10-19 11:00:27 -07:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
priv2 , _ , addr2 := types . KeyTestPubAddr ( )
2018-10-19 11:00:27 -07:00
2019-06-28 13:11:27 -07:00
// set the accounts, we don't need the acc numbers as it is in the genesis block
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , types . NewTestCoins ( ) )
2019-08-15 08:19:16 -07:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc2 . SetAccountNumber ( 1 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc2 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr2 , types . NewTestCoins ( ) )
2018-10-19 11:00:27 -07:00
// msg and signatures
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg := types . NewTestMsg ( addr1 )
fee := types . NewTestStdFee ( )
2018-10-19 11:00:27 -07:00
msgs := [ ] sdk . Msg { msg }
// test good tx from one signer
2018-11-26 03:29:21 -08:00
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-10-19 11:00:27 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
// new tx from wrong account number
2018-11-26 03:29:21 -08:00
seqs = [ ] uint64 { 1 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , [ ] uint64 { 1 } , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnauthorized )
2018-10-19 11:00:27 -07:00
// from correct account number
2018-11-26 03:29:21 -08:00
seqs = [ ] uint64 { 1 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , [ ] uint64 { 0 } , seqs , fee )
2018-10-19 11:00:27 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
// new tx with another signer and incorrect account numbers
2019-06-10 02:57:38 -07:00
msg1 := types . NewTestMsg ( addr1 , addr2 )
msg2 := types . NewTestMsg ( addr2 , addr1 )
2018-10-19 11:00:27 -07:00
msgs = [ ] sdk . Msg { msg1 , msg2 }
2018-11-26 03:29:21 -08:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv1 , priv2 } , [ ] uint64 { 1 , 0 } , [ ] uint64 { 2 , 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnauthorized )
2018-10-19 11:00:27 -07:00
// correct account numbers
2018-11-26 03:29:21 -08:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv1 , priv2 } , [ ] uint64 { 0 , 0 } , [ ] uint64 { 2 , 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-10-19 11:00:27 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
}
2018-03-12 17:40:04 -07:00
// Test logic around sequence checking with one signer and many signers.
func TestAnteHandlerSequences ( t * testing . T ) {
// setup
2019-10-23 07:43:58 -07:00
app , ctx := createTestApp ( false )
2019-08-15 08:19:16 -07:00
ctx = ctx . WithBlockHeight ( 1 )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-03-12 17:40:04 -07:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
priv2 , _ , addr2 := types . KeyTestPubAddr ( )
priv3 , _ , addr3 := types . KeyTestPubAddr ( )
2018-03-12 17:40:04 -07:00
// set the accounts
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc1 . SetAccountNumber ( 0 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , types . NewTestCoins ( ) )
2019-08-15 08:19:16 -07:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc2 . SetAccountNumber ( 1 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc2 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr2 , types . NewTestCoins ( ) )
2019-08-15 08:19:16 -07:00
acc3 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr3 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc3 . SetAccountNumber ( 2 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc3 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr3 , types . NewTestCoins ( ) )
2018-03-12 17:40:04 -07:00
// msg and signatures
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg := types . NewTestMsg ( addr1 )
fee := types . NewTestStdFee ( )
2018-03-12 17:40:04 -07:00
2018-06-21 15:05:25 -07:00
msgs := [ ] sdk . Msg { msg }
2018-03-12 17:40:04 -07:00
// test good tx from one signer
2018-11-26 03:29:21 -08:00
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-03-12 17:40:04 -07:00
// test sending it again fails (replay protection)
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnauthorized )
2018-03-12 17:40:04 -07:00
// fix sequence, should pass
2018-11-26 03:29:21 -08:00
seqs = [ ] uint64 { 1 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-03-12 17:40:04 -07:00
// new tx with another signer and correct sequences
2019-06-10 02:57:38 -07:00
msg1 := types . NewTestMsg ( addr1 , addr2 )
msg2 := types . NewTestMsg ( addr3 , addr1 )
2018-06-21 15:05:25 -07:00
msgs = [ ] sdk . Msg { msg1 , msg2 }
2018-11-26 03:29:21 -08:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv1 , priv2 , priv3 } , [ ] uint64 { 0 , 1 , 2 } , [ ] uint64 { 2 , 0 , 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-03-12 17:40:04 -07:00
// replay fails
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnauthorized )
2018-03-12 17:40:04 -07:00
// tx from just second signer with incorrect sequence fails
2019-06-10 02:57:38 -07:00
msg = types . NewTestMsg ( addr2 )
2018-06-21 15:05:25 -07:00
msgs = [ ] sdk . Msg { msg }
2018-11-26 03:29:21 -08:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv2 } , [ ] uint64 { 1 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrUnauthorized )
2018-03-12 17:40:04 -07:00
// fix the sequence and it passes
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , [ ] crypto . PrivKey { priv2 } , [ ] uint64 { 1 } , [ ] uint64 { 1 } , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-03-12 17:40:04 -07:00
// another tx from both of them that passes
2019-06-10 02:57:38 -07:00
msg = types . NewTestMsg ( addr1 , addr2 )
2018-06-21 15:05:25 -07:00
msgs = [ ] sdk . Msg { msg }
2018-11-26 03:29:21 -08:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv1 , priv2 } , [ ] uint64 { 0 , 1 } , [ ] uint64 { 3 , 2 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-03-12 17:40:04 -07:00
}
2018-03-15 09:37:39 -07:00
// Test logic around fee deduction.
func TestAnteHandlerFees ( t * testing . T ) {
2018-03-17 13:20:24 -07:00
// setup
2019-08-15 08:19:16 -07:00
app , ctx := createTestApp ( true )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-03-17 13:20:24 -07:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
2018-03-17 13:20:24 -07:00
// set the accounts
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2018-03-17 13:20:24 -07:00
// msg and signatures
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg := types . NewTestMsg ( addr1 )
2018-11-26 03:29:21 -08:00
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
fee := types . NewTestStdFee ( )
2018-06-21 15:05:25 -07:00
msgs := [ ] sdk . Msg { msg }
2018-03-17 13:20:24 -07:00
// signer does not have enough funds to pay the fee
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrInsufficientFunds )
2018-03-17 13:20:24 -07:00
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , sdk . NewCoins ( sdk . NewInt64Coin ( "atom" , 149 ) ) )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrInsufficientFunds )
2018-03-17 13:20:24 -07:00
2020-01-30 13:31:16 -08:00
modAcc := app . SupplyKeeper . GetModuleAccount ( ctx , types . FeeCollectorName )
require . True ( t , app . BankKeeper . GetAllBalances ( ctx , modAcc . GetAddress ( ) ) . Empty ( ) )
require . True ( sdk . IntEq ( t , app . BankKeeper . GetAllBalances ( ctx , addr1 ) . AmountOf ( "atom" ) , sdk . NewInt ( 149 ) ) )
2018-05-25 21:10:09 -07:00
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , sdk . NewCoins ( sdk . NewInt64Coin ( "atom" , 150 ) ) )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-05-25 21:10:09 -07:00
2020-01-30 13:31:16 -08:00
require . True ( sdk . IntEq ( t , app . BankKeeper . GetAllBalances ( ctx , modAcc . GetAddress ( ) ) . AmountOf ( "atom" ) , sdk . NewInt ( 150 ) ) )
require . True ( sdk . IntEq ( t , app . BankKeeper . GetAllBalances ( ctx , addr1 ) . AmountOf ( "atom" ) , sdk . NewInt ( 0 ) ) )
2018-03-15 09:37:39 -07:00
}
2018-06-20 12:27:36 -07:00
// Test logic around memo gas consumption.
func TestAnteHandlerMemoGas ( t * testing . T ) {
// setup
2019-08-15 08:19:16 -07:00
app , ctx := createTestApp ( true )
ctx = ctx . WithBlockHeight ( 1 )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-06-20 12:27:36 -07:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
2018-06-20 12:27:36 -07:00
// set the accounts
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc1 . SetAccountNumber ( 0 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc1 )
2018-06-20 12:27:36 -07:00
// msg and signatures
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg := types . NewTestMsg ( addr1 )
2018-11-26 03:29:21 -08:00
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
2019-07-29 08:27:01 -07:00
fee := types . NewStdFee ( 0 , sdk . NewCoins ( sdk . NewInt64Coin ( "atom" , 0 ) ) )
2018-06-20 12:27:36 -07:00
// tx does not have enough gas
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , [ ] sdk . Msg { msg } , privs , accnums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrOutOfGas )
2018-06-20 12:27:36 -07:00
// tx with memo doesn't have enough gas
2019-07-29 08:27:01 -07:00
fee = types . NewStdFee ( 801 , sdk . NewCoins ( sdk . NewInt64Coin ( "atom" , 0 ) ) )
2019-06-10 02:57:38 -07:00
tx = types . NewTestTxWithMemo ( ctx , [ ] sdk . Msg { msg } , privs , accnums , seqs , fee , "abcininasidniandsinasindiansdiansdinaisndiasndiadninsd" )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrOutOfGas )
2018-06-20 12:27:36 -07:00
// memo too large
2019-10-10 05:46:09 -07:00
fee = types . NewStdFee ( 50000 , sdk . NewCoins ( sdk . NewInt64Coin ( "atom" , 0 ) ) )
2019-06-10 02:57:38 -07:00
tx = types . NewTestTxWithMemo ( ctx , [ ] sdk . Msg { msg } , privs , accnums , seqs , fee , strings . Repeat ( "01234567890" , 500 ) )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrMemoTooLarge )
2018-06-20 12:27:36 -07:00
// tx with memo has enough gas
2019-10-10 05:46:09 -07:00
fee = types . NewStdFee ( 50000 , sdk . NewCoins ( sdk . NewInt64Coin ( "atom" , 0 ) ) )
2019-06-10 02:57:38 -07:00
tx = types . NewTestTxWithMemo ( ctx , [ ] sdk . Msg { msg } , privs , accnums , seqs , fee , strings . Repeat ( "0123456789" , 10 ) )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-06-21 15:05:25 -07:00
}
func TestAnteHandlerMultiSigner ( t * testing . T ) {
// setup
2019-10-23 07:43:58 -07:00
app , ctx := createTestApp ( false )
2019-08-15 08:19:16 -07:00
ctx = ctx . WithBlockHeight ( 1 )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-06-21 15:05:25 -07:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
priv2 , _ , addr2 := types . KeyTestPubAddr ( )
priv3 , _ , addr3 := types . KeyTestPubAddr ( )
2018-06-21 15:05:25 -07:00
// set the accounts
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc1 . SetAccountNumber ( 0 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , types . NewTestCoins ( ) )
2019-08-15 08:19:16 -07:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc2 . SetAccountNumber ( 1 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc2 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr2 , types . NewTestCoins ( ) )
2019-08-15 08:19:16 -07:00
acc3 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr3 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc3 . SetAccountNumber ( 2 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc3 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr3 , types . NewTestCoins ( ) )
2018-06-21 15:05:25 -07:00
// set up msgs and fee
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg1 := types . NewTestMsg ( addr1 , addr2 )
msg2 := types . NewTestMsg ( addr3 , addr1 )
msg3 := types . NewTestMsg ( addr2 , addr3 )
2018-06-21 15:05:25 -07:00
msgs := [ ] sdk . Msg { msg1 , msg2 , msg3 }
2019-06-10 02:57:38 -07:00
fee := types . NewTestStdFee ( )
2018-06-21 15:05:25 -07:00
// signers in order
2018-11-26 03:29:21 -08:00
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 , priv2 , priv3 } , [ ] uint64 { 0 , 1 , 2 } , [ ] uint64 { 0 , 0 , 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTxWithMemo ( ctx , msgs , privs , accnums , seqs , fee , "Check signers are in expected order and different account numbers works" )
2018-06-21 15:05:25 -07:00
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-06-21 15:05:25 -07:00
// change sequence numbers
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , [ ] sdk . Msg { msg1 } , [ ] crypto . PrivKey { priv1 , priv2 } , [ ] uint64 { 0 , 1 } , [ ] uint64 { 1 , 1 } , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , [ ] sdk . Msg { msg2 } , [ ] crypto . PrivKey { priv3 , priv1 } , [ ] uint64 { 2 , 0 } , [ ] uint64 { 1 , 2 } , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-06-21 15:05:25 -07:00
// expected seqs = [3, 2, 2]
2019-06-10 02:57:38 -07:00
tx = types . NewTestTxWithMemo ( ctx , msgs , privs , accnums , [ ] uint64 { 3 , 2 , 2 } , fee , "Check signers are in expected order and different account numbers and sequence numbers works" )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-06-20 12:27:36 -07:00
}
2018-03-12 17:40:04 -07:00
func TestAnteHandlerBadSignBytes ( t * testing . T ) {
2018-03-16 17:57:28 -07:00
// setup
2019-08-15 08:19:16 -07:00
app , ctx := createTestApp ( true )
ctx = ctx . WithBlockHeight ( 1 )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-03-16 17:57:28 -07:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
priv2 , _ , addr2 := types . KeyTestPubAddr ( )
2018-03-16 17:57:28 -07:00
// set the accounts
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc1 . SetAccountNumber ( 0 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , types . NewTestCoins ( ) )
2019-08-15 08:19:16 -07:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc2 . SetAccountNumber ( 1 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc2 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr2 , types . NewTestCoins ( ) )
2018-03-16 17:57:28 -07:00
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg := types . NewTestMsg ( addr1 )
2018-06-21 15:05:25 -07:00
msgs := [ ] sdk . Msg { msg }
2019-06-10 02:57:38 -07:00
fee := types . NewTestStdFee ( )
fee2 := types . NewTestStdFee ( )
2018-03-17 13:53:27 -07:00
fee2 . Gas += 100
2019-06-10 02:57:38 -07:00
fee3 := types . NewTestStdFee ( )
2018-06-15 14:16:45 -07:00
fee3 . Amount [ 0 ] . Amount = fee3 . Amount [ 0 ] . Amount . AddRaw ( 100 )
2018-03-16 17:57:28 -07:00
// test good tx and signBytes
2018-11-26 03:29:21 -08:00
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-03-16 17:57:28 -07:00
2018-03-17 11:54:18 -07:00
chainID := ctx . ChainID ( )
2018-03-17 13:53:27 -07:00
chainID2 := chainID + "somemorestuff"
2019-12-27 09:57:54 -08:00
errUnauth := sdkerrors . ErrUnauthorized
2018-03-17 11:54:18 -07:00
cases := [ ] struct {
chainID string
2018-11-26 03:29:21 -08:00
accnum uint64
seq uint64
2019-07-29 08:27:01 -07:00
fee types . StdFee
2018-06-21 15:05:25 -07:00
msgs [ ] sdk . Msg
2019-12-27 09:57:54 -08:00
err error
2018-03-17 11:54:18 -07:00
} {
2019-12-27 09:57:54 -08:00
{ chainID2 , 0 , 1 , fee , msgs , errUnauth } , // test wrong chain_id
{ chainID , 0 , 2 , fee , msgs , errUnauth } , // test wrong seqs
{ chainID , 1 , 1 , fee , msgs , errUnauth } , // test wrong accnum
{ chainID , 0 , 1 , fee , [ ] sdk . Msg { types . NewTestMsg ( addr2 ) } , errUnauth } , // test wrong msg
{ chainID , 0 , 1 , fee2 , msgs , errUnauth } , // test wrong fee
{ chainID , 0 , 1 , fee3 , msgs , errUnauth } , // test wrong fee
2018-03-17 11:54:18 -07:00
}
2018-11-26 03:29:21 -08:00
privs , seqs = [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 1 }
2018-03-17 11:54:18 -07:00
for _ , cs := range cases {
2019-06-10 02:57:38 -07:00
tx := types . NewTestTxWithSignBytes (
2018-06-21 15:05:25 -07:00
msgs , privs , accnums , seqs , fee ,
2019-07-29 08:27:01 -07:00
types . StdSignBytes ( cs . chainID , cs . accnum , cs . seq , cs . fee , cs . msgs , "" ) ,
2018-06-20 12:27:36 -07:00
"" ,
2018-03-17 11:54:18 -07:00
)
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , cs . err )
2018-03-17 11:54:18 -07:00
}
2018-03-16 17:57:28 -07:00
// test wrong signer if public key exist
2018-11-26 03:29:21 -08:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv2 } , [ ] uint64 { 0 } , [ ] uint64 { 1 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrInvalidPubKey )
2018-03-16 17:57:28 -07:00
// test wrong signer if public doesn't exist
2019-06-10 02:57:38 -07:00
msg = types . NewTestMsg ( addr2 )
2018-06-21 15:05:25 -07:00
msgs = [ ] sdk . Msg { msg }
2018-11-26 03:29:21 -08:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 1 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrInvalidPubKey )
2018-03-12 17:40:04 -07:00
}
func TestAnteHandlerSetPubKey ( t * testing . T ) {
2018-03-16 17:57:28 -07:00
// setup
2019-08-15 08:19:16 -07:00
app , ctx := createTestApp ( true )
ctx = ctx . WithBlockHeight ( 1 )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-03-16 17:57:28 -07:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
_ , _ , addr2 := types . KeyTestPubAddr ( )
2018-03-16 17:57:28 -07:00
// set the accounts
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc1 . SetAccountNumber ( 0 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , types . NewTestCoins ( ) )
2019-08-15 08:19:16 -07:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc2 . SetAccountNumber ( 1 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc2 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr2 , types . NewTestCoins ( ) )
2018-03-16 17:57:28 -07:00
var tx sdk . Tx
// test good tx and set public key
2019-06-10 02:57:38 -07:00
msg := types . NewTestMsg ( addr1 )
2018-06-21 15:05:25 -07:00
msgs := [ ] sdk . Msg { msg }
2018-11-26 03:29:21 -08:00
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
fee := types . NewTestStdFee ( )
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2018-08-25 12:12:14 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
2018-03-16 17:57:28 -07:00
2019-08-15 08:19:16 -07:00
acc1 = app . AccountKeeper . GetAccount ( ctx , addr1 )
2018-03-17 11:54:18 -07:00
require . Equal ( t , acc1 . GetPubKey ( ) , priv1 . PubKey ( ) )
2018-03-16 17:57:28 -07:00
// test public key not found
2019-06-10 02:57:38 -07:00
msg = types . NewTestMsg ( addr2 )
2018-06-21 15:05:25 -07:00
msgs = [ ] sdk . Msg { msg }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , [ ] uint64 { 1 } , seqs , fee )
2019-10-10 05:46:09 -07:00
sigs := tx . ( types . StdTx ) . Signatures
2019-06-08 03:04:52 -07:00
sigs [ 0 ] . PubKey = nil
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrInvalidPubKey )
2018-03-16 17:57:28 -07:00
2019-08-15 08:19:16 -07:00
acc2 = app . AccountKeeper . GetAccount ( ctx , addr2 )
2018-06-29 18:10:15 -07:00
require . Nil ( t , acc2 . GetPubKey ( ) )
2018-03-16 17:57:28 -07:00
// test invalid signature and public key
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , [ ] uint64 { 1 } , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrInvalidPubKey )
2018-03-16 17:57:28 -07:00
2019-08-15 08:19:16 -07:00
acc2 = app . AccountKeeper . GetAccount ( ctx , addr2 )
2018-06-29 18:10:15 -07:00
require . Nil ( t , acc2 . GetPubKey ( ) )
2018-03-12 17:40:04 -07:00
}
2018-08-31 10:04:11 -07:00
2019-01-16 09:30:57 -08:00
func generatePubKeysAndSignatures ( n int , msg [ ] byte , keyTypeed25519 bool ) ( pubkeys [ ] crypto . PubKey , signatures [ ] [ ] byte ) {
pubkeys = make ( [ ] crypto . PubKey , n )
signatures = make ( [ ] [ ] byte , n )
for i := 0 ; i < n ; i ++ {
var privkey crypto . PrivKey
if rand . Int63 ( ) % 2 == 0 {
privkey = ed25519 . GenPrivKey ( )
} else {
privkey = secp256k1 . GenPrivKey ( )
}
pubkeys [ i ] = privkey . PubKey ( )
signatures [ i ] , _ = privkey . Sign ( msg )
}
return
}
func expectedGasCostByKeys ( pubkeys [ ] crypto . PubKey ) uint64 {
cost := uint64 ( 0 )
for _ , pubkey := range pubkeys {
pubkeyType := strings . ToLower ( fmt . Sprintf ( "%T" , pubkey ) )
switch {
case strings . Contains ( pubkeyType , "ed25519" ) :
2019-07-29 08:27:01 -07:00
cost += types . DefaultParams ( ) . SigVerifyCostED25519
2019-01-16 09:30:57 -08:00
case strings . Contains ( pubkeyType , "secp256k1" ) :
2019-07-29 08:27:01 -07:00
cost += types . DefaultParams ( ) . SigVerifyCostSecp256k1
2019-01-16 09:30:57 -08:00
default :
panic ( "unexpected key type" )
}
}
return cost
}
2018-11-15 06:30:24 -08:00
func TestCountSubkeys ( t * testing . T ) {
genPubKeys := func ( n int ) [ ] crypto . PubKey {
var ret [ ] crypto . PubKey
for i := 0 ; i < n ; i ++ {
ret = append ( ret , secp256k1 . GenPrivKey ( ) . PubKey ( ) )
}
return ret
}
2019-01-11 09:19:01 -08:00
singleKey := secp256k1 . GenPrivKey ( ) . PubKey ( )
singleLevelMultiKey := multisig . NewPubKeyMultisigThreshold ( 4 , genPubKeys ( 5 ) )
multiLevelSubKey1 := multisig . NewPubKeyMultisigThreshold ( 4 , genPubKeys ( 5 ) )
multiLevelSubKey2 := multisig . NewPubKeyMultisigThreshold ( 4 , genPubKeys ( 5 ) )
multiLevelMultiKey := multisig . NewPubKeyMultisigThreshold ( 2 , [ ] crypto . PubKey {
multiLevelSubKey1 , multiLevelSubKey2 , secp256k1 . GenPrivKey ( ) . PubKey ( ) } )
2018-11-15 06:30:24 -08:00
type args struct {
pub crypto . PubKey
}
tests := [ ] struct {
name string
args args
want int
} {
2019-01-11 09:19:01 -08:00
{ "single key" , args { singleKey } , 1 } ,
{ "single level multikey" , args { singleLevelMultiKey } , 5 } ,
{ "multi level multikey" , args { multiLevelMultiKey } , 11 } ,
2018-11-15 06:30:24 -08:00
}
for _ , tt := range tests {
2019-10-17 06:47:35 -07:00
tt := tt
2018-11-15 06:30:24 -08:00
t . Run ( tt . name , func ( T * testing . T ) {
2019-07-29 08:27:01 -07:00
require . Equal ( t , tt . want , types . CountSubKeys ( tt . args . pub ) )
2018-11-15 06:30:24 -08:00
} )
}
}
func TestAnteHandlerSigLimitExceeded ( t * testing . T ) {
// setup
2019-08-15 08:19:16 -07:00
app , ctx := createTestApp ( true )
ctx = ctx . WithBlockHeight ( 1 )
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
2018-11-15 06:30:24 -08:00
// keys and addresses
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
priv2 , _ , addr2 := types . KeyTestPubAddr ( )
priv3 , _ , addr3 := types . KeyTestPubAddr ( )
priv4 , _ , addr4 := types . KeyTestPubAddr ( )
priv5 , _ , addr5 := types . KeyTestPubAddr ( )
priv6 , _ , addr6 := types . KeyTestPubAddr ( )
priv7 , _ , addr7 := types . KeyTestPubAddr ( )
priv8 , _ , addr8 := types . KeyTestPubAddr ( )
2018-11-15 06:30:24 -08:00
2019-10-10 05:46:09 -07:00
addrs := [ ] sdk . AccAddress { addr1 , addr2 , addr3 , addr4 , addr5 , addr6 , addr7 , addr8 }
2018-11-15 06:30:24 -08:00
// set the accounts
2019-10-10 05:46:09 -07:00
for i , addr := range addrs {
acc := app . AccountKeeper . NewAccountWithAddress ( ctx , addr )
acc . SetAccountNumber ( uint64 ( i ) )
app . AccountKeeper . SetAccount ( ctx , acc )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr , types . NewTestCoins ( ) )
2019-10-10 05:46:09 -07:00
}
2018-11-15 06:30:24 -08:00
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg := types . NewTestMsg ( addr1 , addr2 , addr3 , addr4 , addr5 , addr6 , addr7 , addr8 )
2018-11-15 06:30:24 -08:00
msgs := [ ] sdk . Msg { msg }
2019-06-10 02:57:38 -07:00
fee := types . NewTestStdFee ( )
2018-11-15 06:30:24 -08:00
// test rejection logic
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 , priv2 , priv3 , priv4 , priv5 , priv6 , priv7 , priv8 } ,
2019-10-10 05:46:09 -07:00
[ ] uint64 { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 } , [ ] uint64 { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrTooManySignatures )
2018-11-15 06:30:24 -08:00
}
2019-01-18 08:45:20 -08:00
2019-05-02 12:36:42 -07:00
// Test custom SignatureVerificationGasConsumer
func TestCustomSignatureVerificationGasConsumer ( t * testing . T ) {
// setup
2019-08-15 08:19:16 -07:00
app , ctx := createTestApp ( true )
ctx = ctx . WithBlockHeight ( 1 )
2019-05-02 12:36:42 -07:00
// setup an ante handler that only accepts PubKeyEd25519
2019-10-10 05:46:09 -07:00
anteHandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , func ( meter sdk . GasMeter , sig [ ] byte , pubkey crypto . PubKey , params types . Params ) error {
2019-05-02 12:36:42 -07:00
switch pubkey := pubkey . ( type ) {
case ed25519 . PubKeyEd25519 :
meter . ConsumeGas ( params . SigVerifyCostED25519 , "ante verify: ed25519" )
2019-10-10 05:46:09 -07:00
return nil
2019-05-02 12:36:42 -07:00
default :
2019-10-10 05:46:09 -07:00
return sdkerrors . Wrapf ( sdkerrors . ErrInvalidPubKey , "unrecognized public key type: %T" , pubkey )
2019-05-02 12:36:42 -07:00
}
} )
// verify that an secp256k1 account gets rejected
2019-06-10 02:57:38 -07:00
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
2019-08-15 08:19:16 -07:00
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , sdk . NewCoins ( sdk . NewInt64Coin ( "atom" , 150 ) ) )
2019-06-28 13:11:27 -07:00
2019-05-02 12:36:42 -07:00
var tx sdk . Tx
2019-06-10 02:57:38 -07:00
msg := types . NewTestMsg ( addr1 )
2019-05-02 12:36:42 -07:00
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
fee := types . NewTestStdFee ( )
2019-05-02 12:36:42 -07:00
msgs := [ ] sdk . Msg { msg }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2019-12-27 09:57:54 -08:00
checkInvalidTx ( t , anteHandler , ctx , tx , false , sdkerrors . ErrInvalidPubKey )
2019-05-02 12:36:42 -07:00
// verify that an ed25519 account gets accepted
priv2 := ed25519 . GenPrivKey ( )
pub2 := priv2 . PubKey ( )
addr2 := sdk . AccAddress ( pub2 . Address ( ) )
2019-08-15 08:19:16 -07:00
acc2 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr2 )
2020-01-30 13:31:16 -08:00
require . NoError ( t , app . BankKeeper . SetBalances ( ctx , addr2 , sdk . NewCoins ( sdk . NewInt64Coin ( "atom" , 150 ) ) ) )
2019-06-28 13:11:27 -07:00
require . NoError ( t , acc2 . SetAccountNumber ( 1 ) )
2019-08-15 08:19:16 -07:00
app . AccountKeeper . SetAccount ( ctx , acc2 )
2019-06-10 02:57:38 -07:00
msg = types . NewTestMsg ( addr2 )
2019-05-02 12:36:42 -07:00
privs , accnums , seqs = [ ] crypto . PrivKey { priv2 } , [ ] uint64 { 1 } , [ ] uint64 { 0 }
2019-06-10 02:57:38 -07:00
fee = types . NewTestStdFee ( )
2019-05-02 12:36:42 -07:00
msgs = [ ] sdk . Msg { msg }
2019-06-10 02:57:38 -07:00
tx = types . NewTestTx ( ctx , msgs , privs , accnums , seqs , fee )
2019-05-02 12:36:42 -07:00
checkValidTx ( t , anteHandler , ctx , tx , false )
}
2019-10-23 07:43:58 -07:00
func TestAnteHandlerReCheck ( t * testing . T ) {
// setup
app , ctx := createTestApp ( true )
// set blockheight and recheck=true
ctx = ctx . WithBlockHeight ( 1 )
ctx = ctx . WithIsReCheckTx ( true )
// keys and addresses
priv1 , _ , addr1 := types . KeyTestPubAddr ( )
// priv2, _, addr2 := types.KeyTestPubAddr()
// set the accounts
acc1 := app . AccountKeeper . NewAccountWithAddress ( ctx , addr1 )
require . NoError ( t , acc1 . SetAccountNumber ( 0 ) )
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , types . NewTestCoins ( ) )
2019-10-23 07:43:58 -07:00
antehandler := ante . NewAnteHandler ( app . AccountKeeper , app . SupplyKeeper , ante . DefaultSigVerificationGasConsumer )
// test that operations skipped on recheck do not run
msg := types . NewTestMsg ( addr1 )
msgs := [ ] sdk . Msg { msg }
fee := types . NewTestStdFee ( )
privs , accnums , seqs := [ ] crypto . PrivKey { priv1 } , [ ] uint64 { 0 } , [ ] uint64 { 0 }
tx := types . NewTestTxWithMemo ( ctx , msgs , privs , accnums , seqs , fee , "thisisatestmemo" )
// make signature array empty which would normally cause ValidateBasicDecorator and SigVerificationDecorator fail
// since these decorators don't run on recheck, the tx should pass the antehandler
stdTx := tx . ( types . StdTx )
stdTx . Signatures = [ ] types . StdSignature { }
_ , err := antehandler ( ctx , stdTx , false )
require . Nil ( t , err , "AnteHandler errored on recheck unexpectedly: %v" , err )
tx = types . NewTestTxWithMemo ( ctx , msgs , privs , accnums , seqs , fee , "thisisatestmemo" )
txBytes , err := json . Marshal ( tx )
require . Nil ( t , err , "Error marshalling tx: %v" , err )
ctx = ctx . WithTxBytes ( txBytes )
// require that state machine param-dependent checking is still run on recheck since parameters can change between check and recheck
testCases := [ ] struct {
name string
params types . Params
} {
2019-12-10 08:48:57 -08:00
{ "memo size check" , types . NewParams ( 1 , types . DefaultTxSigLimit , types . DefaultTxSizeCostPerByte , types . DefaultSigVerifyCostED25519 , types . DefaultSigVerifyCostSecp256k1 ) } ,
2019-10-23 07:43:58 -07:00
{ "txsize check" , types . NewParams ( types . DefaultMaxMemoCharacters , types . DefaultTxSigLimit , 10000000 , types . DefaultSigVerifyCostED25519 , types . DefaultSigVerifyCostSecp256k1 ) } ,
{ "sig verify cost check" , types . NewParams ( types . DefaultMaxMemoCharacters , types . DefaultTxSigLimit , types . DefaultTxSizeCostPerByte , types . DefaultSigVerifyCostED25519 , 100000000 ) } ,
}
for _ , tc := range testCases {
// set testcase parameters
app . AccountKeeper . SetParams ( ctx , tc . params )
_ , err := antehandler ( ctx , tx , false )
require . NotNil ( t , err , "tx does not fail on recheck with updated params in test case: %s" , tc . name )
// reset parameters to default values
app . AccountKeeper . SetParams ( ctx , types . DefaultParams ( ) )
}
// require that local mempool fee check is still run on recheck since validator may change minFee between check and recheck
// create new minimum gas price so antehandler fails on recheck
ctx = ctx . WithMinGasPrices ( [ ] sdk . DecCoin { {
Denom : "dnecoin" , // fee does not have this denom
Amount : sdk . NewDec ( 5 ) ,
} } )
_ , err = antehandler ( ctx , tx , false )
require . NotNil ( t , err , "antehandler on recheck did not fail when mingasPrice was changed" )
// reset min gasprice
ctx = ctx . WithMinGasPrices ( sdk . DecCoins { } )
// remove funds for account so antehandler fails on recheck
app . AccountKeeper . SetAccount ( ctx , acc1 )
2020-01-30 13:31:16 -08:00
app . BankKeeper . SetBalances ( ctx , addr1 , sdk . NewCoins ( ) )
2019-10-23 07:43:58 -07:00
_ , err = antehandler ( ctx , tx , false )
require . NotNil ( t , err , "antehandler on recheck did not fail once feePayer no longer has sufficient funds" )
}