@leofcoin/chain 1.5.43 → 1.5.45

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.
@@ -3688,7 +3688,8 @@ class Transaction extends Protocol {
3688
3688
  await globalThis.transactionPoolStore.put(hash, message.encoded);
3689
3689
  // debug(`Added ${hash} to the transaction pool`)
3690
3690
  peernet.publish('add-transaction', message.encoded);
3691
- return { hash, data, fee: await calculateFee(message.decoded), wait, message };
3691
+ const fee = await calculateFee(message.decoded);
3692
+ return { hash, data, fee, wait, message };
3692
3693
  }
3693
3694
  catch (error) {
3694
3695
  console.log('remo');
@@ -3893,8 +3894,11 @@ class Machine {
3893
3894
  break;
3894
3895
  }
3895
3896
  case 'debug': {
3896
- for (const message of data.messages)
3897
- globalThis.debug(message);
3897
+ globalThis.debug(data.message);
3898
+ break;
3899
+ }
3900
+ case 'error': {
3901
+ console.error(data.message);
3898
3902
  break;
3899
3903
  }
3900
3904
  case 'machine-ready': {
@@ -4315,7 +4319,7 @@ class State extends Contract {
4315
4319
  try {
4316
4320
  await this.resolveBlocks();
4317
4321
  this.#machine = await new Machine(this.#blocks);
4318
- await this.#loadBlocks(this.#blocks);
4322
+ // await this.#loadBlocks(this.#blocks)
4319
4323
  }
4320
4324
  catch (error) {
4321
4325
  if (isResolveError(error)) {
@@ -4754,6 +4758,13 @@ class Chain extends VersionControl {
4754
4758
  this.#participants = [];
4755
4759
  this.#participating = false;
4756
4760
  this.#jail = [];
4761
+ this.#addTransaction = async (message) => {
4762
+ const transaction = new TransactionMessage(message);
4763
+ const hash = await transaction.hash();
4764
+ if (await transactionPool.has(hash))
4765
+ await transactionPool.delete(hash);
4766
+ console.log(transaction);
4767
+ };
4757
4768
  // @ts-ignore
4758
4769
  return this.#init();
4759
4770
  }
@@ -4857,9 +4868,7 @@ class Chain extends VersionControl {
4857
4868
  }, validatorInfo.timeout);
4858
4869
  this.#jail.push(validatorInfo.address);
4859
4870
  }
4860
- #addTransaction(message) {
4861
- console.log({ message });
4862
- }
4871
+ #addTransaction;
4863
4872
  async #prepareRequest(request) {
4864
4873
  let node = await new globalThis.peernet.protos['peernet-request']({ request });
4865
4874
  return globalThis.peernet.prepareMessage(node);
@@ -4884,7 +4893,6 @@ class Chain extends VersionControl {
4884
4893
  }
4885
4894
  async #peerConnected(peerId) {
4886
4895
  const peer = peernet.getConnection(peerId);
4887
- console.log({ peer });
4888
4896
  // todo handle version changes
4889
4897
  // for now just do nothing if version doesn't match
4890
4898
  console.log(`${peer.version}, ${this.version}`);
@@ -1,4 +1,4 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-77f8d4c5.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-2c421007.js';
2
2
  import './index-c3bd3090.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './node-browser-77f8d4c5.js';
1
+ import { F as FormatInterface } from './node-browser-2c421007.js';
2
2
  import './index-c3bd3090.js';
3
3
 
4
4
  var proto$b = {
@@ -102,58 +102,62 @@ globalThis.debug = text => {
102
102
  if (globalThis.DEBUG) console.log('\x1b[34m\x1b[1m%s', text, '\x1b[0m'); // bright blue
103
103
  };
104
104
 
105
- class LittlePubSub {
106
- subscribers = {};
107
- verbose;
108
- constructor(verbose) {
109
- this.verbose = verbose;
110
- }
111
- _handleContext(handler, context) {
112
- if (typeof context === 'undefined') {
113
- context = handler;
114
- }
115
- return context;
116
- }
117
- hasSubscribers(event) {
118
- return this.subscribers[event] ? true : false;
119
- }
120
- subscribe(event, handler, context) {
121
- if (!this.hasSubscribers(event))
122
- this.subscribers[event] = { handlers: [], value: undefined };
123
- context = this._handleContext(handler, context);
124
- this.subscribers[event].handlers.push(handler.bind(context));
125
- }
126
- unsubscribe(event, handler, context) {
127
- if (!this.hasSubscribers(event))
128
- return;
129
- context = this._handleContext(handler, context);
130
- const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
131
- this.subscribers[event].handlers.splice(index);
132
- if (this.subscribers[event].handlers.length === 0)
133
- delete this.subscribers[event];
134
- }
135
- publish(event, value) {
136
- // always set value even when having no subscribers
137
- if (!this.hasSubscribers(event))
138
- this.subscribers[event] = {
139
- handlers: []
140
- };
141
- const oldValue = this.subscribers[event]?.value;
142
- this.subscribers[event].value = value;
143
- if (this.verbose || oldValue !== value)
144
- for (const handler of this.subscribers[event].handlers) {
145
- handler(value, oldValue);
146
- }
147
- }
148
- once(event) {
149
- return new Promise((resolve) => {
150
- const cb = (value) => {
151
- this.unsubscribe(event, cb);
152
- resolve(value);
153
- };
154
- this.subscribe(event, cb);
155
- });
156
- }
105
+ class LittlePubSub {
106
+ subscribers = {};
107
+ verbose;
108
+ constructor(verbose) {
109
+ this.verbose = verbose;
110
+ }
111
+ _handleContext(handler, context) {
112
+ if (typeof context === 'undefined') {
113
+ context = handler;
114
+ }
115
+ return context;
116
+ }
117
+ hasSubscribers(event) {
118
+ return this.subscribers[event] ? true : false;
119
+ }
120
+ subscribe(event, handler, context) {
121
+ if (!this.hasSubscribers(event))
122
+ this.subscribers[event] = { handlers: [], value: undefined };
123
+ context = this._handleContext(handler, context);
124
+ this.subscribers[event].handlers.push(handler.bind(context));
125
+ }
126
+ unsubscribe(event, handler, context) {
127
+ if (!this.hasSubscribers(event))
128
+ return;
129
+ context = this._handleContext(handler, context);
130
+ const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
131
+ this.subscribers[event].handlers.splice(index);
132
+ if (this.subscribers[event].handlers.length === 0)
133
+ delete this.subscribers[event];
134
+ }
135
+ publish(event, value, verbose) {
136
+ // always set value even when having no subscribers
137
+ if (!this.hasSubscribers(event))
138
+ this.subscribers[event] = {
139
+ handlers: []
140
+ };
141
+ const oldValue = this.subscribers[event]?.value;
142
+ if (this.verbose || verbose || oldValue !== value) {
143
+ this.subscribers[event].value = value;
144
+ for (const handler of this.subscribers[event].handlers) {
145
+ handler(value, oldValue);
146
+ }
147
+ }
148
+ }
149
+ publishVerbose(event, value) {
150
+ this.publish(event, value, true);
151
+ }
152
+ once(event) {
153
+ return new Promise((resolve) => {
154
+ const cb = (value) => {
155
+ resolve(value);
156
+ this.unsubscribe(event, cb);
157
+ };
158
+ this.subscribe(event, cb);
159
+ });
160
+ }
157
161
  }
158
162
 
159
163
  // base-x encoding / decoding
@@ -27178,7 +27182,7 @@ class Identity {
27178
27182
  this.selectedAccount = new TextDecoder().decode(selected);
27179
27183
  }
27180
27184
  else {
27181
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-2836cc96-40ed5df4.js');
27185
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-687f692b-bcdff490.js');
27182
27186
  const { identity, accounts } = await importee.default(password, this.network);
27183
27187
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
27184
27188
  await globalThis.walletStore.put('version', String(1));
@@ -27369,7 +27373,7 @@ class Peernet {
27369
27373
  this.root = options.root;
27370
27374
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
27371
27375
  // FolderMessageResponse
27372
- } = await import(/* webpackChunkName: "messages" */ './messages-554d2a85-ce06f6eb.js');
27376
+ } = await import(/* webpackChunkName: "messages" */ './messages-1c117282-a35a5fbc.js');
27373
27377
  /**
27374
27378
  * proto Object containing protos
27375
27379
  * @type {Object}
@@ -27579,14 +27583,14 @@ class Peernet {
27579
27583
  }
27580
27584
  globalThis.debug(`star ${id} left`);
27581
27585
  };
27582
- #peerLeft(peer, star) {
27586
+ #peerLeft = (peer, star) => {
27583
27587
  const id = peer.peerId || peer;
27584
27588
  if (this.#connections[id]) {
27585
27589
  this.#connections[id].destroy();
27586
27590
  delete this.#connections[id];
27587
27591
  }
27588
27592
  globalThis.debug(`peer ${id} left`);
27589
- }
27593
+ };
27590
27594
  #createRTCPeerConnection = (peerId, star, version, initiator = false) => {
27591
27595
  const peer = new Peer({
27592
27596
  initiator: initiator,
@@ -27604,8 +27608,7 @@ class Peernet {
27604
27608
  #peerJoined = async ({ peerId, version }, star) => {
27605
27609
  // check if peer rejoined before the previous connection closed
27606
27610
  if (this.#connections[peerId]) {
27607
- if (this.#connections[peerId].connected)
27608
- this.#connections[peerId].destroy();
27611
+ this.#connections[peerId].destroy();
27609
27612
  delete this.#connections[peerId];
27610
27613
  }
27611
27614
  // RTCPeerConnection
@@ -27643,28 +27646,38 @@ class Peernet {
27643
27646
  };
27644
27647
  #peerClose = (peer) => {
27645
27648
  if (this.#connections[peer.peerId]) {
27646
- this.#connections[peer.peerId].destroy();
27649
+ peer.destroy();
27647
27650
  delete this.#connections[peer.peerId];
27648
27651
  }
27649
27652
  globalThis.debug(`closed ${peer.peerId}'s connection`);
27650
27653
  };
27651
27654
  #peerConnect = (peer) => {
27652
27655
  globalThis.debug(`${peer.peerId} connected`);
27653
- globalThis.pubsub.publish(this.#connectEvent, peer.peerId);
27656
+ globalThis.pubsub.publishVerbose(this.#connectEvent, peer.peerId);
27654
27657
  };
27655
- #noticeMessage = (message, id) => {
27658
+ #noticeMessage = (message, id, from, peer) => {
27656
27659
  if (globalThis.pubsub.subscribers[id]) {
27657
- globalThis.pubsub.publish(id, new Uint8Array(message));
27660
+ globalThis.pubsub.publish(id, {
27661
+ data: new Uint8Array(Object.values(message)),
27662
+ id,
27663
+ from,
27664
+ peer
27665
+ });
27658
27666
  }
27659
27667
  else {
27660
- globalThis.pubsub.publish('peer:data', new Uint8Array(message));
27668
+ globalThis.pubsub.publish('peer:data', {
27669
+ data: new Uint8Array(Object.values(message)),
27670
+ id,
27671
+ from,
27672
+ peer
27673
+ });
27661
27674
  }
27662
27675
  };
27663
27676
  #peerData = (peer, data) => {
27664
27677
  const { id, size, chunk } = JSON.parse(new TextDecoder().decode(data));
27665
27678
  peer.bw.down += size;
27666
27679
  if (size <= MAX_MESSAGE_SIZE) {
27667
- this.#noticeMessage(chunk, id);
27680
+ this.#noticeMessage(chunk, id, peer.peerId, peer);
27668
27681
  }
27669
27682
  else {
27670
27683
  if (!this.#messagesToHandle[id])
@@ -27674,7 +27687,7 @@ class Peernet {
27674
27687
  ...Object.values(chunk)
27675
27688
  ];
27676
27689
  if (this.#messagesToHandle[id].length === Number(size)) {
27677
- this.#noticeMessage(this.#messagesToHandle[id], id);
27690
+ this.#noticeMessage(this.#messagesToHandle[id], id, peer.peerId, peer);
27678
27691
  delete this.#messagesToHandle[id];
27679
27692
  }
27680
27693
  }
@@ -27692,7 +27705,7 @@ class Peernet {
27692
27705
  Object.values(this.#connections).map((connection) => connection.destroy()),
27693
27706
  Object.values(this.#stars).map((connection) => connection.close(0))
27694
27707
  ];
27695
- await Promise.allSettled(promises);
27708
+ return Promise.allSettled(promises);
27696
27709
  }
27697
27710
  };
27698
27711
  /**
@@ -27725,9 +27738,23 @@ class Peernet {
27725
27738
  if (peer.connected) {
27726
27739
  await peer.send(data, id);
27727
27740
  this.bw.up += data.length;
27741
+ return id;
27742
+ }
27743
+ else {
27744
+ return new Promise((resolve, reject) => {
27745
+ const onError = (error) => {
27746
+ this.removePeer(peer);
27747
+ reject(error);
27748
+ };
27749
+ peer.once('error', onError);
27750
+ peer.once('connect', async () => {
27751
+ await peer.send(data, id);
27752
+ this.bw.up += data.length;
27753
+ peer.removeListener('error', onError);
27754
+ resolve(id);
27755
+ });
27756
+ });
27728
27757
  }
27729
- else
27730
- this.removePeer(peer);
27731
27758
  }
27732
27759
  async handleDHT(peer, id, proto) {
27733
27760
  let { hash, store } = proto.decoded;
@@ -1,2 +1,2 @@
1
- export { N as default } from './node-browser-77f8d4c5.js';
1
+ export { N as default } from './node-browser-2c421007.js';
2
2
  import './index-c3bd3090.js';
@@ -16,9 +16,11 @@ const run = async (blocks) => {
16
16
  // todo: tx worker or nah?
17
17
  await block.encode();
18
18
  const size = block.encoded.length || block.encoded.byteLength;
19
- console.log(`loaded block: ${await block.hash()} @${block.decoded.index} ${formatBytes(size)}`);
19
+ const hash = await block.hash();
20
+ const index = block.decoded.index;
21
+ console.log(`loaded block: ${hash} @${index} ${formatBytes(size)}`);
20
22
  // todo we don't want this, need shared state
21
- resolve(block.decoded);
23
+ resolve({ ...block.decoded, blockInfo: { hash, size, index } });
22
24
  })
23
25
  )
24
26
  );
@@ -112,27 +112,47 @@ const _init = async ({ contracts, blocks, peerid }) => {
112
112
  pre = './';
113
113
  }
114
114
 
115
- const _worker = await new EasyWorker(pre + './block-worker.js', {
115
+ let _worker = await new EasyWorker(pre + './block-worker.js', {
116
116
  serialization: 'advanced',
117
117
  type: 'module'
118
118
  });
119
- blocks = await _worker.once([blocks[blocks.length - 1]]);
120
-
119
+ blocks = await _worker.once(blocks);
120
+ console.log({ blocks });
121
+ _worker = null;
121
122
  // blocks = unique(globalThis.blocks ? globalThis : [], blocks)
122
123
  // for (let i = 0; i < blocks.length; i++) {
123
124
 
124
125
  // }
125
- // for (const block of blocks) {
126
- // await Promise.all(block.decoded.transactions.map(async message => {
127
- // if (!block.loaded) {
128
- // const {from, to, method, params} = message;
129
- // globalThis.msg = createMessage(from);
130
-
131
- // await execute(to, method, params);
132
- // block.loaded = true
133
- // }
134
- // }));
135
- // }
126
+ for (const block of blocks) {
127
+ try {
128
+ await Promise.all(
129
+ block.transactions.map(async (message) => {
130
+ if (!block.loaded) {
131
+ const { from, to, method, params } = message;
132
+ globalThis.msg = createMessage(from);
133
+
134
+ try {
135
+ await execute(to, method, params);
136
+ } catch (error) {
137
+ throw error
138
+ }
139
+ }
140
+ })
141
+ );
142
+ block.loaded = true;
143
+ worker.postMessage({
144
+ type: 'debug',
145
+ message: `loaded transactions for block: ${block.blockInfo.hash} @${block.blockInfo.index} ${formatBytes(
146
+ block.blockInfo.size
147
+ )}`
148
+ });
149
+ } catch (error) {
150
+ worker.postMessage({
151
+ type: 'error',
152
+ message: error.message
153
+ });
154
+ }
155
+ }
136
156
 
137
157
  if (blocks.length > 0) {
138
158
  lastBlock = blocks[blocks.length - 1];
package/exports/chain.js CHANGED
@@ -201,7 +201,8 @@ class Transaction extends Protocol {
201
201
  await globalThis.transactionPoolStore.put(hash, message.encoded);
202
202
  // debug(`Added ${hash} to the transaction pool`)
203
203
  peernet.publish('add-transaction', message.encoded);
204
- return { hash, data, fee: await calculateFee(message.decoded), wait, message };
204
+ const fee = await calculateFee(message.decoded);
205
+ return { hash, data, fee, wait, message };
205
206
  }
206
207
  catch (error) {
207
208
  console.log('remo');
@@ -304,8 +305,11 @@ class Machine {
304
305
  break;
305
306
  }
306
307
  case 'debug': {
307
- for (const message of data.messages)
308
- globalThis.debug(message);
308
+ globalThis.debug(data.message);
309
+ break;
310
+ }
311
+ case 'error': {
312
+ console.error(data.message);
309
313
  break;
310
314
  }
311
315
  case 'machine-ready': {
@@ -726,7 +730,7 @@ class State extends Contract {
726
730
  try {
727
731
  await this.resolveBlocks();
728
732
  this.#machine = await new Machine(this.#blocks);
729
- await this.#loadBlocks(this.#blocks);
733
+ // await this.#loadBlocks(this.#blocks)
730
734
  }
731
735
  catch (error) {
732
736
  if (isResolveError(error)) {
@@ -1165,6 +1169,13 @@ class Chain extends VersionControl {
1165
1169
  this.#participants = [];
1166
1170
  this.#participating = false;
1167
1171
  this.#jail = [];
1172
+ this.#addTransaction = async (message) => {
1173
+ const transaction = new TransactionMessage(message);
1174
+ const hash = await transaction.hash();
1175
+ if (await transactionPool.has(hash))
1176
+ await transactionPool.delete(hash);
1177
+ console.log(transaction);
1178
+ };
1168
1179
  // @ts-ignore
1169
1180
  return this.#init();
1170
1181
  }
@@ -1268,9 +1279,7 @@ class Chain extends VersionControl {
1268
1279
  }, validatorInfo.timeout);
1269
1280
  this.#jail.push(validatorInfo.address);
1270
1281
  }
1271
- #addTransaction(message) {
1272
- console.log({ message });
1273
- }
1282
+ #addTransaction;
1274
1283
  async #prepareRequest(request) {
1275
1284
  let node = await new globalThis.peernet.protos['peernet-request']({ request });
1276
1285
  return globalThis.peernet.prepareMessage(node);
@@ -1295,7 +1304,6 @@ class Chain extends VersionControl {
1295
1304
  }
1296
1305
  async #peerConnected(peerId) {
1297
1306
  const peer = peernet.getConnection(peerId);
1298
- console.log({ peer });
1299
1307
  // todo handle version changes
1300
1308
  // for now just do nothing if version doesn't match
1301
1309
  console.log(`${peer.version}, ${this.version}`);
@@ -16,9 +16,11 @@ const run = async (blocks) => {
16
16
  // todo: tx worker or nah?
17
17
  await block.encode();
18
18
  const size = block.encoded.length || block.encoded.byteLength;
19
- console.log(`loaded block: ${await block.hash()} @${block.decoded.index} ${formatBytes(size)}`);
19
+ const hash = await block.hash();
20
+ const index = block.decoded.index;
21
+ console.log(`loaded block: ${hash} @${index} ${formatBytes(size)}`);
20
22
  // todo we don't want this, need shared state
21
- resolve(block.decoded);
23
+ resolve({ ...block.decoded, blockInfo: { hash, size, index } });
22
24
  })
23
25
  )
24
26
  );
@@ -112,27 +112,47 @@ const _init = async ({ contracts, blocks, peerid }) => {
112
112
  pre = './';
113
113
  }
114
114
 
115
- const _worker = await new EasyWorker(pre + 'block-worker.js', {
115
+ let _worker = await new EasyWorker(pre + 'block-worker.js', {
116
116
  serialization: 'advanced',
117
117
  type: 'module'
118
118
  });
119
- blocks = await _worker.once([blocks[blocks.length - 1]]);
120
-
119
+ blocks = await _worker.once(blocks);
120
+ console.log({ blocks });
121
+ _worker = null;
121
122
  // blocks = unique(globalThis.blocks ? globalThis : [], blocks)
122
123
  // for (let i = 0; i < blocks.length; i++) {
123
124
 
124
125
  // }
125
- // for (const block of blocks) {
126
- // await Promise.all(block.decoded.transactions.map(async message => {
127
- // if (!block.loaded) {
128
- // const {from, to, method, params} = message;
129
- // globalThis.msg = createMessage(from);
130
-
131
- // await execute(to, method, params);
132
- // block.loaded = true
133
- // }
134
- // }));
135
- // }
126
+ for (const block of blocks) {
127
+ try {
128
+ await Promise.all(
129
+ block.transactions.map(async (message) => {
130
+ if (!block.loaded) {
131
+ const { from, to, method, params } = message;
132
+ globalThis.msg = createMessage(from);
133
+
134
+ try {
135
+ await execute(to, method, params);
136
+ } catch (error) {
137
+ throw error
138
+ }
139
+ }
140
+ })
141
+ );
142
+ block.loaded = true;
143
+ worker.postMessage({
144
+ type: 'debug',
145
+ message: `loaded transactions for block: ${block.blockInfo.hash} @${block.blockInfo.index} ${formatBytes(
146
+ block.blockInfo.size
147
+ )}`
148
+ });
149
+ } catch (error) {
150
+ worker.postMessage({
151
+ type: 'error',
152
+ message: error.message
153
+ });
154
+ }
155
+ }
136
156
 
137
157
  if (blocks.length > 0) {
138
158
  lastBlock = blocks[blocks.length - 1];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.5.43",
3
+ "version": "1.5.45",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": {