diff --git a/docs/node/CATEGORIES/03-net.md b/docs/node/CATEGORIES/03-net.md index 66aa7b2934..7b6514f7b1 100644 --- a/docs/node/CATEGORIES/03-net.md +++ b/docs/node/CATEGORIES/03-net.md @@ -69,6 +69,32 @@ export interface PeerInfo { } ``` +## getNetTotals + +Returns information about network traffic, including bytes in, bytes out, and current time. + +```ts title="client.net.getNetTotals()" +interface net { + getNetTotals (): Promise +} + +interface NetTotals { + totalbytesrecv: number + totalbytessent: number + timemillis: number + uploadtarget: UploadTarget +} + +interface UploadTarget { + timeframe: number + target: number + target_reached: boolean + serve_historical_blocks: boolean + bytes_left_in_cycle: number + time_left_in_cycle: number +} +``` + ## getNetworkInfo Returns an object containing various state info regarding P2P networking. diff --git a/packages/jellyfish-api-core/__tests__/category/net/getNetTotals.test.ts b/packages/jellyfish-api-core/__tests__/category/net/getNetTotals.test.ts new file mode 100644 index 0000000000..35a8d750f2 --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/net/getNetTotals.test.ts @@ -0,0 +1,66 @@ +import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers/dist/index' +import { net } from '../../../src' +import { ContainerAdapterClient } from '../../container_adapter_client' + +describe('Network without masternode', () => { + const container = new RegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + }) + + afterAll(async () => { + await container.stop() + }) + + it('should getNetTotals', async () => { + const info: net.NetTotals = await client.net.getNetTotals() + + expect(info).toStrictEqual({ + totalbytesrecv: expect.any(Number), + totalbytessent: expect.any(Number), + timemillis: expect.any(Number), + uploadtarget: { + timeframe: expect.any(Number), + target: expect.any(Number), + target_reached: expect.any(Boolean), + serve_historical_blocks: expect.any(Boolean), + bytes_left_in_cycle: expect.any(Number), + time_left_in_cycle: expect.any(Number) + } + }) + }) +}) + +describe('Network on masternode', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.generate(1) + }) + + afterAll(async () => { + await container.stop() + }) + + it('should getNetTotals', async () => { + const info: net.NetTotals = await client.net.getNetTotals() + + expect(info).toStrictEqual({ + totalbytesrecv: expect.any(Number), + totalbytessent: expect.any(Number), + timemillis: expect.any(Number), + uploadtarget: { + timeframe: expect.any(Number), + target: expect.any(Number), + target_reached: expect.any(Boolean), + serve_historical_blocks: expect.any(Boolean), + bytes_left_in_cycle: expect.any(Number), + time_left_in_cycle: expect.any(Number) + } + }) + }) +}) diff --git a/packages/jellyfish-api-core/src/category/net.ts b/packages/jellyfish-api-core/src/category/net.ts index 34f71d53d2..61f4105b2e 100644 --- a/packages/jellyfish-api-core/src/category/net.ts +++ b/packages/jellyfish-api-core/src/category/net.ts @@ -28,6 +28,15 @@ export class Net { return await this.client.call('getpeerinfo', [], 'number') } + /** + * Returns information about network traffic, including bytes in, bytes out, and current time. + * + * @return {Promise} + */ + async getNetTotals (): Promise { + return await this.client.call('getnettotals', [], 'number') + } + /** * Returns an object containing various state info regarding P2P networking. * @@ -113,3 +122,19 @@ export interface LocalAddress { port: number score: number } + +export interface NetTotals { + totalbytesrecv: number + totalbytessent: number + timemillis: number + uploadtarget: UploadTarget +} + +export interface UploadTarget { + timeframe: number + target: number + target_reached: boolean + serve_historical_blocks: boolean + bytes_left_in_cycle: number + time_left_in_cycle: number +}