@leofcoin/chain 1.4.12 → 1.4.13
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 +3 -3
- package/src/chain.js +6 -7
- package/src/machine.js +6 -1
- package/src/transaction.js +9 -9
- package/test/chain.js +13 -24
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/chain",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.13",
|
|
4
4
|
"description": "Official javascript implementation",
|
|
5
5
|
"main": "./dist/node.js",
|
|
6
6
|
"module": "./dist/chain.esm",
|
|
@@ -43,12 +43,12 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@leofcoin/addresses": "^1.0.4",
|
|
46
|
-
"@leofcoin/codec-format-interface": "^1.
|
|
46
|
+
"@leofcoin/codec-format-interface": "^1.6.4",
|
|
47
47
|
"@leofcoin/lib": "^1.0.11",
|
|
48
48
|
"@leofcoin/messages": "^1.2.0",
|
|
49
49
|
"@leofcoin/multi-wallet": "^2.1.1",
|
|
50
50
|
"@leofcoin/networks": "^1.0.0",
|
|
51
|
-
"@leofcoin/peernet": "^0.
|
|
51
|
+
"@leofcoin/peernet": "^0.18.0",
|
|
52
52
|
"@leofcoin/storage": "^3.0.6",
|
|
53
53
|
"@leofcoin/utils": "^1.1.4",
|
|
54
54
|
"@leofcoin/workers": "^1.3.5",
|
package/src/chain.js
CHANGED
|
@@ -121,7 +121,7 @@ export default class Chain extends Contract {
|
|
|
121
121
|
this.#runningEpoch = true
|
|
122
122
|
console.log('epoch');
|
|
123
123
|
const validators = await this.staticCall(addresses.validators, 'validators')
|
|
124
|
-
console.log(validators);
|
|
124
|
+
console.log({validators});
|
|
125
125
|
if (!validators[peernet.selectedAccount]?.active) return
|
|
126
126
|
const start = Date.now()
|
|
127
127
|
try {
|
|
@@ -155,7 +155,8 @@ export default class Chain extends Contract {
|
|
|
155
155
|
}]
|
|
156
156
|
|
|
157
157
|
await Promise.all(contracts.map(async ({address, message}) => {
|
|
158
|
-
|
|
158
|
+
// console.log({message});
|
|
159
|
+
message = await new ContractMessage(Uint8Array.from(message.split(',').map(string => Number(string))))
|
|
159
160
|
await contractStore.put(address, message.encoded)
|
|
160
161
|
}))
|
|
161
162
|
console.log('handle native contracts');
|
|
@@ -460,7 +461,6 @@ async resolveBlock(hash) {
|
|
|
460
461
|
if (await transactionPoolStore.size() === 0) return;
|
|
461
462
|
|
|
462
463
|
let transactions = await transactionPoolStore.values(this.transactionLimit)
|
|
463
|
-
|
|
464
464
|
if (Object.keys(transactions)?.length === 0 ) return
|
|
465
465
|
|
|
466
466
|
let block = {
|
|
@@ -471,7 +471,6 @@ async resolveBlock(hash) {
|
|
|
471
471
|
|
|
472
472
|
// exclude failing tx
|
|
473
473
|
transactions = await this.promiseTransactions(transactions)
|
|
474
|
-
|
|
475
474
|
transactions = transactions.sort((a, b) => a.nonce - b.nonce)
|
|
476
475
|
for (let transaction of transactions) {
|
|
477
476
|
const hash = await transaction.hash()
|
|
@@ -480,7 +479,7 @@ async resolveBlock(hash) {
|
|
|
480
479
|
block.transactions.push({hash, ...transaction.decoded})
|
|
481
480
|
block.fees += Number(calculateFee(transaction.decoded))
|
|
482
481
|
await accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)))
|
|
483
|
-
} catch {
|
|
482
|
+
} catch (e) {
|
|
484
483
|
await transactionPoolStore.delete(hash)
|
|
485
484
|
}
|
|
486
485
|
}
|
|
@@ -572,7 +571,6 @@ async resolveBlock(hash) {
|
|
|
572
571
|
peernet.publish('add-block', blockMessage.encoded)
|
|
573
572
|
pubsub.publish('add-block', blockMessage.decoded)
|
|
574
573
|
} catch (error) {
|
|
575
|
-
console.log(error);
|
|
576
574
|
throw new Error(`invalid block ${block}`)
|
|
577
575
|
}
|
|
578
576
|
// data = await this.#machine.execute(to, method, params)
|
|
@@ -588,7 +586,8 @@ async resolveBlock(hash) {
|
|
|
588
586
|
const has = await transactionPoolStore.has(hash)
|
|
589
587
|
if (!has) await transactionPoolStore.put(hash, transaction.encoded)
|
|
590
588
|
if (this.#participating && !this.#runningEpoch) this.#runEpoch()
|
|
591
|
-
} catch {
|
|
589
|
+
} catch (e) {
|
|
590
|
+
console.log(e);
|
|
592
591
|
throw new Error('invalid transaction')
|
|
593
592
|
}
|
|
594
593
|
}
|
package/src/machine.js
CHANGED
|
@@ -26,7 +26,12 @@ export default class Machine {
|
|
|
26
26
|
case 'contractError': {
|
|
27
27
|
console.warn(`removing contract ${await data.hash()}`);
|
|
28
28
|
await contractStore.delete(await data.hash())
|
|
29
|
-
|
|
29
|
+
break
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
case 'initError': {
|
|
33
|
+
console.error(`init error: ${data.message}`);
|
|
34
|
+
break
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
case 'executionError': {
|
package/src/transaction.js
CHANGED
|
@@ -160,7 +160,6 @@ export default class Transaction extends Protocol {
|
|
|
160
160
|
await wallet.recover(identity.mnemonic)
|
|
161
161
|
const account = await wallet.account(0).external(0)
|
|
162
162
|
transaction.signature = await this.#signTransaction(transaction, account)
|
|
163
|
-
transaction.signature = bs32.encode(transaction.signature)
|
|
164
163
|
return transaction
|
|
165
164
|
}
|
|
166
165
|
|
|
@@ -206,28 +205,29 @@ export default class Transaction extends Protocol {
|
|
|
206
205
|
const rawTransaction = await this.ensureNonce({from, to, nonce, method, params: parameters})
|
|
207
206
|
const transaction = await this.signTransaction(rawTransaction, from)
|
|
208
207
|
const message = await new TransactionMessage(transaction)
|
|
209
|
-
|
|
208
|
+
const hash = await message.hash()
|
|
209
|
+
|
|
210
210
|
let data
|
|
211
211
|
const wait = new Promise(async (resolve, reject) => {
|
|
212
|
-
if (pubsub.subscribers[`transaction.completed.${
|
|
213
|
-
const result = pubsub.subscribers[`transaction.completed.${
|
|
212
|
+
if (pubsub.subscribers[`transaction.completed.${hash}`]) {
|
|
213
|
+
const result = pubsub.subscribers[`transaction.completed.${hash}`].value
|
|
214
214
|
result.status === 'fulfilled' ? resolve(result.hash) : reject({hash: result.hash, error: result.error})
|
|
215
215
|
} else {
|
|
216
216
|
const completed = async result => {
|
|
217
217
|
result.status === 'fulfilled' ? resolve(result.hash) : reject({hash: result.hash, error: result.error})
|
|
218
218
|
|
|
219
219
|
setTimeout(async () => {
|
|
220
|
-
pubsub.unsubscribe(`transaction.completed.${
|
|
220
|
+
pubsub.unsubscribe(`transaction.completed.${hash}`, completed)
|
|
221
221
|
}, 10_000)
|
|
222
222
|
}
|
|
223
|
-
pubsub.subscribe(`transaction.completed.${
|
|
223
|
+
pubsub.subscribe(`transaction.completed.${hash}`, completed)
|
|
224
224
|
}
|
|
225
225
|
})
|
|
226
|
-
await transactionPoolStore.put(
|
|
226
|
+
await transactionPoolStore.put(hash, message.encoded)
|
|
227
|
+
debug(`Added ${hash} to the transaction pool`)
|
|
227
228
|
peernet.publish('add-transaction', message.encoded)
|
|
228
|
-
return {hash:
|
|
229
|
+
return {hash: hash, data, fee: await calculateFee(message.decoded), wait, message}
|
|
229
230
|
} catch (error) {
|
|
230
|
-
console.log(error)
|
|
231
231
|
throw error
|
|
232
232
|
}
|
|
233
233
|
}
|
package/test/chain.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
globalThis.DEBUG = true
|
|
4
4
|
const Chain = await import('./../src/chain.js');
|
|
5
5
|
const Node = await import('./../src/node.js');
|
|
@@ -14,28 +14,17 @@
|
|
|
14
14
|
})
|
|
15
15
|
const chain = await new Chain.default()
|
|
16
16
|
let start
|
|
17
|
-
//
|
|
18
|
-
// await chain.deleteAll()
|
|
19
|
-
// return
|
|
20
|
-
// try {
|
|
21
|
-
// const contract = await chain.utils.read('./dist/native.js')
|
|
22
|
-
// const address = await chain.deployContract(peernet.Buffer.from(`return ${contract.toString().replace(/export{([A-Z])\w+ as default}/g, '')}`))
|
|
23
|
-
// console.log(address);
|
|
24
|
-
// } catch (e) {
|
|
25
|
-
// console.log(e);
|
|
26
|
-
// } finally {
|
|
27
|
-
//
|
|
28
|
-
// }
|
|
29
|
-
// '5xdacigaguxg3yjllehp65htk32ha3sztlexxrrhmviobgibz6dt6hkxfu'
|
|
30
|
-
console.log(peernet.selectedAccount);
|
|
31
17
|
await chain.participate(peernet.selectedAccount)
|
|
32
18
|
console.log(peernet.selectedAccount);
|
|
33
19
|
const job = async () => {
|
|
34
|
-
|
|
20
|
+
|
|
21
|
+
let nonce = await chain.getNonce(peernet.selectedAccount)
|
|
22
|
+
// // setTimeout(async () => {
|
|
35
23
|
let tx
|
|
36
24
|
// try {
|
|
37
25
|
// tx = await chain.createTransaction(chain.nativeToken, 'grantRole', [peernet.selectedAccount, 'MINT'])
|
|
38
|
-
//
|
|
26
|
+
// console.log({tx});
|
|
27
|
+
// await tx.wait
|
|
39
28
|
|
|
40
29
|
// } catch (e) {
|
|
41
30
|
// console.log({e});
|
|
@@ -44,16 +33,17 @@ const job = async () => {
|
|
|
44
33
|
// try {
|
|
45
34
|
// tx = await chain.createTransaction(chain.nativeToken, 'mint', [peernet.selectedAccount, chain.utils.parseUnits('100000000000000').toString()])
|
|
46
35
|
|
|
47
|
-
// await tx.wait
|
|
36
|
+
// await tx.wait
|
|
48
37
|
// } catch (e) {
|
|
49
38
|
// console.log({e});
|
|
50
39
|
// }
|
|
51
|
-
|
|
52
|
-
let nonce = await chain.getNonce(peernet.selectedAccount)
|
|
40
|
+
return
|
|
53
41
|
console.log({nonce});
|
|
54
42
|
let balances = await chain.balances
|
|
55
|
-
console.log(
|
|
56
|
-
|
|
43
|
+
console.log({balances});
|
|
44
|
+
// return
|
|
45
|
+
// console.log(`balance for ${Object.keys(balances)[0]}:${chain.utils.formatUnits(balances[Object.keys(balances)[0]]).toString()}`);
|
|
46
|
+
// console.log(`balance for ${Object.keys(balances)[1]}:${chain.utils.formatUnits(balances[Object.keys(balances)[1]]).toString()}`);
|
|
57
47
|
// return
|
|
58
48
|
let promises = []
|
|
59
49
|
// nonce += 1
|
|
@@ -116,5 +106,4 @@ const job = async () => {
|
|
|
116
106
|
} catch (e) {
|
|
117
107
|
console.warn(e);
|
|
118
108
|
}
|
|
119
|
-
|
|
120
|
-
})()
|
|
109
|
+
|