Skip to content

Commit 0e2c471

Browse files
author
HaoyangLiu
authored
R4R: add batch query methods (bnb-chain#115)
* add batch query method * GetTransactionDataAndReceipt * add new rpc to goclient * fix web3 console * rename tx_data to txData
1 parent f16d8e0 commit 0e2c471

5 files changed

Lines changed: 269 additions & 24 deletions

File tree

‎core/types/receipt.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ const (
4646
ReceiptStatusSuccessful = uint64(1)
4747
)
4848

49+
// Receipt represents the results of a transaction.
50+
type OriginalDataAndReceipt struct {
51+
Receipt Receipt `json:"receipt"`
52+
TxData Transaction `json:"txData"`
53+
}
54+
4955
// Receipt represents the results of a transaction.
5056
type Receipt struct {
5157
// Consensus fields: These fields are defined by the Yellow Paper

‎ethclient/ethclient.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,44 @@ func (ec *Client) TransactionInBlock(ctx context.Context, blockHash common.Hash,
265265
return json.tx, err
266266
}
267267

268+
// TransactionInBlock returns a single transaction at index in the given block.
269+
func (ec *Client) TransactionsInBlock(ctx context.Context, number *big.Int) ([]*types.Transaction, error) {
270+
var rpcTxs []*rpcTransaction
271+
err := ec.c.CallContext(ctx, &rpcTxs, "eth_getTransactionsByBlockNumber", toBlockNumArg(number))
272+
if err != nil {
273+
return nil, err
274+
}
275+
fmt.Println(len(rpcTxs))
276+
txs := make([]*types.Transaction, 0, len(rpcTxs))
277+
for _, tx := range rpcTxs {
278+
txs = append(txs, tx.tx)
279+
}
280+
return txs, err
281+
}
282+
283+
// TransactionInBlock returns a single transaction at index in the given block.
284+
func (ec *Client) TransactionRecipientsInBlock(ctx context.Context, number *big.Int) ([]*types.Receipt, error) {
285+
var rs []*types.Receipt
286+
err := ec.c.CallContext(ctx, &rs, "eth_getTransactionReceiptsByBlockNumber", toBlockNumArg(number))
287+
if err != nil {
288+
return nil, err
289+
}
290+
return rs, err
291+
}
292+
293+
// TransactionDataAndReceipt returns the original data and receipt of a transaction by transaction hash.
294+
// Note that the receipt is not available for pending transactions.
295+
func (ec *Client) TransactionDataAndReceipt(ctx context.Context, txHash common.Hash) (*types.OriginalDataAndReceipt, error) {
296+
var r *types.OriginalDataAndReceipt
297+
err := ec.c.CallContext(ctx, &r, "eth_getTransactionDataAndReceipt", txHash)
298+
if err == nil {
299+
if r == nil {
300+
return nil, ethereum.NotFound
301+
}
302+
}
303+
return r, err
304+
}
305+
268306
// TransactionReceipt returns the receipt of a transaction by transaction hash.
269307
// Note that the receipt is not available for pending transactions.
270308
func (ec *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {

‎internal/ethapi/api.go‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,17 @@ func newRPCPendingTransaction(tx *types.Transaction) *RPCTransaction {
11691169
return newRPCTransaction(tx, common.Hash{}, 0, 0)
11701170
}
11711171

1172+
// newRPCTransactionsFromBlockIndex returns transactions that will serialize to the RPC representation.
1173+
func newRPCTransactionsFromBlockIndex(b *types.Block) []*RPCTransaction {
1174+
txs := b.Transactions()
1175+
result := make([]*RPCTransaction, 0, len(txs))
1176+
1177+
for idx, tx := range txs {
1178+
result = append(result, newRPCTransaction(tx, b.Hash(), b.NumberU64(), uint64(idx)))
1179+
}
1180+
return result
1181+
}
1182+
11721183
// newRPCTransactionFromBlockIndex returns a transaction that will serialize to the RPC representation.
11731184
func newRPCTransactionFromBlockIndex(b *types.Block, index uint64) *RPCTransaction {
11741185
txs := b.Transactions()
@@ -1227,6 +1238,14 @@ func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByHash(ctx context.Co
12271238
return nil
12281239
}
12291240

1241+
// GetTransactionsByBlockNumber returns all the transactions for the given block number.
1242+
func (s *PublicTransactionPoolAPI) GetTransactionsByBlockNumber(ctx context.Context, blockNr rpc.BlockNumber) []*RPCTransaction {
1243+
if block, _ := s.b.BlockByNumber(ctx, blockNr); block != nil {
1244+
return newRPCTransactionsFromBlockIndex(block)
1245+
}
1246+
return nil
1247+
}
1248+
12301249
// GetTransactionByBlockNumberAndIndex returns the transaction for the given block number and index.
12311250
func (s *PublicTransactionPoolAPI) GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) *RPCTransaction {
12321251
if block, _ := s.b.BlockByNumber(ctx, blockNr); block != nil {
@@ -1314,6 +1333,141 @@ func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context,
13141333
return rlp.EncodeToBytes(tx)
13151334
}
13161335

1336+
// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
1337+
func (s *PublicTransactionPoolAPI) GetTransactionReceiptsByBlockNumber(ctx context.Context, blockNr rpc.BlockNumber) ([]map[string]interface{}, error) {
1338+
blockNumber := uint64(blockNr.Int64())
1339+
blockHash := rawdb.ReadCanonicalHash(s.b.ChainDb(), blockNumber)
1340+
1341+
receipts, err := s.b.GetReceipts(ctx, blockHash)
1342+
if err != nil {
1343+
return nil, err
1344+
}
1345+
block, err := s.b.BlockByHash(ctx, blockHash)
1346+
if err != nil {
1347+
return nil, err
1348+
}
1349+
txs := block.Transactions()
1350+
if len(txs) != len(receipts) {
1351+
return nil, fmt.Errorf("txs length doesn't equal to receipts' length")
1352+
}
1353+
1354+
txReceipts := make([]map[string]interface{}, 0, len(txs))
1355+
for idx, receipt := range receipts {
1356+
tx := txs[idx]
1357+
var signer types.Signer = types.FrontierSigner{}
1358+
if tx.Protected() {
1359+
signer = types.NewEIP155Signer(tx.ChainId())
1360+
}
1361+
from, _ := types.Sender(signer, tx)
1362+
1363+
fields := map[string]interface{}{
1364+
"blockHash": blockHash,
1365+
"blockNumber": hexutil.Uint64(blockNumber),
1366+
"transactionHash": tx.Hash(),
1367+
"transactionIndex": hexutil.Uint64(idx),
1368+
"from": from,
1369+
"to": tx.To(),
1370+
"gasUsed": hexutil.Uint64(receipt.GasUsed),
1371+
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
1372+
"contractAddress": nil,
1373+
"logs": receipt.Logs,
1374+
"logsBloom": receipt.Bloom,
1375+
}
1376+
1377+
// Assign receipt status or post state.
1378+
if len(receipt.PostState) > 0 {
1379+
fields["root"] = hexutil.Bytes(receipt.PostState)
1380+
} else {
1381+
fields["status"] = hexutil.Uint(receipt.Status)
1382+
}
1383+
if receipt.Logs == nil {
1384+
fields["logs"] = [][]*types.Log{}
1385+
}
1386+
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
1387+
if receipt.ContractAddress != (common.Address{}) {
1388+
fields["contractAddress"] = receipt.ContractAddress
1389+
}
1390+
1391+
txReceipts = append(txReceipts, fields)
1392+
}
1393+
1394+
return txReceipts, nil
1395+
}
1396+
1397+
// GetTransactionDataAndReceipt returns the original transaction data and transaction receipt for the given transaction hash.
1398+
func (s *PublicTransactionPoolAPI) GetTransactionDataAndReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
1399+
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash)
1400+
if tx == nil {
1401+
return nil, nil
1402+
}
1403+
receipts, err := s.b.GetReceipts(ctx, blockHash)
1404+
if err != nil {
1405+
return nil, err
1406+
}
1407+
if len(receipts) <= int(index) {
1408+
return nil, nil
1409+
}
1410+
receipt := receipts[index]
1411+
1412+
var signer types.Signer = types.FrontierSigner{}
1413+
if tx.Protected() {
1414+
signer = types.NewEIP155Signer(tx.ChainId())
1415+
}
1416+
from, _ := types.Sender(signer, tx)
1417+
1418+
rpcTransaction := newRPCTransaction(tx, blockHash, blockNumber, index)
1419+
1420+
txData := map[string]interface{}{
1421+
"blockHash": rpcTransaction.BlockHash.String(),
1422+
"blockNumber": rpcTransaction.BlockNumber.String(),
1423+
"from": rpcTransaction.From.String(),
1424+
"gas": rpcTransaction.Gas.String(),
1425+
"gasPrice": rpcTransaction.GasPrice.String(),
1426+
"hash": rpcTransaction.Hash.String(),
1427+
"input": rpcTransaction.Input.String(),
1428+
"nonce": rpcTransaction.Nonce.String(),
1429+
"to": rpcTransaction.To.String(),
1430+
"transactionIndex": rpcTransaction.TransactionIndex.String(),
1431+
"value": rpcTransaction.Value.String(),
1432+
"v": rpcTransaction.V.String(),
1433+
"r": rpcTransaction.R.String(),
1434+
"s": rpcTransaction.S.String(),
1435+
}
1436+
1437+
fields := map[string]interface{}{
1438+
"blockHash": blockHash,
1439+
"blockNumber": hexutil.Uint64(blockNumber),
1440+
"transactionHash": hash,
1441+
"transactionIndex": hexutil.Uint64(index),
1442+
"from": from,
1443+
"to": tx.To(),
1444+
"gasUsed": hexutil.Uint64(receipt.GasUsed),
1445+
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
1446+
"contractAddress": nil,
1447+
"logs": receipt.Logs,
1448+
"logsBloom": receipt.Bloom,
1449+
}
1450+
1451+
// Assign receipt status or post state.
1452+
if len(receipt.PostState) > 0 {
1453+
fields["root"] = hexutil.Bytes(receipt.PostState)
1454+
} else {
1455+
fields["status"] = hexutil.Uint(receipt.Status)
1456+
}
1457+
if receipt.Logs == nil {
1458+
fields["logs"] = [][]*types.Log{}
1459+
}
1460+
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
1461+
if receipt.ContractAddress != (common.Address{}) {
1462+
fields["contractAddress"] = receipt.ContractAddress
1463+
}
1464+
result := map[string]interface{}{
1465+
"txData": txData,
1466+
"receipt": fields,
1467+
}
1468+
return result, nil
1469+
}
1470+
13171471
// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
13181472
func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
13191473
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash)

‎internal/jsre/deps/bindata.go‎

Lines changed: 13 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/jsre/deps/web3.js‎

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)