@leofcoin/workers 1.3.15 → 1.3.18
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 +27 -26
- package/src/block-worker.js +17 -12
- package/src/log-worker.ts +6 -7
- package/src/machine-worker.js +104 -113
- package/src/pool-worker.js +9 -9
- package/src/transaction-worker.js +2 -2
- package/src/workers.js +1 -1
package/package.json
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@leofcoin/workers",
|
|
3
|
-
"version": "1.3.
|
|
4
|
-
"description": "Workers used in @leofcoin/chain",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"exports": {
|
|
7
|
-
".": "./src/workers.js",
|
|
8
|
-
"./machine-worker": "./src/machine-worker.js",
|
|
9
|
-
"./block-worker": "./src/block-worker.js",
|
|
10
|
-
"./transaction-worker": "./src/transaction-worker.js",
|
|
11
|
-
"./pool-worker": "./src/pool-worker.js"
|
|
12
|
-
},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
"@leofcoin/
|
|
22
|
-
"@leofcoin/
|
|
23
|
-
"@leofcoin/
|
|
24
|
-
"@
|
|
25
|
-
|
|
26
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@leofcoin/workers",
|
|
3
|
+
"version": "1.3.18",
|
|
4
|
+
"description": "Workers used in @leofcoin/chain",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/workers.js",
|
|
8
|
+
"./machine-worker": "./src/machine-worker.js",
|
|
9
|
+
"./block-worker": "./src/block-worker.js",
|
|
10
|
+
"./transaction-worker": "./src/transaction-worker.js",
|
|
11
|
+
"./pool-worker": "./src/pool-worker.js"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"prettier": "prettier src --config ./../../.prettierrc --write",
|
|
15
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@leofcoin/lib": "^1.2.3",
|
|
22
|
+
"@leofcoin/messages": "^1.2.0",
|
|
23
|
+
"@leofcoin/storage": "^3.3.2",
|
|
24
|
+
"@leofcoin/utils": "^1.1.5",
|
|
25
|
+
"@vandeurenglenn/easy-worker": "^1.0.2"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/block-worker.js
CHANGED
|
@@ -8,24 +8,29 @@ const worker = new EasyWorker()
|
|
|
8
8
|
globalThis.BigNumber = BigNumber
|
|
9
9
|
globalThis.contracts = {}
|
|
10
10
|
|
|
11
|
-
const run = async (blocks) => {
|
|
12
|
-
blocks = await Promise.all(blocks.map(block => new BlockMessage(block)))
|
|
11
|
+
const run = async (blocks) => {
|
|
12
|
+
blocks = await Promise.all(blocks.map((block) => new BlockMessage(block)))
|
|
13
13
|
blocks = blocks.sort((a, b) => a.decoded.timestamp - b.decoded.timestamp)
|
|
14
14
|
|
|
15
|
-
blocks = await Promise.all(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
blocks = await Promise.all(
|
|
16
|
+
blocks.map(
|
|
17
|
+
(block) =>
|
|
18
|
+
new Promise(async (resolve, reject) => {
|
|
19
|
+
// todo: tx worker or nah?
|
|
20
|
+
await block.encode()
|
|
21
|
+
const size = block.encoded.length || block.encoded.byteLength
|
|
22
|
+
console.log(`loaded block: ${await block.hash()} @${block.decoded.index} ${formatBytes(size)}`)
|
|
23
|
+
// todo we don't want this, need shared state
|
|
24
|
+
resolve(block.decoded)
|
|
25
|
+
})
|
|
26
|
+
)
|
|
27
|
+
)
|
|
21
28
|
return blocks
|
|
22
29
|
}
|
|
23
30
|
|
|
24
|
-
const tasks = async blocks => {
|
|
31
|
+
const tasks = async (blocks) => {
|
|
25
32
|
blocks = await run(blocks)
|
|
26
33
|
worker.postMessage(blocks)
|
|
27
34
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
worker.onmessage(data => tasks(data))
|
|
31
35
|
|
|
36
|
+
worker.onmessage((data) => tasks(data))
|
package/src/log-worker.ts
CHANGED
|
@@ -10,18 +10,18 @@ await logStore.init()
|
|
|
10
10
|
declare type messageType = 'put' | 'get'
|
|
11
11
|
|
|
12
12
|
declare type message = {
|
|
13
|
-
type: messageType
|
|
13
|
+
type: messageType
|
|
14
14
|
input: string
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
declare type messageReturn = {
|
|
18
|
-
type: messageType
|
|
19
|
-
result?: string
|
|
20
|
-
message?: string
|
|
18
|
+
type: messageType
|
|
19
|
+
result?: string
|
|
20
|
+
message?: string
|
|
21
21
|
error?: Error
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
const handlePut = async message => {
|
|
24
|
+
const handlePut = async (message) => {
|
|
25
25
|
try {
|
|
26
26
|
await fetch(`http://localhost:8844/log/put`, {
|
|
27
27
|
method: 'put',
|
|
@@ -32,7 +32,7 @@ const handlePut = async message => {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
const handleGet = async message => {
|
|
35
|
+
const handleGet = async (message) => {
|
|
36
36
|
try {
|
|
37
37
|
await fetch(`http://localhost:8844/log/get?`, {
|
|
38
38
|
method: 'put',
|
|
@@ -48,4 +48,3 @@ worker.onmessage((message: message) => {
|
|
|
48
48
|
if (message.type === 'put') handlePut(message.input)
|
|
49
49
|
else handleGet(message.input)
|
|
50
50
|
})
|
|
51
|
-
|
package/src/machine-worker.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BlockMessage, ContractMessage } from '@leofcoin/messages'
|
|
2
2
|
import { formatBytes, BigNumber } from '@leofcoin/utils'
|
|
3
|
-
import bytecodes
|
|
3
|
+
import bytecodes from '@leofcoin/lib/bytecodes' assert { type: 'json' }
|
|
4
4
|
import EasyWorker from '@vandeurenglenn/easy-worker'
|
|
5
5
|
const worker = new EasyWorker()
|
|
6
6
|
|
|
@@ -12,11 +12,12 @@ const validatorsMessage = bytecodes.validators
|
|
|
12
12
|
globalThis.BigNumber = BigNumber
|
|
13
13
|
globalThis.contracts = {}
|
|
14
14
|
|
|
15
|
-
const unique =
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const unique = (arr) =>
|
|
16
|
+
arr.filter((el, pos, arr) => {
|
|
17
|
+
return arr.indexOf(el) == pos
|
|
18
|
+
})
|
|
18
19
|
|
|
19
|
-
const has = address => {
|
|
20
|
+
const has = (address) => {
|
|
20
21
|
return contracts[address] ? true : false
|
|
21
22
|
}
|
|
22
23
|
|
|
@@ -30,10 +31,9 @@ const get = (contract, method, params) => {
|
|
|
30
31
|
return result
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
const runContract = async ({decoded, hash, encoded}) => {
|
|
34
|
+
const runContract = async ({ decoded, hash, encoded }) => {
|
|
34
35
|
const params = decoded.constructorParameters
|
|
35
36
|
try {
|
|
36
|
-
|
|
37
37
|
const func = new Function(new TextDecoder().decode(decoded.contract))
|
|
38
38
|
const Contract = func()
|
|
39
39
|
|
|
@@ -41,13 +41,10 @@ const runContract = async ({decoded, hash, encoded}) => {
|
|
|
41
41
|
contracts[hash] = await new Contract(...params)
|
|
42
42
|
worker.postMessage({
|
|
43
43
|
type: 'debug',
|
|
44
|
-
messages: [
|
|
45
|
-
`loaded contract: ${hash}`,
|
|
46
|
-
`size: ${formatBytes(encoded.length)}`
|
|
47
|
-
]
|
|
44
|
+
messages: [`loaded contract: ${hash}`, `size: ${formatBytes(encoded.length)}`]
|
|
48
45
|
})
|
|
49
46
|
} catch (e) {
|
|
50
|
-
console.log(e)
|
|
47
|
+
console.log(e)
|
|
51
48
|
worker.postMessage({
|
|
52
49
|
type: 'contractError',
|
|
53
50
|
hash: await contractMessage.hash()
|
|
@@ -77,7 +74,6 @@ const execute = async (contract, method, params) => {
|
|
|
77
74
|
}
|
|
78
75
|
}
|
|
79
76
|
|
|
80
|
-
|
|
81
77
|
const createMessage = (sender = globalThis.peerid) => {
|
|
82
78
|
return {
|
|
83
79
|
sender,
|
|
@@ -86,23 +82,19 @@ const createMessage = (sender = globalThis.peerid) => {
|
|
|
86
82
|
}
|
|
87
83
|
}
|
|
88
84
|
|
|
89
|
-
const _init = async ({ contracts, blocks, peerid })=> {
|
|
90
|
-
|
|
85
|
+
const _init = async ({ contracts, blocks, peerid }) => {
|
|
91
86
|
globalThis.peerid = peerid
|
|
92
|
-
contracts = [
|
|
93
|
-
contractFactoryMessage,
|
|
94
|
-
nativeTokenMessage,
|
|
95
|
-
nameServiceMessage,
|
|
96
|
-
validatorsMessage
|
|
97
|
-
]
|
|
87
|
+
contracts = [contractFactoryMessage, nativeTokenMessage, nameServiceMessage, validatorsMessage]
|
|
98
88
|
|
|
99
|
-
contracts = await Promise.all(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
89
|
+
contracts = await Promise.all(
|
|
90
|
+
contracts.map(async (contract) => {
|
|
91
|
+
contract = await new ContractMessage(new Uint8Array(contract.split(',')))
|
|
92
|
+
await runContract({ decoded: contract.decoded, encoded: contract.encoded, hash: await contract.hash() })
|
|
93
|
+
return contract
|
|
94
|
+
})
|
|
95
|
+
)
|
|
104
96
|
|
|
105
|
-
let lastBlock = {hash: '0x0'}
|
|
97
|
+
let lastBlock = { hash: '0x0' }
|
|
106
98
|
|
|
107
99
|
if (blocks?.length > 0) {
|
|
108
100
|
let pre
|
|
@@ -110,15 +102,18 @@ const _init = async ({ contracts, blocks, peerid })=> {
|
|
|
110
102
|
try {
|
|
111
103
|
const importee = await import('url')
|
|
112
104
|
const url = importee.default
|
|
113
|
-
if (url) pre = url.fileURLToPath(new URL('.', import.meta.url))
|
|
105
|
+
if (url) pre = url.fileURLToPath(new URL('.', import.meta.url))
|
|
114
106
|
} catch {
|
|
115
107
|
// browser env
|
|
116
108
|
pre = './'
|
|
117
109
|
}
|
|
118
110
|
|
|
119
|
-
const _worker = await new EasyWorker(pre + '@leofcoin/workers/block-worker.js', {
|
|
111
|
+
const _worker = await new EasyWorker(pre + '@leofcoin/workers/block-worker.js', {
|
|
112
|
+
serialization: 'advanced',
|
|
113
|
+
type: 'module'
|
|
114
|
+
})
|
|
120
115
|
blocks = await _worker.once([blocks[blocks.length - 1]])
|
|
121
|
-
|
|
116
|
+
|
|
122
117
|
// blocks = unique(globalThis.blocks ? globalThis : [], blocks)
|
|
123
118
|
// for (let i = 0; i < blocks.length; i++) {
|
|
124
119
|
|
|
@@ -128,112 +123,108 @@ const _init = async ({ contracts, blocks, peerid })=> {
|
|
|
128
123
|
// if (!block.loaded) {
|
|
129
124
|
// const {from, to, method, params} = message;
|
|
130
125
|
// globalThis.msg = createMessage(from);
|
|
131
|
-
|
|
126
|
+
|
|
132
127
|
// await execute(to, method, params);
|
|
133
128
|
// block.loaded = true
|
|
134
129
|
// }
|
|
135
130
|
// }));
|
|
136
131
|
// }
|
|
137
|
-
|
|
132
|
+
|
|
138
133
|
if (blocks.length > 0) {
|
|
139
|
-
lastBlock = blocks[blocks.length - 1]
|
|
140
|
-
|
|
141
|
-
|
|
134
|
+
lastBlock = blocks[blocks.length - 1]
|
|
135
|
+
// todo blocks already loaded as blockmessage so use it!
|
|
136
|
+
lastBlock = await new BlockMessage(lastBlock)
|
|
137
|
+
|
|
142
138
|
lastBlock = {
|
|
143
139
|
...lastBlock.decoded,
|
|
144
140
|
hash: await lastBlock.hash()
|
|
145
|
-
}
|
|
141
|
+
}
|
|
146
142
|
}
|
|
147
143
|
globalThis.blocks = blocks
|
|
148
144
|
}
|
|
149
145
|
|
|
150
|
-
|
|
146
|
+
worker.postMessage({ type: 'machine-ready', lastBlock })
|
|
151
147
|
|
|
152
|
-
|
|
153
|
-
worker.postMessage({type: 'machine-ready', lastBlock});
|
|
154
|
-
|
|
155
148
|
// worker.postMessage({blocks});
|
|
156
149
|
}
|
|
157
150
|
|
|
158
151
|
const tasks = async (e) => {
|
|
159
152
|
const id = e.id
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
153
|
+
if (e.type === 'init') {
|
|
154
|
+
try {
|
|
155
|
+
await _init(e.input)
|
|
156
|
+
} catch (e) {
|
|
157
|
+
worker.postMessage({
|
|
158
|
+
type: 'initError',
|
|
159
|
+
message: e.message,
|
|
160
|
+
id
|
|
161
|
+
})
|
|
170
162
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
163
|
+
}
|
|
164
|
+
if (e.type === 'has') {
|
|
165
|
+
try {
|
|
166
|
+
const value = await has(e.input.address)
|
|
167
|
+
worker.postMessage({
|
|
168
|
+
type: 'response',
|
|
169
|
+
id,
|
|
170
|
+
value
|
|
171
|
+
})
|
|
172
|
+
} catch (error) {
|
|
173
|
+
worker.postMessage({
|
|
174
|
+
type: 'hasError',
|
|
175
|
+
message: error.message,
|
|
176
|
+
id
|
|
177
|
+
})
|
|
186
178
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
179
|
+
}
|
|
180
|
+
if (e.type === 'run') {
|
|
181
|
+
try {
|
|
182
|
+
const value = await runContract(e.input)
|
|
183
|
+
worker.postMessage({
|
|
184
|
+
type: 'response',
|
|
185
|
+
id,
|
|
186
|
+
value
|
|
187
|
+
})
|
|
188
|
+
} catch (e) {
|
|
189
|
+
worker.postMessage({
|
|
190
|
+
type: 'runError',
|
|
191
|
+
message: e.message,
|
|
192
|
+
id
|
|
193
|
+
})
|
|
202
194
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
|
|
195
|
+
}
|
|
196
|
+
if (e.type === 'get') {
|
|
197
|
+
try {
|
|
198
|
+
const value = await get(e.input.contract, e.input.method, e.input.params)
|
|
199
|
+
worker.postMessage({
|
|
200
|
+
type: 'response',
|
|
201
|
+
id,
|
|
202
|
+
value
|
|
203
|
+
})
|
|
204
|
+
} catch (e) {
|
|
205
|
+
worker.postMessage({
|
|
206
|
+
type: 'fetchError',
|
|
207
|
+
message: e.message,
|
|
208
|
+
id
|
|
209
|
+
})
|
|
219
210
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
211
|
+
}
|
|
212
|
+
if (e.type === 'execute') {
|
|
213
|
+
try {
|
|
214
|
+
const value = await execute(e.input.contract, e.input.method, e.input.params)
|
|
215
|
+
worker.postMessage({
|
|
216
|
+
type: 'response',
|
|
217
|
+
id,
|
|
218
|
+
value
|
|
219
|
+
})
|
|
220
|
+
} catch (e) {
|
|
221
|
+
worker.postMessage({
|
|
222
|
+
type: 'executionError',
|
|
223
|
+
message: e.message,
|
|
224
|
+
id
|
|
225
|
+
})
|
|
236
226
|
}
|
|
237
227
|
}
|
|
228
|
+
}
|
|
238
229
|
|
|
239
|
-
worker.onmessage(data => tasks(data))
|
|
230
|
+
worker.onmessage((data) => tasks(data))
|
package/src/pool-worker.js
CHANGED
|
@@ -5,15 +5,15 @@ globalThis.contracts = {}
|
|
|
5
5
|
|
|
6
6
|
const worker = new EasyWorker()
|
|
7
7
|
|
|
8
|
-
const tasks = async transactions => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
const tasks = async (transactions) => {
|
|
9
|
+
transactions = await Promise.all(
|
|
10
|
+
transactions.map(async (message) => {
|
|
11
|
+
message = await new TransactionMessage(message)
|
|
12
|
+
|
|
13
|
+
return { ...message.decoded, hash: await message.hash(), size: message.encoded.length }
|
|
14
|
+
})
|
|
15
|
+
)
|
|
15
16
|
|
|
16
17
|
worker.postMessage(transactions)
|
|
17
|
-
|
|
18
18
|
}
|
|
19
|
-
|
|
19
|
+
worker.onmessage((data) => tasks(data))
|
|
@@ -6,7 +6,7 @@ import EasyWorker from '@vandeurenglenn/easy-worker'
|
|
|
6
6
|
const worker = new EasyWorker()
|
|
7
7
|
|
|
8
8
|
worker.onmessage(async (transactions) => {
|
|
9
|
-
transactions = await Promise.all(transactions.map(async message => new TransactionMessage(message)))
|
|
9
|
+
transactions = await Promise.all(transactions.map(async (message) => new TransactionMessage(message)))
|
|
10
10
|
|
|
11
11
|
worker.postMessage(transactions)
|
|
12
|
-
})
|
|
12
|
+
})
|
package/src/workers.js
CHANGED
|
@@ -6,4 +6,4 @@ import * as machineWorker from './machine-worker.js'
|
|
|
6
6
|
export const TransactionWorker = transactionWorker
|
|
7
7
|
export const BlockWorker = blockWorker
|
|
8
8
|
export const PoolWorker = poolWorker
|
|
9
|
-
export const MachineWorker = machineWorker
|
|
9
|
+
export const MachineWorker = machineWorker
|