Skip to content

Commit

Permalink
perf(core-p2p): skip signature check if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
spkjp committed Jun 18, 2019
1 parent 24fa281 commit da6e474
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/core-p2p/src/peer-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export class PeerVerifier {
return undefined;
}

const claimedHeight = Number(claimedState.header.height);
const ourHeight: number = await this.ourHeight();
const claimedHeight: number = Number(claimedState.header.height);
const ourHeight: number = this.ourHeight();
if (await this.weHavePeersHighestBlock(claimedState, ourHeight)) {
// Case3 and Case5
return new PeerVerificationResult(ourHeight, claimedHeight, claimedHeight);
Expand Down Expand Up @@ -118,6 +118,17 @@ export class PeerVerifier {
}

try {
const ownBlock: Interfaces.IBlock = app
.resolvePlugin<State.IStateService>("state")
.getStore()
.getLastBlocks()
.find(block => block.data.height === blockHeader.height);

// Use shortcut to prevent expensive crypto if the block header equals our own.
if (ownBlock && JSON.stringify(ownBlock.getHeader()) === JSON.stringify(blockHeader)) {
return true;
}

const claimedBlock: Interfaces.IBlock = Blocks.BlockFactory.fromData(blockHeader);
if (claimedBlock.verifySignature()) {
return true;
Expand All @@ -137,11 +148,7 @@ export class PeerVerifier {
}
}

/**
* Retrieve the height of the highest block in our chain.
* @return {Number} chain height
*/
private async ourHeight(): Promise<number> {
private ourHeight(): number {
const height: number = app
.resolvePlugin<State.IStateService>("state")
.getStore()
Expand Down

0 comments on commit da6e474

Please sign in to comment.