From b3d9e01d67a8cffe9ad8b38daec77834ec31bceb Mon Sep 17 00:00:00 2001 From: nabarun Date: Wed, 1 Jun 2022 16:45:02 +0530 Subject: [PATCH] Add test for ethTransactionCidByTxHash query --- pkg/graphql/client.go | 46 +++++++++++++++++++++++++++++++-- pkg/graphql/graphql_test.go | 51 +++++++++++++++++++++++++++++++------ 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/pkg/graphql/client.go b/pkg/graphql/client.go index 255c0951..53420db8 100644 --- a/pkg/graphql/client.go +++ b/pkg/graphql/client.go @@ -50,6 +50,10 @@ type EthTransactionCidResp struct { BlockByMhKey IPFSBlockResp `json:"blockByMhKey"` } +type EthTransactionCidByTxHash struct { + Response EthTransactionCidResp `json:"ethTransactionCidByTxHash"` +} + type EthTransactionCidsByHeaderIdResp struct { Nodes []EthTransactionCidResp `json:"nodes"` } @@ -169,7 +173,7 @@ func (c *Client) AllEthHeaderCids(ctx context.Context, condition EthHeaderCidCon params += fmt.Sprintf(`blockNumber: "%s"`, condition.BlockNumber.String()) } - getLogsQuery := fmt.Sprintf(` + getHeadersQuery := fmt.Sprintf(` query{ allEthHeaderCids(condition: { %s }) { nodes { @@ -202,7 +206,7 @@ func (c *Client) AllEthHeaderCids(ctx context.Context, condition EthHeaderCidCon } `, params) - req := gqlclient.NewRequest(getLogsQuery) + req := gqlclient.NewRequest(getHeadersQuery) req.Header.Set("Cache-Control", "no-cache") var respData map[string]interface{} @@ -223,3 +227,41 @@ func (c *Client) AllEthHeaderCids(ctx context.Context, condition EthHeaderCidCon } return &allEthHeaderCids.Response, nil } + +func (c *Client) EthTransactionCidByTxHash(ctx context.Context, txHash string) (*EthTransactionCidResp, error) { + getTxQuery := fmt.Sprintf(` + query{ + ethTransactionCidByTxHash(txHash: "%s") { + cid + txHash + index + src + dst + blockByMhKey { + data + } + } + } + `, txHash) + + req := gqlclient.NewRequest(getTxQuery) + req.Header.Set("Cache-Control", "no-cache") + + var respData map[string]interface{} + err := c.client.Run(ctx, req, &respData) + if err != nil { + return nil, err + } + + jsonStr, err := json.Marshal(respData) + if err != nil { + return nil, err + } + + var ethTxCid EthTransactionCidByTxHash + err = json.Unmarshal(jsonStr, ðTxCid) + if err != nil { + return nil, err + } + return ðTxCid.Response, nil +} diff --git a/pkg/graphql/graphql_test.go b/pkg/graphql/graphql_test.go index 031ddb70..fc8259a4 100644 --- a/pkg/graphql/graphql_test.go +++ b/pkg/graphql/graphql_test.go @@ -286,11 +286,11 @@ var _ = Describe("GraphQL", func() { }) It("Retrieves header_cids that matches the provided blockHash", func() { - blockHash := blocks[2].Hash().String() + blockHash := blocks[1].Hash().String() allEthHeaderCidsResp, err := client.AllEthHeaderCids(ctx, graphql.EthHeaderCidCondition{BlockHash: &blockHash}) Expect(err).ToNot(HaveOccurred()) - headerCID, txCIDs, err := backend.Retriever.RetrieveHeaderAndTxCIDsByBlockHash(blocks[2].Hash()) + headerCID, txCIDs, err := backend.Retriever.RetrieveHeaderAndTxCIDsByBlockHash(blocks[1].Hash()) Expect(err).ToNot(HaveOccurred()) // Begin tx @@ -316,6 +316,38 @@ var _ = Describe("GraphQL", func() { compareEthHeaderCid(ethHeaderCid, headerCID, txCIDs, headerIPLDs[0]) }) }) + + Describe("ethTransactionCidByTxHash", func() { + It("Retrieves tx_cid that matches the provided txHash", func() { + txHash := blocks[2].Transactions()[0].Hash().String() + ethTransactionCidResp, err := client.EthTransactionCidByTxHash(ctx, txHash) + Expect(err).ToNot(HaveOccurred()) + + txCID, err := backend.Retriever.RetrieveTxCIDByHash(txHash) + Expect(err).ToNot(HaveOccurred()) + + compareEthTxCid(*ethTransactionCidResp, txCID) + + // Begin tx + tx, err := backend.DB.Beginx() + Expect(err).ToNot(HaveOccurred()) + defer func() { + if p := recover(); p != nil { + shared.Rollback(tx) + panic(p) + } else if err != nil { + shared.Rollback(tx) + } else { + err = tx.Commit() + } + }() + + txIPLDs, err := backend.Fetcher.FetchTrxs(tx, []models.TxModel{txCID}) + Expect(err).ToNot(HaveOccurred()) + Expect(len(txIPLDs)).To(Equal(1)) + Expect(ethTransactionCidResp.BlockByMhKey.Data).To(Equal(graphql.Bytes(txIPLDs[0].Data).String())) + }) + }) }) func compareEthHeaderCid(ethHeaderCid graphql.EthHeaderCidResp, headerCID models.HeaderModel, txCIDs []models.TxModel, headerIPLD models.IPLDModel) { @@ -339,14 +371,17 @@ func compareEthHeaderCid(ethHeaderCid graphql.EthHeaderCidResp, headerCID models for tIdx, txCID := range txCIDs { ethTxCid := ethHeaderCid.EthTransactionCidsByHeaderId.Nodes[tIdx] - - Expect(ethTxCid.Cid).To(Equal(txCID.CID)) - Expect(ethTxCid.TxHash).To(Equal(txCID.TxHash)) - Expect(ethTxCid.Index).To(Equal(int32(txCID.Index))) - Expect(ethTxCid.Src).To(Equal(txCID.Src)) - Expect(ethTxCid.Dst).To(Equal(txCID.Dst)) + compareEthTxCid(ethTxCid, txCID) } Expect(ethHeaderCid.BlockByMhKey.Data).To(Equal(graphql.Bytes(headerIPLD.Data).String())) Expect(ethHeaderCid.BlockByMhKey.Key).To(Equal(headerIPLD.Key)) } + +func compareEthTxCid(ethTxCid graphql.EthTransactionCidResp, txCID models.TxModel) { + Expect(ethTxCid.Cid).To(Equal(txCID.CID)) + Expect(ethTxCid.TxHash).To(Equal(txCID.TxHash)) + Expect(ethTxCid.Index).To(Equal(int32(txCID.Index))) + Expect(ethTxCid.Src).To(Equal(txCID.Src)) + Expect(ethTxCid.Dst).To(Equal(txCID.Dst)) +}