@activeledger/activenetwork 5.2.2 → 5.4.0

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 (59) hide show
  1. package/es/network/endpoints.d.ts +1 -0
  2. package/es/network/endpoints.d.ts.map +1 -1
  3. package/es/network/endpoints.js +29 -3
  4. package/es/network/endpoints.js.map +1 -1
  5. package/es/network/host.d.ts +19 -0
  6. package/es/network/host.d.ts.map +1 -1
  7. package/es/network/host.js +245 -50
  8. package/es/network/host.js.map +1 -1
  9. package/es/network/hybridAdmin.d.ts +23 -0
  10. package/es/network/hybridAdmin.d.ts.map +1 -1
  11. package/es/network/hybridAdmin.js +24 -0
  12. package/es/network/hybridAdmin.js.map +1 -1
  13. package/es/network/hybridApi.d.ts +5 -0
  14. package/es/network/hybridApi.d.ts.map +1 -1
  15. package/es/network/hybridApi.js +43 -10
  16. package/es/network/hybridApi.js.map +1 -1
  17. package/es/network/ipc.d.ts +6 -0
  18. package/es/network/ipc.d.ts.map +1 -0
  19. package/es/network/ipc.js +18 -0
  20. package/es/network/ipc.js.map +1 -0
  21. package/es/network/locker.d.ts.map +1 -1
  22. package/es/network/locker.js +21 -19
  23. package/es/network/locker.js.map +1 -1
  24. package/es/network/maintain.d.ts +1 -0
  25. package/es/network/maintain.d.ts.map +1 -1
  26. package/es/network/maintain.js +7 -0
  27. package/es/network/maintain.js.map +1 -1
  28. package/es/network/process.js +51 -10
  29. package/es/network/process.js.map +1 -1
  30. package/lib/network/endpoints.d.ts +1 -0
  31. package/lib/network/endpoints.d.ts.map +1 -1
  32. package/lib/network/endpoints.js +29 -3
  33. package/lib/network/endpoints.js.map +1 -1
  34. package/lib/network/host.d.ts +19 -0
  35. package/lib/network/host.d.ts.map +1 -1
  36. package/lib/network/host.js +244 -49
  37. package/lib/network/host.js.map +1 -1
  38. package/lib/network/hybridAdmin.d.ts +23 -0
  39. package/lib/network/hybridAdmin.d.ts.map +1 -1
  40. package/lib/network/hybridAdmin.js +26 -1
  41. package/lib/network/hybridAdmin.js.map +1 -1
  42. package/lib/network/hybridApi.d.ts +5 -0
  43. package/lib/network/hybridApi.d.ts.map +1 -1
  44. package/lib/network/hybridApi.js +43 -10
  45. package/lib/network/hybridApi.js.map +1 -1
  46. package/lib/network/ipc.d.ts +6 -0
  47. package/lib/network/ipc.d.ts.map +1 -0
  48. package/lib/network/ipc.js +24 -0
  49. package/lib/network/ipc.js.map +1 -0
  50. package/lib/network/locker.d.ts.map +1 -1
  51. package/lib/network/locker.js +21 -19
  52. package/lib/network/locker.js.map +1 -1
  53. package/lib/network/maintain.d.ts +1 -0
  54. package/lib/network/maintain.d.ts.map +1 -1
  55. package/lib/network/maintain.js +7 -0
  56. package/lib/network/maintain.js.map +1 -1
  57. package/lib/network/process.js +51 -10
  58. package/lib/network/process.js.map +1 -1
  59. package/package.json +9 -9
@@ -2,17 +2,19 @@ import { readFileSync } from "fs";
2
2
  import { fork } from "child_process";
3
3
  import * as net from "net";
4
4
  import { ActiveDSConnect, ActiveOptions, ActiveGZip, ActiveClone, } from "@activeledger/activeoptions";
