From ce0b99df4514719f919e8ec119444c14a77a1ea4 Mon Sep 17 00:00:00 2001 From: hero5512 Date: Thu, 8 Dec 2022 21:51:41 -0500 Subject: [PATCH] Fix getBlock bug (#1418) * fix stateRoot rpc type * fix stateRoot rpc type * fix * add bloom --- http/ethrpc/eth/api.go | 12 ++++++++++-- http/ethrpc/utils/utils.go | 26 +++++++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/http/ethrpc/eth/api.go b/http/ethrpc/eth/api.go index dffd1b348..ddbd1c43e 100644 --- a/http/ethrpc/eth/api.go +++ b/http/ethrpc/eth/api.go @@ -409,7 +409,11 @@ func (api *EthereumAPI) GetBlockByHash(hash common.Hash, fullTx bool) (interface if block == nil { return nil, nil } - return utils2.EthBlockFromOntology(block, fullTx), nil + bloom, err := bactor.GetBloomData(block.Header.Height) + if err != nil { + return nil, err + } + return utils2.EthBlockFromOntology(block, fullTx, bloom), nil } func (api *EthereumAPI) GetBlockByNumber(blockNum types2.BlockNumber, fullTx bool) (interface{}, error) { @@ -428,7 +432,11 @@ func (api *EthereumAPI) GetBlockByNumber(blockNum types2.BlockNumber, fullTx boo if block == nil { return nil, nil } - return utils2.EthBlockFromOntology(block, fullTx), nil + bloom, err := bactor.GetBloomData(block.Header.Height) + if err != nil { + return nil, err + } + return utils2.EthBlockFromOntology(block, fullTx, bloom), nil } func (api *EthereumAPI) GetTransactionByHash(hash common.Hash) (*types2.Transaction, error) { diff --git a/http/ethrpc/utils/utils.go b/http/ethrpc/utils/utils.go index d6f573a7a..f83d0426a 100644 --- a/http/ethrpc/utils/utils.go +++ b/http/ethrpc/utils/utils.go @@ -31,7 +31,7 @@ import ( types3 "github.com/ontio/ontology/http/ethrpc/types" ) -func EthBlockFromOntology(block *types.Block, fullTx bool) map[string]interface{} { +func EthBlockFromOntology(block *types.Block, fullTx bool, bloom types2.Bloom) map[string]interface{} { if block == nil { return nil } @@ -45,7 +45,7 @@ func EthBlockFromOntology(block *types.Block, fullTx bool) map[string]interface{ } else { blockTxs = transactions } - return FormatBlock(*block, 0, gasUsed, blockTxs) + return FormatBlock(*block, 0, gasUsed, blockTxs, bloom) } func RawEthBlockFromOntology(block *types.Block, bloom types2.Bloom) *types2.Block { @@ -116,18 +116,22 @@ func OntTxToEthTx(tx types.Transaction, blockHash common.Hash, blockNumber, inde return NewTransaction(eip155Tx, common.Hash(tx.Hash()), blockHash, blockNumber, index) } -func FormatBlock(block types.Block, gasLimit uint64, gasUsed *big.Int, transactions interface{}) map[string]interface{} { +func FormatBlock(block types.Block, gasLimit uint64, gasUsed *big.Int, transactions interface{}, bloom types2.Bloom) map[string]interface{} { size := len(block.ToArray()) header := block.Header hash := header.Hash() + transactionsRoot := types2.EmptyRootHash + if oComm.UINT256_EMPTY != header.TransactionsRoot { + transactionsRoot = common.BytesToHash(header.TransactionsRoot[:]) + } ret := map[string]interface{}{ "number": hexutil.Uint64(header.Height), - "hash": hexutil.Bytes(hash[:]), - "parentHash": hexutil.Bytes(header.PrevBlockHash[:]), + "hash": common.BytesToHash(hash[:]), + "parentHash": common.BytesToHash(header.PrevBlockHash[:]), "nonce": types2.BlockNonce{}, // PoW specific - "sha3Uncles": common.Hash{}, - "logsBloom": types2.Bloom{}, - "transactionsRoot": hexutil.Bytes(header.TransactionsRoot[:]), + "sha3Uncles": types2.EmptyUncleHash, + "logsBloom": bloom, + "transactionsRoot": transactionsRoot, "stateRoot": common.Hash{}, "miner": common.Address{}, "mixHash": common.Hash{}, @@ -135,11 +139,11 @@ func FormatBlock(block types.Block, gasLimit uint64, gasUsed *big.Int, transacti "totalDifficulty": hexutil.Uint64(0), "extraData": hexutil.Bytes{}, "size": hexutil.Uint64(size), - "gasLimit": hexutil.Uint64(gasLimit), // TODO Static gas limit + "gasLimit": hexutil.Uint64(gasLimit), "gasUsed": (*hexutil.Big)(gasUsed), "timestamp": hexutil.Uint64(header.Timestamp), - "uncles": []string{}, - "receiptsRoot": common.Hash{}, + "uncles": []common.Hash{}, + "receiptsRoot": types2.EmptyRootHash, } if !reflect.ValueOf(transactions).IsNil() { switch transactions.(type) {