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

Add getNetworkInfo rpc #180

Merged
merged 2 commits into from
May 3, 2021
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
77 changes: 77 additions & 0 deletions packages/jellyfish-api-core/__tests__/category/net.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../container_adapter_client'
import { net } from '../../src'

describe('non masternode', () => {
const container = new RegTestContainer()
Expand All @@ -18,6 +19,44 @@ describe('non masternode', () => {
const count = await client.net.getConnectionCount()
expect(count).toBeGreaterThanOrEqual(0)
})

it('should getNetworkInfo', async () => {
const info: net.NetworkInfo = await client.net.getNetworkInfo()
expect(info.version).toBeGreaterThanOrEqual(0)
expect(typeof info.subversion).toBe('string')
expect(info.protocolversion).toBeGreaterThanOrEqual(0)
expect(typeof info.localservices).toBe('string')
expect(typeof info.localrelay).toBe('boolean')
expect(info.timeoffset).toBeGreaterThanOrEqual(0)
expect(info.connections).toBeGreaterThanOrEqual(0)
expect(typeof info.networkactive).toBe('boolean')

const networks = info.networks

for (let i = 0; i < networks.length; i += 1) {
const network = networks[i]
expect(['ipv4', 'ipv6', 'onion']).toContain(network.name)
expect(typeof network.limited).toBe('boolean')
expect(typeof network.reachable).toBe('boolean')
expect(typeof network.proxy).toBe('string')
expect(typeof network.proxy_randomize_credentials).toBe('boolean')
}

expect(info.relayfee).toBeGreaterThanOrEqual(0)
expect(info.incrementalfee).toBeGreaterThanOrEqual(0)

const localaddresses = info.localaddresses

for (let i = 0; i < localaddresses.length; i += 1) {
const localaddress = localaddresses[i]
expect(localaddress.address).toBe('string')
expect(localaddress.port).toBeGreaterThanOrEqual(0)
expect(localaddress.port).toBeLessThanOrEqual(65535)
expect(localaddress.score).toBeGreaterThanOrEqual(0)
}

expect(typeof info.warnings).toBe('string')
})
})

describe('masternode', () => {
Expand All @@ -38,4 +77,42 @@ describe('masternode', () => {
const count = await client.net.getConnectionCount()
expect(count).toBeGreaterThanOrEqual(0)
})

it('should getNetworkInfo', async () => {
const info: net.NetworkInfo = await client.net.getNetworkInfo()
expect(info.version).toBeGreaterThanOrEqual(0)
expect(typeof info.subversion).toBe('string')
expect(info.protocolversion).toBeGreaterThanOrEqual(0)
expect(typeof info.localservices).toBe('string')
expect(typeof info.localrelay).toBe('boolean')
expect(info.timeoffset).toBeGreaterThanOrEqual(0)
expect(info.connections).toBeGreaterThanOrEqual(0)
expect(typeof info.networkactive).toBe('boolean')

const networks = info.networks

for (let i = 0; i < networks.length; i += 1) {
const network = networks[i]
expect(['ipv4', 'ipv6', 'onion']).toContain(network.name)
expect(typeof network.limited).toBe('boolean')
expect(typeof network.reachable).toBe('boolean')
expect(typeof network.proxy).toBe('string')
expect(typeof network.proxy_randomize_credentials).toBe('boolean')
}

expect(info.relayfee).toBeGreaterThanOrEqual(0)
expect(info.incrementalfee).toBeGreaterThanOrEqual(0)

const localaddresses = info.localaddresses

for (let i = 0; i < localaddresses.length; i += 1) {
const localaddress = localaddresses[i]
expect(localaddress.address).toBe('string')
expect(localaddress.port).toBeGreaterThanOrEqual(0)
expect(localaddress.port).toBeLessThanOrEqual(65535)
expect(localaddress.score).toBeGreaterThanOrEqual(0)
}

expect(typeof info.warnings).toBe('string')
})
})
39 changes: 39 additions & 0 deletions packages/jellyfish-api-core/src/category/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,43 @@ export class Net {
async getConnectionCount (): Promise<number> {
return await this.client.call('getconnectioncount', [], 'number')
}

/**
* Returns an object containing various state info regarding P2P networking.
*
* @return {Promise<NetworkInfo>}
*/
async getNetworkInfo (): Promise<NetworkInfo> {
return await this.client.call('getnetworkinfo', [], 'number')
}
}

export interface NetworkInfo {
version: number
subversion: string
protocolversion: number
localservices: string
localrelay: boolean
timeoffset: number
connections: number
networkactive: boolean
networks: Network[]
relayfee: number
incrementalfee: number
localaddresses: LocalAddress[]
warnings: string
}

export interface Network {
name: string
limited: boolean
reachable: boolean
proxy: string
proxy_randomize_credentials: boolean
}

export interface LocalAddress {
address: string
port: number
score: number
}
40 changes: 40 additions & 0 deletions website/docs/jellyfish/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,43 @@ interface net {
getConnectionCount (): Promise<number>
}
```

## getNetworkInfo

Returns an object containing various state info regarding P2P networking.

```ts title="client.net.getNetworkInfo()"
interface net {
getNetworkInfo (): Promise<NetworkInfo>
}

interface NetworkInfo {
version: number
subversion: string
protocolversion: number
localservices: string
localrelay: boolean
timeoffset: number
connections: number
networkactive: boolean
networks: Network[]
relayfee: number
incrementalfee: number
localaddresses: LocalAddress[]
warnings: string
}

interface Network {
name: string
limited: boolean
reachable: boolean
proxy: string
proxy_randomize_credentials: boolean
}

interface LocalAddress {
address: string
port: number
score: number
}
```