Skip to content

Commit

Permalink
Deduplicate wildcard queries on multiple registers and partitions (#210)
Browse files Browse the repository at this point in the history
* Deduplicate wildcard queries on multiple registers and partitions

* Fix broken tests
  • Loading branch information
JonoPrest authored Sep 17, 2024
1 parent e72019c commit c46f91a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -127,25 +128,28 @@ module Make = (
)

//Instantiate each time to add new registered contract addresses
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)
}),
})
}
})
->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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -252,7 +259,6 @@ module Make = (
)
})
}
// }

let parsingTimeRef = Hrtime.makeTimer()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
open ChainWorker
open Belt

exception EventRoutingFailed

module Helpers = {
let rec queryLogsPageWithBackoff = async (
~backoffMsOnFailure=200,
Expand Down Expand Up @@ -107,9 +105,12 @@ let makeGetNextPage = (
})
})

let getLogSelectionOrThrow = (~contractAddressMapping): array<LogSelection.t> => {
contracts
->Belt.Array.keepMap((contract): option<LogSelection.t> => {
let getLogSelectionOrThrow = (~contractAddressMapping, ~shouldApplyWildcards): array<
LogSelection.t,
> => {
let nonWildcardLogSelection = contracts->Belt.Array.keepMap((contract): option<
LogSelection.t,
> => {
switch contractAddressMapping->ContractAddressingMap.getAddressesFromContractName(
~contractName=contract.name,
) {
Expand All @@ -127,7 +128,10 @@ let makeGetNextPage = (
}
}
})
->Array.concat(wildcardLogSelection)

shouldApplyWildcards
? nonWildcardLogSelection->Array.concat(wildcardLogSelection)
: nonWildcardLogSelection
}

async (
Expand All @@ -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
Expand All @@ -155,7 +160,7 @@ let makeGetNextPage = (
)

let logSelections = try {
getLogSelectionOrThrow(~contractAddressMapping)
getLogSelectionOrThrow(~contractAddressMapping, ~shouldApplyWildcards)
} catch {
| exn =>
exn->ErrorHandling.mkLogAndRaise(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -530,3 +538,4 @@ module Make = (
~logger,
)->Promise.thenResolve(HyperSync.mapExn)
}

2 changes: 2 additions & 0 deletions scenarios/test_codegen/test/HyperSyncWorker_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe("HyperSyncWorker - getNextPage", () => {
~logger=Logging.logger,
~setCurrentBlockHeight=_blockNumber => (),
~contractAddressMapping=ContractAddressingMap.make(),
~shouldApplyWildcards=true,
)

Assert.deepEqual(
Expand Down Expand Up @@ -126,6 +127,7 @@ describe("HyperSyncWorker - getNextPage", () => {
~logger=Logging.logger,
~setCurrentBlockHeight=_blockNumber => (),
~contractAddressMapping=ContractAddressingMap.make(),
~shouldApplyWildcards=true,
)

Assert.deepEqual(
Expand Down

0 comments on commit c46f91a

Please sign in to comment.