Skip to content

Commit 4c28932

Browse files
committed
refactor: rpc use interface MsgEthereumTx
1 parent f1f4c2a commit 4c28932

File tree

8 files changed

+63
-32
lines changed

8 files changed

+63
-32
lines changed

rpc/backend/backend.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
evmmempool "github.com/cosmos/evm/mempool"
2222
"github.com/cosmos/evm/rpc/types"
23+
"github.com/cosmos/evm/rpc/types/interfaces"
2324
"github.com/cosmos/evm/server/config"
2425
servertypes "github.com/cosmos/evm/server/types"
2526
evmtypes "github.com/cosmos/evm/x/vm/types"
@@ -74,7 +75,7 @@ type EVMBackend interface {
7475
CometBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error)
7576
BlockNumberFromComet(blockNrOrHash types.BlockNumberOrHash) (types.BlockNumber, error)
7677
BlockNumberFromCometByHash(blockHash common.Hash) (*big.Int, error)
77-
EthMsgsFromCometBlock(block *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) []*evmtypes.MsgEthereumTx
78+
EthMsgsFromCometBlock(block *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) []interfaces.IMsgEthereumTx
7879
BlockBloomFromCometBlock(blockRes *tmrpctypes.ResultBlockResults) (ethtypes.Bloom, error)
7980
HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error)
8081
HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)

rpc/backend/comet_to_eth.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
cmtrpctypes "github.com/cometbft/cometbft/rpc/core/types"
1515

1616
rpctypes "github.com/cosmos/evm/rpc/types"
17+
"github.com/cosmos/evm/rpc/types/interfaces"
1718
evmtypes "github.com/cosmos/evm/x/vm/types"
1819

