dash-platform-sdk 1.5.1 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dash-platform-sdk",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "main": "index.js",
5
5
  "description": "Lightweight SDK for accessing Dash Platform blockchain",
6
6
  "repository": {
@@ -58,6 +58,14 @@ export declare class DashPlatformSDK {
58
58
  * @param network
59
59
  */
60
60
  _initialize(grpcPool: GRPCPool): void;
61
+ /**
62
+ * Waits until GRPC connection pool is initialized
63
+ * (seed nodes are checked and evonodes are discovered)
64
+ * SDK can be used without waiting, but requests may be sent to unavailable seed nodes until then
65
+ *
66
+ * @return {Promise<void>}
67
+ */
68
+ waitForInit(): Promise<void>;
61
69
  /**
62
70
  * Get currently used network
63
71
  *
@@ -81,6 +81,16 @@ export class DashPlatformSDK {
81
81
  this.names = new NamesController(grpcPool);
82
82
  this.keyPair = new KeyPairController();
83
83
  }
84
+ /**
85
+ * Waits until GRPC connection pool is initialized
86
+ * (seed nodes are checked and evonodes are discovered)
87
+ * SDK can be used without waiting, but requests may be sent to unavailable seed nodes until then
88
+ *
89
+ * @return {Promise<void>}
90
+ */
91
+ async waitForInit() {
92
+ await this.grpcPool.waitForInit?.();
93
+ }
84
94
  /**
85
95
  * Get currently used network
86
96
  *
@@ -14,6 +14,7 @@ export interface GRPCOptions {
14
14
  export interface GRPCPool {
15
15
  network: Network;
16
16
  getClient: (abortController?: AbortController) => PlatformClient;
17
+ waitForInit?: () => Promise<void>;
17
18
  }
18
19
  export interface MasternodeInfo {
19
20
  proTxHash: string;
@@ -35,9 +36,14 @@ export interface MasternodeInfo {
35
36
  }
36
37
  export declare const createClient: (url: string, abortController?: AbortController) => PlatformClient;
37
38
  export default class GRPCConnectionPool implements GRPCPool {
39
+ #private;
38
40
  dapiUrls: string[];
39
41
  network: Network;
40
42
  constructor(network: Network, grpcOptions?: GRPCOptions);
41
43
  _initialize(network: Network, poolLimit: number, dapiUrl?: string | string[]): Promise<void>;
44
+ /**
45
+ * Resolves when the pool initialization (seed nodes check and evonodes discovery) is finished
46
+ */
47
+ waitForInit(): Promise<void>;
42
48
  getClient(abortController?: AbortController): PlatformClient;
43
49
  }
@@ -8,11 +8,17 @@ const GRPC_DEFAULT_POOL_LIMIT = 5;
8
8
  const seedNodes = {
9
9
  testnet: [
10
10
  // seed-1.pshenmic.dev
11
- 'https://158.160.14.115:1443'
11
+ 'https://158.160.14.115:1443',
12
+ 'https://62.84.119.150:1443',
13
+ // validator
14
+ 'https://68.67.122.26:1443'
12
15
  ],
13
16
  mainnet: [
14
17
  // seed-1.pshenmic.dev
15
- 'https://158.160.14.115:443'
18
+ 'https://158.160.14.115:443',
19
+ 'https://62.84.119.150:443',
20
+ // validator
21
+ 'https://95.216.146.18:443',
16
22
  // mainnet dcg seeds
17
23
  // 'https://158.160.14.115',
18
24
  // 'https://3.0.60.103',
@@ -33,10 +39,11 @@ export const createClient = (url, abortController) => {
33
39
  export default class GRPCConnectionPool {
34
40
  dapiUrls;
35
41
  network;
42
+ #initialization;
36
43
  constructor(network, grpcOptions) {
37
44
  const grpcPoolLimit = grpcOptions?.poolLimit ?? GRPC_DEFAULT_POOL_LIMIT;
38
45
  this.network = network;
39
- this._initialize(network, grpcPoolLimit, grpcOptions?.dapiUrl).catch(console.error);
46
+ this.#initialization = this._initialize(network, grpcPoolLimit, grpcOptions?.dapiUrl).catch(console.error);
40
47
  }
41
48
  async _initialize(network, poolLimit, dapiUrl) {
42
49
  if (typeof dapiUrl === 'string') {
@@ -52,8 +59,18 @@ export default class GRPCConnectionPool {
52
59
  }
53
60
  // Add default seed nodes
54
61
  this.dapiUrls = [...seedNodes[network]];
55
- // retrieve evonodes from current validator sets
56
- const { validatorSets } = await getCurrentQuorumsInfo(this);
62
+ // retrieve evonodes from current validator sets through the first responding seed node,
63
+ // seed nodes that failed to respond are removed from the pool
64
+ const seedRequests = seedNodes[network].map(async (seed) => {
65
+ try {
66
+ return await getCurrentQuorumsInfo({ network, getClient: () => createClient(seed) });
67
+ }
68
+ catch (e) {
69
+ this.dapiUrls = this.dapiUrls.filter(url => url !== seed);
70
+ throw e;
71
+ }
72
+ });
73
+ const { validatorSets } = await Promise.any(seedRequests);
57
74
  // map it to array of dapiUrls
58
75
  const networkDAPIUrls = [...new Set(validatorSets
59
76
  .flatMap(validatorSet => validatorSet.members)
@@ -74,6 +91,14 @@ export default class GRPCConnectionPool {
74
91
  catch (e) {
75
92
  }
76
93
  }
94
+ // wait until every seed node responded or failed, so dead ones are removed from the pool
95
+ await Promise.allSettled(seedRequests);
96
+ }
97
+ /**
98
+ * Resolves when the pool initialization (seed nodes check and evonodes discovery) is finished
99
+ */
100
+ async waitForInit() {
101
+ await this.#initialization;
77
102
  }
78
103
  getClient(abortController) {
79
104
  const dapiUrl = getRandomArrayItem(this.dapiUrls);
@@ -5,8 +5,9 @@ import { LATEST_PLATFORM_VERSION } from '../../src/constants.js';
5
5
  let sdk;
6
6
  let contract;
7
7
  describe('Contested Resources', () => {
8
- beforeAll(() => {
8
+ beforeAll(async () => {
9
9
  sdk = new DashPlatformSDK({ network: 'testnet' });
10
+ await sdk.waitForInit();
10
11
  contract = new DataContractWASM('11111111111111111111111111111111', BigInt(1), {
11
12
  domain: {
12
13
  type: 'object',
@@ -6,8 +6,9 @@ let identityNonce;
6
6
  let config;
7
7
  let schema;
8
8
  describe('DataContract', () => {
9
- beforeAll(() => {
9
+ beforeAll(async () => {
10
10
  sdk = new DashPlatformSDK({ network: 'testnet' });
11
+ await sdk.waitForInit();
11
12
  ownerIdentifier = 'GARSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec';
12
13
  identityNonce = BigInt(11);
13
14
  config = {
@@ -7,8 +7,9 @@ let documentType;
7
7
  let revision;
8
8
  let data;
9
9
  describe('Document', () => {
10
- beforeAll(() => {
10
+ beforeAll(async () => {
11
11
  sdk = new DashPlatformSDK({ network: 'testnet' });
12
+ await sdk.waitForInit();
12
13
  dataContract = '6QMfQTdKpC3Y9uWBcTwXeY3KdzRLDqASUsDnQ4MEc9XC';
13
14
  identity = 'QMfCRPcjXoTnZa9sA9JR2KWgGxZXMRJ4akgS3Uia1Qv';
14
15
  revision = BigInt(1);
@@ -3,8 +3,9 @@ import { DashPlatformSDK } from '../../types.js';
3
3
  import { base58 } from '@scure/base';
4
4
  let sdk;
5
5
  describe('Identity', () => {
6
- beforeAll(() => {
6
+ beforeAll(async () => {
7
7
  sdk = new DashPlatformSDK({ network: 'testnet' });
8
+ await sdk.waitForInit();
8
9
  });
9
10
  test('should be able to get identity by identifier', async () => {
10
11
  const identifier = 'QMfCRPcjXoTnZa9sA9JR2KWgGxZXMRJ4akgS3Uia1Qv';
@@ -7,8 +7,9 @@ jest.mock('../../src/stateTransitions/broadcast');
7
7
  jest.mock('../../src/utils/sleep');
8
8
  let sdk;
9
9
  describe('DPNS names', () => {
10
- beforeAll(() => {
10
+ beforeAll(async () => {
11
11
  sdk = new DashPlatformSDK({ network: 'testnet' });
12
+ await sdk.waitForInit();
12
13
  // @ts-expect-error
13
14
  sleep.mockReturnValue(Promise.resolve());
14
15
  sdk.stateTransitions.broadcast = jest.fn();
@@ -1,8 +1,9 @@
1
1
  import { DashPlatformSDK } from '../../src/DashPlatformSDK.js';
2
2
  let sdk;
3
3
  describe('Node', () => {
4
- beforeAll(() => {
4
+ beforeAll(async () => {
5
5
  sdk = new DashPlatformSDK({ network: 'testnet' });
6
+ await sdk.waitForInit();
6
7
  });
7
8
  test('should be able to call getStatus', async () => {
8
9
  const status = await sdk.node.status();
@@ -3,8 +3,9 @@ import { base58 } from '@scure/base';
3
3
  import { DashPlatformSDK } from '../../src/DashPlatformSDK.js';
4
4
  let sdk;
5
5
  describe('PlatformAddress', () => {
6
- beforeAll(() => {
6
+ beforeAll(async () => {
7
7
  sdk = new DashPlatformSDK({ network: 'testnet' });
8
+ await sdk.waitForInit();
8
9
  });
9
10
  test('getAddressInfo', async () => {
10
11
  const info = await sdk.platformAddresses.getAddressInfo('tdash1kzg5azscav69z7m6dfzr9ner0a5vt7pn9ca4sz8d');
@@ -37,6 +37,7 @@ function makeSpendableNote(seed, value) {
37
37
  describe('Shielded', () => {
38
38
  beforeAll(async () => {
39
39
  sdk = new DashPlatformSDK({ network: 'testnet' });
40
+ await sdk.waitForInit();
40
41
  await sdk.shielded.init();
41
42
  }, 60000);
42
43
  test('getShieldedNotesCount', async () => {
@@ -2,8 +2,9 @@ import { StateTransitionWASM, TokenBaseTransitionWASM, TokenEmergencyActionWASM
2
2
  import { DashPlatformSDK } from '../../src/DashPlatformSDK.js';
3
3
  let sdk;
4
4
  describe('Tokens', () => {
5
- beforeAll(() => {
5
+ beforeAll(async () => {
6
6
  sdk = new DashPlatformSDK({ network: 'testnet' });
7
+ await sdk.waitForInit();
7
8
  });
8
9
  test('should be able to get token total supply', async () => {
9
10
  const tokenTotalSupply = await sdk.tokens.getTokenTotalSupply('9YxdbQUjJmQsmVPen95HjAU3Esj7tVkWSY2EQWT84ZQP');