Skip to content

Commit

Permalink
fix(core-p2p): skip highest common block search on fast validation (#…
Browse files Browse the repository at this point in the history
…4681)

* Fast verification

* Fix tests

* Fix mock

* Return forked if higgestCommonBlock is not calculated

* Add tests

* Extract fast verify logic

* Fix peer verifier test

* Refactor tests

* Fix network state

* Fix

* Peer communicator test
  • Loading branch information
sebastijankuzner committed Jun 29, 2022
1 parent 85b8cee commit 4a5c681
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 16 deletions.
4 changes: 2 additions & 2 deletions __tests__/unit/core-p2p/network-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ describe("NetworkMonitor", () => {

expect(communicator.ping).toBeCalledTimes(peers.length);
for (const peer of peers) {
expect(communicator.ping).toBeCalledWith(peer, config.verifyTimeout, expect.anything());
expect(communicator.ping).toBeCalledWith(peer, config.verifyTimeout, expect.anything(), false);
}
});

Expand All @@ -412,7 +412,7 @@ describe("NetworkMonitor", () => {

expect(communicator.ping).toBeCalledTimes(peers.length);
for (const peer of peers) {
expect(communicator.ping).toBeCalledWith(peer, config.verifyTimeout, expect.anything());
expect(communicator.ping).toBeCalledWith(peer, config.verifyTimeout, expect.anything(), false);
}
});

Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-p2p/network-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Container, Utils as KernelUtils } from "@packages/core-kernel";
import { NetworkStateStatus } from "@packages/core-p2p/src/enums";
import { NetworkState } from "@packages/core-p2p/src/network-state";
import { Peer } from "@packages/core-p2p/src/peer";
import { PeerVerificationResult } from "@packages/core-p2p/src/peer-verifier";
import { FastPeerVerificationResult } from "@packages/core-p2p/src/peer-verifier";
import { Blocks, Crypto, Utils } from "@packages/crypto";

describe("NetworkState", () => {
Expand Down Expand Up @@ -99,7 +99,7 @@ describe("NetworkState", () => {
peer3.state = { header: {}, height: 8, forgingAllowed: true, currentSlot: currentSlot }; // same height
const peer4 = new Peer("184.168.65.65", 4000);
peer4.state = { header: {}, height: 6, forgingAllowed: false, currentSlot: currentSlot - 2 }; // below height
peer4.verificationResult = new PeerVerificationResult(8, 6, 4); // forked
peer4.fastVerificationResult = new FastPeerVerificationResult(8, 6); // forked
const peer5 = new Peer("185.168.65.65", 4000);
peer5.state = { header: {}, height: 6, forgingAllowed: false, currentSlot: currentSlot - 2 }; // below height, not forked
peers = [peer1, peer2, peer3, peer4, peer5];
Expand Down
19 changes: 18 additions & 1 deletion __tests__/unit/core-p2p/peer-communicator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("PeerCommunicator", () => {

const logger = { warning: jest.fn(), debug: jest.fn(), error: jest.fn(), info: jest.fn() };
const configuration = { getOptional: jest.fn(), getRequired: jest.fn() };
const peerVerifier = { initialize: () => {}, checkState: jest.fn() };
const peerVerifier = { initialize: () => {}, checkState: jest.fn(), checkStateFast: jest.fn() };
peerVerifier.initialize = () => peerVerifier;
const version = "3.0.0";
const headers = { version };
Expand Down Expand Up @@ -249,6 +249,23 @@ describe("PeerCommunicator", () => {

const pingResult = await peerCommunicator.ping(peer, 6000);

expect(peerVerifier.checkState).toBeCalledTimes(1);
expect(connector.emit).toBeCalledTimes(1);
expect(connector.emit).toBeCalledWith(peer, event, { headers }, 5000);
expect(pingResult).toEqual(baseGetStatusResponse.state);
});

it("should not throw otherwise using fast option", async () => {
const event = "p2p.peer.getStatus";
const peer = new Peer("187.168.65.65", 4000);
const pingResponse = baseGetStatusResponse;
connector.emit = jest.fn().mockReturnValueOnce({ payload: pingResponse });
configuration.getOptional = jest.fn().mockReturnValueOnce([baseGetStatusResponse.config.version]); // minimumVersions
peerVerifier.checkStateFast = jest.fn().mockReturnValueOnce(new PeerVerificationResult(1, 1, 1));

const pingResult = await peerCommunicator.ping(peer, 6000, false, true);

expect(peerVerifier.checkStateFast).toBeCalledTimes(1);
expect(connector.emit).toBeCalledTimes(1);
expect(connector.emit).toBeCalledWith(peer, event, { headers }, 5000);
expect(pingResult).toEqual(baseGetStatusResponse.state);
Expand Down
Loading

0 comments on commit 4a5c681

Please sign in to comment.