diff --git a/docs/node/CATEGORIES/11-masternode.md b/docs/node/CATEGORIES/11-masternode.md index 108e5e71a6..962e624150 100644 --- a/docs/node/CATEGORIES/11-masternode.md +++ b/docs/node/CATEGORIES/11-masternode.md @@ -190,6 +190,21 @@ interface masternode { } ``` +## getAnchorTeams + +Returns the auth and confirm anchor masternode teams at current or specified height + +```ts title="client.masternode.getAnchorTeams" +interface masternode { + getAnchorTeams (blockHeight?: number): Promise +} + +interface AnchorTeamResult { + auth: string[] + confirm: string[] +} +``` + ## getActiveMasternodeCount Returns number of unique masternodes in the last specified number of blocks. diff --git a/packages/jellyfish-api-core/__tests__/category/masternode/getAnchorTeams.test.ts b/packages/jellyfish-api-core/__tests__/category/masternode/getAnchorTeams.test.ts new file mode 100644 index 0000000000..8e05cafc76 --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/masternode/getAnchorTeams.test.ts @@ -0,0 +1,75 @@ +import { RegTestFoundationKeys } from '@defichain/jellyfish-network' +import { TestingGroup } from '@defichain/jellyfish-testing' +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' + +describe('Masternode', () => { + const tGroup = TestingGroup.create(1) + + beforeAll(async () => { + await tGroup.start() + }) + + afterAll(async () => { + await tGroup.stop() + }) + + it('should be empty when block is not 15', async () => { + for (let i = 0; i < 14; i++) { + await tGroup.get(0).generate(1) + await tGroup.waitForSync() + const blockNumber = await tGroup.get(0).rpc.blockchain.getBlockCount() + expect(blockNumber).toBeLessThan(15) + + const anchorTeams = await tGroup.get(0).rpc.masternode.getAnchorTeams() + expect(anchorTeams.auth).toHaveLength(0) + expect(anchorTeams.confirm).toHaveLength(0) + } + }) + + it('should getAnchorTeams', async () => { + await tGroup.get(0).container.waitForBlockHeight(14) // wait for block height nore than 14 + const blockNumber = await tGroup.get(0).rpc.blockchain.getBlockCount() + expect(blockNumber).toEqual(15) + + const anchorTeams = await tGroup.get(0).rpc.masternode.getAnchorTeams() + expect(anchorTeams.auth).toStrictEqual([RegTestFoundationKeys[0].operator.address]) + expect(anchorTeams.confirm).toStrictEqual([RegTestFoundationKeys[0].operator.address]) + }) + + it('should getAnchorTeams correctly when a new anchor team has been added', async () => { + // add another Test container + const newTestContainer = new MasterNodeRegTestContainer(RegTestFoundationKeys[1]) + await newTestContainer.start() + + await tGroup.add(newTestContainer) + await tGroup.waitForSync() + + await tGroup.get(1).generate(15) + await tGroup.waitForSync() + const anchorTeams = await tGroup.get(1).rpc.masternode.getAnchorTeams() + expect(anchorTeams.auth).toHaveLength(2) + expect(anchorTeams.confirm).toHaveLength(2) + expect(anchorTeams.auth).toEqual( + expect.arrayContaining( + [RegTestFoundationKeys[0].operator.address, + RegTestFoundationKeys[1].operator.address] + ) + ) + expect(anchorTeams.confirm).toEqual( + expect.arrayContaining( + [RegTestFoundationKeys[0].operator.address, + RegTestFoundationKeys[1].operator.address] + ) + ) + }) + + it('should getAnchorTeams correctly when blockheight is passed in', async () => { + const anchorTeamsBeforeFirst = await tGroup.get(0).rpc.masternode.getAnchorTeams(14) + expect(anchorTeamsBeforeFirst.auth).toHaveLength(0) + expect(anchorTeamsBeforeFirst.confirm).toHaveLength(0) + + const anchorTeamsBeforeSecond = await tGroup.get(0).rpc.masternode.getAnchorTeams(29) + expect(anchorTeamsBeforeSecond.auth).toStrictEqual([RegTestFoundationKeys[0].operator.address]) + expect(anchorTeamsBeforeSecond.confirm).toStrictEqual([RegTestFoundationKeys[0].operator.address]) + }) +}) diff --git a/packages/jellyfish-api-core/src/category/masternode.ts b/packages/jellyfish-api-core/src/category/masternode.ts index dbc06dd391..14a6de6662 100644 --- a/packages/jellyfish-api-core/src/category/masternode.ts +++ b/packages/jellyfish-api-core/src/category/masternode.ts @@ -184,6 +184,16 @@ export class Masternode { return await this.client.call('listgovs', [], 'bignumber') } + /** + * Returns the auth and confirm anchor masternode teams at current or specified height + * + * @param {number} blockHeight The height of block which contain tx + * @returns {Promise} + */ + async getAnchorTeams (blockHeight?: number): Promise { + return await this.client.call('getanchorteams', [blockHeight], 'number') + } + /** * Returns number of unique masternodes in the last specified number of blocks. * @@ -228,6 +238,11 @@ export interface MasternodeInfo { timelock?: number } +export interface AnchorTeamResult { + auth: string[] + confirm: string[] +} + export interface MasternodeResult { [id: string]: T } diff --git a/packages/jellyfish-testing/src/testing.ts b/packages/jellyfish-testing/src/testing.ts index cce7313c23..555c88ef3f 100644 --- a/packages/jellyfish-testing/src/testing.ts +++ b/packages/jellyfish-testing/src/testing.ts @@ -99,6 +99,12 @@ export class TestingGroup { return this.testings.length } + async add (container: MasterNodeRegTestContainer): Promise { + await this.group.add(container) + const testing = Testing.create(container) + this.testings.push(testing) + } + async start (): Promise { return await this.group.start() }