trac-peer 0.0.97 → 0.0.99

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,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-peer",
3
3
  "main": "src/index.js",
4
- "version": "0.0.97",
4
+ "version": "0.0.99",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "trac-wallet": "^0.0.34",
@@ -13,7 +13,7 @@
13
13
  "debounceify": "1.1.0",
14
14
  "fastest-validator": "^1.19.0",
15
15
  "hyperbee": "2.24.1",
16
- "hypercore": "11.1.1",
16
+ "hypercore": "11.1.2",
17
17
  "hypercore-crypto": "3.4.0",
18
18
  "hyperdht": "6.20.1",
19
19
  "hyperswarm": "4.11.1",
package/src/check.js CHANGED
@@ -379,7 +379,15 @@ class Check {
379
379
  value : {
380
380
  $$type: "object",
381
381
  tx : { type : "is_hex" },
382
- ch : { type : "is_hex" }
382
+ ch : { type : "is_hex" },
383
+ ws : { type : "is_hex" },
384
+ wn : { type : "string", min : 1 },
385
+ wp : { type : "is_hex" },
386
+ is : { type : "is_hex" },
387
+ in : { type : "string", min : 1 },
388
+ ipk : { type : "is_hex" },
389
+ w : { type : "is_hex" },
390
+ i : { type : "is_hex" }
383
391
  }
384
392
  };
385
393
  return this.validator.compile(schema);
@@ -416,7 +424,10 @@ class Check {
416
424
  $$type: "object",
417
425
  dispatch : { type : "object" },
418
426
  msbsl : { type : "number", integer : true, min : 0 },
419
- ipk : { type : "is_hex" }
427
+ ipk : { type : "is_hex" },
428
+ wp : { type : "is_hex" },
429
+ hash : { type : "is_hex" },
430
+ nonce: { type : "string", min : 1 }
420
431
  }
421
432
  };
422
433
  return this.validator.compile(schema);
package/src/index.js CHANGED
@@ -33,7 +33,7 @@ export class Peer extends ReadyResource {
33
33
  this.key = null;
34
34
  this.tx_pool = {};
35
35
  this.writerLocalKey = null;
36
- this.tx_pool_max_size = options.tx_pool_max_size || 1_000;
36
+ this.tx_pool_max_size = options.tx_pool_max_size || 3_000;
37
37
  this.max_tx_delay = options.max_tx_delay || 60;
38
38
  this.bootstrap = options.bootstrap || null;
39
39
  this.protocol = options.protocol || null;
@@ -81,18 +81,25 @@ export class Peer extends ReadyResource {
81
81
  const op = node.value;
82
82
  if (op.type === 'tx') {
83
83
  if(false === this.check.tx(op)) continue;
84
+ while (_this.msb.base.view.core.signedLength < op.value.msbsl) {
85
+ await new Promise( (resolve, reject) => {
86
+ _this.msb.base.view.core.once('append', resolve);
87
+ });
88
+ }
84
89
  const msb_view_session = _this.msb.base.view.checkout(op.value.msbsl);
85
90
  const post_tx = await msb_view_session.get(op.key);
86
91
  await msb_view_session.close();
87
92
  if(false === this.check.postTx(post_tx)) continue;
88
- if (null === await batch.get('tx/'+op.key) &&
89
- op.key === post_tx.value.tx &&
90
- _this.wallet.verify(post_tx.value.ws, post_tx.value.tx + post_tx.value.wn, post_tx.value.wp) &&
91
- _this.wallet.verify(post_tx.value.is, post_tx.value.tx + post_tx.value.in, post_tx.value.ipk) &&
93
+ const str_dispatch_value = jsonStringify(op.value.dispatch);
94
+ if (op.key === post_tx.value.tx &&
95
+ null === await batch.get('tx/'+post_tx.value.tx) &&
96
+ _this.wallet.verify(post_tx.value.ws, post_tx.value.tx + post_tx.value.wn, op.value.wp) &&
97
+ _this.wallet.verify(post_tx.value.is, post_tx.value.tx + post_tx.value.in, op.value.ipk) &&
98
+ _this.wallet.verify(op.value.hash, post_tx.value.tx + str_dispatch_value + op.value.nonce, post_tx.value.ipk) &&
92
99
  post_tx.value.tx === await _this.protocol_instance.generateTx(
93
100
  _this.bootstrap, _this.msb.bootstrap,
94
- post_tx.value.w, post_tx.value.i, op.value.ipk,
95
- op.value.dispatch, post_tx.value.in
101
+ post_tx.value.w, post_tx.value.i, post_tx.value.ipk,
102
+ str_dispatch_value, post_tx.value.in
96
103
  ) &&
97
104
  false !== await _this.contract_instance.execute(op, node, batch)) {
98
105
  let len = await batch.get('txl');
@@ -101,10 +108,10 @@ export class Peer extends ReadyResource {
101
108
  } else {
102
109
  len = len.value;
103
110
  }
104
- await batch.put('txi/'+len, op.key);
111
+ await batch.put('txi/'+len, post_tx.value.tx);
105
112
  await batch.put('txl', len + 1);
106
- await batch.put('tx/'+op.key, op.value);
107
- console.log(`${op.key} appended. Signed length:`, _this.base.view.core.signedLength);
113
+ await batch.put('tx/'+post_tx.value.tx, op.value);
114
+ console.log(`${post_tx.value.tx} appended. Signed length:`, _this.base.view.core.signedLength, 'tx length', len + 1);
108
115
  }
109
116
  } else if(op.type === 'msg') {
110
117
  if(false === this.check.msg(op)) continue;
@@ -497,16 +504,20 @@ export class Peer extends ReadyResource {
497
504
  const msb_tx = await view_session.get(tx);
498
505
  await view_session.close();
499
506
  if(null !== msb_tx){
507
+ console.log('invalid msb block #1', msbsl, ' >= ', this.msb.base.view.core.signedLength)
500
508
  msb_tx['dispatch'] = this.protocol_instance.prepared_transactions_content[tx];
501
509
  msb_tx['msbsl'] = msbsl;
502
510
  msb_tx['ipk'] = this.wallet.publicKey;
511
+ msb_tx['wp'] = msb_tx.value.wp !== undefined ? msb_tx.value.wp : null;
512
+ msb_tx['nonce'] = Math.random() + '-' + Date.now();
513
+ msb_tx['hash'] = peer.wallet.sign(tx + jsonStringify(msb_tx['dispatch']) + msb_tx['nonce']);
503
514
  delete this.tx_pool[tx];
504
515
  delete this.protocol_instance.prepared_transactions_content[tx];
505
516
  await this.base.append({ type: 'tx', key: tx, value: msb_tx });
506
517
  }
507
- await this.sleep(5);
518
+ await this.sleep(2);
508
519
  }
509
- await this.sleep(10);
520
+ await this.sleep(2);
510
521
  }
511
522
  }
512
523
 
package/src/protocol.js CHANGED
@@ -70,8 +70,7 @@ class Protocol{
70
70
  this.features[key] = feature;
71
71
  }
72
72
 
73
- async generateTx(bootstrap, msb_bootstrap, validator_writer_key, local_writer_key, local_public_key, content, nonce){
74
- const content_hash = await this.peer.createHash('sha256', jsonStringify(content));
73
+ async generateTx(bootstrap, msb_bootstrap, validator_writer_key, local_writer_key, local_public_key, content_hash, nonce){
75
74
  let tx = bootstrap + '-' +
76
75
  msb_bootstrap + '-' +
77
76
  validator_writer_key + '-' +