Skip to content

Commit deb301e

Browse files
committed
refactor
1 parent b7788c4 commit deb301e

3 files changed

Lines changed: 30 additions & 22 deletions

File tree

packages/bootstrap/source/bootstrapper.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export class Bootstrapper {
5656
@inject(Identifiers.Evm.Worker)
5757
private readonly evmWorker!: Contracts.Evm.Worker;
5858

59+
@inject(Identifiers.Services.Log.Service)
60+
private readonly logger!: Contracts.Kernel.Logger;
61+
5962
public async bootstrap(): Promise<void> {
6063
await this.#setGenesisCommit();
6164
await this.#checkStoredGenesisCommit();
@@ -174,6 +177,9 @@ export class Bootstrapper {
174177
throw new Error("snapshot importer not loaded");
175178
}
176179

177-
await this.snapshotImporter.run(genesisBlock);
180+
const result = await this.snapshotImporter.run(genesisBlock);
181+
this.logger.info(
182+
`snapshot import result: ${JSON.stringify({ ...result, initialTotalSupply: result.initialTotalSupply.toString() })}`,
183+
);
178184
}
179185
}

packages/contracts/source/contracts/snapshot.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export interface LegacyImportOptions {
2424

2525
export interface LegacyImportResult {
2626
readonly initialTotalSupply: bigint;
27-
readonly importedValidators: number;
27+
readonly importedValidatorsWithBlsKey: number;
28+
readonly importedValidatorsWithoutBlsKey: number;
2829
readonly importedUsernames: number;
2930
readonly importedVoters: number;
3031
readonly skippedVoters: number;

packages/snapshot-legacy-importer/source/importer.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
293293
const totalSupply = await this.#seedWallets(options);
294294

295295
// 2) Seed validators
296-
const { importedValidators, skippedValidators } = await this.#seedValidators(options);
296+
const { importedValidatorsWithBlsKey, importedValidatorsWithoutBlsKey, skippedValidators } =
297+
await this.#seedValidators(options);
297298

298299
// 3) Seed voters
299300
const { importedVoters, skippedVoters } = await this.#seedVoters(options);
@@ -307,7 +308,8 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
307308

308309
return {
309310
importedUsernames,
310-
importedValidators,
311+
importedValidatorsWithBlsKey,
312+
importedValidatorsWithoutBlsKey,
311313
importedVoters,
312314
initialTotalSupply: totalSupply,
313315
skippedValidators,
@@ -353,15 +355,18 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
353355
return totalSupply;
354356
}
355357

356-
async #seedValidators(
357-
options: Contracts.Snapshot.LegacyImportOptions,
358-
): Promise<{ importedValidators: number; skippedValidators: number }> {
358+
async #seedValidators(options: Contracts.Snapshot.LegacyImportOptions): Promise<{
359+
importedValidatorsWithBlsKey: number;
360+
importedValidatorsWithoutBlsKey: number;
361+
skippedValidators: number;
362+
}> {
359363
const iface = new ethers.Interface(ConsensusAbi.abi);
360364

361365
this.logger.info(`seeding ${this.#data.validators.length} validators`);
362366

363367
const stats = {
364-
importedValidators: 0,
368+
importedValidatorsWithBlsKey: 0,
369+
importedValidatorsWithoutBlsKey: 0,
365370
skippedValidators: 0,
366371
};
367372

@@ -371,18 +376,16 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
371376
if (!validator.blsPublicKey) {
372377
if (!options.mockFakeValidatorBlsKeys) {
373378
this.logger.info(
374-
`skipping legacy delegate ${validator.arkAddress} (${validator.username}) without registered blsPublicKey`,
379+
`importing legacy delegate ${validator.arkAddress} (${validator.username}) without registered blsPublicKey`,
375380
);
376-
stats.skippedValidators++;
381+
stats.importedValidatorsWithoutBlsKey++;
382+
} else {
383+
const entropy = sha256(Buffer.from(validator.username, "utf8")).slice(2, 34);
384+
const mnemonic = entropyToMnemonic(Buffer.from(entropy, "hex"));
377385

378-
continue;
386+
const consensusKeyPair = await this.consensusKeyPairFactory.fromMnemonic(mnemonic);
387+
validator.blsPublicKey = consensusKeyPair.publicKey;
379388
}
380-
381-
const entropy = sha256(Buffer.from(validator.username, "utf8")).slice(2, 34);
382-
const mnemonic = entropyToMnemonic(Buffer.from(entropy, "hex"));
383-
384-
const consensusKeyPair = await this.consensusKeyPairFactory.fromMnemonic(mnemonic);
385-
validator.blsPublicKey = consensusKeyPair.publicKey;
386389
} else {
387390
if (!(await this.consensusPublicKeyFactory.verify(validator.blsPublicKey))) {
388391
this.logger.info(
@@ -391,14 +394,14 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
391394
stats.skippedValidators++;
392395
continue;
393396
}
394-
}
395397

396-
assert.defined<string>(validator.blsPublicKey);
398+
stats.importedValidatorsWithBlsKey++;
399+
}
397400

398401
const data = iface
399402
.encodeFunctionData("addValidator", [
400403
validator.ethAddress,
401-
Buffer.from(validator.blsPublicKey, "hex"),
404+
validator.blsPublicKey ? Buffer.from(validator.blsPublicKey, "hex") : Buffer.alloc(0),
402405
validator.isResigned,
403406
])
404407
.slice(2);
@@ -414,8 +417,6 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
414417
if (!result.receipt.status) {
415418
throw new Error("failed to add validator");
416419
}
417-
418-
stats.importedValidators++;
419420
}
420421

421422
return stats;

0 commit comments

Comments
 (0)