trac-peer 0.1.50 → 0.1.51

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +85 -45
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-peer",
3
3
  "main": "src/index.js",
4
- "version": "0.1.50",
4
+ "version": "0.1.51",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "hypercore": "11.6.2",
package/src/index.js CHANGED
@@ -17,7 +17,6 @@ import {
17
17
  pinMessage
18
18
  } from "./functions.js";
19
19
  import Check from "./check.js";
20
- import DHT from 'hyperdht';
21
20
  export {default as Protocol} from "./protocol.js";
22
21
  export {default as Contract} from "./contract.js";
23
22
  export {default as Feature} from "./feature.js";
@@ -55,7 +54,6 @@ export class Peer extends ReadyResource {
55
54
  this.options = options;
56
55
  this.check = new Check();
57
56
  this.dhtBootstrap = ['116.202.214.143:10001','116.202.214.149:10001', 'node1.hyperdht.org:49737', 'node2.hyperdht.org:49737', 'node3.hyperdht.org:49737'];
58
- this.dhtNode = null;
59
57
  this.seen_auto_add = {};
60
58
  this.validator = null;
61
59
  this.validator_stream = null;
@@ -495,32 +493,63 @@ export class Peer extends ReadyResource {
495
493
  try{ await this.validator_stream.send(b4a.from(jsonStringify(_msg))); } catch(e){ }
496
494
  }
497
495
 
498
- async getValidatorWriterKey(address){
499
- if(null === this.dhtNode) return null;
496
+ async isValidatorAvailable(address){
497
+ if(null === this.msb.getSwarm()) return null;
500
498
  let writer_key = null;
501
- const stream = this.dhtNode.connect(b4a.from(address, 'hex'))
502
- stream.on('connect', async function () {
503
- await stream.send(b4a.from(jsonStringify('get_writer_key')));
504
- });
505
- stream.on('message', (msg) => {
506
- try{
507
- const response = jsonParse(b4a.toString(msg, 'utf-8'));
508
- if(response.op === 'writer_key' && response.key !== undefined){
509
- writer_key = response.key;
510
- }
511
- }catch(e){}
512
- });
513
- stream.on('open', function () { });
514
- stream.on('close', () => { });
515
- stream.on('error', (error) => { });
516
- let i = 0;
517
- while(null === writer_key){
518
- if(i >= 300) break;
519
- await this.sleep(5);
520
- i += 5;
499
+
500
+ this.msb.getSwarm().joinPeer(b4a.from(address, 'hex'))
501
+
502
+ let existing_stream = undefined;
503
+
504
+ if(this.msb.getSwarm().peers.has(address)){
505
+ const peerInfo = this.msb.getSwarm().peers.get(address)
506
+ existing_stream = this.msb.getSwarm()._allConnections.get(peerInfo.publicKey)
507
+ }
508
+
509
+ if(existing_stream !== undefined){
510
+ existing_stream.on('message', (msg) => {
511
+ try{
512
+ const response = jsonParse(b4a.toString(msg, 'utf-8'));
513
+ if(response.op === 'writer_key' && response.key !== undefined){
514
+ writer_key = response.key;
515
+ }
516
+ }catch(e){}
517
+ });
518
+ existing_stream.on('open', function () { });
519
+ existing_stream.on('close', () => { });
520
+ existing_stream.on('error', (error) => { });
521
+ await existing_stream.send(b4a.from(jsonStringify('get_writer_key')));
522
+ let i = 0;
523
+ while(null === writer_key){
524
+ if(i >= 1_000) break;
525
+ await this.sleep(10);
526
+ i += 10;
527
+ }
528
+ return writer_key;
529
+ } else {
530
+ const stream = this.msb.getSwarm().dht.connect(b4a.from(address, 'hex'))
531
+ stream.on('connect', async function () {
532
+ await stream.send(b4a.from(jsonStringify('get_writer_key')));
533
+ });
534
+ stream.on('message', (msg) => {
535
+ try{
536
+ const response = jsonParse(b4a.toString(msg, 'utf-8'));
537
+ if(response.op === 'writer_key' && response.key !== undefined){
538
+ writer_key = response.key;
539
+ }
540
+ }catch(e){}
541
+ });
542
+ stream.on('open', function () { });
543
+ stream.on('close', () => { });
544
+ stream.on('error', (error) => { });
545
+ let i = 0;
546
+ while(null === writer_key){
547
+ if(i >= 5_000) break;
548
+ await this.sleep(10);
549
+ i += 10;
550
+ }
551
+ return writer_key;
521
552
  }
522
- await stream.end();
523
- return writer_key;
524
553
  }
525
554
 
526
555
  async _open() {
@@ -576,7 +605,7 @@ export class Peer extends ReadyResource {
576
605
 
577
606
  async validator_observer(){
578
607
  while(true){
579
- if(this.dhtNode !== null && this.validator_stream === null) {
608
+ if(this.msb.getSwarm() !== null && this.validator_stream === null) {
580
609
  console.log('Looking for available validators, please wait...');
581
610
  const _this = this;
582
611
  let length = await this.msb.base.view.get('wrl');
@@ -594,29 +623,41 @@ export class Peer extends ReadyResource {
594
623
  validator = await _this.msb.base.view.get(validator.value);
595
624
  if(_this.validator_stream !== null) return;
596
625
  if(null !== validator && false !== validator.value.isWriter && false === validator.value.isIndexer) {
597
- const result = await _this.getValidatorWriterKey(validator.value.pub);
626
+ const result = await _this.isValidatorAvailable(validator.value.pub);
598
627
  if(_this.validator_stream !== null) return;
599
628
  if (null !== result) {
600
- _this.validator = validator.value.pub;
601
629
  await _this.sleep(100);
602
630
  if(_this.validator_stream !== null) return;
603
- _this.validator_stream = _this.dhtNode.connect(b4a.from(validator.value.pub, 'hex'));
604
- _this.validator_stream.on('open', function () {
631
+ let existing_stream = undefined;
632
+ if(_this.msb.getSwarm().peers.has(validator.value.pub)){
633
+ const peerInfo = _this.msb.getSwarm().peers.get(validator.value.pub)
634
+ existing_stream = _this.msb.getSwarm()._allConnections.get(peerInfo.publicKey)
635
+ }
636
+ if(existing_stream !== undefined){
637
+ _this.validator_stream = existing_stream;
605
638
  _this.validator = validator.value.pub;
639
+ _this.validator_stream.on('close', () => {
640
+ _this.validator_stream = null;
641
+ _this.validator = null;
642
+ console.log('Validator stream closed', validator.value.pub);
643
+ });
606
644
  console.log('Validator stream established', validator.value.pub);
607
- });
608
- _this.validator_stream.on('close', () => {
609
- try{ _this.validator_stream.destroy() } catch(e) {}
610
- _this.validator_stream = null;
611
- _this.validator = null;
612
- console.log('Stream closed', validator.value.pub)
613
- });
614
- _this.validator_stream.on('error', (error) => {
615
- try{ _this.validator_stream.destroy() } catch(e) {}
616
- _this.validator_stream = null;
617
- _this.validator = null;
618
- console.log(error)
619
- });
645
+ } else {
646
+ _this.validator_stream = _this.msb.getSwarm().dht.connect(b4a.from(validator.value.pub, 'hex'));
647
+ _this.validator_stream.on('open', function () {
648
+ _this.validator = validator.value.pub;
649
+ console.log('Validator stream established', validator.value.pub);
650
+ });
651
+ _this.validator_stream.on('close', () => {
652
+ _this.validator_stream = null;
653
+ _this.validator = null;
654
+ console.log('Validator stream closed', validator.value.pub);
655
+ });
656
+ _this.validator_stream.on('error', (error) => {
657
+ _this.validator_stream = null;
658
+ _this.validator = null;
659
+ });
660
+ }
620
661
  }
621
662
  }
622
663
  }
@@ -701,7 +742,6 @@ export class Peer extends ReadyResource {
701
742
  };
702
743
 
703
744
  this.swarm = new Hyperswarm({ keyPair, bootstrap: this.dhtBootstrap });
704
- this.dhtNode = this.swarm.dht;
705
745
 
706
746
  console.log(`Writer key: ${this.writerLocalKey}`)
707
747