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

feat(jellyfish-api-core): add blockchain getChainTxStats RPC #1051

Merged
merged 5 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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)
eli-lim marked this conversation as resolved.
Show resolved Hide resolved
expect(stats.txcount).toStrictEqual(12)
expect(stats.window_final_block_hash.length).toStrictEqual(64)
eli-lim marked this conversation as resolved.
Show resolved Hide resolved
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')
eli-lim marked this conversation as resolved.
Show resolved Hide resolved
})

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 {string} blockHash - the hash of the block that ends the window
eli-lim marked this conversation as resolved.
Show resolved Hide resolved
*/
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
}