@leofcoin/workers 1.4.19 → 1.5.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @leofcoin/workers
2
2
 
3
+ ## 1.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Update chain & machine-worker to support LIP1
8
+
3
9
  ## 1.4.19
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/workers",
3
- "version": "1.4.19",
3
+ "version": "1.5.0",
4
4
  "description": "Workers used in @leofcoin/chain",
5
5
  "type": "module",
6
6
  "private": false,
@@ -58,7 +58,9 @@ const get = ({ contract, method, params }) => {
58
58
  return result
59
59
  }
60
60
 
61
- const resolveContract = (address) => askFor('contract', address)
61
+ const resolveContract = (hash) => askFor('contract', hash)
62
+
63
+ const resolveTransaction = (hash) => askFor('transaction', hash)
62
64
 
63
65
  const respond = (id, value) => {
64
66
  worker.postMessage({
@@ -214,47 +216,52 @@ _.init = async (message) => {
214
216
  )
215
217
  console.log({ blocks: message.blocks })
216
218
  if (message.blocks?.length > 0) {
217
- let pre
218
-
219
- try {
220
- const importee = await import('url')
221
- const url = importee.default
222
- if (url) pre = url.fileURLToPath(new URL('.', import.meta.url))
223
- } catch {
224
- // browser env
225
- pre = './'
226
- }
219
+ // let pre
220
+
221
+ // try {
222
+ // const importee = await import('url')
223
+ // const url = importee.default
224
+ // if (url) pre = url.fileURLToPath(new URL('.', import.meta.url))
225
+ // } catch {
226
+ // // browser env
227
+ // pre = './'
228
+ // }
227
229
 
228
- let _worker = await new EasyWorker(pre + '@leofcoin/workers/block-worker.js', {
229
- serialization: 'advanced',
230
- type: 'module'
231
- })
232
- blocks = await _worker.once(message.blocks)
233
- _worker = null
230
+ // let _worker = await new EasyWorker(pre + '@leofcoin/workers/block-worker.js', {
231
+ // serialization: 'advanced',
232
+ // type: 'module'
233
+ // })
234
+ // blocks = await _worker.once(message.blocks)
235
+ // _worker = null
234
236
  // blocks = unique(globalThis.blocks ? globalThis : [], blocks)
235
237
  // for (let i = 0; i < blocks.length; i++) {
236
238
 
237
239
  // }
240
+ blocks = message.blocks
238
241
  for (const block of blocks) {
239
242
  // we only revalidate the latest 24 blocks
240
243
  // every 24 blocks a snapshot is taken and stored in state
241
244
  // this means contracts will be restored from this state
242
245
  // this also means devs NEED to make sure the state can be restored
243
246
  // on contract deploy an error will be thrown if state wasn't recoverable
244
- if (block.index > 24) {
245
- const transactionCount = blocks[block.index - 1].transactions.length
246
- latestTransactions.splice(-(transactionCount - 1), latestTransactions.length)
247
+ if (block.index >= blocks.length - 24) {
248
+ const transactionCount = blocks[block.index].transactions.length
249
+ latestTransactions.splice(-transactionCount, latestTransactions.length)
247
250
  }
248
-
249
251
  if (!block.loaded && !fromState) {
250
- const priority = block.transactions.filter((transaction) => transaction.priority)
251
- if (priority.length > 0)
252
- await Promise.all(
253
- priority.sort((a, b) => a.nonce - b.nonce).map((transaction) => _executeTransaction(transaction))
252
+ const transactions = await Promise.all(
253
+ block.transactions.map(async (transaction) =>
254
+ new TransactionMessage(await resolveTransaction(transaction)).decode()
254
255
  )
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
+ }
255
262
 
256
263
  await Promise.all(
257
- block.transactions
264
+ transactions
258
265
  .filter((transaction) => !transaction.priority)
259
266
  .map(async (transaction) => _executeTransaction(transaction))
260
267
  )
@@ -262,9 +269,7 @@ _.init = async (message) => {
262
269
  block.loaded = true
263
270
  worker.postMessage({
264
271
  type: 'debug',
265
- message: `loaded transactions for block: ${block.blockInfo.hash} @${block.blockInfo.index} ${formatBytes(
266
- block.blockInfo.size
267
- )}`
272
+ message: `loaded transactions for block: ${block.hash} @${block.index}`
268
273
  })
269
274
  }
270
275