@ar.io/sdk 3.11.0-alpha.8 → 3.11.0-beta.1

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.
Files changed (89) hide show
  1. package/README.md +52 -0
  2. package/bundles/web.bundle.min.js +106 -106
  3. package/lib/cjs/cli/cli.js +137 -122
  4. package/lib/cjs/cli/commands/readCommands.js +6 -0
  5. package/lib/cjs/cli/utils.js +4 -1
  6. package/lib/cjs/common/ant.js +5 -5
  7. package/lib/cjs/common/contracts/ao-process.js +2 -1
  8. package/lib/cjs/common/io.js +37 -0
  9. package/lib/cjs/common/wayfinder/{gateways.js → gateways/network.js} +3 -41
  10. package/lib/cjs/common/wayfinder/gateways/simple-cache.js +35 -0
  11. package/lib/cjs/common/wayfinder/gateways/static.js +13 -0
  12. package/lib/cjs/common/wayfinder/index.js +11 -8
  13. package/lib/cjs/common/wayfinder/routing/strategies/ping.js +72 -0
  14. package/lib/cjs/common/wayfinder/routing/strategies/ping.test.js +156 -0
  15. package/lib/cjs/common/wayfinder/routing/strategies/random.js +13 -0
  16. package/lib/cjs/common/wayfinder/routing/strategies/random.test.js +68 -0
  17. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +42 -0
  18. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.test.js +78 -0
  19. package/lib/cjs/common/wayfinder/routing/strategies/static.js +29 -0
  20. package/lib/cjs/common/wayfinder/routing/strategies/static.test.js +40 -0
  21. package/lib/cjs/common/wayfinder/verification/{data-root-verifier.js → strategies/data-root-verifier.js} +4 -4
  22. package/lib/cjs/common/wayfinder/verification/{hash-verifier.js → strategies/hash-verifier.js} +4 -4
  23. package/lib/cjs/common/wayfinder/{gateways/trusted-gateways.js → verification/trusted.js} +1 -1
  24. package/lib/cjs/common/wayfinder/wayfinder.js +397 -257
  25. package/lib/cjs/common/wayfinder/wayfinder.test.js +227 -208
  26. package/lib/cjs/version.js +1 -1
  27. package/lib/esm/cli/cli.js +138 -123
  28. package/lib/esm/cli/commands/readCommands.js +5 -0
  29. package/lib/esm/cli/utils.js +4 -1
  30. package/lib/esm/common/ant.js +5 -5
  31. package/lib/esm/common/contracts/ao-process.js +2 -1
  32. package/lib/esm/common/io.js +37 -0
  33. package/lib/esm/common/wayfinder/{gateways.js → gateways/network.js} +2 -38
  34. package/lib/esm/common/wayfinder/gateways/simple-cache.js +31 -0
  35. package/lib/esm/common/wayfinder/gateways/static.js +9 -0
  36. package/lib/esm/common/wayfinder/index.js +11 -8
  37. package/lib/esm/common/wayfinder/routing/strategies/ping.js +68 -0
  38. package/lib/esm/common/wayfinder/routing/strategies/ping.test.js +151 -0
  39. package/lib/esm/common/wayfinder/routing/strategies/random.js +9 -0
  40. package/lib/esm/common/wayfinder/routing/strategies/random.test.js +63 -0
  41. package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +38 -0
  42. package/lib/esm/common/wayfinder/routing/strategies/round-robin.test.js +73 -0
  43. package/lib/esm/common/wayfinder/routing/strategies/static.js +25 -0
  44. package/lib/esm/common/wayfinder/routing/strategies/static.test.js +35 -0
  45. package/lib/esm/common/wayfinder/verification/{data-root-verifier.js → strategies/data-root-verifier.js} +2 -2
  46. package/lib/esm/common/wayfinder/verification/{hash-verifier.js → strategies/hash-verifier.js} +2 -2
  47. package/lib/esm/common/wayfinder/{gateways/trusted-gateways.js → verification/trusted.js} +1 -1
  48. package/lib/esm/common/wayfinder/wayfinder.js +395 -255
  49. package/lib/esm/common/wayfinder/wayfinder.test.js +227 -208
  50. package/lib/esm/version.js +1 -1
  51. package/lib/types/cli/commands/readCommands.d.ts +1 -0
  52. package/lib/types/common/io.d.ts +5 -2
  53. package/lib/types/common/wayfinder/{gateways.d.ts → gateways/network.d.ts} +3 -23
  54. package/lib/types/common/wayfinder/{routers/random.d.ts → gateways/simple-cache.d.ts} +12 -8
  55. package/lib/types/common/wayfinder/{routers → gateways}/static.d.ts +6 -7
  56. package/lib/types/common/wayfinder/index.d.ts +10 -7
  57. package/lib/types/common/wayfinder/{routers/simple-cache.d.ts → routing/strategies/ping.d.ts} +10 -11
  58. package/lib/types/common/wayfinder/routing/strategies/random.d.ts +21 -0
  59. package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +29 -0
  60. package/lib/types/common/wayfinder/routing/strategies/static.d.ts +29 -0
  61. package/lib/types/common/wayfinder/verification/{data-root-verifier.d.ts → strategies/data-root-verifier.d.ts} +2 -2
  62. package/lib/types/common/wayfinder/verification/{hash-verifier.d.ts → strategies/hash-verifier.d.ts} +2 -2
  63. package/lib/types/common/wayfinder/{gateways/trusted-gateways.d.ts → verification/trusted.d.ts} +1 -1
  64. package/lib/types/common/wayfinder/wayfinder.d.ts +111 -77
  65. package/lib/types/types/io.d.ts +16 -1
  66. package/lib/types/types/wayfinder.d.ts +8 -4
  67. package/lib/types/version.d.ts +1 -1
  68. package/package.json +1 -1
  69. package/lib/cjs/common/wayfinder/routers/priority.js +0 -29
  70. package/lib/cjs/common/wayfinder/routers/priority.test.js +0 -155
  71. package/lib/cjs/common/wayfinder/routers/random.js +0 -23
  72. package/lib/cjs/common/wayfinder/routers/random.test.js +0 -25
  73. package/lib/cjs/common/wayfinder/routers/simple-cache.js +0 -25
  74. package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +0 -41
  75. package/lib/cjs/common/wayfinder/routers/static.js +0 -14
  76. package/lib/cjs/common/wayfinder/routers/static.test.js +0 -14
  77. package/lib/esm/common/wayfinder/routers/priority.js +0 -25
  78. package/lib/esm/common/wayfinder/routers/priority.test.js +0 -153
  79. package/lib/esm/common/wayfinder/routers/random.js +0 -19
  80. package/lib/esm/common/wayfinder/routers/random.test.js +0 -23
  81. package/lib/esm/common/wayfinder/routers/simple-cache.js +0 -21
  82. package/lib/esm/common/wayfinder/routers/simple-cache.test.js +0 -39
  83. package/lib/esm/common/wayfinder/routers/static.js +0 -10
  84. package/lib/esm/common/wayfinder/routers/static.test.js +0 -12
  85. package/lib/types/common/wayfinder/routers/priority.d.ts +0 -29
  86. /package/lib/types/common/wayfinder/{routers/priority.test.d.ts → routing/strategies/ping.test.d.ts} +0 -0
  87. /package/lib/types/common/wayfinder/{routers → routing/strategies}/random.test.d.ts +0 -0
  88. /package/lib/types/common/wayfinder/{routers/simple-cache.test.d.ts → routing/strategies/round-robin.test.d.ts} +0 -0
  89. /package/lib/types/common/wayfinder/{routers → routing/strategies}/static.test.d.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ar.io/sdk",
