Skip to content

Commit 54fbb96

Browse files
authored
fix(api-sync): prevent validator round reset after each batch during restore (#1322)
* make block batch size configurable * fix validator round resetting after batch commit * add regression test
1 parent d17e7bd commit 54fbb96

6 files changed

Lines changed: 26 additions & 4 deletions

File tree

packages/api-sync/source/defaults.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Environment } from "@mainsail/kernel";
33

44
export const defaults = {
55
enabled: Environment.isTrue(EnvironmentVariables.MAINSAIL_API_SYNC_ENABLED),
6+
restore: {
7+
blocks: {
8+
batchSize: Environment.get<number>(EnvironmentVariables.MAINSAIL_API_SYNC_RESTORE_BLOCKS_BATCH_SIZE, 1000),
9+
},
10+
},
611
syncInterval: Environment.get<number>(EnvironmentVariables.MAINSAIL_API_SYNC_INTERVAL, 8000),
712
tokenCacheSize: Environment.get<number>(EnvironmentVariables.MAINSAIL_API_SYNC_TOKEN_CACHE_SIZE, 256),
813
tokenWhitelistRefreshInterval: Environment.get<number>(

packages/api-sync/source/restore.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ export class Restore {
159159
@optional()
160160
private readonly snapshotImporter?: Contracts.Snapshot.LegacyImporter;
161161

162+
@inject(Identifiers.ServiceProvider.Configuration)
163+
@tagged("plugin", "api-sync")
164+
private readonly pluginConfiguration!: Contracts.Kernel.PluginConfiguration;
165+
162166
public async restore(): Promise<void> {
163167
const isEmpty = await this.databaseService.isEmpty();
164168
const mostRecentCommit = await (isEmpty
@@ -299,15 +303,16 @@ export class Restore {
299303
validatorRounds,
300304
} = context;
301305

302-
const BATCH_SIZE = 1000;
303-
const CHUNK_SIZE = 1000;
306+
const BATCH_SIZE = this.pluginConfiguration.getRequired<number>("restore.blocks.batchSize");
307+
const CHUNK_SIZE = BATCH_SIZE;
304308
const t0 = performance.now();
305309

306310
const genesisBlockNumber = this.configuration.getGenesisHeight();
307311
let currentBlockNumber = genesisBlockNumber;
308312

309313
let ingestedBlocks = 0;
310314
let ingestedTransactions = 0;
315+
let totalRound = 0;
311316

312317
const multiPaymentContractAddress = this.app.get<string>(
313318
EvmConsensusIdentifiers.Contracts.Addresses.MultiPayment,
@@ -383,7 +388,6 @@ export class Restore {
383388
}
384389
};
385390

386-
let totalRound = 0;
387391
for await (const { block, proof } of commits) {
388392
blocks.push({
389393
commitRound: proof.round,

packages/api-sync/source/service-provider.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@ export class ServiceProvider extends Providers.ServiceProvider {
3939

4040
public configSchema(): Joi.ObjectSchema {
4141
return Joi.object({
42+
restore: Joi.object({
43+
blocks: Joi.object({
44+
batchSize: Joi.number().integer().positive().required(),
45+
}),
46+
}),
4247
syncInterval: Joi.number().integer().positive().required(),
4348
tokenCacheSize: Joi.number().integer().positive().required(),
49+
tokenWhitelistRefreshInterval: Joi.number().integer().positive().required(),
4450
}).unknown(true);
4551
}
4652

packages/constants/source/environment-variables.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ export const EnvironmentVariableNames = [
8181
"MAINSAIL_API_SYNC_TOKEN_WHITELIST_REMOTE_URL",
8282
"MAINSAIL_API_SYNC_LOG_EXTRA",
8383

84+
"MAINSAIL_API_SYNC_RESTORE_BLOCKS_BATCH_SIZE",
85+
8486
// Transaction pool API
8587
"MAINSAIL_API_TRANSACTION_POOL_TRUST_PROXY",
8688
"MAINSAIL_API_TRANSACTION_POOL_DISABLED",

tests/functional/resync/source/resync.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe<{
2323
});
2424

2525
it("should be ok", async ({ syncNode }) => {
26-
await waitBlock(syncNode, 5);
26+
await waitBlock(syncNode, 54);
2727
});
2828

2929
it("should be ok with votes", async ({ syncNode }) => {

tests/functional/resync/source/setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ const setupNode = async (app: Application, dataDirectory: string, configDirector
120120
},
121121
"@mainsail/api-sync": {
122122
maxSyncAttempts: 1,
123+
restore: {
124+
blocks: {
125+
batchSize: 5,
126+
},
127+
},
123128
syncInterval: 250,
124129
truncateDatabase: "1",
125130
},

0 commit comments

Comments
 (0)