@leofcoin/chain 1.0.23 → 1.0.24

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.
Files changed (49) hide show
  1. package/docs/@leofcoin/chain/1.0.24/chain.js.html +891 -0
  2. package/docs/@leofcoin/chain/1.0.24/contracts_factory.js.html +182 -0
  3. package/docs/@leofcoin/chain/1.0.24/contracts_nameService.js.html +231 -0
  4. package/docs/@leofcoin/chain/1.0.24/contracts_proxies_factoryProxy.js.html +135 -0
  5. package/docs/@leofcoin/chain/1.0.24/contracts_proxies_nameServiceProxy.js.html +135 -0
  6. package/docs/@leofcoin/chain/1.0.24/contracts_proxies_nativeTokenProxy.js.html +135 -0
  7. package/docs/@leofcoin/chain/1.0.24/contracts_proxies_validatorsProxy.js.html +135 -0
  8. package/docs/@leofcoin/chain/1.0.24/contracts_proxies_votingProxy.js.html +135 -0
  9. package/docs/@leofcoin/chain/1.0.24/contracts_validators.js.html +242 -0
  10. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Bold-webfont.eot +0 -0
  11. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Bold-webfont.svg +1830 -0
  12. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Bold-webfont.woff +0 -0
  13. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  14. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
  15. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  16. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Italic-webfont.eot +0 -0
  17. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Italic-webfont.svg +1830 -0
  18. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Italic-webfont.woff +0 -0
  19. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Light-webfont.eot +0 -0
  20. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Light-webfont.svg +1831 -0
  21. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Light-webfont.woff +0 -0
  22. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  23. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
  24. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  25. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Regular-webfont.eot +0 -0
  26. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Regular-webfont.svg +1831 -0
  27. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Regular-webfont.woff +0 -0
  28. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Semibold-webfont.eot +0 -0
  29. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Semibold-webfont.svg +1830 -0
  30. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Semibold-webfont.ttf +0 -0
  31. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-Semibold-webfont.woff +0 -0
  32. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-SemiboldItalic-webfont.eot +0 -0
  33. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-SemiboldItalic-webfont.svg +1830 -0
  34. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-SemiboldItalic-webfont.ttf +0 -0
  35. package/docs/@leofcoin/chain/1.0.24/fonts/OpenSans-SemiboldItalic-webfont.woff +0 -0
  36. package/docs/@leofcoin/chain/1.0.24/global.html +1805 -0
  37. package/docs/@leofcoin/chain/1.0.24/icons/home.svg +4 -0
  38. package/docs/@leofcoin/chain/1.0.24/icons/search.svg +4 -0
  39. package/docs/@leofcoin/chain/1.0.24/index.html +125 -0
  40. package/docs/@leofcoin/chain/1.0.24/machine.js.html +255 -0
  41. package/docs/@leofcoin/chain/1.0.24/scripts/linenumber.js +23 -0
  42. package/docs/@leofcoin/chain/1.0.24/scripts/pagelocation.js +89 -0
  43. package/docs/@leofcoin/chain/1.0.24/standards_roles.js.html +188 -0
  44. package/docs/@leofcoin/chain/1.0.24/standards_token.js.html +260 -0
  45. package/docs/@leofcoin/chain/1.0.24/styles/collapse.css +27 -0
  46. package/docs/@leofcoin/chain/1.0.24/styles/jsdoc-default.css +953 -0
  47. package/docs/@leofcoin/chain/1.0.24/styles/prettify-jsdoc.css +111 -0
  48. package/docs/@leofcoin/chain/1.0.24/styles/prettify-tomorrow.css +138 -0
  49. package/package.json +1 -2