1920
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -87,8 +88,8 @@ func (b *Backend) BlockNumberFromCometByHash(blockHash common.Hash) (*big.Int, e
8788
func (b *Backend) EthMsgsFromCometBlock(
8889
resBlock *cmtrpctypes.ResultBlock,
8990
blockRes *cmtrpctypes.ResultBlockResults,
90-
) []*evmtypes.MsgEthereumTx {
91-
var result []*evmtypes.MsgEthereumTx
91+
) []interfaces.IMsgEthereumTx {
92+
var result []interfaces.IMsgEthereumTx
9293
block := resBlock.Block
9394

9495
txResults := blockRes.TxsResults
@@ -110,7 +111,7 @@ func (b *Backend) EthMsgsFromCometBlock(
110111
}
111112

112113
for _, msg := range tx.GetMsgs() {
113-
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)
114+
ethMsg, ok := msg.(interfaces.IMsgEthereumTx)
114115
if !ok {
115116
continue
116117
}
@@ -225,7 +226,7 @@ func (b *Backend) MinerFromCometBlock(
225226
func (b *Backend) ReceiptsFromCometBlock(
226227
resBlock *cmtrpctypes.ResultBlock,
227228
blockRes *cmtrpctypes.ResultBlockResults,
228-
msgs []*evmtypes.MsgEthereumTx,
229+
msgs []interfaces.IMsgEthereumTx,
229230
) ([]*ethtypes.Receipt, error) {
230231
baseFee, err := b.BaseFee(blockRes)
231232
if err != nil {
@@ -237,18 +238,19 @@ func (b *Backend) ReceiptsFromCometBlock(
237238
receipts := make([]*ethtypes.Receipt, len(msgs))
238239
cumulatedGasUsed := uint64(0)
239240
for i, ethMsg := range msgs {
240-
txResult, err := b.GetTxByEthHash(ethMsg.Hash())
241+
tx := ethMsg.AsTransaction()
242+
txResult, err := b.GetTxByEthHash(tx.Hash())
241243
if err != nil {
242-
return nil, fmt.Errorf("tx not found: hash=%s, error=%s", ethMsg.Hash(), err.Error())
244+
return nil, fmt.Errorf("tx not found: hash=%s, error=%s", tx.Hash().Hex(), err.Error())
243245
}
244246

245247
cumulatedGasUsed += txResult.GasUsed
246248

247249
var effectiveGasPrice *big.Int
248250
if baseFee != nil {
249-
effectiveGasPrice = rpctypes.EffectiveGasPrice(ethMsg.Raw.Transaction, baseFee)
251+
effectiveGasPrice = rpctypes.EffectiveGasPrice(tx, baseFee)
250252
} else {
251-
effectiveGasPrice = ethMsg.Raw.GasFeeCap()
253+
effectiveGasPrice = tx.GasFeeCap()
252254
}
253255

254256
var status uint64
@@ -259,8 +261,8 @@ func (b *Backend) ReceiptsFromCometBlock(
259261
}
260262

261263
contractAddress := common.Address{}
262-
if ethMsg.Raw.To() == nil {
263-
contractAddress = crypto.CreateAddress(ethMsg.GetSender(), ethMsg.Raw.Nonce())
264+
if tx.To() == nil {
265+
contractAddress = crypto.CreateAddress(common.Address(ethMsg.GetFrom().Bytes()), tx.Nonce())
264266
}
265267

266268
msgIndex := int(txResult.MsgIndex) // #nosec G115 -- checked for int overflow already
@@ -277,15 +279,15 @@ func (b *Backend) ReceiptsFromCometBlock(
277279

278280
receipt := &ethtypes.Receipt{
279281
// Consensus fields: These fields are defined by the Yellow Paper
280-
Type: ethMsg.Raw.Type(),
282+
Type: tx.Type(),
281283
PostState: nil,
282284
Status: status, // convert to 1=success, 0=failure
283285
CumulativeGasUsed: cumulatedGasUsed,
284286
Bloom: bloom,
285287
Logs: logs,
286288

287289
// Implementation fields: These fields are added by geth when processing a transaction.
288-
TxHash: ethMsg.Hash(),
290+
TxHash: tx.Hash(),
289291
ContractAddress: contractAddress,
290292
GasUsed: txResult.GasUsed,
291293
EffectiveGasPrice: effectiveGasPrice,

rpc/backend/tx_info.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/cosmos/evm/mempool/txpool"
2222
rpctypes "github.com/cosmos/evm/rpc/types"
23+
"github.com/cosmos/evm/rpc/types/interfaces"
2324
servertypes "github.com/cosmos/evm/server/types"
2425
"github.com/cosmos/evm/utils"
2526
evmtypes "github.com/cosmos/evm/x/vm/types"
@@ -47,7 +48,7 @@ func (b *Backend) GetTransactionByHash(txHash common.Hash) (*rpctypes.RPCTransac
4748
}
4849

4950
// the `res.MsgIndex` is inferred from tx index, should be within the bound.
50-
msg, ok := tx.GetMsgs()[res.MsgIndex].(*evmtypes.MsgEthereumTx)
51+
msg, ok := tx.GetMsgs()[res.MsgIndex].(interfaces.IMsgEthereumTx)
5152
if !ok {
5253
return nil, errors.New("invalid ethereum tx")
5354
}
@@ -62,7 +63,7 @@ func (b *Backend) GetTransactionByHash(txHash common.Hash) (*rpctypes.RPCTransac
6263
// Fallback to find tx index by iterating all valid eth transactions
6364
msgs := b.EthMsgsFromCometBlock(block, blockRes)
6465
for i := range msgs {
65-
if msgs[i].Hash() == txHash {
66+
if msgs[i].AsTransaction().Hash() == txHash {
6667
if i > math.MaxInt32 {
6768
return nil, errors.New("tx index overflow")
6869
}
@@ -113,7 +114,7 @@ func (b *Backend) GetTransactionByHashPending(txHash common.Hash) (*rpctypes.RPC
113114
continue
114115
}
115116

116-
if msg.Hash() == txHash {
117+
if msg.AsTransaction().Hash() == txHash {
117118
// use zero block values since it's not included in a block yet
118119
return rpctypes.NewTransactionFromMsg(
119120
msg,
@@ -192,8 +193,8 @@ func (b *Backend) GetTransactionReceipt(hash common.Hash) (map[string]interface{
192193
return nil, fmt.Errorf("block result not found at height %d: %w", res.Height, err)
193194
}
194195

195-
ethMsg := tx.GetMsgs()[res.MsgIndex].(*evmtypes.MsgEthereumTx)
196-
receipts, err := b.ReceiptsFromCometBlock(resBlock, blockRes, []*evmtypes.MsgEthereumTx{ethMsg})
196+
ethMsg := tx.GetMsgs()[res.MsgIndex].(interfaces.IMsgEthereumTx)
197+
receipts, err := b.ReceiptsFromCometBlock(resBlock, blockRes, []interfaces.IMsgEthereumTx{ethMsg})
197198
if err != nil {
198199
return nil, fmt.Errorf("failed to get receipts from comet block")
199200
}
@@ -364,7 +365,7 @@ func (b *Backend) GetTransactionByBlockAndIndex(block *cmtrpctypes.ResultBlock,
364365
return nil, nil
365366
}
366367

367-
var msg *evmtypes.MsgEthereumTx
368+
var msg interfaces.IMsgEthereumTx
368369
// find in tx indexer
369370
res, err := b.GetTxByTxIndex(block.Block.Height, uint(idx))
370371
if err == nil {
@@ -376,7 +377,7 @@ func (b *Backend) GetTransactionByBlockAndIndex(block *cmtrpctypes.ResultBlock,
376377

377378
var ok bool
378379
// msgIndex is inferred from tx events, should be within bound.
379-
msg, ok = tx.GetMsgs()[res.MsgIndex].(*evmtypes.MsgEthereumTx)
380+
msg, ok = tx.GetMsgs()[res.MsgIndex].(interfaces.IMsgEthereumTx)
380381
if !ok {
381382
b.Logger.Debug("invalid ethereum tx", "height", block.Block.Header, "index", idx)
382383
return nil, nil

rpc/backend/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
cmtrpctypes "github.com/cometbft/cometbft/rpc/core/types"
2020

2121
"github.com/cosmos/evm/rpc/types"
22+
"github.com/cosmos/evm/rpc/types/interfaces"
2223
"github.com/cosmos/evm/utils"
2324
feemarkettypes "github.com/cosmos/evm/x/feemarket/types"
2425
evmtypes "github.com/cosmos/evm/x/vm/types"
@@ -91,7 +92,7 @@ func (b *Backend) getAccountNonce(accAddr common.Address, pending bool, height i
9192
// only supports `MsgEthereumTx` style tx
9293
for _, tx := range pendingTxs {
9394
for _, msg := range (*tx).GetMsgs() {
94-
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)
95+
ethMsg, ok := msg.(interfaces.IMsgEthereumTx)
9596
if !ok {
9697
// not ethereum tx
9798
break
@@ -247,7 +248,7 @@ func (b *Backend) ProcessBlock(
247248
}
248249
txGasUsed := uint64(cometTxResult.GasUsed) // #nosec G115
249250
for _, msg := range tx.GetMsgs() {
250-
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)
251+
ethMsg, ok := msg.(interfaces.IMsgEthereumTx)
251252
if !ok {
252253
continue
253254
}

rpc/types/events.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
abci "github.com/cometbft/cometbft/abci/types"
1010
cmtrpctypes "github.com/cometbft/cometbft/rpc/core/types"
1111

12+
"github.com/cosmos/evm/rpc/types/interfaces"
1213
"github.com/cosmos/evm/server/types"
1314
evmtypes "github.com/cosmos/evm/x/vm/types"
1415

@@ -133,7 +134,7 @@ func ParseTxResult(result *abci.ExecTxResult, tx sdk.Tx) (*ParsedTxs, error) {
133134
p.Txs[i].Failed = true
134135

135136
// replace gasUsed with gasLimit because that's what's actually deducted.
136-
gasLimit := tx.GetMsgs()[i].(*evmtypes.MsgEthereumTx).GetGas()
137+
gasLimit := tx.GetMsgs()[i].(interfaces.IMsgEthereumTx).GetGas()
137138
p.Txs[i].GasUsed = gasLimit
138139
}
139140
}

rpc/types/interfaces/interface.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package interfaces
2+
3+
import (
4+
"math/big"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/ethereum/go-ethereum/common"
8+
"github.com/ethereum/go-ethereum/core"
9+
ethtypes "github.com/ethereum/go-ethereum/core/types"
10+
)
11+
12+
type IMsgEthereumTx interface {
13+
FromEthereumTx(tx *ethtypes.Transaction)
14+
FromSignedEthereumTx(tx *ethtypes.Transaction, signer ethtypes.Signer) error
15+
GetFrom() sdk.AccAddress
16+
GetGas() uint64
17+
GetEffectiveFee(baseFee *big.Int) *big.Int
18+
AsTransaction() *ethtypes.Transaction
19+
GetSenderLegacy(signer ethtypes.Signer) (common.Address, error)
20+
AsMessage(baseFee *big.Int) *core.Message
21+
UnmarshalBinary(b []byte, signer ethtypes.Signer) error
22+
}

rpc/types/utils.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
cmtrpccore "github.com/cometbft/cometbft/rpc/core/types"
1919
cmttypes "github.com/cometbft/cometbft/types"
2020

21+
"github.com/cosmos/evm/rpc/types/interfaces"
2122
feemarkettypes "github.com/cosmos/evm/x/feemarket/types"
2223
evmtypes "github.com/cosmos/evm/x/vm/types"
2324

@@ -38,15 +39,15 @@ const ExceedBlockGasLimitError = "out of gas in location: block gas meter; gasWa
3839
const StateDBCommitError = "failed to commit stateDB"
3940

4041
// RawTxToEthTx returns a evm MsgEthereum transaction from raw tx bytes.
41-
func RawTxToEthTx(clientCtx client.Context, txBz cmttypes.Tx) ([]*evmtypes.MsgEthereumTx, error) {
42+
func RawTxToEthTx(clientCtx client.Context, txBz cmttypes.Tx) ([]interfaces.IMsgEthereumTx, error) {
4243
tx, err := clientCtx.TxConfig.TxDecoder()(txBz)
4344
if err != nil {
4445
return nil, errorsmod.Wrap(errortypes.ErrJSONUnmarshal, err.Error())
4546
}
4647

47-
ethTxs := make([]*evmtypes.MsgEthereumTx, len(tx.GetMsgs()))
48+
ethTxs := make([]interfaces.IMsgEthereumTx, len(tx.GetMsgs()))
4849
for i, msg := range tx.GetMsgs() {
49-
ethTx, ok := msg.(*evmtypes.MsgEthereumTx)
50+
ethTx, ok := msg.(interfaces.IMsgEthereumTx)
5051
if !ok {
5152
return nil, fmt.Errorf("invalid message type %T, expected %T", msg, &evmtypes.MsgEthereumTx{})
5253
}
@@ -155,7 +156,7 @@ func MakeHeader(
155156
// NewTransactionFromMsg returns a transaction that will serialize to the RPC
156157
// representation, with the given location metadata set (if available).
157158
func NewTransactionFromMsg(
158-
msg *evmtypes.MsgEthereumTx,
159+
msg interfaces.IMsgEthereumTx,
159160
blockHash common.Hash,
160161
blockNumber, blockTime, index uint64,
161162
baseFee *big.Int,
@@ -429,7 +430,7 @@ func RPCMarshalHeader(head *ethtypes.Header, blockHash []byte) map[string]interf
429430
//
430431
// This method refers to go-ethereum v1.16.3 internal package method - RPCMarshalBlock
431432
// (https://github.com/ethereum/go-ethereum/blob/d818a9af7bd5919808df78f31580f59382c53150/internal/ethapi/api.go#L929-L962)
432-
func RPCMarshalBlock(block *ethtypes.Block, cmtBlock *cmtrpccore.ResultBlock, msgs []*evmtypes.MsgEthereumTx, inclTx bool, fullTx bool, config *ethparams.ChainConfig) (map[string]interface{}, error) {
433+
func RPCMarshalBlock(block *ethtypes.Block, cmtBlock *cmtrpccore.ResultBlock, msgs []interfaces.IMsgEthereumTx, inclTx bool, fullTx bool, config *ethparams.ChainConfig) (map[string]interface{}, error) {
433434
blockHash := cmtBlock.BlockID.Hash.Bytes()
434435
fields := RPCMarshalHeader(block.Header(), blockHash)
435436
fields["size"] = hexutil.Uint64(block.Size())

x/vm/types/msg.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
protov2 "google.golang.org/protobuf/proto"
1414

1515
evmapi "github.com/cosmos/evm/api/cosmos/evm/vm/v1"
16+
"github.com/cosmos/evm/rpc/types/interfaces"
1617

1718
errorsmod "cosmossdk.io/errors"
1819
sdkmath "cosmossdk.io/math"
@@ -30,10 +31,11 @@ import (
3031
)
3132

3233
var (
33-
_ sdk.Msg = &MsgEthereumTx{}
34-
_ sdk.Tx = &MsgEthereumTx{}
35-
_ ante.GasTx = &MsgEthereumTx{}
36-
_ sdk.Msg = &MsgUpdateParams{}
34+
_ sdk.Msg = &MsgEthereumTx{}
35+
_ sdk.Tx = &MsgEthereumTx{}
36+
_ ante.GasTx = &MsgEthereumTx{}
37+
_ interfaces.IMsgEthereumTx = &MsgEthereumTx{}
38+
_ sdk.Msg = &MsgUpdateParams{}
3739
)
3840

3941
// message type and route constants

0 commit comments

Comments
 (0)