Optimize GQL and getBlock APIs #201

Merged
prathamesh0 merged 8 commits from pm-optimize-quries into v4 2022-11-04 04:32:09 +00:00
4 changed files with 15 additions and 9 deletions
Showing only changes of commit 4d1873eb65 - Show all commits

View File

@ -712,7 +712,7 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64)
return headerCIDs, nil return headerCIDs, nil
} }
// RetrieveHeaderAndTxCIDsByBlockHash retrieves header CID and their associated tx CIDs by block hash // RetrieveHeaderAndTxCIDsByBlockHash retrieves header CID and their associated tx CIDs by block hash (and optionally block number)
func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Hash, blockNumber *big.Int) (HeaderCIDRecord, error) { func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Hash, blockNumber *big.Int) (HeaderCIDRecord, error) {
log.Debug("retrieving header cid and tx cids for block hash ", blockHash.String()) log.Debug("retrieving header cid and tx cids for block hash ", blockHash.String())
@ -743,15 +743,20 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Has
return headerCIDs[0], nil return headerCIDs[0], nil
} }
// RetrieveTxCIDByHash returns the tx for the given tx hash // RetrieveTxCIDByHash returns the tx for the given tx hash (and optionally block number)
func (ecr *CIDRetriever) RetrieveTxCIDByHash(txHash string) (TransactionCIDRecord, error) { func (ecr *CIDRetriever) RetrieveTxCIDByHash(txHash string, blockNumber *big.Int) (TransactionCIDRecord, error) {
log.Debug("retrieving tx cid for tx hash ", txHash) log.Debug("retrieving tx cid for tx hash ", txHash)
var txCIDs []TransactionCIDRecord var txCIDs []TransactionCIDRecord
err := ecr.gormDB.Joins("IPLD").Find(&txCIDs, "tx_hash = ? AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number))", txHash).Error var err error
if blockNumber != nil {
err = ecr.gormDB.Joins("IPLD").Find(&txCIDs, "tx_hash = ? AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number)) AND transaction_cids.block_number = ?", txHash, blockNumber.Int64()).Error
} else {
err = ecr.gormDB.Joins("IPLD").Find(&txCIDs, "tx_hash = ? AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number))", txHash).Error
}
if err != nil { if err != nil {
log.Error("header cid retrieval error") log.Error("tx retrieval error")
return TransactionCIDRecord{}, err return TransactionCIDRecord{}, err
} }

View File

@ -1354,8 +1354,9 @@ func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct {
func (r *Resolver) EthTransactionCidByTxHash(ctx context.Context, args struct { func (r *Resolver) EthTransactionCidByTxHash(ctx context.Context, args struct {
TxHash string TxHash string
BlockNumber *BigInt
}) (*EthTransactionCID, error) { }) (*EthTransactionCID, error) {
txCID, err := r.backend.Retriever.RetrieveTxCIDByHash(args.TxHash) txCID, err := r.backend.Retriever.RetrieveTxCIDByHash(args.TxHash, args.BlockNumber.ToInt())
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -311,7 +311,7 @@ var _ = Describe("GraphQL", func() {
ethTransactionCIDResp, err := client.EthTransactionCIDByTxHash(ctx, txHash) ethTransactionCIDResp, err := client.EthTransactionCIDByTxHash(ctx, txHash)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
txCID, err := backend.Retriever.RetrieveTxCIDByHash(txHash) txCID, err := backend.Retriever.RetrieveTxCIDByHash(txHash, nil)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
compareEthTxCID(*ethTransactionCIDResp, txCID) compareEthTxCID(*ethTransactionCIDResp, txCID)

View File

@ -349,6 +349,6 @@ const schema string = `
allEthHeaderCids(condition: EthHeaderCidCondition): EthHeaderCidsConnection allEthHeaderCids(condition: EthHeaderCidCondition): EthHeaderCidsConnection
# PostGraphile alternative to get transactions using transaction hash. # PostGraphile alternative to get transactions using transaction hash.
ethTransactionCidByTxHash(txHash: String!): EthTransactionCid ethTransactionCidByTxHash(txHash: String!, blockNumber: BigInt): EthTransactionCid
} }
` `