@totemsdk/omnia-factory 0.1.4 → 0.1.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/LICENSE +21 -0
- package/README.md +4 -4
- package/dist/commitment.js +6 -3
- package/dist/factory.js +27 -21
- package/dist/index.js +21 -5
- package/dist/script.js +12 -6
- package/dist/settlement.js +17 -13
- package/dist/types.js +2 -1
- package/dist/virtual.js +14 -10
- package/package.json +39 -11
- package/src/__tests__/factory.test.ts +0 -763
- package/src/commitment.ts +0 -35
- package/src/factory.ts +0 -397
- package/src/index.ts +0 -38
- package/src/script.ts +0 -67
- package/src/settlement.ts +0 -188
- package/src/types.ts +0 -142
- package/src/virtual.ts +0 -298
|
@@ -1,763 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createFactory,
|
|
3
|
-
acceptFactory,
|
|
4
|
-
reallocate,
|
|
5
|
-
enforceConservation,
|
|
6
|
-
openVirtualChannel,
|
|
7
|
-
closeVirtualChannel,
|
|
8
|
-
closeFactory,
|
|
9
|
-
buildDisputePayload,
|
|
10
|
-
buildFactoryScript,
|
|
11
|
-
buildAndHashFactoryScript,
|
|
12
|
-
computeFactoryStateCommitment,
|
|
13
|
-
} from '../index';
|
|
14
|
-
import type {
|
|
15
|
-
ChannelFactory,
|
|
16
|
-
FactoryParticipant,
|
|
17
|
-
WotsLeaseBundle,
|
|
18
|
-
OmniaChannel,
|
|
19
|
-
} from '../index';
|
|
20
|
-
|
|
21
|
-
// ─── Mock builders ────────────────────────────────────────────────────────────
|
|
22
|
-
|
|
23
|
-
function makeMockLeaseProvider() {
|
|
24
|
-
let counter = 0;
|
|
25
|
-
return {
|
|
26
|
-
reserveKeyUse: jest.fn(async () => ({
|
|
27
|
-
reservationId: `res-${counter++}`,
|
|
28
|
-
indices: { addressIndex: 0, l1: 0, l2: counter - 1 },
|
|
29
|
-
expiresAt: Date.now() + 60_000,
|
|
30
|
-
})),
|
|
31
|
-
commitKeyUse: jest.fn(async () => {}),
|
|
32
|
-
burnReservation: jest.fn(async () => {}),
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function makeMockSigner(pkd: string) {
|
|
37
|
-
return {
|
|
38
|
-
publicKeyDigest: pkd,
|
|
39
|
-
sign: jest.fn(async () => new Uint8Array(64).fill(0x42)),
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* A WotsLeaseBundle with a mock leaseProvider, mock signer, and a
|
|
45
|
-
* `verify: () => true` override so tests don't need real WOTS key material.
|
|
46
|
-
*/
|
|
47
|
-
function makeMockBundle(pkd: string): WotsLeaseBundle {
|
|
48
|
-
return {
|
|
49
|
-
leaseProvider: makeMockLeaseProvider(),
|
|
50
|
-
signer: makeMockSigner(pkd),
|
|
51
|
-
verify: () => true,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Make a pre-populated fail-verify bundle (for error-path tests). */
|
|
56
|
-
function makeBadBundle(pkd: string): WotsLeaseBundle {
|
|
57
|
-
return {
|
|
58
|
-
leaseProvider: makeMockLeaseProvider(),
|
|
59
|
-
signer: makeMockSigner(pkd),
|
|
60
|
-
verify: () => false,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// ─── Test fixtures ────────────────────────────────────────────────────────────
|
|
65
|
-
|
|
66
|
-
const TOKEN_A = '0x00';
|
|
67
|
-
|
|
68
|
-
const ALICE: FactoryParticipant = {
|
|
69
|
-
partyId: 'alice',
|
|
70
|
-
publicKeyDigest: '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
|
71
|
-
addressIndex: 0,
|
|
72
|
-
contributionAmount: 1000n,
|
|
73
|
-
settlementAddress: '0xALICE_SETTLE',
|
|
74
|
-
};
|
|
75
|
-
const BOB: FactoryParticipant = {
|
|
76
|
-
partyId: 'bob',
|
|
77
|
-
publicKeyDigest: '0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
|
|
78
|
-
addressIndex: 1,
|
|
79
|
-
contributionAmount: 500n,
|
|
80
|
-
settlementAddress: '0xBOB_SETTLE',
|
|
81
|
-
};
|
|
82
|
-
const CAROL: FactoryParticipant = {
|
|
83
|
-
partyId: 'carol',
|
|
84
|
-
publicKeyDigest: '0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
|
|
85
|
-
addressIndex: 2,
|
|
86
|
-
contributionAmount: 500n,
|
|
87
|
-
settlementAddress: '0xCAROL_SETTLE',
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
// One bundle per participant.
|
|
91
|
-
const ALICE_BUNDLE = makeMockBundle(ALICE.publicKeyDigest);
|
|
92
|
-
const BOB_BUNDLE = makeMockBundle(BOB.publicKeyDigest);
|
|
93
|
-
const CAROL_BUNDLE = makeMockBundle(CAROL.publicKeyDigest);
|
|
94
|
-
|
|
95
|
-
const BUNDLES3: Record<string, WotsLeaseBundle> = {
|
|
96
|
-
alice: ALICE_BUNDLE,
|
|
97
|
-
bob: BOB_BUNDLE,
|
|
98
|
-
carol: CAROL_BUNDLE,
|
|
99
|
-
};
|
|
100
|
-
const BUNDLES2: Record<string, WotsLeaseBundle> = {
|
|
101
|
-
alice: ALICE_BUNDLE,
|
|
102
|
-
bob: BOB_BUNDLE,
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Create and fully activate a factory.
|
|
109
|
-
* Proposer = first participant; all others call `acceptFactory`.
|
|
110
|
-
*/
|
|
111
|
-
async function makeActiveFactory(participants: FactoryParticipant[]): Promise<ChannelFactory> {
|
|
112
|
-
const bundles: Record<string, WotsLeaseBundle> = {};
|
|
113
|
-
for (const p of participants) {
|
|
114
|
-
bundles[p.partyId] = makeMockBundle(p.publicKeyDigest);
|
|
115
|
-
}
|
|
116
|
-
const proposer = participants[0];
|
|
117
|
-
let f = await createFactory(participants, TOKEN_A, bundles[proposer.partyId]);
|
|
118
|
-
for (const p of participants.slice(1)) {
|
|
119
|
-
f = await acceptFactory(f, bundles[p.partyId]);
|
|
120
|
-
}
|
|
121
|
-
return f;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** Assert balance conservation at every step. */
|
|
125
|
-
function checkConservation(factory: ChannelFactory): void {
|
|
126
|
-
const allocSum = Object.values(factory.allocations).reduce((a, b) => a + b, 0n);
|
|
127
|
-
const vcSum = factory.virtualChannels.reduce((acc, vc) => acc + vc.totalValue, 0n);
|
|
128
|
-
expect(allocSum + vcSum).toBe(factory.totalValue);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/** Attach a dummy fundingCoinId to a factory (needed by closeFactory). */
|
|
132
|
-
function withFundingCoinId(factory: ChannelFactory, coinId = 'test-coin-001'): ChannelFactory {
|
|
133
|
-
return { ...factory, fundingCoinId: coinId };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// ─── 1. Factory script ────────────────────────────────────────────────────────
|
|
137
|
-
|
|
138
|
-
describe('buildFactoryScript', () => {
|
|
139
|
-
test('generates N-of-N MULTISIG script for 2 parties', () => {
|
|
140
|
-
const script = buildFactoryScript([ALICE, BOB]);
|
|
141
|
-
expect(script).toContain('MULTISIG(2');
|
|
142
|
-
expect(script).toContain('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
|
|
143
|
-
expect(script).toContain('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB');
|
|
144
|
-
expect(script).toContain('LET SETTLEMENT=STATE(100)');
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
test('generates N-of-N MULTISIG script for 3 parties', () => {
|
|
148
|
-
const script = buildFactoryScript([ALICE, BOB, CAROL]);
|
|
149
|
-
expect(script).toContain('MULTISIG(3');
|
|
150
|
-
expect(script).toContain('CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC');
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
test('throws with fewer than 2 participants', () => {
|
|
154
|
-
expect(() => buildFactoryScript([ALICE])).toThrow(/2 participants/);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
test('buildAndHashFactoryScript returns non-empty script and address', () => {
|
|
158
|
-
const { script, address } = buildAndHashFactoryScript([ALICE, BOB]);
|
|
159
|
-
expect(script.length).toBeGreaterThan(0);
|
|
160
|
-
expect(address.length).toBeGreaterThan(0);
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// ─── 2. State commitment ──────────────────────────────────────────────────────
|
|
165
|
-
|
|
166
|
-
describe('computeFactoryStateCommitment', () => {
|
|
167
|
-
test('returns 32 bytes', () => {
|
|
168
|
-
expect(computeFactoryStateCommitment('fid', 1, { alice: 1000n }, []).length).toBe(32);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
test('is deterministic regardless of key order', () => {
|
|
172
|
-
const c1 = computeFactoryStateCommitment('fid', 1, { alice: 1000n, bob: 500n }, ['vc-1']);
|
|
173
|
-
const c2 = computeFactoryStateCommitment('fid', 1, { bob: 500n, alice: 1000n }, ['vc-1']);
|
|
174
|
-
expect(Buffer.from(c1).toString('hex')).toBe(Buffer.from(c2).toString('hex'));
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test('differs for different allocations', () => {
|
|
178
|
-
const c1 = computeFactoryStateCommitment('fid', 1, { alice: 1000n }, []);
|
|
179
|
-
const c2 = computeFactoryStateCommitment('fid', 1, { alice: 999n }, []);
|
|
180
|
-
expect(Buffer.from(c1).toString('hex')).not.toBe(Buffer.from(c2).toString('hex'));
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
test('differs for different sequences', () => {
|
|
184
|
-
const c1 = computeFactoryStateCommitment('fid', 1, { alice: 1000n }, []);
|
|
185
|
-
const c2 = computeFactoryStateCommitment('fid', 2, { alice: 1000n }, []);
|
|
186
|
-
expect(Buffer.from(c1).toString('hex')).not.toBe(Buffer.from(c2).toString('hex'));
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
// ─── 3. createFactory ─────────────────────────────────────────────────────────
|
|
191
|
-
|
|
192
|
-
describe('createFactory', () => {
|
|
193
|
-
test('initialises allocations from contributionAmounts', async () => {
|
|
194
|
-
const f = await createFactory([ALICE, BOB, CAROL], TOKEN_A, ALICE_BUNDLE);
|
|
195
|
-
expect(f.allocations).toEqual({ alice: 1000n, bob: 500n, carol: 500n });
|
|
196
|
-
expect(f.totalValue).toBe(2000n);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
test('status is opening', async () => {
|
|
200
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
201
|
-
expect(f.status).toBe('opening');
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
test('proposer signature stored in pendingSignatures', async () => {
|
|
205
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
206
|
-
expect(f.pendingSignatures['alice']).toBeInstanceOf(Uint8Array);
|
|
207
|
-
expect(f.pendingSignatures['bob']).toBeUndefined();
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
test('pendingCommitment is set', async () => {
|
|
211
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
212
|
-
expect(typeof f.pendingCommitment).toBe('string');
|
|
213
|
-
expect((f.pendingCommitment ?? '').length).toBeGreaterThan(0);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
test('stateLog has create entry', async () => {
|
|
217
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
218
|
-
expect(f.stateLog[0].event).toBe('create');
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
test('fundingTxId is undefined when no chainProvider given', async () => {
|
|
222
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
223
|
-
expect(f.fundingTxId).toBeUndefined();
|
|
224
|
-
expect(f.fundingCoinId).toBeUndefined();
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
test('leaseProvider.reserveKeyUse + commitKeyUse called for proposer', async () => {
|
|
228
|
-
const bundle = makeMockBundle(ALICE.publicKeyDigest);
|
|
229
|
-
await createFactory([ALICE, BOB], TOKEN_A, bundle);
|
|
230
|
-
expect(bundle.leaseProvider.reserveKeyUse).toHaveBeenCalledTimes(1);
|
|
231
|
-
expect(bundle.leaseProvider.commitKeyUse).toHaveBeenCalledTimes(1);
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
test('throws when fewer than 2 participants', async () => {
|
|
235
|
-
await expect(createFactory([ALICE], TOKEN_A, ALICE_BUNDLE)).rejects.toThrow(/2 participants/);
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
test('throws when signer publicKeyDigest not in participants', async () => {
|
|
239
|
-
const strangerBundle = makeMockBundle('0xdeadbeefdeadbeef');
|
|
240
|
-
await expect(
|
|
241
|
-
createFactory([ALICE, BOB], TOKEN_A, strangerBundle),
|
|
242
|
-
).rejects.toThrow(/does not match/);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
test('conservation holds after creation', async () => {
|
|
246
|
-
checkConservation(await createFactory([ALICE, BOB, CAROL], TOKEN_A, ALICE_BUNDLE));
|
|
247
|
-
});
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
// ─── 4. acceptFactory ─────────────────────────────────────────────────────────
|
|
251
|
-
|
|
252
|
-
describe('acceptFactory', () => {
|
|
253
|
-
test('partial accept does not change status', async () => {
|
|
254
|
-
const f = await createFactory([ALICE, BOB, CAROL], TOKEN_A, ALICE_BUNDLE);
|
|
255
|
-
const f2 = await acceptFactory(f, BOB_BUNDLE);
|
|
256
|
-
expect(f2.status).toBe('opening');
|
|
257
|
-
expect(f2.pendingSignatures['bob']).toBeInstanceOf(Uint8Array);
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
test('all N parties → factory becomes active (2-party)', async () => {
|
|
261
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
262
|
-
const f2 = await acceptFactory(f, BOB_BUNDLE);
|
|
263
|
-
expect(f2.status).toBe('active');
|
|
264
|
-
expect(f2.currentSequence).toBe(1);
|
|
265
|
-
expect(f2.pendingCommitment).toBeUndefined();
|
|
266
|
-
expect(Object.keys(f2.pendingSignatures)).toHaveLength(0);
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
test('all N parties → factory becomes active (3-party)', async () => {
|
|
270
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
271
|
-
expect(f.status).toBe('active');
|
|
272
|
-
expect(f.currentSequence).toBe(1);
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
test('stateLog has accept entry after all parties sign', async () => {
|
|
276
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
277
|
-
expect(f.stateLog[1].event).toBe('accept');
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
test('leaseProvider called for each acceptFactory party', async () => {
|
|
281
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
282
|
-
const bobBundle = makeMockBundle(BOB.publicKeyDigest);
|
|
283
|
-
await acceptFactory(f, bobBundle);
|
|
284
|
-
expect(bobBundle.leaseProvider.reserveKeyUse).toHaveBeenCalledTimes(1);
|
|
285
|
-
expect(bobBundle.leaseProvider.commitKeyUse).toHaveBeenCalledTimes(1);
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
test('throws when factory is not in opening status', async () => {
|
|
289
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
290
|
-
await expect(acceptFactory(f, BOB_BUNDLE)).rejects.toThrow(/opening/);
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
test('throws when signer not in participants', async () => {
|
|
294
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
295
|
-
const strangerBundle = makeMockBundle('0xdeadbeef');
|
|
296
|
-
await expect(acceptFactory(f, strangerBundle)).rejects.toThrow(/does not match/);
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
test('throws when party already signed', async () => {
|
|
300
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
301
|
-
await expect(acceptFactory(f, ALICE_BUNDLE)).rejects.toThrow(/already co-signed/);
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
test('burnReservation called when verification fails', async () => {
|
|
305
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
306
|
-
const badBob = makeBadBundle(BOB.publicKeyDigest);
|
|
307
|
-
await expect(acceptFactory(f, badBob)).rejects.toThrow(/verification failed/);
|
|
308
|
-
expect(badBob.leaseProvider.burnReservation).toHaveBeenCalled();
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
test('conservation holds after activation', async () => {
|
|
312
|
-
checkConservation(await makeActiveFactory([ALICE, BOB, CAROL]));
|
|
313
|
-
});
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
// ─── 5. reallocate ────────────────────────────────────────────────────────────
|
|
317
|
-
|
|
318
|
-
describe('reallocate', () => {
|
|
319
|
-
test('updates allocations atomically', async () => {
|
|
320
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
321
|
-
const f2 = await reallocate(f, 'alice', 'bob', 200n, BUNDLES2);
|
|
322
|
-
expect(f2.allocations).toEqual({ alice: 800n, bob: 700n });
|
|
323
|
-
expect(f2.currentSequence).toBe(2);
|
|
324
|
-
checkConservation(f2);
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
test('stateLog has reallocate entry', async () => {
|
|
328
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
329
|
-
const f2 = await reallocate(f, 'alice', 'bob', 100n, BUNDLES2);
|
|
330
|
-
expect(f2.stateLog.at(-1)?.event).toBe('reallocate');
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
test('leaseProvider called for each participant', async () => {
|
|
334
|
-
const lp1 = makeMockLeaseProvider();
|
|
335
|
-
const lp2 = makeMockLeaseProvider();
|
|
336
|
-
const bundles: Record<string, WotsLeaseBundle> = {
|
|
337
|
-
alice: { leaseProvider: lp1, signer: makeMockSigner(ALICE.publicKeyDigest), verify: () => true },
|
|
338
|
-
bob: { leaseProvider: lp2, signer: makeMockSigner(BOB.publicKeyDigest), verify: () => true },
|
|
339
|
-
};
|
|
340
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
341
|
-
await reallocate(f, 'alice', 'bob', 100n, bundles);
|
|
342
|
-
expect(lp1.reserveKeyUse).toHaveBeenCalledTimes(1);
|
|
343
|
-
expect(lp2.reserveKeyUse).toHaveBeenCalledTimes(1);
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
test('throws when from party has insufficient allocation', async () => {
|
|
347
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
348
|
-
await expect(reallocate(f, 'bob', 'alice', 1000n, BUNDLES2)).rejects.toThrow(/Insufficient/);
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
test('throws when factory is not active', async () => {
|
|
352
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
353
|
-
await expect(reallocate(f, 'alice', 'bob', 100n, BUNDLES2)).rejects.toThrow(/active/);
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
test('throws when to party is not a participant', async () => {
|
|
357
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
358
|
-
await expect(reallocate(f, 'alice', 'dave', 100n, BUNDLES2)).rejects.toThrow(/not a registered/);
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
test('throws when amount is zero', async () => {
|
|
362
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
363
|
-
await expect(reallocate(f, 'alice', 'bob', 0n, BUNDLES2)).rejects.toThrow(/positive/);
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
test('throws when a leaseProvider bundle is missing', async () => {
|
|
367
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
368
|
-
const partial = { alice: ALICE_BUNDLE, bob: BOB_BUNDLE }; // carol missing
|
|
369
|
-
await expect(reallocate(f, 'alice', 'bob', 100n, partial)).rejects.toThrow(/Missing/);
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
// ─── 6. openVirtualChannel ────────────────────────────────────────────────────
|
|
374
|
-
|
|
375
|
-
describe('openVirtualChannel', () => {
|
|
376
|
-
test('deducts from participant allocations', async () => {
|
|
377
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
378
|
-
const { factory: f2 } = await openVirtualChannel(
|
|
379
|
-
f, ['alice', 'bob'], { alice: 200n, bob: 100n }, BUNDLES3,
|
|
380
|
-
);
|
|
381
|
-
expect(f2.allocations.alice).toBe(800n);
|
|
382
|
-
expect(f2.allocations.bob).toBe(400n);
|
|
383
|
-
expect(f2.allocations.carol).toBe(500n);
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
test('increments currentSequence', async () => {
|
|
387
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
388
|
-
const seq = f.currentSequence;
|
|
389
|
-
const { factory: f2 } = await openVirtualChannel(
|
|
390
|
-
f, ['alice', 'bob'], { alice: 100n, bob: 50n }, BUNDLES3,
|
|
391
|
-
);
|
|
392
|
-
expect(f2.currentSequence).toBe(seq + 1);
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
test('conservation holds after opening', async () => {
|
|
396
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
397
|
-
const { factory: f2 } = await openVirtualChannel(
|
|
398
|
-
f, ['alice', 'bob'], { alice: 200n, bob: 100n }, BUNDLES3,
|
|
399
|
-
);
|
|
400
|
-
checkConservation(f2);
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
test('returned channel has correct fields', async () => {
|
|
404
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
405
|
-
const { channel } = await openVirtualChannel(
|
|
406
|
-
f, ['alice', 'bob'], { alice: 200n, bob: 100n }, BUNDLES3,
|
|
407
|
-
);
|
|
408
|
-
expect(channel.channelType).toBe('virtual');
|
|
409
|
-
expect(channel.factoryRef).toBe(f.factoryId);
|
|
410
|
-
expect(channel.tokenId).toBe(TOKEN_A);
|
|
411
|
-
expect(channel.totalValue).toBe(300n);
|
|
412
|
-
expect(channel.balances.alice).toBe(200n);
|
|
413
|
-
expect(channel.balances.bob).toBe(100n);
|
|
414
|
-
expect(channel.status).toBe('active');
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
test('channel appears in factory.virtualChannels', async () => {
|
|
418
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
419
|
-
const { factory: f2, channel } = await openVirtualChannel(
|
|
420
|
-
f, ['alice', 'bob'], { alice: 200n, bob: 100n }, BUNDLES3,
|
|
421
|
-
);
|
|
422
|
-
expect(f2.virtualChannels).toHaveLength(1);
|
|
423
|
-
expect(f2.virtualChannels[0].channelId).toBe(channel.channelId);
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
test('two VCs can be opened simultaneously', async () => {
|
|
427
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
428
|
-
const { factory: f2 } = await openVirtualChannel(
|
|
429
|
-
f, ['alice', 'bob'], { alice: 200n, bob: 100n }, BUNDLES3,
|
|
430
|
-
);
|
|
431
|
-
const { factory: f3 } = await openVirtualChannel(
|
|
432
|
-
f2, ['bob', 'carol'], { bob: 100n, carol: 100n }, BUNDLES3,
|
|
433
|
-
);
|
|
434
|
-
expect(f3.virtualChannels).toHaveLength(2);
|
|
435
|
-
checkConservation(f3);
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
test('all N party bundles are invoked (verify-on-sign gate)', async () => {
|
|
439
|
-
const verifyCalls: string[] = [];
|
|
440
|
-
const spyBundles: Record<string, WotsLeaseBundle> = {
|
|
441
|
-
alice: { leaseProvider: makeMockLeaseProvider(), signer: makeMockSigner(ALICE.publicKeyDigest), verify: (_, __, pkd) => { verifyCalls.push(pkd); return true; } },
|
|
442
|
-
bob: { leaseProvider: makeMockLeaseProvider(), signer: makeMockSigner(BOB.publicKeyDigest), verify: (_, __, pkd) => { verifyCalls.push(pkd); return true; } },
|
|
443
|
-
carol: { leaseProvider: makeMockLeaseProvider(), signer: makeMockSigner(CAROL.publicKeyDigest), verify: (_, __, pkd) => { verifyCalls.push(pkd); return true; } },
|
|
444
|
-
};
|
|
445
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
446
|
-
await openVirtualChannel(f, ['alice', 'bob'], { alice: 100n, bob: 50n }, spyBundles);
|
|
447
|
-
expect(verifyCalls).toHaveLength(3);
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
test('throws when party has insufficient allocation', async () => {
|
|
451
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
452
|
-
await expect(openVirtualChannel(f, ['bob', 'carol'], { bob: 600n, carol: 100n }, BUNDLES3))
|
|
453
|
-
.rejects.toThrow(/insufficient/i);
|
|
454
|
-
});
|
|
455
|
-
|
|
456
|
-
test('throws when factory is not active', async () => {
|
|
457
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
458
|
-
await expect(openVirtualChannel(f, ['alice', 'bob'], { alice: 100n, bob: 50n }, BUNDLES2))
|
|
459
|
-
.rejects.toThrow(/active/);
|
|
460
|
-
});
|
|
461
|
-
|
|
462
|
-
test('throws when a leaseProvider bundle is missing', async () => {
|
|
463
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
464
|
-
const partial = { alice: ALICE_BUNDLE, bob: BOB_BUNDLE }; // carol missing
|
|
465
|
-
await expect(openVirtualChannel(f, ['alice', 'bob'], { alice: 100n, bob: 50n }, partial))
|
|
466
|
-
.rejects.toThrow(/Missing/);
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
test('throws when verification fails for any party', async () => {
|
|
470
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
471
|
-
const badBundles: Record<string, WotsLeaseBundle> = {
|
|
472
|
-
alice: makeBadBundle(ALICE.publicKeyDigest),
|
|
473
|
-
bob: makeBadBundle(BOB.publicKeyDigest),
|
|
474
|
-
carol: makeBadBundle(CAROL.publicKeyDigest),
|
|
475
|
-
};
|
|
476
|
-
await expect(openVirtualChannel(f, ['alice', 'bob'], { alice: 100n, bob: 50n }, badBundles))
|
|
477
|
-
.rejects.toThrow(/verification failed/);
|
|
478
|
-
});
|
|
479
|
-
|
|
480
|
-
test('custom channelId is respected', async () => {
|
|
481
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
482
|
-
const { channel } = await openVirtualChannel(
|
|
483
|
-
f, ['alice', 'bob'], { alice: 100n, bob: 50n }, BUNDLES2, 'my-vc-id',
|
|
484
|
-
);
|
|
485
|
-
expect(channel.channelId).toBe('my-vc-id');
|
|
486
|
-
});
|
|
487
|
-
});
|
|
488
|
-
|
|
489
|
-
// ─── 7. closeVirtualChannel ───────────────────────────────────────────────────
|
|
490
|
-
|
|
491
|
-
describe('closeVirtualChannel', () => {
|
|
492
|
-
async function openVC(factory: ChannelFactory) {
|
|
493
|
-
return openVirtualChannel(factory, ['alice', 'bob'], { alice: 200n, bob: 100n }, BUNDLES3);
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
test('uses latestState.balances as final allocations when set', async () => {
|
|
497
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
498
|
-
const { factory: f2, channel } = await openVC(f);
|
|
499
|
-
const channelWithState: OmniaChannel = {
|
|
500
|
-
...channel,
|
|
501
|
-
latestState: { sequence: 1, balances: { alice: 250n, bob: 50n }, pendingHTLCs: [], signatures: {}, signingIndices: {} } as any,
|
|
502
|
-
};
|
|
503
|
-
const f3 = await closeVirtualChannel(f2, channelWithState, BUNDLES3);
|
|
504
|
-
expect(f3.allocations.alice).toBe(1050n); // 800 + 250
|
|
505
|
-
expect(f3.allocations.bob).toBe(450n); // 400 + 50
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
test('uses initial balances when latestState is null', async () => {
|
|
509
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
510
|
-
const { factory: f2, channel } = await openVC(f);
|
|
511
|
-
// channel.latestState is null by default
|
|
512
|
-
const f3 = await closeVirtualChannel(f2, channel, BUNDLES3);
|
|
513
|
-
expect(f3.allocations.alice).toBe(1000n); // 800 + 200 (initial)
|
|
514
|
-
expect(f3.allocations.bob).toBe(500n); // 400 + 100 (initial)
|
|
515
|
-
});
|
|
516
|
-
|
|
517
|
-
test('increments currentSequence', async () => {
|
|
518
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
519
|
-
const { factory: f2, channel } = await openVC(f);
|
|
520
|
-
const seqBefore = f2.currentSequence;
|
|
521
|
-
const f3 = await closeVirtualChannel(f2, channel, BUNDLES3);
|
|
522
|
-
expect(f3.currentSequence).toBe(seqBefore + 1);
|
|
523
|
-
});
|
|
524
|
-
|
|
525
|
-
test('removes channel from virtualChannels', async () => {
|
|
526
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
527
|
-
const { factory: f2, channel } = await openVC(f);
|
|
528
|
-
const f3 = await closeVirtualChannel(f2, channel, BUNDLES3);
|
|
529
|
-
expect(f3.virtualChannels).toHaveLength(0);
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
test('conservation holds after closing', async () => {
|
|
533
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
534
|
-
const { factory: f2, channel } = await openVC(f);
|
|
535
|
-
checkConservation(await closeVirtualChannel(f2, channel, BUNDLES3));
|
|
536
|
-
});
|
|
537
|
-
|
|
538
|
-
test('all N party bundles are invoked', async () => {
|
|
539
|
-
const verifyCalls: string[] = [];
|
|
540
|
-
const spyBundles: Record<string, WotsLeaseBundle> = {
|
|
541
|
-
alice: { leaseProvider: makeMockLeaseProvider(), signer: makeMockSigner(ALICE.publicKeyDigest), verify: (_, __, pkd) => { verifyCalls.push(pkd); return true; } },
|
|
542
|
-
bob: { leaseProvider: makeMockLeaseProvider(), signer: makeMockSigner(BOB.publicKeyDigest), verify: (_, __, pkd) => { verifyCalls.push(pkd); return true; } },
|
|
543
|
-
carol: { leaseProvider: makeMockLeaseProvider(), signer: makeMockSigner(CAROL.publicKeyDigest), verify: (_, __, pkd) => { verifyCalls.push(pkd); return true; } },
|
|
544
|
-
};
|
|
545
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
546
|
-
const { factory: f2, channel } = await openVC(f);
|
|
547
|
-
await closeVirtualChannel(f2, channel, spyBundles);
|
|
548
|
-
expect(verifyCalls).toHaveLength(3);
|
|
549
|
-
});
|
|
550
|
-
|
|
551
|
-
test('throws when channelId not found', async () => {
|
|
552
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
553
|
-
const phantom: OmniaChannel = { channelId: 'nonexistent' } as any;
|
|
554
|
-
await expect(closeVirtualChannel(f, phantom, BUNDLES2)).rejects.toThrow(/not found/);
|
|
555
|
-
});
|
|
556
|
-
|
|
557
|
-
test('throws when a leaseProvider bundle is missing', async () => {
|
|
558
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
559
|
-
const { factory: f2, channel } = await openVC(f);
|
|
560
|
-
const partial = { alice: ALICE_BUNDLE, bob: BOB_BUNDLE }; // carol missing
|
|
561
|
-
await expect(closeVirtualChannel(f2, channel, partial)).rejects.toThrow(/Missing/);
|
|
562
|
-
});
|
|
563
|
-
});
|
|
564
|
-
|
|
565
|
-
// ─── 8. Full 3-party lifecycle ────────────────────────────────────────────────
|
|
566
|
-
|
|
567
|
-
describe('3-party factory lifecycle', () => {
|
|
568
|
-
test('create → activate → open 2 VCs → close VCs → close factory', async () => {
|
|
569
|
-
// Activate
|
|
570
|
-
const f0 = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
571
|
-
checkConservation(f0);
|
|
572
|
-
|
|
573
|
-
// Open two VCs (all 3 parties sign each open)
|
|
574
|
-
const { factory: f1, channel: vc1 } = await openVirtualChannel(
|
|
575
|
-
f0, ['alice', 'bob'], { alice: 200n, bob: 100n }, BUNDLES3,
|
|
576
|
-
);
|
|
577
|
-
const { factory: f2, channel: vc2 } = await openVirtualChannel(
|
|
578
|
-
f1, ['bob', 'carol'], { bob: 100n, carol: 100n }, BUNDLES3,
|
|
579
|
-
);
|
|
580
|
-
expect(f2.virtualChannels).toHaveLength(2);
|
|
581
|
-
checkConservation(f2);
|
|
582
|
-
expect(f2.allocations.alice).toBe(800n);
|
|
583
|
-
expect(f2.allocations.bob).toBe(300n);
|
|
584
|
-
expect(f2.allocations.carol).toBe(400n);
|
|
585
|
-
expect(f2.currentSequence).toBe(f0.currentSequence + 2);
|
|
586
|
-
|
|
587
|
-
// Close VC1 with net transfer alice→bob
|
|
588
|
-
const vc1Final: OmniaChannel = {
|
|
589
|
-
...vc1,
|
|
590
|
-
latestState: { sequence: 1, balances: { alice: 150n, bob: 150n }, pendingHTLCs: [], signatures: {}, signingIndices: {} } as any,
|
|
591
|
-
};
|
|
592
|
-
const f3 = await closeVirtualChannel(f2, vc1Final, BUNDLES3);
|
|
593
|
-
|
|
594
|
-
// Close VC2 with net transfer bob→carol
|
|
595
|
-
const vc2Final: OmniaChannel = {
|
|
596
|
-
...vc2,
|
|
597
|
-
latestState: { sequence: 1, balances: { bob: 70n, carol: 130n }, pendingHTLCs: [], signatures: {}, signingIndices: {} } as any,
|
|
598
|
-
};
|
|
599
|
-
const f4 = await closeVirtualChannel(f3, vc2Final, BUNDLES3);
|
|
600
|
-
expect(f4.virtualChannels).toHaveLength(0);
|
|
601
|
-
checkConservation(f4);
|
|
602
|
-
|
|
603
|
-
// Verify final allocations
|
|
604
|
-
expect(f4.allocations.alice).toBe(950n); // 800 + 150
|
|
605
|
-
expect(f4.allocations.bob).toBe(520n); // 300 + 150 + 70
|
|
606
|
-
expect(f4.allocations.carol).toBe(530n); // 400 + 130
|
|
607
|
-
expect(f4.allocations.alice + f4.allocations.bob + f4.allocations.carol).toBe(2000n);
|
|
608
|
-
expect(f4.currentSequence).toBe(f0.currentSequence + 4);
|
|
609
|
-
|
|
610
|
-
// Cooperative close
|
|
611
|
-
const settlement = await closeFactory(withFundingCoinId(f4), BUNDLES3);
|
|
612
|
-
expect(settlement.factoryId).toBe(f4.factoryId);
|
|
613
|
-
expect(settlement.settlementTxHex.length).toBeGreaterThan(0);
|
|
614
|
-
expect(settlement.finalAllocations).toEqual(f4.allocations);
|
|
615
|
-
});
|
|
616
|
-
|
|
617
|
-
test('sequence is monotonically increasing across all state transitions', async () => {
|
|
618
|
-
const f0 = await makeActiveFactory([ALICE, BOB, CAROL]); // seq=1
|
|
619
|
-
expect(f0.currentSequence).toBe(1);
|
|
620
|
-
|
|
621
|
-
const { factory: f1 } = await openVirtualChannel(
|
|
622
|
-
f0, ['alice', 'bob'], { alice: 100n, bob: 50n }, BUNDLES3,
|
|
623
|
-
);
|
|
624
|
-
expect(f1.currentSequence).toBe(2);
|
|
625
|
-
|
|
626
|
-
const f2 = await reallocate(f1, 'carol', 'alice', 100n, BUNDLES3);
|
|
627
|
-
expect(f2.currentSequence).toBe(3);
|
|
628
|
-
|
|
629
|
-
const f3 = await closeVirtualChannel(f2, f1.virtualChannels[0], BUNDLES3);
|
|
630
|
-
expect(f3.currentSequence).toBe(4);
|
|
631
|
-
});
|
|
632
|
-
|
|
633
|
-
test('conservation is maintained across all operations', async () => {
|
|
634
|
-
const f0 = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
635
|
-
checkConservation(f0);
|
|
636
|
-
|
|
637
|
-
const { factory: f1, channel: vc } = await openVirtualChannel(
|
|
638
|
-
f0, ['alice', 'carol'], { alice: 500n, carol: 200n }, BUNDLES3,
|
|
639
|
-
);
|
|
640
|
-
checkConservation(f1);
|
|
641
|
-
|
|
642
|
-
const f2 = await reallocate(f1, 'bob', 'carol', 100n, BUNDLES3);
|
|
643
|
-
checkConservation(f2);
|
|
644
|
-
|
|
645
|
-
const vcWithFinal: OmniaChannel = {
|
|
646
|
-
...vc,
|
|
647
|
-
latestState: { sequence: 1, balances: { alice: 400n, carol: 300n }, pendingHTLCs: [], signatures: {}, signingIndices: {} } as any,
|
|
648
|
-
};
|
|
649
|
-
const f3 = await closeVirtualChannel(f2, vcWithFinal, BUNDLES3);
|
|
650
|
-
checkConservation(f3);
|
|
651
|
-
});
|
|
652
|
-
});
|
|
653
|
-
|
|
654
|
-
// ─── 9. closeFactory ─────────────────────────────────────────────────────────
|
|
655
|
-
|
|
656
|
-
describe('closeFactory', () => {
|
|
657
|
-
test('returns correct settlement payload', async () => {
|
|
658
|
-
const f = withFundingCoinId(await makeActiveFactory([ALICE, BOB]));
|
|
659
|
-
const s = await closeFactory(f, BUNDLES2);
|
|
660
|
-
expect(s.factoryId).toBe(f.factoryId);
|
|
661
|
-
expect(s.finalAllocations).toEqual({ alice: 1000n, bob: 500n });
|
|
662
|
-
expect(s.settlementTxHex.length).toBeGreaterThan(0);
|
|
663
|
-
});
|
|
664
|
-
|
|
665
|
-
test('settlementTxHex is a non-empty string (serializeTxDraft output)', async () => {
|
|
666
|
-
const f = withFundingCoinId(await makeActiveFactory([ALICE, BOB]));
|
|
667
|
-
const s = await closeFactory(f, BUNDLES2);
|
|
668
|
-
expect(typeof s.settlementTxHex).toBe('string');
|
|
669
|
-
});
|
|
670
|
-
|
|
671
|
-
test('no txpowId when chainProvider omitted', async () => {
|
|
672
|
-
const f = withFundingCoinId(await makeActiveFactory([ALICE, BOB]));
|
|
673
|
-
expect((await closeFactory(f, BUNDLES2)).txpowId).toBeUndefined();
|
|
674
|
-
});
|
|
675
|
-
|
|
676
|
-
test('all N party settlement signers are invoked', async () => {
|
|
677
|
-
const verifyCalls: string[] = [];
|
|
678
|
-
const spyBundles: Record<string, WotsLeaseBundle> = {
|
|
679
|
-
alice: { leaseProvider: makeMockLeaseProvider(), signer: makeMockSigner(ALICE.publicKeyDigest), verify: (_, __, pkd) => { verifyCalls.push(pkd); return true; } },
|
|
680
|
-
bob: { leaseProvider: makeMockLeaseProvider(), signer: makeMockSigner(BOB.publicKeyDigest), verify: (_, __, pkd) => { verifyCalls.push(pkd); return true; } },
|
|
681
|
-
};
|
|
682
|
-
const f = withFundingCoinId(await makeActiveFactory([ALICE, BOB]));
|
|
683
|
-
await closeFactory(f, spyBundles);
|
|
684
|
-
expect(verifyCalls).toHaveLength(2);
|
|
685
|
-
});
|
|
686
|
-
|
|
687
|
-
test('throws when settlement signature verification fails', async () => {
|
|
688
|
-
const f = withFundingCoinId(await makeActiveFactory([ALICE, BOB]));
|
|
689
|
-
const badBundles: Record<string, WotsLeaseBundle> = {
|
|
690
|
-
alice: makeBadBundle(ALICE.publicKeyDigest),
|
|
691
|
-
bob: makeBadBundle(BOB.publicKeyDigest),
|
|
692
|
-
};
|
|
693
|
-
await expect(closeFactory(f, badBundles)).rejects.toThrow(/verification failed/);
|
|
694
|
-
});
|
|
695
|
-
|
|
696
|
-
test('throws (fail-fast) when fundingCoinId is absent', async () => {
|
|
697
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
698
|
-
// fundingCoinId is undefined by default (no chainProvider in test)
|
|
699
|
-
await expect(closeFactory(f, BUNDLES2)).rejects.toThrow(/fundingCoinId/);
|
|
700
|
-
});
|
|
701
|
-
|
|
702
|
-
test('throws when a leaseProvider bundle is missing', async () => {
|
|
703
|
-
const f = withFundingCoinId(await makeActiveFactory([ALICE, BOB, CAROL]));
|
|
704
|
-
await expect(closeFactory(f, BUNDLES2)).rejects.toThrow(/Missing/);
|
|
705
|
-
});
|
|
706
|
-
|
|
707
|
-
test('throws when virtual channels are still open', async () => {
|
|
708
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
709
|
-
const { factory: f2 } = await openVirtualChannel(
|
|
710
|
-
f, ['alice', 'bob'], { alice: 100n, bob: 50n }, BUNDLES2,
|
|
711
|
-
);
|
|
712
|
-
await expect(closeFactory(withFundingCoinId(f2), BUNDLES2)).rejects.toThrow(/virtual channels/);
|
|
713
|
-
});
|
|
714
|
-
|
|
715
|
-
test('throws when factory is not active', async () => {
|
|
716
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
717
|
-
await expect(closeFactory(withFundingCoinId(f), BUNDLES2)).rejects.toThrow(/active/);
|
|
718
|
-
});
|
|
719
|
-
});
|
|
720
|
-
|
|
721
|
-
// ─── 10. buildDisputePayload ──────────────────────────────────────────────────
|
|
722
|
-
|
|
723
|
-
describe('buildDisputePayload', () => {
|
|
724
|
-
test('includes stateLog with dispute entry appended', async () => {
|
|
725
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
726
|
-
const { factory: f2 } = await openVirtualChannel(
|
|
727
|
-
f, ['alice', 'bob'], { alice: 100n, bob: 50n }, BUNDLES2,
|
|
728
|
-
);
|
|
729
|
-
const dispute = buildDisputePayload(f2, '0xevidence');
|
|
730
|
-
expect(dispute.factoryId).toBe(f2.factoryId);
|
|
731
|
-
expect(dispute.latestSequence).toBe(f2.currentSequence);
|
|
732
|
-
expect(dispute.stateLog.at(-1)?.event).toBe('dispute');
|
|
733
|
-
expect(dispute.virtualChannelIds).toHaveLength(1);
|
|
734
|
-
expect(dispute.evidence).toBe('0xevidence');
|
|
735
|
-
});
|
|
736
|
-
|
|
737
|
-
test('allocations match factory', async () => {
|
|
738
|
-
const f = await makeActiveFactory([ALICE, BOB, CAROL]);
|
|
739
|
-
expect(buildDisputePayload(f, 'e').allocations).toEqual(f.allocations);
|
|
740
|
-
});
|
|
741
|
-
|
|
742
|
-
test('works on a factory still in opening status (no restriction)', async () => {
|
|
743
|
-
const f = await createFactory([ALICE, BOB], TOKEN_A, ALICE_BUNDLE);
|
|
744
|
-
const dispute = buildDisputePayload(f, 'emergency');
|
|
745
|
-
expect(dispute.latestSequence).toBe(0);
|
|
746
|
-
expect(dispute.stateLog.at(-1)?.event).toBe('dispute');
|
|
747
|
-
});
|
|
748
|
-
});
|
|
749
|
-
|
|
750
|
-
// ─── 11. enforceConservation ──────────────────────────────────────────────────
|
|
751
|
-
|
|
752
|
-
describe('enforceConservation', () => {
|
|
753
|
-
test('passes for a valid factory', async () => {
|
|
754
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
755
|
-
expect(() => enforceConservation(f)).not.toThrow();
|
|
756
|
-
});
|
|
757
|
-
|
|
758
|
-
test('throws when allocations are corrupted', async () => {
|
|
759
|
-
const f = await makeActiveFactory([ALICE, BOB]);
|
|
760
|
-
const broken: ChannelFactory = { ...f, allocations: { alice: 9999n, bob: 500n } };
|
|
761
|
-
expect(() => enforceConservation(broken)).toThrow(/conservation/);
|
|
762
|
-
});
|
|
763
|
-
});
|