Skip to content

Commit

Permalink
feat(jellyfish-api-core): add blockchain getChainTxStats RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-lim committed Feb 14, 2022
1 parent 5d99525 commit 721fe09
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/node/CATEGORIES/01-blockchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,24 @@ interface WaitBlockResult {
height: number
}
```

## getChainTxStats

Get statistics about the total number and rate of transactions in the chain.

```ts title="client.blockchain.getChainTxStats()"
interface blockchain {
getChainTxStats (nBlocks?: number, blockHash?: string): Promise<ChainTxStats>
}

interface ChainTxStats {
time: number
txcount: number
window_final_block_hash: string
window_final_block_height: number
window_block_count: number
window_tx_count: number
window_interval: number
txrate: number
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { Testing } from '@defichain/jellyfish-testing'

describe('ChainTxStats', () => {
const testing = Testing.create(new MasterNodeRegTestContainer())

beforeAll(async () => {
await testing.container.start()
})

afterAll(async () => {
await testing.container.stop()
})

it('should getChainTxStats for selected interval', async () => {
const blockHash = await testing.misc.waitForBlockHash(3)
const stats = await testing.rpc.blockchain.getChainTxStats(2, blockHash)

expect(stats.time).toBeGreaterThan(1550000000)
expect(stats.txcount).toStrictEqual(12)
expect(stats.window_final_block_hash.length).toStrictEqual(64)
expect(stats.window_final_block_height).toStrictEqual(3)
expect(stats.window_block_count).toStrictEqual(2)
expect(stats.window_tx_count).toStrictEqual(2)
expect(stats.window_interval).toStrictEqual(2)
expect(stats.txrate).toStrictEqual(1)

expect(typeof stats.time).toStrictEqual('number')
expect(typeof stats.txcount).toStrictEqual('number')
expect(typeof stats.window_final_block_hash).toStrictEqual('string')
expect(typeof stats.window_final_block_height).toStrictEqual('number')
expect(typeof stats.window_block_count).toStrictEqual('number')
expect(typeof stats.window_tx_count).toStrictEqual('number')
expect(typeof stats.window_interval).toStrictEqual('number')
expect(typeof stats.txrate).toStrictEqual('number')
})

it('should getChainTxStats for all blocks excl. genesis when called with default arguments', async () => {
await testing.misc.waitForBlockHash(4)
const tipHash = await testing.rpc.blockchain.getBlockHash(5)

const stats = await testing.rpc.blockchain.getChainTxStats() // all 4 blocks excl genesis
const statsFromTipHash = await testing.rpc.blockchain.getChainTxStats(4, tipHash) // equivalent
expect(stats).toStrictEqual(statsFromTipHash)

expect(stats.txcount).toStrictEqual(14)
expect(stats.window_final_block_height).toStrictEqual(5)
expect(stats.window_block_count).toStrictEqual(4)
expect(stats.window_tx_count).toStrictEqual(4)
expect(stats.window_interval).toStrictEqual(2)
expect(stats.txrate).toStrictEqual(2)
expect(stats.time).toBeGreaterThan(1550000000)
expect(stats.window_final_block_hash.length).toStrictEqual(64)
})

it('should getChainTxStats for blocks referencing chain tip when called with nBlocks', async () => {
await testing.misc.waitForBlockHash(4)
const tipHash = await testing.rpc.blockchain.getBlockHash(5)

const stats = await testing.rpc.blockchain.getChainTxStats(3)
const statsFromTipHash = await testing.rpc.blockchain.getChainTxStats(3, tipHash) // equivalent
expect(stats).toStrictEqual(statsFromTipHash)

expect(stats.txcount).toStrictEqual(14)
expect(stats.window_final_block_height).toStrictEqual(5)
expect(stats.window_block_count).toStrictEqual(3)
expect(stats.window_tx_count).toStrictEqual(3)
expect(stats.window_interval).toStrictEqual(2)
expect(stats.txrate).toStrictEqual(1.5)
expect(stats.time).toBeGreaterThan(1550000000)
expect(stats.window_final_block_hash.length).toStrictEqual(64)
})

it('should getChainTxStats for block and all ancestor blocks when called with blockHash', async () => {
const blockHash = await testing.misc.waitForBlockHash(4)

const stats = await testing.rpc.blockchain.getChainTxStats(undefined, blockHash)
const statsWithNBlocks = await testing.rpc.blockchain.getChainTxStats(3, blockHash) // equivalent
expect(stats).toStrictEqual(statsWithNBlocks)

expect(stats.txcount).toStrictEqual(13)
expect(stats.window_final_block_height).toStrictEqual(4)
expect(stats.window_block_count).toStrictEqual(3)
expect(stats.window_tx_count).toStrictEqual(3)
expect(stats.window_interval).toStrictEqual(2)
expect(stats.txrate).toStrictEqual(1.5)
expect(stats.time).toBeGreaterThan(1550000000)
expect(stats.window_final_block_hash.length).toStrictEqual(64)
})
})
21 changes: 21 additions & 0 deletions packages/jellyfish-api-core/src/category/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ export class Blockchain {
async waitForBlockHeight (height: number, timeout: number = 30000): Promise<WaitBlockResult> {
return await this.client.call('waitforblockheight', [height, timeout], 'number')
}

/**
* Get statistics about the total number and rate of transactions in the chain.
*
* @param {number} nBlocks - size of the window in number of blocks
* @param {number} blockHash - the hash of the block that ends the window
*/
async getChainTxStats (nBlocks?: number, blockHash?: string): Promise<ChainTxStats> {
return await this.client.call('getchaintxstats', [nBlocks, blockHash], 'number')
}
}

/**
Expand Down Expand Up @@ -415,3 +425,14 @@ export interface WaitBlockResult {
hash: string
height: number
}

export interface ChainTxStats {
time: number
txcount: number
window_final_block_hash: string
window_final_block_height: number
window_block_count: number
window_tx_count: number
window_interval: number
txrate: number
}

0 comments on commit 721fe09

Please sign in to comment.