Skip to content

Commit

Permalink
new: add gas releated testcases for Ante
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkanani committed Apr 14, 2020
1 parent 95118d4 commit 2c05df3
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 4 deletions.
2 changes: 1 addition & 1 deletion auth/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func NewAnteHandler(
signerAddrs := stdTx.GetSigners()

if len(signerAddrs) == 0 {
return newCtx, sdk.ErrUnauthorized("no signer exists").Result(), true
return newCtx, sdk.ErrNoSignatures("no signers").Result(), true
}

signerAccs := make([]authTypes.Account, len(signerAddrs))
Expand Down
224 changes: 221 additions & 3 deletions auth/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
sdkAuth "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto"

"github.com/maticnetwork/heimdall/app"
"github.com/maticnetwork/heimdall/auth"
"github.com/maticnetwork/heimdall/auth/types"
authTypes "github.com/maticnetwork/heimdall/auth/types"
"github.com/maticnetwork/heimdall/helper"
hmTypes "github.com/maticnetwork/heimdall/types"
"github.com/maticnetwork/heimdall/types/simulation"
Expand Down Expand Up @@ -53,6 +55,87 @@ func TestAnteTestSuite(t *testing.T) {
// Tests
//

func (suite *AnteTestSuite) TestAnteValidation() {
t, happ, ctx, anteHandler := suite.T(), suite.app, suite.ctx, suite.anteHandler

// module account validation
// removing module account
acc := happ.SupplyKeeper.GetModuleAccount(ctx, authTypes.FeeCollectorName)
happ.AccountKeeper.RemoveAccount(ctx, acc)
happ.SupplyKeeper.RemoveModuleAddress(authTypes.FeeCollectorName)

// keys and addresses
priv1, _, addr1 := sdkAuth.KeyTestPubAddr()
msg1 := sdkAuth.NewTestMsg(addr1)
tx1 := types.NewTestTx(ctx, msg1, priv1, uint64(0), uint64(0)) // use sdk's auth module for msg

_, result1, _ := checkInvalidTx(t, anteHandler, ctx, tx1, false, sdk.CodeInternal)
require.Contains(t, result1.Log, "fee_collector module account has not been set")
}

func (suite *AnteTestSuite) TestStdTx() {
t, happ, ctx, anteHandler := suite.T(), suite.app, suite.ctx, suite.anteHandler

// keys and addresses
priv1, _, addr1 := sdkAuth.KeyTestPubAddr()

// test stdTx
fee := sdkAuth.NewTestStdFee()
msg := sdkAuth.NewTestMsg(addr1)
msgs := []sdk.Msg{msg}

// test no signatures
privs, accNums, seqs := []crypto.PrivKey{}, []uint64{}, []uint64{}
tx := sdkAuth.NewTestTx(ctx, msgs, privs, accNums, seqs, fee)

// Check no signatures fails
_, result1, _ := checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInternal)
require.Contains(t, result1.Log, "tx must be StdTx")

// test memo
params := happ.AccountKeeper.GetParams(ctx)
params.MaxMemoCharacters = 5 // setting 5 as max length temporary
happ.AccountKeeper.SetParams(ctx, params)

msg2 := sdkAuth.NewTestMsg(addr1)
memo := "more than 5 length memo"
tx2 := types.NewTestTxWithMemo(ctx, msg2, priv1, uint64(0), uint64(0), memo) // use sdk's auth module for msg

checkInvalidTx(t, anteHandler, ctx, tx2, false, sdk.CodeMemoTooLarge)

// test tx fees
params.TxFees = "non integer" // setting non integer
happ.AccountKeeper.SetParams(ctx, params)
tx2 = types.NewTestTx(ctx, msg2, priv1, uint64(0), uint64(0)) // use sdk's auth module for msg

_, result2, _ := checkInvalidTx(t, anteHandler, ctx, tx2, false, sdk.CodeInternal)
require.Contains(t, result2.Log, "Invalid param tx fees")

// gas wanted
}

func (suite *AnteTestSuite) TestSigErrors() {
t, _, ctx, anteHandler := suite.T(), suite.app, suite.ctx, suite.anteHandler

// keys and addresses
priv1, _, addr1 := sdkAuth.KeyTestPubAddr()
priv2, _, _ := sdkAuth.KeyTestPubAddr()

// test no signers
msg1 := sdkAuth.NewTestMsg()
tx1 := types.NewTestTx(ctx, msg1, priv1, uint64(0), uint64(0)) // use sdk's auth module for msg

// Check no signatures fails
checkInvalidTx(t, anteHandler, ctx, tx1, false, sdk.CodeNoSignatures)

// unknown address error
msg2 := sdkAuth.NewTestMsg(addr1) // using first address
tx2 := types.NewTestTx(ctx, msg2, priv2, uint64(0), uint64(0))

// Check no signatures fails
checkInvalidTx(t, anteHandler, ctx, tx2, false, sdk.CodeUnknownAddress)
}

func (suite *AnteTestSuite) TestAccountNumbers() {
t, happ, ctx, anteHandler := suite.T(), suite.app, suite.ctx, suite.anteHandler

Expand Down Expand Up @@ -87,21 +170,154 @@ func (suite *AnteTestSuite) TestAccountNumbers() {
checkValidTx(t, anteHandler, ctx, tx, false)
}

// Test logic around account number checking with many signers when BlockHeight is 0.
func (suite *AnteTestSuite) TestAccountNumbersAtBlockHeightZero() {
t, happ, ctx, anteHandler := suite.T(), suite.app, suite.ctx, suite.anteHandler

// keys and addresses
priv1, _, addr1 := sdkAuth.KeyTestPubAddr()
priv2, _, addr2 := sdkAuth.KeyTestPubAddr()

// set the accounts, we don't need the acc numbers as it is in the genesis block
acc1 := happ.AccountKeeper.NewAccountWithAddress(ctx, hmTypes.AccAddressToHeimdallAddress(addr1))
acc1.SetCoins(simulation.RandomFeeCoins())
happ.AccountKeeper.SetAccount(ctx, acc1)
acc2 := happ.AccountKeeper.NewAccountWithAddress(ctx, hmTypes.AccAddressToHeimdallAddress(addr2))
acc2.SetCoins(simulation.RandomFeeCoins())
require.NoError(t, acc2.SetAccountNumber(100))
happ.AccountKeeper.SetAccount(ctx, acc2)

// msg and signatures
var tx sdk.Tx
msg1 := sdkAuth.NewTestMsg(addr1)
msg2 := sdkAuth.NewTestMsg(addr2)

acc1 = happ.AccountKeeper.GetAccount(ctx, hmTypes.AccAddressToHeimdallAddress(addr1))
acc2 = happ.AccountKeeper.GetAccount(ctx, hmTypes.AccAddressToHeimdallAddress(addr2))
// accNumber1 := acc1.GetAccountNumber()
// accNumber2 := acc2.GetAccountNumber()

// test good tx from one signer
tx = types.NewTestTx(ctx, msg1, priv1, uint64(0), uint64(0))
checkValidTx(t, anteHandler, ctx, tx, false)

// test good tx from one signer
tx = types.NewTestTx(ctx, msg1, priv1, uint64(0), uint64(1))
checkValidTx(t, anteHandler, ctx, tx, false)

// // new tx from wrong account number
tx = types.NewTestTx(ctx, msg2, priv2, uint64(1), uint64(1))
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnauthorized)

// from correct account number but wrong private key
tx = types.NewTestTx(ctx, msg2, priv1, uint64(1), uint64(0)) // with private key 1
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnauthorized)

// from correct account number but wrong private key
tx = types.NewTestTx(ctx, msg2, priv2, uint64(0), uint64(0)) // with private key 2 (account 2)
checkValidTx(t, anteHandler, ctx, tx, false)
}

