minor changes

This commit is contained in:
Federico Kunze 2021-04-19 12:49:55 +02:00
parent 036071272f
commit 4d849a6241
No known key found for this signature in database
GPG Key ID: 655F93A970080A30
4 changed files with 46 additions and 25 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/ethermint/ethereum/rpc/types" "github.com/cosmos/ethermint/ethereum/rpc/types"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client" rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
) )

View File

@ -43,7 +43,7 @@ type Backend interface {
var _ Backend = (*EVMBackend)(nil) var _ Backend = (*EVMBackend)(nil)
// implements the Backend interface // EVMBackend implements the Backend interface
type EVMBackend struct { type EVMBackend struct {
ctx context.Context ctx context.Context
clientCtx client.Context clientCtx client.Context
@ -51,6 +51,7 @@ type EVMBackend struct {
logger log.Logger logger log.Logger
} }
// NewEVMBackend creates a new EVMBackend instance
func NewEVMBackend(clientCtx client.Context) *EVMBackend { func NewEVMBackend(clientCtx client.Context) *EVMBackend {
return &EVMBackend{ return &EVMBackend{
ctx: context.Background(), ctx: context.Background(),
@ -105,7 +106,7 @@ func (e *EVMBackend) GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (
} }
} }
res, err := e.ethBlockFromTendermint(e.clientCtx, e.queryClient, resBlock.Block, fullTx) res, err := e.EthBlockFromTendermint(e.clientCtx, e.queryClient, resBlock.Block, fullTx)
if err != nil { if err != nil {
e.logger.WithError(err).Warningf("EthBlockFromTendermint failed with block %s", resBlock.Block.String()) e.logger.WithError(err).Warningf("EthBlockFromTendermint failed with block %s", resBlock.Block.String())
} }
@ -121,19 +122,22 @@ func (e *EVMBackend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]i
return nil, err return nil, err
} }
return e.ethBlockFromTendermint(e.clientCtx, e.queryClient, resBlock.Block, fullTx) return e.EthBlockFromTendermint(e.clientCtx, e.queryClient, resBlock.Block, fullTx)
} }
// ethBlockFromTendermint returns a JSON-RPC compatible Ethereum block from a given Tendermint block. // EthBlockFromTendermint returns a JSON-RPC compatible Ethereum block from a given Tendermint block.
func (e *EVMBackend) ethBlockFromTendermint( func (e *EVMBackend) EthBlockFromTendermint(
clientCtx client.Context, clientCtx client.Context,
queryClient evmtypes.QueryClient, queryClient evmtypes.QueryClient,
block *tmtypes.Block, block *tmtypes.Block,
fullTx bool, fullTx bool,
) (map[string]interface{}, error) { ) (map[string]interface{}, error) {
txReceiptsResp, err := queryClient.TxReceiptsByBlockHeight(types.ContextWithHeight(0), &evmtypes.QueryTxReceiptsByBlockHeightRequest{
req := &evmtypes.QueryTxReceiptsByBlockHeightRequest{
Height: block.Height, Height: block.Height,
}) }
txReceiptsResp, err := queryClient.TxReceiptsByBlockHeight(types.ContextWithHeight(0), req)
if err != nil { if err != nil {
e.logger.Warningf("TxReceiptsByBlockHeight fail: %s", err.Error()) e.logger.Warningf("TxReceiptsByBlockHeight fail: %s", err.Error())
return nil, err return nil, err
@ -142,29 +146,30 @@ func (e *EVMBackend) ethBlockFromTendermint(
gasUsed := big.NewInt(0) gasUsed := big.NewInt(0)
ethRPCTxs := make([]interface{}, 0, len(txReceiptsResp.Receipts)) ethRPCTxs := make([]interface{}, 0, len(txReceiptsResp.Receipts))
if !fullTx {
// simply hashes for _, receipt := range txReceiptsResp.Receipts {
for _, receipt := range txReceiptsResp.Receipts { hash := common.BytesToHash(receipt.Hash)
ethRPCTxs = append(ethRPCTxs, common.BytesToHash(receipt.Hash)) if fullTx {
} // full txs from receipts
} else {
// full txns from receipts
for _, receipt := range txReceiptsResp.Receipts {
tx, err := NewTransactionFromData( tx, err := NewTransactionFromData(
receipt.Data, receipt.Data,
common.BytesToAddress(receipt.From), common.BytesToAddress(receipt.From),
common.BytesToHash(receipt.Hash), hash,
common.BytesToHash(receipt.BlockHash), common.BytesToHash(receipt.BlockHash),
receipt.BlockHeight, receipt.BlockHeight,
receipt.Index, receipt.Index,
) )
if err != nil { if err != nil {
e.logger.Warningf("NewTransactionFromData fail: %s", err.Error()) e.logger.Warningf("NewTransactionFromData for receipt %s failed: %s", hash, err.Error())
continue continue
} }
ethRPCTxs = append(ethRPCTxs, tx) ethRPCTxs = append(ethRPCTxs, tx)
gasUsed.Add(gasUsed, new(big.Int).SetUint64(receipt.Result.GasUsed)) gasUsed.Add(gasUsed, new(big.Int).SetUint64(receipt.Result.GasUsed))
} else {
// simply hashes
ethRPCTxs = append(ethRPCTxs, hash)
} }
} }
@ -177,7 +182,7 @@ func (e *EVMBackend) ethBlockFromTendermint(
} }
bloom := ethtypes.BytesToBloom(blockBloomResp.Bloom) bloom := ethtypes.BytesToBloom(blockBloomResp.Bloom)
formattedBlock := formatBlock(block.Header, block.Size(), ethermint.DefaultRPCGasLimit, gasUsed, ethRPCTxs, bloom) formattedBlock := FormatBlock(block.Header, block.Size(), ethermint.DefaultRPCGasLimit, gasUsed, ethRPCTxs, bloom)
return formattedBlock, nil return formattedBlock, nil
} }

View File

@ -144,7 +144,19 @@ func (e *PublicEthAPI) GasPrice() *hexutil.Big {
func (e *PublicEthAPI) Accounts() ([]common.Address, error) { func (e *PublicEthAPI) Accounts() ([]common.Address, error) {
e.logger.Debugln("eth_accounts") e.logger.Debugln("eth_accounts")
return []common.Address{}, nil addresses := make([]common.Address, 0) // return [] instead of nil if empty
infos, err := e.clientCtx.Keyring.List()
if err != nil {
return addresses, err
}
for _, info := range infos {
addressBytes := info.GetPubKey().Address().Bytes()
addresses = append(addresses, common.BytesToAddress(addressBytes))
}
return addresses, nil
} }
// BlockNumber returns the current block number. // BlockNumber returns the current block number.
@ -155,7 +167,7 @@ func (e *PublicEthAPI) BlockNumber() (hexutil.Uint64, error) {
// GetBalance returns the provided account's balance up to the provided block number. // GetBalance returns the provided account's balance up to the provided block number.
func (e *PublicEthAPI) GetBalance(address common.Address, blockNum types.BlockNumber) (*hexutil.Big, error) { // nolint: interfacer func (e *PublicEthAPI) GetBalance(address common.Address, blockNum types.BlockNumber) (*hexutil.Big, error) { // nolint: interfacer
e.logger.Debugln("eth_getBalance", "address", address.Hex(), "block number", blockNum) e.logger.Debugln("eth_getBalance", "address", address.String(), "block number", blockNum)
req := &evmtypes.QueryBalanceRequest{ req := &evmtypes.QueryBalanceRequest{
Address: address.String(), Address: address.String(),
@ -367,7 +379,7 @@ func (e *PublicEthAPI) Call(args types.CallArgs, blockNr types.BlockNumber, _ *m
return (hexutil.Bytes)(data.Ret), nil return (hexutil.Bytes)(data.Ret), nil
} }
var zeroAddr = common.HexToAddress("0x0000000000000000000000000000000000000000") var zeroAddr = common.Address{}
// DoCall performs a simulated call operation through the evmtypes. It returns the // DoCall performs a simulated call operation through the evmtypes. It returns the
// estimated gas used on the operation or an error if fails. // estimated gas used on the operation or an error if fails.
@ -489,10 +501,13 @@ func (e *PublicEthAPI) EstimateGas(args types.CallArgs) (hexutil.Uint64, error)
simRes, err := e.doCall(args, 0, big.NewInt(ethermint.DefaultRPCGasLimit)) simRes, err := e.doCall(args, 0, big.NewInt(ethermint.DefaultRPCGasLimit))
if err != nil { if err != nil {
return 0, err return 0, err
} else if len(simRes.Result.Log) > 0 { }
if len(simRes.Result.Log) > 0 {
var logs []sdkTxLogs var logs []sdkTxLogs
if err := json.Unmarshal([]byte(simRes.Result.Log), &logs); err != nil { if err := json.Unmarshal([]byte(simRes.Result.Log), &logs); err != nil {
e.logger.WithError(err).Errorln("failed to unmarshal simRes.Result.Log") e.logger.WithError(err).Errorln("failed to unmarshal simRes.Result.Log")
return 0, err
} }
if len(logs) > 0 && logs[0].Log == logRevertedFlag { if len(logs) > 0 && logs[0].Log == logRevertedFlag {

View File

@ -89,7 +89,7 @@ func NewTransaction(tx *evmtypes.MsgEthereumTx, txHash, blockHash common.Hash, b
return rpcTx, nil return rpcTx, nil
} }
// NewTransaction returns a transaction that will serialize to the RPC // NewTransactionFromData returns a transaction that will serialize to the RPC
// representation, with the given location metadata set (if available). // representation, with the given location metadata set (if available).
func NewTransactionFromData( func NewTransactionFromData(
txData *evmtypes.TxData, txData *evmtypes.TxData,
@ -118,7 +118,7 @@ func NewTransactionFromData(
S: (*hexutil.Big)(new(big.Int).SetBytes(txData.S)), S: (*hexutil.Big)(new(big.Int).SetBytes(txData.S)),
} }
if rpcTx.To == nil { if rpcTx.To == nil {
addr := common.HexToAddress("0x0000000000000000000000000000000000000000") addr := zeroAddr
rpcTx.To = &addr rpcTx.To = &addr
} }
@ -173,7 +173,7 @@ func bigOrZero(i *big.Int) *hexutil.Big {
return (*hexutil.Big)(i) return (*hexutil.Big)(i)
} }
func formatBlock( func FormatBlock(
header tmtypes.Header, size int, gasLimit int64, header tmtypes.Header, size int, gasLimit int64,
gasUsed *big.Int, transactions interface{}, bloom ethtypes.Bloom, gasUsed *big.Int, transactions interface{}, bloom ethtypes.Bloom,
) map[string]interface{} { ) map[string]interface{} {