Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnteDecorator #5006

Merged
merged 42 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1960993
start decorator changes
AdityaSripal Aug 28, 2019
12244c5
start replacing ante logic with decorators
AdityaSripal Aug 30, 2019
5a46ceb
Fix build errors
AdityaSripal Sep 5, 2019
bdcf294
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ad…
AdityaSripal Sep 5, 2019
ac19f18
fix some tests
AdityaSripal Sep 6, 2019
9a0eac7
fix baseapp tests
AdityaSripal Sep 9, 2019
725f5cf
fix auth tests
AdityaSripal Sep 12, 2019
ff5f1f7
start individual decorator tests
AdityaSripal Sep 14, 2019
73e2739
add signature tests
AdityaSripal Sep 17, 2019
77bc7c6
complete sig tests
AdityaSripal Sep 17, 2019
baf5bfb
fix all test errors
AdityaSripal Sep 17, 2019
71e07ce
remove unnecessary &
AdityaSripal Sep 18, 2019
3b3a74f
fix linter errors
AdityaSripal Sep 18, 2019
6303bec
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ad…
AdityaSripal Sep 18, 2019
e52e89d
interface all decorators except sigs
AdityaSripal Sep 18, 2019
ac8209d
check signer lenght in sigverify
AdityaSripal Sep 19, 2019
b93ca36
Apply suggestions from bez code review
AdityaSripal Sep 22, 2019
5825e81
complete bez suggestions
AdityaSripal Sep 29, 2019
441c050
fix merge conflicts
AdityaSripal Sep 29, 2019
2f22251
create sigTx interface
AdityaSripal Oct 1, 2019
975ad0e
linting
AdityaSripal Oct 1, 2019
c5ede73
finish linting except TODO
AdityaSripal Oct 1, 2019
9a5cb61
make auth tx interfaces extend sdk.Tx
AdityaSripal Oct 1, 2019
e7ec478
Merge branch 'master' into aditya/ante-decorator
alexanderbez Oct 2, 2019
634c1aa
test docs, replace FeeCoins with GetFee
AdityaSripal Oct 2, 2019
66144bf
Merge branch 'aditya/ante-decorator' of https://github.com/cosmos/cos…
AdityaSripal Oct 2, 2019
0c39cd9
Apply suggestions from fede code review
AdityaSripal Oct 2, 2019
29e85bf
address tim comments
AdityaSripal Oct 3, 2019
f1964cf
Merge branch 'aditya/ante-decorator' of https://github.com/cosmos/cos…
AdityaSripal Oct 3, 2019
b6d9166
Merge branch 'master' into aditya/ante-decorator
fedekunze Oct 8, 2019
0fd1234
add order comments
AdityaSripal Oct 8, 2019
aad9673
Add Schwarzenegger art
AdityaSripal Oct 8, 2019
5ce99da
add assertions that StdTx implements all necessary decorator interfaces
AdityaSripal Oct 8, 2019
bd79e58
Merge branch 'aditya/ante-decorator' of https://github.com/cosmos/cos…
AdityaSripal Oct 8, 2019
2f54876
documentation and CHANGELOG
AdityaSripal Oct 9, 2019
a4ef3ec
Run goimports
alexanderbez Oct 9, 2019
33cc17a
Update ChainAnteDecorators godoc
alexanderbez Oct 10, 2019
882fac5
Changelog entries cleanup
alexanderbez Oct 10, 2019
163da7f
Changelog entries cleanup
alexanderbez Oct 10, 2019
492cf0d
Merge branch 'master' into aditya/ante-decorator
alexanderbez Oct 10, 2019
1b1a779
Fix formatter
alexanderbez Oct 10, 2019
440a05c
Merge branch 'master' into aditya/ante-decorator
alexanderbez Oct 10, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
start individual decorator tests
  • Loading branch information
AdityaSripal committed Sep 14, 2019
commit ff5f1f797db6d41ba560474d2a53b0c00c29da9f
1 change: 0 additions & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,6 @@ func TestTxGasLimits(t *testing.T) {
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) {
newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasGranted))

