@leofcoin/chain 1.4.2 → 1.4.3
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 +4 -3
- package/src/chain.js +14 -14
- package/src/contract.js +1 -1
- package/src/machine.js +9 -9
- package/src/node.js +2 -2
- package/src/transaction.js +4 -3
- package/src/apis/token.lfcc +0 -17
- package/src/transactions/transaction.js +0 -1
- package/src/transactions/validator.js +0 -29
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/chain",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"description": "Official javascript implementation",
|
|
5
5
|
"main": "./dist/node.js",
|
|
6
6
|
"module": "./dist/chain.esm",
|
|
@@ -48,10 +48,11 @@
|
|
|
48
48
|
"@leofcoin/lib": "^1.0.11",
|
|
49
49
|
"@leofcoin/messages": "^1.2.0",
|
|
50
50
|
"@leofcoin/multi-wallet": "^2.1.1",
|
|
51
|
+
"@leofcoin/networks": "^1.0.0",
|
|
51
52
|
"@leofcoin/peernet": "^0.16.0",
|
|
52
53
|
"@leofcoin/storage": "^3.0.6",
|
|
53
|
-
"@leofcoin/utils": "^1.
|
|
54
|
-
"@leofcoin/workers": "^1.
|
|
54
|
+
"@leofcoin/utils": "^1.1.4",
|
|
55
|
+
"@leofcoin/workers": "^1.3.5",
|
|
55
56
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
56
57
|
"@vandeurenglenn/easy-worker": "^1.0.2",
|
|
57
58
|
"@vandeurenglenn/queue": "^1.0.0",
|
package/src/chain.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { BigNumber, formatUnits, parseUnits } from '
|
|
1
|
+
import { BigNumber, formatUnits, parseUnits, formatBytes } from '@leofcoin/utils'
|
|
2
2
|
import Machine from './machine.js'
|
|
3
|
-
import { ContractMessage, TransactionMessage, BlockMessage, BWMessage, BWRequestMessage } from '
|
|
4
|
-
import addresses, { nativeToken } from '
|
|
5
|
-
import { contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage, calculateFee } from '
|
|
6
|
-
import { formatBytes } from '../../utils/src/utils.js'
|
|
3
|
+
import { ContractMessage, TransactionMessage, BlockMessage, BWMessage, BWRequestMessage } from '@leofcoin/messages'
|
|
4
|
+
import addresses, { nativeToken } from '@leofcoin/addresses'
|
|
5
|
+
import { contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage, calculateFee } from '@leofcoin/lib'
|
|
7
6
|
import State from './state.js'
|
|
8
7
|
import Contract from './contract.js'
|
|
9
8
|
|
|
@@ -242,7 +241,7 @@ export default class Chain extends Contract {
|
|
|
242
241
|
if (localBlock && localBlock !== '0x0') {
|
|
243
242
|
localBlock = await peernet.get(localBlock, 'block')
|
|
244
243
|
localBlock = await new BlockMessage(localBlock)
|
|
245
|
-
this.#lastBlock = {...localBlock.decoded, hash: await localBlock.hash}
|
|
244
|
+
this.#lastBlock = {...localBlock.decoded, hash: await localBlock.hash()}
|
|
246
245
|
} else {
|
|
247
246
|
const latestBlock = await this.#getLatestBlock()
|
|
248
247
|
await this.#syncChain(latestBlock)
|
|
@@ -392,8 +391,8 @@ async resolveBlock(hash) {
|
|
|
392
391
|
// console.log(block);
|
|
393
392
|
const blockMessage = await new BlockMessage(new Uint8Array(Object.values(block)))
|
|
394
393
|
await Promise.all(blockMessage.decoded.transactions
|
|
395
|
-
.map(async transaction => transactionPoolStore.delete(await transaction.hash)))
|
|
396
|
-
const hash = await blockMessage.hash
|
|
394
|
+
.map(async transaction => transactionPoolStore.delete(await transaction.hash())))
|
|
395
|
+
const hash = await blockMessage.hash()
|
|
397
396
|
|
|
398
397
|
await blockStore.put(hash, blockMessage.encoded)
|
|
399
398
|
|
|
@@ -434,7 +433,7 @@ async resolveBlock(hash) {
|
|
|
434
433
|
}
|
|
435
434
|
|
|
436
435
|
async #updateState(message) {
|
|
437
|
-
const hash = await message.hash
|
|
436
|
+
const hash = await message.hash()
|
|
438
437
|
this.#lastBlock = { hash, ...message.decoded }
|
|
439
438
|
await this.state.updateState(message)
|
|
440
439
|
await chainStore.put('lastBlock', hash)
|
|
@@ -481,7 +480,7 @@ async resolveBlock(hash) {
|
|
|
481
480
|
await accountsStore.put(transaction.from, new TextEncoder().encode(String(transaction.nonce)))
|
|
482
481
|
} catch {
|
|
483
482
|
transaction = await new TransactionMessage(transaction)
|
|
484
|
-
await transactionPoolStore.delete(await transaction.hash)
|
|
483
|
+
await transactionPoolStore.delete(await transaction.hash())
|
|
485
484
|
}
|
|
486
485
|
}
|
|
487
486
|
// don't add empty block
|
|
@@ -558,11 +557,11 @@ async resolveBlock(hash) {
|
|
|
558
557
|
|
|
559
558
|
try {
|
|
560
559
|
await Promise.all(block.transactions
|
|
561
|
-
.map(async transaction => transactionPoolStore.delete(await transaction.hash)))
|
|
560
|
+
.map(async transaction => transactionPoolStore.delete(await transaction.hash())))
|
|
562
561
|
|
|
563
562
|
|
|
564
563
|
let blockMessage = await new BlockMessage(block)
|
|
565
|
-
const hash = await blockMessage.hash
|
|
564
|
+
const hash = await blockMessage.hash()
|
|
566
565
|
|
|
567
566
|
|
|
568
567
|
await peernet.put(hash, blockMessage.encoded, 'block')
|
|
@@ -583,9 +582,10 @@ async resolveBlock(hash) {
|
|
|
583
582
|
|
|
584
583
|
async #addTransaction(transaction) {
|
|
585
584
|
try {
|
|
585
|
+
const hash = await transaction.hash()
|
|
586
586
|
transaction = await new TransactionMessage(transaction)
|
|
587
|
-
const has = await transactionPoolStore.has(
|
|
588
|
-
if (!has) await transactionPoolStore.put(
|
|
587
|
+
const has = await transactionPoolStore.has(hash)
|
|
588
|
+
if (!has) await transactionPoolStore.put(hash, transaction.encoded)
|
|
589
589
|
if (this.#participating && !this.#runningEpoch) this.#runEpoch()
|
|
590
590
|
} catch {
|
|
591
591
|
throw new Error('invalid transaction')
|
package/src/contract.js
CHANGED
package/src/machine.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { contractFactory, nativeToken, validators, nameService } from '
|
|
1
|
+
import { contractFactory, nativeToken, validators, nameService } from '@leofcoin/addresses'
|
|
2
2
|
import randombytes from 'randombytes'
|
|
3
3
|
import { join, dirname } from 'node:path'
|
|
4
4
|
import EasyWorker from '@vandeurenglenn/easy-worker'
|
|
5
|
-
import { ContractMessage } from '
|
|
5
|
+
import { ContractMessage } from '@leofcoin/messages'
|
|
6
6
|
// import State from './state'
|
|
7
|
-
import { fileURLToPath } from 'url';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
8
|
|
|
9
9
|
const __filename = fileURLToPath(import.meta.url);
|
|
10
10
|
const __dirname = dirname(__filename);
|
|
@@ -29,8 +29,8 @@ export default class Machine {
|
|
|
29
29
|
async #onmessage(data) {
|
|
30
30
|
switch (data.type) {
|
|
31
31
|
case 'contractError': {
|
|
32
|
-
console.warn(`removing contract ${await data.hash}`);
|
|
33
|
-
await contractStore.delete(await data.hash)
|
|
32
|
+
console.warn(`removing contract ${await data.hash()}`);
|
|
33
|
+
await contractStore.delete(await data.hash())
|
|
34
34
|
break
|
|
35
35
|
}
|
|
36
36
|
|
|
@@ -65,7 +65,7 @@ export default class Machine {
|
|
|
65
65
|
}
|
|
66
66
|
pubsub.subscribe('machine.ready', machineReady)
|
|
67
67
|
|
|
68
|
-
this.worker = await new EasyWorker('
|
|
68
|
+
this.worker = await new EasyWorker('node_modules/@leofcoin/workers/src/machine-worker.js', {serialization: 'advanced', type:'module'})
|
|
69
69
|
this.worker.onmessage(this.#onmessage.bind(this))
|
|
70
70
|
|
|
71
71
|
// const blocks = await blockStore.values()
|
|
@@ -90,7 +90,7 @@ export default class Machine {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
async #runContract(contractMessage) {
|
|
93
|
-
const hash = await contractMessage.hash
|
|
93
|
+
const hash = await contractMessage.hash()
|
|
94
94
|
return new Promise((resolve, reject) => {
|
|
95
95
|
const id = randombytes(20).toString('hex')
|
|
96
96
|
const onmessage = message => {
|
|
@@ -127,13 +127,13 @@ export default class Machine {
|
|
|
127
127
|
if (!await contractStore.has(parameters[0])) {
|
|
128
128
|
message = await peernet.get(parameters[0], 'contract')
|
|
129
129
|
message = await new ContractMessage(message)
|
|
130
|
-
await contractStore.put(await message.hash, message.encoded)
|
|
130
|
+
await contractStore.put(await message.hash(), message.encoded)
|
|
131
131
|
}
|
|
132
132
|
if (!message) {
|
|
133
133
|
message = await contractStore.get(parameters[0])
|
|
134
134
|
message = await new ContractMessage(message)
|
|
135
135
|
}
|
|
136
|
-
if (!this.#contracts[await message.hash]) await this.#runContract(message)
|
|
136
|
+
if (!this.#contracts[await message.hash()]) await this.#runContract(message)
|
|
137
137
|
}
|
|
138
138
|
} catch (error) {
|
|
139
139
|
throw new Error(`contract deployment failed for ${parameters[0]}\n${error.message}`)
|
package/src/node.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// import config from './config/config'
|
|
2
2
|
import Peernet from '@leofcoin/peernet'
|
|
3
|
-
import nodeConfig from '
|
|
4
|
-
import networks from '
|
|
3
|
+
import nodeConfig from '@leofcoin/lib/node-config';
|
|
4
|
+
import networks from '@leofcoin/networks';
|
|
5
5
|
|
|
6
6
|
export default class Node {
|
|
7
7
|
constructor() {
|
package/src/transaction.js
CHANGED
|
@@ -2,9 +2,10 @@ import Protocol from "./protocol.js"
|
|
|
2
2
|
import * as MultiWallet from '@leofcoin/multi-wallet'
|
|
3
3
|
import {CodecHash} from '@leofcoin/codec-format-interface'
|
|
4
4
|
import bs32 from '@vandeurenglenn/base32'
|
|
5
|
-
import { TransactionMessage, BlockMessage } from "
|
|
6
|
-
import { calculateFee } from '
|
|
7
|
-
import { formatBytes } from '
|
|
5
|
+
import { TransactionMessage, BlockMessage } from "@leofcoin/messages"
|
|
6
|
+
import { calculateFee } from '@leofcoin/lib'
|
|
7
|
+
import { formatBytes } from '@leofcoin/utils'
|
|
8
|
+
|
|
8
9
|
export default class Transaction extends Protocol {
|
|
9
10
|
constructor() {
|
|
10
11
|
super()
|
package/src/apis/token.lfcc
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
get name,
|
|
3
|
-
get symbol,
|
|
4
|
-
get holders,
|
|
5
|
-
get balances,
|
|
6
|
-
get roles,
|
|
7
|
-
function hasRole,
|
|
8
|
-
function grantRole,
|
|
9
|
-
function balance,
|
|
10
|
-
function setApproval,
|
|
11
|
-
function approved,
|
|
12
|
-
function mint,
|
|
13
|
-
function burn,
|
|
14
|
-
function transfer,
|
|
15
|
-
Event()
|
|
16
|
-
msg.sender
|
|
17
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default () => {}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import hasher from './crypto/hasher'
|
|
2
|
-
import {create} from './crypto/transaction'
|
|
3
|
-
|
|
4
|
-
export default class Validator {
|
|
5
|
-
constructor(address) {
|
|
6
|
-
this._init(address)
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
async _init(address) {
|
|
10
|
-
const valid = await this.isValid(address, this)
|
|
11
|
-
if (!valid) throw new Error('Invalid validator')
|
|
12
|
-
return this
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
async isValid(hash, transaction) {
|
|
16
|
-
return this.validateTransaction(hash, transaction)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async validateTransaction(hash, transaction) {
|
|
20
|
-
delete transaction.hash
|
|
21
|
-
transaction = await create(hasher.hash(transaction))
|
|
22
|
-
if (transaction !== hash) throw new Error('invalid hash')
|
|
23
|
-
// for (const input of transaction) {
|
|
24
|
-
// input
|
|
25
|
-
// }
|
|
26
|
-
return true
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
}
|