@leofcoin/workers 1.3.16 → 1.3.19

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