@leofcoin/workers 1.3.19 → 1.4.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/package.json +1 -1
- package/src/block-worker.js +4 -2
- package/src/chain-worker.ts +42 -0
- package/src/machine-worker.js +34 -14
package/package.json
CHANGED
package/src/block-worker.js
CHANGED
|
@@ -19,9 +19,11 @@ const run = async (blocks) => {
|
|
|
19
19
|
// todo: tx worker or nah?
|
|
20
20
|
await block.encode()
|
|
21
21
|
const size = block.encoded.length || block.encoded.byteLength
|
|
22
|
-
|
|
22
|
+
const hash = await block.hash()
|
|
23
|
+
const index = block.decoded.index
|
|
24
|
+
console.log(`loaded block: ${hash} @${index} ${formatBytes(size)}`)
|
|
23
25
|
// todo we don't want this, need shared state
|
|
24
|
-
resolve(block.decoded)
|
|
26
|
+
resolve({ ...block.decoded, blockInfo: { hash, size, index } })
|
|
25
27
|
})
|
|
26
28
|
)
|
|
27
29
|
)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { BlockMessage } from '@leofcoin/messages'
|
|
2
|
+
import { formatBytes, BigNumber } from '@leofcoin/utils'
|
|
3
|
+
|
|
4
|
+
import EasyWorker from '@vandeurenglenn/easy-worker'
|
|
5
|
+
|
|
6
|
+
declare type messageType = 'put' | 'get'
|
|
7
|
+
|
|
8
|
+
declare type message = {
|
|
9
|
+
type: messageType
|
|
10
|
+
input: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare type messageReturn = {
|
|
14
|
+
type: messageType
|
|
15
|
+
result?: string
|
|
16
|
+
message?: string
|
|
17
|
+
error?: Error
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const _addBlock = (block) => {
|
|
21
|
+
blocks[block.index - 1] = block
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const _getBlocks = (index = 0) => {
|
|
25
|
+
worker.postMessage({ type: 'getBlock', result: blocks.slice(index - 1, blocks.length - 1) })
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const _getBlock = (index) => {
|
|
29
|
+
worker.postMessage({ type: 'getBlock', result: blocks[index - 1] })
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const _init = (_blocks) => {
|
|
33
|
+
blocks = _blocks
|
|
34
|
+
try {
|
|
35
|
+
} catch (error) {}
|
|
36
|
+
worker.postMessage({ type: 'init', result: 'ready' })
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
worker.onmessage((message: message) => {
|
|
40
|
+
if (!message.type) message.type = 'init'
|
|
41
|
+
_[message.type](message.input)
|
|
42
|
+
})
|
package/src/machine-worker.js
CHANGED
|
@@ -108,27 +108,47 @@ const _init = async ({ contracts, blocks, peerid }) => {
|
|
|
108
108
|
pre = './'
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
let _worker = await new EasyWorker(pre + '@leofcoin/workers/block-worker.js', {
|
|
112
112
|
serialization: 'advanced',
|
|
113
113
|
type: 'module'
|
|
114
114
|
})
|
|
115
|
-
blocks = await _worker.once(
|
|
116
|
-
|
|
115
|
+
blocks = await _worker.once(blocks)
|
|
116
|
+
console.log({ blocks })
|
|
117
|
+
_worker = null
|
|
117
118
|
// blocks = unique(globalThis.blocks ? globalThis : [], blocks)
|
|
118
119
|
// for (let i = 0; i < blocks.length; i++) {
|
|
119
120
|
|
|
120
121
|
// }
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
122
|
+
for (const block of blocks) {
|
|
123
|
+
try {
|
|
124
|
+
await Promise.all(
|
|
125
|
+
block.transactions.map(async (message) => {
|
|
126
|
+
if (!block.loaded) {
|
|
127
|
+
const { from, to, method, params } = message
|
|
128
|
+
globalThis.msg = createMessage(from)
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
await execute(to, method, params)
|
|
132
|
+
} catch (error) {
|
|
133
|
+
throw error
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
)
|
|
138
|
+
block.loaded = true
|
|
139
|
+
worker.postMessage({
|
|
140
|
+
type: 'debug',
|
|
141
|
+
message: `loaded transactions for block: ${block.blockInfo.hash} @${block.blockInfo.index} ${formatBytes(
|
|
142
|
+
block.blockInfo.size
|
|
143
|
+
)}`
|
|
144
|
+
})
|
|
145
|
+
} catch (error) {
|
|
146
|
+
worker.postMessage({
|
|
147
|
+
type: 'error',
|
|
148
|
+
message: error.message
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
}
|
|
132
152
|
|
|
133
153
|
if (blocks.length > 0) {
|
|
134
154
|
lastBlock = blocks[blocks.length - 1]
|