Skip to content

Commit

Permalink
fix(api-sync): exclude deployed contract receipts, remove 0x prefix &…
Browse files Browse the repository at this point in the history
… fix bootstrap (#755)

* Skip restore if database is empty

* Receipts should exist on commit

* Improve log

* Improve log

* Remove prepareBootstrap

* Use consensusContractService directly to get all validators

* slice 0x

* Ignore receipts from deployment contracts
  • Loading branch information
sebastijankuzner authored Nov 7, 2024
1 parent 8314d6e commit 93da0da
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
13 changes: 7 additions & 6 deletions packages/api-sync/source/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ export class Restore {
@inject(Identifiers.Database.Service)
private readonly databaseService!: Contracts.Database.DatabaseService;

@inject(Identifiers.ValidatorSet.Service)
private readonly validatorSet!: Contracts.ValidatorSet.Service;

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

Expand Down Expand Up @@ -293,8 +290,7 @@ export class Restore {
async #ingestConsensusData(context: RestoreContext): Promise<void> {
const t0 = performance.now();

// Consensus.sol#getAllValidators
const validators = this.validatorSet.getAllValidators();
const validators = await this.consensusContractService.getAllValidators();

for (const validator of validators) {
context.validatorAttributes[validator.address] = {
Expand Down Expand Up @@ -404,12 +400,17 @@ export class Restore {
Utils.assert.defined(receipt.txHash);
Utils.assert.defined(receipt.blockHeight);

// Initial deployment receipts
if (receipt.blockHeight >= BigInt(2 ** 32)) {
continue;
}

receipts.push({
blockHeight: Utils.BigNumber.make(receipt.blockHeight).toFixed(),
deployedContractAddress: receipt.deployedContractAddress,
gasRefunded: Number(receipt.gasRefunded),
gasUsed: Number(receipt.gasUsed),
id: receipt.txHash,
id: receipt.txHash.slice(2),
logs: receipt.logs,
output: receipt.output,
success: receipt.success,
Expand Down
16 changes: 6 additions & 10 deletions packages/api-sync/source/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,14 @@ export class Sync implements Contracts.ApiSync.Service {
@inject(Identifiers.ApiSync.Listener)
private readonly listeners!: Listeners;

public async prepareBootstrap(): Promise<void> {
public async bootstrap(): Promise<void> {
await this.migrations.run();
await this.#resetDatabaseIfNecessary();
this.#queue = await this.createQueue();
}

public async bootstrap(): Promise<void> {
// 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") {
if (blocks.count === "0" && !this.databaseService.isEmpty()) {
await this.#bootstrapRestore();
}

Expand All @@ -123,11 +121,7 @@ export class Sync implements Contracts.ApiSync.Service {
const publicKeyToAddress: Record<string, string> = {};
const transactionReceipts: Models.Receipt[] = [];

let receipts: Map<string, Contracts.Evm.TransactionReceipt> | undefined;

if (unit.hasProcessorResult()) {
receipts = unit.getProcessorResult().receipts;
}
const receipts = unit.getProcessorResult().receipts;

for (const transaction of transactions) {
const { senderPublicKey } = transaction.data;
Expand Down Expand Up @@ -479,7 +473,9 @@ export class Sync implements Contracts.ApiSync.Service {
return;
}

this.logger.warning(`resetting API database and state to genesis block for full restore`);
if (lastHeight !== 0 || blocks.count !== "0") {
this.logger.warning(`Clearing API database for full restore.`);
}

await this.dataSource.transaction("REPEATABLE READ", async (entityManager) => {
const blockRepository = this.blockRepositoryFactory(entityManager);
Expand Down
7 changes: 1 addition & 6 deletions packages/bootstrap/source/bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,14 @@ export class Bootstrapper {

public async bootstrap(): Promise<void> {
try {
if (this.apiSync) {
await this.apiSync.prepareBootstrap();
}

await this.#setGenesisCommit();
await this.#checkStoredGenesisCommit();

await this.#initState();

if (this.apiSync) {
await this.apiSync.bootstrap();
}

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

this.validatorRepository.printLoadedValidators();
Expand Down
1 change: 0 additions & 1 deletion packages/contracts/source/contracts/api-sync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CommitHandler } from "./crypto/commit.js";

export interface Service extends CommitHandler {
prepareBootstrap(): Promise<void>;
bootstrap(): Promise<void>;
beforeCommit(): Promise<void>;
getLastSyncedBlockHeight(): Promise<number>;
Expand Down

0 comments on commit 93da0da

Please sign in to comment.