From 706dfcab8fd1c028372dbba0a6204b3762e4aa9d Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Wed, 2 Nov 2022 17:56:10 +0530 Subject: [PATCH] Use block number while fetching block with transactions --- pkg/eth/cid_retriever.go | 9 +++++++-- pkg/graphql/graphql.go | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/eth/cid_retriever.go b/pkg/eth/cid_retriever.go index 96d50a62..c9d65087 100644 --- a/pkg/eth/cid_retriever.go +++ b/pkg/eth/cid_retriever.go @@ -713,16 +713,21 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64) } // RetrieveHeaderAndTxCIDsByBlockHash retrieves header CID and their associated tx CIDs by block hash -func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Hash) (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()) var headerCIDs []HeaderCIDRecord + conditions := map[string]interface{}{"block_hash": blockHash.String()} + if blockNumber != nil { + conditions["header_cids.block_number"] = blockNumber.Int64() + } + // https://github.com/go-gorm/gorm/issues/4083#issuecomment-778883283 // Will use join for TransactionCIDs once preload for 1:N is supported. err := ecr.gormDB.Preload("TransactionCIDs", func(tx *gorm.DB) *gorm.DB { return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id", "block_number") - }).Joins("IPLD").Find(&headerCIDs, "block_hash = ?", blockHash.String()).Error + }).Joins("IPLD").Find(&headerCIDs, conditions).Error if err != nil { log.Error("header cid retrieval error") diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index 2b203813..8b4e1972 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -1272,7 +1272,7 @@ func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct { var headerCIDs []eth.HeaderCIDRecord var err error if args.Condition.BlockHash != nil { - headerCID, err := r.backend.Retriever.RetrieveHeaderAndTxCIDsByBlockHash(common.HexToHash(*args.Condition.BlockHash)) + headerCID, err := r.backend.Retriever.RetrieveHeaderAndTxCIDsByBlockHash(common.HexToHash(*args.Condition.BlockHash), args.Condition.BlockNumber.ToInt()) if err != nil { if !strings.Contains(err.Error(), "not found") { return nil, err