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

blockchain.getBlockchainInfo #89

Merged
merged 4 commits into from
Apr 1, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../container_adapter_client'
import waitForExpect from 'wait-for-expect'

describe('non masternode', () => {
const container = new RegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
})

afterAll(async () => {
await container.stop()
})

it('should getBlockchainInfo', async () => {
const info = await client.blockchain.getBlockchainInfo()

expect(info.chain).toBe('regtest')
expect(info.blocks).toBe(0)
expect(info.headers).toBe(0)

expect(info.bestblockhash.length).toBe(64)
expect(info.difficulty).toBeGreaterThan(0)
expect(info.mediantime).toBeGreaterThan(1550000000)

expect(info.verificationprogress).toBe(1)
expect(info.initialblockdownload).toBe(true)
expect(info.chainwork.length).toBe(64)
expect(info.size_on_disk).toBeGreaterThan(0)
expect(info.pruned).toBe(false)

expect(info.softforks.amk.type).toBe('buried')
expect(info.softforks.amk.active).toBe(true)
expect(info.softforks.amk.height).toBe(0)

expect(info.softforks.segwit.type).toBe('buried')
expect(info.softforks.segwit.active).toBe(true)
expect(info.softforks.segwit.height).toBe(0)
})
})

describe('masternode', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
})

afterAll(async () => {
await container.stop()
})

it('should getBlockchainInfo', async () => {
await waitForExpect(async () => {
const info = await client.blockchain.getBlockchainInfo()
await expect(info.blocks).toBeGreaterThan(1)
})

const info = await client.blockchain.getBlockchainInfo()

expect(info.chain).toBe('regtest')
expect(info.blocks).toBeGreaterThan(0)
expect(info.headers).toBeGreaterThan(0)

expect(info.bestblockhash.length).toBe(64)
expect(info.difficulty).toBeGreaterThan(0)
expect(info.mediantime).toBeGreaterThan(1550000000)

expect(info.verificationprogress).toBe(1)
expect(info.initialblockdownload).toBe(false)
expect(info.chainwork.length).toBe(64)
expect(info.size_on_disk).toBeGreaterThan(0)
expect(info.pruned).toBe(false)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ describe('masternode', () => {
})

it('should getMintingInfo', async () => {
const info = await client.mining.getMintingInfo()

await waitForExpect(async () => {
const info = await container.getMintingInfo()
expect(info.blocks).toBeGreaterThan(1)
const info = await client.mining.getMintingInfo()
await expect(info.blocks).toBeGreaterThan(1)
})

const info = await client.mining.getMintingInfo()

expect(info.blocks).toBeGreaterThan(0)

expect(info.currentblockweight).toBeGreaterThan(0)
Expand Down
45 changes: 45 additions & 0 deletions packages/jellyfish-api-core/src/category/blockchain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ApiClient } from '../.'

/**
* Blockchain related RPC calls for DeFiChain
*/
export class Blockchain {
private readonly client: ApiClient

constructor (client: ApiClient) {
this.client = client
}

/**
* Get various state info regarding blockchain processing.
* @return Promise<BlockchainInfo>
*/
async getBlockchainInfo (): Promise<BlockchainInfo> {
return await this.client.call('getblockchaininfo', [], 'number')
}
}

/**
* TODO(fuxingloh): defid prune=1 is not type supported yet
*/
export interface BlockchainInfo {
chain: 'main' | 'test' | 'regtest' | string
blocks: number
headers: number
bestblockhash: string
difficulty: number
mediantime: number
verificationprogress: number
initialblockdownload: boolean
chainwork: string
size_on_disk: number
pruned: boolean
softforks: {
[id: string]: {
type: 'buried' | 'bip9'
active: boolean
height: number
}
}
warnings: string
}
37 changes: 20 additions & 17 deletions packages/jellyfish-api-core/src/category/mining.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
import { ApiClient } from '../.'

export interface MintingInfo {
blocks: number
currentblockweight?: number
currentblocktx?: number
difficulty: string
isoperator: boolean
masternodeid?: string
masternodeoperator?: string
masternodestate?: 'PRE_ENABLED' | 'ENABLED' | 'PRE_RESIGNED' | 'RESIGNED' | 'PRE_BANNED' | 'BANNED'
generate?: boolean
mintedblocks?: number
networkhashps: number
pooledtx: number
chain: 'main' | 'test' | 'regtest' | string
warnings: string
}

/**
* Minting related RPC calls for DeFiChain
*/
Expand Down Expand Up @@ -46,3 +29,23 @@ export class Mining {
return await this.client.call('getmintinginfo', [], 'number')
}
}

/**
* Minting related information
*/
export interface MintingInfo {
blocks: number
currentblockweight?: number
currentblocktx?: number
difficulty: string
isoperator: boolean
masternodeid?: string
masternodeoperator?: string
masternodestate?: 'PRE_ENABLED' | 'ENABLED' | 'PRE_RESIGNED' | 'RESIGNED' | 'PRE_BANNED' | 'BANNED'
generate?: boolean
mintedblocks?: number
networkhashps: number
pooledtx: number
chain: 'main' | 'test' | 'regtest' | string
warnings: string
}
3 changes: 3 additions & 0 deletions packages/jellyfish-api-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Precision } from '@defichain/jellyfish-json'
import { Blockchain } from './category/blockchain'
import { Mining } from './category/mining'
import { Wallet } from './category/wallet'

export * from '@defichain/jellyfish-json'
export * from './category/blockchain'
export * from './category/mining'
export * from './category/wallet'

/**
* ApiClient; a protocol agnostic DeFiChain node client, RPC calls are separated into their category.
*/
export abstract class ApiClient {
public readonly blockchain = new Blockchain(this)
public readonly mining = new Mining(this)
public readonly wallet = new Wallet(this)

Expand Down
46 changes: 46 additions & 0 deletions website/docs/jellyfish/api/blockchain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
id: blockchain
title: Blockchain API
sidebar_label: Blockchain API
slug: /jellyfish/api/blockchain
---

```js
import {Client} from '@defichain/jellyfish'
const client = new Client()

// Using client.blockchain.
const something = await client.blockchain.method()
```

## getBlockchainInfo

Get various state info regarding blockchain processing.

```ts title="client.blockchain.getBlockchainInfo()"
interface blockchain {
getBlockchainInfo (): Promise<BlockchainInfo>
}

interface BlockchainInfo {
chain: 'main' | 'test' | 'regtest' | string
blocks: number
headers: number
bestblockhash: string
difficulty: number
mediantime: number
verificationprogress: number
initialblockdownload: boolean
chainwork: string
size_on_disk: number
pruned: boolean
softforks: {
[id: string]: {
type: 'buried' | 'bip9'
active: boolean
height: number
}
}
warnings: string
}
```
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
label: 'DeFi APIs',
collapsed: false,
items: [
'jellyfish/api/blockchain',
'jellyfish/api/mining',
'jellyfish/api/wallet'
]
Expand Down