Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: block broadcast when block comes from forger #2430

Merged
merged 4 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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