From 45f6ab566059d8a67c9c5192a7dc83819c6614bd Mon Sep 17 00:00:00 2001 From: Wang Gerui Date: Wed, 18 Oct 2023 23:51:48 +0800 Subject: [PATCH] merge two API into GetTransactionAndReceiptProof --- internal/ethapi/api.go | 48 +++++++++++++------------------------ internal/web3ext/web3ext.go | 9 ++----- 2 files changed, 18 insertions(+), 39 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index e676fbb23..a3addf16b 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -534,8 +534,8 @@ func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address common.Add return b, state.Error() } -// GetTransactionProof returns the Trie proof of the given transaction. -func (s *PublicBlockChainAPI) GetTransactionProof(ctx context.Context, hash common.Hash) (map[string]interface{}, error) { +// GetTransactionAndReceiptProof returns the Trie transaction and receipt proof of the given transaction hash. +func (s *PublicBlockChainAPI) GetTransactionAndReceiptProof(ctx context.Context, hash common.Hash) (map[string]interface{}, error) { tx, blockHash, _, index := core.GetTransaction(s.b.ChainDb(), hash) if tx == nil { return nil, nil @@ -544,30 +544,14 @@ func (s *PublicBlockChainAPI) GetTransactionProof(ctx context.Context, hash comm if err != nil { return nil, err } - tr := deriveTrie(block.Transactions()) + tx_tr := deriveTrie(block.Transactions()) - var proof proofPairList keybuf := new(bytes.Buffer) rlp.Encode(keybuf, uint(index)) - if err := tr.Prove(keybuf.Bytes(), 0, &proof); err != nil { + var tx_proof proofPairList + if err := tx_tr.Prove(keybuf.Bytes(), 0, &tx_proof); err != nil { return nil, err } - fields := map[string]interface{}{ - "blockHash": blockHash, - "txRoot": tr.Hash(), - "key": hexutil.Encode(keybuf.Bytes()), - "proofKeys": proof.keys, - "proofValues": proof.values, - } - return fields, nil -} - -// GetReceiptProof returns the Trie proof of the receipt for a given transaction. -func (s *PublicBlockChainAPI) GetReceiptProof(ctx context.Context, hash common.Hash) (map[string]interface{}, error) { - tx, blockHash, _, index := core.GetTransaction(s.b.ChainDb(), hash) - if tx == nil { - return nil, nil - } receipts, err := s.b.GetReceipts(ctx, blockHash) if err != nil { return nil, err @@ -575,20 +559,20 @@ func (s *PublicBlockChainAPI) GetReceiptProof(ctx context.Context, hash common.H if len(receipts) <= int(index) { return nil, nil } - tr := deriveTrie(receipts) - - var proof proofPairList - keybuf := new(bytes.Buffer) - rlp.Encode(keybuf, uint(index)) - if err := tr.Prove(keybuf.Bytes(), 0, &proof); err != nil { + receipt_tr := deriveTrie(receipts) + var receipt_proof proofPairList + if err := receipt_tr.Prove(keybuf.Bytes(), 0, &receipt_proof); err != nil { return nil, err } fields := map[string]interface{}{ - "blockHash": blockHash, - "receiptRoot": tr.Hash(), - "key": hexutil.Encode(keybuf.Bytes()), - "proofKeys": proof.keys, - "proofValues": proof.values, + "blockHash": blockHash, + "txRoot": tx_tr.Hash(), + "receiptRoot": receipt_tr.Hash(), + "key": hexutil.Encode(keybuf.Bytes()), + "txProofKeys": tx_proof.keys, + "txProofValues": tx_proof.values, + "receiptProofKeys": receipt_proof.keys, + "receiptProofValues": receipt_proof.values, } return fields, nil } diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 184cb5812..1f08ff05b 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -491,13 +491,8 @@ web3._extend({ params: 1 }), new web3._extend.Method({ - name: 'getTransactionProof', - call: 'eth_getTransactionProof', - params: 1 - }), - new web3._extend.Method({ - name: 'getReceiptProof', - call: 'eth_getReceiptProof', + name: 'getTransactionAndReceiptProof', + call: 'eth_getTransactionAndReceiptProof', params: 1 }), new web3._extend.Method({