@leofcoin/chain 1.1.10 → 1.1.11
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/demo/chain.browser.js +33074 -27805
- package/demo/generate-account.browser.js +50 -0
- package/demo/index.html +8 -1
- package/demo/machine-worker.js +961 -960
- package/demo/messages.browser.js +328 -0
- package/demo/multi-wallet.browser.js +15 -0
- package/demo/node.browser.js +8005 -29603
- package/demo/peernet-swarm.browser.js +832 -0
- package/demo/storage.browser.js +3724 -0
- package/{dist/865.machine-worker.js → demo/workers/865.js} +0 -0
- package/demo/workers/block-worker.js +13200 -0
- package/demo/workers/machine-worker.js +2511 -6198
- package/demo/workers/pool-worker.js +8504 -0
- package/demo/workers/transaction-worker.js +8496 -0
- package/demo/workers/workers/865.js +10 -0
- package/demo/workers/workers/block-worker.js +13200 -0
- package/{dist → demo/workers/workers}/machine-worker.js +2654 -6367
- package/demo/workers/workers/pool-worker.js +8504 -0
- package/demo/workers/workers/transaction-worker.js +8496 -0
- package/dist/browser/workers/865.js +10 -0
- package/dist/browser/workers/block-worker.js +13200 -0
- package/dist/browser/workers/machine-worker.js +13930 -0
- package/dist/browser/workers/pool-worker.js +8504 -0
- package/dist/browser/workers/transaction-worker.js +8496 -0
- package/dist/chain.browser.js +33074 -27805
- package/dist/chain.js +45 -18
- package/dist/generate-account.browser.js +50 -0
- package/dist/messages.browser.js +328 -0
- package/dist/module/chain.js +45 -18
- package/dist/module/workers/block-worker.js +93 -0
- package/dist/module/workers/machine-worker.js +54 -28
- package/dist/module/workers/pool-worker.js +55 -0
- package/dist/module/workers/transaction-worker.js +47 -0
- package/dist/multi-wallet.browser.js +15 -0
- package/dist/node.browser.js +8005 -29603
- package/dist/pako.browser.js +6731 -0
- package/dist/peernet-swarm.browser.js +832 -0
- package/dist/storage.browser.js +3724 -0
- package/dist/workers/machine-worker.js +1 -1
- package/dist/wrtc.browser.js +28 -0
- package/package.json +2 -2
- package/rollup.config.js +30 -0
- package/src/chain.js +46 -16
- package/src/machine.js +1 -1
- package/test/chain.js +1 -1
- package/webpack.config.js +8 -3
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
3
|
+
import '@ethersproject/units';
|
|
4
|
+
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
5
|
+
|
|
6
|
+
var proto = `
|
|
7
|
+
message ValidatorMessage {
|
|
8
|
+
required string address = 1;
|
|
9
|
+
required string reward = 2;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
message Transaction {
|
|
13
|
+
required string hash = 1;
|
|
14
|
+
required uint64 timestamp = 2;
|
|
15
|
+
required string from = 3;
|
|
16
|
+
required string to = 4;
|
|
17
|
+
required uint64 nonce = 5;
|
|
18
|
+
required string method = 6;
|
|
19
|
+
repeated string params = 7;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
message BlockMessage {
|
|
23
|
+
required uint64 index = 1;
|
|
24
|
+
required string previousHash = 3;
|
|
25
|
+
required uint64 timestamp = 4;
|
|
26
|
+
required uint64 reward = 5;
|
|
27
|
+
required string fees = 6;
|
|
28
|
+
repeated Transaction transactions = 7;
|
|
29
|
+
repeated ValidatorMessage validators = 8;
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
class BlockMessage extends FormatInterface {
|
|
34
|
+
get keys() {
|
|
35
|
+
return ['index', 'previousHash', 'timestamp', 'reward', 'fees', 'transactions', 'validators']
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get messageName() {
|
|
39
|
+
return 'BlockMessage'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
constructor(buffer) {
|
|
43
|
+
const name = 'block-message';
|
|
44
|
+
super(buffer, proto, {name});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
49
|
+
|
|
50
|
+
const formatBytes = (bytes, decimals = 2) => {
|
|
51
|
+
if (bytes === 0) return '0 Bytes';
|
|
52
|
+
if (decimals < 0) decimals = 0;
|
|
53
|
+
|
|
54
|
+
const k = 1024;
|
|
55
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
56
|
+
|
|
57
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${byteFormats[i]}`
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const worker = new EasyWorker();
|
|
61
|
+
|
|
62
|
+
globalThis.BigNumber = BigNumber;
|
|
63
|
+
|
|
64
|
+
globalThis.peernet = globalThis.peernet || {};
|
|
65
|
+
globalThis.contracts = {};
|
|
66
|
+
|
|
67
|
+
const run = async (blocks) => {
|
|
68
|
+
blocks = await Promise.all(blocks.map(block => new BlockMessage(block)));
|
|
69
|
+
blocks = blocks.sort((a, b) => a.decoded.timestamp - b.decoded.timestamp);
|
|
70
|
+
|
|
71
|
+
blocks = await Promise.all(blocks.map(block => new Promise(async (resolve, reject) => {
|
|
72
|
+
// todo: tx worker or nah?
|
|
73
|
+
const size = block.encoded.length || block.encoded.byteLength;
|
|
74
|
+
console.log(`loaded block: ${await block.hash} @${block.decoded.index} ${formatBytes(size)}`);
|
|
75
|
+
resolve(block);
|
|
76
|
+
})));
|
|
77
|
+
return blocks
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const tasks = async blocks => {
|
|
81
|
+
globalThis.peernet.codecs = {
|
|
82
|
+
'block-message': {
|
|
83
|
+
codec: parseInt('626d', 16),
|
|
84
|
+
hashAlg: 'keccak-256'
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
blocks = await run(blocks);
|
|
89
|
+
worker.postMessage(blocks);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
worker.onmessage(data => tasks(data));
|
|
@@ -124,7 +124,7 @@ const runContract = async (contractMessage) => {
|
|
|
124
124
|
globalThis.msg = createMessage(contractMessage.decoded.creator);
|
|
125
125
|
// globalThis.msg = {sender: contractMessage.decoded.creator}
|
|
126
126
|
contracts[await contractMessage.hash] = await new Contract(...params);
|
|
127
|
-
|
|
127
|
+
worker.postMessage({
|
|
128
128
|
type: 'debug',
|
|
129
129
|
messages: [
|
|
130
130
|
`loaded contract: ${await contractMessage.hash}`,
|
|
@@ -133,7 +133,7 @@ const runContract = async (contractMessage) => {
|
|
|
133
133
|
});
|
|
134
134
|
} catch (e) {
|
|
135
135
|
console.log(e);
|
|
136
|
-
|
|
136
|
+
worker.postMessage({
|
|
137
137
|
type: 'contractError',
|
|
138
138
|
hash: await contractMessage.hash
|
|
139
139
|
});
|
|
@@ -196,29 +196,36 @@ const _init = async ({ contracts, blocks, peerid })=> {
|
|
|
196
196
|
return contract
|
|
197
197
|
}));
|
|
198
198
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
await
|
|
204
|
-
|
|
205
|
-
|
|
199
|
+
let lastBlock = {hash: '0x0'};
|
|
200
|
+
|
|
201
|
+
if (blocks?.length > 0) {
|
|
202
|
+
const _worker = await new EasyWorker(join(__dirname, './block-worker.js'), {serialization: 'advanced', type: 'module' });
|
|
203
|
+
blocks = await _worker.once(blocks);
|
|
204
|
+
|
|
205
|
+
for (const block of blocks) {
|
|
206
|
+
await Promise.all(block.decoded.transactions.map(async message => {
|
|
207
|
+
const {from, to, method, params} = message;
|
|
208
|
+
globalThis.msg = createMessage(from);
|
|
209
|
+
|
|
210
|
+
await execute(to, method, params);
|
|
211
|
+
}));
|
|
212
|
+
}
|
|
206
213
|
|
|
207
|
-
|
|
208
|
-
|
|
214
|
+
if (blocks.length > 0) {
|
|
215
|
+
lastBlock = blocks[blocks.length - 1].decoded;
|
|
216
|
+
lastBlock = await new BlockMessage(lastBlock);
|
|
217
|
+
|
|
218
|
+
lastBlock = {
|
|
219
|
+
...lastBlock.decoded,
|
|
220
|
+
hash: await lastBlock.hash
|
|
221
|
+
};
|
|
222
|
+
}
|
|
209
223
|
}
|
|
210
224
|
|
|
211
|
-
|
|
212
|
-
if (blocks.length > 0) {
|
|
213
|
-
lastBlock = blocks[blocks.length - 1].decoded;
|
|
214
|
-
lastBlock = await new BlockMessage(lastBlock);
|
|
225
|
+
|
|
215
226
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
hash: await lastBlock.hash
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
worker.postMessage({type: 'machine-ready', lastBlock });
|
|
227
|
+
|
|
228
|
+
worker.postMessage({type: 'machine-ready', lastBlock});
|
|
222
229
|
_worker.terminate();
|
|
223
230
|
|
|
224
231
|
worker.postMessage(blocks);
|
|
@@ -226,14 +233,33 @@ const _init = async ({ contracts, blocks, peerid })=> {
|
|
|
226
233
|
|
|
227
234
|
const tasks = async (e) => {
|
|
228
235
|
const id = e.id;
|
|
229
|
-
if (e.type === 'init')
|
|
236
|
+
if (e.type === 'init') {
|
|
237
|
+
try {
|
|
238
|
+
await _init(e.input);
|
|
239
|
+
} catch (e) {
|
|
240
|
+
worker.postMessage({
|
|
241
|
+
type: 'initError',
|
|
242
|
+
message: e.message,
|
|
243
|
+
id
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
230
247
|
if (e.type === 'get') {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
248
|
+
try {
|
|
249
|
+
const value = await get(e.input.contract, e.input.method, e.input.params);
|
|
250
|
+
worker.postMessage({
|
|
251
|
+
type: 'response',
|
|
252
|
+
id,
|
|
253
|
+
value
|
|
254
|
+
});
|
|
255
|
+
} catch (e) {
|
|
256
|
+
worker.postMessage({
|
|
257
|
+
type: 'fetchError',
|
|
258
|
+
message: e.message,
|
|
259
|
+
id
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
237
263
|
}
|
|
238
264
|
if (e.type === 'execute') {
|
|
239
265
|
try {
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
|
+
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
3
|
+
|
|
4
|
+
var proto = `
|
|
5
|
+
|
|
6
|
+
message TransactionMessage {
|
|
7
|
+
required uint64 timestamp = 1;
|
|
8
|
+
required string from = 2;
|
|
9
|
+
required string to = 3;
|
|
10
|
+
required uint64 nonce = 4;
|
|
11
|
+
required string method = 5;
|
|
12
|
+
repeated string params = 6;
|
|
13
|
+
required string signature = 7;
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
class TransactionMessage extends FormatInterface {
|
|
18
|
+
get keys() {
|
|
19
|
+
return ['timestamp', 'from', 'to', 'nonce', 'method', 'params', 'signature']
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get messageName() {
|
|
23
|
+
return 'TransactionMessage'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
constructor(buffer) {
|
|
27
|
+
const name = 'transaction-message';
|
|
28
|
+
super(buffer, proto, {name});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
globalThis.peernet = globalThis.peernet || {};
|
|
33
|
+
globalThis.contracts = {};
|
|
34
|
+
|
|
35
|
+
const worker = new EasyWorker();
|
|
36
|
+
|
|
37
|
+
const tasks = async transactions => {
|
|
38
|
+
|
|
39
|
+
globalThis.peernet.codecs = {
|
|
40
|
+
'transaction-message': {
|
|
41
|
+
codec: parseInt('746d', 16),
|
|
42
|
+
hashAlg: 'keccak-256'
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
transactions = await Promise.all(transactions.map(async message => {
|
|
47
|
+
message = await new TransactionMessage(message);
|
|
48
|
+
|
|
49
|
+
return {...message.decoded, hash: await message.hash, size: message.encoded.length}
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
worker.postMessage(transactions);
|
|
53
|
+
|
|
54
|
+
};
|
|
55
|
+
worker.onmessage(data => tasks(data));
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
|
+
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
3
|
+
|
|
4
|
+
var proto = `
|
|
5
|
+
|
|
6
|
+
message TransactionMessage {
|
|
7
|
+
required uint64 timestamp = 1;
|
|
8
|
+
required string from = 2;
|
|
9
|
+
required string to = 3;
|
|
10
|
+
required uint64 nonce = 4;
|
|
11
|
+
required string method = 5;
|
|
12
|
+
repeated string params = 6;
|
|
13
|
+
required string signature = 7;
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
class TransactionMessage extends FormatInterface {
|
|
18
|
+
get keys() {
|
|
19
|
+
return ['timestamp', 'from', 'to', 'nonce', 'method', 'params', 'signature']
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get messageName() {
|
|
23
|
+
return 'TransactionMessage'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
constructor(buffer) {
|
|
27
|
+
const name = 'transaction-message';
|
|
28
|
+
super(buffer, proto, {name});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
globalThis.peernet = globalThis.peernet || {};
|
|
33
|
+
globalThis.contracts = {};
|
|
34
|
+
|
|
35
|
+
const worker = new EasyWorker();
|
|
36
|
+
|
|
37
|
+
worker.onmessage(async (transactions) => {
|
|
38
|
+
globalThis.peernet.codecs = {
|
|
39
|
+
'transaction-message': {
|
|
40
|
+
codec: parseInt('746d', 16),
|
|
41
|
+
hashAlg: 'keccak-256'
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
transactions = await Promise.all(transactions.map(async message => new TransactionMessage(message)));
|
|
45
|
+
|
|
46
|
+
worker.postMessage(transactions);
|
|
47
|
+
});
|