@@ -0,0 +1,891 @@
1
+
2
+
3
+ <!DOCTYPE html>
4
+ <html lang="en">
5
+ <head>
6
+
7
+ <meta charset="utf-8">
8
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
9
+
10
+ <title>
11
+ chain.js - Documentation
12
+ </title>
13
+
14
+ <link href="https://www.braintreepayments.com/images/favicon-ccda0b14.png" rel="icon" type="image/png">
15
+
16
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>
17
+ <script>hljs.initHighlightingOnLoad();</script>
18
+
19
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
20
+
21
+ <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
22
+ <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
23
+
24
+ <link type="text/css" rel="stylesheet" href="styles/collapse.css">
25
+
26
+
27
+
28
+
29
+ <!-- start Mixpanel -->
30
+ <script type="text/javascript">(function(e,a){if(!a.__SV){var b=window;try{var c,l,i,j=b.location,g=j.hash;c=function(a,b){return(l=a.match(RegExp(b+"=([^&]*)")))?l[1]:null};g&&c(g,"state")&&(i=JSON.parse(decodeURIComponent(c(g,"state"))),"mpeditor"===i.action&&(b.sessionStorage.setItem("_mpcehash",g),history.replaceState(i.desiredHash||"",e.title,j.pathname+j.search)))}catch(m){}var k,h;window.mixpanel=a;a._i=[];a.init=function(b,c,f){function e(b,a){var c=a.split(".");2==c.length&&(b=b[c[0]],a=c[1]);b[a]=function(){b.push([a].concat(Array.prototype.slice.call(arguments,
31
+ 0)))}}var d=a;"undefined"!==typeof f?d=a[f]=[]:f="mixpanel";d.people=d.people||[];d.toString=function(b){var a="mixpanel";"mixpanel"!==f&&(a+="."+f);b||(a+=" (stub)");return a};d.people.toString=function(){return d.toString(1)+".people (stub)"};k="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
32
+ for(h=0;h<k.length;h++)e(d,k[h]);a._i.push([b,c,f])};a.__SV=1.2;b=e.createElement("script");b.type="text/javascript";b.async=!0;b.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";c=e.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)}})(document,window.mixpanel||[]);
33
+ mixpanel.init("1919205b2da72e4da3b9b6639b444d59");</script>
34
+ <!-- end Mixpanel -->
35
+ </head>
36
+
37
+ <body>
38
+ <svg style="display: none;">
39
+ <defs>
40
+ <symbol id="linkIcon" fill="#706d77" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
41
+ <path d="M0 0h24v24H0z" fill="none"/>
42
+ <path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/>
43
+ </symbol>
44
+ </defs>
45
+ </svg>
46
+
47
+ <input type="checkbox" id="nav-trigger" class="nav-trigger" />
48
+ <label for="nav-trigger" class="navicon-button x">
49
+ <div class="navicon"></div>
50
+ </label>
51
+
52
+ <label for="nav-trigger" class="overlay"></label>
53
+
54
+ <div class="top-nav-wrapper">
55
+ <ul>
56
+ <li >
57
+ <a href="index.html">
58
+
59
+ <svg fill="#6D6D6D" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
60
+ <path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/>
61
+ <path d="M0 0h24v24H0z" fill="none"/>
62
+ </svg>
63
+
64
+
65
+ </a>
66
+ </li>
67
+
68
+
69
+
70
+ </ul>
71
+ </div>
72
+
73
+ <nav>
74
+ <h3 class="reference-title">
75
+ @leofcoin/chain
76
+ </h3>
77
+
78
+
79
+ <h3>
80
+ Resources
81
+ </h3>
82
+
83
+ <a href="https://github.com/arteontoken/monorepo/tree/main/chain">github</a>
84
+
85
+
86
+
87
+ <h3 id="global-nav">Global</h3><ul><li><a href="global.html#addContract">addContract</a></li><li><a href="global.html#createRawTransaction">createRawTransaction</a></li><li><a href="global.html#createTransaction">createTransaction</a></li><li><a href="global.html#createTransactionFrom">createTransactionFrom</a></li><li><a href="global.html#createTransactionHash">createTransactionHash</a></li><li><a href="global.html#deployContract">deployContract</a></li><li><a href="global.html#lookup">lookup</a></li><li><a href="global.html#state">state</a></li></ul>
88
+ </nav>
89
+
90
+ <div id="main">
91
+
92
+ <h1 class="page-title">
93
+ chain.js
94
+ </h1>
95
+
96
+
97
+
98
+
99
+
100
+ <section>
101
+ <article>
102
+ <pre class="prettyprint source linenums"><code>import { BigNumber, formatUnits, parseUnits } from '@leofcoin/utils'
103
+ import Machine from './machine.js'
104
+ import { ContractMessage, TransactionMessage, BlockMessage, BWMessage, BWRequestMessage } from './../../messages/src/messages'
105
+ import { addresses } from './../../addresses/src/addresses'
106
+ import { contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage, calculateFee } from './../../lib/src/lib'
107
+ import MultiWallet from '@leofcoin/multi-wallet'
108
+ import {CodecHash} from '@leofcoin/codec-format-interface/dist/index'
109
+ import bs32 from '@vandeurenglenn/base32'
110
+ import config from './config/config'
111
+ import { formatBytes } from '../../utils/src/utils.js'
112
+
113
+ globalThis.BigNumber = BigNumber
114
+
115
+ // check if browser or local
116
+ export default class Chain {
117
+ #validators = []
118
+ #blocks = []
119
+ #machine
120
+ #runningEpoch = false
121
+ #lastBlock = {index: 0, previousHash: '0x0'}
122
+
123
+ constructor() {
124
+ return this.#init()
125
+ }
126
+
127
+ get lib() {
128
+ return lib
129
+ }
130
+
131
+ get lastBlock() {
132
+ return this.#lastBlock
133
+ }
134
+
135
+ get nativeToken() {
136
+ return addresses.nativeToken
137
+ }
138
+
139
+ get validators() {
140
+ return [...this.#validators]
141
+ }
142
+
143
+ get blocks() {
144
+ return [...this.#blocks]
145
+ }
146
+
147
+ async hasTransactionToHandle() {
148
+ const size = await transactionPoolStore.size()
149
+ console.log({size});
150
+ if (size > 0) return true
151
+ return false
152
+ }
153
+
154
+ async #runEpoch() {
155
+ this.#runningEpoch = true
156
+ console.log('epoch');
157
+ const validators = await this.staticCall(addresses.validators, 'validators')
158
+ if (!validators[peernet.id]?.active) return
159
+ console.log('active');
160
+
161
+
162
+ const start = new Date().getTime()
163
+ try {
164
+ await this.#createBlock()
165
+ } catch (e) {
166
+ console.error(e);
167
+ }
168
+
169
+ const end = new Date().getTime()
170
+ console.log(((end - start) / 1000) + ' s');
171
+
172
+ if (await this.hasTransactionToHandle()) return this.#runEpoch()
173
+ this.#runningEpoch = false
174
+ // if (await this.hasTransactionToHandle() &amp;&amp; !this.#runningEpoch) return this.#runEpoch()
175
+ }
176
+
177
+ async #setup() {
178
+ await contractStore.put(addresses.contractFactory, contractFactoryMessage)
179
+ await contractStore.put(addresses.nativeToken, nativeTokenMessage)
180
+ await contractStore.put(addresses.validators, validatorsMessage)
181
+ await contractStore.put(addresses.nameService, nameServiceMessage)
182
+ console.log('handle native contracts');
183
+ // handle native contracts
184
+ }
185
+
186
+ async #sync() {
187
+ let promises = []
188
+ for (const peer of peernet.connections) {
189
+ if (peer.peerId !== this.id) {
190
+ let data = new peernet.protos['peernet-request']({request: 'lastBlock'})
191
+
192
+ const node = await peernet.prepareMessage(id, data.encoded)
193
+ promises.push(peer.request(node.encoded))
194
+ }
195
+ }
196
+ // if (this.)
197
+
198
+ promises = await Promise.allSettled(promises)
199
+ promises = promises.reduce((set, c) => {
200
+ if (c.index > set.index) {
201
+ set.index = c.index
202
+ set.hash = c.hash
203
+ }
204
+ return set
205
+ }, {index: 0, hash: '0x0'})
206
+ // get lastblock
207
+ }
208
+
209
+ async #init() {
210
+ // this.node = await new Node()
211
+ this.participants = []
212
+ this.participating = false
213
+ const initialized = await contractStore.has(addresses.contractFactory)
214
+ if (!initialized) await this.#setup()
215
+
216
+ this.#machine = await new Machine()
217
+ this.utils = { BigNumber, formatUnits, parseUnits }
218
+
219
+ try {
220
+ let localBlock = await chainStore.get('lastBlock')
221
+ localBlock = await peernet.get(new TextDecoder().decode(localBlock))
222
+ localBlock = await await new BlockMessage(localBlock)
223
+ this.#lastBlock = {...localBlock.decoded, hash: await localBlock.hash}
224
+ // console.log(this.lastBlock.decoded.transactions);
225
+ } catch (e) {
226
+ await this.#sync()
227
+ // this.#setup()
228
+ }
229
+
230
+ await peernet.addRequestHandler('bw-request-message', () => {
231
+
232
+ return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
233
+ })
234
+
235
+ await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this))
236
+
237
+ peernet.subscribe('add-block', this.#addBlock.bind(this))
238
+
239
+ peernet.subscribe('add-transaction', this.#addTransaction.bind(this))
240
+
241
+ pubsub.subscribe('peer:connected', this.#peerConnected.bind(this))
242
+
243
+ // load local blocks
244
+ await this.resolveBlocks()
245
+ return this
246
+ }
247
+
248
+ async #peerConnected(peer) {
249
+ let node = new peernet.protos['peernet-request']({request: 'lastBlock'})
250
+ node = await peernet.prepareMessage(peer.id, node.encoded)
251
+ let response = await peer.request(node.encoded)
252
+ response = new Uint8Array(Object.values(response))
253
+ const proto = new globalThis.peernet.protos['peernet-message'](response)
254
+ response = new globalThis.peernet.protos['peernet-response'](proto.decoded.data)
255
+ let lastBlock = JSON.parse(new TextDecoder().decode(response.decoded.response))
256
+
257
+ if (!this.lastBlock || this.lastBlock.index &lt; lastBlock.index) {
258
+ // TODO: check if valid
259
+ const localIndex = this.lastBlock ? this.lastBlock.index : 0
260
+ const index = lastBlock.index
261
+ await this.resolveBlock(lastBlock.hash)
262
+ this.#lastBlock = this.#blocks[this.#blocks.length - 1]
263
+ console.log({lastBlock: this.#lastBlock});
264
+ console.log(this.#blocks);
265
+ let blocksSynced = localIndex > 0 ? localIndex - index : index
266
+ blocksSynced += 1
267
+ debug(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`)
268
+
269
+ const end = this.#blocks.length
270
+ const start = (this.#blocks.length) - blocksSynced
271
+ await this.#loadBlocks(this.#blocks)
272
+ const message = await new BlockMessage(this.lastBlock)
273
+ await blockStore.put(await message.hash, message.encoded)
274
+ await chainStore.put('lastBlock', new TextEncoder().encode(this.lastBlock.hash))
275
+ }
276
+ }
277
+
278
+ #epochTimeout
279
+
280
+ async #addTransaction(transaction) {
281
+ try {
282
+ transaction = await new TransactionMessage(transaction)
283
+ const has = await transactionPoolStore.has(await transaction.hash)
284
+ if (!has) await transactionPoolStore.put(await transaction.hash, transaction.encoded)
285
+ if (this.participating &amp;&amp; !this.#runningEpoch) this.#runEpoch()
286
+ } catch (e) {
287
+ throw Error('invalid transaction')
288
+ }
289
+ }
290
+
291
+ async #lastBlockHandler() {
292
+ return new peernet.protos['peernet-response']({response: new TextEncoder().encode(JSON.stringify({ hash: this.lastBlock?.hash, index: this.lastBlock?.index }))})
293
+ }
294
+
295
+ async resolveBlock(hash) {
296
+ let block = await peernet.get(hash, 'block')
297
+ if (!await peernet.has(hash, 'block')) await peernet.put(hash, block, 'block')
298
+ block = await new BlockMessage(block)
299
+ const size = block.encoded.length || block.encoded.byteLength
300
+ block = {...block.decoded, hash}
301
+ this.#blocks[block.index] = block
302
+ console.log(`loaded block: ${hash} @${block.index} ${formatBytes(size)}`);
303
+ if (block.index !== 0) {
304
+ return this.resolveBlock(block.previousHash)
305
+ }
306
+ }
307
+
308
+ async resolveBlocks() {
309
+ try {
310
+
311
+ const localBlock = await chainStore.get('lastBlock')
312
+ const hash = new TextDecoder().decode(localBlock)
313
+ if (hash !== '0x0')
314
+ await this.resolveBlock(new TextDecoder().decode(localBlock))
315
+ this.#lastBlock = this.#blocks[this.#blocks.length - 1]
316
+ await this.#loadBlocks(this.#blocks)
317
+ } catch (e) {
318
+ await chainStore.put('lastBlock', new TextEncoder().encode('0x0'))
319
+ return this.resolveBlocks()
320
+ // console.log(e);
321
+ }
322
+ }
323
+
324
+ async #loadBlocks(blocks) {
325
+ for (const block of blocks) {
326
+ if (!block.loaded) {
327
+ for (const transaction of block.transactions) {
328
+ try {
329
+ await this.#machine.execute(transaction.to, transaction.method, transaction.params)
330
+
331
+ } catch (e) {
332
+ console.log(e);
333
+ }
334
+ }
335
+ block.loaded = true
336
+ // let message = await peernet.get(block.hash, 'block')
337
+
338
+ // const compressed = pako.deflate(message);
339
+ // const result = pako.inflate(compressed);
340
+ // console.log(result.length, compressed.length);
341
+ //
342
+ // console.log(result.length - compressed.length);
343
+
344
+ // message = new BlockMessage(message)
345
+ // for (const transaction of message.decoded.transactions) {
346
+ // try {
347
+ // await this.#machine.execute(transaction.to, transaction.method, transaction.params)
348
+ //
349
+ // } catch (e) {
350
+ // // console.log(e);
351
+ // }
352
+ // }
353
+ // block.loaded = true
354
+ }
355
+ }
356
+ }
357
+
358
+ async #executeTransaction({hash, from, to, method, params, nonce}) {
359
+ try {
360
+ let result = await this.#machine.execute(to, method, params, from, nonce)
361
+ // if (!result) result = this.#machine.state
362
+ pubsub.publish(`transaction.completed.${hash}`, {status: 'fulfilled', hash})
363
+ return result ? result : 'no state change'
364
+ } catch (e) {
365
+ pubsub.publish(`transaction.completed.${hash}`, {status: 'fail', hash, error: e})
366
+ throw e
367
+ }
368
+ }
369
+
370
+ async #addBlock(block) {
371
+ const blockMessage = await new BlockMessage(block)
372
+ // if (!Buffer.isBuffer(block)) block = Buffer.from(block, 'hex')
373
+ // const transactionJob = async transaction => {
374
+ // try {
375
+ // transaction = await transactionPoolStore.get(transaction)
376
+ // } catch (e) {
377
+ // try {
378
+ // transaction = await peernet.get(transaction, 'transaction')
379
+ // } catch (e) {
380
+ // console.warn(`couldn't resolve ${transaction}`);
381
+ // }
382
+ // }
383
+ // transaction = new TransactionMessage(transaction)
384
+ // return transaction
385
+ // }
386
+ const deletions = await Promise.all(blockMessage.decoded.transactions
387
+ .map(async transaction => transactionPoolStore.delete(await transaction.hash)))
388
+ const hash = await blockMessage.hash
389
+ // let transactions = blockMessage.decoded.transactions.map(tx => transactionJob(tx))
390
+ // transactions = await Promise.all(transactions)
391
+ this.#lastBlock = { hash, ...blockMessage.decoded }
392
+ await blockStore.put(hash, blockMessage.encoded)
393
+ await chainStore.put('lastBlock', new TextEncoder().encode(hash))
394
+ debug(`added block: ${hash}`)
395
+ let promises = []
396
+ let contracts = []
397
+ for (let transaction of blockMessage.decoded.transactions) {
398
+ // await transactionStore.put(transaction.hash, transaction.encoded)
399
+ const index = contracts.indexOf(transaction.to)
400
+ if (index === -1) contracts.push(transaction.to)
401
+ promises.push(this.#executeTransaction(transaction))
402
+ }
403
+ try {
404
+ promises = await Promise.allSettled(promises)
405
+ for (let transaction of blockMessage.decoded.transactions) {
406
+ await accountsStore.put(transaction.from, new TextEncoder().encode(String(transaction.nonce)))
407
+ }
408
+
409
+ // todo finish state
410
+ // for (const contract of contracts) {
411
+ // const state = await this.#machine.get(contract, 'state')
412
+ // // await stateStore.put(contract, state)
413
+ // console.log(state);
414
+ // }
415
+ pubsub.publish('block-processed', blockMessage.decoded)
416
+ } catch (e) {
417
+ console.log(e);
418
+ }
419
+
420
+ }
421
+
422
+ async #updateState() {
423
+
424
+ }
425
+
426
+
427
+
428
+ async participate() {
429
+ // TODO: validate participant
430
+ // hold min amount of 50k ART for 7 days
431
+ // lock the 50k
432
+ // introduce peer-reputation
433
+ // peerReputation(peerId)
434
+ // {bandwith: {up, down}, uptime}
435
+ this.participating = true
436
+ if (!await this.staticCall(addresses.validators, 'has', [peernet.id])) await this.createTransactionFrom(peernet.id, addresses.validators, 'addValidator', [peernet.id])
437
+ if (await this.hasTransactionToHandle() &amp;&amp; !this.#runningEpoch) await this.#runEpoch()
438
+
439
+ // const runEpoch = () => setTimeout(async () => {
440
+ // if (await this.hasTransactionToHandle() &amp;&amp; !this.#runningEpoch) await this.#runEpoch()
441
+ // runEpoch()
442
+ // }, 5000)
443
+ // runEpoch()
444
+ }
445
+
446
+ calculateFee(transaction) {
447
+ // excluded from fees
448
+ // if (transaction.decoded.to === addresses.validators) return 0
449
+ // fee per gb
450
+ return (transaction.encoded.length / 1024) / 1e-6
451
+ }
452
+
453
+ async getTransactions (transactions) {
454
+ return new Promise(async (resolve, reject) => {
455
+ let size = 0
456
+ const _transactions = []
457
+ const promises = await Promise.all(Object.values(transactions)
458
+ .map(tx => new TransactionMessage(new Uint8Array(tx, tx.byteOffset, tx.byteLength))))
459
+
460
+ for (let transaction of promises) {
461
+ size += transaction.encoded.length
462
+ if ((size / 1024) / 1024 &lt;= 1.15 ) _transactions.push({...transaction.decoded, hash: await transaction.hash })
463
+ else return resolve(_transactions)
464
+
465
+ }
466
+ return resolve(_transactions)
467
+ })
468
+
469
+ }
470
+ async #createBlock() {
471
+ let transactions = await transactionPoolStore.get()
472
+
473
+ if (Object.keys(transactions)?.length === 0 ) return
474
+
475
+ let block = {
476
+ transactions: [],
477
+ validators: [],
478
+ fees: 0
479
+ }
480
+
481
+ // exclude failing tx
482
+ transactions = await this.getTransactions(transactions)
483
+
484
+ transactions = transactions.sort((a, b) => a.nonce - b.nonce)
485
+ for (let transaction of transactions) {
486
+ try {
487
+ await this.#executeTransaction(transaction)
488
+ block.transactions.push(transaction)
489
+ block.fees += Number(calculateFee(transaction))
490
+ await accountsStore.put(transaction.from, new TextEncoder().encode(String(transaction.nonce)))
491
+ } catch (e) {
492
+ console.error(e)
493
+ transaction = await new TransactionMessage(transaction)
494
+ await transactionPoolStore.delete(await transaction.hash)
495
+ console.log(e);
496
+ }
497
+ }
498
+ // don't add empty block
499
+ if (block.transactions.length === 0) return
500
+ const validators = await this.staticCall(addresses.validators, 'validators')
501
+ console.log({validators});
502
+ // block.validators = Object.keys(block.validators).reduce((set, key) => {
503
+ // if (block.validators[key].active) {
504
+ // push({
505
+ // address: key
506
+ // })
507
+ // }
508
+ // }, [])
509
+ const peers = {}
510
+ for (const entry of peernet.peerEntries) {
511
+ peers[entry[0]] = entry[1]
512
+ }
513
+ for (const validator of Object.keys(validators)) {
514
+ if (validators[validator].active) {
515
+ const peer = peers[validator]
516
+ if (peer &amp;&amp; peer.connected) {
517
+ let data = await new BWRequestMessage()
518
+ const node = await peernet.prepareMessage(validator, data.encoded)
519
+ try {
520
+ const bw = await peer.request(node.encoded)
521
+ console.log(bw);
522
+ block.validators.push({
523
+ address: validator,
524
+ bw: bw.up + bw.down
525
+ })
526
+ } catch(e) {
527
+
528
+ }
529
+
530
+ } else if (peernet.id === validator) {
531
+ block.validators.push({
532
+ address: peernet.id,
533
+ bw: peernet.bw.up + peernet.bw.down
534
+ })
535
+
536
+ }
537
+ }
538
+
539
+ }
540
+
541
+ console.log({validators: block.validators});
542
+
543
+ block.reward = 150
544
+ block.validators = block.validators.map(validator => {
545
+ validator.reward = String(Number(block.fees) + block.reward / block.validators.length)
546
+ delete validator.bw
547
+ return validator
548
+ })
549
+ // block.validators = calculateValidatorReward(block.validators, block.fees)
550
+
551
+ block.index = this.lastBlock?.index
552
+ if (block.index === undefined) block.index = 0
553
+ else block.index += 1
554
+
555
+ block.previousHash = this.lastBlock?.hash || '0x0'
556
+ block.timestamp = new Date().getTime()
557
+
558
+ const parts = String(block.fees).split('.')
559
+ let decimals = 0
560
+ if (parts[1]) {
561
+ const potentional = parts[1].split('e')
562
+ if (potentional[0] !== parts[1]) {
563
+ parts[1] = potentional[0]
564
+ decimals = Number(potentional[1]?.replace(/\-|\+/g, '')) + Number(potentional[0].length)
565
+ } else {
566
+ decimals = parts[1].length
567
+ }
568
+
569
+ }
570
+ block.fees = Number.parseFloat(String(block.fees)).toFixed(decimals)
571
+
572
+ try {
573
+ let blockMessage = await new BlockMessage(block)
574
+ const hash = await blockMessage.hash
575
+ await Promise.all(blockMessage.decoded.transactions
576
+ .map(async transaction => transactionPoolStore.delete(await transaction.hash)))
577
+
578
+ this.#lastBlock = { hash, ...blockMessage.decoded }
579
+ await blockStore.put(hash, blockMessage.encoded)
580
+ await chainStore.put('lastBlock', new TextEncoder().encode(hash))
581
+ debug(`created block: ${hash}`)
582
+
583
+ peernet.publish('add-block', blockMessage.encoded)
584
+ this.#updateState(blockMessage)
585
+ } catch (e) {
586
+ console.log(e);
587
+ throw Error(`invalid block ${block}`)
588
+ }
589
+ // data = await this.#machine.execute(to, method, params)
590
+ // transactionStore.put(message.hash, message.encoded)
591
+ }
592
+
593
+ async promiseTransactions(transactions) {
594
+ transactions = await Promise.all(transactions.map(tx => new TransactionMessage(tx)))
595
+ return transactions
596
+ }
597
+
598
+ async promiseTransactionsContent(transactions) {
599
+ transactions = await Promise.all(transactions.map(tx => new Promise(async (resolve, reject) => {
600
+ resolve({ ...tx.decoded, hash: await tx.hash })
601
+ })))
602
+
603
+ return transactions
604
+ }
605
+
606
+ async #getNonceFallback(address) {
607
+ let transactions = await transactionPoolStore.get()
608
+ transactions = await this.promiseTransactions(Object.values(transactions))
609
+ transactions = transactions.filter(tx => tx.decoded.from === address)
610
+ transactions = await this.promiseTransactionsContent(transactions)
611
+
612
+ if (this.lastBlock &amp;&amp; transactions.length === 0) {
613
+ let block = await peernet.get(this.lastBlock.hash)
614
+ block = await new BlockMessage(block)
615
+
616
+ // for (let tx of block.decoded?.transactions) {
617
+ // tx = await peernet.get(tx, 'transaction')
618
+ // transactions.push(new TransactionMessage(tx))
619
+ // }
620
+ transactions = transactions.filter(tx => tx.from === address)
621
+ while (transactions.length === 0 &amp;&amp; block.decoded.index !== 0) {
622
+ block = await blockStore.get(block.decoded.previousHash)
623
+ block = await new BlockMessage(block)
624
+ transactions = block.decoded.transactions.filter(tx => tx.from === address)
625
+ }
626
+
627
+ }
628
+ if (transactions.length === 0) return 0
629
+
630
+ transactions = transactions.sort((a, b) => a.timestamp - b.timestamp)
631
+ return transactions[transactions.length - 1].nonce
632
+ }
633
+
634
+ async getNonce(address) {
635
+ if (!await accountsStore.has(address)) {
636
+ const nonce = await this.#getNonceFallback(address)
637
+ await accountsStore.put(address, new TextEncoder().encode(String(nonce)))
638
+ }
639
+ let nonce = await accountsStore.get(address)
640
+ nonce = new TextDecoder().decode(nonce)
641
+ return Number(nonce)
642
+ }
643
+
644
+ /**
645
+ * whenever method = createContract params should hold the contract hash
646
+ *
647
+ * example: [hash]
648
+ * createTransaction('0x0', 'createContract', [hash])
649
+ *
650
+ * @param {String} to - the contract address for the contract to interact with
651
+ * @param {String} method - the method/function to run
652
+ * @param {Array} params - array of paramters to apply to the contract method
653
+ * @param {Number} nonce - total transaction count [optional]
654
+ */
655
+ async createTransaction(to, method, params, nonce, signature) {
656
+ return this.createTransactionFrom(peernet.id, to, method, params, nonce)
657
+ }
658
+
659
+
660
+
661
+ /**
662
+ *
663
+ * @param {Object} transaction {}
664
+ * @param {String} transaction.from address
665
+ * @param {String} transaction.to address
666
+ * @param {Object} transaction.params {}
667
+ * @param {String} transaction.params.method get, call
668
+ * @param {Buffer} transaction.params.data
669
+ * @returns
670
+ */
671
+ async createTransactionHash(transaction) {
672
+ // todo: validate
673
+ const peernetHash = await new CodecHash(transaction, {name: 'transaction-message'})
674
+ return peernetHash.digest
675
+ }
676
+
677
+ /**
678
+ * @params {object} transaction -
679
+ * @params {object} wallet - any wallet/signer that supports sign(RAWtransaction)
680
+ */
681
+ async #signTransaction (transaction, wallet) {
682
+ return wallet.sign(await this.createTransactionHash(transaction))
683
+ }
684
+
685
+ async signTransaction(transaction, signer) {
686
+ let identity = await walletStore.get('identity')
687
+ identity = JSON.parse(new TextDecoder().decode(identity))
688
+ const wallet = new MultiWallet(peernet.network)
689
+ wallet.recover(identity.mnemonic)
690
+ const account = wallet.account(0).external(0)
691
+ transaction.signature = await this.#signTransaction(transaction, wallet)
692
+ transaction.signature = bs32.encode(transaction.signature)
693
+ return transaction
694
+ }
695
+
696
+ /**
697
+ *
698
+ * @param {Object} transaction
699
+ * @param {Address} transaction.from
700
+ * @param {Address} transaction.to
701
+ * @param {String} transaction.method
702
+ * @param {Array} transaction.params
703
+ * @param {Number} transaction.nonce
704
+ *
705
+ * @returns {Object} transaction
706
+ */
707
+ async createRawTransaction(transaction) {
708
+ if (!transaction.from) transaction.from = peernet.id
709
+ transaction.timestamp = Date.now()
710
+
711
+ if (transaction.nonce === undefined) {
712
+ transaction.nonce = await this.getNonce(transaction.from)
713
+ transaction.nonce += 1
714
+ await accountsStore.put(transaction.from, new TextEncoder().encode(String(transaction.nonce)))
715
+ } else {
716
+ let nonce = await accountsStore.get(transaction.from)
717
+ nonce = new TextDecoder().decode(nonce)
718
+ if (transaction.nonce &lt; nonce) throw Error(`a transaction with a higher nonce already exists`)
719
+ if (transaction.nonce === nonce) throw Error(`a transaction with the same nonce already exists`)
720
+ }
721
+ return transaction
722
+ }
723
+ /**
724
+ * every tx done is trough contracts so no need for amount
725
+ * data is undefined when nothing is returned
726
+ * error is thrown on error so undefined data doesn't mean there is an error...
727
+ *
728
+ * @param {String} from - the sender address
729
+ * @param {String} to - the contract address for the contract to interact with
730
+ * @param {String} method - the method/function to run
731
+ * @param {Array} params - array of paramters to apply to the contract method
732
+ * @param {Number} nonce - total transaction count [optional]
733
+ */
734
+ async createTransactionFrom(from, to, method, params, nonce) {
735
+ try {
736
+
737
+ const rawTransaction = await this.createRawTransaction({from, to, nonce, method, params})
738
+ const transaction = await this.signTransaction(rawTransaction, from)
739
+ const message = await new TransactionMessage(transaction)
740
+
741
+ let data
742
+ // await transactionPoolStore.put(message.hash, new TextEncoder().encode(JSON.stringify({signature, message: message.encoded})))
743
+ const wait = () => new Promise(async (resolve, reject) => {
744
+ if (pubsub.subscribers[`transaction.completed.${await message.hash}`]) {
745
+ const result = pubsub.subscribers[`transaction.completed.${await message.hash}`].value
746
+ result.status === 'fulfilled' ? resolve(await result.hash) : reject({hash: await result.hash, error: result.error})
747
+ } else {
748
+ const completed = async result => {
749
+ result.status === 'fulfilled' ? resolve(await result.hash) : reject({hash: await result.hash, error: result.error})
750
+
751
+ setTimeout(async () => {
752
+ pubsub.unsubscribe(`transaction.completed.${await message.hash}`, completed)
753
+ }, 10000)
754
+ }
755
+ pubsub.subscribe(`transaction.completed.${await message.hash}`, completed)
756
+ }
757
+
758
+
759
+ })
760
+
761
+ await transactionPoolStore.put(await message.hash, message.encoded)
762
+ peernet.publish('add-transaction', message.encoded)
763
+ this.#addTransaction(message.encoded)
764
+ return {hash: await message.hash, data, fee: await calculateFee(message.decoded), wait}
765
+ } catch (e) {
766
+ console.log(e);
767
+ throw e
768
+ }
769
+
770
+ }
771
+
772
+ async createContractMessage(creator, contract, constructorParameters = []) {
773
+ return new ContractMessage({
774
+ creator,
775
+ contract,
776
+ constructorParameters
777
+ })
778
+ }
779
+
780
+ async createContractAddress(creator, contract, constructorParameters = []) {
781
+ contract = await this.createContractMessage(creator, contract, constructorParameters)
782
+ return contract.hash
783
+ }
784
+
785
+ /**
786
+ *
787
+ * @param {String} contract - a contract string (see plugins/deployContract)
788
+ */
789
+ async deployContract(contract, params = []) {
790
+ globalThis.msg = {sender: peernet.id, call: this.call}
791
+
792
+ const hash = await this.createContractAddress(creator, contract, params)
793
+
794
+ try {
795
+ const tx = await this.createTransactionFrom(peernet.id, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters])
796
+ } catch (e) {
797
+ throw e
798
+ }
799
+ return this.#machine.addContract(message)
800
+ }
801
+
802
+ #createMessage(sender = peernet.id) {
803
+ return {
804
+ sender: peernet.id,
805
+ call: this.call,
806
+ staticCall: this.staticCall,
807
+ delegate: this.delegate,
808
+ staticDelegate: this.staticDelegate
809
+ }
810
+ }
811
+
812
+ internalCall(sender, contract, method, params) {
813
+ globalThis.msg = this.#createMessage(sender)
814
+
815
+ return this.#machine.execute(contract, method, params)
816
+ }
817
+
818
+ call(contract, method, params) {
819
+ globalThis.msg = this.#createMessage()
820
+
821
+ return this.#machine.execute(contract, method, params)
822
+ }
823
+
824
+ staticCall(contract, method, params) {
825
+ globalThis.msg = this.#createMessage()
826
+ return this.#machine.get(contract, method, params)
827
+ }
828
+
829
+ delegate(contract, method, params) {
830
+ globalThis.msg = this.#createMessage()
831
+
832
+ return this.#machine.execute(contract, method, params)
833
+ }
834
+
835
+ staticDelegate(contract, method, params) {
836
+ globalThis.msg = this.#createMessage()
837
+
838
+ return this.#machine.get(contract, method, params)
839
+ }
840
+
841
+ mint(to, amount) {
842
+ return this.call(addresses.nativeToken, 'mint', [to, amount])
843
+ }
844
+
845
+ transfer(from, to, amount) {
846
+ return this.call(addresses.nativeToken, 'transfer', [from, to, amount])
847
+ }
848
+
849
+ get balances() {
850
+ return this.staticCall(addresses.nativeToken, 'balances')
851
+ }
852
+
853
+ deleteAll() {
854
+ return this.#machine.deleteAll()
855
+ }
856
+
857
+ /**
858
+ * lookup an address for a registered name using the builtin nameService
859
+ * @check nameService
860
+ *
861
+ * @param {String} - contractName
862
+ * @returns {String} - address
863
+ *
864
+ * @example chain.lookup('myCoolContractName') // qmqsfddfdgfg...
865
+ */
866
+ lookup(name) {
867
+ return this.call(addresses.nameService, 'lookup', [name])
868
+ }
869
+ }
870
+ </code></pre>
871
+ </article>
872
+ </section>
873
+
874
+
875
+
876
+
877
+ </div>
878
+
879
+ <br class="clear">
880
+
881
+ <footer>
882
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.11</a>
883
+ </footer>
884
+
885
+ <script src="scripts/linenumber.js"></script>
886
+ <script src="scripts/pagelocation.js"></script>
887
+
888
+
889
+
890
+ </body>
891
+ </html>