5
+ import { ActiveProtocol } from "@activeledger/activeprotocol";
5
6
  import { ActiveCrypto } from "@activeledger/activecrypto";
6
7
  import { ActiveLogger, ActiveTiming } from "@activeledger/activelogger";
7
8
  import { Home } from "./home";
8
9
  import { HybridApi, LedgerHybridRegistry, recordFailed } from "./hybridApi";
9
10
  import { registerHybridRoutes } from "./hybridRoutes";
10
11
  import { startHybridP2P } from "./hybridP2P";
11
- import { HybridBans, feedsStatus, isLoopback, listHybrids } from "./hybridAdmin";
12
+ import { HybridBans, feedsStatus, hybridStatus, isLoopback, listHybrids } from "./hybridAdmin";
12
13
  import { Neighbour } from "./neighbour";
13
14
  import { ActiveInterfaces } from "./utils";
14
15
  import { Endpoints, Maintain } from "./index";
15
16
  import { Locker } from "./locker";
17
+ import { withJsonFallback } from "./ipc";
16
18
  import { PhysicalCores } from "./cpus";
17
19
  import { DEFAULT_WORKER_COUNT, workerCount } from "./workercount";
18
20
  import { workerExecArgv } from "./workerheap";
@@ -27,10 +29,12 @@ import { gunzip } from "zlib";
27
29
  const GZIP_MIN_RESPONSE_BYTES = 16384;
28
30
  const gunzipAsync = promisify(gunzip);
29
31
  const TIMER_QUEUE_INTERVAL = 2 * 1000;
32
+ const RESENT_LOAD_WAIT_MS = 5 * 1000;
30
33
  const GRACEFUL_PROC_SHUTDOWN = 7 * 60 * 1000;
31
34
  const KILL_PROC_SHUTDOWN = 2.5 * 1000;
32
35
  const QUEUE_MAX_WAIT_MS = Number(process.env.AL_QUEUE_MAX_WAIT_MS || 7_000);
33
36
  const QUEUE_RECHECK_MS = Number(process.env.AL_QUEUE_RECHECK_MS ?? 50);
37
+ const QUEUE_SCAN_MIN_MS = Number(process.env.AL_QUEUE_SCAN_MIN_MS ?? 20);
34
38
  const MAX_RETRIES = 35;
35
39
  function peekBytes(chunks, n) {
36
40
  if (chunks.length > 0 && chunks[0].length >= n) {
@@ -264,51 +268,55 @@ export class Host extends Home {
264
268
  }
265
269
  });
266
270
  }