// Decorator will catch an OutOfGasPanic caused in the next antehandler
// AnteHandlers must have their own defer/recover in order for the BaseApp
// to know how much gas was used! This is because the GasMeter is created in
// the AnteHandler, but if it panics the context won't be set properly in
Expand Down
101 changes: 101 additions & 0 deletions x/auth/ante/basic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package ante_test

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

func TestValidateBasic(t *testing.T) {
// setup
_, ctx := createTestApp(true)

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

// msg and signatures
msg1 := types.NewTestMsg(addr1)
fee := types.NewTestStdFee()

msgs := []sdk.Msg{msg1}

privs, accNums, seqs := []crypto.PrivKey{}, []uint64{}, []uint64{}
invalidTx := types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee)

vbd := ante.NewValidateBasicDecorator()
antehandler := sdk.ChainDecorators(vbd)
_, err := antehandler(ctx, invalidTx, false)

require.NotNil(t, err, "Did not error on invalid tx")

privs, accNums, seqs = []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0}
validTx := types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee)

_, err = antehandler(ctx, validTx, false)
require.Nil(t, err, "ValidateBasicDecorator returned error on valid tx. err: %v", err)
}

func TestValidateMemo(t *testing.T) {
// setup
app, ctx := createTestApp(true)

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

// msg and signatures
msg1 := types.NewTestMsg(addr1)
fee := types.NewTestStdFee()

msgs := []sdk.Msg{msg1}

privs, accNums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0}
invalidTx := types.NewTestTxWithMemo(ctx, msgs, privs, accNums, seqs, fee, strings.Repeat("01234567890", 500))

vmd := ante.NewValidateMemoDecorator(app.AccountKeeper)
antehandler := sdk.ChainDecorators(vmd)
_, err := antehandler(ctx, invalidTx, false)

require.NotNil(t, err, "Did not error on tx with high memo")

validTx := types.NewTestTxWithMemo(ctx, msgs, privs, accNums, seqs, fee, strings.Repeat("01234567890", 10))

_, err = antehandler(ctx, validTx, false)
require.Nil(t, err, "ValidateBasicDecorator returned error on valid tx. err: %v", err)

}

func TestConsumeGasForTxSize(t *testing.T) {
// setup
app, ctx := createTestApp(true)

cgtsd := ante.NewConsumeGasForTxSizeDecorator(app.AccountKeeper)
antehandler := sdk.ChainDecorators(cgtsd)

params := app.AccountKeeper.GetParams(ctx)
txBytes := []byte(strings.Repeat("a", 10))
expectedGas := sdk.Gas(len(txBytes)) * params.TxSizeCostPerByte

// Set ctx with TxBytes manually
ctx = ctx.WithTxBytes(txBytes)

// track how much gas is necessary to retrieve parameters
//
beforeGas := ctx.GasMeter().GasConsumed()
app.AccountKeeper.GetParams(ctx)
afterGas := ctx.GasMeter().GasConsumed()
expectedGas += afterGas - beforeGas

// No need to send tx here since this Decorator will do nothing with it
beforeGas = ctx.GasMeter().GasConsumed()
ctx, err := antehandler(ctx, nil, false)
require.Nil(t, err, "ConsumeGasForTxSizeDecorator returned error: %v", err)

consumedGas := ctx.GasMeter().GasConsumed() - beforeGas
require.Equal(t, sdk.Gas(expectedGas), consumedGas, "Decorator did not consume the correct amount of gas")
}
97 changes: 97 additions & 0 deletions x/auth/ante/fee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package ante_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
)

func TestEnsureMempoolFees(t *testing.T) {
// setup
_, ctx := createTestApp(true)

mfd := ante.NewMempoolFeeDecorator()
antehandler := sdk.ChainDecorators(mfd)

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

// msg and signatures
msg1 := types.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)

// Set high gas price so standard test fee fails
atomPrice := sdk.NewDecCoinFromDec("atom", sdk.NewDec(200).Quo(sdk.NewDec(100000)))
highGasPrice := []sdk.DecCoin{atomPrice}
ctx = ctx.WithMinGasPrices(highGasPrice)

// Set IsCheckTx to true
ctx = ctx.WithIsCheckTx(true)

