Skip to content

Commit

Permalink
refactor(core-p2p): broadcast block delay (#4380)
Browse files Browse the repository at this point in the history
* chore(core-kernel): add fromForger to BlockPing

* chore(core-state): set formForger on blockPing

* chore(core-p2p): broadcast block immediately to all peers if from forger

* test(core-p2p): add network monitor tests

Co-authored-by: air1one <36802613+air1one@users.noreply.github.com>
  • Loading branch information
sebastijankuzner and air1one committed May 4, 2021
1 parent 838d897 commit eb1bb6b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
14 changes: 13 additions & 1 deletion __tests__/unit/core-p2p/network-monitor.test.ts
Expand Up @@ -1024,6 +1024,18 @@ describe("NetworkMonitor", () => {
);

describe("when blockPing.last - blockPing.first < 500ms", () => {
it("should not wait if block is from forger", async () => {
blockchain.getBlockPing = jest
.fn()
.mockReturnValue({ block: block.data, last: 10500, first: 10200, count: 2, fromForger: true });
const spySleep = jest.spyOn(Utils, "sleep");

await networkMonitor.broadcastBlock(block);

expect(communicator.postBlock).toBeCalledTimes(peers.length);
expect(spySleep).toBeCalledTimes(0);
});

it("should wait until 500ms have elapsed between blockPing.last and blockPing.first before broadcasting", async () => {
blockchain.getBlockPing = jest
.fn()
Expand All @@ -1042,7 +1054,7 @@ describe("NetworkMonitor", () => {
.fn()
.mockReturnValueOnce({ block: block.data, last: 10500, first: 10200, count: 2 })
.mockReturnValueOnce({
block: { ...block.data, id: "11111111" },
block: { ...block.data, id: "11111111", height: 3 },
last: 10500,
first: 10200,
count: 2,
Expand Down
1 change: 1 addition & 0 deletions packages/core-kernel/src/contracts/state/state-store.ts
Expand Up @@ -4,6 +4,7 @@ export interface BlockPing {
count: number;
first: number;
last: number;
fromForger: boolean;
block: Interfaces.IBlockData;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core-p2p/src/network-monitor.ts
Expand Up @@ -478,7 +478,7 @@ export class NetworkMonitor implements Contracts.P2P.NetworkMonitor {
let blockPing = blockchain.getBlockPing();
let peers: Contracts.P2P.Peer[] = this.repository.getPeers();

if (blockPing && blockPing.block.id === block.data.id) {
if (blockPing && blockPing.block.id === block.data.id && !blockPing.fromForger) {
// wait a bit before broadcasting if a bit early
const diff = blockPing.last - blockPing.first;
const maxHop = 4;
Expand All @@ -490,7 +490,7 @@ export class NetworkMonitor implements Contracts.P2P.NetworkMonitor {
blockPing = blockchain.getBlockPing()!;

// got aleady a new block, no broadcast
if (blockPing.block.id !== block.data.id) {
if (blockPing.block.height !== block.data.height) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions packages/core-state/src/stores/state.ts
Expand Up @@ -364,6 +364,7 @@ export class StateStore implements Contracts.State.StateStore {
count: fromForger ? 0 : 1,
first: new Date().getTime(),
last: new Date().getTime(),
fromForger: fromForger,
block,
};
}
Expand Down

0 comments on commit eb1bb6b

Please sign in to comment.