Skip to content

Commit

Permalink
clean up tests (#17)
Browse files Browse the repository at this point in the history
* add test cases for simulation mode

* improve a comment

* refactor test helper functions

* reorganize content in test files
  • Loading branch information
larry0x authored Jul 8, 2023
1 parent 3019726 commit 2ea462d
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 113 deletions.
142 changes: 107 additions & 35 deletions x/abstractaccount/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

simapptesting "github.com/larry0x/abstract-account/simapp/testing"
Expand All @@ -18,13 +17,6 @@ import (
"github.com/larry0x/abstract-account/x/abstractaccount/types"
)

const (
mockChainID = "dev-1"
mockAccNum = uint64(12345)
mockSeq = uint64(88888)
signMode = signing.SignMode_SIGN_MODE_DIRECT
)

func TestIsAbstractAccountTx(t *testing.T) {
var (
app = simapptesting.MakeSimpleMockApp()
Expand All @@ -37,40 +29,58 @@ func TestIsAbstractAccountTx(t *testing.T) {
require.NoError(t, err)

acc2, err := makeMockAccount(keybase, "test2")
acc2 = types.NewAbstractAccountFromAccount(acc2)
require.NoError(t, err)

app.AccountKeeper.SetAccount(ctx, acc1)
app.AccountKeeper.SetAccount(ctx, types.NewAbstractAccountFromAccount(acc2))
app.AccountKeeper.SetAccount(ctx, acc2)

signer1 := Signer{
keyName: "test1",
acc: acc1,
overrideAccNum: nil,
overrideSeq: nil,
}
signer2 := Signer{
keyName: "test2",
acc: acc2,
overrideAccNum: nil,
overrideSeq: nil,
}

for _, tc := range []struct {
desc string
msgs []sdk.Msg
expIs bool
desc string
msgs []sdk.Msg
signers []Signer
expIs bool
}{
{
desc: "tx has one signer and it is an AbstractAccount",
msgs: []sdk.Msg{
banktypes.NewMsgSend(acc2.GetAddress(), acc1.GetAddress(), sdk.NewCoins()),
},
expIs: true,
signers: []Signer{signer2},
expIs: true,
},
{
desc: "tx has one signer but it's not an AbstractAccount",
msgs: []sdk.Msg{
banktypes.NewMsgSend(acc1.GetAddress(), acc2.GetAddress(), sdk.NewCoins()),
},
expIs: false,
signers: []Signer{signer1},
expIs: false,
},
{
desc: "tx has more than one signers",
msgs: []sdk.Msg{
banktypes.NewMsgSend(acc1.GetAddress(), acc2.GetAddress(), sdk.NewCoins()),
banktypes.NewMsgSend(acc2.GetAddress(), acc1.GetAddress(), sdk.NewCoins()),
},
expIs: false,
signers: []Signer{signer1, signer2},
expIs: false,
},
} {
sigTx, err := prepareTx(ctx, app, tc.msgs)
sigTx, err := prepareTx(ctx, app, keybase, tc.msgs, tc.signers, mockChainID, true)
require.NoError(t, err)

is, _, _, err := abstractaccount.IsAbstractAccountTx(ctx, sigTx, app.AccountKeeper)
Expand All @@ -79,10 +89,16 @@ func TestIsAbstractAccountTx(t *testing.T) {
}
}

type BaseInstantiateMsg struct {
PubKey []byte `json:"pubkey"`
}

func TestBeforeTx(t *testing.T) {
var (
app = simapptesting.MakeSimpleMockApp()
keybase = keyring.NewInMemory(app.Codec())
app = simapptesting.MakeSimpleMockApp()
keybase = keyring.NewInMemory(app.Codec())
mockAccNum = uint64(12345)
mockSeq = uint64(88888)
)

ctx := app.NewContext(false, tmproto.Header{
Expand Down Expand Up @@ -113,7 +129,7 @@ func TestBeforeTx(t *testing.T) {
// use the pubkey of acc1 as the AbstractAccount's pubkey
acc1.GetAddress(),
testdata.AccountWasm,
&AccountInitMsg{PubKey: acc1.GetPubKey().Bytes()},
&BaseInstantiateMsg{PubKey: acc1.GetPubKey().Bytes()},
sdk.NewCoins(),
)
require.NoError(t, err)
Expand All @@ -127,7 +143,9 @@ func TestBeforeTx(t *testing.T) {

for _, tc := range []struct {
desc string
signWith string
simulate bool // whether to run the AnteHandler in simulation mode
sign bool // whether a signature is to be included with this tx
signWith string // if a sig is to be included, which key to use to sign it
chainID string
accNum uint64
seq uint64
Expand All @@ -137,6 +155,8 @@ func TestBeforeTx(t *testing.T) {
}{
{
desc: "tx signed with the correct key",
simulate: false,
sign: true,
signWith: "test1",
chainID: mockChainID,
accNum: mockAccNum,
Expand All @@ -147,6 +167,8 @@ func TestBeforeTx(t *testing.T) {
},
{
desc: "tx signed with an incorrect key",
simulate: false,
sign: true,
signWith: "test2",
chainID: mockChainID,
accNum: mockAccNum,
Expand All @@ -157,6 +179,8 @@ func TestBeforeTx(t *testing.T) {
},
{
desc: "tx signed with an incorrect chain id",
simulate: false,
sign: true,
signWith: "test1",
chainID: "wrong-chain-id",
accNum: mockAccNum,
Expand All @@ -167,6 +191,8 @@ func TestBeforeTx(t *testing.T) {
},
{
desc: "tx signed with an incorrect account number",
simulate: false,
sign: true,
signWith: "test1",
chainID: mockChainID,
accNum: 4524455,
Expand All @@ -177,6 +203,8 @@ func TestBeforeTx(t *testing.T) {
},
{
desc: "tx signed with an incorrect sequence",
simulate: false,
sign: true,
signWith: "test1",
chainID: mockChainID,
accNum: mockAccNum,
Expand All @@ -187,6 +215,8 @@ func TestBeforeTx(t *testing.T) {
},
{
desc: "contract call exceeds gas limit",
simulate: false,
sign: true,
signWith: "test1",
chainID: mockChainID,
accNum: mockAccNum,
Expand All @@ -195,6 +225,42 @@ func TestBeforeTx(t *testing.T) {
expOk: false,
expPanic: true, // attempting to consume above the gas limit results in panicking
},
{
desc: "not in simulation mode, but tx isn't signed",
simulate: false,
sign: false,
signWith: "",
chainID: mockChainID,
accNum: mockAccNum,
seq: mockSeq,
maxGas: types.DefaultMaxGas,
expOk: false,
expPanic: false,
},
{
desc: "in simulation, tx is signed",
simulate: true,
sign: true,
signWith: "test1",
chainID: mockChainID,
accNum: mockAccNum,
seq: mockSeq,
maxGas: types.DefaultMaxGas,
expOk: true, // we accept it
expPanic: false,
},
{
desc: "in simulation, tx is not signed",
simulate: true,
sign: false,
signWith: "test1",
chainID: mockChainID,
accNum: mockAccNum,
seq: mockSeq,
maxGas: types.DefaultMaxGas,
expOk: true, // in simulation mode, for this particular account type, the credential can be omitted
expPanic: false,
},
} {
// set max gas
app.AbstractAccountKeeper.SetParams(ctx, &types.Params{
Expand All @@ -204,30 +270,35 @@ func TestBeforeTx(t *testing.T) {

msg := banktypes.NewMsgSend(absAcc.GetAddress(), acc2.GetAddress(), sdk.NewCoins())

tx, err := prepareTx2(
signer := Signer{
keyName: tc.signWith,
acc: absAcc,
overrideAccNum: &tc.accNum,
overrideSeq: &tc.seq,
}

tx, err := prepareTx(
ctx,
app,
[]sdk.Msg{msg},
keybase,
tc.signWith,
absAcc,
[]sdk.Msg{msg},
[]Signer{signer},
tc.chainID,
tc.accNum,
tc.seq,
tc.sign,
)
require.NoError(t, err)

if tc.expPanic {
require.Panics(t, func() {
decorator := makeBeforeTxDecorator(app)
decorator.AnteHandle(ctx, tx, false, anteTerminator)
decorator.AnteHandle(ctx, tx, tc.simulate, anteTerminator)
})

return
}

decorator := makeBeforeTxDecorator(app)
_, err = decorator.AnteHandle(ctx, tx, false, anteTerminator)
_, err = decorator.AnteHandle(ctx, tx, tc.simulate, anteTerminator)

if tc.expOk {
require.NoError(t, err)
Expand Down Expand Up @@ -266,24 +337,25 @@ func TestAfterTx(t *testing.T) {
app,
acc.GetAddress(),
testdata.AccountWasm,
&AccountInitMsg{PubKey: acc.GetPubKey().Bytes()},
&BaseInstantiateMsg{PubKey: acc.GetPubKey().Bytes()},
sdk.NewCoins(),
)
require.NoError(t, err)

// save the signer address to mimic what happens in the BeforeTx hook
app.AbstractAccountKeeper.SetSignerAddress(ctx, absAcc.GetAddress())

tx, err := prepareTx2(
tx, err := prepareTx(
ctx,
app,
[]sdk.Msg{banktypes.NewMsgSend(absAcc.GetAddress(), acc.GetAddress(), sdk.NewCoins())},
keybase,
"test1",
absAcc,
[]sdk.Msg{banktypes.NewMsgSend(absAcc.GetAddress(), acc.GetAddress(), sdk.NewCoins())},
[]Signer{{
keyName: "test1",
acc: absAcc,
}},
mockChainID,
absAcc.GetAccountNumber(),
absAcc.GetSequence(),
true,
)
require.NoError(t, err)

Expand Down
Loading

0 comments on commit 2ea462d

Please sign in to comment.