@leofcoin/workers 1.4.19 → 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/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.1",
4
4
  "description": "Workers used in @leofcoin/chain",
5
5
  "type": "module",
6
6
  "private": false,
@@ -58,8 +58,6 @@ const get = ({ contract, method, params }) => {
58
58
  return result
59
59
  }
60
60
 
61
- const resolveContract = (address) => askFor('contract', address)
62
-
63
61
  const respond = (id, value) => {
64
62
  worker.postMessage({
65
63
  type: 'response',
@@ -161,6 +159,13 @@ const _executeTransaction = async (transaction) => {
161
159
  }
162
160
  }
163
161
 
162
+ const addToWantList = (hash) => {
163
+ worker.postMessage({
164
+ type: 'addToWantList',
165
+ hash
166
+ })
167
+ }
168
+
164
169
  _.init = async (message) => {
165
170
  let { peerid, fromState, state, info } = message
166
171
  globalThis.peerid = peerid
@@ -178,6 +183,10 @@ _.init = async (message) => {
178
183
  lastBlock = message.lastBlock
179
184
  const setState = async (address, state) => {
180
185
  const contractBytes = await resolveContract(address)
186
+ if (contractBytes === address) {
187
+ addToWantList(address)
188
+ return
189
+ }
181
190
  const contract = await new ContractMessage(contractBytes)
182
191
 
183
192
  await _.runContract({ hash: address, decoded: contract.decoded, encoded: contract.encoded }, state)
@@ -214,60 +223,75 @@ _.init = async (message) => {
214
223
  )
215
224
  console.log({ blocks: message.blocks })
216
225
  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
- }
226
+ // let pre
227
+
228
+ // try {
229
+ // const importee = await import('url')
230
+ // const url = importee.default
231
+ // if (url) pre = url.fileURLToPath(new URL('.', import.meta.url))
232
+ // } catch {
233
+ // // browser env
234
+ // pre = './'
235
+ // }
227
236
 
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
237
+ // let _worker = await new EasyWorker(pre + '@leofcoin/workers/block-worker.js', {
238
+ // serialization: 'advanced',
239
+ // type: 'module'
240
+ // })
241
+ // blocks = await _worker.once(message.blocks)
242
+ // _worker = null
234
243
  // blocks = unique(globalThis.blocks ? globalThis : [], blocks)
235
244
  // for (let i = 0; i < blocks.length; i++) {
236
245
 
237
246
  // }
247
+ blocks = message.blocks
238
248
  for (const block of blocks) {
239
249
  // we only revalidate the latest 24 blocks
240
250
  // every 24 blocks a snapshot is taken and stored in state
241
251
  // this means contracts will be restored from this state
242
252
  // this also means devs NEED to make sure the state can be restored
243
253
  // 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)
254
+ if (block.index >= blocks.length - 24) {
255
+ const transactionCount = blocks[block.index].transactions.length
256
+ latestTransactions.splice(-transactionCount, latestTransactions.length)
247
257
  }
248
-
249
258
  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))
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
+ })
254
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
+ }
255
275
 
256
- await Promise.all(
257
- block.transactions
258
- .filter((transaction) => !transaction.priority)
259
- .map(async (transaction) => _executeTransaction(transaction))
260
- )
276
+ await Promise.all(
277
+ transactions
278
+ .filter((transaction) => !transaction.priority)
279
+ .map(async (transaction) => _executeTransaction(transaction))
280
+ )
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
+ }
261
293
  }
262
- block.loaded = true
263
- worker.postMessage({
264
- type: 'debug',
265
- message: `loaded transactions for block: ${block.blockInfo.hash} @${block.blockInfo.index} ${formatBytes(
266
- block.blockInfo.size
267
- )}`
268
- })
269
294
  }
270
-
271
295
  if (blocks.length > 0) {
272
296
  lastBlock = blocks[blocks.length - 1]
273
297
  }
@@ -276,8 +300,6 @@ _.init = async (message) => {
276
300
  }
277
301
 
278
302
  worker.postMessage({ type: 'machine-ready', lastBlock })
279
-
280
- // worker.postMessage({blocks});
281
303
  }
282
304
 
283
305
  _.addLoadedBlock = (block) => {
@@ -302,6 +324,10 @@ const askFor = (question, input) =>
302
324
  })
303
325
  })
304
326
 
327
+ const resolveContract = (hash) => askFor('contract', hash)
328
+
329
+ const resolveTransaction = (hash) => askFor('transaction', hash)
330
+
305
331
  const runTask = async (id, taskName, input) => {
306
332
  try {
307
333
  const result = await _[taskName](input)