Skip to content

Commit

Permalink
rpc: fix inconsistent logging levels (evmos#265)
Browse files Browse the repository at this point in the history
* rpc: fix incosistent logging levels

* minor changes
  • Loading branch information
fedekunze authored Jul 12, 2021
1 parent 1a48e09 commit 74b7eaf
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 285 deletions.
14 changes: 8 additions & 6 deletions ethereum/rpc/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package rpc

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"

"github.com/ethereum/go-ethereum/rpc"
"github.com/tharsis/ethermint/ethereum/rpc/backend"
"github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth"
Expand All @@ -29,10 +31,10 @@ const (
)

// GetRPCAPIs returns the list of all APIs
func GetRPCAPIs(clientCtx client.Context, tmWSClient *rpcclient.WSClient) []rpc.API {
func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, tmWSClient *rpcclient.WSClient) []rpc.API {
nonceLock := new(types.AddrLocker)
backend := backend.NewEVMBackend(clientCtx)
ethAPI := eth.NewPublicAPI(clientCtx, backend, nonceLock)
backend := backend.NewEVMBackend(ctx.Logger, clientCtx)
ethAPI := eth.NewPublicAPI(ctx.Logger, clientCtx, backend, nonceLock)

return []rpc.API{
{
Expand All @@ -50,7 +52,7 @@ func GetRPCAPIs(clientCtx client.Context, tmWSClient *rpcclient.WSClient) []rpc.
{
Namespace: EthNamespace,
Version: apiVersion,
Service: filters.NewPublicAPI(tmWSClient, backend),
Service: filters.NewPublicAPI(ctx.Logger, tmWSClient, backend),
Public: true,
},
{
Expand All @@ -62,13 +64,13 @@ func GetRPCAPIs(clientCtx client.Context, tmWSClient *rpcclient.WSClient) []rpc.
{
Namespace: PersonalNamespace,
Version: apiVersion,
Service: personal.NewAPI(ethAPI),
Service: personal.NewAPI(ctx.Logger, ethAPI),
Public: true,
},
{
Namespace: TxPoolNamespace,
Version: apiVersion,
Service: txpool.NewPublicAPI(),
Service: txpool.NewPublicAPI(ctx.Logger),
Public: true,
},
}
Expand Down
50 changes: 23 additions & 27 deletions ethereum/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/tharsis/ethermint/ethereum/rpc/types"

"github.com/pkg/errors"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
log "github.com/xlab/suplog"

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -58,7 +58,7 @@ type EVMBackend struct {
}

// NewEVMBackend creates a new EVMBackend instance
func NewEVMBackend(clientCtx client.Context) *EVMBackend {
func NewEVMBackend(logger log.Logger, clientCtx client.Context) *EVMBackend {
chainID, err := ethermint.ParseChainID(clientCtx.ChainID)
if err != nil {
panic(err)
Expand All @@ -67,7 +67,7 @@ func NewEVMBackend(clientCtx client.Context) *EVMBackend {
ctx: context.Background(),
clientCtx: clientCtx,
queryClient: types.NewQueryClient(clientCtx),
logger: log.WithField("module", "evm-backend"),
logger: logger.With("module", "evm-backend"),
chainID: chainID,
}
}
Expand Down Expand Up @@ -110,21 +110,21 @@ func (e *EVMBackend) GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (

resBlock, err := e.clientCtx.Client.Block(e.ctx, &height)
if err != nil {
// e.logger.Debugf("GetBlockByNumber safely bumping down from %d to latest", height)
// e.logger.Debug("GetBlockByNumber safely bumping down from %d to latest", height)
if resBlock, err = e.clientCtx.Client.Block(e.ctx, nil); err != nil {
e.logger.WithError(err).Debugln("GetBlockByNumber failed to get latest block")
e.logger.Debug("GetBlockByNumber failed to get latest block", "error", err.Error())
return nil, nil
}
}

if resBlock.Block == nil {
e.logger.Debugln("GetBlockByNumber block not found", "height", height)
e.logger.Debug("GetBlockByNumber block not found", "height", height)
return nil, nil
}

res, err := e.EthBlockFromTendermint(e.clientCtx, e.queryClient, resBlock.Block, fullTx)
if err != nil {
e.logger.WithError(err).Debugf("EthBlockFromTendermint failed with block %s", resBlock.Block.String())
e.logger.Debug("EthBlockFromTendermint failed", "height", height, "error", err.Error())
}

return res, err
Expand All @@ -134,12 +134,12 @@ func (e *EVMBackend) GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (
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.WithError(err).Debugln("BlockByHash block not found", "hash", hash.Hex())
e.logger.Debug("BlockByHash block not found", "hash", hash.Hex(), "error", err.Error())
return nil, err
}

if resBlock.Block == nil {
e.logger.Debugln("BlockByHash block not found", "hash", hash.Hex())
e.logger.Debug("BlockByHash block not found", "hash", hash.Hex())
return nil, nil
}

Expand All @@ -161,7 +161,7 @@ func (e *EVMBackend) EthBlockFromTendermint(
for i, txBz := range block.Txs {
tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz)
if err != nil {
e.logger.WithError(err).Warningln("failed to decode transaction in block at height ", block.Height)
e.logger.Debug("failed to decode transaction in block", "height", block.Height, "error", err.Error())
continue
}

Expand All @@ -183,13 +183,13 @@ func (e *EVMBackend) EthBlockFromTendermint(
// get full transaction from message data
from, err := ethMsg.GetSender(e.chainID)
if err != nil {
e.logger.WithError(err).Warningln("failed to get sender from already included transaction ", hash)
e.logger.Debug("failed to get sender from already included transaction", "hash", hash.Hex(), "error", err.Error())
from = common.HexToAddress(ethMsg.From)
}

txData, err := evmtypes.UnpackTxData(ethMsg.Data)
if err != nil {
e.logger.WithError(err).Debugln("decoding failed")
e.logger.Debug("decoding failed", "error", err.Error())
return nil, fmt.Errorf("failed to unpack tx data: %w", err)
}

Expand All @@ -202,7 +202,7 @@ func (e *EVMBackend) EthBlockFromTendermint(
uint64(i),
)
if err != nil {
e.logger.WithError(err).Debugln("NewTransactionFromData for receipt failed", "hash", hash.Hex)
e.logger.Debug("NewTransactionFromData for receipt failed", "hash", hash.Hex(), "error", err.Error())
continue
}
ethRPCTxs = append(ethRPCTxs, ethTx)
Expand All @@ -211,15 +211,13 @@ func (e *EVMBackend) EthBlockFromTendermint(

blockBloomResp, err := queryClient.BlockBloom(types.ContextWithHeight(block.Height), &evmtypes.QueryBlockBloomRequest{})
if err != nil {
e.logger.WithError(err).Debugln("failed to query BlockBloom", "height", block.Height)
e.logger.Debug("failed to query BlockBloom", "height", block.Height, "error", err.Error())

blockBloomResp = &evmtypes.QueryBlockBloomResponse{Bloom: ethtypes.Bloom{}.Bytes()}
}

bloom := ethtypes.BytesToBloom(blockBloomResp.Bloom)
formattedBlock := types.FormatBlock(block.Header, block.Size(), ethermint.DefaultRPCGasLimit, new(big.Int).SetUint64(gasUsed), ethRPCTxs, bloom)

e.logger.Infoln(formattedBlock)
return formattedBlock, nil
}

Expand All @@ -241,22 +239,21 @@ func (e *EVMBackend) HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Heade
height = 1
default:
if blockNum < 0 {
err := errors.Errorf("incorrect block height: %d", height)
return nil, err
return nil, errors.Errorf("incorrect block height: %d", height)
}
}

resBlock, err := e.clientCtx.Client.Block(e.ctx, &height)
if err != nil {
e.logger.Warningf("HeaderByNumber failed")
e.logger.Debug("HeaderByNumber failed")
return nil, err
}

req := &evmtypes.QueryBlockBloomRequest{}

res, err := e.queryClient.BlockBloom(types.ContextWithHeight(resBlock.Block.Height), req)
if err != nil {
e.logger.Warningf("HeaderByNumber BlockBloom fail %d", resBlock.Block.Height)
e.logger.Debug("HeaderByNumber BlockBloom failed", "height", resBlock.Block.Height)
return nil, err
}

Expand All @@ -269,15 +266,15 @@ func (e *EVMBackend) HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Heade
func (e *EVMBackend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) {
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, blockHash.Bytes())
if err != nil {
e.logger.Warningf("HeaderByHash fail")
e.logger.Debug("HeaderByHash failed", "hash", blockHash.Hex())
return nil, err
}

req := &evmtypes.QueryBlockBloomRequest{}

res, err := e.queryClient.BlockBloom(types.ContextWithHeight(resBlock.Block.Height), req)
if err != nil {
e.logger.Warningf("HeaderByHash BlockBloom fail %d", resBlock.Block.Height)
e.logger.Debug("HeaderByHash BlockBloom failed", "height", resBlock.Block.Height)
return nil, err
}

Expand All @@ -296,7 +293,7 @@ func (e *EVMBackend) GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, er

res, err := e.queryClient.TxLogs(e.ctx, req)
if err != nil {
e.logger.Warningf("TxLogs fail")
e.logger.Debug("TxLogs failed", "tx-hash", req.Hash)
return nil, err
}

Expand Down Expand Up @@ -332,7 +329,7 @@ func (e *EVMBackend) GetLogs(blockHash common.Hash) ([][]*ethtypes.Log, error) {

res, err := e.queryClient.BlockLogs(e.ctx, req)
if err != nil {
e.logger.Warningf("BlockLogs fail")
e.logger.Debug("BlockLogs failed", "hash", req.Hash)
return nil, err
}

Expand Down Expand Up @@ -364,8 +361,7 @@ func (e *EVMBackend) GetLogsByNumber(blockNum types.BlockNumber) ([][]*ethtypes.
height = 1
default:
if blockNum < 0 {
err := errors.Errorf("incorrect block height: %d", height)
return nil, err
return nil, errors.Errorf("incorrect block height: %d", height)
}
}

Expand All @@ -375,7 +371,7 @@ func (e *EVMBackend) GetLogsByNumber(blockNum types.BlockNumber) ([][]*ethtypes.
return [][]*ethtypes.Log{}, nil
}

e.logger.Warningf("failed to query block at %d", height)
e.logger.Debug("failed to query block", "height", height)
return nil, err
}

Expand Down
Loading

0 comments on commit 74b7eaf

Please sign in to comment.