2021-06-23 06:38:05 +00:00
|
|
|
package backend
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-15 09:45:03 +00:00
|
|
|
"bytes"
|
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
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
2021-08-10 10:04:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2022-05-02 06:26:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
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"
|
2021-08-10 10:04:16 +00:00
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2021-07-09 08:34:49 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-05-02 06:26:24 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-07-22 15:06:44 +00:00
|
|
|
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/rpc/types"
|
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
|
|
|
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
|
2021-04-18 16:39:15 +00:00
|
|
|
)
|
|
|
|
|
2021-09-28 06:03:08 +00:00
|
|
|
var bAttributeKeyEthereumBloom = []byte(evmtypes.AttributeKeyEthereumBloom)
|
|
|
|
|
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.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) 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
|
2022-05-02 06:26:24 +00:00
|
|
|
_, err := b.queryClient.Params(b.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.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (map[string]interface{}, error) {
|
|
|
|
resBlock, err := b.GetTendermintBlockByNumber(blockNum)
|
2021-09-15 08:46:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
blockRes, err := b.GetTendermintBlockResultByNumber(&resBlock.Block.Height)
|
|
|
|
if err != nil {
|
|
|
|
b.logger.Debug("failed to fetch block result from Tendermint", "height", blockNum, "error", err.Error())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := b.EthBlockFromTendermint(resBlock, blockRes, fullTx)
|
2021-09-15 08:46:01 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("EthBlockFromTendermint failed", "height", blockNum, "error", err.Error())
|
2021-09-15 08:46:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByHash returns the block identified by hash.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error) {
|
2022-05-23 16:36:31 +00:00
|
|
|
resBlock, err := b.GetTendermintBlockByHash(hash)
|
2021-09-15 08:46:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-22 11:26:20 +00:00
|
|
|
|
2022-05-23 16:36:31 +00:00
|
|
|
if resBlock == nil {
|
|
|
|
// block not found
|
2021-09-15 08:46:01 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
blockRes, err := b.GetTendermintBlockResultByNumber(&resBlock.Block.Height)
|
|
|
|
if err != nil {
|
|
|
|
b.logger.Debug("failed to fetch block result from Tendermint", "block-hash", hash.String(), "error", err.Error())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
res, err := b.EthBlockFromTendermint(resBlock, blockRes, fullTx)
|
|
|
|
if err != nil {
|
|
|
|
b.logger.Debug("EthBlockFromTendermint failed", "hash", hash, "error", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2021-09-15 08:46:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 15:01:04 +00:00
|
|
|
// BlockByNumber returns the block identified by number.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) BlockByNumber(blockNum types.BlockNumber) (*ethtypes.Block, error) {
|
2022-05-23 16:36:31 +00:00
|
|
|
resBlock, err := b.GetTendermintBlockByNumber(blockNum)
|
2021-10-25 15:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-23 16:36:31 +00:00
|
|
|
if resBlock == nil {
|
|
|
|
// block not found
|
2022-06-22 11:26:20 +00:00
|
|
|
return nil, fmt.Errorf("block not found for height %d", blockNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
blockRes, err := b.GetTendermintBlockResultByNumber(&resBlock.Block.Height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("block result not found for height %d", resBlock.Block.Height)
|
2021-10-25 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
return b.EthBlockFromTm(resBlock, blockRes)
|
2021-10-25 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockByHash returns the block identified by hash.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) BlockByHash(hash common.Hash) (*ethtypes.Block, error) {
|
2022-05-23 16:36:31 +00:00
|
|
|
resBlock, err := b.GetTendermintBlockByHash(hash)
|
2021-10-25 15:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resBlock == nil || resBlock.Block == nil {
|
2022-06-22 11:26:20 +00:00
|
|
|
return nil, fmt.Errorf("block not found for hash %s", hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
blockRes, err := b.GetTendermintBlockResultByNumber(&resBlock.Block.Height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("block result not found for hash %s", hash)
|
2021-10-25 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
return b.EthBlockFromTm(resBlock, blockRes)
|
2021-10-25 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
func (b *Backend) EthBlockFromTm(resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) (*ethtypes.Block, error) {
|
2022-05-23 16:36:31 +00:00
|
|
|
block := resBlock.Block
|
2021-10-25 15:01:04 +00:00
|
|
|
height := block.Height
|
2022-06-22 11:26:20 +00:00
|
|
|
bloom, err := b.BlockBloom(blockRes)
|
2021-10-25 15:01:04 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("HeaderByNumber BlockBloom failed", "height", height)
|
2021-10-25 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
baseFee, err := b.BaseFee(blockRes)
|
2021-10-25 15:01:04 +00:00
|
|
|
if err != nil {
|
2022-06-22 11:26:20 +00:00
|
|
|
// handle error for pruned node and log
|
|
|
|
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", height, "error", err)
|
2021-10-25 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
ethHeader := types.EthHeaderFromTendermint(block.Header, bloom, baseFee)
|
2021-10-25 15:01:04 +00:00
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
resBlockResult, err := b.GetTendermintBlockResultByNumber(&block.Height)
|
2022-05-23 16:36:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-25 15:01:04 +00:00
|
|
|
|
2022-05-23 16:36:31 +00:00
|
|
|
msgs := b.GetEthereumMsgsFromTendermintBlock(resBlock, resBlockResult)
|
2021-10-25 15:01:04 +00:00
|
|
|
|
2022-05-23 16:36:31 +00:00
|
|
|
txs := make([]*ethtypes.Transaction, len(msgs))
|
|
|
|
for i, ethMsg := range msgs {
|
|
|
|
txs[i] = ethMsg.AsTransaction()
|
2021-10-25 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add tx receipts
|
|
|
|
ethBlock := ethtypes.NewBlock(ethHeader, txs, nil, nil, nil)
|
|
|
|
return ethBlock, nil
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
// GetTendermintBlockByNumber returns a Tendermint formatted block for a given
|
|
|
|
// block number
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetTendermintBlockByNumber(blockNum types.BlockNumber) (*tmrpctypes.ResultBlock, error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
height := blockNum.Int64()
|
2022-05-23 16:36:31 +00:00
|
|
|
if height <= 0 {
|
|
|
|
// fetch the latest block number from the app state, more accurate than the tendermint block store state.
|
|
|
|
n, err := b.BlockNumber()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
2022-05-23 16:36:31 +00:00
|
|
|
height = int64(n)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
2022-05-02 06:26:24 +00:00
|
|
|
resBlock, err := b.clientCtx.Client.Block(b.ctx, &height)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2022-05-23 16:36:31 +00:00
|
|
|
b.logger.Debug("tendermint client failed to get block", "height", height, "error", err.Error())
|
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:45:13 +00:00
|
|
|
if resBlock.Block == nil {
|
2022-05-23 16:36:31 +00:00
|
|
|
b.logger.Debug("GetTendermintBlockByNumber 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
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
// GetTendermintBlockResultByNumber returns a Tendermint-formatted block result by block number
|
|
|
|
func (b *Backend) GetTendermintBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error) {
|
|
|
|
return b.clientCtx.Client.BlockResults(b.ctx, height)
|
|
|
|
}
|
|
|
|
|
2021-11-12 08:50:38 +00:00
|
|
|
// GetTendermintBlockByHash returns a Tendermint format block by block number
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetTendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error) {
|
|
|
|
resBlock, err := b.clientCtx.Client.BlockByHash(b.ctx, blockHash.Bytes())
|
2021-11-12 08:50:38 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("tendermint client failed to get block", "blockHash", blockHash.Hex(), "error", err.Error())
|
2022-05-23 16:36:31 +00:00
|
|
|
return nil, err
|
2021-11-12 08:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resBlock == nil || resBlock.Block == nil {
|
2022-05-23 16:36:31 +00:00
|
|
|
b.logger.Debug("GetTendermintBlockByHash block not found", "blockHash", blockHash.Hex())
|
2021-11-12 08:50:38 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resBlock, nil
|
|
|
|
}
|
|
|
|
|
2021-09-15 09:45:03 +00:00
|
|
|
// BlockBloom query block bloom filter from block results
|
2022-06-22 11:26:20 +00:00
|
|
|
func (b *Backend) BlockBloom(blockRes *tmrpctypes.ResultBlockResults) (ethtypes.Bloom, error) {
|
|
|
|
for _, event := range blockRes.EndBlockEvents {
|
2021-09-15 09:45:03 +00:00
|
|
|
if event.Type != evmtypes.EventTypeBlockBloom {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attr := range event.Attributes {
|
2021-09-28 06:03:08 +00:00
|
|
|
if bytes.Equal(attr.Key, bAttributeKeyEthereumBloom) {
|
2021-09-15 09:45:03 +00:00
|
|
|
return ethtypes.BytesToBloom(attr.Value), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ethtypes.Bloom{}, errors.New("block bloom event is not found")
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
// EthBlockFromTendermint returns a JSON-RPC compatible Ethereum block from a
|
|
|
|
// given Tendermint block and its block result.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) EthBlockFromTendermint(
|
2022-05-23 16:36:31 +00:00
|
|
|
resBlock *tmrpctypes.ResultBlock,
|
2022-06-22 11:26:20 +00:00
|
|
|
blockRes *tmrpctypes.ResultBlockResults,
|
2021-04-18 16:39:15 +00:00
|
|
|
fullTx bool,
|
|
|
|
) (map[string]interface{}, error) {
|
2021-06-08 11:11:37 +00:00
|
|
|
ethRPCTxs := []interface{}{}
|
2022-05-23 16:36:31 +00:00
|
|
|
block := resBlock.Block
|
2021-10-06 11:22:32 +00:00
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
baseFee, err := b.BaseFee(blockRes)
|
2021-11-26 18:10:40 +00:00
|
|
|
if err != nil {
|
2022-06-22 11:26:20 +00:00
|
|
|
// handle the error for pruned node.
|
|
|
|
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", block.Height, "error", err)
|
2021-11-26 18:10:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
msgs := b.GetEthereumMsgsFromTendermintBlock(resBlock, blockRes)
|
2022-05-23 16:36:31 +00:00
|
|
|
for txIndex, ethMsg := range msgs {
|
|
|
|
if !fullTx {
|
|
|
|
hash := common.HexToHash(ethMsg.Hash)
|
|
|
|
ethRPCTxs = append(ethRPCTxs, hash)
|
2021-07-05 16:39:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:36:31 +00:00
|
|
|
tx := ethMsg.AsTransaction()
|
|
|
|
rpcTx, err := types.NewRPCTransaction(
|
|
|
|
tx,
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
uint64(block.Height),
|
|
|
|
uint64(txIndex),
|
|
|
|
baseFee,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
b.logger.Debug("NewTransactionFromData for receipt failed", "hash", tx.Hash().Hex(), "error", err.Error())
|
|
|
|
continue
|
2021-07-08 08:23:59 +00:00
|
|
|
}
|
2022-05-23 16:36:31 +00:00
|
|
|
ethRPCTxs = append(ethRPCTxs, rpcTx)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
bloom, err := b.BlockBloom(blockRes)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.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(),
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
var validatorAccAddr sdk.AccAddress
|
|
|
|
|
2022-05-23 16:36:31 +00:00
|
|
|
ctx := types.ContextWithHeight(block.Height)
|
2022-05-02 06:26:24 +00:00
|
|
|
res, err := b.queryClient.ValidatorAccount(ctx, req)
|
2021-07-14 14:40:32 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug(
|
2021-09-27 22:06:27 +00:00
|
|
|
"failed to query validator operator address",
|
|
|
|
"height", block.Height,
|
|
|
|
"cons-address", req.ConsAddress,
|
|
|
|
"error", err.Error(),
|
|
|
|
)
|
2022-06-22 11:26:20 +00:00
|
|
|
// use zero address as the validator operator address
|
|
|
|
validatorAccAddr = sdk.AccAddress(common.Address{}.Bytes())
|
|
|
|
} else {
|
|
|
|
validatorAccAddr, err = sdk.AccAddressFromBech32(res.AccountAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-14 14:40:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
validatorAddr := common.BytesToAddress(validatorAccAddr)
|
2021-07-14 14:40:32 +00:00
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
gasLimit, err := types.BlockMaxGasFromConsensusParams(ctx, b.clientCtx, block.Height)
|
2021-07-19 01:52:44 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Error("failed to query consensus params", "error", err.Error())
|
2021-07-19 01:52:44 +00:00
|
|
|
}
|
2021-09-14 10:27:20 +00:00
|
|
|
|
|
|
|
gasUsed := uint64(0)
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
for _, txsResult := range blockRes.TxsResults {
|
2022-01-05 07:26:59 +00:00
|
|
|
// workaround for cosmos-sdk bug. https://github.com/cosmos/cosmos-sdk/issues/10832
|
2022-05-31 10:26:40 +00:00
|
|
|
if ShouldIgnoreGasUsed(txsResult) {
|
|
|
|
// block gas limit has exceeded, other txs must have failed with same reason.
|
2022-01-05 07:26:59 +00:00
|
|
|
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
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) CurrentHeader() *ethtypes.Header {
|
|
|
|
header, _ := b.HeaderByNumber(types.EthLatestBlockNumber)
|
2021-09-27 22:06:27 +00:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
// HeaderByNumber returns the block header identified by height.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error) {
|
2022-05-23 16:36:31 +00:00
|
|
|
resBlock, err := b.GetTendermintBlockByNumber(blockNum)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:36:31 +00:00
|
|
|
if resBlock == nil {
|
|
|
|
return nil, errors.Errorf("block not found for height %d", blockNum)
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
blockRes, err := b.GetTendermintBlockResultByNumber(&resBlock.Block.Height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("block result not found for height %d", resBlock.Block.Height)
|
|
|
|
}
|
|
|
|
|
|
|
|
bloom, err := b.BlockBloom(blockRes)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("HeaderByNumber BlockBloom failed", "height", resBlock.Block.Height)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
baseFee, err := b.BaseFee(blockRes)
|
2021-10-06 11:22:32 +00:00
|
|
|
if err != nil {
|
2022-06-22 11:26:20 +00:00
|
|
|
// handle the error for pruned node.
|
|
|
|
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", resBlock.Block.Height, "error", err)
|
2021-10-06 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-11 15:04:45 +00:00
|
|
|
// GetBlockNumberByHash returns the block height of given block hash
|
|
|
|
func (b *Backend) GetBlockNumberByHash(blockHash common.Hash) (*big.Int, error) {
|
|
|
|
resBlock, err := b.GetTendermintBlockByHash(blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resBlock == nil {
|
|
|
|
return nil, errors.Errorf("block not found for hash %s", blockHash.Hex())
|
|
|
|
}
|
|
|
|
return big.NewInt(resBlock.Block.Height), nil
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
// HeaderByHash returns the block header identified by hash.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) {
|
2022-05-23 16:36:31 +00:00
|
|
|
resBlock, err := b.GetTendermintBlockByHash(blockHash)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-23 16:36:31 +00:00
|
|
|
if resBlock == nil {
|
2021-08-25 01:21:57 +00:00
|
|
|
return nil, errors.Errorf("block not found for hash %s", blockHash.Hex())
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
blockRes, err := b.GetTendermintBlockResultByNumber(&resBlock.Block.Height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("block result not found for height %d", resBlock.Block.Height)
|
|
|
|
}
|
|
|
|
|
|
|
|
bloom, err := b.BlockBloom(blockRes)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("HeaderByHash BlockBloom failed", "height", resBlock.Block.Height)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
baseFee, err := b.BaseFee(blockRes)
|
2021-10-06 11:22:32 +00:00
|
|
|
if err != nil {
|
2022-06-22 11:26:20 +00:00
|
|
|
// handle the error for pruned node.
|
|
|
|
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", resBlock.Block.Height, "error", err)
|
2021-10-06 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) PendingTransactions() ([]*sdk.Tx, error) {
|
|
|
|
res, err := b.clientCtx.Client.UnconfirmedTxs(b.ctx, nil)
|
2021-07-09 08:34:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*sdk.Tx, 0, len(res.Txs))
|
|
|
|
for _, txBz := range res.Txs {
|
2022-05-02 06:26:24 +00:00
|
|
|
tx, err := b.clientCtx.TxConfig.TxDecoder()(txBz)
|
2021-07-09 08:34:49 +00:00
|
|
|
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.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) 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.
|
2022-06-22 11:26:20 +00:00
|
|
|
blockRes, err := b.GetTendermintBlockResultByNumber(height)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
return GetLogsFromBlockResults(blockRes)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 09:45:03 +00:00
|
|
|
// GetLogs returns all the logs from all the ethereum transactions in a block.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetLogs(hash common.Hash) ([][]*ethtypes.Log, error) {
|
2022-05-23 16:36:31 +00:00
|
|
|
resBlock, err := b.GetTendermintBlockByHash(hash)
|
2021-09-15 09:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-23 16:36:31 +00:00
|
|
|
if resBlock == nil {
|
|
|
|
return nil, errors.Errorf("block not found for hash %s", hash)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 16:36:31 +00:00
|
|
|
return b.GetLogsByHeight(&resBlock.Block.Header.Height)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BloomStatus returns the BloomBitsBlocks and the number of processed sections maintained
|
|
|
|
// by the chain indexer.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) BloomStatus() (uint64, uint64) {
|
2021-04-18 16:39:15 +00:00
|
|
|
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).
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetCoinbase() (sdk.AccAddress, error) {
|
|
|
|
node, err := b.clientCtx.GetNode()
|
2021-08-04 09:18:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
status, err := node.Status(b.ctx)
|
2021-08-04 09:18:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &evmtypes.QueryValidatorAccountRequest{
|
|
|
|
ConsAddress: sdk.ConsAddress(status.ValidatorInfo.Address).String(),
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
res, err := b.queryClient.ValidatorAccount(b.ctx, req)
|
2021-08-04 09:18:22 +00:00
|
|
|
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
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetTransactionByHash(txHash common.Hash) (*types.RPCTransaction, error) {
|
|
|
|
res, err := b.GetTxByEthHash(txHash)
|
2021-11-16 14:16:57 +00:00
|
|
|
hexTx := txHash.Hex()
|
2022-05-31 10:26:40 +00:00
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
if err != nil {
|
|
|
|
// try to find tx in mempool
|
2022-05-02 06:26:24 +00:00
|
|
|
txs, err := b.PendingTransactions()
|
2021-09-04 20:33:06 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.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),
|
2022-05-19 06:48:37 +00:00
|
|
|
nil,
|
2021-09-04 20:33:06 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rpctx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("tx not found", "hash", hexTx)
|
2021-09-04 20:33:06 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 10:26:40 +00:00
|
|
|
if !TxSuccessOrExceedsBlockGasLimit(&res.TxResult) {
|
2021-12-16 22:30:22 +00:00
|
|
|
return nil, errors.New("invalid ethereum tx")
|
|
|
|
}
|
|
|
|
|
2022-05-31 10:26:40 +00:00
|
|
|
parsedTxs, err := types.ParseTxResult(&res.TxResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse tx events: %s", hexTx)
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedTx := parsedTxs.GetTxByHash(txHash)
|
|
|
|
if parsedTx == nil {
|
2022-01-14 09:37:33 +00:00
|
|
|
return nil, fmt.Errorf("ethereum tx not found in msgs: %s", hexTx)
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
tx, err := b.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.
|
2022-05-31 10:26:40 +00:00
|
|
|
msg, ok := tx.GetMsgs()[parsedTx.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
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
block, err := b.clientCtx.Client.Block(b.ctx, &res.Height)
|
2021-12-16 22:30:22 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("block not found", "height", res.Height, "error", err.Error())
|
2021-12-16 22:30:22 +00:00
|
|
|
return nil, err
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
blockRes, err := b.GetTendermintBlockResultByNumber(&block.Block.Height)
|
|
|
|
if err != nil {
|
|
|
|
b.logger.Debug("block result not found", "height", block.Block.Height, "error", err.Error())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 10:26:40 +00:00
|
|
|
if parsedTx.EthTxIndex == -1 {
|
2021-12-16 22:30:22 +00:00
|
|
|
// Fallback to find tx index by iterating all valid eth transactions
|
2022-05-02 06:26:24 +00:00
|
|
|
msgs := b.GetEthereumMsgsFromTendermintBlock(block, blockRes)
|
2021-12-16 22:30:22 +00:00
|
|
|
for i := range msgs {
|
|
|
|
if msgs[i].Hash == hexTx {
|
2022-05-31 10:26:40 +00:00
|
|
|
parsedTx.EthTxIndex = int64(i)
|
2021-12-16 22:30:22 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 10:26:40 +00:00
|
|
|
if parsedTx.EthTxIndex == -1 {
|
2021-12-16 22:30:22 +00:00
|
|
|
return nil, errors.New("can't find index of ethereum tx")
|
|
|
|
}
|
2021-11-16 14:16:57 +00:00
|
|
|
|
2022-06-22 11:26:20 +00:00
|
|
|
baseFee, err := b.BaseFee(blockRes)
|
2022-05-19 06:48:37 +00:00
|
|
|
if err != nil {
|
2022-06-22 11:26:20 +00:00
|
|
|
// handle the error for pruned node.
|
|
|
|
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", blockRes.Height, "error", err)
|
2022-05-19 06:48:37 +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),
|
2022-05-31 10:26:40 +00:00
|
|
|
uint64(parsedTx.EthTxIndex),
|
2022-05-19 06:48:37 +00:00
|
|
|
baseFee,
|
2021-09-04 20:33:06 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetTxByEthHash(hash common.Hash) (*tmrpctypes.ResultTx, error) {
|
2021-09-04 20:33:06 +00:00
|
|
|
query := fmt.Sprintf("%s.%s='%s'", evmtypes.TypeMsgEthereumTx, evmtypes.AttributeKeyEthereumTxHash, hash.Hex())
|
2022-05-02 06:26:24 +00:00
|
|
|
resTxs, err := b.clientCtx.Client.TxSearch(b.ctx, query, false, nil, nil, "")
|
2021-09-04 20:33:06 +00:00
|
|
|
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
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetTxByTxIndex(height int64, index uint) (*tmrpctypes.ResultTx, error) {
|
2021-12-16 22:30:22 +00:00
|
|
|
query := fmt.Sprintf("tx.height=%d AND %s.%s=%d",
|
|
|
|
height, evmtypes.TypeMsgEthereumTx,
|
|
|
|
evmtypes.AttributeKeyTxIndex, index,
|
|
|
|
)
|
2022-05-02 06:26:24 +00:00
|
|
|
resTxs, err := b.clientCtx.Client.TxSearch(b.ctx, query, false, nil, nil, "")
|
2021-12-16 22:30:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error) {
|
2021-08-10 10:04:16 +00:00
|
|
|
// Look up the wallet containing the requested signer
|
2022-05-02 06:26:24 +00:00
|
|
|
_, err := b.clientCtx.Keyring.KeyByAddress(sdk.AccAddress(args.From.Bytes()))
|
2021-08-10 10:04:16 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Error("failed to find key in keyring", "address", args.From, "error", err.Error())
|
2021-08-10 10:04:16 +00:00
|
|
|
return common.Hash{}, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error())
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
args, err = b.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 {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("tx failed basic validation", "error", err.Error())
|
2021-08-10 10:04:16 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
bn, err := b.BlockNumber()
|
2021-10-06 11:22:32 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("failed to fetch latest block number", "error", err.Error())
|
2021-10-06 11:22:32 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
signer := ethtypes.MakeSigner(b.ChainConfig(), new(big.Int).SetUint64(uint64(bn)))
|
2021-08-10 10:04:16 +00:00
|
|
|
|
|
|
|
// Sign transaction
|
2022-05-02 06:26:24 +00:00
|
|
|
if err := msg.Sign(signer, b.clientCtx.Keyring); err != nil {
|
|
|
|
b.logger.Debug("failed to sign tx", "error", err.Error())
|
2021-08-10 10:04:16 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query params to use the EVM denomination
|
2022-05-02 06:26:24 +00:00
|
|
|
res, err := b.queryClient.QueryClient.Params(b.ctx, &evmtypes.QueryParamsRequest{})
|
2021-08-10 10:04:16 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Error("failed to query evm params", "error", err.Error())
|
2021-08-10 10:04:16 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:44:41 +00:00
|
|
|
// Assemble transaction from fields
|
2022-05-02 06:26:24 +00:00
|
|
|
tx, err := msg.BuildTx(b.clientCtx.TxConfig.NewTxBuilder(), res.Params.EvmDenom)
|
2021-08-10 10:04:16 +00:00
|
|
|
if err != nil {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.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
|
2022-05-02 06:26:24 +00:00
|
|
|
txEncoder := b.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 {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Error("failed to encode eth tx using default encoder", "error", err.Error())
|
2021-08-10 10:04:16 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:50:36 +00:00
|
|
|
ethTx := msg.AsTransaction()
|
|
|
|
|
|
|
|
// check the local node config in case unprotected txs are disabled
|
|
|
|
if !b.UnprotectedAllowed() && !ethTx.Protected() {
|
|
|
|
// Ensure only eip155 signed transactions are submitted if EIP155Required is set.
|
|
|
|
return common.Hash{}, errors.New("only replay-protected (EIP-155) transactions allowed over RPC")
|
|
|
|
}
|
|
|
|
|
|
|
|
txHash := ethTx.Hash()
|
2021-08-10 10:04:16 +00:00
|
|
|
|
|
|
|
// Broadcast transaction in sync mode (default)
|
|
|
|
// NOTE: If error is encountered on the node, the broadcast will not return an error
|
2022-05-02 06:26:24 +00:00
|
|
|
syncCtx := b.clientCtx.WithBroadcastMode(flags.BroadcastSync)
|
2021-08-10 10:04:16 +00:00
|
|
|
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 {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Error("failed to broadcast tx", "error", err.Error())
|
2021-08-10 10:04:16 +00:00
|
|
|
return txHash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return transaction hash
|
|
|
|
return txHash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateGas returns an estimate of gas usage for the given smart contract call.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) 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,
|
2022-05-02 06:26:24 +00:00
|
|
|
GasCap: b.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.
|
2022-05-02 06:26:24 +00:00
|
|
|
res, err := b.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.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) GetTransactionCount(address common.Address, blockNum types.BlockNumber) (*hexutil.Uint64, error) {
|
2021-08-10 10:04:16 +00:00
|
|
|
// Get nonce (sequence) from account
|
|
|
|
from := sdk.AccAddress(address.Bytes())
|
2022-05-02 06:26:24 +00:00
|
|
|
accRet := b.clientCtx.AccountRetriever
|
2021-08-10 10:04:16 +00:00
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
err := accRet.EnsureExists(b.clientCtx, from)
|
2021-08-10 10:04:16 +00:00
|
|
|
if err != nil {
|
|
|
|
// account doesn't exist yet, return 0
|
|
|
|
n := hexutil.Uint64(0)
|
|
|
|
return &n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
includePending := blockNum == types.EthPendingBlockNumber
|
2022-05-02 06:26:24 +00:00
|
|
|
nonce, err := b.getAccountNonce(address, includePending, blockNum.Int64(), b.logger)
|
2021-08-10 10:04:16 +00:00
|
|
|
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.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) RPCGasCap() uint64 {
|
|
|
|
return b.cfg.JSONRPC.GasCap
|
2021-08-18 14:11:51 +00:00
|
|
|
}
|
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.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) RPCEVMTimeout() time.Duration {
|
|
|
|
return b.cfg.JSONRPC.EVMTimeout
|
2021-10-25 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 19:14:39 +00:00
|
|
|
// RPCGasCap is the global gas cap for eth-call variants.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) RPCTxFeeCap() float64 {
|
|
|
|
return b.cfg.JSONRPC.TxFeeCap
|
2021-10-20 19:14:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 11:03:49 +00:00
|
|
|
// RPCFilterCap is the limit for total number of filters that can be created
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) RPCFilterCap() int32 {
|
|
|
|
return b.cfg.JSONRPC.FilterCap
|
2021-10-13 11:03:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 11:58:52 +00:00
|
|
|
// RPCFeeHistoryCap is the limit for total number of blocks that can be fetched
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) RPCFeeHistoryCap() int32 {
|
|
|
|
return b.cfg.JSONRPC.FeeHistoryCap
|
2021-11-17 11:58:52 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 21:47:25 +00:00
|
|
|
// RPCLogsCap defines the max number of results can be returned from single `eth_getLogs` query.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) RPCLogsCap() int32 {
|
|
|
|
return b.cfg.JSONRPC.LogsCap
|
2021-12-29 21:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RPCBlockRangeCap defines the max block range allowed for `eth_getLogs` query.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) RPCBlockRangeCap() int32 {
|
|
|
|
return b.cfg.JSONRPC.BlockRangeCap
|
2021-12-29 21:47:25 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) RPCMinGasPrice() int64 {
|
|
|
|
evmParams, err := b.queryClient.Params(b.ctx, &evmtypes.QueryParamsRequest{})
|
2021-09-28 08:42:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return ethermint.DefaultGasPrice
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
minGasPrice := b.cfg.GetMinGasPrices()
|
2021-09-28 08:42:18 +00:00
|
|
|
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
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
// ChainConfig returns the latest ethereum chain configuration
|
|
|
|
func (b *Backend) ChainConfig() *params.ChainConfig {
|
|
|
|
params, err := b.queryClient.Params(b.ctx, &evmtypes.QueryParamsRequest{})
|
2021-09-27 22:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
return params.Params.ChainConfig.EthereumConfig(b.chainID)
|
2021-09-27 22:06:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-05-02 06:26:24 +00:00
|
|
|
func (b *Backend) SuggestGasTipCap(baseFee *big.Int) (*big.Int, error) {
|
2022-03-03 20:50:40 +00:00
|
|
|
if baseFee == nil {
|
2022-04-26 14:24:32 +00:00
|
|
|
// london hardfork not enabled or feemarket not enabled
|
2022-03-03 20:50:40 +00:00
|
|
|
return big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
params, err := b.queryClient.FeeMarket.Params(b.ctx, &feemarkettypes.QueryParamsRequest{})
|
2022-03-03 20:50:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-26 14:24:32 +00:00
|
|
|
// BaseFee returns the base fee tracked by the Fee Market module.
|
|
|
|
// If the base fee is not enabled globally, the query returns nil.
|
|
|
|
// If the London hard fork is not activated at the current height, the query will
|
|
|
|
// return nil.
|
2022-06-22 11:26:20 +00:00
|
|
|
func (b *Backend) BaseFee(blockRes *tmrpctypes.ResultBlockResults) (*big.Int, error) {
|
2022-04-30 16:11:28 +00:00
|
|
|
// return BaseFee if London hard fork is activated and feemarket is enabled
|
2022-06-22 11:26:20 +00:00
|
|
|
res, err := b.queryClient.BaseFee(types.ContextWithHeight(blockRes.Height), &evmtypes.QueryBaseFeeRequest{})
|
2022-08-03 07:46:52 +00:00
|
|
|
if err != nil || res.BaseFee == nil {
|
|
|
|
// we can't tell if it's london HF not enabled or the state is pruned,
|
|
|
|
// in either case, we'll fallback to parsing from begin blocker event,
|
2022-06-22 11:26:20 +00:00
|
|
|
// faster to iterate reversely
|
|
|
|
for i := len(blockRes.BeginBlockEvents) - 1; i >= 0; i-- {
|
|
|
|
evt := blockRes.BeginBlockEvents[i]
|
|
|
|
if evt.Type == feemarkettypes.EventTypeFeeMarket && len(evt.Attributes) > 0 {
|
|
|
|
baseFee, err := strconv.ParseInt(string(evt.Attributes[0].Value), 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
return big.NewInt(baseFee), nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 11:22:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-26 14:24:32 +00:00
|
|
|
if res.BaseFee == nil {
|
|
|
|
return nil, nil
|
2021-10-06 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 14:24:32 +00:00
|
|
|
return res.BaseFee.BigInt(), nil
|
2021-09-27 22:06:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 13:31:57 +00:00
|
|
|
// GlobalMinGasPrice returns MinGasPrice param from FeeMarket
|
|
|
|
func (b *Backend) GlobalMinGasPrice() (sdk.Dec, error) {
|
|
|
|
res, err := b.queryClient.FeeMarket.Params(b.ctx, &feemarkettypes.QueryParamsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return sdk.ZeroDec(), err
|
|
|
|
}
|
|
|
|
return res.Params.MinGasPrice, nil
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
// FeeHistory returns data relevant for fee estimation based on the specified range of blocks.
|
|
|
|
func (b *Backend) FeeHistory(
|
|
|
|
userBlockCount rpc.DecimalOrHex, // number blocks to fetch, maximum is 100
|
|
|
|
lastBlock rpc.BlockNumber, // the block to start search , to oldest
|
|
|
|
rewardPercentiles []float64, // percentiles to fetch reward
|
|
|
|
) (*types.FeeHistoryResult, error) {
|
|
|
|
blockEnd := int64(lastBlock)
|
|
|
|
|
|
|
|
if blockEnd <= 0 {
|
|
|
|
blockNumber, err := b.BlockNumber()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blockEnd = int64(blockNumber)
|
|
|
|
}
|
|
|
|
userBlockCountInt := int64(userBlockCount)
|
|
|
|
maxBlockCount := int64(b.cfg.JSONRPC.FeeHistoryCap)
|
|
|
|
if userBlockCountInt > maxBlockCount {
|
|
|
|
return nil, fmt.Errorf("FeeHistory user block count %d higher than %d", userBlockCountInt, maxBlockCount)
|
|
|
|
}
|
|
|
|
blockStart := blockEnd - userBlockCountInt
|
|
|
|
if blockStart < 0 {
|
|
|
|
blockStart = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
blockCount := blockEnd - blockStart
|
|
|
|
|
|
|
|
oldestBlock := (*hexutil.Big)(big.NewInt(blockStart))
|
|
|
|
|
|
|
|
// prepare space
|
|
|
|
reward := make([][]*hexutil.Big, blockCount)
|
|
|
|
rewardCount := len(rewardPercentiles)
|
|
|
|
for i := 0; i < int(blockCount); i++ {
|
|
|
|
reward[i] = make([]*hexutil.Big, rewardCount)
|
|
|
|
}
|
|
|
|
thisBaseFee := make([]*hexutil.Big, blockCount)
|
|
|
|
thisGasUsedRatio := make([]float64, blockCount)
|
|
|
|
|
|
|
|
// rewards should only be calculated if reward percentiles were included
|
|
|
|
calculateRewards := rewardCount != 0
|
|
|
|
|
|
|
|
// fetch block
|
|
|
|
for blockID := blockStart; blockID < blockEnd; blockID++ {
|
|
|
|
index := int32(blockID - blockStart)
|
|
|
|
// eth block
|
|
|
|
ethBlock, err := b.GetBlockByNumber(types.BlockNumber(blockID), true)
|
|
|
|
if ethBlock == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// tendermint block
|
|
|
|
tendermintblock, err := b.GetTendermintBlockByNumber(types.BlockNumber(blockID))
|
|
|
|
if tendermintblock == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// tendermint block result
|
2022-06-22 11:26:20 +00:00
|
|
|
tendermintBlockResult, err := b.GetTendermintBlockResultByNumber(&tendermintblock.Block.Height)
|
2022-05-02 06:26:24 +00:00
|
|
|
if tendermintBlockResult == nil {
|
|
|
|
b.logger.Debug("block result not found", "height", tendermintblock.Block.Height, "error", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
oneFeeHistory := types.OneFeeHistory{}
|
|
|
|
err = b.processBlock(tendermintblock, ðBlock, rewardPercentiles, tendermintBlockResult, &oneFeeHistory)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy
|
|
|
|
thisBaseFee[index] = (*hexutil.Big)(oneFeeHistory.BaseFee)
|
|
|
|
thisGasUsedRatio[index] = oneFeeHistory.GasUsedRatio
|
|
|
|
if calculateRewards {
|
|
|
|
for j := 0; j < rewardCount; j++ {
|
|
|
|
reward[index][j] = (*hexutil.Big)(oneFeeHistory.Reward[j])
|
|
|
|
if reward[index][j] == nil {
|
|
|
|
reward[index][j] = (*hexutil.Big)(big.NewInt(0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
feeHistory := types.FeeHistoryResult{
|
|
|
|
OldestBlock: oldestBlock,
|
|
|
|
BaseFee: thisBaseFee,
|
|
|
|
GasUsedRatio: thisGasUsedRatio,
|
|
|
|
}
|
|
|
|
|
|
|
|
if calculateRewards {
|
|
|
|
feeHistory.Reward = reward
|
|
|
|
}
|
|
|
|
|
|
|
|
return &feeHistory, nil
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
// GetEthereumMsgsFromTendermintBlock returns all real MsgEthereumTxs from a
|
|
|
|
// Tendermint block. It also ensures consistency over the correct txs indexes
|
|
|
|
// across RPC endpoints
|
|
|
|
func (b *Backend) GetEthereumMsgsFromTendermintBlock(
|
|
|
|
resBlock *tmrpctypes.ResultBlock,
|
|
|
|
blockRes *tmrpctypes.ResultBlockResults,
|
|
|
|
) []*evmtypes.MsgEthereumTx {
|
2021-11-16 14:16:57 +00:00
|
|
|
var result []*evmtypes.MsgEthereumTx
|
2022-05-31 10:26:40 +00:00
|
|
|
block := resBlock.Block
|
2021-11-16 14:16:57 +00:00
|
|
|
|
2021-12-16 22:30:22 +00:00
|
|
|
txResults := blockRes.TxsResults
|
|
|
|
|
2022-05-31 10:26:40 +00:00
|
|
|
for i, tx := range block.Txs {
|
2022-07-29 10:35:07 +00:00
|
|
|
// Check if tx exists on EVM by cross checking with blockResults:
|
|
|
|
// - Include unsuccessful tx that exceeds block gas limit
|
|
|
|
// - Exclude unsuccessful tx with any other error but ExceedBlockGasLimit
|
2022-05-31 10:26:40 +00:00
|
|
|
if !TxSuccessOrExceedsBlockGasLimit(txResults[i]) {
|
2022-05-02 06:26:24 +00:00
|
|
|
b.logger.Debug("invalid tx result code", "cosmos-hash", hexutil.Encode(tx.Hash()))
|
2021-12-16 22:30:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
tx, err := b.clientCtx.TxConfig.TxDecoder()(tx)
|
2021-11-16 14:16:57 +00:00
|
|
|
if err != nil {
|
2022-05-31 10:26:40 +00:00
|
|
|
b.logger.Debug("failed to decode transaction in block", "height", block.Height, "error", err.Error())
|
2021-11-16 14:16:57 +00:00
|
|
|
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-07-19 15:12:48 +00:00
|
|
|
ethMsg.Hash = ethMsg.AsTransaction().Hash().Hex()
|
2022-01-14 09:37:33 +00:00
|
|
|
result = append(result, ethMsg)
|
|
|
|
}
|
2021-11-16 14:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2022-06-22 10:50:36 +00:00
|
|
|
|
|
|
|
// UnprotectedAllowed returns the node configuration value for allowing
|
|
|
|
// unprotected transactions (i.e not replay-protected)
|
|
|
|
func (b Backend) UnprotectedAllowed() bool {
|
|
|
|
return b.allowUnprotectedTxs
|
|
|
|
}
|