From c06098ce41549140d9f74cc28ae65ad8c0d42ea1 Mon Sep 17 00:00:00 2001 From: Kodemon Date: Tue, 4 Jan 2022 05:18:56 +0000 Subject: [PATCH 01/11] add getPeerInfo rcp --- docs/node/CATEGORIES/03-net.md | 42 +++++++++++ .../category/net/getPeerInfo.test.ts | 74 +++++++++++++++++++ .../jellyfish-api-core/src/category/net.ts | 41 ++++++++++ 3 files changed, 157 insertions(+) create mode 100644 packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts diff --git a/docs/node/CATEGORIES/03-net.md b/docs/node/CATEGORIES/03-net.md index 91f90219cf..85d6cb2531 100644 --- a/docs/node/CATEGORIES/03-net.md +++ b/docs/node/CATEGORIES/03-net.md @@ -23,6 +23,48 @@ interface net { } ``` +## getPeerInfo + +Returns data about each connected network node as a json array of objects. + +```ts title="client.net.getPeerInfo()" +interface net { + getPeerInfo (): Promise +} + +export interface PeerInfo { + id: number + addr: string + addrbind: string + addrlocal: string + services: string + relaytxes: boolean + lastsend: number + lastrecv: number + bytessent: number + bytesrecv: number + conntime: number + timeoffset: number + pingtime: number + minping: number + pingwait: number + version: number + subver: string + inbound: boolean + addnode: boolean + startingheight: number + banscore: number + synced_headers: number + synced_blocks: number + inflight: number[] + whitelisted: boolean + permissions: string[] + minfeefilter: number + bytessent_per_msg: Record + bytesrecv_per_msg: Record +} +``` + ## getNetworkInfo Returns an object containing various state info regarding P2P networking. diff --git a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts new file mode 100644 index 0000000000..34fc35358c --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts @@ -0,0 +1,74 @@ +import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' + +createNetworkTests('Network without masternode', new RegTestContainer()) +createNetworkTests('Network with masternode', new MasterNodeRegTestContainer()) + +function createNetworkTests (name: string, container: RegTestContainer | MasterNodeRegTestContainer): void { + describe(name, () => { + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.addNode('127.0.0.1') + }) + + afterAll(async () => { + await container.stop() + }) + + it('should return a list of peers', async () => { + await expect(client.net.getPeerInfo()).resolves.toHaveLength(2) + }) + + it('should return expected schema', async () => { + for (const peer of await client.net.getPeerInfo()) { + expect(typeof peer.id).toStrictEqual('number') + expect(typeof peer.addr).toStrictEqual('string') + expect(typeof peer.addrbind).toStrictEqual('string') + expect(typeof peer.services).toStrictEqual('string') + expect(typeof peer.relaytxes).toStrictEqual('boolean') + expect(typeof peer.lastsend).toStrictEqual('number') + expect(typeof peer.lastrecv).toStrictEqual('number') + expect(typeof peer.conntime).toStrictEqual('number') + expect(typeof peer.timeoffset).toStrictEqual('number') + expect(typeof peer.version).toStrictEqual('number') + expect(typeof peer.subver).toStrictEqual('string') + expect(typeof peer.inbound).toStrictEqual('boolean') + expect(typeof peer.addnode).toStrictEqual('boolean') + expect(typeof peer.startingheight).toStrictEqual('number') + expect(typeof peer.banscore).toStrictEqual('number') + expect(typeof peer.synced_headers).toStrictEqual('number') + expect(typeof peer.synced_blocks).toStrictEqual('number') + expect(typeof peer.inflight).toStrictEqual('object') + expect(typeof peer.whitelisted).toStrictEqual('boolean') + expect(typeof peer.permissions).toStrictEqual('object') + expect(typeof peer.minfeefilter).toStrictEqual('number') + expect(typeof peer.bytessent_per_msg).toStrictEqual('object') + expect(typeof peer.bytesrecv_per_msg).toStrictEqual('object') + } + }) + + it('should return valid values', async () => { + for (const peer of await client.net.getPeerInfo()) { + expectValidAddress(peer.addr) + expectValidAddress(peer.addrbind) + + expect(peer.services).toHaveLength(16) + + expectValidBytesPerMessage(peer.bytessent_per_msg) + expectValidBytesPerMessage(peer.bytesrecv_per_msg) + } + }) + }) +} + +function expectValidAddress (address: string): void { + return expect(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}(:[0-9]{1,5})?$/.test(address)).toBeTruthy() +} + +function expectValidBytesPerMessage (obj: Record): void { + for (const key in obj) { + expect(obj[key]).toBeGreaterThanOrEqual(0) + } +} diff --git a/packages/jellyfish-api-core/src/category/net.ts b/packages/jellyfish-api-core/src/category/net.ts index e05faf8c9a..9e6faae35d 100644 --- a/packages/jellyfish-api-core/src/category/net.ts +++ b/packages/jellyfish-api-core/src/category/net.ts @@ -19,6 +19,15 @@ export class Net { return await this.client.call('getconnectioncount', [], 'number') } + /** + * Returns data about each connected network node as a json array of objects. + * + * @return {Promise} + */ + async getPeerInfo (): Promise { + return await this.client.call('getpeerinfo', [], 'number') + } + /** * Returns an object containing various state info regarding P2P networking. * @@ -29,6 +38,38 @@ export class Net { } } +export interface PeerInfo { + id: number + addr: string + addrbind: string + addrlocal: string + services: string + relaytxes: boolean + lastsend: number + lastrecv: number + bytessent: number + bytesrecv: number + conntime: number + timeoffset: number + pingtime: number + minping: number + pingwait: number + version: number + subver: string + inbound: boolean + addnode: boolean + startingheight: number + banscore: number + synced_headers: number + synced_blocks: number + inflight: number[] + whitelisted: boolean + permissions: string[] + minfeefilter: number + bytessent_per_msg: Record + bytesrecv_per_msg: Record +} + export interface NetworkInfo { version: number subversion: string From ab27d10cc3111a33fe99229225055720ecb5c7dc Mon Sep 17 00:00:00 2001 From: Kodemon Date: Wed, 5 Jan 2022 03:10:54 +0000 Subject: [PATCH 02/11] update interface to account for optional keys --- .../jellyfish-api-core/src/category/net.ts | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/net.ts b/packages/jellyfish-api-core/src/category/net.ts index 9e6faae35d..30812954ed 100644 --- a/packages/jellyfish-api-core/src/category/net.ts +++ b/packages/jellyfish-api-core/src/category/net.ts @@ -41,8 +41,8 @@ export class Net { export interface PeerInfo { id: number addr: string - addrbind: string - addrlocal: string + addrbind?: string + addrlocal?: string services: string relaytxes: boolean lastsend: number @@ -51,23 +51,27 @@ export interface PeerInfo { bytesrecv: number conntime: number timeoffset: number - pingtime: number - minping: number - pingwait: number + pingtime?: number + minping?: number + pingwait?: number version: number subver: string inbound: boolean addnode: boolean startingheight: number - banscore: number - synced_headers: number - synced_blocks: number + banscore?: number + synced_headers?: number + synced_blocks?: number inflight: number[] whitelisted: boolean permissions: string[] minfeefilter: number - bytessent_per_msg: Record - bytesrecv_per_msg: Record + bytessent_per_msg: { + [msg: string]: number + } + bytesrecv_per_msg: { + [msg: string]: number + } } export interface NetworkInfo { From 44eb27bd2f7115dfddda856d0f1a2551bbf09481 Mon Sep 17 00:00:00 2001 From: Kodemon Date: Wed, 5 Jan 2022 03:12:06 +0000 Subject: [PATCH 03/11] update unit tests - Account for undefined values - Perform key=>value pair checking in 1 schema test instead of 2 (is sort of redundant). --- .../category/net/getPeerInfo.test.ts | 55 ++++++++++++------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts index 34fc35358c..1dc5e8f3c3 100644 --- a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts @@ -21,40 +21,44 @@ function createNetworkTests (name: string, container: RegTestContainer | MasterN await expect(client.net.getPeerInfo()).resolves.toHaveLength(2) }) - it('should return expected schema', async () => { + it('should return valid schema', async () => { for (const peer of await client.net.getPeerInfo()) { expect(typeof peer.id).toStrictEqual('number') - expect(typeof peer.addr).toStrictEqual('string') - expect(typeof peer.addrbind).toStrictEqual('string') + + expectValidAddress(peer.addr) + + expectUndefinedOrValidAddress(peer.addrbind) + expectUndefinedOrValidAddress(peer.addrlocal) + expect(typeof peer.services).toStrictEqual('string') + expect(peer.services).toHaveLength(16) + expect(typeof peer.relaytxes).toStrictEqual('boolean') expect(typeof peer.lastsend).toStrictEqual('number') expect(typeof peer.lastrecv).toStrictEqual('number') + expect(typeof peer.bytessent).toStrictEqual('number') + expect(typeof peer.bytesrecv).toStrictEqual('number') expect(typeof peer.conntime).toStrictEqual('number') expect(typeof peer.timeoffset).toStrictEqual('number') + + expectUndefinedOrEquals('number', peer.pingtime) + expectUndefinedOrEquals('number', peer.minping) + expectUndefinedOrEquals('number', peer.pingwait) + expect(typeof peer.version).toStrictEqual('number') expect(typeof peer.subver).toStrictEqual('string') expect(typeof peer.inbound).toStrictEqual('boolean') expect(typeof peer.addnode).toStrictEqual('boolean') expect(typeof peer.startingheight).toStrictEqual('number') - expect(typeof peer.banscore).toStrictEqual('number') - expect(typeof peer.synced_headers).toStrictEqual('number') - expect(typeof peer.synced_blocks).toStrictEqual('number') + + expectUndefinedOrEquals('number', peer.banscore) + expectUndefinedOrEquals('number', peer.synced_headers) + expectUndefinedOrEquals('number', peer.synced_blocks) + expect(typeof peer.inflight).toStrictEqual('object') expect(typeof peer.whitelisted).toStrictEqual('boolean') expect(typeof peer.permissions).toStrictEqual('object') expect(typeof peer.minfeefilter).toStrictEqual('number') - expect(typeof peer.bytessent_per_msg).toStrictEqual('object') - expect(typeof peer.bytesrecv_per_msg).toStrictEqual('object') - } - }) - - it('should return valid values', async () => { - for (const peer of await client.net.getPeerInfo()) { - expectValidAddress(peer.addr) - expectValidAddress(peer.addrbind) - - expect(peer.services).toHaveLength(16) expectValidBytesPerMessage(peer.bytessent_per_msg) expectValidBytesPerMessage(peer.bytesrecv_per_msg) @@ -63,12 +67,25 @@ function createNetworkTests (name: string, container: RegTestContainer | MasterN }) } +function expectUndefinedOrEquals (expected: any, actual?: any): void { + if (actual !== undefined) { + expect(typeof actual).toStrictEqual(expected) + } +} + +function expectUndefinedOrValidAddress (address?: string): void { + if (address !== undefined) { + expectValidAddress(address) + } +} + function expectValidAddress (address: string): void { - return expect(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}(:[0-9]{1,5})?$/.test(address)).toBeTruthy() + expect(typeof address).toStrictEqual('string') + expect(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}(:[0-9]{1,5})?$/.test(address)).toBeTruthy() } function expectValidBytesPerMessage (obj: Record): void { for (const key in obj) { - expect(obj[key]).toBeGreaterThanOrEqual(0) + expect(typeof obj[key]).toStrictEqual('number') } } From e50d483c349a7af2d156c8d0ef763d6c7f94914f Mon Sep 17 00:00:00 2001 From: Kodemon Date: Wed, 5 Jan 2022 03:14:23 +0000 Subject: [PATCH 04/11] update peerInfo interface documentation Add optional keys --- docs/node/CATEGORIES/03-net.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/node/CATEGORIES/03-net.md b/docs/node/CATEGORIES/03-net.md index 85d6cb2531..2241034779 100644 --- a/docs/node/CATEGORIES/03-net.md +++ b/docs/node/CATEGORIES/03-net.md @@ -35,8 +35,8 @@ interface net { export interface PeerInfo { id: number addr: string - addrbind: string - addrlocal: string + addrbind?: string + addrlocal?: string services: string relaytxes: boolean lastsend: number @@ -45,23 +45,27 @@ export interface PeerInfo { bytesrecv: number conntime: number timeoffset: number - pingtime: number - minping: number - pingwait: number + pingtime?: number + minping?: number + pingwait?: number version: number subver: string inbound: boolean addnode: boolean startingheight: number - banscore: number - synced_headers: number - synced_blocks: number + banscore?: number + synced_headers?: number + synced_blocks?: number inflight: number[] whitelisted: boolean permissions: string[] minfeefilter: number - bytessent_per_msg: Record - bytesrecv_per_msg: Record + bytessent_per_msg: { + [msg: string]: number + } + bytesrecv_per_msg: { + [msg: string]: number + } } ``` From 8fa4b713a2dc10718e3471aae9dca22fe9416e3e Mon Sep 17 00:00:00 2001 From: Kodemon Date: Wed, 5 Jan 2022 03:30:23 +0000 Subject: [PATCH 05/11] update tests to a single retrieval case Matching the other tests in the net package. --- .../__tests__/category/net/getPeerInfo.test.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts index 1dc5e8f3c3..874bc868e6 100644 --- a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts @@ -1,8 +1,9 @@ import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers' +import { PeerInfo } from 'packages/jellyfish-api-core/src/category/net' import { ContainerAdapterClient } from '../../container_adapter_client' createNetworkTests('Network without masternode', new RegTestContainer()) -createNetworkTests('Network with masternode', new MasterNodeRegTestContainer()) +createNetworkTests('Network on masternode', new MasterNodeRegTestContainer()) function createNetworkTests (name: string, container: RegTestContainer | MasterNodeRegTestContainer): void { describe(name, () => { @@ -17,11 +18,11 @@ function createNetworkTests (name: string, container: RegTestContainer | MasterN await container.stop() }) - it('should return a list of peers', async () => { - await expect(client.net.getPeerInfo()).resolves.toHaveLength(2) - }) + it('should getPeerInfo', async () => { + const peers: PeerInfo[] = await client.net.getPeerInfo() + + expect(peers.length).toBeGreaterThan(0) - it('should return valid schema', async () => { for (const peer of await client.net.getPeerInfo()) { expect(typeof peer.id).toStrictEqual('number') From 32d71c030e9e7835766f249205803d5d2b0118ea Mon Sep 17 00:00:00 2001 From: Kodemon Date: Wed, 5 Jan 2022 03:33:36 +0000 Subject: [PATCH 06/11] update unit tests to use retrieved peers Forgot to loop through the newly retrieved list of peers avoiding a second call for peers in the same test. --- .../__tests__/category/net/getPeerInfo.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts index 874bc868e6..43291ebbc3 100644 --- a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts @@ -23,7 +23,7 @@ function createNetworkTests (name: string, container: RegTestContainer | MasterN expect(peers.length).toBeGreaterThan(0) - for (const peer of await client.net.getPeerInfo()) { + for (const peer of peers) { expect(typeof peer.id).toStrictEqual('number') expectValidAddress(peer.addr) From 13a10e220a1449a6cd122d00ffa4127b7fab59e7 Mon Sep 17 00:00:00 2001 From: Kodemon Date: Wed, 5 Jan 2022 07:40:21 +0000 Subject: [PATCH 07/11] assign node ip from the container --- .../__tests__/category/net/getPeerInfo.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts index 43291ebbc3..54e0b007d4 100644 --- a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts @@ -11,7 +11,7 @@ function createNetworkTests (name: string, container: RegTestContainer | MasterN beforeAll(async () => { await container.start() - await container.addNode('127.0.0.1') + await container.addNode(await container.getIp('bridge')) }) afterAll(async () => { From 33b8e9a57b20a0683eae24c0e79727e6422d88f5 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Thu, 6 Jan 2022 15:12:28 +0800 Subject: [PATCH 08/11] added docker network operation --- .../category/net/getPeerInfo.test.ts | 28 ++++++++++++----- .../src/containers/DockerContainer.ts | 31 ++++++++++++++++++- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts index 54e0b007d4..a796b5be33 100644 --- a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts @@ -1,21 +1,35 @@ import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers' import { PeerInfo } from 'packages/jellyfish-api-core/src/category/net' import { ContainerAdapterClient } from '../../container_adapter_client' +import Dockerode from 'dockerode' -createNetworkTests('Network without masternode', new RegTestContainer()) -createNetworkTests('Network on masternode', new MasterNodeRegTestContainer()) +createNetworkTests('Network without masternode', [new RegTestContainer(), new RegTestContainer()]) +createNetworkTests('Network on masternode', [new MasterNodeRegTestContainer(), new MasterNodeRegTestContainer()]) -function createNetworkTests (name: string, container: RegTestContainer | MasterNodeRegTestContainer): void { +function createNetworkTests (name: string, container: RegTestContainer[] | MasterNodeRegTestContainer[]): void { describe(name, () => { - const client = new ContainerAdapterClient(container) + const client = new ContainerAdapterClient(container[0]) + let createdNetwork: Dockerode.NetworkInspectInfo beforeAll(async () => { - await container.start() - await container.addNode(await container.getIp('bridge')) + await container[0].start() + await container[0].createNetwork('test') + const networks = await container[0].listNetworks() + createdNetwork = networks.find(n => n.Name === 'test') as Dockerode.NetworkInspectInfo + await container[0].connectNetwork(createdNetwork.Id) + + await container[1].start() + await container[1].connectNetwork(createdNetwork.Id) + + // addnode + await container[0].addNode(await container[1].getIp('test')) }) afterAll(async () => { - await container.stop() + await container[0].stop() + await container[1].stop() + + await container[0].removeNetwork(createdNetwork.Id) }) it('should getPeerInfo', async () => { diff --git a/packages/testcontainers/src/containers/DockerContainer.ts b/packages/testcontainers/src/containers/DockerContainer.ts index ae96ed08ec..7fb2efbddf 100644 --- a/packages/testcontainers/src/containers/DockerContainer.ts +++ b/packages/testcontainers/src/containers/DockerContainer.ts @@ -1,8 +1,9 @@ -import Dockerode, { Container, DockerOptions } from 'dockerode' +import Dockerode, { Container, DockerOptions, Network } from 'dockerode' export abstract class DockerContainer { protected readonly docker: Dockerode protected container?: Container + protected dNetwork?: Network protected constructor ( protected readonly image: string, @@ -21,6 +22,34 @@ export abstract class DockerContainer { return networks[name].IPAddress } + async listNetworks (): Promise { + return await this.docker.listNetworks() + } + + async getNetwork (id: string): Promise { + return this.docker.getNetwork(id) + } + + async createNetwork (name: string): Promise { + await this.docker.createNetwork({ + Name: name, + IPAM: { + Driver: 'default', + Config: [] + } + }) + } + + async connectNetwork (id: string): Promise { + const network = await this.getNetwork(id) + await network.connect({ Container: this.id }) + } + + async removeNetwork (id: string): Promise { + const network = await this.getNetwork(id) + await network.remove(id) + } + /** * Try pull docker image if it doesn't already exist. */ From 7fbcf7b8f2c3403ec93b7ac2ebb3bb048b90181b Mon Sep 17 00:00:00 2001 From: websterlcl Date: Thu, 6 Jan 2022 15:38:34 +0800 Subject: [PATCH 09/11] rm unuse import --- packages/testcontainers/src/containers/DockerContainer.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/testcontainers/src/containers/DockerContainer.ts b/packages/testcontainers/src/containers/DockerContainer.ts index 7fb2efbddf..dcbfc98cac 100644 --- a/packages/testcontainers/src/containers/DockerContainer.ts +++ b/packages/testcontainers/src/containers/DockerContainer.ts @@ -1,9 +1,8 @@ -import Dockerode, { Container, DockerOptions, Network } from 'dockerode' +import Dockerode, { Container, DockerOptions } from 'dockerode' export abstract class DockerContainer { protected readonly docker: Dockerode protected container?: Container - protected dNetwork?: Network protected constructor ( protected readonly image: string, From e95cbd09c2a5d4dc112e68c29a9aff5289a4e574 Mon Sep 17 00:00:00 2001 From: Kodemon Date: Mon, 17 Jan 2022 05:55:43 +0000 Subject: [PATCH 10/11] remove abstractions from unit tests --- .../category/net/getPeerInfo.test.ts | 262 ++++++++++++------ 1 file changed, 180 insertions(+), 82 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts index a796b5be33..7fe4647111 100644 --- a/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/net/getPeerInfo.test.ts @@ -1,106 +1,204 @@ -import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers' +import { RegTestContainer } from '@defichain/testcontainers' import { PeerInfo } from 'packages/jellyfish-api-core/src/category/net' import { ContainerAdapterClient } from '../../container_adapter_client' import Dockerode from 'dockerode' +import { TestingGroup } from '@defichain/jellyfish-testing' + +const addrRegExp = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}(:[0-9]{1,5})?$/ + +describe('Network without masternode', () => { + const container = [ + new RegTestContainer(), + new RegTestContainer() + ] + const client = new ContainerAdapterClient(container[0]) + let createdNetwork: Dockerode.NetworkInspectInfo + + beforeAll(async () => { + await container[0].start() + await container[0].createNetwork('test') + const networks = await container[0].listNetworks() + createdNetwork = networks.find(n => n.Name === 'test') as Dockerode.NetworkInspectInfo + await container[0].connectNetwork(createdNetwork.Id) + + await container[1].start() + await container[1].connectNetwork(createdNetwork.Id) + + // addnode + await container[0].addNode(await container[1].getIp('test')) + }) + + afterAll(async () => { + await container[0].stop() + await container[1].stop() + + await container[0].removeNetwork(createdNetwork.Id) + }) + + it('should getPeerInfo', async () => { + const peers: PeerInfo[] = await client.net.getPeerInfo() + + expect(peers.length).toBeGreaterThan(0) + + for (const peer of peers) { + expect(typeof peer.id).toStrictEqual('number') + + expect(typeof peer.addr).toStrictEqual('string') + expect(addrRegExp.test(peer.addr)).toBeTruthy() + + if (peer.addrbind !== undefined) { + expect(typeof peer.addrbind).toStrictEqual('string') + expect(addrRegExp.test(peer.addrbind)).toBeTruthy() + } + + if (peer.addrlocal !== undefined) { + expect(typeof peer.addrlocal).toStrictEqual('string') + expect(addrRegExp.test(peer.addrlocal)).toBeTruthy() + } + + expect(typeof peer.services).toStrictEqual('string') + expect(peer.services).toHaveLength(16) + + expect(typeof peer.relaytxes).toStrictEqual('boolean') + expect(typeof peer.lastsend).toStrictEqual('number') + expect(typeof peer.lastrecv).toStrictEqual('number') + expect(typeof peer.bytessent).toStrictEqual('number') + expect(typeof peer.bytesrecv).toStrictEqual('number') + expect(typeof peer.conntime).toStrictEqual('number') + expect(typeof peer.timeoffset).toStrictEqual('number') + + if (peer.pingtime !== undefined) { + expect(typeof peer.pingtime).toStrictEqual('number') + } + + if (peer.minping !== undefined) { + expect(typeof peer.minping).toStrictEqual('number') + } + + if (peer.pingwait !== undefined) { + expect(typeof peer.pingwait).toStrictEqual('number') + } + + expect(typeof peer.version).toStrictEqual('number') + expect(typeof peer.subver).toStrictEqual('string') + expect(typeof peer.inbound).toStrictEqual('boolean') + expect(typeof peer.addnode).toStrictEqual('boolean') + expect(typeof peer.startingheight).toStrictEqual('number') + + if (peer.banscore !== undefined) { + expect(typeof peer.banscore).toStrictEqual('number') + } -createNetworkTests('Network without masternode', [new RegTestContainer(), new RegTestContainer()]) -createNetworkTests('Network on masternode', [new MasterNodeRegTestContainer(), new MasterNodeRegTestContainer()]) + if (peer.synced_headers !== undefined) { + expect(typeof peer.synced_headers).toStrictEqual('number') + } + + if (peer.synced_blocks !== undefined) { + expect(typeof peer.synced_blocks).toStrictEqual('number') + } -function createNetworkTests (name: string, container: RegTestContainer[] | MasterNodeRegTestContainer[]): void { - describe(name, () => { - const client = new ContainerAdapterClient(container[0]) - let createdNetwork: Dockerode.NetworkInspectInfo + expect(typeof peer.inflight).toStrictEqual('object') + expect(typeof peer.whitelisted).toStrictEqual('boolean') + expect(typeof peer.permissions).toStrictEqual('object') + expect(typeof peer.minfeefilter).toStrictEqual('number') - beforeAll(async () => { - await container[0].start() - await container[0].createNetwork('test') - const networks = await container[0].listNetworks() - createdNetwork = networks.find(n => n.Name === 'test') as Dockerode.NetworkInspectInfo - await container[0].connectNetwork(createdNetwork.Id) + for (const key in peer.bytessent_per_msg) { + expect(typeof peer.bytessent_per_msg[key]).toStrictEqual('number') + } - await container[1].start() - await container[1].connectNetwork(createdNetwork.Id) + for (const key in peer.bytesrecv_per_msg) { + expect(typeof peer.bytesrecv_per_msg[key]).toStrictEqual('number') + } + } + }) +}) - // addnode - await container[0].addNode(await container[1].getIp('test')) - }) +describe('Network with masternode', () => { + const tgroup = TestingGroup.create(2) + const { rpc: { net } } = tgroup.get(0) - afterAll(async () => { - await container[0].stop() - await container[1].stop() + beforeAll(async () => { + await tgroup.start() + }) - await container[0].removeNetwork(createdNetwork.Id) - }) + afterAll(async () => { + await tgroup.stop() + }) - it('should getPeerInfo', async () => { - const peers: PeerInfo[] = await client.net.getPeerInfo() + it('should getPeerInfo', async () => { + const peers: PeerInfo[] = await net.getPeerInfo() - expect(peers.length).toBeGreaterThan(0) + expect(peers.length).toBeGreaterThan(0) - for (const peer of peers) { - expect(typeof peer.id).toStrictEqual('number') + for (const peer of peers) { + expect(typeof peer.id).toStrictEqual('number') - expectValidAddress(peer.addr) + expect(typeof peer.addr).toStrictEqual('string') + expect(addrRegExp.test(peer.addr)).toBeTruthy() - expectUndefinedOrValidAddress(peer.addrbind) - expectUndefinedOrValidAddress(peer.addrlocal) + if (peer.addrbind !== undefined) { + expect(typeof peer.addrbind).toStrictEqual('string') + expect(addrRegExp.test(peer.addrbind)).toBeTruthy() + } - expect(typeof peer.services).toStrictEqual('string') - expect(peer.services).toHaveLength(16) + if (peer.addrlocal !== undefined) { + expect(typeof peer.addrlocal).toStrictEqual('string') + expect(addrRegExp.test(peer.addrlocal)).toBeTruthy() + } - expect(typeof peer.relaytxes).toStrictEqual('boolean') - expect(typeof peer.lastsend).toStrictEqual('number') - expect(typeof peer.lastrecv).toStrictEqual('number') - expect(typeof peer.bytessent).toStrictEqual('number') - expect(typeof peer.bytesrecv).toStrictEqual('number') - expect(typeof peer.conntime).toStrictEqual('number') - expect(typeof peer.timeoffset).toStrictEqual('number') + expect(typeof peer.services).toStrictEqual('string') + expect(peer.services).toHaveLength(16) - expectUndefinedOrEquals('number', peer.pingtime) - expectUndefinedOrEquals('number', peer.minping) - expectUndefinedOrEquals('number', peer.pingwait) + expect(typeof peer.relaytxes).toStrictEqual('boolean') + expect(typeof peer.lastsend).toStrictEqual('number') + expect(typeof peer.lastrecv).toStrictEqual('number') + expect(typeof peer.bytessent).toStrictEqual('number') + expect(typeof peer.bytesrecv).toStrictEqual('number') + expect(typeof peer.conntime).toStrictEqual('number') + expect(typeof peer.timeoffset).toStrictEqual('number') - expect(typeof peer.version).toStrictEqual('number') - expect(typeof peer.subver).toStrictEqual('string') - expect(typeof peer.inbound).toStrictEqual('boolean') - expect(typeof peer.addnode).toStrictEqual('boolean') - expect(typeof peer.startingheight).toStrictEqual('number') + if (peer.pingtime !== undefined) { + expect(typeof peer.pingtime).toStrictEqual('number') + } - expectUndefinedOrEquals('number', peer.banscore) - expectUndefinedOrEquals('number', peer.synced_headers) - expectUndefinedOrEquals('number', peer.synced_blocks) + if (peer.minping !== undefined) { + expect(typeof peer.minping).toStrictEqual('number') + } - expect(typeof peer.inflight).toStrictEqual('object') - expect(typeof peer.whitelisted).toStrictEqual('boolean') - expect(typeof peer.permissions).toStrictEqual('object') - expect(typeof peer.minfeefilter).toStrictEqual('number') + if (peer.pingwait !== undefined) { + expect(typeof peer.pingwait).toStrictEqual('number') + } + + expect(typeof peer.version).toStrictEqual('number') + expect(typeof peer.subver).toStrictEqual('string') + expect(typeof peer.inbound).toStrictEqual('boolean') + expect(typeof peer.addnode).toStrictEqual('boolean') + expect(typeof peer.startingheight).toStrictEqual('number') + + if (peer.banscore !== undefined) { + expect(typeof peer.banscore).toStrictEqual('number') + } + + if (peer.synced_headers !== undefined) { + expect(typeof peer.synced_headers).toStrictEqual('number') + } + + if (peer.synced_blocks !== undefined) { + expect(typeof peer.synced_blocks).toStrictEqual('number') + } + + expect(typeof peer.inflight).toStrictEqual('object') + expect(typeof peer.whitelisted).toStrictEqual('boolean') + expect(typeof peer.permissions).toStrictEqual('object') + expect(typeof peer.minfeefilter).toStrictEqual('number') + + for (const key in peer.bytessent_per_msg) { + expect(typeof peer.bytessent_per_msg[key]).toStrictEqual('number') + } - expectValidBytesPerMessage(peer.bytessent_per_msg) - expectValidBytesPerMessage(peer.bytesrecv_per_msg) + for (const key in peer.bytesrecv_per_msg) { + expect(typeof peer.bytesrecv_per_msg[key]).toStrictEqual('number') } - }) + } }) -} - -function expectUndefinedOrEquals (expected: any, actual?: any): void { - if (actual !== undefined) { - expect(typeof actual).toStrictEqual(expected) - } -} - -function expectUndefinedOrValidAddress (address?: string): void { - if (address !== undefined) { - expectValidAddress(address) - } -} - -function expectValidAddress (address: string): void { - expect(typeof address).toStrictEqual('string') - expect(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}(:[0-9]{1,5})?$/.test(address)).toBeTruthy() -} - -function expectValidBytesPerMessage (obj: Record): void { - for (const key in obj) { - expect(typeof obj[key]).toStrictEqual('number') - } -} +}) From 2649d770ba3ac94bd6ab5b2f1bdf2e36817a7783 Mon Sep 17 00:00:00 2001 From: Kodemon Date: Mon, 17 Jan 2022 09:06:43 +0000 Subject: [PATCH 11/11] add wait for sync before deposit --- .../jellyfish-api-core/__tests__/category/loan/takeLoan.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jellyfish-api-core/__tests__/category/loan/takeLoan.test.ts b/packages/jellyfish-api-core/__tests__/category/loan/takeLoan.test.ts index 738811d04e..ab9eecaaab 100644 --- a/packages/jellyfish-api-core/__tests__/category/loan/takeLoan.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/loan/takeLoan.test.ts @@ -339,6 +339,7 @@ describe('takeLoan success', () => { loanSchemeId: 'scheme5' }) await bob.generate(1) + await tGroup.waitForSync() await alice.rpc.loan.depositToVault({ vaultId: vaultId, from: aliceAddr, amount: '10000@DFI'