func (suite *AnteTestSuite) TestSequences() {
t, happ, ctx, anteHandler := suite.T(), suite.app, suite.ctx, suite.anteHandler
ctx = ctx.WithBlockHeight(1)

// keys and addresses
priv1, _, addr1 := sdkAuth.KeyTestPubAddr()
priv2, _, addr2 := sdkAuth.KeyTestPubAddr()

// set the accounts, we don't need the acc numbers as it is in the genesis block
acc1 := happ.AccountKeeper.NewAccountWithAddress(ctx, hmTypes.AccAddressToHeimdallAddress(addr1))
acc1.SetCoins(simulation.RandomFeeCoins())
happ.AccountKeeper.SetAccount(ctx, acc1)
acc2 := happ.AccountKeeper.NewAccountWithAddress(ctx, hmTypes.AccAddressToHeimdallAddress(addr2))
acc2.SetCoins(simulation.RandomFeeCoins())
require.NoError(t, acc2.SetAccountNumber(100))
happ.AccountKeeper.SetAccount(ctx, acc2)

// msg and signatures
var tx sdk.Tx
msg1 := sdkAuth.NewTestMsg(addr1)
msg2 := sdkAuth.NewTestMsg(addr2)

acc1 = happ.AccountKeeper.GetAccount(ctx, hmTypes.AccAddressToHeimdallAddress(addr1))
acc2 = happ.AccountKeeper.GetAccount(ctx, hmTypes.AccAddressToHeimdallAddress(addr2))
accNumber1 := acc1.GetAccountNumber()
accNumber2 := acc2.GetAccountNumber()

// test good tx from one signer
tx = types.NewTestTx(ctx, msg1, priv1, accNumber1, uint64(0))
checkValidTx(t, anteHandler, ctx, tx, false)

// test good tx from one signer
tx = types.NewTestTx(ctx, msg1, priv1, accNumber1, uint64(1))
checkValidTx(t, anteHandler, ctx, tx, false)

// // new tx from wrong account number
tx = types.NewTestTx(ctx, msg2, priv2, accNumber2, uint64(1))
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnauthorized)

