-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync_test.ts
301 lines (273 loc) · 9.42 KB
/
sync_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
import { load as loadDotenv } from 'https://deno.land/[email protected]/dotenv/mod.ts';
import { sleep } from 'https://deno.land/x/[email protected]/mod.ts';
import { InMemoryStorage } from '@ucla-irl/ndnts-aux/storage';
import { Workspace } from '@ucla-irl/ndnts-aux/workspace';
import { AsyncDisposableStack, base64ToBytes } from '@ucla-irl/ndnts-aux/utils';
import { CertStorage } from '@ucla-irl/ndnts-aux/security';
import { Decoder } from '@ndn/tlv';
import { Component, Data, Name, ValidityPeriod } from '@ndn/packet';
import { Certificate, CertNaming, createSigner, createVerifier, ECDSA } from '@ndn/keychain';
// import { WsTransport } from '@ndn/ws-transport';
import { TcpTransport, UdpTransport, UnixTransport } from '@ndn/node-transport';
import { digestSigning, Signer } from '@ndn/packet';
import * as nfdmgmt from '@ndn/nfdmgmt';
import { Forwarder, FwTracer } from '@ndn/fw';
import { fchQuery } from '@ndn/autoconfig';
import * as Y from 'yjs';
import { produce } from '@ndn/endpoint';
import { fromHex } from '@ndn/util';
// Global configurations
let DEBUG = false;
// const UPDATE_INTERVAL = [300, 1000];
const UPDATE_INTERVAL = [100, 101];
const MAX_SEQUENCE = 1000;
const PAYLOAD_LENGTH = 100;
const LOCAL = true;
const groupKeyBits = fromHex('0102030405060708090A0B0C0D0E0F10');
const decodeCert = (b64Value: string) => {
const wire = base64ToBytes(b64Value);
const data = Decoder.decode(wire, Data);
const cert = Certificate.fromData(data);
return cert;
};
const payloadValue = Array.from({ length: PAYLOAD_LENGTH }).map(() => 'a').join('');
const decodeKeys = async (
tag: string,
certB64?: string,
prvB64?: string,
): Promise<[Signer, Certificate]> => {
if (!certB64 || !prvB64) {
throw new Error(`${tag} key is missing`);
}
const cert = decodeCert(certB64);
console.log(`${tag} Cert: ${cert.name.toString()} \n Period: ${cert.validity.toString()}`);
const prvKeyBits = base64ToBytes(prvB64);
const keyPair = await ECDSA.cryptoGenerate({
importPkcs8: [prvKeyBits, cert.publicKeySpki],
}, true);
const signer = createSigner(
cert.name.getPrefix(cert.name.length - 2),
ECDSA,
keyPair,
).withKeyLocator(cert.name);
return [signer, cert];
};
const randomUint = () => crypto.getRandomValues(new Uint32Array(1))[0];
const issue = async (idName: Name, issuerPrivateKey: Signer): Promise<[Signer, Certificate, ArrayBuffer]> => {
const keyName = CertNaming.makeKeyName(idName);
const algo = ECDSA;
const gen = await ECDSA.cryptoGenerate({}, true);
const privateKey = createSigner(keyName, algo, gen);
const publicKey = createVerifier(keyName, algo, gen);
const prvKeyBits = await crypto.subtle.exportKey('pkcs8', gen.privateKey);
const cert = await Certificate.issue({
validity: ValidityPeriod.daysFromNow(1),
issuerId: new Component(8, 'CA'),
issuerPrivateKey,
publicKey,
});
return [privateKey, cert, prvKeyBits];
};
const doFch = async () => {
try {
const fchRes = await fchQuery({
// transport: 'wss',
transport: 'tcp', // Cannot use TCP/UDP due to prefix registration failure
network: 'ndn',
});
if (fchRes.routers.length > 0) {
return new URL(fchRes.routers[0].connect).host;
} else {
console.error('FCH gives no response.');
Deno.exit(1);
}
} catch {
console.error('FCH server is down');
Deno.exit(1);
}
};
const registerPrefixes = async (fw: Forwarder, workspaceName: Name, nodeId: Name, signer: Signer) => {
// Register prefixes
const cr = await nfdmgmt.invoke('rib/register', {
name: workspaceName,
origin: 65, // client
cost: 0,
flags: 0x02, // CAPTURE
}, {
cOpts: { fw },
prefix: LOCAL ? nfdmgmt.localhostPrefix : nfdmgmt.localhopPrefix,
signer: LOCAL ? digestSigning : signer,
});
if (cr.statusCode !== 200) {
console.error(
`[${Deno.env.get('NODE_ID')}:${Deno.env.get('HOST')}]Unable to register route: ${cr.statusCode} ${cr.statusText}`,
);
Deno.exit();
}
const cr2 = await nfdmgmt.invoke('rib/register', {
name: nodeId,
origin: 65, // client
cost: 0,
flags: 0x02, // CAPTURE
}, {
cOpts: { fw },
prefix: LOCAL ? nfdmgmt.localhostPrefix : nfdmgmt.localhopPrefix,
signer: LOCAL ? digestSigning : signer,
});
if (cr2.statusCode !== 200) {
console.error(
`[${Deno.env.get('NODE_ID')}:${
Deno.env.get('HOST')
}]Unable to register route: ${cr2.statusCode} ${cr2.statusText}`,
);
Deno.exit();
}
};
type ItemType = {
nodeId: string;
timestamp: number;
seq: number;
delta?: number;
payload?: string;
};
const main = async () => {
const testbedCertB64 = Deno.env.get('TESTBED_CERT');
const testbedPrvKeyB64 = Deno.env.get('TESTBED_PRVKEY');
const caCertB64 = Deno.env.get('WORKSPACE_CA_CERT');
const caPrvKeyB64 = Deno.env.get('WORKSPACE_CA_PRVKEY');
const nodeIdInt = parseInt(Deno.env.get('NODE_ID') ?? '0');
const host = Deno.env.get('HOST') ?? '';
await using closers = new AsyncDisposableStack();
// Load credentials
const [testbedSigner, testbedCert] = await decodeKeys('Testbed', testbedCertB64, testbedPrvKeyB64);
const [caSigner, caCert] = await decodeKeys('Workspace CA', caCertB64, caPrvKeyB64);
const workspaceName = caCert.name.getPrefix(caCert.name.length - 4);
// Generate node identity
const nodeIdStr = `node-${nodeIdInt}-${randomUint()}`;
const nodeId = workspaceName.append(nodeIdStr);
const [_mySigner, myCert, myKeyBits] = await issue(nodeId, caSigner);
console.log(`[${nodeIdInt}]My Cert: ${myCert.name.toString()} \n Period: ${myCert.validity.toString()}`);
console.log(`[${nodeIdInt}]DEST: ${host}`);
// Connect to testbed
const fw = Forwarder.getDefault();
// const host = await doFch();
// const wsUrl = `wss://${host}/ws/`;
// const wsFace = await WsTransport.createFace({ l3: { local: false }, fw }, wsUrl);
let face;
try {
if (LOCAL) {
face = await UnixTransport.createFace({ l3: { local: true }, fw }, '/run/nfd/nfd.sock');
} else {
// const wsUrl = `wss://${host}/ws/`;
// face = await WsTransport.createFace({ l3: { local: false }, fw }, wsUrl);
// face = await TcpTransport.createFace({ l3: { local: false }, fw }, { host, port: 6363 });
face = await UdpTransport.createFace({ l3: { local: false }, fw }, { host });
}
} catch (err) {
console.error(`[${nodeIdInt}]Unable to connect: ${host}: ${err}`);
return;
}
closers.defer(() => face.close());
// Create workspace
const storage = new InMemoryStorage();
closers.use(storage);
const certStore = new CertStorage(caCert, myCert, storage, fw, new Uint8Array(myKeyBits));
// Serve certificate
const certProducer = produce(
testbedCert.name.getPrefix(testbedCert.name.length - 2),
() => Promise.resolve(testbedCert.data),
);
closers.defer(() => certProducer.close());
// Register prefixes
try {
await registerPrefixes(fw, workspaceName, nodeId, testbedSigner);
} catch (err) {
console.error(`[${nodeIdInt}]Unable to register: ${err}`);
// await sleep(180);
return;
}
// Run workspace
const rootDoc = new Y.Doc();
const workspace = await Workspace.create({
nodeId: nodeId,
persistStore: storage,
fw,
rootDoc: rootDoc,
signer: certStore.signer,
verifier: certStore.verifier,
useBundler: false,
groupKeyBits: groupKeyBits,
});
closers.defer(() => workspace.destroy());
console.log(`[${nodeIdInt}]Workspace started.`);
// Set array
const arr = rootDoc.getArray('my array');
let seqNum = 0;
const push = () => {
arr.push([JSON.stringify(
{
nodeId: nodeIdStr,
timestamp: Date.now(),
seq: seqNum++,
payload: payloadValue,
} satisfies ItemType,
)]);
};
// Observe log
const logRecords = [] as Array<ItemType>;
arr.observe((event) => {
for (const item of event.changes.added) {
const val = JSON.parse(item.content.getContent()[0]) as ItemType;
const { nodeId, timestamp } = val;
if (nodeId === nodeIdStr || !nodeId) {
continue;
}
const timeNow = Date.now();
const delta = timeNow - timestamp;
// console.log(`[${nodeId} : ${seq}] Delta: ${delta} ms`);
logRecords.push({ ...val, delta });
}
});
// Exit signal
let stop = false;
const { promise: exitSignal, resolve: exitResolve } = Promise.withResolvers<void>();
Deno.addSignalListener('SIGINT', () => {
console.log(`[${nodeIdInt}]Stopped by Ctrl+C`);
stop = true;
exitResolve();
});
// Random push
const runner = (async () => {
const [lower, upper] = UPDATE_INTERVAL;
while (!stop) {
push();
if (MAX_SEQUENCE > 0 && seqNum >= MAX_SEQUENCE) {
break;
}
const intervalMs = randomUint() % (upper - lower) + lower;
await Promise.any([sleep(intervalMs / 1000), exitSignal]);
}
})();
// Await close
await runner;
if (!stop) {
const timer = setTimeout(() => {
exitResolve(); // Assume all sequences are received at this time
}, 1500);
await exitSignal;
clearTimeout(timer);
}
// Write csv file
using file = await Deno.create(`./logs/${nodeIdInt}.csv`);
await file.write(new TextEncoder().encode('id,seq,delay\n'));
for (const item of logRecords) {
await file.write(new TextEncoder().encode(`${item.nodeId},${item.seq},${item.delta}\n`));
}
console.log(`[${nodeIdInt}]Done`);
};
if (import.meta.main) {
await loadDotenv({ export: true });
DEBUG = Deno.env.get('DEBUG') ? true : false;
if (DEBUG) FwTracer.enable();
await main();
}