Skip to content

Commit 17fe4de

Browse files
fix(p2p): include previous block hash when calculating signatures (#1336)
* Fix * Empty * Remove default value * No optional values * style: resolve style guide violations [ci-lint-fix]
1 parent 29fce41 commit 17fe4de

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

packages/consensus/source/processors/commit-processor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class CommitProcessor extends AbstractProcessor implements Contracts.Cons
3737
: Enums.Consensus.ProcessorResult.Invalid;
3838
}
3939

40-
async hasValidSignature(commit: Contracts.Crypto.Commit): Promise<boolean> {
40+
async hasValidSignature(commit: Contracts.Crypto.Commit, previousBlockHash: string): Promise<boolean> {
4141
const { block, proof } = commit;
4242

4343
const publicKeys: Buffer[] = [];
@@ -64,7 +64,7 @@ export class CommitProcessor extends AbstractProcessor implements Contracts.Cons
6464
},
6565
{
6666
genesisBlockHash: this.stateStore.getGenesisCommit().block.hash,
67-
previousBlockHash: this.stateStore.getLastBlock().hash,
67+
previousBlockHash,
6868
},
6969
);
7070

packages/contracts/source/contracts/consensus/processor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ export interface MessageProcessor {
1212

1313
export interface CommitProcessor {
1414
process(commit: Commit, broadcast?: boolean): Promise<ProcessorResult>;
15-
hasValidSignature(commit: Commit): Promise<boolean>;
15+
hasValidSignature(commit: Commit, previousBlockHash: string): Promise<boolean>;
1616
}

packages/p2p/source/downloader/block-downloader.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,15 @@ export class BlockDownloader implements Contracts.P2P.Downloader {
159159
}
160160
}
161161

162+
// Each commit's precommit signature covers the previous block hash, so verify
163+
// against the actual predecessor in the chain instead of the store's last block
164+
// (which is stale for every commit but the first until the batch is processed).
162165
const hasValidSignatures = await Promise.all(
163-
commits.map(async (commit) => await this.commitProcessor.hasValidSignature(commit)),
166+
commits.map(async (commit, index) =>
167+
index === 0
168+
? await this.commitProcessor.hasValidSignature(commit, this.stateStore.getLastBlock().hash)
169+
: await this.commitProcessor.hasValidSignature(commit, commits[index - 1].block.hash),
170+
),
164171
);
165172

166173
if (!hasValidSignatures.every(Boolean)) {

0 commit comments

Comments
 (0)