This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
observation.test.ts
404 lines (391 loc) · 15.4 KB
/
observation.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import { Contract, JWKInterface } from 'warp-contracts';
import { BlockHeight, Gateways, IOState, WeightedObserver } from '../src/types';
import {
EPOCH_BLOCK_LENGTH,
EPOCH_DISTRIBUTION_DELAY,
EXAMPLE_OBSERVER_REPORT_TX_IDS,
INVALID_OBSERVATION_CALLER_MESSAGE,
OBSERVATION_FAILURE_THRESHOLD,
WALLETS_TO_CREATE,
} from './utils/constants';
import {
createLocalWallet,
getCurrentBlock,
getLocalArNSContractKey,
getLocalWallet,
mineBlocks,
} from './utils/helper';
import { arweave, warp } from './utils/services';
describe('Observation', () => {
const gatewayWalletAddresses: string[] = [];
let contract: Contract<IOState>;
let srcContractId: string;
let prevState: IOState;
const wallets: {
addr: string;
jwk: JWKInterface;
}[] = [];
let prescribedObserverWallets: {
addr: string;
jwk: JWKInterface;
}[] = [];
let currentEpochStartHeight: BlockHeight;
let prescribedObservers: WeightedObserver[] = [];
let failedGateways: string[];
beforeAll(async () => {
for (let i = 0; i < WALLETS_TO_CREATE; i++) {
const gatewayWallet = getLocalWallet(i);
const gatewayWalletAddress = await arweave.wallets.getAddress(
gatewayWallet,
);
wallets.push({
addr: gatewayWalletAddress,
jwk: gatewayWallet,
});
gatewayWalletAddresses.push(gatewayWalletAddress);
}
srcContractId = getLocalArNSContractKey('id');
contract = warp.contract<IOState>(srcContractId);
failedGateways = [
wallets[0].addr,
wallets[1].addr,
wallets[8].addr, // should not be include as its start block is after current
wallets[9].addr, // should not be included as its leaving
];
});
beforeEach(async () => {
prevState = (await contract.readState()).cachedValue.state;
const { result }: { result: WeightedObserver[] } = await contract.viewState(
{
function: 'prescribedObservers',
},
);
prescribedObservers = result;
prescribedObserverWallets = wallets.filter((wallet) =>
prescribedObservers.find(
(observer: { gatewayAddress: string }) =>
observer.gatewayAddress === wallet.addr,
),
);
currentEpochStartHeight = await contract
.viewState({
function: 'epoch',
})
.then(
(response) =>
new BlockHeight(
(response.result as { epochStartHeight: number }).epochStartHeight,
),
);
});
describe('valid observer', () => {
describe('read interactions', () => {
it('should return the same prescribed observers for the current epoch', async () => {
const {
result: refreshPrescribedObservers,
}: { result: WeightedObserver[] } = await contract.viewState({
function: 'prescribedObservers',
});
expect(refreshPrescribedObservers).toEqual(prescribedObservers);
});
});
describe('write interactions', () => {
beforeAll(async () => {
// ensure that we are past the delay period
const minimumObservationHeight =
prevState.distributions.epochEndHeight + EPOCH_DISTRIBUTION_DELAY + 1;
const diffInBlocks =
minimumObservationHeight - (await getCurrentBlock(arweave)).valueOf();
if (diffInBlocks > 0) {
await mineBlocks(arweave, diffInBlocks);
}
});
it('should save observations in epoch if prescribed observer and the current block height is past the epoch start height + delay period', async () => {
const writeInteractions = await Promise.all(
prescribedObserverWallets.map((wallet) => {
contract = warp
.contract<IOState>(srcContractId)
.connect(wallet.jwk);
return contract.writeInteraction({
function: 'saveObservations',
observerReportTxId: EXAMPLE_OBSERVER_REPORT_TX_IDS[0],
failedGateways: [failedGateways[0]],
});
}),
);
const { cachedValue: newCachedValue } = await contract.readState();
const newState = newCachedValue.state as IOState;
expect(
writeInteractions.every((interaction) => interaction?.originalTxId),
).toEqual(true);
expect(
writeInteractions.every((interaction) => {
return !newCachedValue.errorMessages[interaction?.originalTxId];
}),
).toEqual(true);
expect(
newState.observations[currentEpochStartHeight.valueOf()],
).toEqual({
failureSummaries: {
[failedGateways[0]]: expect.arrayContaining(
prescribedObservers.map((w) => w.observerAddress),
),
},
reports: prescribedObservers.reduce(
(report, observer) => ({
...report,
[observer.observerAddress]: EXAMPLE_OBSERVER_REPORT_TX_IDS[0],
}),
{},
),
});
});
it('should allow an observer to update their observation with new failures/report if selected as observer and the current block height is past the epoch start height + delay period', async () => {
const previousSummary =
prevState.observations[currentEpochStartHeight.valueOf()]
?.failureSummaries;
const previousReports =
prevState.observations[currentEpochStartHeight.valueOf()]?.reports;
contract = warp
.contract<IOState>(srcContractId)
.connect(prescribedObserverWallets[0].jwk);
const writeInteraction = await contract.writeInteraction({
function: 'saveObservations',
observerReportTxId: EXAMPLE_OBSERVER_REPORT_TX_IDS[1],
failedGateways: failedGateways,
});
expect(writeInteraction?.originalTxId).not.toBe(undefined);
const { cachedValue: newCachedValue } = await contract.readState();
const newState = newCachedValue.state as IOState;
expect(
newCachedValue.errorMessages[writeInteraction?.originalTxId],
).toBeUndefined();
expect(
newState.observations[currentEpochStartHeight.valueOf()],
).toEqual({
failureSummaries: {
...previousSummary,
[failedGateways[1]]: [prescribedObserverWallets[0].addr],
},
reports: expect.objectContaining({
...previousReports,
[prescribedObserverWallets[0].addr]:
EXAMPLE_OBSERVER_REPORT_TX_IDS[1],
}),
});
});
});
it('should save observations if prescribed observer with all using multiple failed gateways', async () => {
const writeInteractions = await Promise.all(
prescribedObserverWallets.map((wallet) => {
contract = warp.contract<IOState>(srcContractId).connect(wallet.jwk);
return contract.writeInteraction({
function: 'saveObservations',
observerReportTxId: EXAMPLE_OBSERVER_REPORT_TX_IDS[0],
failedGateways: failedGateways,
});
}),
);
const { cachedValue: newCachedValue } = await contract.readState();
const updatedState = newCachedValue.state as IOState;
expect(
writeInteractions.every((interaction) => interaction?.originalTxId),
).toEqual(true);
expect(
writeInteractions.every((interaction) => {
return !Object.keys(newCachedValue.errorMessages).includes(
interaction?.originalTxId,
);
}),
).toEqual(true);
expect(
updatedState.observations[currentEpochStartHeight.valueOf()],
).toEqual({
failureSummaries: {
[failedGateways[0]]: expect.arrayContaining(
prescribedObservers.map((w) => w.observerAddress),
),
[failedGateways[1]]: expect.arrayContaining(
prescribedObservers.map((w) => w.observerAddress),
),
},
reports: prescribedObserverWallets.reduce(
(report, wallet) => ({
...report,
[wallet.addr]: EXAMPLE_OBSERVER_REPORT_TX_IDS[0],
}),
{},
),
});
});
it('should update gateways observerReportTxId tx id if gateway is a prescribed observer saves observation again within the same epoch', async () => {
const previousReportsAndSummary =
prevState.observations[currentEpochStartHeight.valueOf()];
const writeInteractions = await Promise.all(
prescribedObserverWallets.map((wallet) => {
contract = warp.contract<IOState>(srcContractId).connect(wallet.jwk);
return contract.writeInteraction({
function: 'saveObservations',
observerReportTxId: EXAMPLE_OBSERVER_REPORT_TX_IDS[1],
failedGateways: [],
});
}),
);
const { cachedValue: newCachedValue } = await contract.readState();
const newState = newCachedValue.state as IOState;
expect(
writeInteractions.every((interaction) => interaction?.originalTxId),
).toEqual(true);
expect(
writeInteractions.every((interaction) => {
return !Object.keys(newCachedValue.errorMessages).includes(
interaction?.originalTxId,
);
}),
).toEqual(true);
expect(newState.observations[currentEpochStartHeight.valueOf()]).toEqual({
failureSummaries: previousReportsAndSummary.failureSummaries,
reports: prescribedObserverWallets.reduce(
(report, wallet) => ({
...report,
[wallet.addr]: EXAMPLE_OBSERVER_REPORT_TX_IDS[1],
}),
{},
),
});
});
});
describe('non-prescribed observer', () => {
describe('write interactions', () => {
it('should not save observation report if not prescribed observer', async () => {
const nonPrescribedObserver = wallets[8].jwk; // not allowed to observe
contract = warp
.contract<IOState>(srcContractId)
.connect(nonPrescribedObserver);
const writeInteraction = await contract.writeInteraction({
function: 'saveObservations',
observerReportTxId: EXAMPLE_OBSERVER_REPORT_TX_IDS[0],
failedGateways: failedGateways,
});
expect(writeInteraction?.originalTxId).toBeDefined();
const { cachedValue: newCachedValue } = await contract.readState();
expect(
newCachedValue.errorMessages[writeInteraction?.originalTxId],
).toEqual(INVALID_OBSERVATION_CALLER_MESSAGE);
expect(newCachedValue.state).toEqual(prevState);
});
it('should not save observation report if the caller is not a registered observer', async () => {
const notJoinedGateway = await createLocalWallet(arweave);
contract = warp
.contract<IOState>(srcContractId)
.connect(notJoinedGateway.wallet);
const writeInteraction = await contract.writeInteraction({
function: 'saveObservations',
observerReportTxId: EXAMPLE_OBSERVER_REPORT_TX_IDS[0],
failedGateways: failedGateways,
});
expect(writeInteraction?.originalTxId).toBeDefined();
const { cachedValue: newCachedValue } = await contract.readState();
expect(
newCachedValue.errorMessages[writeInteraction?.originalTxId],
).toEqual(INVALID_OBSERVATION_CALLER_MESSAGE);
expect(newCachedValue.state).toEqual(prevState);
});
});
});
describe('fast forwarding to the next epoch', () => {
it('should update the prescribed observers, distributed balances, and increment gateway stats when distribution happens', async () => {
await mineBlocks(arweave, EPOCH_BLOCK_LENGTH);
const writeInteraction = await contract
.connect(wallets[0].jwk)
.writeInteraction({
function: 'tick',
});
// it should have have failed
const { cachedValue: newCachedValue } = await contract.readState();
expect(
newCachedValue.errorMessages[writeInteraction?.originalTxId],
).toBeUndefined();
const newState = newCachedValue.state as IOState;
// updated correctly
expect(newState.distributions).toEqual({
epochZeroStartHeight: prevState.distributions.epochZeroStartHeight,
epochPeriod: prevState.distributions.epochPeriod + 1,
epochStartHeight: prevState.distributions.epochEndHeight + 1,
epochEndHeight:
prevState.distributions.epochEndHeight + EPOCH_BLOCK_LENGTH,
nextDistributionHeight:
prevState.distributions.epochEndHeight +
EPOCH_BLOCK_LENGTH +
EPOCH_DISTRIBUTION_DELAY,
});
const gatewaysAroundDuringEpoch = Object.keys(prevState.gateways).filter(
(gatewayAddress) =>
prevState.gateways[gatewayAddress].start <=
prevState.distributions.epochStartHeight &&
(prevState.gateways[gatewayAddress].end === 0 ||
prevState.gateways[gatewayAddress].end >
prevState.distributions.epochEndHeight),
);
const gatewaysExistedButNotStarted = Object.keys(
prevState.gateways,
).reduce((gateways: Gateways, gatewayAddress) => {
if (
prevState.gateways[gatewayAddress].start >
prevState.distributions.epochStartHeight
) {
return {
...gateways,
[gatewayAddress]: prevState.gateways[gatewayAddress],
};
}
return gateways;
}, {});
expect(newState.gateways).toEqual({
...gatewaysExistedButNotStarted,
...gatewaysAroundDuringEpoch.reduce(
(gateways: Gateways, gatewayAddress) => {
const gateway = prevState.gateways[gatewayAddress];
const didFail =
prevState.observations[prevState.distributions.epochStartHeight]
?.failureSummaries[gatewayAddress] ||
[].length >
prescribedObservers.length * OBSERVATION_FAILURE_THRESHOLD;
const wasPrescribed = prescribedObservers.some(
(observer) => observer.observerAddress === gateway.observerWallet,
);
const didObserve =
prevState.observations[prevState.distributions.epochStartHeight]
.reports[gateway.observerWallet] !== undefined;
return {
...gateways,
[gatewayAddress]: {
...gateway,
operatorStake: newState.gateways[gatewayAddress].operatorStake, // staking happens in other tests, so just ensure it's a number
delegates: newState.gateways[gatewayAddress].delegates,
// what we care about are the stats being properly incremented for the epoch
stats: {
failedConsecutiveEpochs: didFail
? gateway.stats.failedConsecutiveEpochs + 1
: gateway.stats.failedConsecutiveEpochs,
submittedEpochCount: didObserve
? gateway.stats.submittedEpochCount + 1
: gateway.stats.submittedEpochCount,
totalEpochsPrescribedCount: wasPrescribed
? gateway.stats.totalEpochsPrescribedCount + 1
: gateway.stats.totalEpochsPrescribedCount,
passedEpochCount: didFail
? gateway.stats.passedEpochCount
: gateway.stats.passedEpochCount + 1,
totalEpochParticipationCount:
gateway.stats.totalEpochParticipationCount + 1,
},
},
};
},
{},
),
});
});
});
});