@leofcoin/workers 1.5.0 → 1.5.1

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/workers",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Workers used in @leofcoin/chain",
5
5
  "type": "module",
6
6
  "private": false,
@@ -58,10 +58,6 @@ const get = ({ contract, method, params }) => {
58
58
  return result
59
59
  }
60
60
 
61
- const resolveContract = (hash) => askFor('contract', hash)
62
-
63
- const resolveTransaction = (hash) => askFor('transaction', hash)
64
-
65
61
  const respond = (id, value) => {
66
62
  worker.postMessage({
67
63
  type: 'response',
@@ -163,6 +159,13 @@ const _executeTransaction = async (transaction) => {
163
159
  }
164
160
  }
165
161
 
162
+ const addToWantList = (hash) => {
163
+ worker.postMessage({
164
+ type: 'addToWantList',
165
+ hash
166
+ })
167
+ }
168
+
166
169
  _.init = async (message) => {
167
170
  let { peerid, fromState, state, info } = message
168
171
  globalThis.peerid = peerid
@@ -180,6 +183,10 @@ _.init = async (message) => {
180
183
  lastBlock = message.lastBlock
181
184
  const setState = async (address, state) => {
182
185
  const contractBytes = await resolveContract(address)
186
+ if (contractBytes === address) {
187
+ addToWantList(address)
188
+ return
189
+ }
183
190
  const contract = await new ContractMessage(contractBytes)
184
191
 
185
192
  await _.runContract({ hash: address, decoded: contract.decoded, encoded: contract.encoded }, state)
@@ -249,30 +256,42 @@ _.init = async (message) => {
249
256
  latestTransactions.splice(-transactionCount, latestTransactions.length)
250
257
  }
251
258
  if (!block.loaded && !fromState) {
252
- const transactions = await Promise.all(
253
- block.transactions.map(async (transaction) =>
254
- new TransactionMessage(await resolveTransaction(transaction)).decode()
259
+ try {
260
+ const transactions = await Promise.all(
261
+ block.transactions.map(async (transaction) => {
262
+ const message = new TransactionMessage(await resolveTransaction(transaction)).decode()
263
+ if (message === transaction) {
264
+ throw new Error(`nothing found for ${transaction}`)
265
+ }
266
+ })
267
+ )
268
+ const priority = transactions
269
+ .filter((transaction) => transaction.priority)
270
+ ?.sort((a, b) => a.nonce - b.nonce)
271
+ if (priority.length > 0)
272
+ for (const transaction of priority) {
273
+ await _executeTransaction(transaction)
274
+ }
275
+
276
+ await Promise.all(
277
+ transactions
278
+ .filter((transaction) => !transaction.priority)
279
+ .map(async (transaction) => _executeTransaction(transaction))
255
280
  )
256
- )
257
- const priority = transactions.filter((transaction) => transaction.priority)?.sort((a, b) => a.nonce - b.nonce)
258
- if (priority.length > 0)
259
- for (const transaction of priority) {
260
- await _executeTransaction(transaction)
261
- }
262
-
263
- await Promise.all(
264
- transactions
265
- .filter((transaction) => !transaction.priority)
266
- .map(async (transaction) => _executeTransaction(transaction))
267
- )
281
+ block.loaded = true
282
+ worker.postMessage({
283
+ type: 'debug',
284
+ message: `loaded transactions for block: ${block.hash} @${block.index}`
285
+ })
286
+ } catch (error) {
287
+ // just tell chain it's ready so we can get this node sync
288
+ // when a node connects this node will try to resolve the wantList
289
+ // this should result in the node beeing sync
290
+ if (error.message.includes('nothing found for')) worker.postMessage({ type: 'machine-ready', lastBlock })
291
+ else console.error(error)
292
+ }
268
293
  }
269
- block.loaded = true
270
- worker.postMessage({
271
- type: 'debug',
272
- message: `loaded transactions for block: ${block.hash} @${block.index}`
273
- })
274
294
  }
275
-
276
295
  if (blocks.length > 0) {
277
296
  lastBlock = blocks[blocks.length - 1]
278
297
  }
@@ -281,8 +300,6 @@ _.init = async (message) => {
281
300
  }
282
301
 
283
302
  worker.postMessage({ type: 'machine-ready', lastBlock })
284
-
285
- // worker.postMessage({blocks});
286
303
  }
287
304
 
288
305
  _.addLoadedBlock = (block) => {
@@ -307,6 +324,10 @@ const askFor = (question, input) =>
307
324
  })
308
325
  })
309
326
 
327
+ const resolveContract = (hash) => askFor('contract', hash)
328
+
329
+ const resolveTransaction = (hash) => askFor('transaction', hash)
330
+
310
331
  const runTask = async (id, taskName, input) => {
311
332
  try {
312
333
  const result = await _[taskName](input)