From c46f91ab866d489f686046584b8ff0247dc192c6 Mon Sep 17 00:00:00 2001 From: Jono Prest <65739024+JonoPrest@users.noreply.github.com> Date: Tue, 17 Sep 2024 16:24:47 +0200 Subject: [PATCH] Deduplicate wildcard queries on multiple registers and partitions (#210) * Deduplicate wildcard queries on multiple registers and partitions * Fix broken tests --- .../chainWorkers/HyperFuelWorker.res | 46 +++++++++++-------- .../chainWorkers/HyperSyncWorker.res | 23 +++++++--- .../test/HyperSyncWorker_test.res | 2 + 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/codegenerator/cli/templates/static/codegen/src/eventFetching/chainWorkers/HyperFuelWorker.res b/codegenerator/cli/templates/static/codegen/src/eventFetching/chainWorkers/HyperFuelWorker.res index d3722073f..cdc292e04 100644 --- a/codegenerator/cli/templates/static/codegen/src/eventFetching/chainWorkers/HyperFuelWorker.res +++ b/codegenerator/cli/templates/static/codegen/src/eventFetching/chainWorkers/HyperFuelWorker.res @@ -114,6 +114,7 @@ module Make = ( ~logger, ~setCurrentBlockHeight, ~contractAddressMapping, + ~shouldApplyWildcards, ) => { //Wait for a valid range to query //This should never have to wait since we check that the from block is below the toBlock @@ -127,25 +128,28 @@ module Make = ( ) //Instantiate each time to add new registered contract addresses - let contractsReceiptQuery = - T.contracts - ->Belt.Array.keepMap((contract): option => { - switch contractAddressMapping->ContractAddressingMap.getAddressesFromContractName( - ~contractName=contract.name, - ) { - | [] => None - | addresses => - Some({ - addresses, - rb: contract.events->Array.keepMap(eventMod => { - let module(Event: Types.Event) = eventMod - let {isWildcard} = Event.handlerRegister->Types.HandlerTypes.Register.getEventOptions - isWildcard ? None : Some(Event.sighash->BigInt.fromStringUnsafe) - }), - }) - } - }) - ->Array.concat(wildcardLogSelection) + let contractsReceiptQuery = T.contracts->Belt.Array.keepMap((contract): option< + HyperFuel.contractReceiptQuery, + > => { + switch contractAddressMapping->ContractAddressingMap.getAddressesFromContractName( + ~contractName=contract.name, + ) { + | [] => None + | addresses => + Some({ + addresses, + rb: contract.events->Array.keepMap(eventMod => { + let module(Event: Types.Event) = eventMod + let {isWildcard} = Event.handlerRegister->Types.HandlerTypes.Register.getEventOptions + isWildcard ? None : Some(Event.sighash->BigInt.fromStringUnsafe) + }), + }) + } + }) + + let contractsReceiptQuery = shouldApplyWildcards + ? contractsReceiptQuery->Array.concat(wildcardLogSelection) + : contractsReceiptQuery let startFetchingBatchTimeRef = Hrtime.makeTimer() @@ -185,6 +189,9 @@ module Make = ( ~contractAddressMapping, ~logger, ~setCurrentBlockHeight, + //Only apply wildcards on the first partition and root register + //to avoid duplicate wildcard queries + ~shouldApplyWildcards=fetchStateRegisterId == Root && partitionId == 0, ) //set height and next from block @@ -252,7 +259,6 @@ module Make = ( ) }) } - // } let parsingTimeRef = Hrtime.makeTimer() diff --git a/codegenerator/cli/templates/static/codegen/src/eventFetching/chainWorkers/HyperSyncWorker.res b/codegenerator/cli/templates/static/codegen/src/eventFetching/chainWorkers/HyperSyncWorker.res index a42437400..a87475e9a 100644 --- a/codegenerator/cli/templates/static/codegen/src/eventFetching/chainWorkers/HyperSyncWorker.res +++ b/codegenerator/cli/templates/static/codegen/src/eventFetching/chainWorkers/HyperSyncWorker.res @@ -1,8 +1,6 @@ open ChainWorker open Belt -exception EventRoutingFailed - module Helpers = { let rec queryLogsPageWithBackoff = async ( ~backoffMsOnFailure=200, @@ -107,9 +105,12 @@ let makeGetNextPage = ( }) }) - let getLogSelectionOrThrow = (~contractAddressMapping): array => { - contracts - ->Belt.Array.keepMap((contract): option => { + let getLogSelectionOrThrow = (~contractAddressMapping, ~shouldApplyWildcards): array< + LogSelection.t, + > => { + let nonWildcardLogSelection = contracts->Belt.Array.keepMap((contract): option< + LogSelection.t, + > => { switch contractAddressMapping->ContractAddressingMap.getAddressesFromContractName( ~contractName=contract.name, ) { @@ -127,7 +128,10 @@ let makeGetNextPage = ( } } }) - ->Array.concat(wildcardLogSelection) + + shouldApplyWildcards + ? nonWildcardLogSelection->Array.concat(wildcardLogSelection) + : nonWildcardLogSelection } async ( @@ -137,6 +141,7 @@ let makeGetNextPage = ( ~logger, ~setCurrentBlockHeight, ~contractAddressMapping, + ~shouldApplyWildcards, ) => { //Wait for a valid range to query //This should never have to wait since we check that the from block is below the toBlock @@ -155,7 +160,7 @@ let makeGetNextPage = ( ) let logSelections = try { - getLogSelectionOrThrow(~contractAddressMapping) + getLogSelectionOrThrow(~contractAddressMapping, ~shouldApplyWildcards) } catch { | exn => exn->ErrorHandling.mkLogAndRaise( @@ -289,6 +294,9 @@ module Make = ( ~contractAddressMapping, ~logger, ~setCurrentBlockHeight, + //Only apply wildcards on the first partition and root register + //to avoid duplicate wildcard queries + ~shouldApplyWildcards=fetchStateRegisterId == Root && partitionId == 0, //only ) //set height and next from block @@ -530,3 +538,4 @@ module Make = ( ~logger, )->Promise.thenResolve(HyperSync.mapExn) } + diff --git a/scenarios/test_codegen/test/HyperSyncWorker_test.res b/scenarios/test_codegen/test/HyperSyncWorker_test.res index a3a171672..639698a1c 100644 --- a/scenarios/test_codegen/test/HyperSyncWorker_test.res +++ b/scenarios/test_codegen/test/HyperSyncWorker_test.res @@ -76,6 +76,7 @@ describe("HyperSyncWorker - getNextPage", () => { ~logger=Logging.logger, ~setCurrentBlockHeight=_blockNumber => (), ~contractAddressMapping=ContractAddressingMap.make(), + ~shouldApplyWildcards=true, ) Assert.deepEqual( @@ -126,6 +127,7 @@ describe("HyperSyncWorker - getNextPage", () => { ~logger=Logging.logger, ~setCurrentBlockHeight=_blockNumber => (), ~contractAddressMapping=ContractAddressingMap.make(), + ~shouldApplyWildcards=true, ) Assert.deepEqual(