-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
packages/jellyfish-api-core/__tests__/category/blockchain.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers' | ||
import { ContainerAdapterClient } from '../container_adapter_client' | ||
import waitForExpect from 'wait-for-expect' | ||
|
||
describe('non masternode', () => { | ||
const container = new RegTestContainer() | ||
const client = new ContainerAdapterClient(container) | ||
|
||
beforeAll(async () => { | ||
await container.start() | ||
await container.waitForReady() | ||
}) | ||
|
||
afterAll(async () => { | ||
await container.stop() | ||
}) | ||
|
||
it('should getBlockchainInfo', async () => { | ||
const info = await client.blockchain.getBlockchainInfo() | ||
|
||
expect(info.chain).toBe('regtest') | ||
expect(info.blocks).toBe(0) | ||
expect(info.headers).toBe(0) | ||
|
||
expect(info.bestblockhash.length).toBe(64) | ||
expect(info.difficulty).toBeGreaterThan(0) | ||
expect(info.mediantime).toBeGreaterThan(1550000000) | ||
|
||
expect(info.verificationprogress).toBe(1) | ||
expect(info.initialblockdownload).toBe(true) | ||
expect(info.chainwork.length).toBe(64) | ||
expect(info.size_on_disk).toBeGreaterThan(0) | ||
expect(info.pruned).toBe(false) | ||
|
||
expect(Object.keys(info.softforks).length).toBe(10) | ||
|
||
expect(info.softforks.amk.type).toBe('buried') | ||
expect(info.softforks.amk.active).toBe(true) | ||
expect(info.softforks.amk.height).toBe(0) | ||
|
||
expect(info.softforks.segwit.type).toBe('buried') | ||
expect(info.softforks.segwit.active).toBe(true) | ||
expect(info.softforks.segwit.height).toBe(0) | ||
}) | ||
}) | ||
|
||
describe('masternode', () => { | ||
const container = new MasterNodeRegTestContainer() | ||
const client = new ContainerAdapterClient(container) | ||
|
||
beforeAll(async () => { | ||
await container.start() | ||
await container.waitForReady() | ||
}) | ||
|
||
afterAll(async () => { | ||
await container.stop() | ||
}) | ||
|
||
it('should getBlockchainInfo', async () => { | ||
await waitForExpect(async () => { | ||
const info = await client.blockchain.getBlockchainInfo() | ||
await expect(info.blocks).toBeGreaterThan(1) | ||
}) | ||
|
||
const info = await client.blockchain.getBlockchainInfo() | ||
|
||
expect(info.chain).toBe('regtest') | ||
expect(info.blocks).toBeGreaterThan(0) | ||
expect(info.headers).toBeGreaterThan(0) | ||
|
||
expect(info.bestblockhash.length).toBe(64) | ||
expect(info.difficulty).toBeGreaterThan(0) | ||
expect(info.mediantime).toBeGreaterThan(1550000000) | ||
|
||
expect(info.verificationprogress).toBe(1) | ||
expect(info.initialblockdownload).toBe(false) | ||
expect(info.chainwork.length).toBe(64) | ||
expect(info.size_on_disk).toBeGreaterThan(0) | ||
expect(info.pruned).toBe(false) | ||
|
||
expect(Object.keys(info.softforks).length).toBe(10) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ApiClient } from '../.' | ||
|
||
/** | ||
* Blockchain related RPC calls for DeFiChain | ||
*/ | ||
export class Blockchain { | ||
private readonly client: ApiClient | ||
|
||
constructor (client: ApiClient) { | ||
this.client = client | ||
} | ||
|
||
/** | ||
* Returns an object containing various state info regarding blockchain processing. | ||
* @return Promise<BlockchainInfo> | ||
*/ | ||
async getBlockchainInfo (): Promise<BlockchainInfo> { | ||
return await this.client.call('getblockchaininfo', [], 'number') | ||
} | ||
} | ||
|
||
/** | ||
* TODO(fuxingloh): defid prune=1 is not type supported yet | ||
*/ | ||
export interface BlockchainInfo { | ||
chain: 'main' | 'test' | 'regtest' | string | ||
blocks: number | ||
headers: number | ||
bestblockhash: string | ||
difficulty: number | ||
mediantime: number | ||
verificationprogress: number | ||
initialblockdownload: boolean | ||
chainwork: string | ||
size_on_disk: number | ||
pruned: boolean | ||
softforks: { | ||
[id: string]: { | ||
type: 'buried' | 'bip9' | ||
active: boolean | ||
height: number | ||
} | ||
} | ||
warnings: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters