diff options
author | johnliu-dexon <42129254+johnliu-dexon@users.noreply.github.com> | 2018-11-27 17:13:22 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:54 +0800 |
commit | c32fad6a98d7276e6b9317e48fed871d2b13959f (patch) | |
tree | e85a71a16722a22aada7210987f91cfd0ec71fae | |
parent | d6d7599ac09fa11f8db4ee76a90aab7ea1637f36 (diff) | |
download | go-tangerine-c32fad6a98d7276e6b9317e48fed871d2b13959f.tar go-tangerine-c32fad6a98d7276e6b9317e48fed871d2b13959f.tar.gz go-tangerine-c32fad6a98d7276e6b9317e48fed871d2b13959f.tar.bz2 go-tangerine-c32fad6a98d7276e6b9317e48fed871d2b13959f.tar.lz go-tangerine-c32fad6a98d7276e6b9317e48fed871d2b13959f.tar.xz go-tangerine-c32fad6a98d7276e6b9317e48fed871d2b13959f.tar.zst go-tangerine-c32fad6a98d7276e6b9317e48fed871d2b13959f.zip |
internal/ethapi: add getBlockReceiptsByHash (#56)
* dex: add getBlockReceiptsByHash
add rpc method for performance acceleration
* Update internal/ethapi/api.go
Co-Authored-By: johnliu-dexon <42129254+johnliu-dexon@users.noreply.github.com>
-rw-r--r-- | internal/ethapi/api.go | 35 | ||||
-rw-r--r-- | internal/jsre/deps/web3.js | 20 |
2 files changed, 55 insertions, 0 deletions
diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index a0b79329c..2226bebb1 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -673,6 +673,41 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.A return res[:], state.Error() } +// GetBlockReceiptsByNumber returns the requested block receipts. +func (s *PublicBlockChainAPI) GetBlockReceiptsByHash(ctx context.Context, blockHash common.Hash) ([]map[string]interface{}, error) { + receipts, err := s.b.GetReceipts(ctx, blockHash) + if err != nil { + return nil, err + } + + resp := make([]map[string]interface{}, 0, len(receipts)) + for _, receipt := range receipts { + fields := map[string]interface{}{ + "gasUsed": hexutil.Uint64(receipt.GasUsed), + "cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed), + "contractAddress": nil, + "logs": receipt.Logs, + "logsBloom": receipt.Bloom, + } + + // Assign receipt status or post state. + if len(receipt.PostState) > 0 { + fields["root"] = hexutil.Bytes(receipt.PostState) + } else { + fields["status"] = hexutil.Uint(receipt.Status) + } + if receipt.Logs == nil { + fields["logs"] = [][]*types.Log{} + } + // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation + if receipt.ContractAddress != (common.Address{}) { + fields["contractAddress"] = receipt.ContractAddress + } + resp = append(resp, fields) + } + return resp, nil +} + // CallArgs represents the arguments for a call. type CallArgs struct { From common.Address `json:"from"` diff --git a/internal/jsre/deps/web3.js b/internal/jsre/deps/web3.js index 7848393dd..333c32b48 100644 --- a/internal/jsre/deps/web3.js +++ b/internal/jsre/deps/web3.js @@ -3811,6 +3811,16 @@ var outputTransactionReceiptFormatter = function (receipt){ return receipt; }; +var outputTransactionReceiptsFormatter = function (receipts){ + if(utils.isArray(receipts)) { + receipts = receipts.map(function(receipt){ + return outputTransactionReceiptFormatter(receipt); + }); + } + + return receipts; +}; + /** * Formats the output of a block to its proper values * @@ -3957,6 +3967,7 @@ module.exports = { outputBigNumberFormatter: outputBigNumberFormatter, outputTransactionFormatter: outputTransactionFormatter, outputTransactionReceiptFormatter: outputTransactionReceiptFormatter, + outputTransactionReceiptsFormatter: outputTransactionReceiptsFormatter, outputBlockFormatter: outputBlockFormatter, outputLogFormatter: outputLogFormatter, outputPostFormatter: outputPostFormatter, @@ -5289,6 +5300,14 @@ var methods = function () { inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter] }); + var getBlockReceiptsByHash = new Method({ + name: 'getBlockReceiptsByHash', + call: 'eth_getBlockReceiptsByHash', + params: 1, + inputFormatter: [formatters.inputBlockNumberFormatter], + outputFormatter: formatters.outputTransactionReceiptsFormatter, + }); + var getBlock = new Method({ name: 'getBlock', call: blockCall, @@ -5435,6 +5454,7 @@ var methods = function () { getBalance, getStorageAt, getCode, + getBlockReceiptsByHash, getBlock, getUncle, getCompilers, |