Skip to content

Commit

Permalink
feat(jellyfish-api-core): add getAnchorTeams to rpc_masternode (#923)
Browse files Browse the repository at this point in the history
* add getAnchorTeams to rpc_masternode

* edit spelling

* remove redundant test

* add in missing block generate
  • Loading branch information
chee-chyuan authored Dec 30, 2021
1 parent 74e2c02 commit d04d874
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/node/CATEGORIES/11-masternode.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnchorTeamResult>
}

interface AnchorTeamResult {
auth: string[]
confirm: string[]
}
```

## getActiveMasternodeCount

Returns number of unique masternodes in the last specified number of blocks.
Expand Down
Original file line number Diff line number Diff line change
@@ -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])
})
})
15 changes: 15 additions & 0 deletions packages/jellyfish-api-core/src/category/masternode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnchorTeamResult>}
*/
async getAnchorTeams (blockHeight?: number): Promise<AnchorTeamResult> {
return await this.client.call('getanchorteams', [blockHeight], 'number')
}

/**
* Returns number of unique masternodes in the last specified number of blocks.
*
Expand Down Expand Up @@ -228,6 +238,11 @@ export interface MasternodeInfo {
timelock?: number
}

export interface AnchorTeamResult {
auth: string[]
confirm: string[]
}

export interface MasternodeResult<T> {
[id: string]: T
}
6 changes: 6 additions & 0 deletions packages/jellyfish-testing/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export class TestingGroup {
return this.testings.length
}

async add (container: MasterNodeRegTestContainer): Promise<void> {
await this.group.add(container)
const testing = Testing.create(container)
this.testings.push(testing)
}

async start (): Promise<void> {
return await this.group.start()
}
Expand Down

0 comments on commit d04d874

Please sign in to comment.