trac-msb 0.1.19 → 0.1.20

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,2 +1 @@
1
- e914ba0570858924b6cf7bfc1852882cf1fc520f410ea84dc0c0248ba2ca725f
2
- 01a6fc0108514ef6c8ff7b23e1eb25235724934e7e31bea391903baf15b742b9
1
+ 2ca71cadf349f877d2239633108afe7b5a8d98ac4796b093bb813323b1d18af9
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.1.19",
4
+ "version": "0.1.20",
5
5
  "pear": {
6
6
  "name": "trac-msb",
7
7
  "type": "terminal"
package/src/index.js CHANGED
@@ -29,7 +29,6 @@ export class MainSettlementBus extends ReadyResource {
29
29
  // Internal flags
30
30
  #shouldListenToAdminEvents = false;
31
31
  #shouldListenToWriterEvents = false;
32
- #shouldValidatorObserverWorks = true;
33
32
  #isStreaming = false;
34
33
 
35
34
  // internal attributes
@@ -78,7 +77,7 @@ export class MainSettlementBus extends ReadyResource {
78
77
  this.#dht_bootstrap = ['116.202.214.143:10001', '116.202.214.149:10001', 'node1.hyperdht.org:49737', 'node2.hyperdht.org:49737', 'node3.hyperdht.org:49737'];
79
78
  this.#dht_node = null;
80
79
  this.#validator_stream = null
81
- this.validator = null;
80
+ this.#validator = null;
82
81
  this.#base = null;
83
82
  this.#writingKey = null;
84
83
  this.#enable_txchannel = options.enable_txchannel !== false;
@@ -402,7 +401,7 @@ export class MainSettlementBus extends ReadyResource {
402
401
  await this.#removeWriter(op, batch, base);
403
402
 
404
403
  }
405
-
404
+
406
405
  async _open() {
407
406
  await this.#base.ready();
408
407
  if (this.#enable_wallet) {
@@ -467,19 +466,51 @@ export class MainSettlementBus extends ReadyResource {
467
466
  }
468
467
 
469
468
  #sendMessageToAdmin(adminEntry, message) {
469
+ try{
470
+ if (!adminEntry || !message) {
471
+ return;
472
+ }
473
+ let stream;
474
+
475
+ if(this.#validator_stream !== null){
476
+ stream = this.#validator_stream;
477
+ } else {
478
+ stream = this.#dht_node.connect(b4a.from(adminEntry.tracPublicKey, 'hex'))
479
+ }
480
+
481
+ stream.on('connect', async function () {
482
+ console.log('Trying to send message to admin.');
483
+ await stream.send(b4a.from(JSON.stringify(message)));
484
+ await stream.end();
485
+ });
486
+ stream.on('open', function () { console.log('Message channel opened') });
487
+ stream.on('close', () => { console.log('Message channel closed') });
488
+ stream.on('error', (error) => { console.log('Message send error', error) });
489
+ }catch(e){
490
+ console.log(e)
491
+ }
492
+ }
493
+
494
+ async #sendMessageToAdmin2(adminEntry, message) {
470
495
  if (!adminEntry || !message) {
471
496
  return;
472
497
  }
473
498
 
474
- const stream = this.#dht_node.connect(b4a.from(adminEntry.tracPublicKey, 'hex'))
499
+ this.#swarm.joinPeer(b4a.from(adminEntry.tracPublicKey, 'hex'))
475
500
 
476
- stream.on('connect', async function () {
477
- console.log('Trying to send message to admin.');
478
- await stream.send(b4a.from(JSON.stringify(message)));
479
- });
480
- stream.on('open', function () { console.log('Message channel opened') });
481
- stream.on('close', () => { console.log('Message channel closed') });
482
- stream.on('error', (error) => { console.log('Message send error', error) });
501
+ let stream = undefined;
502
+
503
+ stream = this.#dht_node.connect(b4a.from(adminEntry.tracPublicKey, 'hex'))
504
+
505
+ if(stream !== undefined && stream.op !== undefined){
506
+ stream.on('connect', async function () {
507
+ console.log('Trying to send message to admin.');
508
+ await stream.send(b4a.from(JSON.stringify(message)));
509
+ });
510
+ stream.on('open', function () { console.log('Message channel opened') });
511
+ stream.on('close', () => { console.log('Message channel closed') });
512
+ stream.on('error', (error) => { console.log('Message send error', error) });
513
+ }
483
514
  }
484
515
 
485
516
  async #verifyMessage(signature, publicKey, bufferMessage) {
@@ -646,7 +677,6 @@ export class MainSettlementBus extends ReadyResource {
646
677
  if (!adminEntry && this.#wallet && this.#writingKey && this.#writingKey === this.#bootstrap) {
647
678
  await this.#base.append(addAdminMessage);
648
679
  } else if (adminEntry && this.#wallet && adminEntry.tracPublicKey === this.#wallet.publicKey && this.#writingKey && this.#writingKey !== adminEntry.wk) {
649
-
650
680
  if (null === this.#validator_stream) return;
651
681
  await this.#validator_stream.send(b4a.from(JSON.stringify(addAdminMessage)));
652
682
  }
@@ -716,7 +746,7 @@ export class MainSettlementBus extends ReadyResource {
716
746
  }
717
747
 
718
748
  if (assembledMessage) {
719
- this.#sendMessageToAdmin(adminEntry, assembledMessage);
749
+ await this.#sendMessageToAdmin(adminEntry, assembledMessage);
720
750
  }
721
751
  }
722
752
 
@@ -749,11 +779,70 @@ export class MainSettlementBus extends ReadyResource {
749
779
  }
750
780
  }
751
781
 
782
+ async isValidatorAvailable(address){
783
+ if(null === this.#swarm) return null;
784
+ let writer_key = null;
785
+
786
+ this.#swarm.joinPeer(b4a.from(address, 'hex'))
787
+
788
+ let existing_stream = undefined;
789
+
790
+ if(this.#swarm.peers.has(address)){
791
+ const peerInfo = this.#swarm.peers.get(address)
792
+ existing_stream = this.#swarm._allConnections.get(peerInfo.publicKey)
793
+ }
794
+
795
+ if(existing_stream !== undefined){
796
+ existing_stream.on('message', (msg) => {
797
+ try{
798
+ const response = JSON.parse(b4a.toString(msg, 'utf-8'));
799
+ if(response.op === 'writer_key' && response.key !== undefined){
800
+ writer_key = response.key;
801
+ }
802
+ }catch(e){}
803
+ });
804
+ existing_stream.on('open', function () { });
805
+ existing_stream.on('close', () => { });
806
+ existing_stream.on('error', (error) => { });
807
+ await existing_stream.send(b4a.from(JSON.stringify('get_writer_key')));
808
+ let i = 0;
809
+ while(null === writer_key){
810
+ if(i >= 1_000) break;
811
+ await sleep(10);
812
+ i += 10;
813
+ }
814
+ return writer_key;
815
+ } else {
816
+ const stream = this.#swarm.dht.connect(b4a.from(address, 'hex'))
817
+ stream.on('connect', async function () {
818
+ await stream.send(b4a.from(JSON.stringify('get_writer_key')));
819
+ });
820
+ stream.on('message', (msg) => {
821
+ try{
822
+ const response = jsonParse(b4a.toString(msg, 'utf-8'));
823
+ if(response.op === 'writer_key' && response.key !== undefined){
824
+ writer_key = response.key;
825
+ }
826
+ }catch(e){}
827
+ });
828
+ stream.on('open', function () { });
829
+ stream.on('close', () => { });
830
+ stream.on('error', (error) => { });
831
+ let i = 0;
832
+ while(null === writer_key){
833
+ if(i >= 5_000) break;
834
+ await sleep(10);
835
+ i += 10;
836
+ }
837
+ return writer_key;
838
+ }
839
+ }
840
+
752
841
  async validatorObserver() {
753
842
  // Finding writers for admin recovery case
754
- while (this.#shouldValidatorObserverWorks) {
843
+ while (this.#enable_wallet) {
755
844
 
756
- if (this.#dht_node === null || this.#validator_stream !== null || this.#base.writable) {
845
+ if (this.#dht_node === null || this.#validator_stream !== null || this.#validator !== null) {
757
846
  await sleep(1000);
758
847
  continue;
759
848
  }
@@ -767,41 +856,59 @@ export class MainSettlementBus extends ReadyResource {
767
856
  const wriEntry = await _this.#base.view.get('wri/' + rndIndex);
768
857
  if (_this.#validator_stream !== null || wriEntry === null) return;
769
858
 
770
-
771
859
  const validatorEntry = await _this.#base.view.get(wriEntry.value);
772
860
  if (
773
861
  _this.#validator_stream !== null ||
862
+ _this.#validator !== null ||
774
863
  validatorEntry === null ||
775
864
  !validatorEntry.value.isWriter ||
776
865
  validatorEntry.value.isIndexer
777
866
  ) return;
778
867
 
779
868
  const pubKey = validatorEntry.value.pub;
780
- if (pubKey === _this.#wallet.publicKey) return; // avoid establishing connection to itself
781
- _this.#validator = pubKey;
782
- if (_this.#validator_stream !== null) return;
869
+ if (pubKey === _this.#wallet.publicKey) return;
783
870
 
784
- const stream = _this.#dht_node.connect(b4a.from(pubKey, 'hex'));
785
- _this.#validator_stream = stream;
871
+ const available = await _this.isValidatorAvailable(pubKey);
872
+ if (_this.#validator_stream !== null) return;
873
+ if(available === null) return;
786
874
 
787
- _this.#validator_stream.on('open', () => {
788
- _this.#validator = pubKey;
789
- console.log('Validator stream established', pubKey);
790
- });
875
+ let existing_stream = undefined;
791
876
 
792
- _this.#validator_stream.on('close', () => {
793
- try { stream.destroy(); } catch { }
794
- _this.#validator_stream = null;
795
- _this.#validator = null;
796
- console.log('Stream closed', pubKey);
797
- });
877
+ if(_this.#swarm.peers.has(pubKey)){
878
+ const peerInfo = _this.#swarm.peers.get(pubKey)
879
+ existing_stream = _this.#swarm._allConnections.get(peerInfo.publicKey)
880
+ }
881
+ if (_this.#validator_stream !== null) return;
798
882
 
799
- _this.#validator_stream.on('error', (err) => {
800
- try { stream.destroy(); } catch { }
801
- _this.#validator_stream = null;
802
- _this.#validator = null;
803
- console.log(err);
804
- });
883
+ if(existing_stream !== undefined){
884
+ _this.#validator_stream = existing_stream;
885
+ _this.#validator = pubKey;
886
+ _this.#validator_stream.on('close', () => {
887
+ if(_this.#validator_stream !== null && b4a.toString(_this.#validator_stream.publicKey, 'hex') === pubKey){
888
+ _this.#validator_stream = null;
889
+ _this.#validator = null;
890
+ console.log('Exissting Stream closed', pubKey);
891
+ }
892
+ });
893
+ console.log('Existing Stream established', pubKey);
894
+ } else {
895
+ _this.#validator_stream = _this.#dht_node.connect(b4a.from(pubKey, 'hex'));
896
+ _this.#validator = pubKey;
897
+ _this.#validator_stream.on('close', () => {
898
+ if(_this.#validator_stream !== null && b4a.toString(_this.#validator_stream.publicKey, 'hex') === pubKey){
899
+ _this.#validator_stream = null;
900
+ _this.#validator = null;
901
+ console.log('Stream closed', pubKey);
902
+ }
903
+ });
904
+
905
+ _this.#validator_stream.on('error', (err) => {
906
+ if(_this.#validator_stream !== null && b4a.toString(_this.#validator_stream.publicKey, 'hex') === pubKey) {
907
+ _this.#validator_stream = null;
908
+ _this.#validator = null;
909
+ }
910
+ });
911
+ }
805
912
  }
806
913
 
807
914
  const promises = [];
package/src/network.js CHANGED
@@ -44,7 +44,7 @@ class Network {
44
44
  swarm.on('connection', async (connection) => {
45
45
  wakeup.addStream(connection);
46
46
  store.replicate(connection);
47
- connection.on('close', () => { });
47
+ connection.on('close', () => {});
48
48
  connection.on('error', (error) => { });
49
49
 
50
50
  if(enable_txchannel){
@@ -84,7 +84,6 @@ class Network {
84
84
 
85
85
  }
86
86
  else {
87
- //await connection.destroy();
88
87
  if (base.isIndexer || !base.writable) return;
89
88
 
90
89
  if (network.tx_pool.length >= 1000) {