Skip to content

Commit

Permalink
apply 1f0432f1 (eth header by hash)
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Nov 11, 2024
1 parent bf18711 commit b8c1774
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 93 deletions.
21 changes: 10 additions & 11 deletions rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package backend

import (
"context"
"fmt"
tmrpcclient "github.com/cometbft/cometbft/rpc/client"
"math/big"
"time"

Expand All @@ -28,19 +30,9 @@ import (

// BackendI implements the Cosmos and EVM backend.
type BackendI interface { //nolint: revive
CosmosBackend
EVMBackend
}

// CosmosBackend implements the functionality shared within cosmos namespaces
// as defined by Wallet Connect V2: https://docs.walletconnect.com/2.0/json-rpc/cosmos.
// Implemented by Backend.
type CosmosBackend interface { // TODO: define
// GetAccounts()
// SignDirect()
// SignAmino()
}

// EVMBackend implements the functionality shared within ethereum namespaces
// as defined by EIP-1474: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md
// Implemented by Backend.
Expand Down Expand Up @@ -71,7 +63,6 @@ type EVMBackend interface {
GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint
GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumber) *hexutil.Uint
TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpctypes.ResultBlock, error)
TendermintBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error)
TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error)
BlockNumberFromTendermint(blockNrOrHash rpctypes.BlockNumberOrHash) (rpctypes.BlockNumber, error)
BlockNumberFromTendermintByHash(blockHash common.Hash) (*big.Int, error)
Expand Down Expand Up @@ -107,6 +98,7 @@ type EVMBackend interface {
GetTxByTxIndex(height int64, txIndex uint) (*evmostypes.TxResult, error)
GetTransactionByBlockAndIndex(block *tmrpctypes.ResultBlock, idx hexutil.Uint) (*rpctypes.RPCTransaction, error)
GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error)
GetTransactionLogs(hash common.Hash) ([]*ethtypes.Log, error)
GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) (*rpctypes.RPCTransaction, error)
GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockNumber, idx hexutil.Uint) (*rpctypes.RPCTransaction, error)

