@leofcoin/workers 1.5.0 → 1.5.2
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 +1 -1
- package/src/machine-worker.js +49 -27
package/package.json
CHANGED
package/src/machine-worker.js
CHANGED
|
@@ -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,43 @@ _.init = async (message) => {
|
|
|
249
256
|
latestTransactions.splice(-transactionCount, latestTransactions.length)
|
|
250
257
|
}
|
|
251
258
|
if (!block.loaded && !fromState) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
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
|
+
return message
|
|
267
|
+
})
|
|
268
|
+
)
|
|
269
|
+
const priority = transactions
|
|
270
|
+
.filter((transaction) => transaction.priority)
|
|
271
|
+
?.sort((a, b) => a.nonce - b.nonce)
|
|
272
|
+
if (priority.length > 0)
|
|
273
|
+
for (const transaction of priority) {
|
|
274
|
+
await _executeTransaction(transaction)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
await Promise.all(
|
|
278
|
+
transactions
|
|
279
|
+
.filter((transaction) => !transaction.priority)
|
|
280
|
+
.map(async (transaction) => _executeTransaction(transaction))
|
|
255
281
|
)
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
282
|
+
block.loaded = true
|
|
283
|
+
worker.postMessage({
|
|
284
|
+
type: 'debug',
|
|
285
|
+
message: `loaded transactions for block: ${block.hash} @${block.index}`
|
|
286
|
+
})
|
|
287
|
+
} catch (error) {
|
|
288
|
+
// just tell chain it's ready so we can get this node sync
|
|
289
|
+
// when a node connects this node will try to resolve the wantList
|
|
290
|
+
// this should result in the node beeing sync
|
|
291
|
+
if (error.message.includes('nothing found for')) worker.postMessage({ type: 'machine-ready', lastBlock })
|
|
292
|
+
else console.error(error)
|
|
293
|
+
}
|
|
268
294
|
}
|
|
269
|
-
block.loaded = true
|
|
270
|
-
worker.postMessage({
|
|
271
|
-
type: 'debug',
|
|
272
|
-
message: `loaded transactions for block: ${block.hash} @${block.index}`
|
|
273
|
-
})
|
|
274
295
|
}
|
|
275
|
-
|
|
276
296
|
if (blocks.length > 0) {
|
|
277
297
|
lastBlock = blocks[blocks.length - 1]
|
|
278
298
|
}
|
|
@@ -281,8 +301,6 @@ _.init = async (message) => {
|
|
|
281
301
|
}
|
|
282
302
|
|
|
283
303
|
worker.postMessage({ type: 'machine-ready', lastBlock })
|
|
284
|
-
|
|
285
|
-
// worker.postMessage({blocks});
|
|
286
304
|
}
|
|
287
305
|
|
|
288
306
|
_.addLoadedBlock = (block) => {
|
|
@@ -307,6 +325,10 @@ const askFor = (question, input) =>
|
|
|
307
325
|
})
|
|
308
326
|
})
|
|
309
327
|
|
|
328
|
+
const resolveContract = (hash) => askFor('contract', hash)
|
|
329
|
+
|
|
330
|
+
const resolveTransaction = (hash) => askFor('transaction', hash)
|
|
331
|
+
|
|
310
332
|
const runTask = async (id, taskName, input) => {
|
|
311
333
|
try {
|
|
312
334
|
const result = await _[taskName](input)
|