Skip to content

Commit

Permalink
added getBlockchainInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh committed Mar 29, 2021
1 parent 1af1690 commit d4be3f0
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
84 changes: 84 additions & 0 deletions packages/jellyfish-api-core/__tests__/category/blockchain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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(Object.keys(info.softforks).length).toBe(10)

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)

expect(Object.keys(info.softforks).length).toBe(10)
})
})
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
}

/**
* Returns an object containing 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
}
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

0 comments on commit d4be3f0

Please sign in to comment.