Skip to content

Commit

Permalink
refactor: block broadcast when block comes from forger (#2430)
Browse files Browse the repository at this point in the history
  • Loading branch information
air1one authored and faustbrian committed Apr 16, 2019
1 parent d012ce8 commit 92adfad
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
8 changes: 4 additions & 4 deletions packages/core-blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ export class Blockchain implements blockchain.IBlockchain {
/**
* Push a block to the process queue.
*/
public handleIncomingBlock(block: Interfaces.IBlockData, remoteAddress: string): void {
this.pushPingBlock(block);
public handleIncomingBlock(block: Interfaces.IBlockData, remoteAddress: string, fromForger: boolean = false): void {
this.pushPingBlock(block, fromForger);

logger.info(
`Received new block at height ${block.height.toLocaleString()} with ${pluralize(
Expand Down Expand Up @@ -482,7 +482,7 @@ export class Blockchain implements blockchain.IBlockchain {
/**
* Push ping block.
*/
public pushPingBlock(block: Interfaces.IBlockData): void {
this.state.pushPingBlock(block);
public pushPingBlock(block: Interfaces.IBlockData, fromForger: boolean = false): void {
this.state.pushPingBlock(block, fromForger);
}
}
4 changes: 2 additions & 2 deletions packages/core-blockchain/src/state-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class StateStorage implements Blockchain.IStateStorage {
/**
* Push ping block.
*/
public pushPingBlock(block: Interfaces.IBlockData): void {
public pushPingBlock(block: Interfaces.IBlockData, fromForger: boolean = false): void {
// logging for stats about network health
if (this.blockPing) {
logger.info(
Expand All @@ -231,7 +231,7 @@ export class StateStorage implements Blockchain.IStateStorage {
}

this.blockPing = {
count: 1,
count: fromForger ? 0 : 1, // if block comes from forger, it hasn't "pinged" blockchain even once
first: new Date().getTime(),
last: new Date().getTime(),
block,
Expand Down
4 changes: 2 additions & 2 deletions packages/core-interfaces/src/core-blockchain/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface IBlockchain {
/**
* Push a block to the process queue.
*/
handleIncomingBlock(block: Interfaces.IBlockData, remoteAddress: string): void;
handleIncomingBlock(block: Interfaces.IBlockData, remoteAddress: string, fromForger?: boolean): void;

/**
* Remove N number of blocks.
Expand Down Expand Up @@ -163,5 +163,5 @@ export interface IBlockchain {
* Push ping block.
* @return {Object}
*/
pushPingBlock(block: Interfaces.IBlockData): void;
pushPingBlock(block: Interfaces.IBlockData, fromForger?: boolean): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ export interface IStateStorage {
/**
* Push ping block
*/
pushPingBlock(block: Interfaces.IBlockData): void;
pushPingBlock(block: Interfaces.IBlockData, fromForger?: boolean): void;
}
10 changes: 5 additions & 5 deletions packages/core-p2p/src/network-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ export class NetworkMonitor implements P2P.INetworkMonitor {
// wait a bit before broadcasting if a bit early
const diff = blockPing.last - blockPing.first;
const maxHop = 4;
let proba = (maxHop - blockPing.count) / maxHop;
let broadcastQuota: number = (maxHop - blockPing.count) / maxHop;

if (diff < 500 && proba > 0) {
if (diff < 500 && broadcastQuota > 0) {
await delay(500 - diff);

blockPing = blockchain.getBlockPing();
Expand All @@ -322,11 +322,11 @@ export class NetworkMonitor implements P2P.INetworkMonitor {
return;
}

proba = (maxHop - blockPing.count) / maxHop;
broadcastQuota = (maxHop - blockPing.count) / maxHop;
}

// TODO: to be put in config?
peers = peers.filter(p => Math.random() < proba);
peers = broadcastQuota <= 0 ? [] : shuffle(peers).slice(0, Math.ceil(broadcastQuota * peers.length));
// select a portion of our peers according to quota calculated before
}

this.logger.info(
Expand Down
5 changes: 3 additions & 2 deletions packages/core-p2p/src/socket-server/versions/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ export async function postBlock({ req }): Promise<void> {
const blockchain: Blockchain.IBlockchain = app.resolvePlugin<Blockchain.IBlockchain>("blockchain");

const block: Interfaces.IBlockData = req.data.block;
const fromForger: boolean = isWhitelisted(app.resolveOptions("p2p").remoteAccess, req.headers.remoteAddress);

if (!isWhitelisted(app.resolveOptions("p2p").remoteAccess, req.headers.remoteAddress)) {
if (!fromForger) {
if (blockchain.pingBlock(block)) {
return;
}
Expand All @@ -76,7 +77,7 @@ export async function postBlock({ req }): Promise<void> {
}
}

blockchain.handleIncomingBlock(block, req.headers.remoteAddress);
blockchain.handleIncomingBlock(block, req.headers.remoteAddress, fromForger);
}

export async function postTransactions({ service, req }: { service: P2P.IPeerService; req }): Promise<string[]> {
Expand Down

0 comments on commit 92adfad

Please sign in to comment.