Skip to content

Commit 611ee0d

Browse files
authored
Error when contract start block is less than network's (#670)
* Remove old dockerfiles * Clean up FetchState.make API * Add an error that start_block for contract should be higher than for network
1 parent 16ff0ea commit 611ee0d

File tree

7 files changed

+138
-175
lines changed

7 files changed

+138
-175
lines changed

.github/Dockerfile

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/Dockerfile.typescript

Lines changed: 0 additions & 24 deletions
This file was deleted.

codegenerator/cli/npm/envio/src/FetchState.res

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -966,9 +966,7 @@ let make = (
966966
~startBlock,
967967
~endBlock,
968968
~eventConfigs: array<Internal.eventConfig>,
969-
~staticContracts: dict<array<Address.t>>,
970-
~staticContractStartBlocks: dict<option<int>>=Js.Dict.empty(),
971-
~dynamicContracts: array<indexingContract>,
969+
~contracts: array<indexingContract>,
972970
~maxAddrInPartition,
973971
~chainId,
974972
~blockLag=?,
@@ -1042,52 +1040,21 @@ let make = (
10421040

10431041
let pendingNormalPartition = ref(makePendingNormalPartition())
10441042

1045-
let registerAddress = (contractName, address, ~dc: option<indexingContract>=?) => {
1046-
let pendingPartition = pendingNormalPartition.contents
1047-
switch pendingPartition.addressesByContractName->Utils.Dict.dangerouslyGetNonOption(
1048-
contractName,
1049-
) {
1050-
| Some(addresses) => addresses->Array.push(address)
1051-
| None => pendingPartition.addressesByContractName->Js.Dict.set(contractName, [address])
1052-
}
1053-
indexingContracts->Js.Dict.set(
1054-
address->Address.toString,
1055-
switch dc {
1056-
| Some(dc) => dc
1057-
| None => {
1058-
address,
1059-
contractName,
1060-
startBlock: switch staticContractStartBlocks->Js.Dict.get(contractName) {
1061-
| Some(Some(contractStartBlock)) => contractStartBlock
1062-
| Some(None) | None => startBlock
1063-
},
1064-
register: Config,
1065-
}
1066-
},
1067-
)
1068-
if (
1069-
pendingPartition.addressesByContractName->addressesByContractNameCount ===
1070-
maxAddrInPartition
1071-
) {
1072-
partitions->Array.push(pendingPartition)
1073-
pendingNormalPartition := makePendingNormalPartition()
1074-
}
1075-
}
1076-
1077-
staticContracts
1078-
->Js.Dict.entries
1079-
->Array.forEach(((contractName, addresses)) => {
1080-
if contractNamesWithNormalEvents->Utils.Set.has(contractName) {
1081-
addresses->Array.forEach(a => {
1082-
registerAddress(contractName, a)
1083-
})
1084-
}
1085-
})
1086-
1087-
dynamicContracts->Array.forEach(dc => {
1088-
let contractName = dc.contractName
1043+
contracts->Array.forEach(contract => {
1044+
let contractName = contract.contractName
10891045
if contractNamesWithNormalEvents->Utils.Set.has(contractName) {
1090-
registerAddress(contractName, dc.address, ~dc)
1046+
let pendingPartition = pendingNormalPartition.contents
1047+
pendingPartition.addressesByContractName->Utils.Dict.push(contractName, contract.address)
1048+
indexingContracts->Js.Dict.set(contract.address->Address.toString, contract)
1049+
if (
1050+
pendingPartition.addressesByContractName->addressesByContractNameCount ===
1051+
maxAddrInPartition
1052+
) {
1053+
// FIXME: should split into separate partitions
1054+
// depending on the start block
1055+
partitions->Array.push(pendingPartition)
1056+
pendingNormalPartition := makePendingNormalPartition()
1057+
}
10911058
}
10921059
})
10931060

codegenerator/cli/templates/static/codegen/src/eventFetching/ChainFetcher.res

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ let make = (
5151
let eventRouter = EventRouter.empty()
5252

5353
// Aggregate events we want to fetch
54-
let staticContracts = Js.Dict.empty()
55-
let staticContractStartBlocks = Js.Dict.empty()
54+
let contracts = []
5655
let eventConfigs: array<Internal.eventConfig> = []
5756

5857
chainConfig.contracts->Array.forEach(contract => {
@@ -91,15 +90,29 @@ let make = (
9190
}
9291
})
9392

94-
staticContracts->Js.Dict.set(contractName, contract.addresses)
95-
staticContractStartBlocks->Js.Dict.set(contractName, contract.startBlock)
93+
switch contract.startBlock {
94+
| Some(startBlock) if startBlock < chainConfig.startBlock =>
95+
Js.Exn.raiseError(
96+
`The start block for contract "${contractName}" is less than the chain start block. This is not supported yet.`,
97+
)
98+
| _ => ()
99+
}
100+
101+
contract.addresses->Array.forEach(address => {
102+
contracts->Array.push({
103+
FetchState.address,
104+
contractName: contract.name,
105+
startBlock: switch contract.startBlock {
106+
| Some(startBlock) => startBlock
107+
| None => chainConfig.startBlock
108+
},
109+
register: Config,
110+
})
111+
})
96112
})
97113

98-
let fetchState = FetchState.make(
99-
~maxAddrInPartition,
100-
~staticContracts,
101-
~staticContractStartBlocks,
102-
~dynamicContracts=dynamicContracts->Array.map(dc => {
114+
dynamicContracts->Array.forEach(dc =>
115+
contracts->Array.push({
103116
FetchState.address: dc.contractAddress,
104117
contractName: (dc.contractType :> string),
105118
startBlock: dc.registeringEventBlockNumber,
@@ -110,7 +123,12 @@ let make = (
110123
registeringEventName: dc.registeringEventName,
111124
registeringEventSrcAddress: dc.registeringEventSrcAddress,
112125
}),
113-
}),
126+
})
127+
)
128+
129+
let fetchState = FetchState.make(
130+
~maxAddrInPartition,
131+
~contracts,
114132
~startBlock,
115133
~endBlock,
116134
~eventConfigs,
@@ -314,7 +332,11 @@ let cleanUpProcessingFilters = (
314332
/**
315333
* Helper function to get the configured start block for a contract from config
316334
*/
317-
let getContractStartBlock = (config: Config.t, ~chain: ChainMap.Chain.t, ~contractName: string): option<int> => {
335+
let getContractStartBlock = (
336+
config: Config.t,
337+
~chain: ChainMap.Chain.t,
338+
~contractName: string,
339+
): option<int> => {
318340
let chainConfig = config.chainMap->ChainMap.get(chain)
319341
chainConfig.contracts
320342
->Js.Array2.find(contract => contract.name === contractName)
@@ -338,7 +360,11 @@ let runContractRegistersOrThrow = async (
338360
let {timestamp, blockNumber, logIndex} = eventItem
339361

340362
// Use contract-specific start block if configured, otherwise fall back to registration block
341-
let contractStartBlock = switch getContractStartBlock(config, ~chain=eventItem.chain, ~contractName=(contractName: Enums.ContractType.t :> string)) {
363+
let contractStartBlock = switch getContractStartBlock(
364+
config,
365+
~chain=eventItem.chain,
366+
~contractName=(contractName: Enums.ContractType.t :> string),
367+
) {
342368
| Some(configuredStartBlock) => configuredStartBlock
343369
| None => blockNumber
344370
}
@@ -374,7 +400,9 @@ let runContractRegistersOrThrow = async (
374400

375401
// Catch sync and async errors
376402
try {
377-
let result = contractRegister(eventItem->UserContext.getContractRegisterArgs(~onRegister, ~config))
403+
let result = contractRegister(
404+
eventItem->UserContext.getContractRegisterArgs(~onRegister, ~config),
405+
)
378406

379407
// Even though `contractRegister` always returns a promise,
380408
// in the ReScript type, but it might return a non-promise value for TS API.

scenarios/test_codegen/pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scenarios/test_codegen/test/ChainManager_test.res

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ let populateChainQueuesWithRandomEvents = (~runTime=1000, ~maxBlockTime=15, ())
2727
let fetcherStateInit: FetchState.t = FetchState.make(
2828
~maxAddrInPartition=Env.maxAddrInPartition,
2929
~endBlock=None,
30-
~staticContracts=Js.Dict.empty(),
3130
~eventConfigs,
32-
~dynamicContracts=[],
31+
~contracts=[],
3332
~startBlock=0,
3433
~chainId=0,
3534
)

0 commit comments

Comments
 (0)