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): rpc masternode listanchor #921

Merged
merged 7 commits into from
Dec 30, 2021
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
23 changes: 23 additions & 0 deletions docs/node/CATEGORIES/11-masternode.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,26 @@ interface masternode {
getActiveMasternodeCount (blockCount: number = 20160): Promise<number>
}
```

## listAnchors
Return array of anchors if any
chee-chyuan marked this conversation as resolved.
Show resolved Hide resolved

```ts title="client.masternode.getActiveMasternodeCount"
chee-chyuan marked this conversation as resolved.
Show resolved Hide resolved
interface masternode {
listAnchors (): Promise<MasternodeResult<MasternodeAnchor>>
}

interface MasternodeAnchor {
anchorHeight: number
anchorHash: string
rewardAddress: string
dfiRewardHash: string
btcAnchorHeight: number
btcAnchorHash: string
confirmSignHash: string
}

interface MasternodeResult<T> {
[id: string]: T
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { TestingGroup } from '@defichain/jellyfish-testing'

describe('Masternode', () => {
const tGroup = TestingGroup.create(3)

async function setMockTime (offsetHour: number): Promise<void> {
await tGroup.exec(async testing => {
await testing.misc.offsetTimeHourly(offsetHour)
})
}

async function setup (): Promise<void> {
const anchorAuths = await tGroup.get(0).rpc.spv.listAnchorAuths()
expect(anchorAuths.length).toEqual(0)

// time travel back 12 hours ago
const initOffsetHour = -12
await setMockTime(initOffsetHour) // 15 as anchor frequency

for (let i = 0; i < 15; i += 1) {
const { container } = tGroup.get(i % tGroup.length())
await container.generate(1)
await tGroup.waitForSync()
}
await tGroup.get(0).container.waitForAnchorTeams(tGroup.length())
const anchorTeams = await tGroup.get(0).container.call('getanchorteams')
expect(anchorTeams.auth.length).toEqual(tGroup.length())
expect(anchorTeams.confirm.length).toEqual(tGroup.length())

const anchorAuthBefore = await tGroup.get(0).rpc.spv.listAnchorAuths()
expect(anchorAuthBefore.length).toEqual(0)

await tGroup.anchor.generateAnchorAuths(2, initOffsetHour)
await tGroup.get(0).container.waitForAnchorAuths(tGroup.length())

for (let i = 0; i < tGroup.length(); i += 1) {
const auths = await tGroup.get(i).rpc.spv.listAnchorAuths()
expect(auths.length).toStrictEqual(2)
expect(auths[0].signers).toStrictEqual(tGroup.length())
}
}

beforeAll(async () => {
await tGroup.start()
await setup()
})

afterAll(async () => {
await tGroup.stop()
})

it('should listAnchors to be empty initially', async () => {
const anchorRewards = await tGroup.get(0).rpc.spv.listAnchorRewards()
expect(anchorRewards.length).toEqual(0)

const anchors = await tGroup.get(0).rpc.masternode.listAnchors()
expect(anchors.length).toEqual(0)
})

it('should listAnchors when anchor has been rewarded', async () => {
const rewardAddress = await tGroup.get(0).rpc.spv.getNewAddress()
const anchor = await tGroup.get(0).rpc.spv.createAnchor([{
txid: '11a276bb25585f6973a4dd68373cffff41dbcaddf12bbc1c2b489d1dc84564ee',
vout: 2,
amount: 15800,
privkey: 'b0528d87cfdb09f72c9d10b7b3cc00727062d93537a3e8abcf1fde821d08b59d'
}], rewardAddress)

// reward is still pending
const pendingAnchorList = await tGroup.get(0).rpc.spv.listAnchorsPending()
expect(pendingAnchorList.length).toEqual(1)

expect((await tGroup.get(0).rpc.masternode.listAnchors()).length).toEqual(0)

// get other anchor to send the tx as well
await tGroup.get(1).container.call('spv_sendrawtx', [anchor.txHex])

await tGroup.get(0).generate(1)
await tGroup.waitForSync()

// reward is no longer pending
const pendingAnchorListAfter = await tGroup.get(0).rpc.spv.listAnchorsPending()
expect(pendingAnchorListAfter.length).toEqual(0)

await tGroup.get(0).container.call('spv_setlastheight', [6])
await tGroup.get(1).container.call('spv_setlastheight', [6])
const anchorRewardsBefore = await tGroup.get(0).rpc.spv.listAnchorRewards()
expect(anchorRewardsBefore.length).toEqual(0)

// anchor reward not confirmed
const masternodeAnchor = await tGroup.get(0).rpc.masternode.listAnchors()
expect(masternodeAnchor.length).toEqual(0)

await tGroup.get(0).container.waitForAnchorRewardConfirms()
await tGroup.get(0).generate(1)
const anchorRewards = await tGroup.get(0).rpc.spv.listAnchorRewards()
expect(anchorRewards.length).toEqual(1)

const masternodeAnchors = await tGroup.get(0).rpc.masternode.listAnchors()
expect(masternodeAnchors.length).toEqual(1)
expect(masternodeAnchors[0].btcAnchorHash).toStrictEqual(anchorRewards[0].AnchorTxHash)
expect(masternodeAnchors[0].dfiRewardHash).toStrictEqual(anchorRewards[0].RewardTxHash)

// list anchor from other masternode
const masternode1Anchors = await tGroup.get(1).rpc.masternode.listAnchors()
const masternode2Anchors = await tGroup.get(2).rpc.masternode.listAnchors()
expect(masternode1Anchors).toStrictEqual(masternodeAnchors)
expect(masternode2Anchors).toStrictEqual(masternodeAnchors)
})
})
18 changes: 18 additions & 0 deletions packages/jellyfish-api-core/src/category/masternode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ export class Masternode {
async getActiveMasternodeCount (blockCount: number = 20160): Promise<number> {
return await this.client.call('getactivemasternodecount', [blockCount], 'number')
}

/**
* Returns array of anchors if any
* @return {} Array of anchors
chee-chyuan marked this conversation as resolved.
Show resolved Hide resolved
*/
async listAnchors (): Promise<MasternodeResult<MasternodeAnchor>> {
return await this.client.call('listanchors', [], 'number')
}
}

export interface UTXO {
Expand Down Expand Up @@ -228,6 +236,16 @@ export interface MasternodeInfo {
timelock?: number
}

export interface MasternodeAnchor {
anchorHeight: number
anchorHash: string
rewardAddress: string
dfiRewardHash: string
btcAnchorHeight: number
btcAnchorHash: string
confirmSignHash: string
}

export interface MasternodeResult<T> {
[id: string]: T
}