@leofcoin/chain 1.5.10 → 1.5.12
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/browser-store.js +4 -3
- package/exports/browser/chain.js +55 -45
- package/exports/browser/{index-9c85cd32-ab3cf058.js → index-329e0324-67daaccb.js} +2 -2
- package/exports/browser/{index-b32f624f.js → index-e8f03811.js} +155 -144
- package/exports/browser/{messages-b66f5393-8ba2fe1a.js → messages-000b7f84-fd07a3bf.js} +2 -2
- package/exports/browser/{node-browser-3364f1f2.js → node-browser-505084de.js} +230 -252
- package/exports/browser/node-browser.js +2 -2
- package/exports/browser/workers/block-worker.js +159 -148
- package/exports/browser/workers/machine-worker.js +159 -148
- package/exports/chain.d.ts +72 -0
- package/exports/chain.js +41 -28
- package/exports/contract.d.ts +45 -0
- package/exports/{types/machine.d.ts → machine.d.ts} +3 -3
- package/exports/node.js +1 -0
- package/exports/{types/state.d.ts → state.d.ts} +2 -2
- package/exports/{types/transaction.d.ts → transaction.d.ts} +2 -1
- package/exports/{types/typer.d.ts → typer.d.ts} +1 -1
- package/exports/workers/block-worker.js +159 -148
- package/exports/workers/machine-worker.js +159 -148
- package/package.json +18 -3
- package/exports/types/contract.d.ts +0 -31
- /package/exports/{types/config → config}/config.d.ts +0 -0
- /package/exports/{types/config → config}/main.d.ts +0 -0
- /package/exports/{types/config → config}/protocol.d.ts +0 -0
- /package/exports/{types/consensus → consensus}/consensus.d.ts +0 -0
- /package/exports/{types/consensus → consensus}/helpers/sort-transactions.d.ts +0 -0
- /package/exports/{types/fee → fee}/config.d.ts +0 -0
- /package/exports/{types/jobs → jobs}/jobber.d.ts +0 -0
- /package/exports/{types/machine-state.d.ts → machine-state.d.ts} +0 -0
- /package/exports/{types/node-browser.d.ts → node-browser.d.ts} +0 -0
- /package/exports/{types/node.d.ts → node.d.ts} +0 -0
- /package/exports/{types/protocol.d.ts → protocol.d.ts} +0 -0
- /package/exports/{types/sync-controller.d.ts → sync-controller.d.ts} +0 -0
- /package/exports/{types/types.d.ts → types.d.ts} +0 -0
package/exports/chain.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { formatBytes, BigNumber, formatUnits, parseUnits } from '@leofcoin/utils';
|
|
2
2
|
import { TransactionMessage, BlockMessage, ContractMessage, BWMessage, BWRequestMessage } from '@leofcoin/messages';
|
|
3
3
|
import addresses, { contractFactory, nativeToken, validators, nameService } from '@leofcoin/addresses';
|
|
4
|
-
import { calculateFee, createContractMessage, contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage
|
|
4
|
+
import { calculateFee, createContractMessage, signTransaction, contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage } from '@leofcoin/lib';
|
|
5
5
|
import { randombytes } from '@leofcoin/crypto';
|
|
6
6
|
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
7
7
|
import { isResolveError, ResolveError } from '@leofcoin/errors';
|
|
@@ -83,7 +83,9 @@ class Transaction extends Protocol {
|
|
|
83
83
|
transactions = await this.promiseTransactions(transactions);
|
|
84
84
|
transactions = transactions.filter(tx => tx.decoded.from === address);
|
|
85
85
|
transactions = await this.promiseTransactionsContent(transactions);
|
|
86
|
+
// @ts-ignore
|
|
86
87
|
if (this.lastBlock?.hash && transactions.length === 0 && this.lastBlock.hash !== '0x0') {
|
|
88
|
+
// @ts-ignore
|
|
87
89
|
let block = await peernet.get(this.lastBlock.hash, 'block');
|
|
88
90
|
block = await new BlockMessage(block);
|
|
89
91
|
// for (let tx of block.decoded?.transactions) {
|
|
@@ -147,6 +149,9 @@ class Transaction extends Protocol {
|
|
|
147
149
|
return true;
|
|
148
150
|
return false;
|
|
149
151
|
}
|
|
152
|
+
async createTransactionMessage(transaction, signature) {
|
|
153
|
+
return new TransactionMessage({ ...transaction, signature });
|
|
154
|
+
}
|
|
150
155
|
async createTransaction(transaction) {
|
|
151
156
|
return {
|
|
152
157
|
from: transaction.from,
|
|
@@ -239,17 +244,25 @@ class Contract extends Transaction {
|
|
|
239
244
|
* @param {Array} parameters
|
|
240
245
|
* @returns
|
|
241
246
|
*/
|
|
242
|
-
async deployContract(contract, constructorParameters = []) {
|
|
243
|
-
const message = await createContractMessage(
|
|
247
|
+
async deployContract(signer, contract, constructorParameters = []) {
|
|
248
|
+
const message = await createContractMessage(await signer.address, contract, constructorParameters);
|
|
249
|
+
return this.deployContractMessage(signer, message);
|
|
250
|
+
}
|
|
251
|
+
async deployContractMessage(signer, message) {
|
|
244
252
|
try {
|
|
245
|
-
await contractStore.put(await message.hash(), message.encoded);
|
|
253
|
+
await globalThis.contractStore.put(await message.hash(), message.encoded);
|
|
246
254
|
}
|
|
247
255
|
catch (error) {
|
|
248
256
|
throw error;
|
|
249
257
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
258
|
+
let transaction = {
|
|
259
|
+
from: await signer.address,
|
|
260
|
+
to: addresses.contractFactory,
|
|
261
|
+
method: 'registerContract',
|
|
262
|
+
params: [await message.hash()]
|
|
263
|
+
};
|
|
264
|
+
transaction = await signTransaction(await this.createTransaction(transaction), signer);
|
|
265
|
+
return this.sendTransaction(transaction);
|
|
253
266
|
}
|
|
254
267
|
}
|
|
255
268
|
|
|
@@ -264,8 +277,7 @@ class Machine {
|
|
|
264
277
|
// @ts-ignore
|
|
265
278
|
return this.#init(blocks);
|
|
266
279
|
}
|
|
267
|
-
|
|
268
|
-
#createMessage(sender = globalThis.peernet.selectedAccount) {
|
|
280
|
+
#createMessage(sender = peernet.selectedAccount) {
|
|
269
281
|
return {
|
|
270
282
|
sender,
|
|
271
283
|
call: this.execute,
|
|
@@ -467,7 +479,7 @@ class Machine {
|
|
|
467
479
|
* @returns Promise
|
|
468
480
|
*/
|
|
469
481
|
async deleteAll() {
|
|
470
|
-
let hashes = await globalThis.contractStore.
|
|
482
|
+
let hashes = await globalThis.contractStore.keys();
|
|
471
483
|
hashes = Object.keys(hashes).map(hash => this.delete(hash));
|
|
472
484
|
return Promise.all(hashes);
|
|
473
485
|
}
|
|
@@ -678,6 +690,7 @@ class State extends Contract {
|
|
|
678
690
|
this.#lastBlock = { hash, ...message.decoded };
|
|
679
691
|
// await this.state.updateState(message)
|
|
680
692
|
await globalThis.chainStore.put('lastBlock', hash);
|
|
693
|
+
globalThis.pubsub.publish('lastBlock', this.#lastBlock);
|
|
681
694
|
}
|
|
682
695
|
async #lastBlockHandler() {
|
|
683
696
|
return new globalThis.peernet.protos['peernet-response']({ response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index } });
|
|
@@ -686,6 +699,7 @@ class State extends Contract {
|
|
|
686
699
|
return new globalThis.peernet.protos['peernet-response']({ response: { blocks: this.#blocks.map((block) => block.hash) } });
|
|
687
700
|
}
|
|
688
701
|
getLatestBlock() {
|
|
702
|
+
// @ts-ignore
|
|
689
703
|
return this.#getLatestBlock();
|
|
690
704
|
}
|
|
691
705
|
async getAndPutBlock(hash) {
|
|
@@ -696,7 +710,7 @@ class State extends Contract {
|
|
|
696
710
|
const { index } = block.decoded;
|
|
697
711
|
if (this.#blocks[index - 1] && this.#blocks[index - 1].hash !== block.hash)
|
|
698
712
|
throw `invalid block ${hash} @${index}`;
|
|
699
|
-
if (!await globalThis.peernet.has(hash
|
|
713
|
+
if (!await globalThis.peernet.has(hash))
|
|
700
714
|
await globalThis.peernet.put(hash, block.encoded, 'block');
|
|
701
715
|
}
|
|
702
716
|
return block;
|
|
@@ -839,7 +853,7 @@ class State extends Contract {
|
|
|
839
853
|
if (this.knownBlocks?.length === Number(lastBlock.index) + 1) {
|
|
840
854
|
let promises = [];
|
|
841
855
|
promises = await Promise.allSettled(this.knownBlocks.map(async (address) => {
|
|
842
|
-
const has = await globalThis.peernet.has(address
|
|
856
|
+
const has = await globalThis.peernet.has(address);
|
|
843
857
|
return { has, address };
|
|
844
858
|
}));
|
|
845
859
|
promises = promises.filter(({ status, value }) => status === 'fulfilled' && !value.has);
|
|
@@ -869,6 +883,7 @@ class State extends Contract {
|
|
|
869
883
|
let data = await new globalThis.peernet.protos['peernet-request']({ request: 'lastBlock' });
|
|
870
884
|
let node = await globalThis.peernet.prepareMessage(data);
|
|
871
885
|
for (const peer of globalThis.peernet?.connections) {
|
|
886
|
+
// @ts-ignore
|
|
872
887
|
if (peer.connected && peer.version === this.version) {
|
|
873
888
|
const task = async () => {
|
|
874
889
|
try {
|
|
@@ -882,6 +897,7 @@ class State extends Contract {
|
|
|
882
897
|
promises.push(task());
|
|
883
898
|
}
|
|
884
899
|
}
|
|
900
|
+
// @ts-ignore
|
|
885
901
|
promises = await this.promiseRequests(promises);
|
|
886
902
|
let latest = { index: 0, hash: '0x0', previousHash: '0x0' };
|
|
887
903
|
promises = promises.sort((a, b) => b.index - a.index);
|
|
@@ -952,6 +968,7 @@ class State extends Contract {
|
|
|
952
968
|
}
|
|
953
969
|
}
|
|
954
970
|
this.#blocks[block.index - 1].loaded = true;
|
|
971
|
+
// @ts-ignore
|
|
955
972
|
globalThis.debug(`loaded block: ${block.hash} @${block.index}`);
|
|
956
973
|
globalThis.pubsub.publish('block-loaded', { ...block });
|
|
957
974
|
}
|
|
@@ -999,7 +1016,7 @@ class State extends Contract {
|
|
|
999
1016
|
}
|
|
1000
1017
|
|
|
1001
1018
|
globalThis.BigNumber = BigNumber;
|
|
1002
|
-
const ignorelist = [
|
|
1019
|
+
const ignorelist = [];
|
|
1003
1020
|
// check if browser or local
|
|
1004
1021
|
class Chain extends State {
|
|
1005
1022
|
#state;
|
|
@@ -1073,7 +1090,9 @@ class Chain extends State {
|
|
|
1073
1090
|
message: nameServiceMessage
|
|
1074
1091
|
}];
|
|
1075
1092
|
await Promise.all(contracts.map(async ({ address, message }) => {
|
|
1093
|
+
// @ts-ignore
|
|
1076
1094
|
message = await new ContractMessage(Uint8Array.from(message.split(',').map(string => Number(string))));
|
|
1095
|
+
// @ts-ignore
|
|
1077
1096
|
await globalThis.contractStore.put(address, message.encoded);
|
|
1078
1097
|
}));
|
|
1079
1098
|
console.log('handle native contracts');
|
|
@@ -1205,20 +1224,24 @@ class Chain extends State {
|
|
|
1205
1224
|
async #addBlock(block) {
|
|
1206
1225
|
const blockMessage = await new BlockMessage(block);
|
|
1207
1226
|
await Promise.all(blockMessage.decoded.transactions
|
|
1208
|
-
|
|
1227
|
+
// @ts-ignore
|
|
1228
|
+
.map(async (transaction) => transactionPoolStore.delete(transaction.hash)));
|
|
1209
1229
|
const hash = await blockMessage.hash();
|
|
1210
1230
|
await globalThis.blockStore.put(hash, blockMessage.encoded);
|
|
1211
|
-
if (this.lastBlock.index < blockMessage.decoded.index)
|
|
1231
|
+
if (this.lastBlock.index < Number(blockMessage.decoded.index))
|
|
1212
1232
|
await this.updateState(blockMessage);
|
|
1213
1233
|
globalThis.debug(`added block: ${hash}`);
|
|
1214
1234
|
let promises = [];
|
|
1215
1235
|
let contracts = [];
|
|
1216
1236
|
for (let transaction of blockMessage.decoded.transactions) {
|
|
1217
1237
|
// await transactionStore.put(transaction.hash, transaction.encoded)
|
|
1238
|
+
// @ts-ignore
|
|
1218
1239
|
const index = contracts.indexOf(transaction.to);
|
|
1240
|
+
// @ts-ignore
|
|
1219
1241
|
if (index === -1)
|
|
1220
1242
|
contracts.push(transaction.to);
|
|
1221
|
-
// Todo: go trough all accounts
|
|
1243
|
+
// Todo: go trough all accounts
|
|
1244
|
+
// @ts-ignore
|
|
1222
1245
|
promises.push(this.#executeTransaction(transaction));
|
|
1223
1246
|
}
|
|
1224
1247
|
try {
|
|
@@ -1344,7 +1367,7 @@ class Chain extends State {
|
|
|
1344
1367
|
const peer = peers[validator];
|
|
1345
1368
|
if (peer && peer.connected && peer.version === this.version) {
|
|
1346
1369
|
let data = await new BWRequestMessage();
|
|
1347
|
-
const node = await globalThis.peernet.prepareMessage(
|
|
1370
|
+
const node = await globalThis.peernet.prepareMessage(data.encoded);
|
|
1348
1371
|
try {
|
|
1349
1372
|
const bw = await peer.request(node.encoded);
|
|
1350
1373
|
block.validators.push({
|
|
@@ -1449,9 +1472,7 @@ class Chain extends State {
|
|
|
1449
1472
|
return {
|
|
1450
1473
|
sender,
|
|
1451
1474
|
call: this.call,
|
|
1452
|
-
staticCall: this.staticCall
|
|
1453
|
-
delegate: this.delegate,
|
|
1454
|
-
staticDelegate: this.staticDelegate
|
|
1475
|
+
staticCall: this.staticCall
|
|
1455
1476
|
};
|
|
1456
1477
|
}
|
|
1457
1478
|
/**
|
|
@@ -1481,14 +1502,6 @@ class Chain extends State {
|
|
|
1481
1502
|
globalThis.msg = this.#createMessage();
|
|
1482
1503
|
return this.machine.get(contract, method, parameters);
|
|
1483
1504
|
}
|
|
1484
|
-
delegate(contract, method, parameters) {
|
|
1485
|
-
globalThis.msg = this.#createMessage();
|
|
1486
|
-
return this.machine.execute(contract, method, parameters);
|
|
1487
|
-
}
|
|
1488
|
-
staticDelegate(contract, method, parameters) {
|
|
1489
|
-
globalThis.msg = this.#createMessage();
|
|
1490
|
-
return this.machine.get(contract, method, parameters);
|
|
1491
|
-
}
|
|
1492
1505
|
mint(to, amount) {
|
|
1493
1506
|
return this.call(addresses.nativeToken, 'mint', [to, amount]);
|
|
1494
1507
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import Transaction from "./transaction.js";
|
|
2
|
+
import type MultiWallet from '@leofcoin/multi-wallet';
|
|
3
|
+
/**
|
|
4
|
+
* @extends {Transaction}
|
|
5
|
+
*/
|
|
6
|
+
export default class Contract extends Transaction {
|
|
7
|
+
constructor();
|
|
8
|
+
init(): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param {Address} creator
|
|
12
|
+
* @param {String} contract
|
|
13
|
+
* @param {Array} constructorParameters
|
|
14
|
+
* @returns lib.createContractMessage
|
|
15
|
+
*/
|
|
16
|
+
createContractMessage(creator: any, contract: any, constructorParameters?: any[]): Promise<import("@leofcoin/messages").ContractMessage>;
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param {Address} creator
|
|
20
|
+
* @param {String} contract
|
|
21
|
+
* @param {Array} constructorParameters
|
|
22
|
+
* @returns {Address}
|
|
23
|
+
*/
|
|
24
|
+
createContractAddress(creator: any, contract: any, constructorParameters?: any[]): Promise<any>;
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @param {String} contract
|
|
28
|
+
* @param {Array} parameters
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
deployContract(signer: MultiWallet, contract: any, constructorParameters?: any[]): Promise<{
|
|
32
|
+
hash: any;
|
|
33
|
+
data: any;
|
|
34
|
+
fee: string | 0 | import("@ethersproject/bignumber").BigNumber;
|
|
35
|
+
wait: Promise<unknown>;
|
|
36
|
+
message: any;
|
|
37
|
+
}>;
|
|
38
|
+
deployContractMessage(signer: any, message: any): Promise<{
|
|
39
|
+
hash: any;
|
|
40
|
+
data: any;
|
|
41
|
+
fee: string | 0 | import("@ethersproject/bignumber").BigNumber;
|
|
42
|
+
wait: Promise<unknown>;
|
|
43
|
+
message: any;
|
|
44
|
+
}>;
|
|
45
|
+
}
|
|
@@ -15,10 +15,10 @@ export default class Machine {
|
|
|
15
15
|
* @param {Array} parameters
|
|
16
16
|
* @returns Promise<message>
|
|
17
17
|
*/
|
|
18
|
-
execute(contract: any, method: any, parameters: any): Promise<
|
|
19
|
-
get(contract: any, method: any, parameters?: any): Promise<
|
|
18
|
+
execute(contract: any, method: any, parameters: any): Promise<any>;
|
|
19
|
+
get(contract: any, method: any, parameters?: any): Promise<any>;
|
|
20
20
|
has(address: any): Promise<unknown>;
|
|
21
|
-
delete(hash: any): Promise<
|
|
21
|
+
delete(hash: any): Promise<void>;
|
|
22
22
|
/**
|
|
23
23
|
*
|
|
24
24
|
* @returns Promise
|
package/exports/node.js
CHANGED
|
@@ -27,8 +27,8 @@ export default class State extends Contract {
|
|
|
27
27
|
constructor();
|
|
28
28
|
init(): Promise<void>;
|
|
29
29
|
updateState(message: any): Promise<void>;
|
|
30
|
-
getLatestBlock(): Promise<BlockMessage
|
|
31
|
-
getAndPutBlock(hash: string): BlockMessage
|
|
30
|
+
getLatestBlock(): Promise<BlockMessage['decoded']>;
|
|
31
|
+
getAndPutBlock(hash: string): Promise<BlockMessage>;
|
|
32
32
|
resolveBlock(hash: any): any;
|
|
33
33
|
resolveBlocks(): Promise<void>;
|
|
34
34
|
restoreChain(): Promise<void>;
|
|
@@ -29,6 +29,7 @@ export default class Transaction extends Protocol {
|
|
|
29
29
|
getNonce(address: any): Promise<number>;
|
|
30
30
|
validateNonce(address: any, nonce: any): Promise<void>;
|
|
31
31
|
isTransactionMessage(message: any): boolean;
|
|
32
|
+
createTransactionMessage(transaction: any, signature: any): Promise<TransactionMessage>;
|
|
32
33
|
createTransaction(transaction: any): Promise<{
|
|
33
34
|
from: any;
|
|
34
35
|
to: any;
|
|
@@ -40,7 +41,7 @@ export default class Transaction extends Protocol {
|
|
|
40
41
|
sendTransaction(message: any): Promise<{
|
|
41
42
|
hash: any;
|
|
42
43
|
data: any;
|
|
43
|
-
fee:
|
|
44
|
+
fee: string | 0 | import("@leofcoin/utils").BigNumber;
|
|
44
45
|
wait: Promise<unknown>;
|
|
45
46
|
message: any;
|
|
46
47
|
}>;
|