Skip to content

Commit

Permalink
Merge PR: fix missing transaction detail when calling func GetBlockBy…
Browse files Browse the repository at this point in the history
…Hash and GetBlockByNumber (#655)

* fix missing tx detail when calling func GetBlockByHash and GetBlockByNumber and passing true as param fullTx

* fix miner address to proposer address
  • Loading branch information
KamiD authored Feb 18, 2021
1 parent 1a60711 commit c04cbf5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
6 changes: 3 additions & 3 deletions app/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (b *EthermintBackend) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullT
return nil, nil
}

return rpctypes.EthBlockFromTendermint(b.clientCtx, resBlock.Block)
return rpctypes.EthBlockFromTendermint(b.clientCtx, resBlock.Block, fullTx)
}

// GetBlockByHash returns the block identified by hash.
Expand All @@ -107,7 +107,7 @@ func (b *EthermintBackend) GetBlockByHash(hash common.Hash, fullTx bool) (map[st
return nil, nil
}

return rpctypes.EthBlockFromTendermint(b.clientCtx, resBlock.Block)
return rpctypes.EthBlockFromTendermint(b.clientCtx, resBlock.Block, fullTx)
}

// HeaderByNumber returns the block header identified by height.
Expand Down Expand Up @@ -267,4 +267,4 @@ func (b *EthermintBackend) LatestBlockNumber() (int64, error) {
}

