@leofcoin/chain 1.3.10 → 1.3.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/demo/chain.browser.js +7667 -570
- package/demo/workers/machine-worker.js +28 -566
- package/dist/865.browser.js +10 -0
- package/dist/browser/workers/865.js +10 -0
- package/dist/browser/workers/block-worker.js +13175 -0
- package/dist/browser/workers/machine-worker.js +13385 -0
- package/dist/browser/workers/pool-worker.js +8503 -0
- package/dist/browser/workers/transaction-worker.js +8495 -0
- package/dist/chain.browser.js +66842 -0
- package/dist/chain.js +444 -302
- package/dist/generate-account.browser.js +50 -0
- package/dist/messages.browser.js +328 -0
- package/dist/module/chain.js +439 -298
- package/dist/multi-wallet.browser.js +15 -0
- package/dist/node.browser.js +9858 -0
- package/dist/pako.browser.js +6900 -0
- package/dist/peernet-swarm.browser.js +839 -0
- package/dist/storage.browser.js +3724 -0
- package/dist/wrtc.browser.js +28 -0
- package/package.json +4 -3
- package/rollup.config.js +17 -27
- package/src/chain.js +104 -269
- package/src/contract.js +51 -0
- package/src/protocol.js +3 -0
- package/src/state.js +2 -2
- package/src/transaction.js +233 -0
- package/test/chain.js +3 -1
- package/webpack.config.js +2 -1
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
(self["webpackChunk_leofcoin_chain"] = self["webpackChunk_leofcoin_chain"] || []).push([[228],{
|
|
2
|
+
|
|
3
|
+
/***/ 9347:
|
|
4
|
+
/***/ (function(__unused_webpack_module, exports) {
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.MediaStream = window.MediaStream;
|
|
9
|
+
exports.MediaStreamTrack = window.MediaStreamTrack;
|
|
10
|
+
exports.RTCDataChannel = window.RTCDataChannel;
|
|
11
|
+
exports.RTCDataChannelEvent = window.RTCDataChannelEvent;
|
|
12
|
+
exports.RTCDtlsTransport = window.RTCDtlsTransport;
|
|
13
|
+
exports.RTCIceCandidate = window.RTCIceCandidate;
|
|
14
|
+
exports.RTCIceTransport = window.RTCIceTransport;
|
|
15
|
+
exports.RTCPeerConnection = window.RTCPeerConnection;
|
|
16
|
+
exports.RTCPeerConnectionIceEvent = window.RTCPeerConnectionIceEvent;
|
|
17
|
+
exports.RTCRtpReceiver = window.RTCRtpReceiver;
|
|
18
|
+
exports.RTCRtpSender = window.RTCRtpSender;
|
|
19
|
+
exports.RTCRtpTransceiver = window.RTCRtpTransceiver;
|
|
20
|
+
exports.RTCSctpTransport = window.RTCSctpTransport;
|
|
21
|
+
exports.RTCSessionDescription = window.RTCSessionDescription;
|
|
22
|
+
exports.getUserMedia = window.getUserMedia;
|
|
23
|
+
exports.mediaDevices = navigator.mediaDevices;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
/***/ })
|
|
27
|
+
|
|
28
|
+
}])
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/chain",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.12",
|
|
4
4
|
"description": "Official javascript implementation",
|
|
5
5
|
"main": "./dist/node.js",
|
|
6
6
|
"module": "./dist/chain.esm",
|
|
@@ -35,11 +35,13 @@
|
|
|
35
35
|
"path-browserify": "^1.0.1",
|
|
36
36
|
"rollup": "^2.70.1",
|
|
37
37
|
"rollup-plugin-cleanup": "^3.2.1",
|
|
38
|
+
"rollup-plugin-modify": "^3.0.0",
|
|
38
39
|
"rollup-plugin-terser": "^7.0.2",
|
|
39
40
|
"stream-browserify": "^3.0.0",
|
|
40
41
|
"tape": "^5.5.2",
|
|
41
42
|
"terser": "^5.16.0",
|
|
42
43
|
"vm-browserify": "^1.1.2",
|
|
44
|
+
"webpack": "^5.75.0",
|
|
43
45
|
"webpack-cli": "^4.9.2"
|
|
44
46
|
},
|
|
45
47
|
"dependencies": {
|
|
@@ -59,8 +61,7 @@
|
|
|
59
61
|
"@vandeurenglenn/easy-worker": "^1.0.1",
|
|
60
62
|
"@vandeurenglenn/queue": "^1.0.0",
|
|
61
63
|
"chalk": "^4.0.0",
|
|
62
|
-
"pako": "^2.0.4"
|
|
63
|
-
"webpack": "^5.74.0"
|
|
64
|
+
"pako": "^2.0.4"
|
|
64
65
|
},
|
|
65
66
|
"eslintConfig": {
|
|
66
67
|
"env": {
|
package/rollup.config.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import json from '@rollup/plugin-json'
|
|
2
2
|
import strip from '@rollup/plugin-strip';
|
|
3
3
|
import { terser } from "rollup-plugin-terser";
|
|
4
|
+
import modify from 'rollup-plugin-modify'
|
|
4
5
|
|
|
5
6
|
import { execSync } from 'node:child_process'
|
|
6
7
|
|
|
@@ -12,49 +13,38 @@ try {
|
|
|
12
13
|
}
|
|
13
14
|
export default [{
|
|
14
15
|
input: ['./src/chain.js'],
|
|
15
|
-
output: {
|
|
16
|
+
output: [{
|
|
16
17
|
dir: './dist',
|
|
17
18
|
format: 'cjs'
|
|
18
|
-
},
|
|
19
|
-
plugins: [
|
|
20
|
-
json(),
|
|
21
|
-
// terser({
|
|
22
|
-
// keep_classnames: true
|
|
23
|
-
// }),
|
|
24
|
-
]
|
|
25
|
-
}, {
|
|
26
|
-
input: ['./src/node.js'],
|
|
27
|
-
output: {
|
|
28
|
-
dir: 'dist',
|
|
29
|
-
format: 'cjs'
|
|
30
|
-
},
|
|
31
|
-
plugins: [
|
|
32
|
-
json(),
|
|
33
|
-
// terser({
|
|
34
|
-
// keep_classnames: true
|
|
35
|
-
// }),
|
|
36
|
-
// cjs()
|
|
37
|
-
]
|
|
38
|
-
}, {
|
|
39
|
-
input: ['./src/chain.js'],
|
|
40
|
-
output: {
|
|
19
|
+
}, {
|
|
41
20
|
dir: './dist/module',
|
|
42
21
|
format: 'es'
|
|
43
|
-
},
|
|
22
|
+
}],
|
|
44
23
|
plugins: [
|
|
45
24
|
json(),
|
|
25
|
+
modify({
|
|
26
|
+
'node:path': 'path',
|
|
27
|
+
'node:crypto': 'crypto'
|
|
28
|
+
})
|
|
46
29
|
// terser({
|
|
47
30
|
// keep_classnames: true
|
|
48
31
|
// }),
|
|
49
32
|
]
|
|
50
33
|
}, {
|
|
51
34
|
input: ['./src/node.js'],
|
|
52
|
-
output: {
|
|
35
|
+
output: [{
|
|
36
|
+
dir: 'dist',
|
|
37
|
+
format: 'cjs'
|
|
38
|
+
}, {
|
|
53
39
|
dir: 'dist/module',
|
|
54
40
|
format: 'es'
|
|
55
|
-
},
|
|
41
|
+
}],
|
|
56
42
|
plugins: [
|
|
57
43
|
json(),
|
|
44
|
+
modify({
|
|
45
|
+
'node:path': 'path',
|
|
46
|
+
'node:crypto': 'crypto'
|
|
47
|
+
})
|
|
58
48
|
// terser({
|
|
59
49
|
// keep_classnames: true
|
|
60
50
|
// }),
|
package/src/chain.js
CHANGED
|
@@ -1,31 +1,97 @@
|
|
|
1
1
|
import { BigNumber, formatUnits, parseUnits } from '@leofcoin/utils'
|
|
2
2
|
import Machine from './machine.js'
|
|
3
3
|
import { ContractMessage, TransactionMessage, BlockMessage, BWMessage, BWRequestMessage } from './../../messages/src/messages'
|
|
4
|
-
import addresses from './../../addresses/src/addresses'
|
|
5
|
-
import {
|
|
6
|
-
import MultiWallet from '@leofcoin/multi-wallet'
|
|
7
|
-
import {CodecHash} from '@leofcoin/codec-format-interface/dist/index'
|
|
8
|
-
import bs32 from '@vandeurenglenn/base32'
|
|
4
|
+
import addresses, { nativeToken } from './../../addresses/src/addresses'
|
|
5
|
+
import { contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage, calculateFee } from './../../lib/src/lib'
|
|
9
6
|
import { formatBytes } from '../../utils/src/utils.js'
|
|
7
|
+
import State from './state.js'
|
|
8
|
+
import Contract from './contract.js'
|
|
10
9
|
|
|
11
10
|
globalThis.BigNumber = BigNumber
|
|
12
11
|
|
|
13
12
|
// check if browser or local
|
|
14
|
-
export default class Chain {
|
|
13
|
+
export default class Chain extends Contract {
|
|
14
|
+
/** {Address[]} */
|
|
15
15
|
#validators = []
|
|
16
|
+
/** {Block[]} */
|
|
16
17
|
#blocks = []
|
|
18
|
+
|
|
17
19
|
#machine
|
|
20
|
+
/** {Boolean} */
|
|
18
21
|
#runningEpoch = false
|
|
22
|
+
|
|
23
|
+
/** {Boolean} */
|
|
19
24
|
#chainSyncing = false
|
|
25
|
+
|
|
26
|
+
/** {Number} */
|
|
27
|
+
#totalSize = 0
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* {Block} {index, hash, previousHash}
|
|
31
|
+
*/
|
|
20
32
|
#lastBlock = {index: 0, hash: '0x0', previousHash: '0x0'}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* amount the native token has been iteracted with
|
|
36
|
+
*/
|
|
37
|
+
#nativeCalls = 0
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* amount the native token has been iteracted with
|
|
41
|
+
*/
|
|
42
|
+
#nativeTransfers = 0
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* amount of native token burned
|
|
46
|
+
* {Number}
|
|
47
|
+
*/
|
|
48
|
+
#nativeBurns = 0
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* amount of native tokens minted
|
|
52
|
+
* {Number}
|
|
53
|
+
*/
|
|
54
|
+
#nativeMints = 0
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* total amount of transactions
|
|
58
|
+
* {Number}
|
|
59
|
+
*/
|
|
60
|
+
#totalTransactions = 0
|
|
61
|
+
|
|
21
62
|
#participants = []
|
|
22
63
|
#participating = false
|
|
23
64
|
#jail = []
|
|
24
65
|
|
|
25
66
|
constructor() {
|
|
67
|
+
super()
|
|
26
68
|
return this.#init()
|
|
27
69
|
}
|
|
28
70
|
|
|
71
|
+
get nativeMints() {
|
|
72
|
+
return this.#nativeMints
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
get nativeBurns() {
|
|
76
|
+
return this.#nativeBurns
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
get nativeTransfers() {
|
|
80
|
+
return this.#nativeTransfers
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
get totalTransactions() {
|
|
84
|
+
return this.#totalTransactions
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get nativeCalls() {
|
|
88
|
+
return this.#nativeCalls
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get totalSize() {
|
|
92
|
+
return this.#totalSize
|
|
93
|
+
}
|
|
94
|
+
|
|
29
95
|
get lib() {
|
|
30
96
|
return lib
|
|
31
97
|
}
|
|
@@ -161,6 +227,8 @@ export default class Chain {
|
|
|
161
227
|
|
|
162
228
|
|
|
163
229
|
this.utils = { BigNumber, formatUnits, parseUnits }
|
|
230
|
+
|
|
231
|
+
this.state = new State()
|
|
164
232
|
|
|
165
233
|
try {
|
|
166
234
|
let localBlock
|
|
@@ -244,16 +312,6 @@ export default class Chain {
|
|
|
244
312
|
|
|
245
313
|
#epochTimeout
|
|
246
314
|
|
|
247
|
-
async #addTransaction(transaction) {
|
|
248
|
-
try {
|
|
249
|
-
transaction = await new TransactionMessage(transaction)
|
|
250
|
-
const has = await transactionPoolStore.has(await transaction.hash)
|
|
251
|
-
if (!has) await transactionPoolStore.put(await transaction.hash, transaction.encoded)
|
|
252
|
-
if (this.#participating && !this.#runningEpoch) this.#runEpoch()
|
|
253
|
-
} catch {
|
|
254
|
-
throw new Error('invalid transaction')
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
315
|
|
|
258
316
|
async #lastBlockHandler() {
|
|
259
317
|
return new peernet.protos['peernet-response']({response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index }})
|
|
@@ -265,6 +323,7 @@ async resolveBlock(hash) {
|
|
|
265
323
|
block = await new BlockMessage(block)
|
|
266
324
|
if (!await peernet.has(hash, 'block')) await peernet.put(hash, block.encoded, 'block')
|
|
267
325
|
const size = block.encoded.length > 0 ? block.encoded.length : block.encoded.byteLength
|
|
326
|
+
this.#totalSize += size
|
|
268
327
|
block = {...block.decoded, hash}
|
|
269
328
|
if (this.#blocks[block.index] && this.#blocks[block.index].hash !== block.hash) throw `invalid block ${hash} @${block.index}`
|
|
270
329
|
this.#blocks[block.index] = block
|
|
@@ -290,37 +349,29 @@ async resolveBlock(hash) {
|
|
|
290
349
|
}
|
|
291
350
|
}
|
|
292
351
|
|
|
352
|
+
/**
|
|
353
|
+
*
|
|
354
|
+
* @param {Block[]} blocks
|
|
355
|
+
*/
|
|
293
356
|
async #loadBlocks(blocks) {
|
|
294
357
|
for (const block of blocks) {
|
|
295
358
|
if (block && !block.loaded) {
|
|
296
359
|
for (const transaction of block.transactions) {
|
|
297
360
|
try {
|
|
298
361
|
await this.#machine.execute(transaction.to, transaction.method, transaction.params)
|
|
299
|
-
|
|
362
|
+
if (transaction.to === nativeToken) {
|
|
363
|
+
this.#nativeCalls += 1
|
|
364
|
+
if (transaction.method === 'burn') this.#nativeBurns += 1
|
|
365
|
+
if (transaction.method === 'mint') this.#nativeMints += 1
|
|
366
|
+
if (transaction.method === 'transfer') this.#nativeTransfers += 1
|
|
367
|
+
}
|
|
368
|
+
this.#totalTransactions += 1
|
|
300
369
|
} catch (error) {
|
|
301
370
|
console.log(error);
|
|
302
371
|
}
|
|
303
372
|
}
|
|
304
373
|
this.#blocks[block.index].loaded = true
|
|
305
|
-
|
|
306
|
-
// let message = await peernet.get(block.hash, 'block')
|
|
307
|
-
|
|
308
|
-
// const compressed = pako.deflate(message);
|
|
309
|
-
// const result = pako.inflate(compressed);
|
|
310
|
-
// console.log(result.length, compressed.length);
|
|
311
|
-
//
|
|
312
|
-
// console.log(result.length - compressed.length);
|
|
313
|
-
|
|
314
|
-
// message = new BlockMessage(message)
|
|
315
|
-
// for (const transaction of message.decoded.transactions) {
|
|
316
|
-
// try {
|
|
317
|
-
// await this.#machine.execute(transaction.to, transaction.method, transaction.params)
|
|
318
|
-
//
|
|
319
|
-
// } catch (e) {
|
|
320
|
-
// // console.log(e);
|
|
321
|
-
// }
|
|
322
|
-
// }
|
|
323
|
-
// block.loaded = true
|
|
374
|
+
debug(`loaded block: ${block.hash} @${block.index}`);
|
|
324
375
|
}
|
|
325
376
|
}
|
|
326
377
|
}
|
|
@@ -340,25 +391,9 @@ async resolveBlock(hash) {
|
|
|
340
391
|
async #addBlock(block) {
|
|
341
392
|
// console.log(block);
|
|
342
393
|
const blockMessage = await new BlockMessage(new Uint8Array(Object.values(block)))
|
|
343
|
-
|
|
344
|
-
// const transactionJob = async transaction => {
|
|
345
|
-
// try {
|
|
346
|
-
// transaction = await transactionPoolStore.get(transaction)
|
|
347
|
-
// } catch (e) {
|
|
348
|
-
// try {
|
|
349
|
-
// transaction = await peernet.get(transaction, 'transaction')
|
|
350
|
-
// } catch (e) {
|
|
351
|
-
// console.warn(`couldn't resolve ${transaction}`);
|
|
352
|
-
// }
|
|
353
|
-
// }
|
|
354
|
-
// transaction = new TransactionMessage(transaction)
|
|
355
|
-
// return transaction
|
|
356
|
-
// }
|
|
357
|
-
const deletions = await Promise.all(blockMessage.decoded.transactions
|
|
394
|
+
await Promise.all(blockMessage.decoded.transactions
|
|
358
395
|
.map(async transaction => transactionPoolStore.delete(await transaction.hash)))
|
|
359
396
|
const hash = await blockMessage.hash
|
|
360
|
-
// let transactions = blockMessage.decoded.transactions.map(tx => transactionJob(tx))
|
|
361
|
-
// transactions = await Promise.all(transactions)
|
|
362
397
|
|
|
363
398
|
await blockStore.put(hash, blockMessage.encoded)
|
|
364
399
|
|
|
@@ -401,6 +436,7 @@ async resolveBlock(hash) {
|
|
|
401
436
|
async #updateState(message) {
|
|
402
437
|
const hash = await message.hash
|
|
403
438
|
this.#lastBlock = { hash, ...message.decoded }
|
|
439
|
+
await this.state.updateState(message)
|
|
404
440
|
await chainStore.put('lastBlock', hash)
|
|
405
441
|
}
|
|
406
442
|
|
|
@@ -416,12 +452,6 @@ async resolveBlock(hash) {
|
|
|
416
452
|
this.#participating = true
|
|
417
453
|
if (!await this.staticCall(addresses.validators, 'has', [address])) await this.createTransactionFrom(address, addresses.validators, 'addValidator', [address])
|
|
418
454
|
if (await this.hasTransactionToHandle() && !this.#runningEpoch) await this.#runEpoch()
|
|
419
|
-
|
|
420
|
-
// const runEpoch = () => setTimeout(async () => {
|
|
421
|
-
// if (await this.hasTransactionToHandle() && !this.#runningEpoch) await this.#runEpoch()
|
|
422
|
-
// runEpoch()
|
|
423
|
-
// }, 5000)
|
|
424
|
-
// runEpoch()
|
|
425
455
|
}
|
|
426
456
|
|
|
427
457
|
calculateFee(transaction) {
|
|
@@ -431,25 +461,6 @@ async resolveBlock(hash) {
|
|
|
431
461
|
return (transaction.encoded.length / 1024) / 1e-6
|
|
432
462
|
}
|
|
433
463
|
|
|
434
|
-
async getTransactions (transactions) {
|
|
435
|
-
return new Promise(async (resolve, reject) => {
|
|
436
|
-
let size = 0
|
|
437
|
-
const _transactions = []
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
const promises = await Promise.all(transactions
|
|
441
|
-
.map(async tx => {
|
|
442
|
-
tx = await new TransactionMessage(tx)
|
|
443
|
-
size += tx.encoded.length
|
|
444
|
-
if (!formatBytes(size).includes('MB') || formatBytes(size).includes('MB') && Number(formatBytes(size).split(' MB')[0]) <= 0.75) _transactions.push({...tx.decoded, hash: await tx.hash})
|
|
445
|
-
else resolve(_transactions)
|
|
446
|
-
}))
|
|
447
|
-
|
|
448
|
-
return resolve(_transactions)
|
|
449
|
-
})
|
|
450
|
-
|
|
451
|
-
}
|
|
452
|
-
|
|
453
464
|
// todo filter tx that need to wait on prev nonce
|
|
454
465
|
async #createBlock(limit = 1800) {
|
|
455
466
|
// vote for transactions
|
|
@@ -575,58 +586,18 @@ async resolveBlock(hash) {
|
|
|
575
586
|
// transactionStore.put(message.hash, message.encoded)
|
|
576
587
|
}
|
|
577
588
|
|
|
578
|
-
|
|
579
|
-
transactions = await Promise.all(transactions.map(tx => new TransactionMessage(tx)))
|
|
580
|
-
return transactions
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
async promiseTransactionsContent(transactions) {
|
|
584
|
-
transactions = await Promise.all(transactions.map(tx => new Promise(async (resolve, reject) => {
|
|
585
|
-
resolve({ ...tx.decoded, hash: await tx.hash })
|
|
586
|
-
})))
|
|
587
|
-
|
|
588
|
-
return transactions
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
async #getNonceFallback(address) {
|
|
592
|
-
let transactions = await transactionPoolStore.values()
|
|
593
|
-
transactions = await this.promiseTransactions(transactions)
|
|
594
|
-
transactions = transactions.filter(tx => tx.decoded.from === address)
|
|
595
|
-
transactions = await this.promiseTransactionsContent(transactions)
|
|
596
|
-
|
|
597
|
-
if (this.lastBlock?.hash && transactions.length === 0 && this.lastBlock.hash !== '0x0') {
|
|
598
|
-
|
|
599
|
-
let block = await peernet.get(this.lastBlock.hash)
|
|
600
|
-
block = await new BlockMessage(block)
|
|
601
|
-
|
|
602
|
-
// for (let tx of block.decoded?.transactions) {
|
|
603
|
-
// tx = await peernet.get(tx, 'transaction')
|
|
604
|
-
// transactions.push(new TransactionMessage(tx))
|
|
605
|
-
// }
|
|
606
|
-
transactions = transactions.filter(tx => tx.from === address)
|
|
607
|
-
while (transactions.length === 0 && block.decoded.index !== 0 && block.decoded.previousHash !== '0x0') {
|
|
608
|
-
block = await blockStore.get(block.decoded.previousHash)
|
|
609
|
-
block = await new BlockMessage(block)
|
|
610
|
-
transactions = block.decoded.transactions.filter(tx => tx.from === address)
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
}
|
|
614
|
-
if (transactions.length === 0) return 0
|
|
615
|
-
|
|
616
|
-
transactions = transactions.sort((a, b) => a.timestamp - b.timestamp)
|
|
617
|
-
return transactions[transactions.length - 1].nonce
|
|
618
|
-
}
|
|
589
|
+
|
|
619
590
|
|
|
620
|
-
async
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
await
|
|
591
|
+
async #addTransaction(transaction) {
|
|
592
|
+
try {
|
|
593
|
+
transaction = await new TransactionMessage(transaction)
|
|
594
|
+
const has = await transactionPoolStore.has(await transaction.hash)
|
|
595
|
+
if (!has) await transactionPoolStore.put(await transaction.hash, transaction.encoded)
|
|
596
|
+
if (this.#participating && !this.#runningEpoch) this.#runEpoch()
|
|
597
|
+
} catch {
|
|
598
|
+
throw new Error('invalid transaction')
|
|
624
599
|
}
|
|
625
|
-
let nonce = await accountsStore.get(address)
|
|
626
|
-
nonce = new TextDecoder().decode(nonce)
|
|
627
|
-
return Number(nonce)
|
|
628
600
|
}
|
|
629
|
-
|
|
630
601
|
/**
|
|
631
602
|
* whenever method = createContract params should hold the contract hash
|
|
632
603
|
*
|
|
@@ -638,72 +609,9 @@ async resolveBlock(hash) {
|
|
|
638
609
|
* @param {Array} params - array of paramters to apply to the contract method
|
|
639
610
|
* @param {Number} nonce - total transaction count [optional]
|
|
640
611
|
*/
|
|
641
|
-
|
|
612
|
+
async createTransaction(to, method, parameters, nonce, signature) {
|
|
642
613
|
return this.createTransactionFrom(peernet.selectedAccount, to, method, parameters, nonce)
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
/**
|
|
648
|
-
*
|
|
649
|
-
* @param {Transaction} transaction
|
|
650
|
-
* @param {String} transaction.from address
|
|
651
|
-
* @param {String} transaction.to address
|
|
652
|
-
* @param {Object} transaction.params {}
|
|
653
|
-
* @param {String} transaction.params.method get, call
|
|
654
|
-
* @param {Buffer} transaction.params.data
|
|
655
|
-
* @returns
|
|
656
|
-
*/
|
|
657
|
-
async createTransactionHash(transaction) {
|
|
658
|
-
// todo: validate
|
|
659
|
-
const peernetHash = await new CodecHash(transaction, {name: 'transaction-message'})
|
|
660
|
-
return peernetHash.digest
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
/**
|
|
664
|
-
* @param {Transaction} transaction
|
|
665
|
-
* @param {object} wallet any wallet/signer that supports sign(RAWtransaction)
|
|
666
|
-
*/
|
|
667
|
-
async #signTransaction (transaction, wallet) {
|
|
668
|
-
return wallet.sign(await this.createTransactionHash(transaction))
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
async signTransaction(transaction, signer) {
|
|
672
|
-
let identity = await walletStore.get('identity')
|
|
673
|
-
identity = JSON.parse(new TextDecoder().decode(identity))
|
|
674
|
-
const wallet = new MultiWallet(peernet.network)
|
|
675
|
-
wallet.recover(identity.mnemonic)
|
|
676
|
-
const account = wallet.account(0).external(0)
|
|
677
|
-
transaction.signature = await this.#signTransaction(transaction, account)
|
|
678
|
-
transaction.signature = bs32.encode(transaction.signature)
|
|
679
|
-
return transaction
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
/**
|
|
683
|
-
*
|
|
684
|
-
* @param {Transaction} transaction
|
|
685
|
-
* @param {Address} transaction.from
|
|
686
|
-
* @param {Address} transaction.to
|
|
687
|
-
* @param {String} transaction.method
|
|
688
|
-
* @param {Array} transaction.params
|
|
689
|
-
* @param {Number} transaction.nonce
|
|
690
|
-
*
|
|
691
|
-
* @returns {RawTransaction} transaction
|
|
692
|
-
*/
|
|
693
|
-
async createRawTransaction(transaction) {
|
|
694
|
-
if (!transaction.from) transaction.from = peernet.selectedAccount
|
|
695
|
-
transaction.timestamp = Date.now()
|
|
696
|
-
|
|
697
|
-
if (transaction.nonce === undefined) {
|
|
698
|
-
transaction.nonce = await this.getNonce(transaction.from)
|
|
699
|
-
} else {
|
|
700
|
-
let nonce = await accountsStore.get(transaction.from)
|
|
701
|
-
nonce = new TextDecoder().decode(nonce)
|
|
702
|
-
if (transaction.nonce < nonce) throw new Error(`a transaction with a higher nonce already exists`)
|
|
703
|
-
if (transaction.nonce === nonce) throw new Error(`a transaction with the same nonce already exists`)
|
|
704
|
-
}
|
|
705
|
-
return transaction
|
|
706
|
-
}
|
|
614
|
+
}
|
|
707
615
|
/**
|
|
708
616
|
* every tx done is trough contracts so no need for amount
|
|
709
617
|
* data is undefined when nothing is returned
|
|
@@ -715,83 +623,10 @@ async #signTransaction (transaction, wallet) {
|
|
|
715
623
|
* @param {Array} params - array of paramters to apply to the contract method
|
|
716
624
|
* @param {Number} nonce - total transaction count [optional]
|
|
717
625
|
*/
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
const rawTransaction = await this.createRawTransaction({from, to, nonce, method, params: parameters})
|
|
723
|
-
const transaction = await this.signTransaction(rawTransaction, from)
|
|
724
|
-
const message = await new TransactionMessage(transaction)
|
|
725
|
-
|
|
726
|
-
let data
|
|
727
|
-
// await transactionPoolStore.put(message.hash, new TextEncoder().encode(JSON.stringify({signature, message: message.encoded})))
|
|
728
|
-
const wait = () => new Promise(async (resolve, reject) => {
|
|
729
|
-
if (pubsub.subscribers[`transaction.completed.${await message.hash}`]) {
|
|
730
|
-
const result = pubsub.subscribers[`transaction.completed.${await message.hash}`].value
|
|
731
|
-
result.status === 'fulfilled' ? resolve(await result.hash) : reject({hash: await result.hash, error: result.error})
|
|
732
|
-
} else {
|
|
733
|
-
const completed = async result => {
|
|
734
|
-
result.status === 'fulfilled' ? resolve(await result.hash) : reject({hash: await result.hash, error: result.error})
|
|
735
|
-
|
|
736
|
-
setTimeout(async () => {
|
|
737
|
-
pubsub.unsubscribe(`transaction.completed.${await message.hash}`, completed)
|
|
738
|
-
}, 10_000)
|
|
739
|
-
}
|
|
740
|
-
pubsub.subscribe(`transaction.completed.${await message.hash}`, completed)
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
})
|
|
745
|
-
|
|
746
|
-
await transactionPoolStore.put(await message.hash, message.encoded)
|
|
747
|
-
peernet.publish('add-transaction', message.encoded)
|
|
748
|
-
this.#addTransaction(message.encoded)
|
|
749
|
-
debug('creating tx')
|
|
750
|
-
return {hash: await message.hash, data, fee: await calculateFee(message.decoded), wait}
|
|
751
|
-
} catch (error) {
|
|
752
|
-
console.log(error)
|
|
753
|
-
throw error
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
/**
|
|
759
|
-
*
|
|
760
|
-
* @param {Address} creator
|
|
761
|
-
* @param {String} contract
|
|
762
|
-
* @param {Array} constructorParameters
|
|
763
|
-
* @returns lib.createContractMessage
|
|
764
|
-
*/
|
|
765
|
-
async createContractMessage(creator, contract, constructorParameters = []) {
|
|
766
|
-
return createContractMessage(creator, contract, constructorParameters)
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
/**
|
|
770
|
-
*
|
|
771
|
-
* @param {Address} creator
|
|
772
|
-
* @param {String} contract
|
|
773
|
-
* @param {Array} constructorParameters
|
|
774
|
-
* @returns {Address}
|
|
775
|
-
*/
|
|
776
|
-
async createContractAddress(creator, contract, constructorParameters = []) {
|
|
777
|
-
contract = await this.createContractMessage(creator, contract, constructorParameters)
|
|
778
|
-
return contract.hash
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
/**
|
|
782
|
-
*
|
|
783
|
-
* @param {String} contract
|
|
784
|
-
* @param {Array} parameters
|
|
785
|
-
* @returns
|
|
786
|
-
*/
|
|
787
|
-
async deployContract(contract, constructorParameters = []) {
|
|
788
|
-
const message = await createContractMessage(peernet.selectedAccount, contract, constructorParameters)
|
|
789
|
-
try {
|
|
790
|
-
await contractStore.put(await message.hash, message.encoded)
|
|
791
|
-
} catch (error) {
|
|
792
|
-
throw error
|
|
793
|
-
}
|
|
794
|
-
return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash])
|
|
626
|
+
async createTransactionFrom(from, to, method, parameters, nonce) {
|
|
627
|
+
const event = await super.createTransactionFrom(from, to, method, parameters, nonce)
|
|
628
|
+
this.#addTransaction(event.message.encoded)
|
|
629
|
+
return event
|
|
795
630
|
}
|
|
796
631
|
|
|
797
632
|
/**
|
package/src/contract.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Transaction from "./transaction";
|
|
2
|
+
import { createContractMessage } from './../../lib/src/lib'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @extends {Transaction}
|
|
6
|
+
*/
|
|
7
|
+
export default class Contract extends Transaction {
|
|
8
|
+
constructor() {
|
|
9
|
+
super()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param {Address} creator
|
|
15
|
+
* @param {String} contract
|
|
16
|
+
* @param {Array} constructorParameters
|
|
17
|
+
* @returns lib.createContractMessage
|
|
18
|
+
*/
|
|
19
|
+
async createContractMessage(creator, contract, constructorParameters = []) {
|
|
20
|
+
return createContractMessage(creator, contract, constructorParameters)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* @param {Address} creator
|
|
26
|
+
* @param {String} contract
|
|
27
|
+
* @param {Array} constructorParameters
|
|
28
|
+
* @returns {Address}
|
|
29
|
+
*/
|
|
30
|
+
async createContractAddress(creator, contract, constructorParameters = []) {
|
|
31
|
+
contract = await this.createContractMessage(creator, contract, constructorParameters)
|
|
32
|
+
return contract.hash
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @param {String} contract
|
|
38
|
+
* @param {Array} parameters
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
async deployContract(contract, constructorParameters = []) {
|
|
42
|
+
const message = await createContractMessage(peernet.selectedAccount, contract, constructorParameters)
|
|
43
|
+
try {
|
|
44
|
+
await contractStore.put(await message.hash, message.encoded)
|
|
45
|
+
} catch (error) {
|
|
46
|
+
throw error
|
|
47
|
+
}
|
|
48
|
+
return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash])
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
}
|
package/src/protocol.js
ADDED
package/src/state.js
CHANGED
|
@@ -15,7 +15,7 @@ export default class State {
|
|
|
15
15
|
// }
|
|
16
16
|
|
|
17
17
|
async put(key, value, isCompressed = true) {
|
|
18
|
-
value = isCompressed ?
|
|
18
|
+
value = isCompressed ? value : await pako.deflate(value)
|
|
19
19
|
await stateStore.put(key, value)
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -26,6 +26,6 @@ export default class State {
|
|
|
26
26
|
|
|
27
27
|
updateState(block) {
|
|
28
28
|
// block.decoded.index
|
|
29
|
-
this.#isUpdateNeeded()
|
|
29
|
+
// this.#isUpdateNeeded()
|
|
30
30
|
}
|
|
31
31
|
}
|