-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathbasic_test.go
101 lines (72 loc) · 3 KB
/
basic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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")
}