From c10486bba079731b6ee238e2cffc207cac8c8d30 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Wed, 2 Nov 2022 13:15:09 +0530 Subject: [PATCH] Add optional block number filter to getLogs API --- pkg/eth/cid_retriever.go | 8 +++++++- pkg/graphql/graphql.go | 7 ++++--- pkg/graphql/schema.go | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/eth/cid_retriever.go b/pkg/eth/cid_retriever.go index d2f12ecf..96d50a62 100644 --- a/pkg/eth/cid_retriever.go +++ b/pkg/eth/cid_retriever.go @@ -360,7 +360,7 @@ func receiptFilterConditions(id *int, pgStr string, args []interface{}, rctFilte // RetrieveFilteredGQLLogs retrieves and returns all the log CIDs provided blockHash that conform to the provided // filter parameters. -func (ecr *CIDRetriever) RetrieveFilteredGQLLogs(tx *sqlx.Tx, rctFilter ReceiptFilter, blockHash *common.Hash) ([]LogResult, error) { +func (ecr *CIDRetriever) RetrieveFilteredGQLLogs(tx *sqlx.Tx, rctFilter ReceiptFilter, blockHash *common.Hash, blockNumber *big.Int) ([]LogResult, error) { log.Debug("retrieving log cids for receipt ids with block hash", blockHash.String()) args := make([]interface{}, 0, 4) id := 1 @@ -379,6 +379,12 @@ func (ecr *CIDRetriever) RetrieveFilteredGQLLogs(tx *sqlx.Tx, rctFilter ReceiptF args = append(args, blockHash.String()) id++ + if blockNumber != nil { + pgStr += ` AND receipt_cids.block_number = $2` + id++ + args = append(args, blockNumber.Int64()) + } + pgStr, args = logFilterCondition(&id, pgStr, args, rctFilter) pgStr += ` ORDER BY log_cids.index` diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index 97282037..2b203813 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -1036,8 +1036,9 @@ func (r *Resolver) GetStorageAt(ctx context.Context, args struct { } func (r *Resolver) GetLogs(ctx context.Context, args struct { - BlockHash common.Hash - Addresses *[]common.Address + BlockHash common.Hash + BlockNumber *BigInt + Addresses *[]common.Address }) (*[]*Log, error) { var filter eth.ReceiptFilter @@ -1054,7 +1055,7 @@ func (r *Resolver) GetLogs(ctx context.Context, args struct { return nil, err } - filteredLogs, err := r.backend.Retriever.RetrieveFilteredGQLLogs(tx, filter, &args.BlockHash) + filteredLogs, err := r.backend.Retriever.RetrieveFilteredGQLLogs(tx, filter, &args.BlockHash, args.BlockNumber.ToInt()) if err != nil { return nil, err } diff --git a/pkg/graphql/schema.go b/pkg/graphql/schema.go index d07d311b..9e09a359 100644 --- a/pkg/graphql/schema.go +++ b/pkg/graphql/schema.go @@ -343,7 +343,7 @@ const schema string = ` getStorageAt(blockHash: Bytes32!, contract: Address!, slot: Bytes32!): StorageResult # Get contract logs by block hash and contract address. - getLogs(blockHash: Bytes32!, addresses: [Address!]): [Log!] + getLogs(blockHash: Bytes32!, blockNumber: BigInt, addresses: [Address!]): [Log!] # PostGraphile alternative to get headers with transactions using block number or block hash. allEthHeaderCids(condition: EthHeaderCidCondition): EthHeaderCidsConnection