Skip to content

Commit

Permalink
Revert "Merge pull request #21 from b-harvest/patch/invalid-chainid-t…
Browse files Browse the repository at this point in the history
…xreceipt"

This reverts commit 79be3ff, reversing
changes made to 6d3c5c6.
  • Loading branch information
dudong2 committed Sep 25, 2024
1 parent d0d1df0 commit 50aca0b
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 194 deletions.
26 changes: 0 additions & 26 deletions rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,6 @@ func (suite *BackendTestSuite) buildEthereumTx() (*evmtypes.MsgEthereumTx, []byt
return msgEthereumTx, bz
}

func (suite *BackendTestSuite) buildEthereumTxWithChainId(chainId *big.Int) (*evmtypes.MsgEthereumTx, []byte) {
msgEthereumTx := evmtypes.NewTx(
chainId,
uint64(0),
&common.Address{},
big.NewInt(0),
100000,
big.NewInt(1),
nil,
nil,
nil,
nil,
)

// A valid msg should have empty `From`
msgEthereumTx.From = ""

txBuilder := suite.backend.clientCtx.TxConfig.NewTxBuilder()
err := txBuilder.SetMsgs(msgEthereumTx)
suite.Require().NoError(err)

bz, err := suite.backend.clientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
suite.Require().NoError(err)
return msgEthereumTx, bz
}

