* Add TxWrapper, encoder, decoder and DirectModeHandler * fix pkg name * Update API and leave test TODO's * Update TxWrapper API * tests for tx wrapper (#6410) * WIP: added test for direct mode handler * updated code * Add msg * Update TxWrapper API * Fix pubkey declaration * Add pubkey for tests * Fix SetFee * Remove logs * Avoid global var declaration for tests * Add test for GetPubKeys * Fix direct signing tests * Add more test cases for GetSignBytes * Revert SetFee API * Remove logs * Refactor tests Co-authored-by: anilCSE <anil@vitwit.com> Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> * Refactoring * Refactoring * Integrate SignatureV2 API * Fix wrapper tests * Fix tests * Linting and API tweaks * Update API * WIP on updating API * Fix tests * Update to new SigVerifiableTx * Rename * Update docs to reflect ADR 020 * proto-gen * proto docs * cleanup * cleanup * cleanup * cleanup * cleanup * cleanup * cleanup * Add tests * Refactor and improving test coverage * WIP on test coverage * WIP on test coverage * proto-gen * Fix CompactBitArray.Size() bug * Rename * Remove Builder interface * Address review comments * Update x/auth/tx/sigs.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update x/auth/tx/encoder.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update x/auth/tx/encoder.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Address review feedback * Fix build issues * Resolve conflicts * Fix ValidateBasic test coverage * Add test for malicious multisig Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com> Co-authored-by: anilCSE <anil@vitwit.com> Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
100 lines
2.9 KiB
Go
100 lines
2.9 KiB
Go
package ante_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec/testdata"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/ante"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
)
|
|
|
|
func TestSetup(t *testing.T) {
|
|
// setup
|
|
_, ctx := createTestApp(true)
|
|
|
|
// keys and addresses
|
|
priv1, _, addr1 := types.KeyTestPubAddr()
|
|
|
|
// msg and signatures
|
|
msg1 := testdata.NewTestMsg(addr1)
|
|
fee := types.NewTestStdFee()
|
|
|
|
msgs := []sdk.Msg{msg1}
|
|
|
|
privs, accNums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0}
|
|
tx := types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee)
|
|
|
|
sud := ante.NewSetUpContextDecorator()
|
|
antehandler := sdk.ChainAnteDecorators(sud)
|
|
|
|
// Set height to non-zero value for GasMeter to be set
|
|
ctx = ctx.WithBlockHeight(1)
|
|
|
|
// Context GasMeter Limit not set
|
|
require.Equal(t, uint64(0), ctx.GasMeter().Limit(), "GasMeter set with limit before setup")
|
|
|
|
newCtx, err := antehandler(ctx, tx, false)
|
|
require.Nil(t, err, "SetUpContextDecorator returned error")
|
|
|
|
// Context GasMeter Limit should be set after SetUpContextDecorator runs
|
|
require.Equal(t, fee.Gas, newCtx.GasMeter().Limit(), "GasMeter not set correctly")
|
|
}
|
|
|
|
func TestRecoverPanic(t *testing.T) {
|
|
// setup
|
|
_, ctx := createTestApp(true)
|
|
|
|
// keys and addresses
|
|
priv1, _, addr1 := types.KeyTestPubAddr()
|
|
|
|
// msg and signatures
|
|
msg1 := testdata.NewTestMsg(addr1)
|
|
fee := types.NewTestStdFee()
|
|
|
|
msgs := []sdk.Msg{msg1}
|
|
|
|
privs, accNums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0}
|
|
tx := types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee)
|
|
|
|
sud := ante.NewSetUpContextDecorator()
|
|
antehandler := sdk.ChainAnteDecorators(sud, OutOfGasDecorator{})
|
|
|
|
// Set height to non-zero value for GasMeter to be set
|
|
ctx = ctx.WithBlockHeight(1)
|
|
|
|
newCtx, err := antehandler(ctx, tx, false)
|
|
|
|
require.NotNil(t, err, "Did not return error on OutOfGas panic")
|
|
|
|
require.True(t, sdkerrors.ErrOutOfGas.Is(err), "Returned error is not an out of gas error")
|
|
require.Equal(t, fee.Gas, newCtx.GasMeter().Limit())
|
|
|
|
antehandler = sdk.ChainAnteDecorators(sud, PanicDecorator{})
|
|
require.Panics(t, func() { antehandler(ctx, tx, false) }, "Recovered from non-Out-of-Gas panic") // nolint:errcheck
|
|
}
|
|
|
|
type OutOfGasDecorator struct{}
|
|
|
|
// AnteDecorator that will throw OutOfGas panic
|
|
func (ogd OutOfGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
|
overLimit := ctx.GasMeter().Limit() + 1
|
|
|
|
// Should panic with outofgas error
|
|
ctx.GasMeter().ConsumeGas(overLimit, "test panic")
|
|
|
|
// not reached
|
|
return next(ctx, tx, simulate)
|
|
}
|
|
|
|
type PanicDecorator struct{}
|
|
|
|
func (pd PanicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
|
panic("random error")
|
|
}
|