querysub 0.311.0 → 0.312.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "querysub",
3
- "version": "0.311.0",
3
+ "version": "0.312.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "note1": "note on node-forge fork, see https://github.com/digitalbazaar/forge/issues/744 for details",
@@ -16,6 +16,7 @@ import { timeoutToError } from "../errors";
16
16
  import { delay } from "socket-function/src/batching";
17
17
  import { formatTime } from "socket-function/src/formatting/format";
18
18
  import { waitForFirstTimeSync } from "socket-function/time/trueTimeShim";
19
+ import { red } from "socket-function/src/formatting/logColors";
19
20
 
20
21
  let callerInfo = new Map<CallerContext, {
21
22
  reconnectNodeId: string | undefined;
@@ -200,5 +201,10 @@ SocketFunction.addGlobalClientHook(async function identityHook(context) {
200
201
  if (isClientNodeId(context.call.nodeId)) {
201
202
  return;
202
203
  }
204
+ let time = Date.now();
203
205
  await changeIdentityOnce(context.connectionId);
206
+ let duration = Date.now() - time;
207
+ if (duration > 200) {
208
+ console.log(red(`IdentityHook took ${formatTime(duration)} for ${context.connectionId.nodeId} ${context.call.classGuid}.${context.call.functionName}`));
209
+ }
204
210
  });
@@ -51,7 +51,7 @@ export const assertIsNetworkTrusted = requiresNetworkTrustHook;
51
51
  let lastArchivesTrusted: string[] | undefined;
52
52
  let trustedCache = new Set<string>();
53
53
  let untrustedCache = new Map<string, number>();
54
- export const isTrusted = runInSerial(async function isTrusted(machineId: string) {
54
+ export const isTrusted = measureWrap(async function isTrusted(machineId: string) {
55
55
  // See the comment in requiresNetworkTrustHook for why clients have to trust all callers.
56
56
  if (isClient()) return true;
57
57
 
@@ -68,11 +68,26 @@ export const isTrusted = runInSerial(async function isTrusted(machineId: string)
68
68
  return false;
69
69
  }
70
70
 
71
- // Find is faster than get, and usually we don't need the full certificate
72
- let trustedMachineIds = await archives().find("");
73
- lastArchivesTrusted = trustedMachineIds.slice();
71
+ return await isTrustedBase(machineId);
72
+ });
73
+ let trustedCachePopulated = false;
74
+ const isTrustedBase = runInSerial(measureWrap(async function isTrustedBase(machineId: string) {
75
+ if (!trustedCachePopulated) {
76
+ trustedCachePopulated = true;
77
+ let trustedMachineIds = await archives().find("");
78
+ lastArchivesTrusted = trustedMachineIds.slice();
79
+ for (let trustedMachineId of trustedMachineIds) {
80
+ trustedCache.add(trustedMachineId);
81
+ }
82
+ } else {
83
+ // Checking a single entry is a lot faster (as find is slow)
84
+ let trusted = await archives().get(machineId);
85
+ if (trusted) {
86
+ trustedCache.add(machineId);
87
+ }
88
+ }
74
89
  // Always trust ourself
75
- trustedMachineIds.push(getOwnMachineId());
90
+ trustedCache.add(getOwnMachineId());
76
91
  // IF developing, trust localhost. This allows us to develop without port forwards,
77
92
  // on our services, which INCREASES security, and prevents dev machines from being
78
93
  // connected to by attackers (as dev machines might reveal unfinished content, or even
@@ -80,13 +95,7 @@ export const isTrusted = runInSerial(async function isTrusted(machineId: string)
80
95
  // - Don't trust this on public, as in theory an attacker MIGHT be able to connect
81
96
  // from localhost (but not have disk read/write access)? Maybe...
82
97
  if (!isPublic()) {
83
- trustedMachineIds.push("127-0-0-1." + getDomain());
84
- }
85
-
86
- // NOTE: This should be safe from collisions with existing files, because... while there might be a metadata
87
- // file which exists, it will never also be a valid hash/public key, which machineId will always be.
88
- for (let trustedMachineId of trustedMachineIds) {
89
- trustedCache.add(trustedMachineId);
98
+ trustedCache.add("127-0-0-1." + getDomain());
90
99
  }
91
100
 
92
101
  if (!trustedCache.has(machineId)) {
@@ -95,7 +104,7 @@ export const isTrusted = runInSerial(async function isTrusted(machineId: string)
95
104
  } else {
96
105
  return true;
97
106
  }
98
- });
107
+ }));
99
108
 
100
109
  export async function isNodeTrusted(nodeId: string) {
101
110
  let domainName = getNodeIdDomainMaybeUndefined(nodeId);
@@ -44,6 +44,8 @@ let DISK_AUDIT_RATE = timeInMinute * 15;
44
44
  // probably is less than that). Which is around 2.5 cents on digital ocean IF we go over
45
45
  // our 1TB/month allowance.
46
46
  let API_AUDIT_RATE = timeInSecond * 30;
47
+ // BUT, for now, poll less often... because I think it is lagging our 2 core potato digital ocean server.
48
+ API_AUDIT_RATE = timeInMinute * 5;
47
49
  let API_AUDIT_COUNT = 12;
48
50
 
49
51
 
@@ -353,7 +355,7 @@ async function runHeartbeatAuditLoop() {
353
355
  if (removedNodeIds.length > 0) {
354
356
  console.log(blue(`Removed ${removedNodeIds.length}/${nodeIds.length} nodes from node list`), { removedNodeIds });
355
357
  await syncArchives();
356
- await tellEveryoneNodesChanges();
358
+ await tellEveryoneNodesChanges(`removedNodeIds ${removedNodeIds.join("|")}`);
357
359
  }
358
360
  });
359
361
  }
@@ -517,7 +519,7 @@ if (isServer()) {
517
519
 
518
520
  export async function forceRemoveNode(nodeId: string) {
519
521
  await archives().del(nodeId);
520
- void tellEveryoneNodesChanges();
522
+ void tellEveryoneNodesChanges(`forceRemoveNode ${nodeId}`);
521
523
  }
522
524
 
523
525
 
@@ -528,13 +530,14 @@ export async function nodeDiscoveryShutdown() {
528
530
  if (isServer()) {
529
531
  await archives().del(getOwnNodeId());
530
532
  }
531
- void tellEveryoneNodesChanges();
533
+ void tellEveryoneNodesChanges("nodeDiscoveryShutdown");
532
534
  }
533
- const tellEveryoneNodesChanges = throttleFunction(1000, function tellEveryoneNodesChanges() {
535
+ const tellEveryoneNodesChanges = throttleFunction(1000, function tellEveryoneNodesChanges(reason: string) {
534
536
  if (isClient()) return;
537
+ console.log(red(`Telling everyone nodes changed`));
535
538
  for (let nodeId of allNodeIds2) {
536
539
  if (isOwnNodeId(nodeId)) continue;
537
- ignoreErrors(NodeDiscoveryController.nodes[nodeId].resyncNodes());
540
+ ignoreErrors(NodeDiscoveryController.nodes[nodeId].resyncNodes(reason));
538
541
  }
539
542
  });
540
543
 
@@ -544,7 +547,9 @@ class NodeDiscoveryControllerBase {
544
547
  console.log(magenta(`Received addNode`), { nodeId });
545
548
  addNodeId(nodeId);
546
549
  }
547
- public async resyncNodes() {
550
+ public async resyncNodes(reason: string) {
551
+ let caller = SocketFunction.getCaller();
552
+ console.log(magenta(`Received resyncNodes from ${caller.nodeId}, reason = ${reason}`));
548
553
  await syncArchives();
549
554
  }
550
555
  public async getAllNodesHash(): Promise<string> {