Skip to content

Commit

Permalink
add tx by index test
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Jun 14, 2024
1 parent eb004fa commit c469b23
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
33 changes: 31 additions & 2 deletions rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ type BackendTestSuite struct {
signer keyring.Signer
}

// testTx is a dummy implementation of cosmos Tx used for testing.
type testTx struct {
}

func (tx testTx) GetMsgs() []sdk.Msg { return nil }
func (tx testTx) GetSigners() []sdk.AccAddress { return nil }

func (tx testTx) ValidateBasic() error { return nil }
func (t testTx) ProtoMessage() { panic("not implemented") }
func (t testTx) Reset() { panic("not implemented") }

func (t testTx) String() string { panic("not implemented") }

func (t testTx) Bytes() []byte { panic("not implemented") }

func (t testTx) VerifySignature(msg []byte, sig []byte) bool { panic("not implemented") }

func (t testTx) Type() string { panic("not implemented") }

var (
_ sdk.Tx = (*testTx)(nil)
_ sdk.Msg = (*testTx)(nil)
)

func TestBackendTestSuite(t *testing.T) {
suite.Run(t, new(BackendTestSuite))
}
Expand Down Expand Up @@ -119,8 +143,13 @@ func (suite *BackendTestSuite) buildEthereumTx() (*evmtypes.MsgEthereumTx, []byt
return msgEthereumTx, bz
}

func (suite *BackendTestSuite) buildSyntheticTxResult(txHash string) abci.ResponseDeliverTx {
return abci.ResponseDeliverTx{
func (suite *BackendTestSuite) buildSyntheticTxResult(txHash string) ([]byte, abci.ResponseDeliverTx) {
testTx := &testTx{}
txBuilder := suite.backend.clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetSignatures()
txBuilder.SetMsgs(testTx)
bz, _ := suite.backend.clientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
return bz, abci.ResponseDeliverTx{
Code: 0,
Events: []abci.Event{
{Type: evmtypes.EventTypeEthereumTx, Attributes: []abci.EventAttribute{
Expand Down
3 changes: 2 additions & 1 deletion rpc/backend/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ func RegisterBlock(
// with tx
block := types.MakeBlock(height, []types.Tx{tx}, nil, nil)
block.ChainID = ChainID
resBlock := &tmrpctypes.ResultBlock{Block: block}
blockHash := common.BigToHash(big.NewInt(height)).Bytes()
resBlock := &tmrpctypes.ResultBlock{Block: block, BlockID: types.BlockID{Hash: bytes.HexBytes(blockHash)}}
client.On("Block", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).Return(resBlock, nil)
return resBlock, nil
}
Expand Down
33 changes: 31 additions & 2 deletions rpc/backend/tx_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func (suite *BackendTestSuite) TestGetSyntheticTransactionByHash() {
hash := sample.Hash().Hex()
txRes := suite.buildSyntheticTxResult(hash)
_, txRes := suite.buildSyntheticTxResult(hash)

suite.backend.indexer = nil
client := suite.backend.clientCtx.Client.(*mocks.Client)
Expand Down Expand Up @@ -63,7 +63,7 @@ func (suite *BackendTestSuite) TestGetSyntheticTransactionByHash() {

func (suite *BackendTestSuite) TestGetSyntheticTransactionReceiptByHash() {
hash := sample.Hash().Hex()
txRes := suite.buildSyntheticTxResult(hash)
_, txRes := suite.buildSyntheticTxResult(hash)

suite.backend.indexer = nil
client := suite.backend.clientCtx.Client.(*mocks.Client)
Expand Down Expand Up @@ -100,6 +100,35 @@ func (suite *BackendTestSuite) TestGetSyntheticTransactionReceiptByHash() {
suite.Require().Equal(uint64(8888), txIndex)
}

func (suite *BackendTestSuite) TestGetSyntheticTransactionByBlockNumberAndIndex() {
hash := sample.Hash().Hex()
tx, txRes := suite.buildSyntheticTxResult(hash)

suite.backend.indexer = nil
client := suite.backend.clientCtx.Client.(*mocks.Client)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBlock(client, 1, tx)
RegisterBlockResultsWithTxResults(client, 1, []*abci.ResponseDeliverTx{&txRes})
RegisterBaseFee(queryClient, sdk.NewInt(1))

res, err := suite.backend.GetTransactionByBlockNumberAndIndex(rpctypes.BlockNumber(1), 0)
suite.Require().NoError(err)

// assert fields
suite.Require().Equal(hash, res.Hash.Hex())
nonce, _ := hexutil.DecodeUint64(res.Nonce.String())
suite.Require().Equal(uint64(1), nonce)
suite.Require().Equal("0x775b87ef5D82ca211811C1a02CE0fE0CA3a455d7", res.To.Hex())
suite.Require().Equal("0x735b14BB79463307AAcBED86DAf3322B1e6226aB", res.From.Hex())
txType, _ := hexutil.DecodeUint64(res.Type.String())
suite.Require().Equal(uint64(88), txType)
suite.Require().Equal(int64(7001), res.ChainID.ToInt().Int64())
suite.Require().Equal(int64(1000), res.Value.ToInt().Int64())
suite.Require().Nil(res.V)
suite.Require().Nil(res.R)
suite.Require().Nil(res.S)
}

func (suite *BackendTestSuite) TestGetTransactionByHash() {
msgEthereumTx, _ := suite.buildEthereumTx()
txHash := msgEthereumTx.AsTransaction().Hash()
Expand Down

0 comments on commit c469b23

Please sign in to comment.