Skip to content

Commit

Permalink
Add getNetworkInfo rpc (#180)
Browse files Browse the repository at this point in the history
* Add getnetworkinfo rpc

* Remove commas
  • Loading branch information
jingyi2811 authored May 3, 2021
1 parent 6c805af commit db95cd0
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
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
}
```

0 comments on commit db95cd0

Please sign in to comment.