Skip to content

Commit

Permalink
always restore genesis commit
Browse files Browse the repository at this point in the history
  • Loading branch information
oXtxNt9U committed Dec 11, 2024
1 parent 92b1176 commit 1ffb716
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
39 changes: 28 additions & 11 deletions packages/bootstrap/source/bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ export class Bootstrapper {
await this.#setGenesisCommit();
await this.#checkStoredGenesisCommit();

if (this.apiSync) {
await this.apiSync.bootstrap();
if (this.databaseService.isEmpty()) {
await this.#initGenesisState();
} else {
await this.#initPostGenesisState();
}

await this.#initState();
this.state.setBootstrap(false);

this.validatorRepository.printLoadedValidators();
Expand Down Expand Up @@ -111,15 +112,31 @@ export class Bootstrapper {
}
}

async #initState(): Promise<void> {
if (this.databaseService.isEmpty()) {
await this.#tryImportSnapshot();
await this.#processGenesisBlock();
} else {
const commit = await this.databaseService.getLastCommit();
this.stateStore.setLastBlock(commit.block);
this.stateStore.setTotalRound(this.databaseService.getState().totalRound);
async #initApiSync(): Promise<void> {
if (this.apiSync) {
await this.apiSync.bootstrap();
}
}

async #initGenesisState(): Promise<void> {
if (!this.databaseService.isEmpty()) {
throw new Error("initGenesisState must be called on empty database");
}

await this.#tryImportSnapshot();
await this.#processGenesisBlock();
await this.validatorSet.restore();

// After genesis commit to restore all data
await this.#initApiSync();
}

async #initPostGenesisState(): Promise<void> {
await this.#initApiSync();

const commit = await this.databaseService.getLastCommit();
this.stateStore.setLastBlock(commit.block);
this.stateStore.setTotalRound(this.databaseService.getState().totalRound);

await this.validatorSet.restore();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/processor/source/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class BlockProcessor implements Contracts.Processor.BlockProcessor {
}

public async commit(unit: Contracts.Processor.ProcessableUnit): Promise<void> {
if (this.apiSync) {
if (this.apiSync && unit.height > 0) {
await this.apiSync.beforeCommit();
}

Expand All @@ -111,7 +111,7 @@ export class BlockProcessor implements Contracts.Processor.BlockProcessor {
await this.txPoolWorker.onCommit(unit);
await this.evmWorker.onCommit(unit);

if (this.apiSync) {
if (this.apiSync && unit.height > 0) {
await this.apiSync.onCommit(unit);
}

Expand Down

0 comments on commit 1ffb716

Please sign in to comment.