// buildFormattedBlock returns a formatted block for testing
func (suite *BackendTestSuite) buildFormattedBlock(
blockRes *tmrpctypes.ResultBlockResults,
Expand Down
11 changes: 1 addition & 10 deletions rpc/backend/tx_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,7 @@ func (b *Backend) GetTransactionReceipt(hash common.Hash) (map[string]interface{
return nil, err
}

// NOTE
// This patch applies only to the period when the chain-id was set to 9000 between the v8 and v8.1.1 versions of Canto.
// It is intended to ensure that reverted transaction receipt returns the same results instead of 'invalid chain id' error.
// The upgrade height for v8 is 10848200, and for v8.1.1, it is 10849447.
var from common.Address
if res.Height >= 10848200 && res.Height < 10849447 {
from, err = ethMsg.GetSender(big.NewInt(9000)) // 9000 is the default chain-id that was applied during that period.
} else {
from, err = ethMsg.GetSender(chainID.ToInt())
}
from, err := ethMsg.GetSender(chainID.ToInt())
if err != nil {
return nil, err
}
Expand Down
158 changes: 0 additions & 158 deletions rpc/backend/tx_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ import (
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/server"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/evmos/ethermint/indexer"
"github.com/evmos/ethermint/rpc/backend/mocks"
rpctypes "github.com/evmos/ethermint/rpc/types"
"github.com/evmos/ethermint/tests"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -608,161 +605,6 @@ func (suite *BackendTestSuite) TestGetTransactionReceipt() {
}
}

func (suite *BackendTestSuite) TestCheckChainIdWithTransactionReceipt() {

patchedHeight := int64(10848200)
notPatchedHeight := int64(10848100)

// TX using chain id 9000
from, pk := tests.NewAddrKey()
msgEthereumTx, _ := suite.buildEthereumTxWithChainId(big.NewInt(9000))
txHash := msgEthereumTx.AsTransaction().Hash()

signer := tests.NewSigner(pk)
ethSigner := ethtypes.NewLondonSigner(big.NewInt(9000))
msgEthereumTx.From = from.String()
err := msgEthereumTx.Sign(ethSigner, signer)
suite.NoError(err)

tx, err := msgEthereumTx.BuildTx(suite.backend.clientCtx.TxConfig.NewTxBuilder(), "aphoton")
suite.NoError(err)
txEncoder := suite.backend.clientCtx.TxConfig.TxEncoder()
txBz, err := txEncoder(tx)
suite.NoError(err)

testCases := []struct {
name string
registerMock func()
tx *evmtypes.MsgEthereumTx
block *types.Block
blockResult *abci.ResponseFinalizeBlock
expTxReceipt map[string]interface{}
expPass bool
}{
{
"success- Receipts with a different chain ID within a patched height",
func() {

ctx := server.NewDefaultContext()

// overwrite client conext
suite.backend.clientCtx.WithChainID("canto_7700-1").WithHeight(patchedHeight)

idxer := indexer.NewKVIndexer(dbm.NewMemDB(), ctx.Logger, suite.backend.clientCtx)
suite.backend = NewBackend(ctx, ctx.Logger, suite.backend.clientCtx, true, idxer)
suite.backend.queryClient.QueryClient = mocks.NewEVMQueryClient(suite.T())
suite.backend.clientCtx.Client = mocks.NewClient(suite.T())
suite.backend.queryClient.FeeMarket = mocks.NewFeeMarketQueryClient(suite.T())
suite.backend.ctx = rpctypes.ContextWithHeight(patchedHeight)

var header metadata.MD
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterParams(queryClient, &header, patchedHeight)
RegisterParamsWithoutHeader(queryClient, patchedHeight)
RegisterBlock(client, patchedHeight, txBz)
RegisterBlockResults(client, patchedHeight)
},
msgEthereumTx,
&types.Block{Header: types.Header{Height: patchedHeight}, Data: types.Data{Txs: []types.Tx{txBz}}},
&abci.ResponseFinalizeBlock{
TxResults: []*abci.ExecTxResult{
{
Code: 0,
Events: []abci.Event{
{Type: evmtypes.EventTypeEthereumTx, Attributes: []abci.EventAttribute{
{Key: "ethereumTxHash", Value: txHash.Hex()},
{Key: "txIndex", Value: "0"},
{Key: "amount", Value: "1000"},
{Key: "txGasUsed", Value: "21000"},
{Key: "txHash", Value: ""},
{Key: "recipient", Value: "0x775b87ef5D82ca211811C1a02CE0fE0CA3a455d7"},
{Key: "status", Value: "0x0"},
}},
},
},
},
},
map[string]interface{}(nil),
true,
},
{
"false- Receipts with a different chain ID not in the patched height. expected to return an invalid chain ID",
func() {

ctx := server.NewDefaultContext()

// overwrite client conext
suite.backend.clientCtx = suite.backend.clientCtx.WithChainID("canto_7700-1").WithHeight(notPatchedHeight)

idxer := indexer.NewKVIndexer(dbm.NewMemDB(), ctx.Logger, suite.backend.clientCtx)
suite.backend = NewBackend(ctx, ctx.Logger, suite.backend.clientCtx, true, idxer)
suite.backend.queryClient.QueryClient = mocks.NewEVMQueryClient(suite.T())
suite.backend.clientCtx.Client = mocks.NewClient(suite.T())
suite.backend.queryClient.FeeMarket = mocks.NewFeeMarketQueryClient(suite.T())
suite.backend.ctx = rpctypes.ContextWithHeight(notPatchedHeight)

var header metadata.MD
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterParams(queryClient, &header, notPatchedHeight)
RegisterParamsWithoutHeader(queryClient, notPatchedHeight)
RegisterBlock(client, notPatchedHeight, txBz)
RegisterBlockResults(client, notPatchedHeight)
},
msgEthereumTx,
&types.Block{Header: types.Header{Height: notPatchedHeight}, Data: types.Data{Txs: []types.Tx{txBz}}},
&abci.ResponseFinalizeBlock{
TxResults: []*abci.ExecTxResult{
{
Code: 0,
Events: []abci.Event{
{Type: evmtypes.EventTypeEthereumTx, Attributes: []abci.EventAttribute{
{Key: "ethereumTxHash", Value: txHash.Hex()},
{Key: "txIndex", Value: "0"},
{Key: "amount", Value: "1000"},
{Key: "txGasUsed", Value: "21000"},
{Key: "txHash", Value: ""},
{Key: "recipient", Value: "0x775b87ef5D82ca211811C1a02CE0fE0CA3a455d7"},
{Key: "status", Value: "0x0"},
}},
},
},
},
},
map[string]interface{}(nil),
false, // invalid chain id error
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset
tc.registerMock()

db := dbm.NewMemDB()
suite.backend.indexer = indexer.NewKVIndexer(db, log.NewNopLogger(), suite.backend.clientCtx)
err := suite.backend.indexer.IndexBlock(tc.block, tc.blockResult)
suite.Require().NoError(err)

txReceipt, err := suite.backend.GetTransactionReceipt(common.HexToHash(tc.tx.Hash))
if tc.expPass {
suite.Require().NoError(err)

// if test passes, all expected receipt keys should be exist.
for k, _ := range tc.expTxReceipt {
_, keyExist := txReceipt[k]
suite.Require().True(keyExist)
}

} else {
suite.Require().Error(err)
}

})
}
}

func (suite *BackendTestSuite) TestGetGasUsed() {
origin := suite.backend.cfg.JSONRPC.FixRevertGasRefundHeight
testCases := []struct {
Expand Down

0 comments on commit 50aca0b

Please sign in to comment.