github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/mock/app_test.go (about) 1 package mock 2 3 import ( 4 "testing" 5 6 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply" 7 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 8 "github.com/stretchr/testify/require" 9 10 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 11 sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors" 12 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth" 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply/exported" 14 ) 15 16 const msgRoute = "testMsg" 17 18 var ( 19 numAccts = 2 20 genCoins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 77)} 21 accs, addrs, _, privKeys = CreateGenAccounts(numAccts, genCoins) 22 ) 23 24 // testMsg is a mock transaction that has a validation which can fail. 25 type testMsg struct { 26 signers []sdk.AccAddress 27 positiveNum int64 28 } 29 30 func (tx testMsg) Route() string { return msgRoute } 31 func (tx testMsg) Type() string { return "test" } 32 func (tx testMsg) GetMsg() sdk.Msg { return tx } 33 func (tx testMsg) GetMemo() string { return "" } 34 func (tx testMsg) GetSignBytes() []byte { return nil } 35 func (tx testMsg) GetSigners() []sdk.AccAddress { return tx.signers } 36 func (tx testMsg) GetSignatures() []auth.StdSignature { return nil } 37 func (tx testMsg) ValidateBasic() error { 38 if tx.positiveNum >= 0 { 39 return nil 40 } 41 return sdkerrors.Wrap(sdkerrors.ErrTxDecode, "positiveNum should be a non-negative integer") 42 } 43 44 // getMockApp returns an initialized mock application. 45 func getMockApp(t *testing.T) *App { 46 mApp := NewApp() 47 48 mApp.Router().AddRoute(msgRoute, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { 49 return &sdk.Result{}, nil 50 }) 51 require.NoError(t, mApp.CompleteSetup()) 52 53 return mApp 54 } 55 56 func TestCheckAndDeliverGenTx(t *testing.T) { 57 mApp := getMockApp(t) 58 mApp.Cdc.GetCdc().RegisterConcrete(testMsg{}, "mock/testMsg", nil) 59 mApp.Cdc.GetCdc().RegisterInterface((*exported.ModuleAccountI)(nil), nil) 60 mApp.Cdc.GetCdc().RegisterConcrete(supply.ModuleAccount{}, "cosmos-sdk/ModuleAccount", nil) 61 62 SetGenesis(mApp, accs) 63 mApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: mApp.LastBlockHeight() + 1}}) 64 mApp.EndBlock(abci.RequestEndBlock{}) 65 mApp.Commit(abci.RequestCommit{}) 66 67 ctxCheck := mApp.BaseApp.NewContext(true, abci.Header{}) 68 69 msg := testMsg{signers: []sdk.AccAddress{addrs[0]}, positiveNum: 1} 70 71 acct := mApp.AccountKeeper.GetAccount(ctxCheck, addrs[0]) 72 require.Equal(t, accs[0], acct.(*auth.BaseAccount)) 73 74 header := abci.Header{Height: mApp.LastBlockHeight() + 1} 75 SignCheckDeliver( 76 t, mApp.Cdc.GetCdc(), mApp.BaseApp, header, []sdk.Msg{msg}, 77 []uint64{accs[0].GetAccountNumber()}, []uint64{accs[0].GetSequence()}, 78 true, true, privKeys[0], 79 ) 80 81 // Signing a tx with the wrong privKey should result in an auth error 82 header = abci.Header{Height: mApp.LastBlockHeight() + 1} 83 _, _, err := SignCheckDeliver( 84 t, mApp.Cdc.GetCdc(), mApp.BaseApp, header, []sdk.Msg{msg}, 85 []uint64{accs[1].GetAccountNumber()}, []uint64{accs[1].GetSequence() + 1}, 86 true, false, privKeys[1], 87 ) 88 89 // Will fail on SetPubKey decorator 90 space, code, log := sdkerrors.ABCIInfo(err, false) 91 require.Equal(t, sdkerrors.ErrInvalidPubKey.ABCICode(), code, log) 92 require.Equal(t, sdkerrors.ErrInvalidPubKey.Codespace(), space) 93 94 // Resigning the tx with the correct privKey should result in an OK result 95 header = abci.Header{Height: mApp.LastBlockHeight() + 1} 96 SignCheckDeliver( 97 t, mApp.Cdc.GetCdc(), mApp.BaseApp, header, []sdk.Msg{msg}, 98 []uint64{accs[0].GetAccountNumber()}, []uint64{accs[0].GetSequence() + 1}, 99 true, true, privKeys[0], 100 ) 101 } 102 103 func TestCheckGenTx(t *testing.T) { 104 mApp := getMockApp(t) 105 mApp.Cdc.GetCdc().RegisterConcrete(testMsg{}, "mock/testMsg", nil) 106 mApp.Cdc.GetCdc().RegisterInterface((*exported.ModuleAccountI)(nil), nil) 107 108 SetGenesis(mApp, accs) 109 110 msg1 := testMsg{signers: []sdk.AccAddress{addrs[0]}, positiveNum: 1} 111 CheckGenTx( 112 t, mApp.BaseApp, []sdk.Msg{msg1}, 113 []uint64{accs[0].GetAccountNumber()}, []uint64{accs[0].GetSequence()}, 114 true, privKeys[0], 115 ) 116 117 msg2 := testMsg{signers: []sdk.AccAddress{addrs[0]}, positiveNum: -1} 118 CheckGenTx( 119 t, mApp.BaseApp, []sdk.Msg{msg2}, 120 []uint64{accs[0].GetAccountNumber()}, []uint64{accs[0].GetSequence()}, 121 false, privKeys[0], 122 ) 123 }