github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/mock/test_utils.go (about) 1 package mock 2 3 import ( 4 "math/big" 5 "math/rand" 6 "testing" 7 8 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 9 10 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 11 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto" 12 "github.com/stretchr/testify/require" 13 14 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/baseapp" 15 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 16 ) 17 18 // BigInterval is a representation of the interval [lo, hi), where 19 // lo and hi are both of type sdk.Int 20 type BigInterval struct { 21 lo sdk.Int 22 hi sdk.Int 23 } 24 25 // RandFromBigInterval chooses an interval uniformly from the provided list of 26 // BigIntervals, and then chooses an element from an interval uniformly at random. 27 func RandFromBigInterval(r *rand.Rand, intervals []BigInterval) sdk.Int { 28 if len(intervals) == 0 { 29 return sdk.ZeroInt() 30 } 31 32 interval := intervals[r.Intn(len(intervals))] 33 34 lo := interval.lo 35 hi := interval.hi 36 37 diff := hi.Sub(lo) 38 result := sdk.NewIntFromBigInt(new(big.Int).Rand(r, diff.BigInt())) 39 result = result.Add(lo) 40 41 return result 42 } 43 44 // CheckBalance checks the balance of an account. 45 func CheckBalance(t *testing.T, app *App, addr sdk.AccAddress, exp sdk.Coins) { 46 ctxCheck := app.BaseApp.NewContext(true, abci.Header{}) 47 res := app.AccountKeeper.GetAccount(ctxCheck, addr) 48 49 require.Equal(t, exp, res.GetCoins()) 50 } 51 52 // CheckGenTx checks a generated signed transaction. The result of the check is 53 // compared against the parameter 'expPass'. A test assertion is made using the 54 // parameter 'expPass' against the result. A corresponding result is returned. 55 func CheckGenTx( 56 t *testing.T, app *baseapp.BaseApp, msgs []sdk.Msg, accNums []uint64, 57 seq []uint64, expPass bool, priv ...crypto.PrivKey, 58 ) (sdk.GasInfo, *sdk.Result, error) { 59 tx := GenTx(msgs, accNums, seq, priv...) 60 gInfo, res, err := app.Check(tx) 61 62 if expPass { 63 require.NoError(t, err) 64 require.NotNil(t, res) 65 } else { 66 require.Error(t, err) 67 require.Nil(t, res) 68 } 69 70 return gInfo, res, err 71 } 72 73 // SignCheckDeliver checks a generated signed transaction and simulates a 74 // block commitment with the given transaction. A test assertion is made using 75 // the parameter 'expPass' against the result. A corresponding result is 76 // returned. 77 func SignCheckDeliver( 78 t *testing.T, cdc *codec.Codec, app *baseapp.BaseApp, header abci.Header, msgs []sdk.Msg, 79 accNums, seq []uint64, expSimPass, expPass bool, priv ...crypto.PrivKey, 80 ) (sdk.GasInfo, *sdk.Result, error) { 81 82 tx := GenTx(msgs, accNums, seq, priv...) 83 84 txBytes, err := cdc.MarshalBinaryLengthPrefixed(tx) 85 require.Nil(t, err) 86 87 // Must simulate now as CheckTx doesn't run Msgs anymore 88 _, res, err := app.Simulate(txBytes, tx, 0, nil) 89 90 if expSimPass { 91 require.NoError(t, err) 92 require.NotNil(t, res) 93 } else { 94 require.Error(t, err) 95 require.Nil(t, res) 96 } 97 98 // Simulate a sending a transaction and committing a block 99 app.BeginBlock(abci.RequestBeginBlock{Header: header}) 100 101 gInfo, res, err := app.Deliver(tx) 102 103 if expPass { 104 require.NoError(t, err) 105 require.NotNil(t, res) 106 } else { 107 require.Error(t, err) 108 require.Nil(t, res) 109 } 110 111 app.EndBlock(abci.RequestEndBlock{}) 112 app.Commit(abci.RequestCommit{}) 113 114 return gInfo, res, err 115 }