271
+ static p2pServerWanted() {
272
+ return ActiveOptions.get("p2pStreamServer", false);
273
+ }
274
+ p2pConnection(socket) {
275
+ socket.setNoDelay(true);
276
+ ActiveLogger.info(`P2P TCP connection accepted from ${socket.remoteAddress}`);
277
+ let chunks = [];
278
+ let bufferLength = 0;
279
+ socket.on("data", async (data) => {
280
+ chunks.push(data);
281
+ bufferLength += data.length;
282
+ while (bufferLength >= 44) {
283
+ const header = peekBytes(chunks, 44);
284
+ const length = header.readUInt32BE(40);
285
+ if (bufferLength >= 44 + length) {
286
+ const fullBuffer = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks);
287
+ const senderRef = fullBuffer.slice(0, 40).toString().trim();
288
+ const item = fullBuffer.slice(44, 44 + length);
289
+ const remaining = fullBuffer.slice(44 + length);
290
+ chunks = remaining.length > 0 ? [remaining] : [];
291
+ bufferLength = remaining.length;
292
+ let tx;
293
+ try {
294
+ tx = await ActiveClone.deserialize(item);
295
+ }
296
+ catch (error) {
297
+ ActiveLogger.warn(error, `P2P dropped malformed frame from ${socket.remoteAddress} (sender ${senderRef})`);
298
+ continue;
299
+ }
300
+ this.processEndpoints({
301
+ headers: { "X-Activeledger": senderRef },
302
+ method: "POST",
303
+ url: "/a/init",
304
+ connection: { remoteAddress: socket.remoteAddress || "127.0.0.1" },
305
+ }, null, tx, senderRef, true);
306
+ }
307
+ else {
308
+ break;
309
+ }
310
+ }
311
+ });
312
+ socket.on("error", (err) => {
313
+ ActiveLogger.error(err, "P2P socket error");
314
+ });
315
+ }
267
316
  constructor() {
268
317
  super();
269
- if (ActiveOptions.get("p2pStreamServer", false)) {
270
- this.p2pServer = net.createServer((socket) => {
271
- socket.setNoDelay(true);
272
- ActiveLogger.info(`P2P TCP connection accepted from ${socket.remoteAddress}`);
273
- let chunks = [];
274
- let bufferLength = 0;
275
- socket.on("data", async (data) => {
276
- chunks.push(data);
277
- bufferLength += data.length;
278
- while (bufferLength >= 44) {
279
- const header = peekBytes(chunks, 44);
280
- const length = header.readUInt32BE(40);
281
- if (bufferLength >= 44 + length) {
282
- const fullBuffer = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks);
283
- const senderRef = fullBuffer.slice(0, 40).toString().trim();
284
- const item = fullBuffer.slice(44, 44 + length);
285
- const remaining = fullBuffer.slice(44 + length);
286
- chunks = remaining.length > 0 ? [remaining] : [];
287
- bufferLength = remaining.length;
288
- let tx;
289
- try {
290
- tx = await ActiveClone.deserialize(item);
291
- }
292
- catch (error) {
293
- ActiveLogger.warn(error, `P2P dropped malformed frame from ${socket.remoteAddress} (sender ${senderRef})`);
294
- continue;
295
- }
296
- this.processEndpoints({
297
- headers: { "X-Activeledger": senderRef },
298
- method: "POST",
299
- url: "/a/init",
300
- connection: { remoteAddress: socket.remoteAddress || "127.0.0.1" },
301
- }, null, tx, senderRef, true);
302
- }
303
- else {
304
- break;
305
- }
306
- }
307
- });
308
- socket.on("error", (err) => {
309
- ActiveLogger.error(err, "P2P socket error");
310
- });
311
- });
318
+ if (Host.p2pServerWanted()) {
319
+ this.p2pServer = net.createServer((socket) => this.p2pConnection(socket));
312
320
  const p2pPort = parseInt(ActiveInterfaces.getBindingDetails("port")) + 1;
313
321
  this.p2pServer.listen(p2pPort, () => {
314
322
  ActiveLogger.info(`P2P TCP server listening on port ${p2pPort}`);
@@ -321,6 +329,7 @@ export class Host extends Home {
321
329
  this.dbErrorConnection.info();
322
330
  this.dbEventConnection = new ActiveDSConnect(db.url + "/" + db.event);
323
331
  this.dbEventConnection.info();
332
+ void this.loadResentGuard();
324
333
  this.api = App();
325
334
  this.hybridRegistry = new LedgerHybridRegistry(this.dbConnection);
326
335
  const hybridApi = new HybridApi({
@@ -335,7 +344,10 @@ export class Host extends Home {
335
344
  },
336
345
  setupStreamId: () => ActiveOptions.get("network", "").split("@")[0],
337
346
  banned: () => Host.hybridBans.set(),
347
+ attest: (umid, revs, facts) => ActiveProtocol.Attestation.attestation(this.reference, umid, revs, facts),
338
348
  });
349
+ ActiveProtocol.Attestation.key = Home.identity;
350
+ ActiveProtocol.Attestation.useIdentityFile(ActiveOptions.get("identity", "./.identity"));
339
351
  this.hybridFeeds = registerHybridRoutes(this.api, hybridApi, {
340
352
  maxFeeds: () => Host.hybridMaxFeeds(),
341
353
  });
@@ -605,13 +617,57 @@ export class Host extends Home {
605
617
  },
606
618
  };
607
619
  }
620
+ static ipcSerialization() {
621
+ return ActiveOptions.get("ipcSerialization", "json") === "advanced"
622
+ ? "advanced"
623
+ : "json";
624
+ }
625
+ batchBroadcasts(child) {
626
+ if (ActiveOptions.get("ipcBatching", false) !== true)
627
+ return;
628
+ const rawSend = child.send.bind(child);
629
+ let queued = [];
630
+ let scheduled = false;
631
+ const flush = () => {
632
+ scheduled = false;
633
+ if (!queued.length)
634
+ return true;
635
+ const out = queued;
636
+ queued = [];
637
+ try {
638
+ return out.length === 1
639
+ ? rawSend(out[0])
640
+ : rawSend({ type: "batch", data: { messages: out } });
641
+ }
642
+ catch (error) {
643
+ ActiveLogger.warn(error, "Worker message not sent");
644
+ return false;
645
+ }
646
+ };
647
+ child.send = (message, ...rest) => {
648
+ if (message && message.type === "broadcast" && !rest.length) {
649
+ queued.push(message);
650
+ if (!scheduled) {
651
+ scheduled = true;
652
+ setImmediate(flush);
653
+ }
654
+ return true;
655
+ }
656
+ flush();
657
+ return rawSend(message, ...rest);
658
+ };
659
+ }
608
660
  createProcessor(cpuTotal) {
609
661
  const pFork = fork(`${__dirname}/process.js`, [], {
610
662
  cwd: process.cwd(),
611
663
  execArgv: workerExecArgv(),
612
664
  stdio: "inherit",
665
+ serialization: Host.ipcSerialization(),
613
666
  });
614
667
  pFork.stop = false;
668
+ if (Host.ipcSerialization() === "advanced")
669
+ withJsonFallback(pFork);
670
+ this.batchBroadcasts(pFork);
615
671
  let unloadHandled = false;
616
672
  const unloadProcessorSafely = (...error) => {
617
673
  if (unloadHandled || !this.listenSocket) {
@@ -686,6 +742,9 @@ export class Host extends Home {
686
742
  recordFailed(this.dbEventConnection, this.dbConnection, pending.entry, ActiveOptions.get("build", 0));
687
743
  break;
688
744
  case "commited":
745
+ if (m.data.broadcast) {
746
+ this.broadcast(m.data.umid, false);
747
+ }
689
748
  if (!pending)
690
749
  return;
691
750
  pending.resolve({
@@ -755,6 +814,7 @@ export class Host extends Home {
755
814
  Home.right = new Neighbour(this.host, this.port);
756
815
  this.neighbourhood.reset(config.neighbourhood);
757
816
  this.terriBuildMap();
817
+ Maintain.restart();
758
818
  }
759
819
  const reloadMsg = {
760
820
  type: "reload",
@@ -1021,6 +1081,96 @@ export class Host extends Home {
1021
1081
  }
1022
1082
  return robin;
1023
1083
  }
1084
+ resentGuardUntil = 0;
1085
+ resentInFlight = new Map();
1086
+ resentTimer;
1087
+ resentLoaded = false;
1088
+ resentLoading;
1089
+ rememberResent(replaced, replacement, expire) {
1090
+ const until = expire ? new Date(expire).getTime() : NaN;
1091
+ if (!Number.isFinite(until) || until <= Date.now())
1092
+ return;
1093
+ this.resentGuardUntil = Math.max(this.resentGuardUntil, until);
1094
+ this.resentInFlight.set(replaced, replacement);
1095
+ this.dbEventConnection
1096
+ .post({ _id: `resent:${replaced}`, to: replacement, until })
1097
+ .then(() => void this.resentInFlight.delete(replaced), (error) => {
1098
+ ActiveLogger.error({ replaced, error }, "Could not record a resent transaction; refusing a repost from memory only");
1099
+ setTimeout(() => this.resentInFlight.delete(replaced), Math.max(0, until - Date.now())).unref();
1100
+ });
1101
+ this.startResentHousekeeping();
1102
+ }
1103
+ async resentAs(umid) {
1104
+ if (this.resentLoading && !this.resentLoaded)
1105
+ await this.waitForResentList();
1106
+ if (Date.now() >= this.resentGuardUntil)
1107
+ return undefined;
1108
+ const inFlight = this.resentInFlight.get(umid);
1109
+ if (inFlight)
1110
+ return inFlight;
1111
+ const row = await this.dbEventConnection.get(`resent:${umid}`).catch(() => null);
1112
+ if (row === null || row === undefined)
1113
+ throw new Error("could not check whether the transaction was resent");
1114
+ return typeof row.to === "string" && Number(row.until) > Date.now() ? row.to : undefined;
1115
+ }
1116
+ loadResentGuard() {
1117
+ this.resentLoading = this.refreshResentGuard(false).then(() => undefined);
1118
+ return this.resentLoading;
1119
+ }
1120
+ resentWaiting;
1121
+ waitForResentList() {
1122
+ this.resentWaiting ??= (async () => {
1123
+ try {
1124
+ await this.resentLoading;
1125
+ for (let attempt = 0; !this.resentLoaded && attempt < RESENT_LOAD_WAIT_MS / 500; attempt++) {
1126
+ await new Promise((resolve) => setTimeout(resolve, 500).unref());
1127
+ await this.refreshResentGuard(false);
1128
+ }
1129
+ }
1130
+ finally {
1131
+ this.resentWaiting = undefined;
1132
+ }
1133
+ if (!this.resentLoaded)
1134
+ throw new Error("the list of resent transactions has not been read yet");
1135
+ })();
1136
+ return this.resentWaiting;
1137
+ }
1138
+ async refreshResentGuard(retryLater = true) {
1139
+ let latest = 0;
1140
+ try {
1141
+ const list = await this.dbEventConnection.allDocs({ startkey: "resent:", endkey: "resent:x", include_docs: true });
1142
+ if (!list || !Array.isArray(list.rows))
1143
+ throw new Error("no answer");
1144
+ const now = Date.now();
1145
+ for (const row of list.rows) {
1146
+ const doc = row?.doc ?? row;
1147
+ if (!doc?._id)
1148
+ continue;
1149
+ if (Number(doc.until) > now)
1150
+ latest = Math.max(latest, Number(doc.until));
1151
+ else
1152
+ await this.dbEventConnection.purge(doc).catch(() => undefined);
1153
+ }
1154
+ }
1155
+ catch (error) {
1156
+ if (retryLater) {
1157
+ ActiveLogger.warn(`Could not read the resent transactions, trying again in 30s: ${error?.message ?? error}`);
1158
+ setTimeout(() => void this.refreshResentGuard(), 30_000).unref();
1159
+ }
1160
+ return false;
1161
+ }
1162
+ this.resentGuardUntil = Math.max(latest, this.resentInFlight.size ? this.resentGuardUntil : 0);
1163
+ this.resentLoaded = true;
1164
+ if (latest > 0)
1165
+ this.startResentHousekeeping();
1166
+ return true;
1167
+ }
1168
+ startResentHousekeeping() {
1169
+ if (this.resentTimer)
1170
+ return;
1171
+ this.resentTimer = setInterval(() => void this.refreshResentGuard(), 60 * 60 * 1000);
1172
+ this.resentTimer.unref();
1173
+ }
1024
1174
  release(umid, owner) {
1025
1175
  if (owner && this.processPending[umid] && this.processPending[umid].entry !== owner) {
1026
1176
  ActiveLogger.warn(umid, "Not releasing, pending entry belongs to another submission");
@@ -1047,20 +1197,49 @@ export class Host extends Home {
1047
1197
  this.destroy(umid);
1048
1198
  }
1049
1199
  }, hold);
1050
- if (QUEUE_RECHECK_MS > 0) {
1051
- setTimeout(() => this.processQueue(), QUEUE_RECHECK_MS);
1052
- }
1053
- else {
1054
- setImmediate(() => this.processQueue());
1055
- }
1200
+ this.scheduleQueueRecheck(QUEUE_RECHECK_MS);
1056
1201
  }
1057
1202
  else {
1058
1203
  ActiveLogger.warn(umid, "Trying to release unknown umid");
1059
1204
  }
1060
1205
  }
1061
1206
  processingBLQ = false;
1207
+ lastQueueScan = 0;
1208
+ queueRecheckPending = false;
1209
+ scheduleQueueRecheck(ms) {
1210
+ if (this.queueRecheckPending)
1211
+ return;
1212
+ this.queueRecheckPending = true;
1213
+ const run = () => {
1214
+ this.queueRecheckPending = false;
1215
+ this.processQueue();
1216
+ };
1217
+ if (ms > 0) {
1218
+ setTimeout(run, ms);
1219
+ }
1220
+ else {
1221
+ setImmediate(run);
1222
+ }
1223
+ }
1062
1224
  processQueue(next, internal = false) {
1063
1225
  if (!this.processingBLQ) {
1226
+ const queued = this.busyLocksQueue.internal.length + this.busyLocksQueue.external.length;
1227
+ if (!queued) {
1228
+ if (next)
1229
+ this.hold(next);
1230
+ return;
1231
+ }
1232
+ const since = Date.now() - this.lastQueueScan;
1233
+ if (QUEUE_SCAN_MIN_MS > 0 && since < QUEUE_SCAN_MIN_MS) {
1234
+ if (next) {
1235
+ this.hold(next);
1236
+ }
1237
+ else {
1238
+ this.scheduleQueueRecheck(QUEUE_SCAN_MIN_MS - since);
1239
+ }
1240
+ return;
1241
+ }
1242
+ this.lastQueueScan = Date.now();
1064
1243
  this.processingBLQ = true;
1065
1244
  const checked = new Set();
1066
1245
  if (this.busyLocksQueue.internal.length) {
@@ -1125,11 +1304,27 @@ export class Host extends Home {
1125
1304
  case "/a/status":
1126
1305
  response = Endpoints.status(this, requester);
1127
1306
  break;
1307
+ case "/a/hybrid/status":
1128
1308
  case "/a/hybrid/list":
1129
1309
  case "/a/hybrid/feeds":
1130
1310
  if (!isLoopback(req.connection.remoteAddress)) {
1131
1311
  return this.writeResponse(res, 403, "Forbidden", gzipAccepted);
1132
1312
  }
1313
+ if (req.url === "/a/hybrid/status") {
1314
+ const attest = ActiveProtocol.Attestation;
1315
+ response = hybridStatus(this.hybridRegistry, this.hybridFeeds, Host.hybridMaxFeeds(), {
1316
+ build: ActiveOptions.get("build", 0),
1317
+ requiredBuild: attest.BUILD,
1318
+ section: ActiveOptions.get("hybrid", undefined),
1319
+ signing: !attest.enabled()
1320
+ ? "off"
1321
+ : attest.signer() === attest.attestKey
1322
+ ? "ed25519"
1323
+ : "rsa",
1324
+ attestKeyHeld: !!attest.attestKey,
1325
+ }).then((content) => ({ statusCode: 200, content }));
1326
+ break;
1327
+ }
1133
1328
  response =
1134
1329
  req.url === "/a/hybrid/list"
1135
1330
  ? listHybrids(this.hybridRegistry, this.hybridFeeds, Host.hybridBans.set()).then((content) => ({ statusCode: 200, content }))