From 5c33ae63b9b55d245cebb6cea0ee868f9c4b3329 Mon Sep 17 00:00:00 2001 From: Siradji Date: Wed, 19 May 2021 11:44:57 +0100 Subject: [PATCH 01/13] getBlockStats, getBestBlockHash RCP implementation --- .../__tests__/category/blockchain.test.ts | 31 ++++++++ .../src/category/blockchain.ts | 79 +++++++++++++++++++ website/docs/jellyfish/api/blockchain.md | 63 +++++++++++++++ 3 files changed, 173 insertions(+) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index 12ef84dc46..5f2deff8bc 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -314,4 +314,35 @@ describe('masternode', () => { }) }) }) + + describe('getBlockStats', () => { + it('should get blockchain start and return all values ', async () => { + const blockHash = await waitForBlockHash(1); + const stats = await client.blockchain.getBlockStats(blockHash); + + + expect(stats.height).toBeGreaterThanOrEqual(1); + expect(stats.minfee).toBeLessThanOrEqual(stats.medianfee); + expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee); + }) + + it('should get blockchain start with specific values', async () => { + const blockHash = await waitForBlockHash(1); + const stats = await client.blockchain.getBlockStats(blockHash, ["avgfee", "height"]); + + expect("height" in stats).toBeTruthy(); + expect("avgfee" in stats).toBeTruthy(); + expect(stats.height).toBeGreaterThanOrEqual(1); + expect(Object.keys(stats).length).toEqual(2); + + }) + }) + describe('getBestBlockHash', () => { + test('should Get hash of best block and return a string', async () => { + const bestBlockHash = await client.blockchain.getBestBlockHash(); + expect(bestBlockHash).toBeTruthy(); + }) + + }) + }) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 18fc2df497..878ab150da 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -152,8 +152,48 @@ export class Blockchain { async getRawMempool (verbose: boolean): Promise { return await this.client.call('getrawmempool', [verbose], 'bignumber') } + +/** + * Get block statistics for a given window. + * @param {number} hashOrHeight The block hash or height of the target block. + * @param {Array} stats Default = all values. See BlockStats Interface. + *@return {Promise} +**/ + +async getBlockStats(hashOrHeight: number | string, stats: Array = + ["avgfee", "avgfeerate", + "avgtxsize", "blockhash", + "feerate_percentiles", "height", + "ins", "maxfee", "maxfeerate", + "maxtxsize", "medianfee", + "mediantime", "mediantxsize", + "minfee", "minfeerate", + "mintxsize", "outs", + "subsidy", "swtotal_weight", + "swtotal_size", "swtxs", "time", + "total_out", "total_size", + "total_weight", "totalfee", + "txs", "utxo_increase", + "utxo_size_inc"] + + ): Promise { + return await this.client.call('getblockstats', [hashOrHeight, stats], 'number' ) +} + +/** + * Get the hash of the best (tip) block in the most-work fully-validated chain + * @returns {Promise} + */ + +async getBestBlockHash(): Promise { + return await this.client.call("getbestblockhash", [], 'number') +} } + + + + /** * TODO(fuxingloh): defid prune=1 is not type supported yet */ @@ -306,3 +346,42 @@ export interface MempoolTx { 'bip125-replaceable': boolean } } + + +export interface BlockStats { + avgfee: number + avgfeerate: number + avgtxsize: number + blockhash: number + height: number + ins: number + maxfee: number + maxfeerate: number + maxtxsize: number + medianfee: number + mediantime: number + mediantxsize: number + minfee: number + minfeerate: number + mintxsize: number + outs: number + subsidy: number + swtxs: number + time: number + totalfee: number + txs: number + "swtotal_size": number + "swtotal_weight": number + "total_out": number + "total_size": number + "total_weight": number + "utxo_increase": number + "utxo_size_inc": number + "feerate_percentiles": { + '10th_percentile_feerate': number + '25th_percentile_feerate': number + '50th_percentile_feerate': number + '75th_percentile_feerate': number + '90th_percentile_feerate': number + } +} \ No newline at end of file diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index 833a23f936..ce424e3989 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -257,3 +257,66 @@ interface MempoolTx { } } ``` + +## getBlockStats + +Get block statistics for a given window. Returns all stats values if nothing is passed in the second param. + + +```ts title="client.blockchain.getBlockStats()" + +interface blockchain { + getBlockStats(hashOrHeight: number | string): Promise + getBlockStats(hashOrHeight: number | string, stats: Array): Promise +} + +interface BlockStats { + avgfee: number + avgfeerate: number + avgtxsize: number + blockhash: number + height: number + ins: number + maxfee: number + maxfeerate: number + maxtxsize: number + medianfee: number + mediantime: number + mediantxsize: number + minfee: number + minfeerate: number + mintxsize: number + outs: number + subsidy: number + swtxs: number + time: number + totalfee: number + txs: number + "swtotal_size": number + "swtotal_weight": number + "total_out": number + "total_size": number + "total_weight": number + "utxo_increase": number + "utxo_size_inc": number + "feerate_percentiles": { + '10th_percentile_feerate': number + '25th_percentile_feerate': number + '50th_percentile_feerate': number + '75th_percentile_feerate': number + '90th_percentile_feerate': number + } +} + +``` + +## getBestBlockHash + +Get the hash of the best (tip) block in the most-work fully-validated chain. + +```ts title="client.blockchain.getBlockStats()" +interface blockchain { + getBestBlockHash (): Promise +} + +``` \ No newline at end of file From 2429bb85e3d619ba89d71a0579ba97283b47e37f Mon Sep 17 00:00:00 2001 From: Siradji Date: Wed, 19 May 2021 13:32:19 +0100 Subject: [PATCH 02/13] Fixed and implemented requested changes. --- .../__tests__/category/blockchain.test.ts | 4 ++-- .../src/category/blockchain.ts | 16 ++++++++-------- website/docs/jellyfish/api/blockchain.md | 16 ++++++++-------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index 5f2deff8bc..4fb30ac2e3 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -316,7 +316,7 @@ describe('masternode', () => { }) describe('getBlockStats', () => { - it('should get blockchain start and return all values ', async () => { + it('should get blockchain stats and return all values', async () => { const blockHash = await waitForBlockHash(1); const stats = await client.blockchain.getBlockStats(blockHash); @@ -326,7 +326,7 @@ describe('masternode', () => { expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee); }) - it('should get blockchain start with specific values', async () => { + it('should get blockchain stats with specific values', async () => { const blockHash = await waitForBlockHash(1); const stats = await client.blockchain.getBlockStats(blockHash, ["avgfee", "height"]); diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 878ab150da..0a9d91b6f3 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -370,14 +370,14 @@ export interface BlockStats { time: number totalfee: number txs: number - "swtotal_size": number - "swtotal_weight": number - "total_out": number - "total_size": number - "total_weight": number - "utxo_increase": number - "utxo_size_inc": number - "feerate_percentiles": { + swtotal_size: number + swtotal_weight: number + total_out: number + total_size: number + total_weight: number + utxo_increase: number + utxo_size_inc: number + feerate_percentiles: { '10th_percentile_feerate': number '25th_percentile_feerate': number '50th_percentile_feerate': number diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index ce424e3989..667c7e97f7 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -292,14 +292,14 @@ interface BlockStats { time: number totalfee: number txs: number - "swtotal_size": number - "swtotal_weight": number - "total_out": number - "total_size": number - "total_weight": number - "utxo_increase": number - "utxo_size_inc": number - "feerate_percentiles": { + swtotal_size: number + swtotal_weight: number + total_out: number + total_size: number + total_weight: number + utxo_increase: number + utxo_size_inc: number + feerate_percentiles: { '10th_percentile_feerate': number '25th_percentile_feerate': number '50th_percentile_feerate': number From 6405bc241e2b47bba3c435f489cd01ea4c78d438 Mon Sep 17 00:00:00 2001 From: Siradji Date: Thu, 20 May 2021 00:15:25 +0100 Subject: [PATCH 03/13] Fixed requested changes --- .../src/category/blockchain.ts | 48 +++++-------------- website/docs/jellyfish/api/blockchain.md | 2 +- 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 0a9d91b6f3..39dfe426f5 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -153,47 +153,26 @@ export class Blockchain { return await this.client.call('getrawmempool', [verbose], 'bignumber') } -/** + /** * Get block statistics for a given window. - * @param {number} hashOrHeight The block hash or height of the target block. - * @param {Array} stats Default = all values. See BlockStats Interface. - *@return {Promise} + * @param {number} hashOrHeight The block hash or height of the target block. + * @param {Array} stats Default = all values. See BlockStats Interface. + *@return {Promise} **/ -async getBlockStats(hashOrHeight: number | string, stats: Array = - ["avgfee", "avgfeerate", - "avgtxsize", "blockhash", - "feerate_percentiles", "height", - "ins", "maxfee", "maxfeerate", - "maxtxsize", "medianfee", - "mediantime", "mediantxsize", - "minfee", "minfeerate", - "mintxsize", "outs", - "subsidy", "swtotal_weight", - "swtotal_size", "swtxs", "time", - "total_out", "total_size", - "total_weight", "totalfee", - "txs", "utxo_increase", - "utxo_size_inc"] - - ): Promise { - return await this.client.call('getblockstats', [hashOrHeight, stats], 'number' ) -} + async getBlockStats (hashOrHeight: number | string, stats?: Array): Promise { + return await this.client.call('getblockstats', [hashOrHeight, stats], 'number') + } -/** + /** * Get the hash of the best (tip) block in the most-work fully-validated chain * @returns {Promise} */ - -async getBestBlockHash(): Promise { - return await this.client.call("getbestblockhash", [], 'number') -} + async getBestBlockHash (): Promise { + return await this.client.call('getbestblockhash', [], 'number') + } } - - - - /** * TODO(fuxingloh): defid prune=1 is not type supported yet */ @@ -347,7 +326,6 @@ export interface MempoolTx { } } - export interface BlockStats { avgfee: number avgfeerate: number @@ -383,5 +361,5 @@ export interface BlockStats { '50th_percentile_feerate': number '75th_percentile_feerate': number '90th_percentile_feerate': number - } -} \ No newline at end of file + } +} diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index 667c7e97f7..0494fd6d39 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -314,7 +314,7 @@ interface BlockStats { Get the hash of the best (tip) block in the most-work fully-validated chain. -```ts title="client.blockchain.getBlockStats()" +```ts title="client.blockchain.getBestBlockHash()" interface blockchain { getBestBlockHash (): Promise } From 9c582dc5d4694697276e9835de9dbe4aa35eedde Mon Sep 17 00:00:00 2001 From: Siradji Date: Thu, 20 May 2021 00:25:49 +0100 Subject: [PATCH 04/13] Minor fixes --- .../__tests__/category/blockchain.test.ts | 36 +++++++++---------- .../src/category/blockchain.ts | 1 - 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index 4fb30ac2e3..d1821f4f50 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -317,32 +317,28 @@ describe('masternode', () => { describe('getBlockStats', () => { it('should get blockchain stats and return all values', async () => { - const blockHash = await waitForBlockHash(1); - const stats = await client.blockchain.getBlockStats(blockHash); - + const blockHash = await waitForBlockHash(1) + const stats = await client.blockchain.getBlockStats(blockHash) - expect(stats.height).toBeGreaterThanOrEqual(1); - expect(stats.minfee).toBeLessThanOrEqual(stats.medianfee); - expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee); + expect(stats.height).toBeGreaterThanOrEqual(1) + expect(stats.minfee).toBeLessThanOrEqual(stats.medianfee) + expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee) }) it('should get blockchain stats with specific values', async () => { - const blockHash = await waitForBlockHash(1); - const stats = await client.blockchain.getBlockStats(blockHash, ["avgfee", "height"]); - - expect("height" in stats).toBeTruthy(); - expect("avgfee" in stats).toBeTruthy(); - expect(stats.height).toBeGreaterThanOrEqual(1); - expect(Object.keys(stats).length).toEqual(2); - + const blockHash = await waitForBlockHash(1) + const stats = await client.blockchain.getBlockStats(blockHash, ['avgfee', 'height']) + + expect('height' in stats).toBeTruthy() + expect('avgfee' in stats).toBeTruthy() + expect(stats.height).toBeGreaterThanOrEqual(1) + expect(Object.keys(stats).length).toEqual(2) }) }) describe('getBestBlockHash', () => { - test('should Get hash of best block and return a string', async () => { - const bestBlockHash = await client.blockchain.getBestBlockHash(); - expect(bestBlockHash).toBeTruthy(); - }) - + test('should Get hash of best block and return a string', async () => { + const bestBlockHash = await client.blockchain.getBestBlockHash() + expect(bestBlockHash).toBeTruthy() + }) }) - }) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 39dfe426f5..4dd242245a 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -325,7 +325,6 @@ export interface MempoolTx { 'bip125-replaceable': boolean } } - export interface BlockStats { avgfee: number avgfeerate: number From 2df14197c19b178425ea3bdc1d5b079ba555aaef Mon Sep 17 00:00:00 2001 From: Siradji Date: Mon, 24 May 2021 15:27:34 +0100 Subject: [PATCH 05/13] Changed assertion to toStricEquals, additional test for getblockstats --- .../__tests__/category/blockchain.test.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index d1821f4f50..ce6c272c14 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -329,16 +329,21 @@ describe('masternode', () => { const blockHash = await waitForBlockHash(1) const stats = await client.blockchain.getBlockStats(blockHash, ['avgfee', 'height']) - expect('height' in stats).toBeTruthy() - expect('avgfee' in stats).toBeTruthy() + expect('height' in stats).toStrictEqual(true) + expect('avgfee' in stats).toStrictEqual(true) expect(stats.height).toBeGreaterThanOrEqual(1) expect(Object.keys(stats).length).toEqual(2) }) + + it('should fail when a negative height is provided', async () => { + const promise = client.blockchain.getBlockStats(-1, ['avgfee', 'height']) + await expect(promise).rejects.toThrow('Target block height -1 is negative') + }) }) describe('getBestBlockHash', () => { test('should Get hash of best block and return a string', async () => { const bestBlockHash = await client.blockchain.getBestBlockHash() - expect(bestBlockHash).toBeTruthy() + expect(typeof bestBlockHash).toStrictEqual('string') }) }) }) From 1ef6aee27747dc00294e7770b1fca2b0cc3c7459 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Tue, 25 May 2021 08:38:36 +0100 Subject: [PATCH 06/13] Update packages/jellyfish-api-core/src/category/blockchain.ts Co-authored-by: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> --- .../src/category/blockchain.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 4dd242245a..45241dd957 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -154,23 +154,23 @@ export class Blockchain { } /** - * Get block statistics for a given window. - * @param {number} hashOrHeight The block hash or height of the target block. - * @param {Array} stats Default = all values. See BlockStats Interface. - *@return {Promise} -**/ - + * Get block statistics for a given window. + * @param {number} hashOrHeight The block hash or height of the target block. + * @param {Array} stats Default = all values. See BlockStats Interface. + * @return {Promise} + */ async getBlockStats (hashOrHeight: number | string, stats?: Array): Promise { return await this.client.call('getblockstats', [hashOrHeight, stats], 'number') } /** - * Get the hash of the best (tip) block in the most-work fully-validated chain - * @returns {Promise} - */ + * Get the hash of the best (tip) block in the most-work fully-validated chain + * @returns {Promise} + */ async getBestBlockHash (): Promise { return await this.client.call('getbestblockhash', [], 'number') } + } /** From 4a8a6da95564d1839f05bac4ea1c104cdd99d42a Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Tue, 25 May 2021 08:54:03 +0100 Subject: [PATCH 07/13] Update packages/jellyfish-api-core/__tests__/category/blockchain.test.ts Co-authored-by: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> --- .../__tests__/category/blockchain.test.ts | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index ce6c272c14..ee8681ac61 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -315,35 +315,36 @@ describe('masternode', () => { }) }) - describe('getBlockStats', () => { - it('should get blockchain stats and return all values', async () => { - const blockHash = await waitForBlockHash(1) - const stats = await client.blockchain.getBlockStats(blockHash) - - expect(stats.height).toBeGreaterThanOrEqual(1) - expect(stats.minfee).toBeLessThanOrEqual(stats.medianfee) - expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee) - }) +describe('getBlockStats', () => { + it('should get blockchain stats and return all values', async () => { + const blockHash = await waitForBlockHash(1) + const stats = await client.blockchain.getBlockStats(blockHash) + + expect(stats.height).toBeGreaterThanOrEqual(1) + expect(stats.minfee).toBeLessThanOrEqual(stats.medianfee) + expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee) + }) - it('should get blockchain stats with specific values', async () => { - const blockHash = await waitForBlockHash(1) - const stats = await client.blockchain.getBlockStats(blockHash, ['avgfee', 'height']) + it('should get blockchain stats with specific values', async () => { + const blockHash = await waitForBlockHash(1) + const stats = await client.blockchain.getBlockStats(blockHash, ['avgfee', 'height']) - expect('height' in stats).toStrictEqual(true) - expect('avgfee' in stats).toStrictEqual(true) - expect(stats.height).toBeGreaterThanOrEqual(1) - expect(Object.keys(stats).length).toEqual(2) - }) + expect('height' in stats).toStrictEqual(true) + expect('avgfee' in stats).toStrictEqual(true) + expect(stats.height).toBeGreaterThanOrEqual(1) + expect(Object.keys(stats).length).toEqual(2) + }) - it('should fail when a negative height is provided', async () => { - const promise = client.blockchain.getBlockStats(-1, ['avgfee', 'height']) - await expect(promise).rejects.toThrow('Target block height -1 is negative') - }) + it('should fail when a negative height is provided', async () => { + const promise = client.blockchain.getBlockStats(-1, ['avgfee', 'height']) + await expect(promise).rejects.toThrow('Target block height -1 is negative') }) - describe('getBestBlockHash', () => { - test('should Get hash of best block and return a string', async () => { - const bestBlockHash = await client.blockchain.getBestBlockHash() - expect(typeof bestBlockHash).toStrictEqual('string') - }) +}) + +describe('getBestBlockHash', () => { + test('should Get hash of best block and return a string', async () => { + const bestBlockHash = await client.blockchain.getBestBlockHash() + expect(typeof bestBlockHash).toStrictEqual('string') }) }) +}) From 5f236493c5b3b1e34df33046d6afe58f98f0de48 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Tue, 25 May 2021 09:18:34 +0100 Subject: [PATCH 08/13] Update packages/jellyfish-api-core/src/category/blockchain.ts Co-authored-by: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> --- packages/jellyfish-api-core/src/category/blockchain.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 45241dd957..6826fbecfc 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -325,6 +325,7 @@ export interface MempoolTx { 'bip125-replaceable': boolean } } + export interface BlockStats { avgfee: number avgfeerate: number From e48b4e7b281b3b7ebc5a9d3818f141cde16ac5c3 Mon Sep 17 00:00:00 2001 From: Siradji Date: Tue, 25 May 2021 10:34:07 +0100 Subject: [PATCH 09/13] Additional test cases for getBlockStats --- .../__tests__/category/blockchain.test.ts | 78 ++++++++++++------- .../src/category/blockchain.ts | 11 +-- website/docs/jellyfish/api/blockchain.md | 10 +-- 3 files changed, 56 insertions(+), 43 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index ee8681ac61..69eb28bd0d 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -315,36 +315,62 @@ describe('masternode', () => { }) }) -describe('getBlockStats', () => { - it('should get blockchain stats and return all values', async () => { - const blockHash = await waitForBlockHash(1) - const stats = await client.blockchain.getBlockStats(blockHash) - - expect(stats.height).toBeGreaterThanOrEqual(1) - expect(stats.minfee).toBeLessThanOrEqual(stats.medianfee) - expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee) - }) + describe('getBlockStats', () => { + it('should get blockchain stats and return all values', async () => { + const blockHash = await waitForBlockHash(1) + const stats = await client.blockchain.getBlockStats(blockHash) + + expect(stats.height).toBeGreaterThanOrEqual(1) + expect(stats.minfee).toBeLessThanOrEqual(stats.medianfee) + expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee) + expect(stats.mediantxsize).toBeLessThanOrEqual(stats.maxtxsize) + expect(stats.avgfeerate).toBeLessThanOrEqual(stats.maxfeerate) + expect(stats.mintxsize).toBeLessThanOrEqual(stats.mediantxsize) + expect(stats.utxo_increase).toStrictEqual(stats.outs - stats.ins) + + expect(typeof stats.utxo_size_inc).toStrictEqual('number') + expect(typeof stats.avgfee).toStrictEqual('number') + expect(typeof stats.avgtxsize).toStrictEqual('number') + expect(typeof stats.blockhash).toStrictEqual('string') + expect(typeof stats.outs).toStrictEqual('number') + expect(typeof stats.totalfee).toStrictEqual('number') + expect(typeof stats.total_out).toStrictEqual('number') + expect(typeof stats.total_size).toStrictEqual('number') + expect(typeof stats.total_weight).toStrictEqual('number') + expect(typeof stats.time).toStrictEqual('number') + expect(typeof stats.swtotal_size).toStrictEqual('number') + expect(typeof stats.swtxs).toStrictEqual('number') + expect(typeof stats.swtxs).toStrictEqual('number') + expect(typeof stats.subsidy).toStrictEqual('number') + + expect(stats.feerate_percentiles.length).toStrictEqual(5) + expect(stats.feerate_percentiles[0]).toBeLessThanOrEqual(stats.feerate_percentiles[1]) + expect(stats.feerate_percentiles[1]).toBeLessThanOrEqual(stats.feerate_percentiles[2]) + expect(stats.feerate_percentiles[2]).toBeLessThanOrEqual(stats.feerate_percentiles[3]) + expect(stats.feerate_percentiles[3]).toBeLessThanOrEqual(stats.feerate_percentiles[4]) + }) - it('should get blockchain stats with specific values', async () => { - const blockHash = await waitForBlockHash(1) - const stats = await client.blockchain.getBlockStats(blockHash, ['avgfee', 'height']) + it('should get blockchain stats with specific values', async () => { + const blockHash = await waitForBlockHash(1) + const stats = await client.blockchain.getBlockStats(blockHash, ['avgfee', 'height']) - expect('height' in stats).toStrictEqual(true) - expect('avgfee' in stats).toStrictEqual(true) - expect(stats.height).toBeGreaterThanOrEqual(1) - expect(Object.keys(stats).length).toEqual(2) - }) + expect('height' in stats).toStrictEqual(true) + expect('avgfee' in stats).toStrictEqual(true) + expect(stats.height).toBeGreaterThanOrEqual(1) + expect(Object.keys(stats).length).toStrictEqual(2) + }) - it('should fail when a negative height is provided', async () => { - const promise = client.blockchain.getBlockStats(-1, ['avgfee', 'height']) - await expect(promise).rejects.toThrow('Target block height -1 is negative') + it('should fail when a negative height is provided', async () => { + const promise = client.blockchain.getBlockStats(-1, ['avgfee', 'height']) + await expect(promise).rejects.toThrow('Target block height -1 is negative') + }) }) -}) -describe('getBestBlockHash', () => { - test('should Get hash of best block and return a string', async () => { - const bestBlockHash = await client.blockchain.getBestBlockHash() - expect(typeof bestBlockHash).toStrictEqual('string') + describe('getBestBlockHash', () => { + it('should get hash of best block and return a string', async () => { + const bestBlockHash = await client.blockchain.getBestBlockHash() + + expect(typeof bestBlockHash).toStrictEqual('string') + }) }) }) -}) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 6826fbecfc..2e31eea1ec 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -170,7 +170,6 @@ export class Blockchain { async getBestBlockHash (): Promise { return await this.client.call('getbestblockhash', [], 'number') } - } /** @@ -330,7 +329,7 @@ export interface BlockStats { avgfee: number avgfeerate: number avgtxsize: number - blockhash: number + blockhash: string height: number ins: number maxfee: number @@ -355,11 +354,5 @@ export interface BlockStats { total_weight: number utxo_increase: number utxo_size_inc: number - feerate_percentiles: { - '10th_percentile_feerate': number - '25th_percentile_feerate': number - '50th_percentile_feerate': number - '75th_percentile_feerate': number - '90th_percentile_feerate': number - } + feerate_percentiles: [number, number, number, number, number] } diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index 0494fd6d39..22c55e585d 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -274,7 +274,7 @@ interface BlockStats { avgfee: number avgfeerate: number avgtxsize: number - blockhash: number + blockhash: string height: number ins: number maxfee: number @@ -299,13 +299,7 @@ interface BlockStats { total_weight: number utxo_increase: number utxo_size_inc: number - feerate_percentiles: { - '10th_percentile_feerate': number - '25th_percentile_feerate': number - '50th_percentile_feerate': number - '75th_percentile_feerate': number - '90th_percentile_feerate': number - } + feerate_percentiles: [number, number, number, number, number] } ``` From e06c173b5b93cae18521f7fd15675070198559cb Mon Sep 17 00:00:00 2001 From: Siradji Date: Tue, 25 May 2021 12:25:37 +0100 Subject: [PATCH 10/13] Minor fixes --- .../jellyfish-api-core/__tests__/category/blockchain.test.ts | 4 ++-- packages/jellyfish-api-core/src/category/blockchain.ts | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index 69eb28bd0d..a42d019233 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -320,7 +320,7 @@ describe('masternode', () => { const blockHash = await waitForBlockHash(1) const stats = await client.blockchain.getBlockStats(blockHash) - expect(stats.height).toBeGreaterThanOrEqual(1) + expect(stats.height).toStrictEqual(1) expect(stats.minfee).toBeLessThanOrEqual(stats.medianfee) expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee) expect(stats.mediantxsize).toBeLessThanOrEqual(stats.maxtxsize) @@ -356,7 +356,7 @@ describe('masternode', () => { expect('height' in stats).toStrictEqual(true) expect('avgfee' in stats).toStrictEqual(true) - expect(stats.height).toBeGreaterThanOrEqual(1) + expect(stats.height).toStrictEqual(1) expect(Object.keys(stats).length).toStrictEqual(2) }) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 2e31eea1ec..c1d61a3689 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -155,6 +155,7 @@ export class Blockchain { /** * Get block statistics for a given window. + * * @param {number} hashOrHeight The block hash or height of the target block. * @param {Array} stats Default = all values. See BlockStats Interface. * @return {Promise} @@ -164,7 +165,8 @@ export class Blockchain { } /** - * Get the hash of the best (tip) block in the most-work fully-validated chain + * Get the hash of the best (tip) block in the most-work fully-validated chain. + * * @returns {Promise} */ async getBestBlockHash (): Promise { From 883247859d15a1e825dacd1e25797817b63369f0 Mon Sep 17 00:00:00 2001 From: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> Date: Wed, 26 May 2021 19:31:35 +0800 Subject: [PATCH 11/13] Update blockchain.md --- website/docs/jellyfish/api/blockchain.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index 22c55e585d..e53001c867 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -264,7 +264,6 @@ Get block statistics for a given window. Returns all stats values if nothing is ```ts title="client.blockchain.getBlockStats()" - interface blockchain { getBlockStats(hashOrHeight: number | string): Promise getBlockStats(hashOrHeight: number | string, stats: Array): Promise @@ -301,7 +300,6 @@ interface BlockStats { utxo_size_inc: number feerate_percentiles: [number, number, number, number, number] } - ``` ## getBestBlockHash @@ -312,5 +310,4 @@ Get the hash of the best (tip) block in the most-work fully-validated chain. interface blockchain { getBestBlockHash (): Promise } - -``` \ No newline at end of file +``` From e0fffee14ad5012ea21f8f609fcad81b10a19b07 Mon Sep 17 00:00:00 2001 From: Siradji Date: Wed, 26 May 2021 16:01:44 +0100 Subject: [PATCH 12/13] Minor fixes --- .../jellyfish-api-core/__tests__/category/blockchain.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index a42d019233..669945a2b6 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -325,6 +325,7 @@ describe('masternode', () => { expect(stats.medianfee).toBeLessThanOrEqual(stats.maxfee) expect(stats.mediantxsize).toBeLessThanOrEqual(stats.maxtxsize) expect(stats.avgfeerate).toBeLessThanOrEqual(stats.maxfeerate) + expect(stats.minfeerate).toBeLessThanOrEqual(stats.maxfeerate) expect(stats.mintxsize).toBeLessThanOrEqual(stats.mediantxsize) expect(stats.utxo_increase).toStrictEqual(stats.outs - stats.ins) @@ -341,7 +342,10 @@ describe('masternode', () => { expect(typeof stats.swtotal_size).toStrictEqual('number') expect(typeof stats.swtxs).toStrictEqual('number') expect(typeof stats.swtxs).toStrictEqual('number') + expect(typeof stats.swtotal_weight).toStrictEqual('number') expect(typeof stats.subsidy).toStrictEqual('number') + expect(typeof stats.txs).toStrictEqual('number') + expect(typeof stats.minfeerate).toStrictEqual('number') expect(stats.feerate_percentiles.length).toStrictEqual(5) expect(stats.feerate_percentiles[0]).toBeLessThanOrEqual(stats.feerate_percentiles[1]) From 26eeb71b694dc6ea7abc21846af836f3f167247c Mon Sep 17 00:00:00 2001 From: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> Date: Thu, 27 May 2021 12:49:53 +0800 Subject: [PATCH 13/13] Update blockchain.md --- website/docs/jellyfish/api/blockchain.md | 1 - 1 file changed, 1 deletion(-) diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index e53001c867..46572c5209 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -262,7 +262,6 @@ interface MempoolTx { Get block statistics for a given window. Returns all stats values if nothing is passed in the second param. - ```ts title="client.blockchain.getBlockStats()" interface blockchain { getBlockStats(hashOrHeight: number | string): Promise