Skip to content

Commit

Permalink
Add getChainTips rpc (#205)
Browse files Browse the repository at this point in the history
* Add getChainTips rpc

* Minor changes

* Minor

* Minor fix
  • Loading branch information
jingyi2811 authored May 12, 2021
1 parent a7dc2e2 commit f37cfe7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../container_adapter_client'
import waitForExpect from 'wait-for-expect'
import { BigNumber, blockchain, wallet } from '../../src'
Expand Down Expand Up @@ -235,6 +235,20 @@ describe('masternode', () => {
})
})

describe('getChainTips', () => {
it('should getChainTips', async () => {
const chainTips: blockchain.ChainTip[] = await client.blockchain.getChainTips()
for (let i = 0; i < chainTips.length; i += 1) {
const data = chainTips[i]
expect(data.height).toBeGreaterThan(0)
expect(typeof data.hash).toBe('string')
expect(data.hash.length).toBe(64)
expect(data.branchlen).toBeGreaterThanOrEqual(0)
expect(['invalid', 'headers-only', 'valid-headers', 'valid-fork', 'active'].includes(data.status)).toBe(true)
}
})
})

describe('getRawMempool', () => {
let transactionId = ''

Expand Down
17 changes: 17 additions & 0 deletions packages/jellyfish-api-core/src/category/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ export class Blockchain {
return await this.client.call('getblockheader', [hash, verbosity], 'number')
}

/**
* Return information about all known tips in the block tree
* including the main chain as well as orphaned branches.
*
* @return {Promise<ChainTip[]>}
*/
async getChainTips (): Promise<ChainTip[]> {
return await this.client.call('getchaintips', [], 'number')
}

/**
* Get details of unspent transaction output (UTXO).
*
Expand Down Expand Up @@ -259,6 +269,13 @@ export interface ScriptPubKey {
addresses: string[]
}

export interface ChainTip {
height: number
hash: string
branchlen: number
status: string
}

export interface MempoolTx {
[key: string]: {
vsize: BigNumber
Expand Down
18 changes: 18 additions & 0 deletions website/docs/jellyfish/api/blockchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ interface blockchain {
}
```

## getChainTips

Return information about all known tips in the block tree
including the main chain as well as orphaned branches.

```ts title="client.blockchain.getChainTips()"
interface blockchain {
getChainTips (): Promise<ChainTip[]>
}

interface ChainTip {
height: number
hash: string
branchlen: number
status: string
}
```

## getTxOut

Get details of unspent transaction output (UTXO).
Expand Down

0 comments on commit f37cfe7

Please sign in to comment.