// from correct account number but wrong private key
tx = types.NewTestTx(ctx, msg2, priv1, accNumber2, uint64(0)) // with private key 1
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeUnauthorized)

// from correct account number but wrong private key
tx = types.NewTestTx(ctx, msg2, priv2, accNumber2, uint64(0)) // with private key 2 (account 2)
checkValidTx(t, anteHandler, ctx, tx, false)
}

// Test logic around fee deduction.
func (suite *AnteTestSuite) TestFees() {
t, happ, ctx, anteHandler := suite.T(), suite.app, suite.ctx, suite.anteHandler

// keys and addresses
priv1, _, addr1 := sdkAuth.KeyTestPubAddr()

// set the accounts
acc1 := happ.AccountKeeper.NewAccountWithAddress(ctx, hmTypes.AccAddressToHeimdallAddress(addr1))
// acc1.SetCoins(simulation.RandomFeeCoins())
happ.AccountKeeper.SetAccount(ctx, acc1)

// msg and signatures
var tx sdk.Tx
msg1 := sdkAuth.NewTestMsg(addr1)
acc1 = happ.AccountKeeper.GetAccount(ctx, hmTypes.AccAddressToHeimdallAddress(addr1))
tx = types.NewTestTx(ctx, msg1, priv1, uint64(0), uint64(0))
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds)

// set some coins
acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin(authTypes.FeeToken, 149)))
happ.AccountKeeper.SetAccount(ctx, acc1)
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeInsufficientFunds)

require.True(t, happ.SupplyKeeper.GetModuleAccount(ctx, authTypes.FeeCollectorName).GetCoins().Empty())
require.True(sdk.IntEq(t, happ.AccountKeeper.GetAccount(ctx, hmTypes.AccAddressToHeimdallAddress(addr1)).GetCoins().AmountOf(authTypes.FeeToken), sdk.NewInt(149)))

amt, _ := sdk.NewIntFromString(authTypes.DefaultTxFees)
acc1.SetCoins(sdk.NewCoins(sdk.NewCoin(authTypes.FeeToken, amt)))
happ.AccountKeeper.SetAccount(ctx, acc1)
checkValidTx(t, anteHandler, ctx, tx, false)

require.True(sdk.IntEq(t, happ.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().AmountOf(authTypes.FeeToken), amt))
require.True(sdk.IntEq(t, happ.AccountKeeper.GetAccount(ctx, hmTypes.AccAddressToHeimdallAddress(addr1)).GetCoins().AmountOf(authTypes.FeeToken), sdk.NewInt(0)))
}

//
// utils
//

// run the tx through the anteHandler and ensure its valid
func checkValidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, simulate bool) {
_, result, abort := anteHandler(ctx, tx, simulate)
func checkValidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, sdk.Result, bool) {
newCtx, result, abort := anteHandler(ctx, tx, simulate)
require.Equal(t, "", result.Log)
require.False(t, abort)
require.Equal(t, sdk.CodeOK, result.Code)
require.True(t, result.IsOK())
return newCtx, result, abort
}

// 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, simulate bool, code sdk.CodeType) {
func checkInvalidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, simulate bool, code sdk.CodeType) (sdk.Context, sdk.Result, bool) {
newCtx, result, abort := anteHandler(ctx, tx, simulate)
require.True(t, abort)

Expand All @@ -116,4 +332,6 @@ func checkInvalidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context,
// Check that context is set correctly
require.Equal(t, result.GasUsed, newCtx.GasMeter().GasConsumed(), "Context not updated correctly")
}

return newCtx, result, abort
}
10 changes: 10 additions & 0 deletions supply/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func (k Keeper) GetModuleAddress(moduleName string) (addr hmTypes.HeimdallAddres
return
}

// RemoveModuleAddress removes module address
func (k Keeper) RemoveModuleAddress(moduleName string) bool {
_, ok := k.permAddrs[moduleName]
if !ok {
return false
}
delete(k.permAddrs, moduleName)
return true
}

// GetModuleAddressAndPermissions returns an address and permissions based on the module name
func (k Keeper) GetModuleAddressAndPermissions(moduleName string) (addr hmTypes.HeimdallAddress, permissions []string) {
permAddr, ok := k.permAddrs[moduleName]
Expand Down

0 comments on commit 2c05df3

Please sign in to comment.