@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 +6 -0
- package/package.json +1 -1
- package/src/machine-worker.js +34 -29
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/machine-worker.js
CHANGED
|
@@ -58,7 +58,9 @@ const get = ({ contract, method, params }) => {
|
|
|
58
58
|
return result
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
const resolveContract = (
|
|
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
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
} catch {
|
|
224
|
-
|
|
225
|
-
|
|
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
|
-
|
|
230
|
-
|
|
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
|
|
245
|
-
const transactionCount = blocks[block.index
|
|
246
|
-
latestTransactions.splice(-
|
|
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
|
|
251
|
-
|
|
252
|
-
|
|
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
|
-
|
|
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.
|
|
266
|
-
block.blockInfo.size
|
|
267
|
-
)}`
|
|
272
|
+
message: `loaded transactions for block: ${block.hash} @${block.index}`
|
|
268
273
|
})
|
|
269
274
|
}
|
|
270
275
|
|