-
Notifications
You must be signed in to change notification settings - Fork 37
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
|
@@ -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() | ||
|
@@ -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) | ||
|
@@ -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) | ||
} | ||
|
||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). | ||
* | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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.