github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/ante/basic_test.go (about) 1 package ante_test 2 3 import ( 4 "encoding/json" 5 "strings" 6 "testing" 7 8 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto" 9 "github.com/stretchr/testify/require" 10 11 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 12 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/ante" 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types" 14 ) 15 16 func TestValidateBasic(t *testing.T) { 17 // setup 18 _, ctx := createTestApp(true) 19 20 // keys and addresses 21 priv1, _, addr1 := types.KeyTestPubAddr() 22 23 // msg and signatures 24 msg1 := types.NewTestMsg(addr1) 25 fee := types.NewTestStdFee() 26 27 msgs := []sdk.Msg{msg1} 28 29 privs, accNums, seqs := []crypto.PrivKey{}, []uint64{}, []uint64{} 30 invalidTx := types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee) 31 32 vbd := ante.NewValidateBasicDecorator() 33 antehandler := sdk.ChainAnteDecorators(vbd) 34 _, err := antehandler(ctx, invalidTx, false) 35 36 require.NotNil(t, err, "Did not error on invalid tx") 37 38 privs, accNums, seqs = []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 39 validTx := types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee) 40 41 _, err = antehandler(ctx, validTx, false) 42 require.Nil(t, err, "ValidateBasicDecorator returned error on valid tx. err: %v", err) 43 44 // test decorator skips on recheck 45 ctx.SetIsReCheckTx(true) 46 47 // decorator should skip processing invalidTx on recheck and thus return nil-error 48 _, err = antehandler(ctx, invalidTx, false) 49 50 require.Nil(t, err, "ValidateBasicDecorator ran on ReCheck") 51 } 52 53 func TestValidateMemo(t *testing.T) { 54 // setup 55 app, ctx := createTestApp(true) 56 57 // keys and addresses 58 priv1, _, addr1 := types.KeyTestPubAddr() 59 60 // msg and signatures 61 msg1 := types.NewTestMsg(addr1) 62 fee := types.NewTestStdFee() 63 64 msgs := []sdk.Msg{msg1} 65 66 privs, accNums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 67 invalidTx := types.NewTestTxWithMemo(ctx, msgs, privs, accNums, seqs, fee, strings.Repeat("01234567890", 500)) 68 69 // require that long memos get rejected 70 vmd := ante.NewValidateMemoDecorator(app.AccountKeeper) 71 antehandler := sdk.ChainAnteDecorators(vmd) 72 _, err := antehandler(ctx, invalidTx, false) 73 74 require.NotNil(t, err, "Did not error on tx with high memo") 75 76 validTx := types.NewTestTxWithMemo(ctx, msgs, privs, accNums, seqs, fee, strings.Repeat("01234567890", 10)) 77 78 // require small memos pass ValidateMemo Decorator 79 _, err = antehandler(ctx, validTx, false) 80 require.Nil(t, err, "ValidateBasicDecorator returned error on valid tx. err: %v", err) 81 } 82 83 func TestConsumeGasForTxSize(t *testing.T) { 84 // setup 85 app, ctx := createTestApp(true) 86 87 // keys and addresses 88 priv1, _, addr1 := types.KeyTestPubAddr() 89 90 // msg and signatures 91 msg1 := types.NewTestMsg(addr1) 92 fee := types.NewTestStdFee() 93 94 msgs := []sdk.Msg{msg1} 95 96 privs, accNums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 97 tx := types.NewTestTxWithMemo(ctx, msgs, privs, accNums, seqs, fee, strings.Repeat("01234567890", 10)) 98 txBytes, err := json.Marshal(tx) 99 require.Nil(t, err, "Cannot marshal tx: %v", err) 100 101 cgtsd := ante.NewConsumeGasForTxSizeDecorator(app.AccountKeeper) 102 antehandler := sdk.ChainAnteDecorators(cgtsd) 103 104 params := app.AccountKeeper.GetParams(ctx) 105 expectedGas := sdk.Gas(len(txBytes)) * params.TxSizeCostPerByte 106 107 // Set ctx with TxBytes manually 108 ctx.SetTxBytes(txBytes) 109 110 // track how much gas is necessary to retrieve parameters 111 beforeGas := ctx.GasMeter().GasConsumed() 112 app.AccountKeeper.GetParams(ctx) 113 afterGas := ctx.GasMeter().GasConsumed() 114 expectedGas += afterGas - beforeGas 115 116 beforeGas = ctx.GasMeter().GasConsumed() 117 ctx, err = antehandler(ctx, tx, false) 118 require.Nil(t, err, "ConsumeTxSizeGasDecorator returned error: %v", err) 119 120 // require that decorator consumes expected amount of gas 121 consumedGas := ctx.GasMeter().GasConsumed() - beforeGas 122 require.Equal(t, expectedGas, consumedGas, "Decorator did not consume the correct amount of gas") 123 124 // simulation must not underestimate gas of this decorator even with nil signatures 125 sigTx := tx.(*types.StdTx) 126 sigTx.Signatures = []types.StdSignature{{}} 127 128 simTxBytes, err := json.Marshal(sigTx) 129 require.Nil(t, err) 130 // require that simulated tx is smaller than tx with signatures 131 require.True(t, len(simTxBytes) < len(txBytes), "simulated tx still has signatures") 132 133 // Set ctx with smaller simulated TxBytes manually 134 ctx.SetTxBytes(txBytes) 135 136 beforeSimGas := ctx.GasMeter().GasConsumed() 137 138 // run antehandler with simulate=true 139 ctx, err = antehandler(ctx, sigTx, true) 140 consumedSimGas := ctx.GasMeter().GasConsumed() - beforeSimGas 141 142 // require that antehandler passes and does not underestimate decorator cost 143 require.Nil(t, err, "ConsumeTxSizeGasDecorator returned error: %v", err) 144 require.True(t, consumedSimGas >= expectedGas, "Simulate mode underestimates gas on AnteDecorator. Simulated cost: %d, expected cost: %d", consumedSimGas, expectedGas) 145 }