From 9550d6046792a750a8728220f5baabcbe44ca3bb Mon Sep 17 00:00:00 2001 From: nabarun Date: Mon, 30 May 2022 12:01:27 +0530 Subject: [PATCH] Add query ethTransactionCidByTxHash --- pkg/eth/cid_retriever.go | 32 +++++++++++++++++++++++++++ pkg/graphql/graphql.go | 48 +++++++++++++++++++++++++++++++++++----- pkg/graphql/schema.go | 4 ++++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/pkg/eth/cid_retriever.go b/pkg/eth/cid_retriever.go index dc77608c..81190809 100644 --- a/pkg/eth/cid_retriever.go +++ b/pkg/eth/cid_retriever.go @@ -593,6 +593,7 @@ func (ecr *CIDRetriever) RetrieveTxCIDsByHeaderID(tx *sqlx.Tx, headerID string) return txCIDs, tx.Select(&txCIDs, pgStr, headerID) } +// RetrieveTxCIDsByBlockNumber retrieves all tx CIDs for the given blockNumber func (ecr *CIDRetriever) RetrieveTxCIDsByBlockNumber(tx *sqlx.Tx, blockNumber int64) ([]models.TxModel, error) { log.Debug("retrieving tx cids for block number ", blockNumber) pgStr := `SELECT CAST(block_number as Text), header_id, index, tx_hash, cid, mh_key, @@ -617,6 +618,7 @@ func (ecr *CIDRetriever) RetrieveReceiptCIDsByTxIDs(tx *sqlx.Tx, txHashes []stri return rctCIDs, tx.Select(&rctCIDs, pgStr, pq.Array(txHashes)) } +// RetrieveHeaderAndTxCIDsByBlockNumber retrieves header CIDs and their associated tx CIDs by block number func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64) ([]models.HeaderModel, [][]models.TxModel, error) { log.Debug("retrieving header cids and tx cids for block number ", blockNumber) @@ -677,6 +679,7 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64) return headerCIDs, allTxCIDs, nil } +// RetrieveHeaderAndTxCIDsByBlockHash retrieves header CID and their associated tx CIDs by block hash func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Hash) (models.HeaderModel, []models.TxModel, error) { log.Debug("retrieving header cid and tx cids for block hash ", blockHash.String()) @@ -715,3 +718,32 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Has return headerCID, txCIDs, nil } + +// RetrieveTxCIDByHash returns the tx for the given tx hash +func (ecr *CIDRetriever) RetrieveTxCIDByHash(txHash string) (models.TxModel, error) { + log.Debug("retrieving tx cid for tx hash ", txHash) + + // Begin new db tx + tx, err := ecr.db.Beginx() + if err != nil { + return models.TxModel{}, err + } + defer func() { + if p := recover(); p != nil { + shared.Rollback(tx) + panic(p) + } else if err != nil { + shared.Rollback(tx) + } else { + err = tx.Commit() + } + }() + + pgStr := `SELECT CAST(block_number as Text), header_id, index, tx_hash, cid, mh_key, + dst, src, tx_data, tx_type, value + FROM eth.transaction_cids + WHERE tx_hash = $1 + ORDER BY index` + var txCID models.TxModel + return txCID, tx.Get(&txCID, pgStr, txHash) +} diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index 2de90c46..c5553993 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -1126,11 +1126,12 @@ func decomposeGQLLogs(logCIDs []eth.LogResult) []logsCID { } type EthTransactionCid struct { - cid string - txHash string - index int32 - src string - dst string + cid string + txHash string + index int32 + src string + dst string + ipfsBlock IPFSBlock } func (t EthTransactionCid) Cid(ctx context.Context) string { @@ -1153,6 +1154,10 @@ func (t EthTransactionCid) Dst(ctx context.Context) string { return t.dst } +func (t EthTransactionCid) BlockByMhKey(ctx context.Context) IPFSBlock { + return t.ipfsBlock +} + type EthTransactionCidsConnection struct { nodes []*EthTransactionCid } @@ -1337,3 +1342,36 @@ func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct { nodes: resultNodes, }, nil } + +func (r *Resolver) EthTransactionCidByTxHash(ctx context.Context, args struct { + TxHash string +}) (*EthTransactionCid, error) { + txCID, err := r.backend.Retriever.RetrieveTxCIDByHash(args.TxHash) + + if err != nil { + return nil, err + } + + // Begin tx + tx, err := r.backend.DB.Beginx() + if err != nil { + return nil, err + } + + txIPLDs, err := r.backend.Fetcher.FetchTrxs(tx, []models.TxModel{txCID}) + if err != nil { + return nil, err + } + + return &EthTransactionCid{ + cid: txCID.CID, + txHash: txCID.TxHash, + index: int32(txCID.Index), + src: txCID.Src, + dst: txCID.Dst, + ipfsBlock: IPFSBlock{ + key: txIPLDs[0].Key, + data: hexutil.Bytes(txIPLDs[0].Data).String(), + }, + }, nil +} diff --git a/pkg/graphql/schema.go b/pkg/graphql/schema.go index 2a25043e..48a2309c 100644 --- a/pkg/graphql/schema.go +++ b/pkg/graphql/schema.go @@ -294,6 +294,7 @@ const schema string = ` index: Int! src: String! dst: String! + blockByMhKey: IPFSBlock! } type EthTransactionCidsConnection { @@ -351,5 +352,8 @@ const schema string = ` # PostGraphile alternative to get headers with transactions using block number or block hash. allEthHeaderCids(condition: EthHeaderCidCondition): EthHeaderCidsConnection + + # PostGraphile alternative to get transactions using transaction hash. + ethTransactionCidByTxHash(txHash: String!): EthTransactionCid } `