3
- "version": "3.11.0-alpha.8",
3
+ "version": "3.11.0-beta.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ar-io/ar-io-sdk.git"
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PriorityGatewayRouter = void 0;
4
- const random_js_1 = require("../../../utils/random.js");
5
- const gateways_js_1 = require("../gateways.js");
6
- // TODO: one of N where N are in the last time window have met certain performance thresholds
7
- // TODO: look at bitorrent routing protocols for inspiration
8
- // TODO: router that looks at local stats/metrics and adjusts based on those
9
- class PriorityGatewayRouter {
10
- name = 'priority';
11
- gatewaysProvider;
12
- constructor({ ario, sortBy, sortOrder, limit, }) {
13
- this.gatewaysProvider = new gateways_js_1.NetworkGatewaysProvider({
14
- ario,
15
- sortBy,
16
- sortOrder,
17
- limit,
18
- });
19
- }
20
- async getTargetGateway() {
21
- const gateways = await this.gatewaysProvider.getGateways();
22
- const targetGateway = gateways[(0, random_js_1.randomInt)(0, gateways.length)];
23
- if (targetGateway === undefined) {
24
- throw new Error('No target gateway found');
25
- }
26
- return targetGateway;
27
- }
28
- }
29
- exports.PriorityGatewayRouter = PriorityGatewayRouter;
@@ -1,155 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const node_assert_1 = require("node:assert");
4
- const node_test_1 = require("node:test");
5
- const priority_js_1 = require("./priority.js");
6
- (0, node_test_1.describe)('PriorityRouter', () => {
7
- const mockGateways = [
8
- {
9
- settings: {
10
- fqdn: 'gateway1.net',
11
- port: 443,
12
- protocol: 'https',
13
- allowDelegatedStaking: false,
14
- delegateRewardShareRatio: 0.5,
15
- allowedDelegates: [],
16
- minDelegatedStake: 0,
17
- autoStake: false,
18
- properties: '',
19
- label: '',
20
- note: '',
21
- },
22
- gatewayAddress: 'addr1',
23
- observerAddress: 'addr1',
24
- totalDelegatedStake: 1000,
25
- startTimestamp: 0,
26
- endTimestamp: 0,
27
- operatorStake: 100,
28
- status: 'joined',
29
- weights: {
30
- normalizedCompositeWeight: 0.5,
31
- stakeWeight: 0.5,
32
- tenureWeight: 0.5,
33
- gatewayPerformanceRatio: 0.5,
34
- observerPerformanceRatio: 0.5,
35
- compositeWeight: 0.5,
36
- gatewayRewardRatioWeight: 0.5,
37
- observerRewardRatioWeight: 0.5,
38
- },
39
- stats: {
40
- passedConsecutiveEpochs: 10,
41
- failedConsecutiveEpochs: 5,
42
- totalEpochCount: 15,
43
- passedEpochCount: 10,
44
- failedEpochCount: 5,
45
- observedEpochCount: 15,
46
- prescribedEpochCount: 20,
47
- },
48
- },
49
- {
50
- settings: {
51
- fqdn: 'gateway1.net',
52
- port: 443,
53
- protocol: 'https',
54
- allowDelegatedStaking: false,
55
- delegateRewardShareRatio: 0.5,
56
- allowedDelegates: [],
57
- minDelegatedStake: 0,
58
- autoStake: false,
59
- properties: '',
60
- label: '',
61
- note: '',
62
- },
63
- gatewayAddress: 'addr1',
64
- observerAddress: 'addr1',
65
- totalDelegatedStake: 2000,
66
- startTimestamp: 0,
67
- endTimestamp: 0,
68
- operatorStake: 2000,
69
- status: 'joined',
70
- weights: {
71
- normalizedCompositeWeight: 0.5,
72
- stakeWeight: 0.5,
73
- tenureWeight: 0.5,
74
- gatewayPerformanceRatio: 0.5,
75
- observerPerformanceRatio: 0.5,
76
- compositeWeight: 0.5,
77
- gatewayRewardRatioWeight: 0.5,
78
- observerRewardRatioWeight: 0.5,
79
- },
80
- stats: {
81
- passedConsecutiveEpochs: 10,
82
- failedConsecutiveEpochs: 5,
83
- totalEpochCount: 15,
84
- passedEpochCount: 10,
85
- failedEpochCount: 5,
86
- observedEpochCount: 15,
87
- prescribedEpochCount: 20,
88
- },
89
- },
90
- {
91
- settings: {
92
- fqdn: 'gateway2.net',
93
- port: 443,
94
- protocol: 'https',
95
- allowDelegatedStaking: false,
96
- delegateRewardShareRatio: 0.5,
97
- allowedDelegates: [],
98
- minDelegatedStake: 0,
99
- autoStake: false,
100
- properties: '',
101
- label: '',
102
- note: '',
103
- },
104
- gatewayAddress: 'addr2',
105
- observerAddress: 'addr2',
106
- totalDelegatedStake: 0,
107
- startTimestamp: 0,
108
- endTimestamp: 0,
109
- operatorStake: 0,
110
- status: 'leaving',
111
- weights: {
112
- normalizedCompositeWeight: 0.5,
113
- stakeWeight: 0.5,
114
- tenureWeight: 0.5,
115
- gatewayPerformanceRatio: 0.5,
116
- observerPerformanceRatio: 0.5,
117
- compositeWeight: 0.5,
118
- gatewayRewardRatioWeight: 0.5,
119
- observerRewardRatioWeight: 0.5,
120
- },
121
- stats: {
122
- passedConsecutiveEpochs: 10,
123
- failedConsecutiveEpochs: 5,
124
- totalEpochCount: 15,
125
- passedEpochCount: 10,
126
- failedEpochCount: 5,
127
- observedEpochCount: 15,
128
- prescribedEpochCount: 20,
129
- },
130
- },
131
- ];
132
- const mockArIOClient = {
133
- getGateways: async () => ({ items: mockGateways }),
134
- };
135
- (0, node_test_1.it)('should prioritize gateway with highest success rate when using successRate weight', async () => {
136
- const router = new priority_js_1.PriorityGatewayRouter({
137
- ario: mockArIOClient,
138
- sortBy: 'operatorStake',
139
- sortOrder: 'desc',
140
- limit: 1,
141
- });
142
- const result = await router.getTargetGateway();
143
- node_assert_1.strict.deepStrictEqual(result, new URL('http://gateway1.net'));
144
- });
145
- (0, node_test_1.it)('should prioritize gateway with lowest latency when using latency weight', async () => {
146
- const router = new priority_js_1.PriorityGatewayRouter({
147
- ario: mockArIOClient,
148
- sortBy: 'operatorStake',
149
- sortOrder: 'desc',
150
- limit: 1,
151
- });
152
- const result = await router.getTargetGateway();
153
- node_assert_1.strict.deepStrictEqual(result, new URL('http://gateway2.net'));
154
- });
155
- });
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RandomGatewayRouter = void 0;
4
- const random_js_1 = require("../../../utils/random.js");
5
- class RandomGatewayRouter {
6
- name = 'random';
7
- gatewaysProvider;
8
- blocklist;
9
- constructor({ gatewaysProvider, blocklist = [], }) {
10
- this.gatewaysProvider = gatewaysProvider;
11
- this.blocklist = blocklist;
12
- }
13
- async getTargetGateway() {
14
- const allGateways = await this.gatewaysProvider.getGateways();
15
- const gateways = allGateways.filter((g) => !this.blocklist.includes(g.hostname));
16
- const targetGateway = gateways[(0, random_js_1.randomInt)(0, gateways.length)];
17
- if (targetGateway === undefined) {
18
- throw new Error('No target gateway found');
19
- }
20
- return targetGateway;
21
- }
22
- }
23
- exports.RandomGatewayRouter = RandomGatewayRouter;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const node_assert_1 = require("node:assert");
4
- const node_test_1 = require("node:test");
5
- const random_js_1 = require("./random.js");
6
- (0, node_test_1.describe)('RandomRouter', () => {
7
- const mockGateways = [
8
- new URL('https://gateway1.net'),
9
- new URL('https://gateway2.net'),
10
- new URL('https://gateway3.net'),
11
- ];
12
- const mockGatewaysProvider = {
13
- getGateways: async () => mockGateways,
14
- };
15
- (0, node_test_1.it)('should only return joined gateways', async () => {
16
- const router = new random_js_1.RandomGatewayRouter({
17
- gatewaysProvider: mockGatewaysProvider,
18
- });
19
- // random gateway should be one of the mock gateways
20
- for (let i = 0; i < 10; i++) {
21
- const result = await router.getTargetGateway();
22
- node_assert_1.strict.ok(mockGateways.includes(result));
23
- }
24
- });
25
- });
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SimpleCacheRouter = void 0;
4
- class SimpleCacheRouter {
5
- name;
6
- lastUpdatedTimestamp;
7
- ttlSeconds;
8
- targetGateway;
9
- router;
10
- constructor({ router, ttlSeconds = 5 * 60, // 5 minutes
11
- }) {
12
- this.router = router;
13
- this.ttlSeconds = ttlSeconds;
14
- }
15
- async getTargetGateway() {
16
- if (this.targetGateway === undefined ||
17
- this.lastUpdatedTimestamp + this.ttlSeconds * 1000 < Date.now()) {
18
- this.targetGateway = await this.router.getTargetGateway();
19
- this.lastUpdatedTimestamp = Date.now();
20
- }
21
- return this.targetGateway;
22
- }
23
- }
24
- exports.SimpleCacheRouter = SimpleCacheRouter;
25
- // TODO: a router that accepts ario and a router, and adds read through promise cache to ario.getGateways to avoid calling the ARIO contract on every request
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const node_assert_1 = require("node:assert");
4
- const node_test_1 = require("node:test");
5
- const simple_cache_js_1 = require("./simple-cache.js");
6
- (0, node_test_1.describe)('SimpleCacheRouter', () => {
7
- (0, node_test_1.it)('should cache gateway for TTL period even if underlying gateway changes', async () => {
8
- const gateway1 = new URL('https://gateway1.net');
9
- const gateway2 = new URL('https://gateway2.net');
10
- let currentGateway = gateway1;
11
- const mockRouter = {
12
- name: 'mock',
13
- getTargetGateway: async () => currentGateway,
14
- };
15
- const router = new simple_cache_js_1.SimpleCacheRouter({
16
- router: mockRouter,
17
- ttlSeconds: 300, // 5 minutes
18
- });
19
- // Get initial gateway which should be cached
20
- const initial = await router.getTargetGateway();
21
- node_assert_1.strict.deepStrictEqual(initial, gateway1);
22
- // Change the underlying gateway
23
- currentGateway = gateway2;
24
- // Should still return cached gateway1 for multiple calls
25
- for (let i = 0; i < 5; i++) {
26
- const result = await router.getTargetGateway();
27
- node_assert_1.strict.deepStrictEqual(result, gateway1);
28
- }
29
- // Advance time past TTL
30
- const originalNow = Date.now;
31
- try {
32
- Date.now = () => originalNow() + 300 * 1000 + 1;
33
- // Should now return the new gateway2
34
- const result = await router.getTargetGateway();
35
- node_assert_1.strict.deepStrictEqual(result, gateway2);
36
- }
37
- finally {
38
- Date.now = originalNow;
39
- }
40
- });
41
- });
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StaticGatewayRouter = void 0;
4
- class StaticGatewayRouter {
5
- name = 'static';
6
- gateway;
7
- constructor({ gateway }) {
8
- this.gateway = new URL(gateway);
9
- }
10
- async getTargetGateway() {
11
- return this.gateway;
12
- }
13
- }
14
- exports.StaticGatewayRouter = StaticGatewayRouter;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const node_assert_1 = require("node:assert");
4
- const node_test_1 = require("node:test");
5
- const static_js_1 = require("./static.js");
6
- (0, node_test_1.describe)('StaticGatewayRouter', () => {
7
- (0, node_test_1.it)('should return the provided gateway', async () => {
8
- const router = new static_js_1.StaticGatewayRouter({
9
- gateway: 'http://test-gateway.net',
10
- });
11
- const result = await router.getTargetGateway();
12
- node_assert_1.strict.deepStrictEqual(result, new URL('http://test-gateway.net'));
13
- });
14
- });
@@ -1,25 +0,0 @@
1
- import { randomInt } from '../../../utils/random.js';
2
- import { NetworkGatewaysProvider } from '../gateways.js';
3
- // TODO: one of N where N are in the last time window have met certain performance thresholds
4
- // TODO: look at bitorrent routing protocols for inspiration
5
- // TODO: router that looks at local stats/metrics and adjusts based on those
6
- export class PriorityGatewayRouter {
7
- name = 'priority';
8
- gatewaysProvider;
9
- constructor({ ario, sortBy, sortOrder, limit, }) {
10
- this.gatewaysProvider = new NetworkGatewaysProvider({
11
- ario,
12
- sortBy,
13
- sortOrder,
14
- limit,
15
- });
16
- }
17
- async getTargetGateway() {
18
- const gateways = await this.gatewaysProvider.getGateways();
19
- const targetGateway = gateways[randomInt(0, gateways.length)];
20
- if (targetGateway === undefined) {
21
- throw new Error('No target gateway found');
22
- }
23
- return targetGateway;
24
- }
25
- }
@@ -1,153 +0,0 @@
1
- import { strict as assert } from 'node:assert';
2
- import { describe, it } from 'node:test';
3
- import { PriorityGatewayRouter } from './priority.js';
4
- describe('PriorityRouter', () => {
5
- const mockGateways = [
6
- {
7
- settings: {
8
- fqdn: 'gateway1.net',
9
- port: 443,
10
- protocol: 'https',
11
- allowDelegatedStaking: false,
12
- delegateRewardShareRatio: 0.5,
13
- allowedDelegates: [],
14
- minDelegatedStake: 0,
15
- autoStake: false,
16
- properties: '',
17
- label: '',
18
- note: '',
19
- },
20
- gatewayAddress: 'addr1',
21
- observerAddress: 'addr1',
22
- totalDelegatedStake: 1000,
23
- startTimestamp: 0,
24
- endTimestamp: 0,
25
- operatorStake: 100,
26
- status: 'joined',
27
- weights: {
28
- normalizedCompositeWeight: 0.5,
29
- stakeWeight: 0.5,
30
- tenureWeight: 0.5,
31
- gatewayPerformanceRatio: 0.5,
32
- observerPerformanceRatio: 0.5,
33
- compositeWeight: 0.5,
34
- gatewayRewardRatioWeight: 0.5,
35
- observerRewardRatioWeight: 0.5,
36
- },
37
- stats: {
38
- passedConsecutiveEpochs: 10,
39
- failedConsecutiveEpochs: 5,
40
- totalEpochCount: 15,
41
- passedEpochCount: 10,
42
- failedEpochCount: 5,
43
- observedEpochCount: 15,
44
- prescribedEpochCount: 20,
45
- },
46
- },
47
- {
48
- settings: {
49
- fqdn: 'gateway1.net',
50
- port: 443,
51
- protocol: 'https',
52
- allowDelegatedStaking: false,
53
- delegateRewardShareRatio: 0.5,
54
- allowedDelegates: [],
55
- minDelegatedStake: 0,
56
- autoStake: false,
57
- properties: '',
58
- label: '',
59
- note: '',
60
- },
61
- gatewayAddress: 'addr1',
62
- observerAddress: 'addr1',
63
- totalDelegatedStake: 2000,
64
- startTimestamp: 0,
65
- endTimestamp: 0,
66
- operatorStake: 2000,
67
- status: 'joined',
68
- weights: {
69
- normalizedCompositeWeight: 0.5,
70
- stakeWeight: 0.5,
71
- tenureWeight: 0.5,
72
- gatewayPerformanceRatio: 0.5,
73
- observerPerformanceRatio: 0.5,
74
- compositeWeight: 0.5,
75
- gatewayRewardRatioWeight: 0.5,
76
- observerRewardRatioWeight: 0.5,
77
- },
78
- stats: {
79
- passedConsecutiveEpochs: 10,
80
- failedConsecutiveEpochs: 5,
81
- totalEpochCount: 15,
82
- passedEpochCount: 10,
83
- failedEpochCount: 5,
84
- observedEpochCount: 15,
85
- prescribedEpochCount: 20,
86
- },
87
- },
88
- {
89
- settings: {
90
- fqdn: 'gateway2.net',
91
- port: 443,
92
- protocol: 'https',
93
- allowDelegatedStaking: false,
94
- delegateRewardShareRatio: 0.5,
95
- allowedDelegates: [],
96
- minDelegatedStake: 0,
97
- autoStake: false,
98
- properties: '',
99
- label: '',
100
- note: '',
101
- },
102
- gatewayAddress: 'addr2',
103
- observerAddress: 'addr2',
104
- totalDelegatedStake: 0,
105
- startTimestamp: 0,
106
- endTimestamp: 0,
107
- operatorStake: 0,
108
- status: 'leaving',
109
- weights: {
110
- normalizedCompositeWeight: 0.5,
111
- stakeWeight: 0.5,
112
- tenureWeight: 0.5,
113
- gatewayPerformanceRatio: 0.5,
114
- observerPerformanceRatio: 0.5,
115
- compositeWeight: 0.5,
116
- gatewayRewardRatioWeight: 0.5,
117
- observerRewardRatioWeight: 0.5,
118
- },
119
- stats: {
120
- passedConsecutiveEpochs: 10,
121
- failedConsecutiveEpochs: 5,
122
- totalEpochCount: 15,
123
- passedEpochCount: 10,
124
- failedEpochCount: 5,
125
- observedEpochCount: 15,
126
- prescribedEpochCount: 20,
127
- },
128
- },
129
- ];
130
- const mockArIOClient = {
131
- getGateways: async () => ({ items: mockGateways }),
132
- };
133
- it('should prioritize gateway with highest success rate when using successRate weight', async () => {
134
- const router = new PriorityGatewayRouter({
135
- ario: mockArIOClient,
136
- sortBy: 'operatorStake',
137
- sortOrder: 'desc',
138
- limit: 1,
139
- });
140
- const result = await router.getTargetGateway();
141
- assert.deepStrictEqual(result, new URL('http://gateway1.net'));
142
- });
143
- it('should prioritize gateway with lowest latency when using latency weight', async () => {
144
- const router = new PriorityGatewayRouter({
145
- ario: mockArIOClient,
146
- sortBy: 'operatorStake',
147
- sortOrder: 'desc',
148
- limit: 1,
149
- });
150
- const result = await router.getTargetGateway();
151
- assert.deepStrictEqual(result, new URL('http://gateway2.net'));
152
- });
153
- });
@@ -1,19 +0,0 @@
1
- import { randomInt } from '../../../utils/random.js';
2
- export class RandomGatewayRouter {
3
- name = 'random';
4
- gatewaysProvider;
5
- blocklist;
6
- constructor({ gatewaysProvider, blocklist = [], }) {
7
- this.gatewaysProvider = gatewaysProvider;
8
- this.blocklist = blocklist;
9
- }
10
- async getTargetGateway() {
11
- const allGateways = await this.gatewaysProvider.getGateways();
12
- const gateways = allGateways.filter((g) => !this.blocklist.includes(g.hostname));
13
- const targetGateway = gateways[randomInt(0, gateways.length)];
14
- if (targetGateway === undefined) {
15
- throw new Error('No target gateway found');
16
- }
17
- return targetGateway;
18
- }
19
- }
@@ -1,23 +0,0 @@
1
- import { strict as assert } from 'node:assert';
2
- import { describe, it } from 'node:test';
3
- import { RandomGatewayRouter } from './random.js';
4
- describe('RandomRouter', () => {
5
- const mockGateways = [
6
- new URL('https://gateway1.net'),
7
- new URL('https://gateway2.net'),
8
- new URL('https://gateway3.net'),
9
- ];
10
- const mockGatewaysProvider = {
11
- getGateways: async () => mockGateways,
12
- };
13
- it('should only return joined gateways', async () => {
14
- const router = new RandomGatewayRouter({
15
- gatewaysProvider: mockGatewaysProvider,
16
- });
17
- // random gateway should be one of the mock gateways
18
- for (let i = 0; i < 10; i++) {
19
- const result = await router.getTargetGateway();
20
- assert.ok(mockGateways.includes(result));
21
- }
22
- });
23
- });
@@ -1,21 +0,0 @@
1
- export class SimpleCacheRouter {
2
- name;
3
- lastUpdatedTimestamp;
4
- ttlSeconds;
5
- targetGateway;
6
- router;
7
- constructor({ router, ttlSeconds = 5 * 60, // 5 minutes
8
- }) {
9
- this.router = router;
10
- this.ttlSeconds = ttlSeconds;
11
- }
12
- async getTargetGateway() {
13
- if (this.targetGateway === undefined ||
14
- this.lastUpdatedTimestamp + this.ttlSeconds * 1000 < Date.now()) {
15
- this.targetGateway = await this.router.getTargetGateway();
16
- this.lastUpdatedTimestamp = Date.now();
17
- }
18
- return this.targetGateway;
19
- }
20
- }
21
- // TODO: a router that accepts ario and a router, and adds read through promise cache to ario.getGateways to avoid calling the ARIO contract on every request
@@ -1,39 +0,0 @@
1
- import { strict as assert } from 'node:assert';
2
- import { describe, it } from 'node:test';
3
- import { SimpleCacheRouter } from './simple-cache.js';
4
- describe('SimpleCacheRouter', () => {
5
- it('should cache gateway for TTL period even if underlying gateway changes', async () => {
6
- const gateway1 = new URL('https://gateway1.net');
7
- const gateway2 = new URL('https://gateway2.net');
8
- let currentGateway = gateway1;
9
- const mockRouter = {
10
- name: 'mock',
11
- getTargetGateway: async () => currentGateway,
12
- };
13
- const router = new SimpleCacheRouter({
14
- router: mockRouter,
15
- ttlSeconds: 300, // 5 minutes
16
- });
17
- // Get initial gateway which should be cached
18
- const initial = await router.getTargetGateway();
19
- assert.deepStrictEqual(initial, gateway1);
20
- // Change the underlying gateway
21
- currentGateway = gateway2;
22
- // Should still return cached gateway1 for multiple calls
23
- for (let i = 0; i < 5; i++) {
24
- const result = await router.getTargetGateway();
25
- assert.deepStrictEqual(result, gateway1);
26
- }
27
- // Advance time past TTL
28
- const originalNow = Date.now;
29
- try {
30
- Date.now = () => originalNow() + 300 * 1000 + 1;
31
- // Should now return the new gateway2
32
- const result = await router.getTargetGateway();
33
- assert.deepStrictEqual(result, gateway2);
34
- }
35
- finally {
36
- Date.now = originalNow;
37
- }
38
- });
39
- });
@@ -1,10 +0,0 @@
1
- export class StaticGatewayRouter {
2
- name = 'static';
3
- gateway;
4
- constructor({ gateway }) {
5
- this.gateway = new URL(gateway);
6
- }
7
- async getTargetGateway() {
8
- return this.gateway;
9
- }
10
- }
@@ -1,12 +0,0 @@
1
- import { strict as assert } from 'node:assert';
2
- import { describe, it } from 'node:test';
3
- import { StaticGatewayRouter } from './static.js';
4
- describe('StaticGatewayRouter', () => {
5
- it('should return the provided gateway', async () => {
6
- const router = new StaticGatewayRouter({
7
- gateway: 'http://test-gateway.net',
8
- });
9
- const result = await router.getTargetGateway();
10
- assert.deepStrictEqual(result, new URL('http://test-gateway.net'));
11
- });
12
- });