return info.LastHeight, nil
}
}
12 changes: 9 additions & 3 deletions app/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func (api *PublicEthereumAPI) GetBlockByHash(hash common.Hash, fullTx bool) (map
// GetBlockByNumber returns the block identified by number.
func (api *PublicEthereumAPI) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) (map[string]interface{}, error) {
api.logger.Debug("eth_getBlockByNumber", "number", blockNum, "full", fullTx)

var blockTxs interface{}
if blockNum != rpctypes.PendingBlockNumber {
return api.backend.GetBlockByNumber(blockNum, fullTx)
}
Expand All @@ -687,11 +687,17 @@ func (api *PublicEthereumAPI) GetBlockByNumber(blockNum rpctypes.BlockNumber, fu
return nil, err
}

pendingTxs, gasUsed, err := rpctypes.EthTransactionsFromTendermint(api.clientCtx, unconfirmedTxs.Txs)
pendingTxs, gasUsed, ethTxs, err := rpctypes.EthTransactionsFromTendermint(api.clientCtx, unconfirmedTxs.Txs, common.BytesToHash(latestBlock.Block.Hash()), uint64(height))
if err != nil {
return nil, err
}

if fullTx {
blockTxs = ethTxs
} else {
blockTxs = pendingTxs
}

return rpctypes.FormatBlock(
tmtypes.Header{
Version: latestBlock.Block.Version,
Expand All @@ -705,7 +711,7 @@ func (api *PublicEthereumAPI) GetBlockByNumber(blockNum rpctypes.BlockNumber, fu
latestBlock.Block.Hash(),
0,
gasUsed,
pendingTxs,
blockTxs,
ethtypes.Bloom{},
), nil

Expand Down
37 changes: 28 additions & 9 deletions app/rpc/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ func NewTransaction(tx *evmtypes.MsgEthereumTx, txHash, blockHash common.Hash, b
}

// EthBlockFromTendermint returns a JSON-RPC compatible Ethereum blockfrom a given Tendermint block.
func EthBlockFromTendermint(clientCtx clientcontext.CLIContext, block *tmtypes.Block) (map[string]interface{}, error) {
func EthBlockFromTendermint(clientCtx clientcontext.CLIContext, block *tmtypes.Block, fullTx bool) (map[string]interface{}, error) {
var blockTxs interface{}
gasLimit, err := BlockMaxGasFromConsensusParams(context.Background(), clientCtx)
if err != nil {
return nil, err
}

transactions, gasUsed, err := EthTransactionsFromTendermint(clientCtx, block.Txs)
transactions, gasUsed, ethTxs, err := EthTransactionsFromTendermint(clientCtx, block.Txs, common.BytesToHash(block.Hash()), uint64(block.Height))
if err != nil {
return nil, err
}
Expand All @@ -90,8 +91,13 @@ func EthBlockFromTendermint(clientCtx clientcontext.CLIContext, block *tmtypes.B
clientCtx.Codec.MustUnmarshalJSON(res, &bloomRes)

bloom := bloomRes.Bloom
if fullTx {
blockTxs = ethTxs
} else {
blockTxs = transactions
}

return FormatBlock(block.Header, block.Size(), block.Hash(), gasLimit, gasUsed, transactions, bloom), nil
return FormatBlock(block.Header, block.Size(), block.Hash(), gasLimit, gasUsed, blockTxs, bloom), nil
}

// EthHeaderFromTendermint is an util function that returns an Ethereum Header
Expand All @@ -115,9 +121,11 @@ func EthHeaderFromTendermint(header tmtypes.Header) *ethtypes.Header {

// EthTransactionsFromTendermint returns a slice of ethereum transaction hashes and the total gas usage from a set of
// tendermint block transactions.
func EthTransactionsFromTendermint(clientCtx clientcontext.CLIContext, txs []tmtypes.Tx) ([]common.Hash, *big.Int, error) {
transactionHashes := []common.Hash{}
func EthTransactionsFromTendermint(clientCtx clientcontext.CLIContext, txs []tmtypes.Tx, blockHash common.Hash, blockNumber uint64) ([]common.Hash, *big.Int, []*Transaction, error) {
var transactionHashes []common.Hash
var transactions []*Transaction
gasUsed := big.NewInt(0)
index := uint64(0)

for _, tx := range txs {
ethTx, err := RawTxToEthTx(clientCtx, tx)
Expand All @@ -128,9 +136,14 @@ func EthTransactionsFromTendermint(clientCtx clientcontext.CLIContext, txs []tmt
// TODO: Remove gas usage calculation if saving gasUsed per block
gasUsed.Add(gasUsed, big.NewInt(int64(ethTx.GetGas())))
transactionHashes = append(transactionHashes, common.BytesToHash(tx.Hash()))
tx, err := NewTransaction(ethTx, common.BytesToHash(tx.Hash()), blockHash, blockNumber, index)
if err == nil {
transactions = append(transactions, tx)
index++
}
}

return transactionHashes, gasUsed, nil
return transactionHashes, gasUsed, transactions, nil
}

// BlockMaxGasFromConsensusParams returns the gas limit for the latest block from the chain consensus params.
Expand Down Expand Up @@ -163,7 +176,7 @@ func FormatBlock(
header.DataHash = tmbytes.HexBytes(common.Hash{}.Bytes())
}

return map[string]interface{}{
ret := map[string]interface{}{
"number": hexutil.Uint64(header.Height),
"hash": hexutil.Bytes(curBlockHash),
"parentHash": hexutil.Bytes(header.LastBlockID.Hash),
Expand All @@ -172,7 +185,7 @@ func FormatBlock(
"logsBloom": bloom,
"transactionsRoot": hexutil.Bytes(header.DataHash),
"stateRoot": hexutil.Bytes(header.AppHash),
"miner": common.Address{},
"miner": common.BytesToAddress(header.ProposerAddress),
"mixHash": common.Hash{},
"difficulty": hexutil.Uint64(0),
"totalDifficulty": hexutil.Uint64(0),
Expand All @@ -181,10 +194,16 @@ func FormatBlock(
"gasLimit": hexutil.Uint64(gasLimit), // Static gas limit
"gasUsed": (*hexutil.Big)(gasUsed),
"timestamp": hexutil.Uint64(header.Time.Unix()),
"transactions": transactions.([]common.Hash),
"uncles": []string{},
"receiptsRoot": common.Hash{},
}
switch transactions.(type) {
case []common.Hash:
ret["transactions"] = transactions.([]common.Hash)
case []*Transaction:
ret["transactions"] = transactions.([]*Transaction)
}
return ret
}

// GetKeyByAddress returns the private key matching the given address. If not found it returns false.
Expand Down

0 comments on commit c04cbf5

Please sign in to comment.