Skip to content

Commit

Permalink
eth_getBlockByNumber fix on batch transactions (evmos#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
crypto-facs authored and yihuang committed Apr 26, 2022
1 parent 2f302a3 commit 6c3f9f1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (rpc) [tharsis#975](https://github.com/tharsis/ethermint/pull/975) Fix unexpected `nil` values for `reward`, returned by `EffectiveGasTipValue(blockBaseFee)` in the `eth_feeHistory` RPC method.
* (rpc) [#970] (https://github.com/tharsis/ethermint/pull/970) Fix unexpected nil reward values on `eth_feeHistory` response
* (rpc) [tharsis#1050](https://github.com/tharsis/ethermint/pull/1050) `eth_getBlockByNumber` fix on batch transactions

### API Breaking

Expand Down
4 changes: 3 additions & 1 deletion rpc/ethereum/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ func (e *EVMBackend) EthBlockFromTendermint(
}

txResults := resBlockResult.TxsResults
txIndex := uint64(0)

for i, txBz := range block.Txs {
tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz)
Expand Down Expand Up @@ -394,14 +395,15 @@ func (e *EVMBackend) EthBlockFromTendermint(
tx,
common.BytesToHash(block.Hash()),
uint64(block.Height),
uint64(i),
txIndex,
baseFee,
)
if err != nil {
e.logger.Debug("NewTransactionFromData for receipt failed", "hash", tx.Hash().Hex(), "error", err.Error())
continue
}
ethRPCTxs = append(ethRPCTxs, rpcTx)
txIndex++
}
}

Expand Down
94 changes: 92 additions & 2 deletions tests/e2e/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"bytes"
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/client/flags"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/tharsis/ethermint/rpc/ethereum/types"
"math/big"
"testing"

Expand Down Expand Up @@ -132,7 +136,6 @@ func (s *IntegrationTestSuite) TestBlock() {
s.Require().NotNil(blockByNum)

// compare the ethereum header with the tendermint header
s.Require().Equal(len(block.Block.Txs), len(blockByNum.Body().Transactions))
s.Require().Equal(block.Block.LastBlockID.Hash.Bytes(), blockByNum.Header().ParentHash.Bytes())

hash := common.BytesToHash(block.Block.Hash())
Expand All @@ -143,7 +146,12 @@ func (s *IntegrationTestSuite) TestBlock() {
blockByHash, err := s.network.Validators[0].JSONRPCClient.BlockByHash(s.ctx, hash)
s.Require().NoError(err)
s.Require().NotNil(blockByHash)
s.Require().Equal(blockByNum, blockByHash)

//Compare blockByNumber and blockByHash results
s.Require().Equal(blockByNum.Hash(), blockByHash.Hash())
s.Require().Equal(blockByNum.Transactions().Len(), blockByHash.Transactions().Len())
s.Require().Equal(blockByNum.ParentHash(), blockByHash.ParentHash())
s.Require().Equal(blockByNum.Root(), blockByHash.Root())

// TODO: parse Tm block to Ethereum and compare
}
Expand Down Expand Up @@ -747,3 +755,85 @@ func (s *IntegrationTestSuite) TestPendingTransactionFilter() {
s.Require().NoError(err)
s.Require().Equal([]common.Hash{signedTx.Hash()}, filterResult)
}

// TODO: add transactionIndex tests once we have OpenRPC interfaces
func (s *IntegrationTestSuite) TestBatchETHTransactions() {
const ethTxs = 2
txBuilder := s.network.Validators[0].ClientCtx.TxConfig.NewTxBuilder()
builder, ok := txBuilder.(authtx.ExtensionOptionsTxBuilder)
s.Require().True(ok)

recipient := common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec")
accountNonce := s.getAccountNonce(recipient)
feeAmount := sdk.ZeroInt()

var gasLimit uint64
var msgs []sdk.Msg

for i := 0; i < ethTxs; i++ {
chainId, err := s.network.Validators[0].JSONRPCClient.ChainID(s.ctx)
s.Require().NoError(err)

gasPrice := s.getGasPrice()
from := common.BytesToAddress(s.network.Validators[0].Address)
nonce := accountNonce + uint64(i) + 1

msgTx := evmtypes.NewTx(
chainId,
nonce,
&recipient,
big.NewInt(10),
100000,
gasPrice,
big.NewInt(200),
nil,
nil,
nil,
)
msgTx.From = from.Hex()
err = msgTx.Sign(s.ethSigner, s.network.Validators[0].ClientCtx.Keyring)
s.Require().NoError(err)

msgs = append(msgs, msgTx.GetMsgs()...)
txData, err := evmtypes.UnpackTxData(msgTx.Data)
s.Require().NoError(err)
feeAmount = feeAmount.Add(sdk.NewIntFromBigInt(txData.Fee()))
gasLimit = gasLimit + txData.GetGas()
}

option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{})
s.Require().NoError(err)

queryClient := types.NewQueryClient(s.network.Validators[0].ClientCtx)
res, err := queryClient.Params(s.ctx, &evmtypes.QueryParamsRequest{})

fees := make(sdk.Coins, 0)
if feeAmount.Sign() > 0 {
fees = fees.Add(sdk.Coin{Denom: res.Params.EvmDenom, Amount: feeAmount})
}

builder.SetExtensionOptions(option)
err = builder.SetMsgs(msgs...)
s.Require().NoError(err)
builder.SetFeeAmount(fees)
builder.SetGasLimit(gasLimit)

tx := builder.GetTx()
txEncoder := s.network.Validators[0].ClientCtx.TxConfig.TxEncoder()
txBytes, err := txEncoder(tx)
s.Require().NoError(err)

syncCtx := s.network.Validators[0].ClientCtx.WithBroadcastMode(flags.BroadcastBlock)
txResponse, err := syncCtx.BroadcastTx(txBytes)
s.Require().NoError(err)
s.Require().Equal(uint32(0), txResponse.Code)

block, err := s.network.Validators[0].JSONRPCClient.BlockByNumber(s.ctx, big.NewInt(txResponse.Height))
s.Require().NoError(err)

txs := block.Transactions()
s.Require().Len(txs, ethTxs)
for i, tx := range txs {
s.Require().Equal(accountNonce+uint64(i)+1, tx.Nonce())
}
}

0 comments on commit 6c3f9f1

Please sign in to comment.