From 77d28264f7c59cc0260150049d52fc6961a7153d Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Mon, 20 Sep 2021 16:01:27 +0530 Subject: [PATCH] Address comments. --- pkg/eth/cid_retriever.go | 2 +- pkg/eth/filterer.go | 26 +++++++------- pkg/eth/ipld_retriever.go | 22 ++++++++---- pkg/eth/test_helpers.go | 2 +- pkg/eth/test_helpers/test_data.go | 60 +++++++++++++++---------------- 5 files changed, 60 insertions(+), 52 deletions(-) diff --git a/pkg/eth/cid_retriever.go b/pkg/eth/cid_retriever.go index 82c75df6..4b3a193b 100644 --- a/pkg/eth/cid_retriever.go +++ b/pkg/eth/cid_retriever.go @@ -321,7 +321,7 @@ func (ecr *CIDRetriever) RetrieveFilteredGQLLogs(tx *sqlx.Tx, rctFilter ReceiptF WHERE eth.log_cids.receipt_id = receipt_cids.id AND receipt_cids.tx_id = transaction_cids.id AND transaction_cids.header_id = header_cids.id - AND receipt_cids.leaf_mh_key = blocks.key AND header_cids.block_hash = $1` + AND log_cids.leaf_mh_key = blocks.key AND header_cids.block_hash = $1` args = append(args, blockHash.String()) id++ diff --git a/pkg/eth/filterer.go b/pkg/eth/filterer.go index b9dac454..3f258e37 100644 --- a/pkg/eth/filterer.go +++ b/pkg/eth/filterer.go @@ -165,7 +165,11 @@ func checkTransactionAddrs(wantedSrc, wantedDst []string, actualSrc, actualDst s func (s *ResponseFilterer) filerReceipts(receiptFilter ReceiptFilter, response *IPLDs, payload ConvertedPayload, trxHashes []common.Hash) error { if !receiptFilter.Off { response.Receipts = make([]ipfs.BlockModel, 0, len(payload.Receipts)) - rctLeafCID, rctIPLDData := FetchRctLeafNodeData(payload.Receipts) + rctLeafCID, rctIPLDData, err := GetRctLeafNodeData(payload.Receipts) + if err != nil { + return err + } + for idx, receipt := range payload.Receipts { // topics is always length 4 topics := make([][]string, 4) @@ -323,27 +327,23 @@ func checkNodeKeys(wantedKeys []common.Hash, actualKey []byte) bool { return false } -// FetchRctLeafNodeData fetches receipt leaf node IPLD data and CIDs -func FetchRctLeafNodeData(rcts types.Receipts) ([]cid.Cid, [][]byte) { +// GetRctLeafNodeData converts the receipts to receipt trie and returns the receipt leaf node IPLD data and +// corresponding CIDs +func GetRctLeafNodeData(rcts types.Receipts) ([]cid.Cid, [][]byte, error) { receiptTrie := ipld.NewRctTrie() for idx, rct := range rcts { ethRct, err := ipld.NewReceipt(rct) if err != nil { - return nil, nil + return nil, nil, err } if err = receiptTrie.Add(idx, ethRct.RawData()); err != nil { - return nil, nil + return nil, nil, err } } - _, err := receiptTrie.GetNodes() - if err != nil { - return nil, nil - } - rctLeafNodes, keys, err := receiptTrie.GetLeafNodes() if err != nil { - return nil, nil + return nil, nil, err } ethRctleafNodeCids := make([]cid.Cid, len(rctLeafNodes)) @@ -354,12 +354,12 @@ func FetchRctLeafNodeData(rcts types.Receipts) ([]cid.Cid, [][]byte) { r := bytes.NewReader(keys[i].TrieKey) err = rlp.Decode(r, &idx) if err != nil { - return nil, nil + return nil, nil, err } ethRctleafNodeCids[idx] = rln.Cid() ethRctleafNodeData[idx] = rln.RawData() } - return ethRctleafNodeCids, ethRctleafNodeData + return ethRctleafNodeCids, ethRctleafNodeData, nil } diff --git a/pkg/eth/ipld_retriever.go b/pkg/eth/ipld_retriever.go index 6609865d..38f1b0ae 100644 --- a/pkg/eth/ipld_retriever.go +++ b/pkg/eth/ipld_retriever.go @@ -361,12 +361,12 @@ func (r *IPLDRetriever) RetrieveReceiptsByTxHashes(hashes []common.Hash) ([]stri return nil, nil, err } rcts[i] = nodeVal - //rcts[i] = res.Data } return cids, rcts, nil } -// RetrieveReceiptsByBlockHash returns the cids and rlp bytes for the receipts corresponding to the provided block hash +// RetrieveReceiptsByBlockHash returns the cids and rlp bytes for the receipts corresponding to the provided block hash. +// cid returned corresponds to the leaf node data which contains the receipt. func (r *IPLDRetriever) RetrieveReceiptsByBlockHash(hash common.Hash) ([]string, [][]byte, []common.Hash, error) { rctResults := make([]rctIpldResult, 0) if err := r.db.Select(&rctResults, RetrieveReceiptsByBlockHashPgStr, hash.Hex()); err != nil { @@ -383,14 +383,14 @@ func (r *IPLDRetriever) RetrieveReceiptsByBlockHash(hash common.Hash) ([]string, return nil, nil, nil, err } rcts[i] = nodeVal - //rcts[i] = res.Data txs[i] = common.HexToHash(res.TxHash) } return cids, rcts, txs, nil } -// RetrieveReceiptsByBlockNumber returns the cids and rlp bytes for the receipts corresponding to the provided block hash +// RetrieveReceiptsByBlockNumber returns the cids and rlp bytes for the receipts corresponding to the provided block hash. +// cid returned corresponds to the leaf node data which contains the receipt. func (r *IPLDRetriever) RetrieveReceiptsByBlockNumber(number uint64) ([]string, [][]byte, error) { rctResults := make([]rctIpldResult, 0) if err := r.db.Select(&rctResults, RetrieveReceiptsByBlockNumberPgStr, number); err != nil { @@ -405,15 +405,23 @@ func (r *IPLDRetriever) RetrieveReceiptsByBlockNumber(number uint64) ([]string, return nil, nil, err } rcts[i] = nodeVal - //rcts[i] = res.Data } return cids, rcts, nil } -// RetrieveReceiptByHash returns the cid and rlp bytes for the receipt corresponding to the provided tx hash +// RetrieveReceiptByHash returns the cid and rlp bytes for the receipt corresponding to the provided tx hash. +// cid returned corresponds to the leaf node data which contains the receipt. func (r *IPLDRetriever) RetrieveReceiptByHash(hash common.Hash) (string, []byte, error) { rctResult := new(rctIpldResult) - return rctResult.LeafCID, rctResult.Data, r.db.Get(rctResult, RetrieveReceiptByTxHashPgStr, hash.Hex()) + if err := r.db.Select(&rctResult, RetrieveReceiptByTxHashPgStr, hash.Hex()); err != nil { + return "", nil, err + } + + nodeVal, err := DecodeLeafNode(rctResult.Data) + if err != nil { + return "", nil, err + } + return rctResult.LeafCID, nodeVal, nil } type nodeInfo struct { diff --git a/pkg/eth/test_helpers.go b/pkg/eth/test_helpers.go index 491f837b..8c4557db 100644 --- a/pkg/eth/test_helpers.go +++ b/pkg/eth/test_helpers.go @@ -56,7 +56,7 @@ func TxModelsContainsCID(txs []models.TxModel, cid string) bool { return false } -// ListContainsBytes used to check if a list of byte arrays contains a particular byte array +// ReceiptModelsContainsCID used to check if a list of ReceiptModel contains a specific cid string func ReceiptModelsContainsCID(rcts []models.ReceiptModel, cid string) bool { for _, rct := range rcts { if rct.LeafCID == cid { diff --git a/pkg/eth/test_helpers/test_data.go b/pkg/eth/test_helpers/test_data.go index d67f607d..72cf3184 100644 --- a/pkg/eth/test_helpers/test_data.go +++ b/pkg/eth/test_helpers/test_data.go @@ -167,36 +167,36 @@ var ( Tx3 = GetTxnRlp(2, MockTransactions) Tx4 = GetTxnRlp(3, MockTransactions) - rctCIDs, rctIPLDData = eth.FetchRctLeafNodeData(MockReceipts) - HeaderCID, _ = ipld.RawdataToCid(ipld.MEthHeader, MockHeaderRlp, multihash.KECCAK_256) - HeaderMhKey = shared.MultihashKeyFromCID(HeaderCID) - Trx1CID, _ = ipld.RawdataToCid(ipld.MEthTx, Tx1, multihash.KECCAK_256) - Trx1MhKey = shared.MultihashKeyFromCID(Trx1CID) - Trx2CID, _ = ipld.RawdataToCid(ipld.MEthTx, Tx2, multihash.KECCAK_256) - Trx2MhKey = shared.MultihashKeyFromCID(Trx2CID) - Trx3CID, _ = ipld.RawdataToCid(ipld.MEthTx, Tx3, multihash.KECCAK_256) - Trx3MhKey = shared.MultihashKeyFromCID(Trx3CID) - Trx4CID, _ = ipld.RawdataToCid(ipld.MEthTx, Tx4, multihash.KECCAK_256) - Trx4MhKey = shared.MultihashKeyFromCID(Trx4CID) - Rct1CID = rctCIDs[0] - Rct1MhKey = shared.MultihashKeyFromCID(Rct1CID) - Rct2CID = rctCIDs[1] - Rct2MhKey = shared.MultihashKeyFromCID(Rct2CID) - Rct3CID = rctCIDs[2] - Rct3MhKey = shared.MultihashKeyFromCID(Rct3CID) - Rct4CID = rctCIDs[3] - Rct4MhKey = shared.MultihashKeyFromCID(Rct4CID) - State1CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, ContractLeafNode, multihash.KECCAK_256) - State1MhKey = shared.MultihashKeyFromCID(State1CID) - State2CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, AccountLeafNode, multihash.KECCAK_256) - State2MhKey = shared.MultihashKeyFromCID(State2CID) - StorageCID, _ = ipld.RawdataToCid(ipld.MEthStorageTrie, StorageLeafNode, multihash.KECCAK_256) - StorageMhKey = shared.MultihashKeyFromCID(StorageCID) - Rct1IPLD = rctIPLDData[0] - Rct2IPLD = rctIPLDData[1] - Rct3IPLD = rctIPLDData[2] - Rct4IPLD = rctIPLDData[3] - MockTrxMeta = []models.TxModel{ + rctCIDs, rctIPLDData, _ = eth.GetRctLeafNodeData(MockReceipts) + HeaderCID, _ = ipld.RawdataToCid(ipld.MEthHeader, MockHeaderRlp, multihash.KECCAK_256) + HeaderMhKey = shared.MultihashKeyFromCID(HeaderCID) + Trx1CID, _ = ipld.RawdataToCid(ipld.MEthTx, Tx1, multihash.KECCAK_256) + Trx1MhKey = shared.MultihashKeyFromCID(Trx1CID) + Trx2CID, _ = ipld.RawdataToCid(ipld.MEthTx, Tx2, multihash.KECCAK_256) + Trx2MhKey = shared.MultihashKeyFromCID(Trx2CID) + Trx3CID, _ = ipld.RawdataToCid(ipld.MEthTx, Tx3, multihash.KECCAK_256) + Trx3MhKey = shared.MultihashKeyFromCID(Trx3CID) + Trx4CID, _ = ipld.RawdataToCid(ipld.MEthTx, Tx4, multihash.KECCAK_256) + Trx4MhKey = shared.MultihashKeyFromCID(Trx4CID) + Rct1CID = rctCIDs[0] + Rct1MhKey = shared.MultihashKeyFromCID(Rct1CID) + Rct2CID = rctCIDs[1] + Rct2MhKey = shared.MultihashKeyFromCID(Rct2CID) + Rct3CID = rctCIDs[2] + Rct3MhKey = shared.MultihashKeyFromCID(Rct3CID) + Rct4CID = rctCIDs[3] + Rct4MhKey = shared.MultihashKeyFromCID(Rct4CID) + State1CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, ContractLeafNode, multihash.KECCAK_256) + State1MhKey = shared.MultihashKeyFromCID(State1CID) + State2CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, AccountLeafNode, multihash.KECCAK_256) + State2MhKey = shared.MultihashKeyFromCID(State2CID) + StorageCID, _ = ipld.RawdataToCid(ipld.MEthStorageTrie, StorageLeafNode, multihash.KECCAK_256) + StorageMhKey = shared.MultihashKeyFromCID(StorageCID) + Rct1IPLD = rctIPLDData[0] + Rct2IPLD = rctIPLDData[1] + Rct3IPLD = rctIPLDData[2] + Rct4IPLD = rctIPLDData[3] + MockTrxMeta = []models.TxModel{ { CID: "", // This is empty until we go to publish to ipfs MhKey: "",