Skip to content

Commit e4eed5a

Browse files
authored
Load commitment period from node (#70)
1 parent 3756189 commit e4eed5a

File tree

6 files changed

+143
-124
lines changed

6 files changed

+143
-124
lines changed

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"main": "cjs/index.js",
55
"types": "cjs/index.d.ts",
66
"scripts": {
7-
"testCommand": "jest --detect-open-handles",
7+
"testCommand": "jest",
88
"prepublishOnly": "npm run build",
99
"build": "tsc -p ./tsconfig-cjs.json",
1010
"test": "node-state -e -n -m typescript -o ./test/_state.ts -r"
@@ -45,8 +45,8 @@
4545
"@types/jest": "^30.0.0",
4646
"@typescript-eslint/eslint-plugin": "^8.56.0",
4747
"@typescript-eslint/parser": "^8.56.0",
48-
"@waves/node-state": "^0.2.0-snapshot.1",
49-
"@waves/waves-transactions": "4.4.0-snapshot.2",
48+
"@waves/node-state": "^0.2.0-snapshot.2",
49+
"@waves/waves-transactions": "4.4.0-snapshot.3",
5050
"eslint": "^9.35.0",
5151
"eslint-config-prettier": "^10.1.8",
5252
"eslint-plugin-import": "^2.32.0",
@@ -60,6 +60,7 @@
6060
},
6161
"overrides": {
6262
"test-exclude": "^8.0.0",
63-
"glob": "^13.0.6"
63+
"glob": "^13.0.6",
64+
"flatted": "^3.4.2"
6465
}
6566
}

src/api-node/finality/index.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import request from '../../tools/request';
2+
import {TLong} from '../../interface';
3+
import {fetchActivationStatus} from '../activation';
4+
import {fetchHeight, IBlockHeader} from '../blocks';
5+
6+
7+
/**
8+
* GET /blocks/headers/finalized
9+
* Last finalized block header
10+
* @param base
11+
* @param options
12+
*/
13+
export function fetchFinalized(base: string, options: RequestInit = Object.create(null)): Promise<IBlockHeader> {
14+
return request({
15+
base,
16+
url: `/blocks/headers/finalized`,
17+
options
18+
});
19+
}
20+
21+
/**
22+
* GET last finalized block height
23+
* @param base
24+
* @param options
25+
*/
26+
export function fetchFinalizedHeight(base: string, options: RequestInit = Object.create(null)): Promise<{ height: number }> {
27+
return request({
28+
base,
29+
url: `/blocks/height/finalized`,
30+
options
31+
})
32+
}
33+
34+
/**
35+
* GET finalized block height at
36+
* @param base
37+
* @param height
38+
* @param options
39+
*/
40+
export function fetchFinalizedHeightAt(base: string, height: number, options: RequestInit = Object.create(null)): Promise<{ height: number }> {
41+
return request({
42+
base,
43+
url: `/blocks/finalized/at/${height}`,
44+
options
45+
})
46+
}
47+
48+
/**
49+
* GET /generators/at/{height}
50+
* Committed generators list at height
51+
* @param base
52+
* @param height
53+
* @param options
54+
*/
55+
export function fetchCommittedGeneratorsAt(base: string, height: number, options: RequestInit = Object.create(null)): Promise<Array<ICommittedGenerator>> {
56+
return request({
57+
base,
58+
url: `/generators/at/${height}`,
59+
options
60+
});
61+
}
62+
63+
/**
64+
* Get committed generator index for provided address.
65+
* Returns index from 0, or -1 when address is missing in the list.
66+
* @param base
67+
* @param height
68+
* @param address
69+
* @param options
70+
*/
71+
export function fetchCommittedGeneratorIndex(base: string, height: number, address: string, options: RequestInit = Object.create(null)): Promise<number> {
72+
return fetchCommittedGeneratorsAt(base, height, options).then((list) => {
73+
const index = list.findIndex((item) => item.address === address);
74+
return index >= 0 ? index : -1;
75+
});
76+
}
77+
78+
export function fetchFinalityInfo(base: string, options: RequestInit = Object.create(null)): Promise<IFinalityInfo> {
79+
return request({
80+
base,
81+
url: '/blockchain/finality',
82+
options
83+
})
84+
}
85+
86+
export interface IGenerationPeriod {
87+
start: number;
88+
end: number;
89+
}
90+
91+
export interface IFinalityInfo {
92+
height: number;
93+
finalizedHeight: number;
94+
currentGenerationPeriod?: IGenerationPeriod;
95+
currentGenerators: ICommittedGenerator[];
96+
nextGenerationPeriod?: IGenerationPeriod;
97+
nextGenerators: INextCommittedGenerator[];
98+
}
99+
100+
export interface ICommittedGenerator {
101+
address: string;
102+
balance: TLong;
103+
transactionId: string;
104+
conflictHeight?: number;
105+
}
106+
107+
export interface INextCommittedGenerator {
108+
address: string;
109+
transactionId: string;
110+
}

