2022-01-08 00:39:27 +00:00
|
|
|
package ante_test
|
2020-12-02 17:37:11 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-04 00:08:45 +00:00
|
|
|
"math/rand"
|
2020-12-02 17:37:11 +00:00
|
|
|
"testing"
|
2023-04-04 00:08:45 +00:00
|
|
|
"time"
|
2020-12-02 17:37:11 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2020-12-02 17:37:11 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/app/ante"
|
2020-12-02 17:37:11 +00:00
|
|
|
)
|
|
|
|
|
2022-05-09 18:37:36 +00:00
|
|
|
var _ sdk.AnteHandler = (&MockAnteHandler{}).AnteHandle
|
2020-12-02 17:37:11 +00:00
|
|
|
|
|
|
|
type MockAnteHandler struct {
|
|
|
|
WasCalled bool
|
2022-05-21 00:12:56 +00:00
|
|
|
CalledCtx sdk.Context
|
2020-12-02 17:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mah *MockAnteHandler) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) {
|
|
|
|
mah.WasCalled = true
|
2022-05-21 00:12:56 +00:00
|
|
|
mah.CalledCtx = ctx
|
2020-12-02 17:37:11 +00:00
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func mockAddressFetcher(addresses ...sdk.AccAddress) ante.AddressFetcher {
|
2020-12-02 17:37:11 +00:00
|
|
|
return func(sdk.Context) []sdk.AccAddress { return addresses }
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthenticatedMempoolDecorator_AnteHandle_NotCheckTx(t *testing.T) {
|
2022-01-08 00:39:27 +00:00
|
|
|
txConfig := app.MakeEncodingConfig().TxConfig
|
|
|
|
|
|
|
|
testPrivKeys, testAddresses := app.GeneratePrivKeyAddressPairs(5)
|
2020-12-02 17:37:11 +00:00
|
|
|
fetcher := mockAddressFetcher(testAddresses[1:]...)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
decorator := ante.NewAuthenticatedMempoolDecorator(fetcher)
|
2023-04-04 00:08:45 +00:00
|
|
|
tx, err := helpers.GenSignedMockTx(
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano())),
|
2022-01-08 00:39:27 +00:00
|
|
|
txConfig,
|
2020-12-02 17:37:11 +00:00
|
|
|
[]sdk.Msg{
|
2022-01-08 00:39:27 +00:00
|
|
|
banktypes.NewMsgSend(
|
2020-12-02 17:37:11 +00:00
|
|
|
testAddresses[0],
|
2022-01-08 00:39:27 +00:00
|
|
|
testAddresses[1],
|
|
|
|
sdk.NewCoins(sdk.NewInt64Coin("ukava", 100_000_000)),
|
2020-12-02 17:37:11 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
sdk.NewCoins(), // no fee
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
"testing-chain-id",
|
|
|
|
[]uint64{0},
|
|
|
|
[]uint64{0},
|
|
|
|
testPrivKeys[0], // address is not authorized
|
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-02 17:37:11 +00:00
|
|
|
mmd := MockAnteHandler{}
|
|
|
|
ctx := sdk.Context{}.WithIsCheckTx(false) // run as it would be during block update ('DeliverTx'), not just checking entry to mempool
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
_, err = decorator.AnteHandle(ctx, tx, false, mmd.AnteHandle)
|
2020-12-02 17:37:11 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, mmd.WasCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthenticatedMempoolDecorator_AnteHandle_Pass(t *testing.T) {
|
2022-01-08 00:39:27 +00:00
|
|
|
txConfig := app.MakeEncodingConfig().TxConfig
|
|
|
|
|
|
|
|
testPrivKeys, testAddresses := app.GeneratePrivKeyAddressPairs(5)
|
2020-12-02 17:37:11 +00:00
|
|
|
fetcher := mockAddressFetcher(testAddresses[1:]...)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
decorator := ante.NewAuthenticatedMempoolDecorator(fetcher)
|
2020-12-02 17:37:11 +00:00
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
tx, err := helpers.GenSignedMockTx(
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano())),
|
2022-01-08 00:39:27 +00:00
|
|
|
txConfig,
|
2020-12-02 17:37:11 +00:00
|
|
|
[]sdk.Msg{
|
2022-01-08 00:39:27 +00:00
|
|
|
banktypes.NewMsgSend(
|
2020-12-02 17:37:11 +00:00
|
|
|
testAddresses[0],
|
|
|
|
testAddresses[1],
|
|
|
|
sdk.NewCoins(sdk.NewInt64Coin("ukava", 100_000_000)),
|
|
|
|
),
|
2022-01-08 00:39:27 +00:00
|
|
|
banktypes.NewMsgSend(
|
2020-12-02 17:37:11 +00:00
|
|
|
testAddresses[2],
|
2022-01-08 00:39:27 +00:00
|
|
|
testAddresses[1],
|
|
|
|
sdk.NewCoins(sdk.NewInt64Coin("ukava", 100_000_000)),
|
2020-12-02 17:37:11 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
sdk.NewCoins(), // no fee
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
"testing-chain-id",
|
|
|
|
[]uint64{0, 123},
|
|
|
|
[]uint64{0, 123},
|
|
|
|
testPrivKeys[0], // not in list of authorized addresses
|
2022-01-08 00:39:27 +00:00
|
|
|
testPrivKeys[2], // in list of authorized addresses
|
2020-12-02 17:37:11 +00:00
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-02 17:37:11 +00:00
|
|
|
mmd := MockAnteHandler{}
|
|
|
|
ctx := sdk.Context{}.WithIsCheckTx(true)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
_, err = decorator.AnteHandle(ctx, tx, false, mmd.AnteHandle)
|
2020-12-02 17:37:11 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, mmd.WasCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthenticatedMempoolDecorator_AnteHandle_Reject(t *testing.T) {
|
2022-01-08 00:39:27 +00:00
|
|
|
txConfig := app.MakeEncodingConfig().TxConfig
|
|
|
|
|
|
|
|
testPrivKeys, testAddresses := app.GeneratePrivKeyAddressPairs(5)
|
2020-12-02 17:37:11 +00:00
|
|
|
fetcher := mockAddressFetcher(testAddresses[1:]...)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
decorator := ante.NewAuthenticatedMempoolDecorator(fetcher)
|
2020-12-02 17:37:11 +00:00
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
tx, err := helpers.GenSignedMockTx(
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano())),
|
2022-01-08 00:39:27 +00:00
|
|
|
txConfig,
|
2020-12-02 17:37:11 +00:00
|
|
|
[]sdk.Msg{
|
2022-01-08 00:39:27 +00:00
|
|
|
banktypes.NewMsgSend(
|
2020-12-02 17:37:11 +00:00
|
|
|
testAddresses[0],
|
|
|
|
testAddresses[1],
|
|
|
|
sdk.NewCoins(sdk.NewInt64Coin("ukava", 100_000_000)),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
sdk.NewCoins(), // no fee
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
"testing-chain-id",
|
|
|
|
[]uint64{0},
|
|
|
|
[]uint64{0},
|
|
|
|
testPrivKeys[0], // not in list of authorized addresses
|
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-02 17:37:11 +00:00
|
|
|
mmd := MockAnteHandler{}
|
|
|
|
ctx := sdk.Context{}.WithIsCheckTx(true)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
_, err = decorator.AnteHandle(ctx, tx, false, mmd.AnteHandle)
|
2020-12-02 17:37:11 +00:00
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
require.False(t, mmd.WasCalled)
|
|
|
|
}
|