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

feat(jellyfish-api-core): add net getNetTotals RPC #1843

Merged
merged 5 commits into from
Oct 27, 2022
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
26 changes: 26 additions & 0 deletions docs/node/CATEGORIES/03-net.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetTotals>
}

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
marktanrj marked this conversation as resolved.
Show resolved Hide resolved
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)
}
})
})
})
25 changes: 25 additions & 0 deletions packages/jellyfish-api-core/src/category/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetTotals>}
*/
async getNetTotals (): Promise<NetTotals> {
return await this.client.call('getnettotals', [], 'number')
}

/**
* Returns an object containing various state info regarding P2P networking.
*
Expand Down Expand Up @@ -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
}