@leofcoin/workers 1.4.18 → 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 +17 -0
- package/package.json +6 -6
- package/src/machine-worker.js +65 -33
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @leofcoin/workers
|
|
2
2
|
|
|
3
|
+
## 1.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Update chain & machine-worker to support LIP1
|
|
8
|
+
|
|
9
|
+
## 1.4.19
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 78299bf: bump
|
|
14
|
+
- Updated dependencies [78299bf]
|
|
15
|
+
- @leofcoin/addresses@1.0.23
|
|
16
|
+
- @leofcoin/lib@1.2.43
|
|
17
|
+
- @leofcoin/messages@1.4.13
|
|
18
|
+
- @leofcoin/utils@1.1.18
|
|
19
|
+
|
|
3
20
|
## 1.4.18
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/workers",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Workers used in @leofcoin/chain",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"author": "",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@leofcoin/addresses": "^1.0.
|
|
25
|
-
"@leofcoin/lib": "^1.2.
|
|
26
|
-
"@leofcoin/messages": "^1.4.
|
|
27
|
-
"@leofcoin/storage": "^3.5.
|
|
28
|
-
"@leofcoin/utils": "^1.1.
|
|
24
|
+
"@leofcoin/addresses": "^1.0.23",
|
|
25
|
+
"@leofcoin/lib": "^1.2.43",
|
|
26
|
+
"@leofcoin/messages": "^1.4.13",
|
|
27
|
+
"@leofcoin/storage": "^3.5.28",
|
|
28
|
+
"@leofcoin/utils": "^1.1.18",
|
|
29
29
|
"@vandeurenglenn/easy-worker": "^1.0.2"
|
|
30
30
|
}
|
|
31
31
|
}
|
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({
|
|
@@ -135,9 +137,18 @@ const _executeTransaction = async (transaction) => {
|
|
|
135
137
|
await _.execute({ contract: to, method, params })
|
|
136
138
|
if (to === nativeToken) {
|
|
137
139
|
nativeCalls += 1
|
|
138
|
-
if (method === 'burn')
|
|
139
|
-
|
|
140
|
-
|
|
140
|
+
if (method === 'burn') {
|
|
141
|
+
nativeBurns += 1
|
|
142
|
+
totalBurnAmount += params[0]
|
|
143
|
+
}
|
|
144
|
+
if (method === 'mint') {
|
|
145
|
+
nativeMints += 1
|
|
146
|
+
totalMintAmount += params[0]
|
|
147
|
+
}
|
|
148
|
+
if (method === 'transfer') {
|
|
149
|
+
nativeTransfers += 1
|
|
150
|
+
totalTransferAmount += params[0]
|
|
151
|
+
}
|
|
141
152
|
}
|
|
142
153
|
totalTransactions += 1
|
|
143
154
|
|
|
@@ -153,10 +164,19 @@ const _executeTransaction = async (transaction) => {
|
|
|
153
164
|
}
|
|
154
165
|
|
|
155
166
|
_.init = async (message) => {
|
|
156
|
-
let { peerid, fromState, state } = message
|
|
167
|
+
let { peerid, fromState, state, info } = message
|
|
157
168
|
globalThis.peerid = peerid
|
|
158
169
|
console.log({ fromState })
|
|
159
170
|
if (fromState) {
|
|
171
|
+
nativeCalls = info.nativeCalls
|
|
172
|
+
nativeBurns = info.nativeBurns
|
|
173
|
+
nativeMints = info.nativeMints
|
|
174
|
+
nativeTransfers = info.nativeTransfers
|
|
175
|
+
totalTransactions = info.totalTransactions
|
|
176
|
+
totalBurnAmount = info.totalBurnAmount
|
|
177
|
+
totalMintAmount = info.totalMintAmount
|
|
178
|
+
totalTransferAmount = info.totalTransferAmount
|
|
179
|
+
|
|
160
180
|
lastBlock = message.lastBlock
|
|
161
181
|
const setState = async (address, state) => {
|
|
162
182
|
const contractBytes = await resolveContract(address)
|
|
@@ -196,47 +216,52 @@ _.init = async (message) => {
|
|
|
196
216
|
)
|
|
197
217
|
console.log({ blocks: message.blocks })
|
|
198
218
|
if (message.blocks?.length > 0) {
|
|
199
|
-
let pre
|
|
200
|
-
|
|
201
|
-
try {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
} catch {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
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
|
+
// }
|
|
209
229
|
|
|
210
|
-
let _worker = await new EasyWorker(pre + '@leofcoin/workers/block-worker.js', {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
})
|
|
214
|
-
blocks = await _worker.once(message.blocks)
|
|
215
|
-
_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
|
|
216
236
|
// blocks = unique(globalThis.blocks ? globalThis : [], blocks)
|
|
217
237
|
// for (let i = 0; i < blocks.length; i++) {
|
|
218
238
|
|
|
219
239
|
// }
|
|
240
|
+
blocks = message.blocks
|
|
220
241
|
for (const block of blocks) {
|
|
221
242
|
// we only revalidate the latest 24 blocks
|
|
222
243
|
// every 24 blocks a snapshot is taken and stored in state
|
|
223
244
|
// this means contracts will be restored from this state
|
|
224
245
|
// this also means devs NEED to make sure the state can be restored
|
|
225
246
|
// on contract deploy an error will be thrown if state wasn't recoverable
|
|
226
|
-
if (block.index
|
|
227
|
-
const transactionCount = blocks[block.index
|
|
228
|
-
latestTransactions.splice(-
|
|
247
|
+
if (block.index >= blocks.length - 24) {
|
|
248
|
+
const transactionCount = blocks[block.index].transactions.length
|
|
249
|
+
latestTransactions.splice(-transactionCount, latestTransactions.length)
|
|
229
250
|
}
|
|
230
|
-
|
|
231
251
|
if (!block.loaded && !fromState) {
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
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()
|
|
236
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
|
+
}
|
|
237
262
|
|
|
238
263
|
await Promise.all(
|
|
239
|
-
|
|
264
|
+
transactions
|
|
240
265
|
.filter((transaction) => !transaction.priority)
|
|
241
266
|
.map(async (transaction) => _executeTransaction(transaction))
|
|
242
267
|
)
|
|
@@ -244,9 +269,7 @@ _.init = async (message) => {
|
|
|
244
269
|
block.loaded = true
|
|
245
270
|
worker.postMessage({
|
|
246
271
|
type: 'debug',
|
|
247
|
-
message: `loaded transactions for block: ${block.
|
|
248
|
-
block.blockInfo.size
|
|
249
|
-
)}`
|
|
272
|
+
message: `loaded transactions for block: ${block.hash} @${block.index}`
|
|
250
273
|
})
|
|
251
274
|
}
|
|
252
275
|
|
|
@@ -330,6 +353,15 @@ worker.onmessage(({ id, type, input }) => {
|
|
|
330
353
|
case 'nativeTransfers':
|
|
331
354
|
respond(id, nativeTransfers)
|
|
332
355
|
break
|
|
356
|
+
case 'totalBurnAmount':
|
|
357
|
+
respond(id, totalBurnAmount)
|
|
358
|
+
break
|
|
359
|
+
case 'totalMintAmount':
|
|
360
|
+
respond(id, totalMintAmount)
|
|
361
|
+
break
|
|
362
|
+
case 'totalTransferAmount':
|
|
363
|
+
respond(id, totalTransferAmount)
|
|
364
|
+
break
|
|
333
365
|
case 'totalTransfers':
|
|
334
366
|
respond(id, totalTransfers)
|
|
335
367
|
break
|