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 new getBlockHeader rpc #163

Merged
merged 2 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -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 @@ -61,7 +61,7 @@ describe('masternode', () => {
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 Down Expand Up @@ -89,7 +89,7 @@ describe('masternode', () => {
async function waitForBlockHash (height: number): Promise<string> {
await waitForExpect(async () => {
const info = await client.blockchain.getBlockchainInfo()
await expect(info.blocks).toBeGreaterThan(height)
expect(info.blocks).toBeGreaterThan(height)
})

return await client.blockchain.getBlockHash(height)
Expand Down Expand Up @@ -157,11 +157,57 @@ describe('masternode', () => {
})
})

describe('getBlockHeader', () => {
/**
* 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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing that everyone is using this utility for waitForBlockHash, you should refactor and move it towards the top level and remove all duplicates.


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 just a string that is serialized, hex-encoded data for block', async () => {
const blockHash = await waitForBlockHash(1)
const hash: string = await client.blockchain.getBlockHeader(blockHash, false)
expect(hash).not.toBeNull()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert test this is a 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)
Expand All @@ -174,7 +220,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 about 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 an Object with information about block header.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns a string that is serialized, hex-encoded data for blockheader.

*
* @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
29 changes: 29 additions & 0 deletions website/docs/jellyfish/api/blockchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ 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>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

}

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