src/api-node/finalization/index.ts

Lines changed: 0 additions & 116 deletions
This file was deleted.

src/create.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as utilsModule from './api-node/utils';
88
import * as debugModule from './api-node/debug';
99
import * as aliasModule from './api-node/alias';
1010
import * as activationModule from './api-node/activation';
11-
import * as finalizationModule from './api-node/finalization';
11+
import * as finalityModule from './api-node/finality';
1212
import * as nodeModule from './api-node/node';
1313
import * as assetsModule from './api-node/assets';
1414
import * as ethModule from './api-node/eth';
@@ -49,7 +49,7 @@ export function create(base: string) {
4949
const debug: TWrapRecord<typeof debugModule> = wrapRecord(base, debugModule);
5050
const alias: TWrapRecord<typeof aliasModule> = wrapRecord(base, aliasModule);
5151
const activation: TWrapRecord<typeof activationModule> = wrapRecord(base, activationModule);
52-
const finalization: TWrapRecord<typeof finalizationModule> = wrapRecord(base, finalizationModule);
52+
const finality: TWrapRecord<typeof finalityModule> = wrapRecord(base, finalityModule);
5353
const node: TWrapRecord<typeof nodeModule> = wrapRecord(base, nodeModule);
5454
const assets: TWrapRecord<typeof assetsModule> = wrapRecord(base, assetsModule);
5555
const eth: TWrapRecord<typeof ethModule> = wrapRecord(base, ethModule);
@@ -92,7 +92,7 @@ export function create(base: string) {
9292
debug,
9393
alias,
9494
activation,
95-
finalization,
95+
finality,
9696
node,
9797
assets,
9898
eth

src/nodeInteraction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface INodeRequestOptions {
3333

3434
const DEFAULT_NODE_REQUEST_OPTIONS: Required<INodeRequestOptions> = {
3535
timeout: 120000,
36-
apiBase: 'https://nodes.wavesplatform.com',
36+
apiBase: 'https://nodes.wavesnodes.com',
3737
};
3838

3939
export const currentHeight = async (apiBase: string): Promise<number> =>

test/api-node/finality.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {MASTER_ACCOUNT, NODE_URL} from '../_state';
2+
import {create} from '../../src';
3+
4+
import {commitToGeneration} from '@waves/waves-transactions';
5+
6+
const api = create(NODE_URL);
7+
8+
it('Finality info', async () => {
9+
const finalityInfo = await api.finality.fetchFinalityInfo()
10+
11+
const tx = await api.transactions.broadcast(commitToGeneration({
12+
chainId: 82,
13+
generationPeriodStart: finalityInfo.nextGenerationPeriod!.start
14+
}, MASTER_ACCOUNT.SEED))
15+
16+
await api.tools.transactions.wait(tx, {confirmations: 1})
17+
18+
const newFinality = await api.finality.fetchFinalityInfo()
19+
expect(newFinality.nextGenerators).toContainEqual({
20+
transactionId: tx.id,
21+
address: MASTER_ACCOUNT.ADDRESS
22+
})
23+
24+
}, 10000)

0 commit comments

Comments
 (0)