Skip to content

Commit

Permalink
Add new getBlockHeader rpc (#163)
Browse files Browse the repository at this point in the history
* Add getBlockHeader rpc
* Some minor fixes
  • Loading branch information
jingyi2811 authored Apr 27, 2021
1 parent 365cb35 commit 8489cb1
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 19 deletions.
70 changes: 51 additions & 19 deletions packages/jellyfish-api-core/__tests__/category/blockchain.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../container_adapter_client'
import waitForExpect from 'wait-for-expect'
import { BigNumber, Block, MempoolTx, Transaction, WalletFlag } from '../../src'
import { BigNumber, Block, BlockHeader, MempoolTx, Transaction, WalletFlag } from '../../src'

describe('non masternode', () => {
const container = new RegTestContainer()
Expand Down Expand Up @@ -57,11 +57,23 @@ describe('masternode', () => {
await container.stop()
})

/**
* Wait for block hash to reach a certain height
*/
async function waitForBlockHash (height: number): Promise<string> {
await waitForExpect(async () => {
const info = await client.blockchain.getBlockchainInfo()
expect(info.blocks).toBeGreaterThan(height)
})

return await client.blockchain.getBlockHash(height)
}

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

const info = await client.blockchain.getBlockchainInfo()
Expand All @@ -83,23 +95,10 @@ describe('masternode', () => {
})

describe('getBlock', () => {
/**
* Wait for block hash to reach a certain height
*/
async function waitForBlockHash (height: number): Promise<string> {
await waitForExpect(async () => {
const info = await client.blockchain.getBlockchainInfo()
await expect(info.blocks).toBeGreaterThan(height)
})

return await client.blockchain.getBlockHash(height)
}

it('should getBlock with verbosity 0 and return just a string that is serialized, hex-encoded data for block', async () => {
const blockHash = await waitForBlockHash(1)
const hash: string = await client.blockchain.getBlock(blockHash, 0)

expect(hash).not.toBeNull()
expect(typeof hash).toBe('string')
})

it('should getBlock with verbosity 1 and return block with tx as hex', async () => {
Expand Down Expand Up @@ -157,15 +156,48 @@ describe('masternode', () => {
})
})

describe('getBlockHeader', () => {
it('should getBlockHeader with verbosity true and return block with tx as hex', async () => {
const blockHash = await waitForBlockHash(1)
const blockHeader: BlockHeader = await client.blockchain.getBlockHeader(blockHash, true)

expect(blockHeader.hash.length).toBe(64)

expect(blockHeader.confirmations).toBeGreaterThanOrEqual(2)
expect(blockHeader.height).toBeGreaterThanOrEqual(1)

expect(blockHeader.version).toBeGreaterThanOrEqual(536870912)
expect(blockHeader.versionHex).toStrictEqual('20000000')
expect(blockHeader.merkleroot.length).toBe(64)

expect(blockHeader.time).toBeGreaterThan(1)
expect(blockHeader.mediantime).toBeGreaterThan(1)

expect(blockHeader.bits).toStrictEqual('207fffff')
expect(blockHeader.difficulty).toBeGreaterThan(0)

expect(blockHeader.chainwork.length).toBe(64)
expect(blockHeader.nTx).toBeGreaterThanOrEqual(1)
expect(blockHeader.previousblockhash.length).toBe(64)
expect(blockHeader.nextblockhash.length).toBe(64)
})

it('should getBlockHeader with verbosity false and return a string that is serialized, hex-encoded data for block header', async () => {
const blockHash = await waitForBlockHash(1)
const hash: string = await client.blockchain.getBlockHeader(blockHash, false)
expect(typeof hash).toBe('string')
})
})

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

const blockHash: string = await client.blockchain.getBlockHash(1)
expect(blockHash).not.toBeNull()
expect(typeof blockHash).toBe('string')
expect(blockHash.length).toBe(64)
})
})
Expand All @@ -174,7 +206,7 @@ describe('masternode', () => {
it('should getBlockCount', async () => {
await waitForExpect(async () => {
const info = await client.blockchain.getBlockchainInfo()
await expect(info.blocks).toBeGreaterThan(1)
expect(info.blocks).toBeGreaterThan(1)
})

const blockCount: number = await client.blockchain.getBlockCount()
Expand Down
41 changes: 41 additions & 0 deletions packages/jellyfish-api-core/src/category/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ export class Blockchain {
return await this.client.call('getblock', [hash, verbosity], 'number')
}

/**
* Get block header data with particular header hash.
* Returns an Object with information for block header.
*
* @param {string} hash of the block
* @param {boolean} verbosity true
* @return {Promise<BlockHeader>}
*/
getBlockHeader (hash: string, verbosity: true): Promise<BlockHeader>

/**
* Get block header data with particular header hash.
* Returns a string that is serialized, hex-encoded data for block header.
*
* @param {string} hash of the block
* @param {boolean} verbosity false
* @return {Promise<string>}
*/
getBlockHeader (hash: string, verbosity: false): Promise<string>

async getBlockHeader (hash: string, verbosity: boolean): Promise<string | BlockHeader> {
return await this.client.call('getblockheader', [hash, verbosity], 'number')
}

/**
* Get details of unspent transaction output (UTXO).
*
Expand Down Expand Up @@ -167,6 +191,23 @@ export interface Block<T> {
nextblockhash: string
}

export interface BlockHeader {
hash: string
confirmations: number
height: number
version: number
versionHex: string
merkleroot: string
time: number
mediantime: number
bits: string
difficulty: number
chainwork: string
nTx: number
previousblockhash: string
nextblockhash: string
}

export interface Transaction {
txid: string
hash: string
Expand Down
28 changes: 28 additions & 0 deletions website/docs/jellyfish/api/blockchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ interface Vout {
}
```

## getBlockHeader

Get block header data with particular header hash.

```ts title="client.blockchain.getBlockHeader()"
interface blockchain {
getBlockHeader (hash: string, verbosity: true): Promise<BlockHeader>
getBlockHeader (hash: string, verbosity: false): Promise<string>
}

interface BlockHeader {
hash: string
confirmations: number
height: number
version: number
versionHex: string
merkleroot: string
time: number
mediantime: number
bits: string
difficulty: number
chainwork: string
nTx: number
previousblockhash: string
nextblockhash: string
}
```

## getBlockHash

Get a hash of block in best-block-chain at height provided.
Expand Down

0 comments on commit 8489cb1

Please sign in to comment.