Skip to content

Commit

Permalink
improve state hash check
Browse files Browse the repository at this point in the history
  • Loading branch information
oXtxNt9U committed Feb 27, 2025
1 parent 464ed17 commit a98dcc4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/contracts/source/contracts/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface LegacyImporter {
validators: ImportedLegacyValidator[];
voters: ImportedLegacyVoter[];
snapshotHash: string;
result: LegacyImportResult | undefined;
}

export interface LegacyImportOptions {
Expand Down
13 changes: 9 additions & 4 deletions packages/processor/source/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class BlockProcessor implements Contracts.Processor.BlockProcessor {
@optional()
private readonly apiSync?: Contracts.ApiSync.Service;

@inject(Identifiers.Snapshot.Legacy.Importer)
@optional()
private readonly snapshotImporter?: Contracts.Snapshot.LegacyImporter;

public async process(unit: Contracts.Processor.ProcessableUnit): Promise<Contracts.Processor.BlockProcessorResult> {
const processResult = { gasUsed: 0, receipts: new Map(), success: false };

Expand Down Expand Up @@ -176,12 +180,13 @@ export class BlockProcessor implements Contracts.Processor.BlockProcessor {
let previousStateHash;
if (block.header.height === 0) {
// Assume snapshot is present if the previous block points to a non-zero hash
// and skip state hash check since it will be performed during import.
if (block.header.previousBlock !== "0000000000000000000000000000000000000000000000000000000000000000") {
return;
Utils.assert.defined(this.snapshotImporter);
Utils.assert.defined(this.snapshotImporter.result);
previousStateHash = this.snapshotImporter.result.stateHash;
} else {
previousStateHash = "0000000000000000000000000000000000000000000000000000000000000000";
}

previousStateHash = "0000000000000000000000000000000000000000000000000000000000000000";
} else {
const previousBlock = this.stateStore.getLastBlock();
previousStateHash = previousBlock.header.stateHash;
Expand Down
9 changes: 9 additions & 0 deletions packages/snapshot-legacy-importer/source/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
voters: Contracts.Snapshot.ImportedLegacyVoter[];
validators: Contracts.Snapshot.ImportedLegacyValidator[];
snapshotHash: string;
result: Contracts.Snapshot.LegacyImportResult | undefined;
} = {
snapshotHash: "",
validators: [],
voters: [],
wallets: [],
result: undefined,
};

public get voters(): Contracts.Snapshot.ImportedLegacyVoter[] {
Expand All @@ -69,6 +71,10 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
return this.#data.snapshotHash;
}

public get result(): Contracts.Snapshot.LegacyImportResult | undefined {
return this.#data.result;
}

#nonce = 0n;

public async run(genesisBlock: Contracts.Crypto.Commit): Promise<Contracts.Snapshot.LegacyImportResult> {
Expand Down Expand Up @@ -105,6 +111,8 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
throw new Error("genesis block snapshot supply mismatch ");
}

this.#data.result = result;

return result;
}

Expand Down Expand Up @@ -203,6 +211,7 @@ export class Importer implements Contracts.Snapshot.LegacyImporter {
validators,
voters,
wallets,
result: undefined,
};
}

Expand Down

0 comments on commit a98dcc4

Please sign in to comment.