@ar.io/sdk 3.10.0-alpha.4 → 3.10.0-alpha.6
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/bundles/web.bundle.min.js +63 -63
- package/lib/cjs/common/index.js +2 -0
- package/lib/cjs/common/wayfinder/gateways.js +75 -0
- package/lib/cjs/common/wayfinder/index.js +36 -0
- package/lib/cjs/common/wayfinder/routers/fixed.js +14 -0
- package/lib/cjs/common/wayfinder/routers/fixed.test.js +14 -0
- package/lib/cjs/common/wayfinder/routers/priority.js +38 -0
- package/lib/cjs/common/wayfinder/routers/priority.test.js +155 -0
- package/lib/cjs/common/wayfinder/routers/random.js +23 -0
- package/lib/cjs/common/wayfinder/routers/random.test.js +104 -0
- package/lib/cjs/common/wayfinder/routers/simple-cache.js +25 -0
- package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +41 -0
- package/lib/cjs/common/wayfinder/wayfinder.js +194 -0
- package/lib/cjs/common/wayfinder/wayfinder.test.js +254 -0
- package/lib/cjs/types/ant.js +12 -1
- package/lib/cjs/types/index.js +1 -0
- package/lib/cjs/types/wayfinder.js +2 -0
- package/lib/cjs/utils/ao.js +5 -0
- package/lib/cjs/version.js +1 -1
- package/lib/esm/common/index.js +2 -0
- package/lib/esm/common/wayfinder/gateways.js +69 -0
- package/lib/esm/common/wayfinder/index.js +20 -0
- package/lib/esm/common/wayfinder/routers/fixed.js +10 -0
- package/lib/esm/common/wayfinder/routers/fixed.test.js +12 -0
- package/lib/esm/common/wayfinder/routers/priority.js +34 -0
- package/lib/esm/common/wayfinder/routers/priority.test.js +153 -0
- package/lib/esm/common/wayfinder/routers/random.js +19 -0
- package/lib/esm/common/wayfinder/routers/random.test.js +102 -0
- package/lib/esm/common/wayfinder/routers/simple-cache.js +21 -0
- package/lib/esm/common/wayfinder/routers/simple-cache.test.js +39 -0
- package/lib/esm/common/wayfinder/wayfinder.js +187 -0
- package/lib/esm/common/wayfinder/wayfinder.test.js +249 -0
- package/lib/esm/types/ant.js +11 -0
- package/lib/esm/types/index.js +1 -0
- package/lib/esm/types/wayfinder.js +1 -0
- package/lib/esm/utils/ao.js +5 -0
- package/lib/esm/version.js +1 -1
- package/lib/types/common/index.d.ts +1 -0
- package/lib/types/common/io.d.ts +0 -1
- package/lib/types/common/wayfinder/gateways.d.ts +44 -0
- package/lib/types/common/wayfinder/index.d.ts +19 -0
- package/lib/types/common/wayfinder/routers/fixed.d.ts +25 -0
- package/lib/types/common/wayfinder/routers/fixed.test.d.ts +1 -0
- package/lib/types/common/wayfinder/routers/priority.d.ts +34 -0
- package/lib/types/common/wayfinder/routers/priority.test.d.ts +1 -0
- package/lib/types/common/wayfinder/routers/random.d.ts +28 -0
- package/lib/types/common/wayfinder/routers/random.test.d.ts +1 -0
- package/lib/types/common/wayfinder/routers/simple-cache.d.ts +29 -0
- package/lib/types/common/wayfinder/routers/simple-cache.test.d.ts +1 -0
- package/lib/types/common/wayfinder/wayfinder.d.ts +106 -0
- package/lib/types/common/wayfinder/wayfinder.test.d.ts +1 -0
- package/lib/types/types/ant.d.ts +53 -1
- package/lib/types/types/index.d.ts +1 -0
- package/lib/types/types/io.d.ts +1 -0
- package/lib/types/types/token.d.ts +1 -0
- package/lib/types/types/wayfinder.d.ts +20 -0
- package/lib/types/utils/ao.d.ts +1 -12
- package/lib/types/utils/base64.d.ts +1 -0
- package/lib/types/version.d.ts +1 -1
- package/package.json +4 -3
package/lib/cjs/common/index.js
CHANGED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SimpleCacheGatewaysProvider = exports.StaticGatewaysProvider = exports.ARIOGatewaysProvider = void 0;
|
|
4
|
+
class ARIOGatewaysProvider {
|
|
5
|
+
ario;
|
|
6
|
+
constructor({ ario }) {
|
|
7
|
+
this.ario = ario;
|
|
8
|
+
}
|
|
9
|
+
async getGateways() {
|
|
10
|
+
let cursor;
|
|
11
|
+
let attempts = 0;
|
|
12
|
+
const gateways = [];
|
|
13
|
+
do {
|
|
14
|
+
try {
|
|
15
|
+
const { items: newGateways, nextCursor } = await this.ario.getGateways({
|
|
16
|
+
limit: 1000,
|
|
17
|
+
cursor,
|
|
18
|
+
});
|
|
19
|
+
gateways.push(...newGateways);
|
|
20
|
+
cursor = nextCursor;
|
|
21
|
+
attempts = 0; // reset attempts if we get a new cursor
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
console.error('Error fetching gateways', {
|
|
25
|
+
cursor,
|
|
26
|
+
attempts,
|
|
27
|
+
error,
|
|
28
|
+
});
|
|
29
|
+
attempts++;
|
|
30
|
+
}
|
|
31
|
+
} while (cursor !== undefined && attempts < 3);
|
|
32
|
+
// filter out any gateways that are not joined
|
|
33
|
+
return gateways.filter((g) => g.status === 'joined');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.ARIOGatewaysProvider = ARIOGatewaysProvider;
|
|
37
|
+
class StaticGatewaysProvider {
|
|
38
|
+
gateways;
|
|
39
|
+
constructor({ gateways }) {
|
|
40
|
+
this.gateways = gateways;
|
|
41
|
+
}
|
|
42
|
+
async getGateways() {
|
|
43
|
+
return this.gateways;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.StaticGatewaysProvider = StaticGatewaysProvider;
|
|
47
|
+
class SimpleCacheGatewaysProvider {
|
|
48
|
+
gatewaysProvider;
|
|
49
|
+
ttlSeconds;
|
|
50
|
+
lastUpdated;
|
|
51
|
+
gatewaysCache;
|
|
52
|
+
constructor({ gatewaysProvider, ttlSeconds = 5 * 60, // 5 minutes
|
|
53
|
+
}) {
|
|
54
|
+
this.gatewaysCache = [];
|
|
55
|
+
this.gatewaysProvider = gatewaysProvider;
|
|
56
|
+
this.ttlSeconds = ttlSeconds;
|
|
57
|
+
}
|
|
58
|
+
async getGateways() {
|
|
59
|
+
const now = Date.now();
|
|
60
|
+
if (this.gatewaysCache.length === 0 ||
|
|
61
|
+
now - this.lastUpdated > this.ttlSeconds * 1000) {
|
|
62
|
+
try {
|
|
63
|
+
// preserve the cache if the fetch fails
|
|
64
|
+
const allGateways = await this.gatewaysProvider.getGateways();
|
|
65
|
+
this.gatewaysCache = allGateways.filter((g) => g.status === 'joined');
|
|
66
|
+
this.lastUpdated = now;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.error('Error fetching gateways', error);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return this.gatewaysCache;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.SimpleCacheGatewaysProvider = SimpleCacheGatewaysProvider;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
/**
|
|
18
|
+
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
19
|
+
*
|
|
20
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
21
|
+
* you may not use this file except in compliance with the License.
|
|
22
|
+
* You may obtain a copy of the License at
|
|
23
|
+
*
|
|
24
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
25
|
+
*
|
|
26
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
27
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
28
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
29
|
+
* See the License for the specific language governing permissions and
|
|
30
|
+
* limitations under the License.
|
|
31
|
+
*/
|
|
32
|
+
__exportStar(require("./wayfinder.js"), exports);
|
|
33
|
+
// routers
|
|
34
|
+
__exportStar(require("./routers/random.js"), exports);
|
|
35
|
+
__exportStar(require("./routers/priority.js"), exports);
|
|
36
|
+
__exportStar(require("./routers/fixed.js"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FixedGatewayRouter = void 0;
|
|
4
|
+
class FixedGatewayRouter {
|
|
5
|
+
name = 'fixed';
|
|
6
|
+
gateway;
|
|
7
|
+
constructor({ gateway }) {
|
|
8
|
+
this.gateway = gateway;
|
|
9
|
+
}
|
|
10
|
+
async getTargetGateway() {
|
|
11
|
+
return this.gateway;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.FixedGatewayRouter = FixedGatewayRouter;
|
|
@@ -0,0 +1,14 @@
|
|
|
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 fixed_js_1 = require("./fixed.js");
|
|
6
|
+
(0, node_test_1.describe)('FixedRouter', () => {
|
|
7
|
+
(0, node_test_1.it)('should return the provided gateway', async () => {
|
|
8
|
+
const router = new fixed_js_1.FixedGatewayRouter({
|
|
9
|
+
gateway: new URL('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
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PriorityGatewayRouter = void 0;
|
|
4
|
+
const wayfinder_js_1 = require("../wayfinder.js");
|
|
5
|
+
// TODO: one of N where N are in the last time window have met certain performance thresholds
|
|
6
|
+
// TODO: look at bitorrent routing protocols for inspiration
|
|
7
|
+
// TODO: router that looks at local stats/metrics and adjusts based on those
|
|
8
|
+
class PriorityGatewayRouter {
|
|
9
|
+
name = 'priority';
|
|
10
|
+
gatewaysProvider;
|
|
11
|
+
limit;
|
|
12
|
+
sortBy;
|
|
13
|
+
sortOrder;
|
|
14
|
+
blocklist;
|
|
15
|
+
constructor({ gatewaysProvider, limit = 1, sortBy = 'operatorStake', sortOrder = 'desc', blocklist = [], }) {
|
|
16
|
+
this.gatewaysProvider = gatewaysProvider;
|
|
17
|
+
this.limit = limit;
|
|
18
|
+
this.sortBy = sortBy;
|
|
19
|
+
this.sortOrder = sortOrder;
|
|
20
|
+
this.blocklist = blocklist;
|
|
21
|
+
}
|
|
22
|
+
async getTargetGateway() {
|
|
23
|
+
const allGateways = await this.gatewaysProvider.getGateways();
|
|
24
|
+
const gateways = allGateways.filter((gateway) => gateway.status === 'joined' &&
|
|
25
|
+
!this.blocklist.includes(gateway.settings.fqdn));
|
|
26
|
+
const sortedGateways = gateways
|
|
27
|
+
.sort(this.sortOrder === 'asc'
|
|
28
|
+
? (a, b) => a[this.sortBy] - b[this.sortBy]
|
|
29
|
+
: (a, b) => b[this.sortBy] - a[this.sortBy])
|
|
30
|
+
.slice(0, this.limit);
|
|
31
|
+
const targetGateway = sortedGateways[(0, wayfinder_js_1.randomInt)(0, sortedGateways.length)];
|
|
32
|
+
if (targetGateway === undefined) {
|
|
33
|
+
throw new Error('No target gateway found');
|
|
34
|
+
}
|
|
35
|
+
return new URL(`${targetGateway.settings.protocol}://${targetGateway.settings.fqdn}:${targetGateway.settings.port}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.PriorityGatewayRouter = PriorityGatewayRouter;
|
|
@@ -0,0 +1,155 @@
|
|
|
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 mockGatewaysProvider = {
|
|
133
|
+
getGateways: async () => 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
|
+
gatewaysProvider: mockGatewaysProvider,
|
|
138
|
+
sortBy: 'totalDelegatedStake',
|
|
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
|
+
gatewaysProvider: mockGatewaysProvider,
|
|
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
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RandomGatewayRouter = void 0;
|
|
4
|
+
const wayfinder_js_1 = require("../wayfinder.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) => g.status === 'joined' && !this.blocklist.includes(g.settings.fqdn));
|
|
16
|
+
const targetGateway = gateways[(0, wayfinder_js_1.randomInt)(0, gateways.length)];
|
|
17
|
+
if (targetGateway === undefined) {
|
|
18
|
+
throw new Error('No target gateway found');
|
|
19
|
+
}
|
|
20
|
+
return new URL(`${targetGateway.settings.protocol}://${targetGateway.settings.fqdn}:${targetGateway.settings.port}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.RandomGatewayRouter = RandomGatewayRouter;
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
{
|
|
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: 'gateway2.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: 'addr2',
|
|
64
|
+
observerAddress: 'addr2',
|
|
65
|
+
totalDelegatedStake: 0,
|
|
66
|
+
startTimestamp: 0,
|
|
67
|
+
endTimestamp: 0,
|
|
68
|
+
operatorStake: 0,
|
|
69
|
+
status: 'leaving',
|
|
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
|
+
const mockGatewaysProvider = {
|
|
92
|
+
getGateways: async () => mockGateways,
|
|
93
|
+
};
|
|
94
|
+
(0, node_test_1.it)('should only return joined gateways', async () => {
|
|
95
|
+
const router = new random_js_1.RandomGatewayRouter({
|
|
96
|
+
gatewaysProvider: mockGatewaysProvider,
|
|
97
|
+
});
|
|
98
|
+
// Run multiple times to ensure we only get joined gateways
|
|
99
|
+
for (let i = 0; i < 10; i++) {
|
|
100
|
+
const result = await router.getTargetGateway();
|
|
101
|
+
node_assert_1.strict.deepStrictEqual(result, new URL('https://gateway1.net'));
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
});
|