// antehandler errors with insufficient fees
_, err := antehandler(ctx, tx, false)
require.NotNil(t, err, "Decorator should have errored on too low fee for local gasPrice")

// Set IsCheckTx to false
ctx = ctx.WithIsCheckTx(false)

// antehandler should not error since we do not check minGasPrice in DeliverTx
_, err = antehandler(ctx, tx, false)
require.Nil(t, err, "MempoolFeeDecorator returned error in DeliverTx")

// Set IsCheckTx back to true for testing sufficient mempool fee
ctx = ctx.WithIsCheckTx(true)

atomPrice = sdk.NewDecCoinFromDec("atom", sdk.NewDec(0).Quo(sdk.NewDec(100000)))
lowGasPrice := []sdk.DecCoin{atomPrice}
ctx = ctx.WithMinGasPrices(lowGasPrice)

_, err = antehandler(ctx, tx, false)
require.Nil(t, err, "Decorator should not have errored on fee higher than local gasPrice")
}

func TestDeductFees(t *testing.T) {
// setup
app, ctx := createTestApp(true)

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

// msg and signatures
msg1 := types.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)

// Set account with insufficient funds
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc.SetCoins([]sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(10))})
app.AccountKeeper.SetAccount(ctx, acc)

dfd := ante.NewDeductFeeDecorator(app.AccountKeeper, app.SupplyKeeper)
antehandler := sdk.ChainDecorators(dfd)

_, err := antehandler(ctx, tx, false)

require.NotNil(t, err, "Tx did not error when fee payer had insufficient funds")

// Set account with sufficient funds
acc.SetCoins([]sdk.Coin{sdk.NewCoin("atom", sdk.NewInt(200))})
app.AccountKeeper.SetAccount(ctx, acc)

_, err = antehandler(ctx, tx, false)

require.Nil(t, err, "Tx errored after account has been set with sufficient funds")
}
99 changes: 99 additions & 0 deletions x/auth/ante/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package ante_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
errs "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
)

func TestSetup(t *testing.T) {
// setup
_, ctx := createTestApp(true)

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

// msg and signatures
msg1 := types.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.NewSetupDecorator()
antehandler := sdk.ChainDecorators(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, "SetupDecorator returned error")

// Context GasMeter Limit should be set after SetupDecorator 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 := types.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.NewSetupDecorator()
antehandler := sdk.ChainDecorators(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, errs.ErrOutOfGas.Is(err), "Returned error is not an out of gas error")
require.Equal(t, fee.Gas, newCtx.GasMeter().Limit())

antehandler = sdk.ChainDecorators(sud, PanicDecorator{})
require.Panics(t, func() { antehandler(ctx, tx, false) }, "Recovered from non-Out-of-Gas panic")
}

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) (sdk.Context, error) {
panic("random error")

// not reached
return ctx, nil
}
Empty file added x/auth/ante/sigverify_test.go
Empty file.
2 changes: 1 addition & 1 deletion x/auth/types/stdtx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestStdSignBytes(t *testing.T) {
}{
{
args{"1234", 3, 6, defaultFee, []sdk.Msg{sdk.NewTestMsg(addr)}, "memo"},
fmt.Sprintf("{\"account_number\":\"3\",\"chain_id\":\"1234\",\"fee\":{\"amount\":[{\"amount\":\"150\",\"denom\":\"atom\"}],\"gas\":\"50000\"},\"memo\":\"memo\",\"msgs\":[[\"%s\"]],\"sequence\":\"6\"}", addr),
fmt.Sprintf("{\"account_number\":\"3\",\"chain_id\":\"1234\",\"fee\":{\"amount\":[{\"amount\":\"150\",\"denom\":\"atom\"}],\"gas\":\"70000\"},\"memo\":\"memo\",\"msgs\":[[\"%s\"]],\"sequence\":\"6\"}", addr),
},
}
for i, tc := range tests {
Expand Down
2 changes: 1 addition & 1 deletion x/auth/types/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewTestMsg(addrs ...sdk.AccAddress) *sdk.TestMsg {
}

func NewTestStdFee() StdFee {
return NewStdFee(50000,
return NewStdFee(100000,
sdk.NewCoins(sdk.NewInt64Coin("atom", 150)),
)
}
Expand Down