@leofcoin/chain 1.4.82 → 1.4.84
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/exports/browser/chain.js +72 -52
- package/exports/browser/client-c8558012-c8558012.js +10727 -0
- package/exports/browser/{index-6311966e-b5f647d8.js → index-ed6cbdf7-eb75dc7a.js} +2 -2
- package/exports/browser/{index-c3f4012c.js → index-f4429da1.js} +200 -210
- package/exports/browser/{messages-54aa3cad-5309745b.js → messages-35d069e1-9413ac70.js} +2 -2
- package/exports/browser/{node-browser-3cbee72b.js → node-browser-504c804a.js} +509 -600
- package/exports/browser/node-browser.js +2 -2
- package/exports/browser/workers/block-worker.js +200 -210
- package/exports/browser/workers/machine-worker.js +219 -212
- package/exports/chain.js +49 -35
- package/exports/types/config/main.d.ts +5 -0
- package/exports/{typings → types}/config/protocol.d.ts +1 -1
- package/exports/{typings → types}/contract.d.ts +1 -1
- package/exports/{typings → types}/fee/config.d.ts +1 -1
- package/exports/{typings → types}/machine.d.ts +8 -7
- package/exports/{typings → types}/node-browser.d.ts +1 -1
- package/exports/{typings → types}/node.d.ts +3 -3
- package/exports/workers/block-worker.js +6035 -0
- package/exports/workers/machine-worker.js +6298 -0
- package/package.json +18 -10
- package/exports/browser/client-6072af1a-6072af1a.js +0 -41092
- package/exports/typings/config/main.d.ts +0 -5
- /package/exports/{typings → types}/config/config.d.ts +0 -0
- /package/exports/{typings → types}/jobs/jobber.d.ts +0 -0
- /package/exports/{typings → types}/machine-state.d.ts +0 -0
- /package/exports/{typings → types}/protocol.d.ts +0 -0
- /package/exports/{typings → types}/sync-controller.d.ts +0 -0
- /package/exports/{typings → types}/transaction.d.ts +0 -0
- /package/exports/{typings → types}/typer.d.ts +0 -0
- /package/exports/{typings → types}/types.d.ts +0 -0
package/exports/chain.js
CHANGED
|
@@ -158,7 +158,7 @@ class Transaction extends Protocol {
|
|
|
158
158
|
}
|
|
159
159
|
async sendTransaction(message) {
|
|
160
160
|
if (!this.isTransactionMessage(message))
|
|
161
|
-
message = new TransactionMessage(message);
|
|
161
|
+
message = await new TransactionMessage(message);
|
|
162
162
|
if (!message.decoded.signature)
|
|
163
163
|
throw new Error(`transaction not signed`);
|
|
164
164
|
if (message.decoded.nonce === undefined)
|
|
@@ -254,13 +254,16 @@ class Contract extends Transaction {
|
|
|
254
254
|
|
|
255
255
|
// import State from './state'
|
|
256
256
|
class Machine {
|
|
257
|
+
worker;
|
|
257
258
|
#contracts = {};
|
|
258
259
|
#nonces = {};
|
|
259
260
|
lastBlock = { index: 0, hash: '0x0', previousHash: '0x0' };
|
|
260
261
|
constructor(blocks) {
|
|
262
|
+
// @ts-ignore
|
|
261
263
|
return this.#init(blocks);
|
|
262
264
|
}
|
|
263
|
-
|
|
265
|
+
// @ts-ignore
|
|
266
|
+
#createMessage(sender = globalThis.peernet.selectedAccount) {
|
|
264
267
|
return {
|
|
265
268
|
sender,
|
|
266
269
|
call: this.execute,
|
|
@@ -271,6 +274,7 @@ class Machine {
|
|
|
271
274
|
switch (data.type) {
|
|
272
275
|
case 'contractError': {
|
|
273
276
|
console.warn(`removing contract ${await data.hash()}`);
|
|
277
|
+
// @ts-ignore
|
|
274
278
|
await contractStore.delete(await data.hash());
|
|
275
279
|
break;
|
|
276
280
|
}
|
|
@@ -285,7 +289,7 @@ class Machine {
|
|
|
285
289
|
}
|
|
286
290
|
case 'debug': {
|
|
287
291
|
for (const message of data.messages)
|
|
288
|
-
debug(message);
|
|
292
|
+
globalThis.debug(message);
|
|
289
293
|
break;
|
|
290
294
|
}
|
|
291
295
|
case 'machine-ready': {
|
|
@@ -306,20 +310,32 @@ class Machine {
|
|
|
306
310
|
resolve(this);
|
|
307
311
|
};
|
|
308
312
|
pubsub.subscribe('machine.ready', machineReady);
|
|
309
|
-
|
|
313
|
+
let pre;
|
|
314
|
+
try {
|
|
315
|
+
const importee = await import('url');
|
|
316
|
+
const url = importee.default;
|
|
317
|
+
if (url)
|
|
318
|
+
pre = url.fileURLToPath(new URL('.', import.meta.url));
|
|
319
|
+
}
|
|
320
|
+
catch {
|
|
321
|
+
// browser env
|
|
322
|
+
pre = './';
|
|
323
|
+
}
|
|
324
|
+
this.worker = await new EasyWorker(pre + 'workers/machine-worker.js', { serialization: 'advanced', type: 'module' });
|
|
310
325
|
this.worker.onmessage(this.#onmessage.bind(this));
|
|
311
326
|
// const blocks = await blockStore.values()
|
|
312
327
|
const contracts = await Promise.all([
|
|
313
|
-
contractStore.get(contractFactory),
|
|
314
|
-
contractStore.get(nativeToken),
|
|
315
|
-
contractStore.get(validators),
|
|
316
|
-
contractStore.get(nameService)
|
|
328
|
+
globalThis.contractStore.get(contractFactory),
|
|
329
|
+
globalThis.contractStore.get(nativeToken),
|
|
330
|
+
globalThis.contractStore.get(validators),
|
|
331
|
+
globalThis.contractStore.get(nameService)
|
|
317
332
|
]);
|
|
318
333
|
const message = {
|
|
319
334
|
type: 'init',
|
|
320
335
|
input: {
|
|
321
336
|
contracts,
|
|
322
337
|
blocks,
|
|
338
|
+
// @ts-ignore
|
|
323
339
|
peerid: peernet.peerId
|
|
324
340
|
}
|
|
325
341
|
};
|
|
@@ -329,6 +345,7 @@ class Machine {
|
|
|
329
345
|
async #runContract(contractMessage) {
|
|
330
346
|
const hash = await contractMessage.hash();
|
|
331
347
|
return new Promise((resolve, reject) => {
|
|
348
|
+
// @ts-ignore
|
|
332
349
|
const id = randombytes(20).toString('hex');
|
|
333
350
|
const onmessage = message => {
|
|
334
351
|
pubsub.unsubscribe(id, onmessage);
|
|
@@ -362,13 +379,13 @@ class Machine {
|
|
|
362
379
|
if (await this.has(parameters[0]))
|
|
363
380
|
throw new Error(`duplicate contract @${parameters[0]}`);
|
|
364
381
|
let message;
|
|
365
|
-
if (!await contractStore.has(parameters[0])) {
|
|
382
|
+
if (!await globalThis.contractStore.has(parameters[0])) {
|
|
366
383
|
message = await peernet.get(parameters[0], 'contract');
|
|
367
384
|
message = await new ContractMessage(message);
|
|
368
|
-
await contractStore.put(await message.hash(), message.encoded);
|
|
385
|
+
await globalThis.contractStore.put(await message.hash(), message.encoded);
|
|
369
386
|
}
|
|
370
387
|
if (!message) {
|
|
371
|
-
message = await contractStore.get(parameters[0]);
|
|
388
|
+
message = await globalThis.contractStore.get(parameters[0]);
|
|
372
389
|
message = await new ContractMessage(message);
|
|
373
390
|
}
|
|
374
391
|
if (!await this.has(await message.hash()))
|
|
@@ -379,6 +396,7 @@ class Machine {
|
|
|
379
396
|
throw new Error(`contract deployment failed for ${parameters[0]}\n${error.message}`);
|
|
380
397
|
}
|
|
381
398
|
return new Promise((resolve, reject) => {
|
|
399
|
+
// @ts-ignore
|
|
382
400
|
const id = randombytes(20).toString('hex');
|
|
383
401
|
const onmessage = message => {
|
|
384
402
|
pubsub.unsubscribe(id, onmessage);
|
|
@@ -420,6 +438,7 @@ class Machine {
|
|
|
420
438
|
}
|
|
421
439
|
async has(address) {
|
|
422
440
|
return new Promise((resolve, reject) => {
|
|
441
|
+
// @ts-ignore
|
|
423
442
|
const id = randombytes(20).toString('hex');
|
|
424
443
|
const onmessage = message => {
|
|
425
444
|
pubsub.unsubscribe(id, onmessage);
|
|
@@ -439,14 +458,14 @@ class Machine {
|
|
|
439
458
|
});
|
|
440
459
|
}
|
|
441
460
|
async delete(hash) {
|
|
442
|
-
return contractStore.delete(hash);
|
|
461
|
+
return globalThis.contractStore.delete(hash);
|
|
443
462
|
}
|
|
444
463
|
/**
|
|
445
464
|
*
|
|
446
465
|
* @returns Promise
|
|
447
466
|
*/
|
|
448
467
|
async deleteAll() {
|
|
449
|
-
let hashes = await contractStore.get();
|
|
468
|
+
let hashes = await globalThis.contractStore.get();
|
|
450
469
|
hashes = Object.keys(hashes).map(hash => this.delete(hash));
|
|
451
470
|
return Promise.all(hashes);
|
|
452
471
|
}
|
|
@@ -621,7 +640,7 @@ class State extends Contract {
|
|
|
621
640
|
if (block !== undefined) {
|
|
622
641
|
block = await new BlockMessage(block);
|
|
623
642
|
const { index } = block.decoded;
|
|
624
|
-
if (this.#blocks[index] && this.#blocks[index].hash !== block.hash)
|
|
643
|
+
if (this.#blocks[index - 1] && this.#blocks[index - 1].hash !== block.hash)
|
|
625
644
|
throw `invalid block ${hash} @${index}`;
|
|
626
645
|
if (!await globalThis.peernet.has(hash, 'block'))
|
|
627
646
|
await globalThis.peernet.put(hash, block.encoded, 'block');
|
|
@@ -630,9 +649,9 @@ class State extends Contract {
|
|
|
630
649
|
}
|
|
631
650
|
async #resolveBlock(hash) {
|
|
632
651
|
let index = this.#blockHashMap.get(hash);
|
|
633
|
-
if (this.#blocks[index]) {
|
|
634
|
-
if (this.#blocks[index].previousHash !== '0x0') {
|
|
635
|
-
return this.resolveBlock(this.#blocks[index].previousHash);
|
|
652
|
+
if (this.#blocks[index - 1]) {
|
|
653
|
+
if (this.#blocks[index - 1].previousHash !== '0x0') {
|
|
654
|
+
return this.resolveBlock(this.#blocks[index - 1].previousHash);
|
|
636
655
|
}
|
|
637
656
|
else {
|
|
638
657
|
return;
|
|
@@ -643,11 +662,11 @@ class State extends Contract {
|
|
|
643
662
|
index = block.decoded.index;
|
|
644
663
|
const size = block.encoded.length > 0 ? block.encoded.length : block.encoded.byteLength;
|
|
645
664
|
this.#totalSize += size;
|
|
646
|
-
this.#blocks[index] = { hash, ...block.decoded };
|
|
665
|
+
this.#blocks[index - 1] = { hash, ...block.decoded };
|
|
647
666
|
this.#blockHashMap.set(hash, index);
|
|
648
667
|
globalThis.debug(`resolved block: ${hash} @${index} ${formatBytes(size)}`);
|
|
649
668
|
globalThis.pubsub.publish('block-resolved', { hash, index });
|
|
650
|
-
this.#lastResolved = this.#blocks[index];
|
|
669
|
+
this.#lastResolved = this.#blocks[index - 1];
|
|
651
670
|
this.#lastResolvedTime = Date.now();
|
|
652
671
|
}
|
|
653
672
|
catch (error) {
|
|
@@ -757,12 +776,12 @@ class State extends Contract {
|
|
|
757
776
|
if (!this.#lastBlock || Number(this.#lastBlock.index) < Number(lastBlock.index)) {
|
|
758
777
|
// TODO: check if valid
|
|
759
778
|
const localIndex = this.#lastBlock ? this.lastBlock.index : 0;
|
|
760
|
-
const index = lastBlock.index;
|
|
779
|
+
const index = lastBlock.index - 1;
|
|
761
780
|
await this.resolveBlock(lastBlock.hash);
|
|
762
781
|
console.log('ok');
|
|
763
782
|
let blocksSynced = localIndex > 0 ? (localIndex > index ? localIndex - index : index - localIndex) : index;
|
|
764
783
|
globalThis.debug(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`);
|
|
765
|
-
const start = (this.#blocks.length - blocksSynced)
|
|
784
|
+
const start = (this.#blocks.length - blocksSynced);
|
|
766
785
|
if (this.#machine)
|
|
767
786
|
await this.#loadBlocks(this.blocks.slice(start));
|
|
768
787
|
await this.updateState(new BlockMessage(this.#blocks[this.#blocks.length - 1]));
|
|
@@ -847,7 +866,7 @@ class State extends Contract {
|
|
|
847
866
|
return false;
|
|
848
867
|
}
|
|
849
868
|
}
|
|
850
|
-
this.#blocks[block.index].loaded = true;
|
|
869
|
+
this.#blocks[block.index - 1].loaded = true;
|
|
851
870
|
globalThis.debug(`loaded block: ${block.hash} @${block.index}`);
|
|
852
871
|
globalThis.pubsub.publish('block-loaded', { ...block });
|
|
853
872
|
}
|
|
@@ -902,7 +921,7 @@ class Chain extends State {
|
|
|
902
921
|
#state;
|
|
903
922
|
#slotTime = 10000;
|
|
904
923
|
id;
|
|
905
|
-
utils;
|
|
924
|
+
utils = {};
|
|
906
925
|
/** {Address[]} */
|
|
907
926
|
#validators = [];
|
|
908
927
|
/** {Boolean} */
|
|
@@ -912,6 +931,7 @@ class Chain extends State {
|
|
|
912
931
|
#jail = [];
|
|
913
932
|
constructor() {
|
|
914
933
|
super();
|
|
934
|
+
// @ts-ignore
|
|
915
935
|
return this.#init();
|
|
916
936
|
}
|
|
917
937
|
get nativeToken() {
|
|
@@ -939,7 +959,6 @@ class Chain extends State {
|
|
|
939
959
|
}
|
|
940
960
|
catch (error) {
|
|
941
961
|
console.error(error);
|
|
942
|
-
console.log('ttttt');
|
|
943
962
|
}
|
|
944
963
|
const end = Date.now();
|
|
945
964
|
console.log(((end - start) / 1000) + ' s');
|
|
@@ -963,7 +982,6 @@ class Chain extends State {
|
|
|
963
982
|
message: nameServiceMessage
|
|
964
983
|
}];
|
|
965
984
|
await Promise.all(contracts.map(async ({ address, message }) => {
|
|
966
|
-
// console.log({message});
|
|
967
985
|
message = await new ContractMessage(Uint8Array.from(message.split(',').map(string => Number(string))));
|
|
968
986
|
await globalThis.contractStore.put(address, message.encoded);
|
|
969
987
|
}));
|
|
@@ -1033,7 +1051,7 @@ class Chain extends State {
|
|
|
1033
1051
|
this.#jail.push(validatorInfo.address);
|
|
1034
1052
|
}
|
|
1035
1053
|
#addTransaction(message) {
|
|
1036
|
-
console.log(message);
|
|
1054
|
+
console.log({ message });
|
|
1037
1055
|
}
|
|
1038
1056
|
async #prepareRequest(request) {
|
|
1039
1057
|
let node = await new globalThis.peernet.protos['peernet-request']({ request });
|
|
@@ -1051,13 +1069,11 @@ class Chain extends State {
|
|
|
1051
1069
|
const lastBlock = await this.#makeRequest(peer, 'lastBlock');
|
|
1052
1070
|
let transactionsInPool = await this.#makeRequest(peer, 'transactionPool');
|
|
1053
1071
|
const transactions = await globalThis.transactionPoolStore.keys();
|
|
1054
|
-
console.log({ transactionsInPool });
|
|
1055
1072
|
const transactionsToGet = [];
|
|
1056
1073
|
for (const key of transactionsInPool) {
|
|
1057
1074
|
if (!transactions.includes(key) && !ignorelist.includes(key))
|
|
1058
1075
|
transactionsToGet.push(transactionPoolStore.put(key, (await peernet.get(key, 'transaction'))));
|
|
1059
1076
|
}
|
|
1060
|
-
console.log(await transactionPoolStore.keys());
|
|
1061
1077
|
await Promise.all(transactionsToGet);
|
|
1062
1078
|
if (Object.keys(lastBlock).length > 0) {
|
|
1063
1079
|
if (!this.lastBlock || !this.blocks[this.blocks.length - 1]?.loaded || lastBlock && lastBlock.index > this.lastBlock?.index || !this.loaded && !this.resolving) {
|
|
@@ -1132,7 +1148,6 @@ class Chain extends State {
|
|
|
1132
1148
|
console.log(error.hash);
|
|
1133
1149
|
console.log('errrrr');
|
|
1134
1150
|
await transactionPoolStore.delete(error.hash);
|
|
1135
|
-
console.log({ e: error });
|
|
1136
1151
|
}
|
|
1137
1152
|
}
|
|
1138
1153
|
async participate(address) {
|
|
@@ -1183,7 +1198,6 @@ class Chain extends State {
|
|
|
1183
1198
|
// exclude failing tx
|
|
1184
1199
|
transactions = await this.promiseTransactions(transactions);
|
|
1185
1200
|
transactions = transactions.sort((a, b) => a.nonce - b.nonce);
|
|
1186
|
-
console.log({ transactions });
|
|
1187
1201
|
for (let transaction of transactions) {
|
|
1188
1202
|
const hash = await transaction.hash();
|
|
1189
1203
|
const doubleTransactions = [];
|
|
@@ -1202,7 +1216,6 @@ class Chain extends State {
|
|
|
1202
1216
|
// if (timestamp + this.#slotTime > Date.now()) {
|
|
1203
1217
|
try {
|
|
1204
1218
|
const result = await this.#executeTransaction({ ...transaction.decoded, hash });
|
|
1205
|
-
console.log({ result });
|
|
1206
1219
|
block.transactions.push(transaction);
|
|
1207
1220
|
block.fees = block.fees.add(await calculateFee(transaction.decoded));
|
|
1208
1221
|
await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
|
|
@@ -1296,7 +1309,7 @@ class Chain extends State {
|
|
|
1296
1309
|
// transactionStore.put(message.hash, message.encoded)
|
|
1297
1310
|
}
|
|
1298
1311
|
async #sendTransaction(transaction) {
|
|
1299
|
-
transaction = await new TransactionMessage(transaction.encoded);
|
|
1312
|
+
transaction = await new TransactionMessage(transaction.encoded || transaction);
|
|
1300
1313
|
const hash = await transaction.hash();
|
|
1301
1314
|
try {
|
|
1302
1315
|
const has = await globalThis.transactionPoolStore.has(hash);
|
|
@@ -1319,9 +1332,10 @@ class Chain extends State {
|
|
|
1319
1332
|
* error is thrown on error so undefined data doesn't mean there is an error...
|
|
1320
1333
|
**/
|
|
1321
1334
|
async sendTransaction(transaction) {
|
|
1322
|
-
const
|
|
1323
|
-
|
|
1324
|
-
|
|
1335
|
+
const transactionMessage = await new TransactionMessage({ ...transaction });
|
|
1336
|
+
const event = await super.sendTransaction(transactionMessage);
|
|
1337
|
+
this.#sendTransaction(transactionMessage.encoded);
|
|
1338
|
+
globalThis.peernet.publish('send-transaction', transactionMessage.encoded);
|
|
1325
1339
|
return event;
|
|
1326
1340
|
}
|
|
1327
1341
|
async addContract(transaction, contractMessage) {
|
|
@@ -10,7 +10,7 @@ export default class Contract extends Transaction {
|
|
|
10
10
|
* @param {Array} constructorParameters
|
|
11
11
|
* @returns lib.createContractMessage
|
|
12
12
|
*/
|
|
13
|
-
createContractMessage(creator: Address, contract: string, constructorParameters?: any[]): Promise<
|
|
13
|
+
createContractMessage(creator: Address, contract: string, constructorParameters?: any[]): Promise<any>;
|
|
14
14
|
/**
|
|
15
15
|
*
|
|
16
16
|
* @param {Address} creator
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
1
2
|
export default class Machine {
|
|
2
|
-
|
|
3
|
+
#private;
|
|
4
|
+
worker: EasyWorker;
|
|
3
5
|
lastBlock: {
|
|
4
6
|
index: number;
|
|
5
7
|
hash: string;
|
|
6
8
|
previousHash: string;
|
|
7
9
|
};
|
|
8
|
-
|
|
10
|
+
constructor(blocks: any);
|
|
9
11
|
/**
|
|
10
12
|
*
|
|
11
13
|
* @param {Address} contract
|
|
@@ -13,14 +15,13 @@ export default class Machine {
|
|
|
13
15
|
* @param {Array} parameters
|
|
14
16
|
* @returns Promise<message>
|
|
15
17
|
*/
|
|
16
|
-
execute(contract:
|
|
17
|
-
get(contract: any, method: any, parameters
|
|
18
|
-
has(address: any): Promise<
|
|
18
|
+
execute(contract: any, method: any, parameters: any): Promise<unknown>;
|
|
19
|
+
get(contract: any, method: any, parameters?: any): Promise<unknown>;
|
|
20
|
+
has(address: any): Promise<unknown>;
|
|
19
21
|
delete(hash: any): Promise<any>;
|
|
20
22
|
/**
|
|
21
23
|
*
|
|
22
24
|
* @returns Promise
|
|
23
25
|
*/
|
|
24
|
-
deleteAll(): Promise<any>;
|
|
25
|
-
#private;
|
|
26
|
+
deleteAll(): Promise<any[]>;
|
|
26
27
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export default class Node {
|
|
2
2
|
#private;
|
|
3
|
-
constructor(config: any, password:
|
|
3
|
+
constructor(config: any, password: any);
|
|
4
4
|
_init(config: {
|
|
5
5
|
network: string;
|
|
6
6
|
networkName: string;
|
|
7
7
|
networkVersion: string;
|
|
8
|
-
stars:
|
|
8
|
+
stars: string[];
|
|
9
9
|
autoStart: boolean;
|
|
10
|
-
}, password:
|
|
10
|
+
}, password: any): Promise<this>;
|
|
11
11
|
}
|