2021-06-23 06:38:05 +00:00
|
|
|
package backend
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-10 10:04:16 +00:00
|
|
|
"encoding/json"
|
2021-07-05 16:39:08 +00:00
|
|
|
"fmt"
|
2021-04-18 16:39:15 +00:00
|
|
|
"math/big"
|
2021-07-22 15:06:44 +00:00
|
|
|
"strconv"
|
2021-10-25 15:01:04 +00:00
|
|
|
"time"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-08-10 10:04:16 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2021-08-18 14:11:51 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
2021-11-26 14:19:28 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-08-10 10:04:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2021-09-27 22:06:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2021-11-17 11:58:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2022-04-11 08:09:39 +00:00
|
|
|
|
|
|
|
tmrpctypes "github.com/tendermint/tendermint/rpc/coretypes"
|
2021-08-10 10:04:16 +00:00
|
|
|
|
2021-07-22 15:06:44 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/metadata"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-07-12 18:39:35 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2021-04-18 16:39:15 +00:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2021-07-09 08:34:49 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-07-22 15:06:44 +00:00
|
|
|
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
2021-04-18 16:39:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
|
2021-10-01 14:49:22 +00:00
|
|
|
"github.com/tharsis/ethermint/rpc/ethereum/types"
|
2021-08-18 14:11:51 +00:00
|
|
|
"github.com/tharsis/ethermint/server/config"
|
2021-06-22 10:49:18 +00:00
|
|
|
ethermint "github.com/tharsis/ethermint/types"
|
|
|
|
evmtypes "github.com/tharsis/ethermint/x/evm/types"
|
2021-10-05 15:38:20 +00:00
|
|
|
feemarkettypes "github.com/tharsis/ethermint/x/feemarket/types"
|
2021-04-18 16:39:15 +00:00
|
|
|
)
|
|
|
|
|
2021-08-10 10:04:16 +00:00
|
|
|
// Backend implements the functionality shared within namespaces.
|
2021-04-18 16:39:15 +00:00
|
|
|
// Implemented by EVMBackend.
|
|
|
|
type Backend interface {
|
2021-11-17 11:58:52 +00:00
|
|
|
// Fee API
|
|
|
|
FeeHistory(blockCount rpc.DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*types.FeeHistoryResult, error)
|
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
// General Ethereum API
|
2021-10-25 15:01:04 +00:00
|
|
|
RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
|
|
|
|
RPCEVMTimeout() time.Duration // global timeout for eth_call over rpc: DoS protection
|
|
|
|
RPCTxFeeCap() float64 // RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for send-transaction variants. The unit is ether.
|
2021-10-20 19:14:39 +00:00
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
RPCMinGasPrice() int64
|
2022-03-03 20:50:40 +00:00
|
|
|
SuggestGasTipCap(baseFee *big.Int) (*big.Int, error)
|
2021-10-06 11:22:32 +00:00
|
|
|
|
|
|
|
// Blockchain API
|
2021-04-18 16:39:15 +00:00
|
|
|
BlockNumber() (hexutil.Uint64, error)
|
2021-10-06 11:22:32 +00:00
|
|
|
GetTendermintBlockByNumber(blockNum types.BlockNumber) (*tmrpctypes.ResultBlock, error)
|
2021-11-12 08:50:38 +00:00
|
|
|
GetTendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error)
|
2021-04-18 16:39:15 +00:00
|
|
|
GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (map[string]interface{}, error)
|
|
|
|
GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error)
|
2021-10-25 15:01:04 +00:00
|
|
|
BlockByNumber(blockNum types.BlockNumber) (*ethtypes.Block, error)
|
|
|
|
BlockByHash(blockHash common.Hash) (*ethtypes.Block, error)
|
2021-09-27 22:06:27 +00:00
|
|
|
CurrentHeader() *ethtypes.Header
|
2021-08-10 10:04:16 +00:00
|
|
|
HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error)
|
|
|
|
HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)
|
2021-07-09 08:34:49 +00:00
|
|
|
PendingTransactions() ([]*sdk.Tx, error)
|
2021-08-10 10:04:16 +00:00
|
|
|
GetTransactionCount(address common.Address, blockNum types.BlockNumber) (*hexutil.Uint64, error)
|
2021-10-05 15:38:20 +00:00
|
|
|
SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error)
|
2021-08-04 09:18:22 +00:00
|
|
|
GetCoinbase() (sdk.AccAddress, error)
|
2021-09-04 20:33:06 +00:00
|
|
|
GetTransactionByHash(txHash common.Hash) (*types.RPCTransaction, error)
|
|
|
|
GetTxByEthHash(txHash common.Hash) (*tmrpctypes.ResultTx, error)
|
2021-12-16 22:30:22 +00:00
|
|
|
GetTxByTxIndex(height int64, txIndex uint) (*tmrpctypes.ResultTx, error)
|
2021-10-05 15:38:20 +00:00
|
|
|
EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *types.BlockNumber) (hexutil.Uint64, error)
|
2021-10-13 13:39:47 +00:00
|
|
|
BaseFee(height int64) (*big.Int, error)
|
2021-10-06 11:22:32 +00:00
|
|
|
|
|
|
|
// Filter API
|
|
|
|
BloomStatus() (uint64, uint64)
|
|
|
|
GetLogs(hash common.Hash) ([][]*ethtypes.Log, error)
|
|
|
|
GetLogsByHeight(height *int64) ([][]*ethtypes.Log, error)
|
|
|
|
ChainConfig() *params.ChainConfig
|
|
|
|
SetTxDefaults(args evmtypes.TransactionArgs) (evmtypes.TransactionArgs, error)
|
2021-12-16 22:30:22 +00:00
|
|
|
GetEthereumMsgsFromTendermintBlock(block *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) []*evmtypes.MsgEthereumTx
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Backend = (*EVMBackend)(nil)
|
|
|
|
|
2021-09-28 06:03:08 +00:00
|
|
|
var bAttributeKeyEthereumBloom = []byte(evmtypes.AttributeKeyEthereumBloom)
|
|
|
|
|
2021-04-19 10:49:55 +00:00
|
|
|
// EVMBackend implements the Backend interface
|
2021-04-18 16:39:15 +00:00
|
|
|
type EVMBackend struct {
|
|
|
|
ctx context.Context
|
|
|
|
clientCtx client.Context
|
|
|
|
queryClient *types.QueryClient // gRPC query client
|
|
|
|
logger log.Logger
|
2021-07-02 09:34:15 +00:00
|
|
|
chainID *big.Int
|
2021-08-18 14:11:51 +00:00
|
|
|
cfg config.Config
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 10:49:55 +00:00
|
|
|
// NewEVMBackend creates a new EVMBackend instance
|
2021-08-18 14:11:51 +00:00
|
|
|
func NewEVMBackend(ctx *server.Context, logger log.Logger, clientCtx client.Context) *EVMBackend {
|
2021-07-02 09:34:15 +00:00
|
|
|
chainID, err := ethermint.ParseChainID(clientCtx.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-08-18 14:11:51 +00:00
|
|
|
|
2021-09-17 23:47:13 +00:00
|
|
|
appConf := config.GetConfig(ctx.Viper)
|
2021-08-18 14:11:51 +00:00
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
return &EVMBackend{
|
|
|
|
ctx: context.Background(),
|
|
|
|
clientCtx: clientCtx,
|
|
|
|
queryClient: types.NewQueryClient(clientCtx),
|
2021-07-12 18:39:35 +00:00
|
|
|
logger: logger.With("module", "evm-backend"),
|
2021-07-02 09:34:15 +00:00
|
|
|
chainID: chainID,
|
2021-09-17 23:47:13 +00:00
|
|
|
cfg: appConf,
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 15:06:44 +00:00
|
|
|
// BlockNumber returns the current block number in abci app state.
|
|
|
|
// Because abci app state could lag behind from tendermint latest block, it's more stable
|
|
|
|
// for the client to use the latest block number in abci app state than tendermint rpc.
|
2021-04-18 16:39:15 +00:00
|
|
|
func (e *EVMBackend) BlockNumber() (hexutil.Uint64, error) {
|
2021-07-22 15:06:44 +00:00
|
|
|
// do any grpc query, ignore the response and use the returned block height
|
|
|
|
var header metadata.MD
|
|
|
|
_, err := e.queryClient.Params(e.ctx, &evmtypes.QueryParamsRequest{}, grpc.Header(&header))
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return hexutil.Uint64(0), err
|
|
|
|
}
|
|
|
|
|
2021-07-22 15:06:44 +00:00
|
|
|
blockHeightHeader := header.Get(grpctypes.GRPCBlockHeightHeader)
|
|
|
|
if headerLen := len(blockHeightHeader); headerLen != 1 {
|
|
|
|
return 0, fmt.Errorf("unexpected '%s' gRPC header length; got %d, expected: %d", grpctypes.GRPCBlockHeightHeader, headerLen, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
height, err := strconv.ParseUint(blockHeightHeader[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to parse block height: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hexutil.Uint64(height), nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByNumber returns the block identified by number.
|
|
|
|
func (e *EVMBackend) GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (map[string]interface{}, error) {
|
2021-09-15 08:46:01 +00:00
|
|
|
resBlock, err := e.GetTendermintBlockByNumber(blockNum)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-01 10:58:22 +00:00
|
|
|
// return if requested block height is greater than the current one
|
|
|
|
if resBlock == nil || resBlock.Block == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-09-15 08:46:01 +00:00
|
|
|
res, err := e.EthBlockFromTendermint(resBlock.Block, fullTx)
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("EthBlockFromTendermint failed", "height", blockNum, "error", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByHash returns the block identified by hash.
|
|
|
|
func (e *EVMBackend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error) {
|
|
|
|
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, hash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("BlockByHash block not found", "hash", hash.Hex(), "error", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 15:01:04 +00:00
|
|
|
if resBlock == nil || resBlock.Block == nil {
|
2021-09-15 08:46:01 +00:00
|
|
|
e.logger.Debug("BlockByHash block not found", "hash", hash.Hex())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.EthBlockFromTendermint(resBlock.Block, fullTx)
|
|
|
|
}
|
|
|
|
|
2021-10-25 15:01:04 +00:00
|
|
|
// BlockByNumber returns the block identified by number.
|
|
|
|
func (e *EVMBackend) BlockByNumber(blockNum types.BlockNumber) (*ethtypes.Block, error) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
|
|
|
|
switch blockNum {
|
|
|
|
case types.EthLatestBlockNumber:
|
|
|
|
currentBlockNumber, _ := e.BlockNumber()
|
|
|
|
if currentBlockNumber > 0 {
|
|
|
|
height = int64(currentBlockNumber)
|
|
|
|
}
|
|
|
|
case types.EthPendingBlockNumber:
|
|
|
|
currentBlockNumber, _ := e.BlockNumber()
|
|
|
|
if currentBlockNumber > 0 {
|
|
|
|
height = int64(currentBlockNumber)
|
|
|
|
}
|
|
|
|
case types.EthEarliestBlockNumber:
|
|
|
|
height = 1
|
|
|
|
default:
|
|
|
|
if blockNum < 0 {
|
|
|
|
return nil, errors.Errorf("incorrect block height: %d", height)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resBlock, err := e.clientCtx.Client.Block(e.ctx, &height)
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("HeaderByNumber failed", "height", height)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resBlock == nil || resBlock.Block == nil {
|
|
|
|
return nil, errors.Errorf("block not found for height %d", height)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.EthBlockFromTm(resBlock.Block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockByHash returns the block identified by hash.
|
|
|
|
func (e *EVMBackend) BlockByHash(hash common.Hash) (*ethtypes.Block, error) {
|
|
|
|
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, hash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("HeaderByHash failed", "hash", hash.Hex())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resBlock == nil || resBlock.Block == nil {
|
|
|
|
return nil, errors.Errorf("block not found for hash %s", hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.EthBlockFromTm(resBlock.Block)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EVMBackend) EthBlockFromTm(block *tmtypes.Block) (*ethtypes.Block, error) {
|
|
|
|
height := block.Height
|
|
|
|
bloom, err := e.BlockBloom(&height)
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("HeaderByNumber BlockBloom failed", "height", height)
|
|
|
|
}
|
|
|
|
|
|
|
|
baseFee, err := e.BaseFee(height)
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("HeaderByNumber BaseFee failed", "height", height, "error", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
ethHeader := types.EthHeaderFromTendermint(block.Header, bloom, baseFee)
|
2021-10-25 15:01:04 +00:00
|
|
|
|
|
|
|
var txs []*ethtypes.Transaction
|
|
|
|
for _, txBz := range block.Txs {
|
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz)
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("failed to decode transaction in block", "height", height, "error", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, msg := range tx.GetMsgs() {
|
|
|
|
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := ethMsg.AsTransaction()
|
|
|
|
txs = append(txs, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add tx receipts
|
|
|
|
ethBlock := ethtypes.NewBlock(ethHeader, txs, nil, nil, nil)
|
|
|
|
return ethBlock, nil
|
|
|
|
}
|
|
|
|
|
2021-09-15 08:46:01 +00:00
|
|
|
// GetTendermintBlockByNumber returns a Tendermint format block by block number
|
|
|
|
func (e *EVMBackend) GetTendermintBlockByNumber(blockNum types.BlockNumber) (*tmrpctypes.ResultBlock, error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
height := blockNum.Int64()
|
|
|
|
currentBlockNumber, _ := e.BlockNumber()
|
|
|
|
|
|
|
|
switch blockNum {
|
|
|
|
case types.EthLatestBlockNumber:
|
|
|
|
if currentBlockNumber > 0 {
|
2021-07-15 11:07:23 +00:00
|
|
|
height = int64(currentBlockNumber)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
case types.EthPendingBlockNumber:
|
|
|
|
if currentBlockNumber > 0 {
|
|
|
|
height = int64(currentBlockNumber)
|
|
|
|
}
|
|
|
|
case types.EthEarliestBlockNumber:
|
|
|
|
height = 1
|
|
|
|
default:
|
|
|
|
if blockNum < 0 {
|
2021-10-01 10:58:22 +00:00
|
|
|
return nil, errors.Errorf("cannot fetch a negative block height: %d", height)
|
|
|
|
}
|
|
|
|
if height > int64(currentBlockNumber) {
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resBlock, err := e.clientCtx.Client.Block(e.ctx, &height)
|
|
|
|
if err != nil {
|
|
|
|
if resBlock, err = e.clientCtx.Client.Block(e.ctx, nil); err != nil {
|
2021-10-01 10:58:22 +00:00
|
|
|
e.logger.Debug("tendermint client failed to get latest block", "height", height, "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 12:45:13 +00:00
|
|
|
if resBlock.Block == nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("GetBlockByNumber block not found", "height", height)
|
2021-07-12 12:45:13 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2021-10-01 10:58:22 +00:00
|
|
|
|
2021-09-15 08:46:01 +00:00
|
|
|
return resBlock, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 08:50:38 +00:00
|
|
|
// GetTendermintBlockByHash returns a Tendermint format block by block number
|
|
|
|
func (e *EVMBackend) GetTendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error) {
|
|
|
|
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, blockHash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("tendermint client failed to get block", "blockHash", blockHash.Hex(), "error", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if resBlock == nil || resBlock.Block == nil {
|
|
|
|
e.logger.Debug("GetBlockByNumber block not found", "blockHash", blockHash.Hex())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resBlock, nil
|
|
|
|
}
|
|
|
|
|
2021-09-15 09:45:03 +00:00
|
|
|
// BlockBloom query block bloom filter from block results
|
|
|
|
func (e *EVMBackend) BlockBloom(height *int64) (ethtypes.Bloom, error) {
|
|
|
|
result, err := e.clientCtx.Client.BlockResults(e.ctx, height)
|
|
|
|
if err != nil {
|
|
|
|
return ethtypes.Bloom{}, err
|
|
|
|
}
|
|
|
|
for _, event := range result.EndBlockEvents {
|
|
|
|
if event.Type != evmtypes.EventTypeBlockBloom {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attr := range event.Attributes {
|
2022-04-11 08:09:39 +00:00
|
|
|
if attr.Key == string(bAttributeKeyEthereumBloom) {
|
|
|
|
return ethtypes.BytesToBloom([]byte(attr.Value)), nil
|
2021-09-15 09:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ethtypes.Bloom{}, errors.New("block bloom event is not found")
|
|
|
|
}
|
|
|
|
|
2021-09-14 10:27:20 +00:00
|
|
|
// EthBlockFromTendermint returns a JSON-RPC compatible Ethereum block from a given Tendermint block and its block result.
|
2021-04-19 10:49:55 +00:00
|
|
|
func (e *EVMBackend) EthBlockFromTendermint(
|
2021-04-18 16:39:15 +00:00
|
|
|
block *tmtypes.Block,
|
|
|
|
fullTx bool,
|
|
|
|
) (map[string]interface{}, error) {
|
2021-06-08 11:11:37 +00:00
|
|
|
ethRPCTxs := []interface{}{}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
ctx := types.ContextWithHeight(block.Height)
|
|
|
|
|
2021-10-13 13:39:47 +00:00
|
|
|
baseFee, err := e.BaseFee(block.Height)
|
2021-10-06 11:22:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-26 18:10:40 +00:00
|
|
|
resBlockResult, err := e.clientCtx.Client.BlockResults(ctx, &block.Height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
txResults := resBlockResult.TxsResults
|
|
|
|
|
2021-06-08 11:11:37 +00:00
|
|
|
for i, txBz := range block.Txs {
|
2021-07-08 08:23:59 +00:00
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz)
|
2021-07-05 16:39:08 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("failed to decode transaction in block", "height", block.Height, "error", err.Error())
|
2021-07-05 16:39:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-08 08:23:59 +00:00
|
|
|
for _, msg := range tx.GetMsgs() {
|
|
|
|
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
tx := ethMsg.AsTransaction()
|
|
|
|
|
2021-11-26 18:10:40 +00:00
|
|
|
// check tx exists on EVM by cross checking with blockResults
|
|
|
|
if txResults[i].Code != 0 {
|
|
|
|
e.logger.Debug("invalid tx result code", "hash", tx.Hash().Hex())
|
2021-11-16 14:16:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-08 08:23:59 +00:00
|
|
|
if !fullTx {
|
2021-10-06 11:22:32 +00:00
|
|
|
hash := tx.Hash()
|
2021-07-08 08:23:59 +00:00
|
|
|
ethRPCTxs = append(ethRPCTxs, hash)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
rpcTx, err := types.NewRPCTransaction(
|
|
|
|
tx,
|
2021-07-08 08:23:59 +00:00
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
uint64(block.Height),
|
|
|
|
uint64(i),
|
2021-10-06 15:20:34 +00:00
|
|
|
baseFee,
|
2021-07-08 08:23:59 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-10-06 11:22:32 +00:00
|
|
|
e.logger.Debug("NewTransactionFromData for receipt failed", "hash", tx.Hash().Hex(), "error", err.Error())
|
2021-07-08 08:23:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-10-06 11:22:32 +00:00
|
|
|
ethRPCTxs = append(ethRPCTxs, rpcTx)
|
2021-07-08 08:23:59 +00:00
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 09:45:03 +00:00
|
|
|
bloom, err := e.BlockBloom(&block.Height)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("failed to query BlockBloom", "height", block.Height, "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 14:40:32 +00:00
|
|
|
req := &evmtypes.QueryValidatorAccountRequest{
|
|
|
|
ConsAddress: sdk.ConsAddress(block.Header.ProposerAddress).String(),
|
|
|
|
}
|
|
|
|
|
2021-09-27 22:06:27 +00:00
|
|
|
res, err := e.queryClient.ValidatorAccount(ctx, req)
|
2021-07-14 14:40:32 +00:00
|
|
|
if err != nil {
|
2021-09-27 22:06:27 +00:00
|
|
|
e.logger.Debug(
|
|
|
|
"failed to query validator operator address",
|
|
|
|
"height", block.Height,
|
|
|
|
"cons-address", req.ConsAddress,
|
|
|
|
"error", err.Error(),
|
|
|
|
)
|
2021-07-14 14:40:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := sdk.AccAddressFromBech32(res.AccountAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
validatorAddr := common.BytesToAddress(addr)
|
|
|
|
|
2021-11-25 11:30:03 +00:00
|
|
|
gasLimit, err := types.BlockMaxGasFromConsensusParams(ctx, e.clientCtx, block.Height)
|
2021-07-19 01:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
e.logger.Error("failed to query consensus params", "error", err.Error())
|
|
|
|
}
|
2021-09-14 10:27:20 +00:00
|
|
|
|
|
|
|
gasUsed := uint64(0)
|
|
|
|
|
2021-11-26 18:10:40 +00:00
|
|
|
for _, txsResult := range txResults {
|
2022-01-05 07:26:59 +00:00
|
|
|
// workaround for cosmos-sdk bug. https://github.com/cosmos/cosmos-sdk/issues/10832
|
|
|
|
if txsResult.GetCode() == 11 && txsResult.GetLog() == "no block gas left to run tx: out of gas" {
|
|
|
|
// block gas limit has exceeded, other txs must have failed with same reason.
|
|
|
|
break
|
|
|
|
}
|
2021-09-14 10:27:20 +00:00
|
|
|
gasUsed += uint64(txsResult.GetGasUsed())
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
formattedBlock := types.FormatBlock(
|
|
|
|
block.Header, block.Size(),
|
|
|
|
gasLimit, new(big.Int).SetUint64(gasUsed),
|
2021-10-06 15:20:34 +00:00
|
|
|
ethRPCTxs, bloom, validatorAddr, baseFee,
|
2021-10-05 15:38:20 +00:00
|
|
|
)
|
2021-04-18 16:39:15 +00:00
|
|
|
return formattedBlock, nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 22:06:27 +00:00
|
|
|
// CurrentHeader returns the latest block header
|
|
|
|
func (e *EVMBackend) CurrentHeader() *ethtypes.Header {
|
|
|
|
header, _ := e.HeaderByNumber(types.EthLatestBlockNumber)
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
// HeaderByNumber returns the block header identified by height.
|
|
|
|
func (e *EVMBackend) HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
|
|
|
|
switch blockNum {
|
|
|
|
case types.EthLatestBlockNumber:
|
2021-10-25 15:01:04 +00:00
|
|
|
currentBlockNumber, _ := e.BlockNumber()
|
2021-04-18 16:39:15 +00:00
|
|
|
if currentBlockNumber > 0 {
|
2021-07-15 11:07:23 +00:00
|
|
|
height = int64(currentBlockNumber)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
case types.EthPendingBlockNumber:
|
2021-10-25 15:01:04 +00:00
|
|
|
currentBlockNumber, _ := e.BlockNumber()
|
2021-04-18 16:39:15 +00:00
|
|
|
if currentBlockNumber > 0 {
|
|
|
|
height = int64(currentBlockNumber)
|
|
|
|
}
|
|
|
|
case types.EthEarliestBlockNumber:
|
|
|
|
height = 1
|
|
|
|
default:
|
|
|
|
if blockNum < 0 {
|
2021-07-12 18:39:35 +00:00
|
|
|
return nil, errors.Errorf("incorrect block height: %d", height)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resBlock, err := e.clientCtx.Client.Block(e.ctx, &height)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("HeaderByNumber failed")
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-15 09:45:03 +00:00
|
|
|
bloom, err := e.BlockBloom(&resBlock.Block.Height)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("HeaderByNumber BlockBloom failed", "height", resBlock.Block.Height)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 13:39:47 +00:00
|
|
|
baseFee, err := e.BaseFee(resBlock.Block.Height)
|
2021-10-06 11:22:32 +00:00
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("HeaderByNumber BaseFee failed", "height", resBlock.Block.Height, "error", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
ethHeader := types.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee)
|
2021-04-18 16:39:15 +00:00
|
|
|
return ethHeader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeaderByHash returns the block header identified by hash.
|
|
|
|
func (e *EVMBackend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) {
|
|
|
|
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, blockHash.Bytes())
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("HeaderByHash failed", "hash", blockHash.Hex())
|
2021-04-18 16:39:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 15:01:04 +00:00
|
|
|
if resBlock == nil || resBlock.Block == nil {
|
2021-08-25 01:21:57 +00:00
|
|
|
return nil, errors.Errorf("block not found for hash %s", blockHash.Hex())
|
|
|
|
}
|
|
|
|
|
2021-09-15 09:45:03 +00:00
|
|
|
bloom, err := e.BlockBloom(&resBlock.Block.Height)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
e.logger.Debug("HeaderByHash BlockBloom failed", "height", resBlock.Block.Height)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 13:39:47 +00:00
|
|
|
baseFee, err := e.BaseFee(resBlock.Block.Height)
|
2021-10-06 11:22:32 +00:00
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("HeaderByHash BaseFee failed", "height", resBlock.Block.Height, "error", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
ethHeader := types.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee)
|
2021-04-18 16:39:15 +00:00
|
|
|
return ethHeader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PendingTransactions returns the transactions that are in the transaction pool
|
|
|
|
// and have a from address that is one of the accounts this node manages.
|
2021-07-09 08:34:49 +00:00
|
|
|
func (e *EVMBackend) PendingTransactions() ([]*sdk.Tx, error) {
|
|
|
|
res, err := e.clientCtx.Client.UnconfirmedTxs(e.ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*sdk.Tx, 0, len(res.Txs))
|
|
|
|
for _, txBz := range res.Txs {
|
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, &tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 09:45:03 +00:00
|
|
|
// GetLogsByHeight returns all the logs from all the ethereum transactions in a block.
|
|
|
|
func (e *EVMBackend) GetLogsByHeight(height *int64) ([][]*ethtypes.Log, error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
// NOTE: we query the state in case the tx result logs are not persisted after an upgrade.
|
2021-09-15 09:45:03 +00:00
|
|
|
blockRes, err := e.clientCtx.Client.BlockResults(e.ctx, height)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-05 11:03:06 +00:00
|
|
|
blockLogs := [][]*ethtypes.Log{}
|
2021-09-15 09:45:03 +00:00
|
|
|
for _, txResult := range blockRes.TxsResults {
|
2022-01-14 09:37:33 +00:00
|
|
|
logs, err := AllTxLogsFromEvents(txResult.Events)
|
2021-10-05 10:26:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
blockLogs = append(blockLogs, logs...)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return blockLogs, nil
|
|
|
|
}
|
|
|
|
|
2021-09-15 09:45:03 +00:00
|
|
|
// GetLogs returns all the logs from all the ethereum transactions in a block.
|
|
|
|
func (e *EVMBackend) GetLogs(hash common.Hash) ([][]*ethtypes.Log, error) {
|
|
|
|
block, err := e.clientCtx.Client.BlockByHash(e.ctx, hash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return e.GetLogsByHeight(&block.Block.Header.Height)
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
func (e *EVMBackend) GetLogsByNumber(blockNum types.BlockNumber) ([][]*ethtypes.Log, error) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
|
|
|
|
switch blockNum {
|
|
|
|
case types.EthLatestBlockNumber:
|
2021-10-25 15:01:04 +00:00
|
|
|
currentBlockNumber, _ := e.BlockNumber()
|
2021-04-18 16:39:15 +00:00
|
|
|
if currentBlockNumber > 0 {
|
2021-07-15 11:07:23 +00:00
|
|
|
height = int64(currentBlockNumber)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
case types.EthPendingBlockNumber:
|
2021-10-25 15:01:04 +00:00
|
|
|
currentBlockNumber, _ := e.BlockNumber()
|
2021-04-18 16:39:15 +00:00
|
|
|
if currentBlockNumber > 0 {
|
|
|
|
height = int64(currentBlockNumber)
|
|
|
|
}
|
|
|
|
case types.EthEarliestBlockNumber:
|
|
|
|
height = 1
|
|
|
|
default:
|
|
|
|
if blockNum < 0 {
|
2021-07-12 18:39:35 +00:00
|
|
|
return nil, errors.Errorf("incorrect block height: %d", height)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 09:45:03 +00:00
|
|
|
return e.GetLogsByHeight(&height)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BloomStatus returns the BloomBitsBlocks and the number of processed sections maintained
|
|
|
|
// by the chain indexer.
|
|
|
|
func (e *EVMBackend) BloomStatus() (uint64, uint64) {
|
|
|
|
return 4096, 0
|
|
|
|
}
|
2021-08-04 09:18:22 +00:00
|
|
|
|
|
|
|
// GetCoinbase is the address that staking rewards will be send to (alias for Etherbase).
|
|
|
|
func (e *EVMBackend) GetCoinbase() (sdk.AccAddress, error) {
|
|
|
|
node, err := e.clientCtx.GetNode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := node.Status(e.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &evmtypes.QueryValidatorAccountRequest{
|
|
|
|
ConsAddress: sdk.ConsAddress(status.ValidatorInfo.Address).String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := e.queryClient.ValidatorAccount(e.ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
address, _ := sdk.AccAddressFromBech32(res.AccountAddress)
|
|
|
|
return address, nil
|
|
|
|
}
|
2021-08-10 10:04:16 +00:00
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
// GetTransactionByHash returns the Ethereum format transaction identified by Ethereum transaction hash
|
|
|
|
func (e *EVMBackend) GetTransactionByHash(txHash common.Hash) (*types.RPCTransaction, error) {
|
|
|
|
res, err := e.GetTxByEthHash(txHash)
|
2021-11-16 14:16:57 +00:00
|
|
|
hexTx := txHash.Hex()
|
2021-09-04 20:33:06 +00:00
|
|
|
if err != nil {
|
|
|
|
// try to find tx in mempool
|
|
|
|
txs, err := e.PendingTransactions()
|
|
|
|
if err != nil {
|
2021-11-16 14:16:57 +00:00
|
|
|
e.logger.Debug("tx not found", "hash", hexTx, "error", err.Error())
|
2021-09-04 20:33:06 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tx := range txs {
|
2022-01-14 09:37:33 +00:00
|
|
|
msg, err := evmtypes.UnwrapEthereumMsg(tx, txHash)
|
2021-09-04 20:33:06 +00:00
|
|
|
if err != nil {
|
|
|
|
// not ethereum tx
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-16 14:16:57 +00:00
|
|
|
if msg.Hash == hexTx {
|
2021-09-04 20:33:06 +00:00
|
|
|
rpctx, err := types.NewTransactionFromMsg(
|
|
|
|
msg,
|
|
|
|
common.Hash{},
|
|
|
|
uint64(0),
|
|
|
|
uint64(0),
|
|
|
|
e.chainID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rpctx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 14:16:57 +00:00
|
|
|
e.logger.Debug("tx not found", "hash", hexTx)
|
2021-09-04 20:33:06 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:30:22 +00:00
|
|
|
if res.TxResult.Code != 0 {
|
|
|
|
return nil, errors.New("invalid ethereum tx")
|
|
|
|
}
|
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
msgIndex, attrs := types.FindTxAttributes(res.TxResult.Events, hexTx)
|
|
|
|
if msgIndex < 0 {
|
|
|
|
return nil, fmt.Errorf("ethereum tx not found in msgs: %s", hexTx)
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:30:22 +00:00
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(res.Tx)
|
2021-09-04 20:33:06 +00:00
|
|
|
if err != nil {
|
2021-12-16 22:30:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
// the `msgIndex` is inferred from tx events, should be within the bound.
|
|
|
|
msg, ok := tx.GetMsgs()[msgIndex].(*evmtypes.MsgEthereumTx)
|
2021-12-16 22:30:22 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("invalid ethereum tx")
|
|
|
|
}
|
2021-09-04 20:33:06 +00:00
|
|
|
|
2021-12-16 22:30:22 +00:00
|
|
|
block, err := e.clientCtx.Client.Block(e.ctx, &res.Height)
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("block not found", "height", res.Height, "error", err.Error())
|
|
|
|
return nil, err
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 22:30:22 +00:00
|
|
|
// Try to find txIndex from events
|
|
|
|
found := false
|
2022-01-14 09:37:33 +00:00
|
|
|
txIndex, err := types.GetUint64Attribute(attrs, evmtypes.AttributeKeyTxIndex)
|
2021-12-16 22:30:22 +00:00
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
} else {
|
|
|
|
// Fallback to find tx index by iterating all valid eth transactions
|
|
|
|
blockRes, err := e.clientCtx.Client.BlockResults(e.ctx, &block.Block.Height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
msgs := e.GetEthereumMsgsFromTendermintBlock(block, blockRes)
|
|
|
|
for i := range msgs {
|
|
|
|
if msgs[i].Hash == hexTx {
|
|
|
|
txIndex = uint64(i)
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, errors.New("can't find index of ethereum tx")
|
|
|
|
}
|
2021-11-16 14:16:57 +00:00
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
return types.NewTransactionFromMsg(
|
|
|
|
msg,
|
2021-12-16 22:30:22 +00:00
|
|
|
common.BytesToHash(block.BlockID.Hash.Bytes()),
|
2021-09-04 20:33:06 +00:00
|
|
|
uint64(res.Height),
|
2021-11-16 14:16:57 +00:00
|
|
|
txIndex,
|
2021-09-04 20:33:06 +00:00
|
|
|
e.chainID,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxByEthHash uses `/tx_query` to find transaction by ethereum tx hash
|
|
|
|
// TODO: Don't need to convert once hashing is fixed on Tendermint
|
|
|
|
// https://github.com/tendermint/tendermint/issues/6539
|
|
|
|
func (e *EVMBackend) GetTxByEthHash(hash common.Hash) (*tmrpctypes.ResultTx, error) {
|
|
|
|
query := fmt.Sprintf("%s.%s='%s'", evmtypes.TypeMsgEthereumTx, evmtypes.AttributeKeyEthereumTxHash, hash.Hex())
|
|
|
|
resTxs, err := e.clientCtx.Client.TxSearch(e.ctx, query, false, nil, nil, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(resTxs.Txs) == 0 {
|
|
|
|
return nil, errors.Errorf("ethereum tx not found for hash %s", hash.Hex())
|
|
|
|
}
|
|
|
|
return resTxs.Txs[0], nil
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:30:22 +00:00
|
|
|
// GetTxByTxIndex uses `/tx_query` to find transaction by tx index of valid ethereum txs
|
|
|
|
func (e *EVMBackend) GetTxByTxIndex(height int64, index uint) (*tmrpctypes.ResultTx, error) {
|
|
|
|
query := fmt.Sprintf("tx.height=%d AND %s.%s=%d",
|
|
|
|
height, evmtypes.TypeMsgEthereumTx,
|
|
|
|
evmtypes.AttributeKeyTxIndex, index,
|
|
|
|
)
|
|
|
|
resTxs, err := e.clientCtx.Client.TxSearch(e.ctx, query, false, nil, nil, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(resTxs.Txs) == 0 {
|
|
|
|
return nil, errors.Errorf("ethereum tx not found for block %d index %d", height, index)
|
|
|
|
}
|
|
|
|
return resTxs.Txs[0], nil
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
func (e *EVMBackend) SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error) {
|
2021-08-10 10:04:16 +00:00
|
|
|
// Look up the wallet containing the requested signer
|
|
|
|
_, err := e.clientCtx.Keyring.KeyByAddress(sdk.AccAddress(args.From.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Error("failed to find key in keyring", "address", args.From, "error", err.Error())
|
|
|
|
return common.Hash{}, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
args, err = e.SetTxDefaults(args)
|
2021-08-10 10:04:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := args.ToTransaction()
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
e.logger.Debug("tx failed basic validation", "error", err.Error())
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
bn, err := e.BlockNumber()
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("failed to fetch latest block number", "error", err.Error())
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
signer := ethtypes.MakeSigner(e.ChainConfig(), new(big.Int).SetUint64(uint64(bn)))
|
2021-08-10 10:04:16 +00:00
|
|
|
|
|
|
|
// Sign transaction
|
|
|
|
if err := msg.Sign(signer, e.clientCtx.Keyring); err != nil {
|
|
|
|
e.logger.Debug("failed to sign tx", "error", err.Error())
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query params to use the EVM denomination
|
|
|
|
res, err := e.queryClient.QueryClient.Params(e.ctx, &evmtypes.QueryParamsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Error("failed to query evm params", "error", err.Error())
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:44:41 +00:00
|
|
|
// Assemble transaction from fields
|
|
|
|
tx, err := msg.BuildTx(e.clientCtx.TxConfig.NewTxBuilder(), res.Params.EvmDenom)
|
2021-08-10 10:04:16 +00:00
|
|
|
if err != nil {
|
2022-01-26 10:44:41 +00:00
|
|
|
e.logger.Error("build cosmos tx failed", "error", err.Error())
|
2021-08-10 10:04:16 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode transaction by default Tx encoder
|
|
|
|
txEncoder := e.clientCtx.TxConfig.TxEncoder()
|
2022-01-26 10:44:41 +00:00
|
|
|
txBytes, err := txEncoder(tx)
|
2021-08-10 10:04:16 +00:00
|
|
|
if err != nil {
|
|
|
|
e.logger.Error("failed to encode eth tx using default encoder", "error", err.Error())
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
txHash := msg.AsTransaction().Hash()
|
|
|
|
|
|
|
|
// Broadcast transaction in sync mode (default)
|
|
|
|
// NOTE: If error is encountered on the node, the broadcast will not return an error
|
|
|
|
syncCtx := e.clientCtx.WithBroadcastMode(flags.BroadcastSync)
|
|
|
|
rsp, err := syncCtx.BroadcastTx(txBytes)
|
2021-11-26 14:19:28 +00:00
|
|
|
if rsp != nil && rsp.Code != 0 {
|
|
|
|
err = sdkerrors.ABCIError(rsp.Codespace, rsp.Code, rsp.RawLog)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-08-10 10:04:16 +00:00
|
|
|
e.logger.Error("failed to broadcast tx", "error", err.Error())
|
|
|
|
return txHash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return transaction hash
|
|
|
|
return txHash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateGas returns an estimate of gas usage for the given smart contract call.
|
2021-10-05 15:38:20 +00:00
|
|
|
func (e *EVMBackend) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *types.BlockNumber) (hexutil.Uint64, error) {
|
2021-08-10 10:04:16 +00:00
|
|
|
blockNr := types.EthPendingBlockNumber
|
|
|
|
if blockNrOptional != nil {
|
|
|
|
blockNr = *blockNrOptional
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := json.Marshal(&args)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-08-18 14:11:51 +00:00
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
req := evmtypes.EthCallRequest{
|
2021-10-21 15:29:19 +00:00
|
|
|
Args: bz,
|
|
|
|
GasCap: e.RPCGasCap(),
|
2021-10-06 11:22:32 +00:00
|
|
|
}
|
2021-08-10 10:04:16 +00:00
|
|
|
|
|
|
|
// From ContextWithHeight: if the provided height is 0,
|
|
|
|
// it will return an empty context and the gRPC query will use
|
|
|
|
// the latest block height for querying.
|
2021-10-21 15:29:19 +00:00
|
|
|
res, err := e.queryClient.EstimateGas(types.ContextWithHeight(blockNr.Int64()), &req)
|
2021-08-10 10:04:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return hexutil.Uint64(res.Gas), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionCount returns the number of transactions at the given address up to the given block number.
|
|
|
|
func (e *EVMBackend) GetTransactionCount(address common.Address, blockNum types.BlockNumber) (*hexutil.Uint64, error) {
|
|
|
|
// Get nonce (sequence) from account
|
|
|
|
from := sdk.AccAddress(address.Bytes())
|
|
|
|
accRet := e.clientCtx.AccountRetriever
|
|
|
|
|
|
|
|
err := accRet.EnsureExists(e.clientCtx, from)
|
|
|
|
if err != nil {
|
|
|
|
// account doesn't exist yet, return 0
|
|
|
|
n := hexutil.Uint64(0)
|
|
|
|
return &n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
includePending := blockNum == types.EthPendingBlockNumber
|
|
|
|
nonce, err := e.getAccountNonce(address, includePending, blockNum.Int64(), e.logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n := hexutil.Uint64(nonce)
|
|
|
|
return &n, nil
|
|
|
|
}
|
2021-08-18 14:11:51 +00:00
|
|
|
|
|
|
|
// RPCGasCap is the global gas cap for eth-call variants.
|
|
|
|
func (e *EVMBackend) RPCGasCap() uint64 {
|
|
|
|
return e.cfg.JSONRPC.GasCap
|
|
|
|
}
|
2021-09-17 23:47:13 +00:00
|
|
|
|
2021-10-25 15:01:04 +00:00
|
|
|
// RPCEVMTimeout is the global evm timeout for eth-call variants.
|
|
|
|
func (e *EVMBackend) RPCEVMTimeout() time.Duration {
|
|
|
|
return e.cfg.JSONRPC.EVMTimeout
|
|
|
|
}
|
|
|
|
|
2021-10-20 19:14:39 +00:00
|
|
|
// RPCGasCap is the global gas cap for eth-call variants.
|
|
|
|
func (e *EVMBackend) RPCTxFeeCap() float64 {
|
|
|
|
return e.cfg.JSONRPC.TxFeeCap
|
|
|
|
}
|
|
|
|
|
2021-10-13 11:03:49 +00:00
|
|
|
// RPCFilterCap is the limit for total number of filters that can be created
|
|
|
|
func (e *EVMBackend) RPCFilterCap() int32 {
|
|
|
|
return e.cfg.JSONRPC.FilterCap
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:58:52 +00:00
|
|
|
// RPCFeeHistoryCap is the limit for total number of blocks that can be fetched
|
|
|
|
func (e *EVMBackend) RPCFeeHistoryCap() int32 {
|
|
|
|
return e.cfg.JSONRPC.FeeHistoryCap
|
|
|
|
}
|
|
|
|
|
2021-12-29 21:47:25 +00:00
|
|
|
// RPCLogsCap defines the max number of results can be returned from single `eth_getLogs` query.
|
|
|
|
func (e *EVMBackend) RPCLogsCap() int32 {
|
|
|
|
return e.cfg.JSONRPC.LogsCap
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPCBlockRangeCap defines the max block range allowed for `eth_getLogs` query.
|
|
|
|
func (e *EVMBackend) RPCBlockRangeCap() int32 {
|
|
|
|
return e.cfg.JSONRPC.BlockRangeCap
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:42:18 +00:00
|
|
|
// RPCMinGasPrice returns the minimum gas price for a transaction obtained from
|
|
|
|
// the node config. If set value is 0, it will default to 20.
|
|
|
|
|
2021-09-17 23:47:13 +00:00
|
|
|
func (e *EVMBackend) RPCMinGasPrice() int64 {
|
2021-10-06 11:22:32 +00:00
|
|
|
evmParams, err := e.queryClient.Params(e.ctx, &evmtypes.QueryParamsRequest{})
|
2021-09-28 08:42:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return ethermint.DefaultGasPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
minGasPrice := e.cfg.GetMinGasPrices()
|
|
|
|
amt := minGasPrice.AmountOf(evmParams.Params.EvmDenom).TruncateInt64()
|
|
|
|
if amt == 0 {
|
|
|
|
return ethermint.DefaultGasPrice
|
2021-09-17 23:47:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 08:42:18 +00:00
|
|
|
return amt
|
2021-09-17 23:47:13 +00:00
|
|
|
}
|
2021-09-27 14:35:37 +00:00
|
|
|
|
2021-09-27 22:06:27 +00:00
|
|
|
// ChainConfig return the ethereum chain configuration
|
|
|
|
func (e *EVMBackend) ChainConfig() *params.ChainConfig {
|
|
|
|
params, err := e.queryClient.Params(e.ctx, &evmtypes.QueryParamsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return params.Params.ChainConfig.EthereumConfig(e.chainID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SuggestGasTipCap returns the suggested tip cap
|
2022-03-03 20:50:40 +00:00
|
|
|
// Although we don't support tx prioritization yet, but we return a positive value to help client to
|
|
|
|
// mitigate the base fee changes.
|
|
|
|
func (e *EVMBackend) SuggestGasTipCap(baseFee *big.Int) (*big.Int, error) {
|
|
|
|
if baseFee == nil {
|
|
|
|
// london hardfork not enabled or feemarket not enabeld
|
|
|
|
return big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params, err := e.queryClient.FeeMarket.Params(e.ctx, &feemarkettypes.QueryParamsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// calculate the maximum base fee delta in current block, assuming all block gas limit is consumed
|
|
|
|
// ```
|
|
|
|
// GasTarget = GasLimit / ElasticityMultiplier
|
|
|
|
// Delta = BaseFee * (GasUsed - GasTarget) / GasTarget / Denominator
|
|
|
|
// ```
|
|
|
|
// The delta is at maximum when `GasUsed` is equal to `GasLimit`, which is:
|
|
|
|
// ```
|
|
|
|
// MaxDelta = BaseFee * (GasLimit - GasLimit / ElasticityMultiplier) / (GasLimit / ElasticityMultiplier) / Denominator
|
|
|
|
// = BaseFee * (ElasticityMultiplier - 1) / Denominator
|
|
|
|
// ```
|
|
|
|
maxDelta := baseFee.Int64() * (int64(params.Params.ElasticityMultiplier) - 1) / int64(params.Params.BaseFeeChangeDenominator)
|
|
|
|
if maxDelta < 0 {
|
|
|
|
// impossible if the parameter validation passed.
|
|
|
|
maxDelta = 0
|
|
|
|
}
|
|
|
|
return big.NewInt(maxDelta), nil
|
2021-10-06 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BaseFee returns the base fee tracked by the Fee Market module. If the base fee is not enabled,
|
2021-10-13 13:39:47 +00:00
|
|
|
// it returns the initial base fee amount. Return nil if London is not activated.
|
|
|
|
func (e *EVMBackend) BaseFee(height int64) (*big.Int, error) {
|
|
|
|
cfg := e.ChainConfig()
|
|
|
|
if !cfg.IsLondon(new(big.Int).SetInt64(height)) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 17:47:32 +00:00
|
|
|
// Checks the feemarket param NoBaseFee settings, return 0 if it is enabled.
|
2021-12-13 23:55:41 +00:00
|
|
|
resParams, err := e.queryClient.FeeMarket.Params(types.ContextWithHeight(height), &feemarkettypes.QueryParamsRequest{})
|
2021-10-23 17:47:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resParams.Params.NoBaseFee {
|
|
|
|
return big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2021-10-15 08:59:26 +00:00
|
|
|
blockRes, err := e.clientCtx.Client.BlockResults(e.ctx, &height)
|
2021-10-06 11:22:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-15 02:10:52 +00:00
|
|
|
baseFee := types.BaseFeeFromEvents(blockRes.BeginBlockEvents)
|
2021-10-15 08:59:26 +00:00
|
|
|
if baseFee != nil {
|
|
|
|
return baseFee, nil
|
2021-10-06 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 08:59:26 +00:00
|
|
|
// If we cannot find in events, we tried to get it from the state.
|
|
|
|
// It will return feemarket.baseFee if london is activated but feemarket is not enable
|
|
|
|
res, err := e.queryClient.FeeMarket.BaseFee(types.ContextWithHeight(height), &feemarkettypes.QueryBaseFeeRequest{})
|
|
|
|
if err == nil && res.BaseFee != nil {
|
|
|
|
return res.BaseFee.BigInt(), nil
|
2021-10-06 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 08:59:26 +00:00
|
|
|
return nil, nil
|
2021-09-27 22:06:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 14:16:57 +00:00
|
|
|
// GetEthereumMsgsFromTendermintBlock returns all real MsgEthereumTxs from a Tendermint block.
|
|
|
|
// It also ensures consistency over the correct txs indexes across RPC endpoints
|
2021-12-16 22:30:22 +00:00
|
|
|
func (e *EVMBackend) GetEthereumMsgsFromTendermintBlock(block *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) []*evmtypes.MsgEthereumTx {
|
2021-11-16 14:16:57 +00:00
|
|
|
var result []*evmtypes.MsgEthereumTx
|
|
|
|
|
2021-12-16 22:30:22 +00:00
|
|
|
txResults := blockRes.TxsResults
|
|
|
|
|
|
|
|
for i, tx := range block.Block.Txs {
|
|
|
|
// check tx exists on EVM by cross checking with blockResults
|
|
|
|
if txResults[i].Code != 0 {
|
|
|
|
e.logger.Debug("invalid tx result code", "cosmos-hash", hexutil.Encode(tx.Hash()))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-16 14:16:57 +00:00
|
|
|
tx, err := e.clientCtx.TxConfig.TxDecoder()(tx)
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Debug("failed to decode transaction in block", "height", block.Block.Height, "error", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
for _, msg := range tx.GetMsgs() {
|
|
|
|
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-12-16 22:30:22 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
result = append(result, ethMsg)
|
|
|
|
}
|
2021-11-16 14:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|