Skip to content

Commit

Permalink
fix(api-sync): restore tables on empty state (#764)
Browse files Browse the repository at this point in the history
* fix restore on empty state

* update version correctly
  • Loading branch information
oXtxNt9U authored Nov 18, 2024
1 parent 734a3bd commit 9a4056a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
11 changes: 9 additions & 2 deletions packages/api-sync/source/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export class Restore {
@inject(Identifiers.Database.Service)
private readonly databaseService!: Contracts.Database.DatabaseService;

@inject(Identifiers.State.Store)
private readonly stateStore!: Contracts.State.Store;

@inject(ApiDatabaseIdentifiers.BlockRepositoryFactory)
private readonly blockRepositoryFactory!: ApiDatabaseContracts.BlockRepositoryFactory;

Expand Down Expand Up @@ -105,7 +108,9 @@ export class Restore {
private readonly consensusContractService!: Contracts.Evm.ConsensusContractService;

public async restore(): Promise<void> {
const mostRecentCommit = await this.databaseService.getLastCommit();
const mostRecentCommit = await (this.databaseService.isEmpty()
? this.stateStore.getGenesisCommit()
: this.databaseService.getLastCommit());
Utils.assert.defined<Contracts.Crypto.Commit>(mostRecentCommit);

this.logger.info(
Expand All @@ -127,7 +132,9 @@ export class Restore {
receiptRepository: this.receiptRepositoryFactory(entityManager),
stateRepository: this.stateRepositoryFactory(entityManager),

totalSupply: Utils.BigNumber.ZERO,
totalSupply: this.databaseService.isEmpty()
? this.stateStore.getGenesisCommit().block.data.totalAmount
: Utils.BigNumber.ZERO,

transactionRepository: this.transactionRepositoryFactory(entityManager),
transactionTypeRepository: this.transactionTypeRepositoryFactory(entityManager),
Expand Down
21 changes: 10 additions & 11 deletions packages/api-sync/source/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Sync implements Contracts.ApiSync.Service {

// if our database is empty, we sync all blocks from scratch
const [blocks] = await this.dataSource.query("select count(1) from blocks");
if (blocks.count === "0" && !this.databaseService.isEmpty()) {
if (blocks.count === "0") {
await this.#bootstrapRestore();
}

Expand Down Expand Up @@ -382,16 +382,15 @@ export class Sync implements Contracts.ApiSync.Service {
.execute();
}

if (deferred.newMilestones) {
await configurationRepository
.createQueryBuilder()
.update()
.set({
activeMilestones: deferred.newMilestones,
})
.where("id = :id", { id: 1 })
.execute();
}
await configurationRepository
.createQueryBuilder()
.update()
.set({
version: this.app.version(),
...(deferred.newMilestones ? { activeMilestones: deferred.newMilestones } : {}),
})
.where("id = :id", { id: 1 })
.execute();

for (const batch of chunk(deferred.wallets, 256)) {
const batchParameterLength = 6;
Expand Down

0 comments on commit 9a4056a

Please sign in to comment.