Skip to content

Commit

Permalink
refactor(consenus): improve CommitProcessor (#758)
Browse files Browse the repository at this point in the history
* Process block in consensus

* Skip lock
  • Loading branch information
sebastijankuzner authored Nov 11, 2024
1 parent 6d6ba7e commit be432c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
12 changes: 12 additions & 0 deletions packages/consensus/source/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ export class Consensus implements Contracts.Consensus.Service {
return;
}

await this.#processBlock(commitState);

await this.onMajorityPrecommit(commitState);
});
}
Expand Down Expand Up @@ -587,4 +589,14 @@ export class Consensus implements Contracts.Consensus.Service {
}
}
}

async #processBlock(commitState: Contracts.Processor.ProcessableUnit): Promise<void> {
if (!commitState.hasProcessorResult()) {
try {
commitState.setProcessorResult(await this.processor.process(commitState));
} catch {
commitState.setProcessorResult({ gasUsed: 0, receipts: new Map(), success: false });
}
}
}
}
34 changes: 8 additions & 26 deletions packages/consensus/source/processors/commit-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ export class CommitProcessor extends AbstractProcessor implements Contracts.Cons
@inject(Identifiers.Cryptography.Configuration)
private readonly configuration!: Contracts.Crypto.Configuration;

@inject(Identifiers.Processor.BlockProcessor)
private readonly processor!: Contracts.Processor.BlockProcessor;

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

Expand All @@ -25,32 +22,17 @@ export class CommitProcessor extends AbstractProcessor implements Contracts.Cons
private readonly commitStateFactory!: Contracts.Consensus.CommitStateFactory;

async process(commit: Contracts.Crypto.Commit): Promise<Contracts.Consensus.ProcessorResult> {
let promise: Promise<void> | undefined;

const result = await this.commitLock.runNonExclusive(async (): Promise<Contracts.Consensus.ProcessorResult> => {
if (!this.#hasValidHeight(commit)) {
return Contracts.Consensus.ProcessorResult.Skipped;
}

const commitState = this.commitStateFactory(commit);

const result = await this.processor.process(commitState);

if (!result.success) {
return Contracts.Consensus.ProcessorResult.Invalid;
}

commitState.setProcessorResult(result);
if (!this.#hasValidHeight(commit)) {
return Contracts.Consensus.ProcessorResult.Skipped;
}

promise = this.getConsensus().handleCommitState(commitState);
const commitState = this.commitStateFactory(commit);

return Contracts.Consensus.ProcessorResult.Accepted;
});
await this.getConsensus().handleCommitState(commitState);

// Execute outside the lock, to avoid deadlocks.
// We want to make sure that the block is handled before we return the result to block downloader. This is different from the other processors.
await promise;
return result;
return commitState.getProcessorResult().success
? Contracts.Consensus.ProcessorResult.Accepted
: Contracts.Consensus.ProcessorResult.Invalid;
}

async hasValidSignature(commit: Contracts.Crypto.Commit): Promise<boolean> {
Expand Down

0 comments on commit be432c4

Please sign in to comment.