Skip to content

Commit

Permalink
feat(jellyfish-api-core): add net getNetTotals RPC (#1843)
Browse files Browse the repository at this point in the history
<!--  Thanks for sending a pull request! -->

#### What this PR does / why we need it:
/kind feature

#### Which issue(s) does this PR fixes?:
<!--
(Optional) Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes part of #48 
- Implements `getnettotals` type for RPC.

Signed-off-by: Mark Tan <[email protected]>
Co-authored-by: Fuxing Loh <[email protected]>
  • Loading branch information
marktanrj and fuxingloh authored Oct 27, 2022
1 parent 406808a commit cfde140
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
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()
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
}

0 comments on commit cfde140

Please sign in to comment.