trac-msb 0.1.76 → 0.1.77

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.
@@ -1 +1 @@
1
- 7a28af069a36ce674ad04718e8ee63fe38c81a7770b144714c7666d085897d1c
1
+
package/msb.mjs CHANGED
@@ -4,11 +4,11 @@ const opts = {
4
4
  stores_directory : 'stores2/',
5
5
  store_name : typeof process !== "undefined" ? process.argv[2] : Pear.config.args[0],
6
6
  bootstrap: 'a4951e5f744e2a9ceeb875a7965762481dab0a7bb0531a71568e34bf7abd2c53',
7
- channel: '0002tracnetworkmainsettlementbus',
7
+ channel: '0002tracnetworkmainsettlementbus'
8
8
  };
9
9
 
10
10
  const msb = new MainSettlementBus(opts);
11
11
 
12
- msb.ready().then(() => {
13
- msb.interactiveMode();
14
- });
12
+ msb.ready()
13
+ .then(() => { msb.interactiveMode(); })
14
+ .catch(function () { });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.1.76",
4
+ "version": "0.1.77",
5
5
  "pear": {
6
6
  "name": "trac-msb",
7
7
  "type": "terminal"
package/src/index.js CHANGED
@@ -15,11 +15,9 @@ import {
15
15
  OperationType,
16
16
  EventType,
17
17
  WHITELIST_SLEEP_INTERVAL,
18
- UPDATER_INTERVAL,
19
18
  MAX_INDEXERS,
20
19
  MIN_INDEXERS,
21
20
  WHITELIST_PREFIX,
22
- TRAC_NAMESPACE
23
21
  } from './utils/constants.js';
24
22
  import Network from './network.js';
25
23
  import Check from './utils/check.js';
@@ -56,6 +54,8 @@ export class MainSettlementBus extends ReadyResource {
56
54
  #readline_instance;
57
55
  #enable_txlogs;
58
56
  #disable_rate_limit;
57
+ #enableValidatorObserver;
58
+ #enableRoleRequester;
59
59
 
60
60
  constructor(options = {}) {
61
61
  super();
@@ -65,7 +65,8 @@ export class MainSettlementBus extends ReadyResource {
65
65
  this.#boot();
66
66
  this.#setupInternalListeners();
67
67
  this.#network = new Network(this.#base);
68
- this.ready().catch(noop);
68
+ this.#enableValidatorObserver = options.enableValidatorObserver !== undefined ? options.enableValidatorObserver : true;
69
+ this.#enableRoleRequester = options.enableRoleRequester !== undefined ? options.enableRoleRequester : true;
69
70
  }
70
71
 
71
72
  #initInternalAttributes(options) {
@@ -454,20 +455,60 @@ export class MainSettlementBus extends ReadyResource {
454
455
  this.validatorObserver();
455
456
  }
456
457
 
457
- async close() {
458
- console.log('Closing everything...');
459
- if (this.#swarm) {
458
+ async _close() {
459
+ console.log('Closing everything gracefully... This may take a moment.');
460
+
461
+ if (this.#network !== null) {
462
+ this.#network.stopPool();
463
+ }
464
+ await sleep(100);
465
+
466
+ if (this.#enableValidatorObserver) {
467
+ this.stopValidatorObserver();
468
+ }
469
+
470
+ await sleep(5_000); // stopValidatorObserver is using findValidator which is reading from the base, so we need to wait fot it to finish.. Temporary workaround?
471
+ if (this.#swarm !== null) {
460
472
  await this.#swarm.destroy();
461
473
  }
462
- await this.#base.close();
474
+ await sleep(100);
475
+
476
+ if (this.#base !== null) {
477
+ await this.#base.close();
478
+ }
479
+ await sleep(100);
480
+ if (this.#bee !== null) {
481
+ await this.#bee.close();
482
+ }
483
+ await sleep(100);
484
+ if (this.#readline_instance) {
485
+
486
+ const inputClosed = new Promise(resolve => this.#readline_instance.input.once('close', resolve));
487
+ const outputClosed = new Promise(resolve => this.#readline_instance.output.once('close', resolve));
488
+
489
+ this.#readline_instance.close();
490
+ this.#readline_instance.input.destroy();
491
+ this.#readline_instance.output.destroy();
492
+
493
+ // Do not remove this. Without it, readline may close too quickly and still hang.
494
+ await Promise.all([inputClosed, outputClosed]).catch(e => console.log("Error during closing readline stream:", e));
495
+ }
496
+ await sleep(100);
497
+
498
+ if (this.#store !== null) {
499
+ await this.#store.close();
500
+ }
501
+ await sleep(100);
463
502
  }
464
503
 
465
504
  async #setUpRoleAutomatically() {
466
- if (!this.#base.writable) {
505
+ if (!this.#base.writable && this.#enableRoleRequester) {
506
+ console.log('Requesting writer role... This may take a moment.');
467
507
  await this.#requestWriterRole(false)
468
508
  setTimeout(async () => {
469
509
  await this.#requestWriterRole(true)
470
510
  }, 5_000);
511
+ await sleep(5_000);
471
512
  }
472
513
  }
473
514
 
@@ -835,9 +876,11 @@ export class MainSettlementBus extends ReadyResource {
835
876
  }
836
877
  }
837
878
 
879
+ // TODO: AFTER WHILE LOOP SIGNAL TO THE PROCESS THAT VALIDATOR OBSERVER STOPPED OPERATING.
880
+ // OS CALLS, ACCUMULATORS, MAYBE THIS IS POSSIBLE TO CHECK I/O QUEUE IF IT COINTAIN IT. FOR NOW WE ARE USING SLEEP.
838
881
  async validatorObserver() {
839
882
  // Finding writers for admin recovery case
840
- while (this.#enable_wallet) {
883
+ while (this.#enableValidatorObserver && this.#enable_wallet) {
841
884
 
842
885
  if (this.#dht_node === null || this.#network.validator_stream !== null) {
843
886
  await sleep(1000);
@@ -911,6 +954,10 @@ export class MainSettlementBus extends ReadyResource {
911
954
  await this.#banValidator(tracPublicKey);
912
955
  }
913
956
 
957
+ stopValidatorObserver() {
958
+ this.#enableValidatorObserver = false;
959
+ }
960
+
914
961
  printHelp() {
915
962
  console.log('Available commands:');
916
963
  console.log('- /add_writer: add yourself as validator to this MSB once whitelisted.');
@@ -938,10 +985,8 @@ export class MainSettlementBus extends ReadyResource {
938
985
  this.printHelp();
939
986
  break;
940
987
  case '/exit':
941
- console.log('Exiting...');
942
988
  rl.close();
943
989
  await this.close();
944
- typeof process !== "undefined" ? process.exit(0) : Pear.exit(0);
945
990
  break;
946
991
  case '/push_writer_add':
947
992
  await this.#requestWriterRole(true)
@@ -1001,5 +1046,4 @@ export class MainSettlementBus extends ReadyResource {
1001
1046
 
1002
1047
  }
1003
1048
 
1004
- function noop() { }
1005
1049
  export default MainSettlementBus;
package/src/network.js CHANGED
@@ -20,6 +20,7 @@ import c from 'compact-encoding'
20
20
  const wakeup = new w();
21
21
 
22
22
  class Network {
23
+ #shouldStopPool = false;
23
24
  constructor(base) {
24
25
  this.tx_pool = [];
25
26
  this.pool(base);
@@ -28,7 +29,7 @@ class Network {
28
29
  this.admin = null
29
30
  this.validator_stream = null
30
31
  this.validator = null;
31
- this.custom_stream = null;
32
+ this.custom_stream = null;
32
33
  this.custom_node = null
33
34
  }
34
35
 
@@ -48,7 +49,7 @@ class Network {
48
49
  let clean = Date.now();
49
50
  let conns = {};
50
51
 
51
- swarm = new Hyperswarm({ keyPair, bootstrap : bootstrap, maxPeers: MAX_PEERS, maxParallel: MAX_PARALLEL, maxServerConnections: MAX_SERVER_CONNECTIONS, maxClientConnections : MAX_CLIENT_CONNECTIONS});
52
+ swarm = new Hyperswarm({ keyPair, bootstrap: bootstrap, maxPeers: MAX_PEERS, maxParallel: MAX_PARALLEL, maxServerConnections: MAX_SERVER_CONNECTIONS, maxClientConnections: MAX_CLIENT_CONNECTIONS });
52
53
 
53
54
  console.log(`Channel: ${b4a.toString(channel)}`);
54
55
  swarm.on('connection', async (connection) => {
@@ -68,7 +69,7 @@ class Network {
68
69
  encoding: c.json,
69
70
  async onmessage(msg) {
70
71
  try {
71
-
72
+
72
73
  if (msg === 'get_validator') {
73
74
  const nonce = Wallet.generateNonce().toString('hex');
74
75
  const _msg = {
@@ -78,7 +79,7 @@ class Network {
78
79
  channel: b4a.toString(channel, 'utf8')
79
80
  };
80
81
  const sig = wallet.sign(JSON.stringify(_msg) + nonce);
81
- message.send({response: _msg, sig, nonce})
82
+ message.send({ response: _msg, sig, nonce })
82
83
  swarm.leavePeer(connection.remotePublicKey)
83
84
  } else if (msg === 'get_admin') {
84
85
  const res = await msb.get(EntryType.ADMIN);
@@ -91,10 +92,10 @@ class Network {
91
92
  channel: b4a.toString(channel, 'utf8')
92
93
  };
93
94
  const sig = wallet.sign(JSON.stringify(_msg) + nonce);
94
- message.send({response: _msg, sig, nonce})
95
+ message.send({ response: _msg, sig, nonce })
95
96
  swarm.leavePeer(connection.remotePublicKey)
96
- } else if (msg === 'get_node') {
97
-
97
+ } else if (msg === 'get_node') {
98
+
98
99
  const nonce = Wallet.generateNonce().toString('hex');
99
100
  const _msg = {
100
101
  op: 'node',
@@ -103,9 +104,9 @@ class Network {
103
104
  channel: b4a.toString(channel, 'utf8')
104
105
  };
105
106
  const sig = wallet.sign(JSON.stringify(_msg) + nonce);
106
- message.send({response: _msg, sig, nonce})
107
+ message.send({ response: _msg, sig, nonce })
107
108
  swarm.leavePeer(connection.remotePublicKey)
108
-
109
+
109
110
  } else if (msg.response !== undefined && msg.response.op !== undefined && msg.response.op === 'validator') {
110
111
  const res = await msb.get(msg.response.address);
111
112
  if (res === null) return;
@@ -127,7 +128,7 @@ class Network {
127
128
  }
128
129
  swarm.leavePeer(connection.remotePublicKey)
129
130
  }
130
- else if (msg.response !== undefined && msg.response.op !== undefined && msg.response.op === 'node'){
131
+ else if (msg.response !== undefined && msg.response.op !== undefined && msg.response.op === 'node') {
131
132
 
132
133
  const verified = wallet.verify(msg.sig, JSON.stringify(msg.response) + msg.nonce, msg.response.address)
133
134
  if (verified && msg.response.channel === b4a.toString(channel, 'utf8')) {
@@ -180,7 +181,7 @@ class Network {
180
181
  }
181
182
 
182
183
  if (conns[peer] === undefined) {
183
- conns[peer] = {prev: _now, now: 0, tx_cnt: 0}
184
+ conns[peer] = { prev: _now, now: 0, tx_cnt: 0 }
184
185
  }
185
186
 
186
187
  conns[peer].now = _now;
@@ -227,7 +228,7 @@ class Network {
227
228
  wp: wallet.publicKey,
228
229
  wn: nonce
229
230
  };
230
- network.tx_pool.push({tx: parsedPreTx.tx, append_tx: append_tx});
231
+ network.tx_pool.push({ tx: parsedPreTx.tx, append_tx: append_tx });
231
232
  }
232
233
 
233
234
  swarm.leavePeer(connection.remotePublicKey)
@@ -260,6 +261,7 @@ class Network {
260
261
 
261
262
  // must be called AFTER the protomux init above
262
263
  const stream = store.replicate(connection);
264
+ stream.on('error', (error) => { });
263
265
  wakeup.addStream(stream);
264
266
 
265
267
  connection.on('error', (error) => { });
@@ -276,7 +278,7 @@ class Network {
276
278
  }
277
279
 
278
280
  async pool(base) {
279
- while (true) {
281
+ while (!this.#shouldStopPool) {
280
282
  if (this.tx_pool.length > 0) {
281
283
  const length = this.tx_pool.length;
282
284
  const batch = [];
@@ -290,6 +292,9 @@ class Network {
290
292
  await sleep(5);
291
293
  }
292
294
  }
293
- }
294
295
 
296
+ stopPool() {
297
+ this.#shouldStopPool = true;
298
+ }
299
+ }
295
300
  export default Network;
@@ -0,0 +1,4 @@
1
+ device/platform=darwin
2
+ device/inode=9178292
3
+ device/created=1748134175967
4
+ device/attribute=original
Binary file
@@ -0,0 +1 @@
1
+ MANIFEST-000005
@@ -0,0 +1 @@
1
+ 77f84488-ec5a-4c9f-84c5-932fa7f67b28
File without changes