Expand Down Expand Up @@ -134,6 +126,7 @@ var _ BackendI = (*Backend)(nil)
type Backend struct {
ctx context.Context
clientCtx client.Context
rpcClient tmrpcclient.SignClient
queryClient *rpctypes.QueryClient // gRPC query client
logger log.Logger
chainID *big.Int
Expand All @@ -160,9 +153,15 @@ func NewBackend(
panic(err)
}

rpcClient, ok := clientCtx.Client.(tmrpcclient.SignClient)
if !ok {
panic(fmt.Sprintf("invalid rpc client, expected: tmrpcclient.SignClient, got: %T", clientCtx.Client))
}

return &Backend{
ctx: context.Background(),
clientCtx: clientCtx,
rpcClient: rpcClient,
queryClient: rpctypes.NewQueryClient(clientCtx),
logger: logger.With("module", "backend"),
chainID: chainID,
Expand Down
4 changes: 2 additions & 2 deletions rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func (suite *BackendTestSuite) SetupTest() {
WithTxConfig(encodingConfig.TxConfig).
WithKeyringDir(clientDir).
WithKeyring(keyRing).
WithAccountRetriever(client.TestAccountRetriever{Accounts: accounts})
WithAccountRetriever(client.TestAccountRetriever{Accounts: accounts}).
WithClient(mocks.NewClient(suite.T()))

allowUnprotectedTxs := false
idxer := indexer.NewKVIndexer(dbm.NewMemDB(), ctx.Logger, clientCtx)
Expand All @@ -86,7 +87,6 @@ func (suite *BackendTestSuite) SetupTest() {
suite.backend.cfg.JSONRPC.EVMTimeout = 0
suite.backend.cfg.JSONRPC.AllowInsecureUnlock = true
suite.backend.queryClient.QueryClient = mocks.NewEVMQueryClient(suite.T())
suite.backend.clientCtx.Client = mocks.NewClient(suite.T())
suite.backend.queryClient.FeeMarket = mocks.NewFeeMarketQueryClient(suite.T())
suite.backend.ctx = rpctypes.ContextWithHeight(1)

Expand Down
60 changes: 26 additions & 34 deletions rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"math/big"
"strconv"

tmrpcclient "github.com/cometbft/cometbft/rpc/client"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
Expand Down Expand Up @@ -66,7 +65,7 @@ func (b *Backend) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) (
return nil, nil
}

blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height)
blockRes, err := b.rpcClient.BlockResults(b.ctx, &resBlock.Block.Height)
if err != nil {
b.logger.Debug("failed to fetch block result from Tendermint", "height", blockNum, "error", err.Error())
return nil, nil
Expand Down Expand Up @@ -94,7 +93,7 @@ func (b *Backend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]inte
return nil, nil
}

blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height)
blockRes, err := b.rpcClient.BlockResults(b.ctx, &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
Expand All @@ -112,12 +111,7 @@ func (b *Backend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]inte
// GetBlockTransactionCountByHash returns the number of Ethereum transactions in
// the block identified by hash.
func (b *Backend) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint {
sc, ok := b.clientCtx.Client.(tmrpcclient.SignClient)
if !ok {
b.logger.Error("invalid rpc client")
}

block, err := sc.BlockByHash(b.ctx, hash.Bytes())
block, err := b.rpcClient.BlockByHash(b.ctx, hash.Bytes())
if err != nil {
b.logger.Debug("block not found", "hash", hash.Hex(), "error", err.Error())
return nil
Expand Down Expand Up @@ -151,7 +145,7 @@ func (b *Backend) GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumber
// GetBlockTransactionCount returns the number of Ethereum transactions in a
// given block.
func (b *Backend) GetBlockTransactionCount(block *tmrpctypes.ResultBlock) *hexutil.Uint {
blockRes, err := b.TendermintBlockResultByNumber(&block.Block.Height)
blockRes, err := b.rpcClient.BlockResults(b.ctx, &block.Block.Height)
if err != nil {
return nil
}
Expand All @@ -173,7 +167,7 @@ func (b *Backend) TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpc
}
height = int64(n) //#nosec G115 -- checked for int overflow already
}
resBlock, err := b.clientCtx.Client.Block(b.ctx, &height)
resBlock, err := b.rpcClient.Block(b.ctx, &height)
if err != nil {
b.logger.Debug("tendermint client failed to get block", "height", height, "error", err.Error())
return nil, err
Expand All @@ -190,20 +184,12 @@ func (b *Backend) TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpc
// TendermintBlockResultByNumber returns a Tendermint-formatted block result
// by block number
func (b *Backend) TendermintBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error) {
sc, ok := b.clientCtx.Client.(tmrpcclient.SignClient)
if !ok {
return nil, errors.New("invalid rpc client")
}
return sc.BlockResults(b.ctx, height)
return b.rpcClient.BlockResults(b.ctx, height)
}

// TendermintBlockByHash returns a Tendermint-formatted block by block number
func (b *Backend) TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error) {
sc, ok := b.clientCtx.Client.(tmrpcclient.SignClient)
if !ok {
return nil, errors.New("invalid rpc client")
}
resBlock, err := sc.BlockByHash(b.ctx, blockHash.Bytes())
resBlock, err := b.rpcClient.BlockByHash(b.ctx, blockHash.Bytes())
if err != nil {
b.logger.Debug("tendermint client failed to get block", "blockHash", blockHash.Hex(), "error", err.Error())
return nil, err
Expand Down Expand Up @@ -237,14 +223,16 @@ func (b *Backend) BlockNumberFromTendermint(blockNrOrHash rpctypes.BlockNumberOr

// BlockNumberFromTendermintByHash returns the block height of given block hash
func (b *Backend) BlockNumberFromTendermintByHash(blockHash common.Hash) (*big.Int, error) {
resBlock, err := b.TendermintBlockByHash(blockHash)
resBlock, err := b.rpcClient.HeaderByHash(b.ctx, blockHash.Bytes())
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

return big.NewInt(resBlock.Header.Height), nil
}

// EthMsgsFromTendermintBlock returns all real MsgEthereumTxs from a
Expand Down Expand Up @@ -300,9 +288,9 @@ func (b *Backend) HeaderByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Heade
return nil, errors.Errorf("block not found for height %d", blockNum)
}

blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height)
blockRes, err := b.rpcClient.BlockResults(b.ctx, &resBlock.Block.Height)
if err != nil {
return nil, fmt.Errorf("block result not found for height %d. %w", resBlock.Block.Height, err)
return nil, errors.Errorf("block result not found for height %d", resBlock.Block.Height)
}

bloom, err := b.BlockBloom(blockRes)
Expand All @@ -322,31 +310,34 @@ func (b *Backend) HeaderByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Heade

// HeaderByHash returns the block header identified by hash.
func (b *Backend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) {
resBlock, err := b.TendermintBlockByHash(blockHash)
resHeader, err := b.rpcClient.HeaderByHash(b.ctx, blockHash.Bytes())
if err != nil {
return nil, err
}
if resBlock == nil {
return nil, errors.Errorf("block not found for hash %s", blockHash.Hex())

if resHeader == nil {
return nil, errors.Errorf("header not found for hash %s", blockHash.Hex())
}

blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height)
height := resHeader.Header.Height

blockRes, err := b.rpcClient.BlockResults(b.ctx, &resHeader.Header.Height)
if err != nil {
return nil, errors.Errorf("block result not found for height %d", resBlock.Block.Height)
return nil, errors.Errorf("block result not found for height %d", height)
}

bloom, err := b.BlockBloom(blockRes)
if err != nil {
b.logger.Debug("HeaderByHash BlockBloom failed", "height", resBlock.Block.Height)
b.logger.Debug("HeaderByHash BlockBloom failed", "height", height)
}

baseFee, err := b.BaseFee(blockRes)
if err != nil {
// 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)
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", height, "error", err)
}

ethHeader := rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee)
ethHeader := rpctypes.EthHeaderFromTendermint(*resHeader.Header, bloom, baseFee)
return ethHeader, nil
}

Expand Down Expand Up @@ -469,12 +460,13 @@ func (b *Backend) EthBlockByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Blo
if err != nil {
return nil, err
}

if resBlock == nil {
// block not found
return nil, fmt.Errorf("block not found for height %d", blockNum)
}

blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height)
blockRes, err := b.rpcClient.BlockResults(b.ctx, &resBlock.Block.Height)
if err != nil {
return nil, fmt.Errorf("block result not found for height %d", resBlock.Block.Height)
}
Expand Down
Loading

0 comments on commit b8c1774

Please sign in to comment.