Skip to content

Commit

Permalink
implement test for vesting ante handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Aug 9, 2023
1 parent 578ff9f commit 468ee98
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/ante/ante_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ante_test

import sdk "github.com/cosmos/cosmos-sdk/types"

var _ sdk.AnteHandler = (&MockAnteHandler{}).AnteHandle

// MockAnteHandler mocks an AnteHandler
type MockAnteHandler struct {
WasCalled bool
CalledCtx sdk.Context
}

// AnteHandle implements AnteHandler
func (mah *MockAnteHandler) AnteHandle(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) {
mah.WasCalled = true
mah.CalledCtx = ctx
return ctx, nil
}
104 changes: 104 additions & 0 deletions app/ante/vesting_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package ante_test

import (
"math/rand"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/app"
"github.com/zeta-chain/zetacore/app/ante"
"github.com/zeta-chain/zetacore/testutil/sample"
)

func TestVesting_AnteHandle(t *testing.T) {
txConfig := app.MakeEncodingConfig().TxConfig

testPrivKey, testAddress := sample.PrivKeyAddressPair()
_, testAddress2 := sample.PrivKeyAddressPair()

decorator := ante.NewVestingAccountDecorator()

tests := []struct {
name string
msg sdk.Msg
wantHasErr bool
wantErr string
}{
{
"MsgCreateVestingAccount",
vesting.NewMsgCreateVestingAccount(
testAddress, testAddress2,
sdk.NewCoins(sdk.NewInt64Coin("azeta", 100_000_000)),
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC).Unix(),
false,
),
true,
"MsgTypeURL /cosmos.vesting.v1beta1.MsgCreateVestingAccount not supported",
},
{
"MsgCreatePermanentLockedAccount",
vesting.NewMsgCreatePermanentLockedAccount(
testAddress, testAddress2,
sdk.NewCoins(sdk.NewInt64Coin("azeta", 100_000_000)),
),
true,
"MsgTypeURL /cosmos.vesting.v1beta1.MsgCreatePermanentLockedAccount not supported",
},
{
"MsgCreatePeriodicVestingAccount",
vesting.NewMsgCreatePeriodicVestingAccount(
testAddress, testAddress2,
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC).Unix(),
nil,
),
true,
"MsgTypeURL /cosmos.vesting.v1beta1.MsgCreatePeriodicVestingAccount not supported",
},
{
"Non blocked message",
banktypes.NewMsgSend(
testAddress, testAddress2,
sdk.NewCoins(sdk.NewInt64Coin("azeta", 100_000_000)),
),
false,
"",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tx, err := helpers.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
txConfig,
[]sdk.Msg{
tt.msg,
},
sdk.NewCoins(),
helpers.DefaultGenTxGas,
"testing-chain-id",
[]uint64{0},
[]uint64{0},
testPrivKey,
)
require.NoError(t, err)

mmd := MockAnteHandler{}
ctx := sdk.Context{}.WithIsCheckTx(true)

_, err = decorator.AnteHandle(ctx, tx, false, mmd.AnteHandle)

if tt.wantHasErr {
require.Error(t, err)
require.Contains(t, err.Error(), tt.wantErr)
} else {
require.NoError(t, err)
}
})
}
}
8 changes: 8 additions & 0 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ func AccAddress() string {
addr := pk.Address()
return sdk.AccAddress(addr).String()
}

// PrivKeyAddressPair returns a private key, address pair
func PrivKeyAddressPair() (*ed25519.PrivKey, sdk.AccAddress) {
privKey := ed25519.GenPrivKey()
addr := privKey.PubKey().Address()

return privKey, sdk.AccAddress(addr)
}

0 comments on commit 468ee98

Please sign in to comment.