@leofcoin/chain 1.0.19 → 1.0.20
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/docs/@leofcoin/chain/1.0.19/chain.js.html +738 -0
- package/docs/@leofcoin/chain/1.0.19/contracts_factory.js.html +182 -0
- package/docs/@leofcoin/chain/1.0.19/contracts_nameService.js.html +231 -0
- package/docs/@leofcoin/chain/1.0.19/contracts_proxies_factoryProxy.js.html +135 -0
- package/docs/@leofcoin/chain/1.0.19/contracts_proxies_nameServiceProxy.js.html +135 -0
- package/docs/@leofcoin/chain/1.0.19/contracts_proxies_nativeTokenProxy.js.html +135 -0
- package/docs/@leofcoin/chain/1.0.19/contracts_proxies_validatorsProxy.js.html +135 -0
- package/docs/@leofcoin/chain/1.0.19/contracts_proxies_votingProxy.js.html +135 -0
- package/docs/@leofcoin/chain/1.0.19/contracts_validators.js.html +242 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Bold-webfont.svg +1830 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Italic-webfont.svg +1830 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Light-webfont.eot +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Light-webfont.svg +1831 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Light-webfont.woff +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Regular-webfont.svg +1831 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Semibold-webfont.eot +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Semibold-webfont.svg +1830 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Semibold-webfont.ttf +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-Semibold-webfont.woff +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-SemiboldItalic-webfont.eot +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-SemiboldItalic-webfont.svg +1830 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-SemiboldItalic-webfont.ttf +0 -0
- package/docs/@leofcoin/chain/1.0.19/fonts/OpenSans-SemiboldItalic-webfont.woff +0 -0
- package/docs/@leofcoin/chain/1.0.19/global.html +1153 -0
- package/docs/@leofcoin/chain/1.0.19/icons/home.svg +4 -0
- package/docs/@leofcoin/chain/1.0.19/icons/search.svg +4 -0
- package/docs/@leofcoin/chain/1.0.19/index.html +125 -0
- package/docs/@leofcoin/chain/1.0.19/machine.js.html +251 -0
- package/docs/@leofcoin/chain/1.0.19/scripts/linenumber.js +23 -0
- package/docs/@leofcoin/chain/1.0.19/scripts/pagelocation.js +89 -0
- package/docs/@leofcoin/chain/1.0.19/standards_roles.js.html +188 -0
- package/docs/@leofcoin/chain/1.0.19/standards_token.js.html +260 -0
- package/docs/@leofcoin/chain/1.0.19/styles/collapse.css +27 -0
- package/docs/@leofcoin/chain/1.0.19/styles/jsdoc-default.css +953 -0
- package/docs/@leofcoin/chain/1.0.19/styles/prettify-jsdoc.css +111 -0
- package/docs/@leofcoin/chain/1.0.19/styles/prettify-tomorrow.css +138 -0
- package/package.json +2 -2
|
@@ -0,0 +1,738 @@
|
|
|
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/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#createTransaction">createTransaction</a></li><li><a href="global.html#createTransactionFrom">createTransactionFrom</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'
|
|
104
|
+
import lib from '@leofcoin/lib'
|
|
105
|
+
|
|
106
|
+
globalThis.BigNumber = BigNumber
|
|
107
|
+
|
|
108
|
+
const { ContractMessage, TransactionMessage, BlockMessage, BWMessage, BWRequestMessage } = lib
|
|
109
|
+
// check if browser or local
|
|
110
|
+
export default class Chain {
|
|
111
|
+
#validators = []
|
|
112
|
+
#blocks = []
|
|
113
|
+
#machine
|
|
114
|
+
#runningEpoch = false
|
|
115
|
+
#lastBlock = {index: 0, previousHash: '0x0'}
|
|
116
|
+
|
|
117
|
+
constructor() {
|
|
118
|
+
return this.#init()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
get lib() {
|
|
122
|
+
return lib
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
get lastBlock() {
|
|
126
|
+
return this.#lastBlock
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
get nativeToken() {
|
|
130
|
+
return lib.nativeToken
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
get validators() {
|
|
134
|
+
return [...this.#validators]
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
get blocks() {
|
|
138
|
+
return [...this.#blocks]
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async hasTransactionToHandle() {
|
|
142
|
+
const size = await transactionPoolStore.size()
|
|
143
|
+
if (size > 0) return true
|
|
144
|
+
return false
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async #runEpoch() {
|
|
148
|
+
const validators = await this.staticCall(lib.validators, 'validators')
|
|
149
|
+
if (!validators[peernet.id]?.active) return
|
|
150
|
+
|
|
151
|
+
this.#runningEpoch = true
|
|
152
|
+
const start = new Date().getTime()
|
|
153
|
+
await this.#createBlock()
|
|
154
|
+
const end = new Date().getTime()
|
|
155
|
+
console.log((end - start) / 1000 + ' s');
|
|
156
|
+
if (await this.hasTransactionToHandle()) return this.#runEpoch()
|
|
157
|
+
this.#runningEpoch = false
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async #setup() {
|
|
161
|
+
await contractStore.put(lib.contractFactory, lib.contractFactoryMessage)
|
|
162
|
+
await contractStore.put(lib.nativeToken, lib.nativeTokenMessage)
|
|
163
|
+
await contractStore.put(lib.validators, lib.validatorsMessage)
|
|
164
|
+
await contractStore.put(lib.nameService, lib.nameServiceMessage)
|
|
165
|
+
console.log('handle native contracts');
|
|
166
|
+
// handle native contracts
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async #sync() {
|
|
170
|
+
let promises = []
|
|
171
|
+
for (const peer of peernet.connections) {
|
|
172
|
+
if (peer.peerId !== this.id) {
|
|
173
|
+
let data = new peernet.protos['peernet-request']({request: 'lastBlock'})
|
|
174
|
+
|
|
175
|
+
const node = await peernet.prepareMessage(id, data.encoded)
|
|
176
|
+
promises.push(peer.request(node.encoded))
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// if (this.)
|
|
180
|
+
|
|
181
|
+
promises = await Promise.allSettled(promises)
|
|
182
|
+
promises = promises.reduce((set, c) => {
|
|
183
|
+
console.log({c});
|
|
184
|
+
if (c.index > set.index) {
|
|
185
|
+
set.index = c.index
|
|
186
|
+
set.hash = c.hash
|
|
187
|
+
}
|
|
188
|
+
return set
|
|
189
|
+
}, {index: 0, hash: '0x0'})
|
|
190
|
+
// get lastblock
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async #init() {
|
|
194
|
+
// this.node = await new Node()
|
|
195
|
+
this.participants = []
|
|
196
|
+
const initialized = await contractStore.has(lib.contractFactory)
|
|
197
|
+
if (!initialized) await this.#setup()
|
|
198
|
+
|
|
199
|
+
this.#machine = await new Machine()
|
|
200
|
+
this.utils = { BigNumber, formatUnits, parseUnits }
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
let localBlock = await chainStore.get('lastBlock')
|
|
204
|
+
localBlock = await peernet.get(new TextDecoder().decode(localBlock))
|
|
205
|
+
localBlock = new BlockMessage(localBlock)
|
|
206
|
+
this.#lastBlock = {...localBlock.decoded, hash: localBlock.hash}
|
|
207
|
+
// console.log(this.lastBlock.decoded.transactions);
|
|
208
|
+
} catch (e) {
|
|
209
|
+
await this.#sync()
|
|
210
|
+
// this.#setup()
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
await peernet.addRequestHandler('bw-request-message', () => {
|
|
214
|
+
|
|
215
|
+
return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this))
|
|
219
|
+
|
|
220
|
+
peernet.subscribe('add-block', this.#addBlock.bind(this))
|
|
221
|
+
|
|
222
|
+
peernet.subscribe('add-transaction', async transaction => {
|
|
223
|
+
try {
|
|
224
|
+
transaction = new TransactionMessage(transaction)
|
|
225
|
+
await transactionPoolStore.put(transaction.hash, transaction.encoded)
|
|
226
|
+
if (await this.hasTransactionToHandle() && !this.#runningEpoch) return this.#runEpoch()
|
|
227
|
+
} catch (e) {
|
|
228
|
+
throw Error('invalid transaction')
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
pubsub.subscribe('peer:connected', this.#peerConnected.bind(this))
|
|
233
|
+
|
|
234
|
+
// load local blocks
|
|
235
|
+
await this.resolveBlocks()
|
|
236
|
+
return this
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async #peerConnected(peer) {
|
|
240
|
+
let node = new peernet.protos['peernet-request']({request: 'lastBlock'})
|
|
241
|
+
node = await peernet.prepareMessage(peer.id, node.encoded)
|
|
242
|
+
let response = await peer.request(node.encoded)
|
|
243
|
+
response = new Uint8Array(Object.values(response))
|
|
244
|
+
const proto = new globalThis.peernet.protos['peernet-message'](response)
|
|
245
|
+
response = new globalThis.peernet.protos['peernet-response'](proto.decoded.data)
|
|
246
|
+
let lastBlock = JSON.parse(new TextDecoder().decode(response.decoded.response))
|
|
247
|
+
|
|
248
|
+
if (!this.lastBlock || this.lastBlock.index < lastBlock.index) {
|
|
249
|
+
// TODO: check if valid
|
|
250
|
+
const localIndex = this.lastBlock ? this.lastBlock.index : 0
|
|
251
|
+
const index = lastBlock.index
|
|
252
|
+
await this.resolveBlock(lastBlock.hash)
|
|
253
|
+
this.#lastBlock = this.#blocks[this.#blocks.length - 1]
|
|
254
|
+
console.log({lastBlock: this.#lastBlock});
|
|
255
|
+
console.log(this.#blocks);
|
|
256
|
+
let blocksSynced = localIndex > 0 ? localIndex - index : index
|
|
257
|
+
blocksSynced += 1
|
|
258
|
+
debug(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`)
|
|
259
|
+
|
|
260
|
+
const end = this.#blocks.length
|
|
261
|
+
const start = (this.#blocks.length) - blocksSynced
|
|
262
|
+
await this.#loadBlocks(this.#blocks)
|
|
263
|
+
const message = new BlockMessage(this.lastBlock)
|
|
264
|
+
await blockStore.put(message.hash, message.encoded)
|
|
265
|
+
await chainStore.put('lastBlock', new TextEncoder().encode(this.lastBlock.hash))
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async #lastBlockHandler() {
|
|
270
|
+
return new peernet.protos['peernet-response']({response: new TextEncoder().encode(JSON.stringify({ hash: this.lastBlock?.hash, index: this.lastBlock?.index }))})
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async resolveBlock(hash) {
|
|
274
|
+
let block = await peernet.get(hash, 'block')
|
|
275
|
+
if (!await peernet.has(hash, 'block')) await peernet.put(hash, block, 'block')
|
|
276
|
+
block = await new BlockMessage(block)
|
|
277
|
+
const size = block.encoded.length || block.encoded.byteLength
|
|
278
|
+
block = {...block.decoded, hash}
|
|
279
|
+
this.#blocks[block.index] = block
|
|
280
|
+
console.log(`loaded block: ${hash} @${block.index} ${Math.round((size / 1024 / 1024) * 100) / 100} mb`);
|
|
281
|
+
if (block.index !== 0) {
|
|
282
|
+
return this.resolveBlock(block.previousHash)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async resolveBlocks() {
|
|
287
|
+
try {
|
|
288
|
+
|
|
289
|
+
const localBlock = await chainStore.get('lastBlock')
|
|
290
|
+
const hash = new TextDecoder().decode(localBlock)
|
|
291
|
+
if (hash !== '0x0')
|
|
292
|
+
await this.resolveBlock(new TextDecoder().decode(localBlock))
|
|
293
|
+
this.#lastBlock = this.#blocks[this.#blocks.length - 1]
|
|
294
|
+
await this.#loadBlocks(this.#blocks)
|
|
295
|
+
} catch (e) {
|
|
296
|
+
await chainStore.put('lastBlock', new TextEncoder().encode('0x0'))
|
|
297
|
+
return this.resolveBlocks()
|
|
298
|
+
// console.log(e);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async #loadBlocks(blocks) {
|
|
303
|
+
for (const block of blocks) {
|
|
304
|
+
if (!block.loaded) {
|
|
305
|
+
for (const transaction of block.transactions) {
|
|
306
|
+
try {
|
|
307
|
+
await this.#machine.execute(transaction.to, transaction.method, transaction.params)
|
|
308
|
+
|
|
309
|
+
} catch (e) {
|
|
310
|
+
console.log(e);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
block.loaded = true
|
|
314
|
+
// let message = await peernet.get(block.hash, 'block')
|
|
315
|
+
|
|
316
|
+
// const compressed = pako.deflate(message);
|
|
317
|
+
// const result = pako.inflate(compressed);
|
|
318
|
+
// console.log(result.length, compressed.length);
|
|
319
|
+
//
|
|
320
|
+
// console.log(result.length - compressed.length);
|
|
321
|
+
|
|
322
|
+
// message = new BlockMessage(message)
|
|
323
|
+
// for (const transaction of message.decoded.transactions) {
|
|
324
|
+
// try {
|
|
325
|
+
// await this.#machine.execute(transaction.to, transaction.method, transaction.params)
|
|
326
|
+
//
|
|
327
|
+
// } catch (e) {
|
|
328
|
+
// // console.log(e);
|
|
329
|
+
// }
|
|
330
|
+
// }
|
|
331
|
+
// block.loaded = true
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async #executeTransaction({hash, from, to, method, params, nonce}) {
|
|
337
|
+
try {
|
|
338
|
+
let result = await this.#machine.execute(to, method, params, from, nonce)
|
|
339
|
+
// if (!result) result = this.#machine.state
|
|
340
|
+
pubsub.publish(`transaction.completed.${hash}`, {status: 'fulfilled', hash})
|
|
341
|
+
return result ? result : 'no state change'
|
|
342
|
+
} catch (e) {
|
|
343
|
+
pubsub.publish(`transaction.completed.${hash}`, {status: 'fail', hash, error: e})
|
|
344
|
+
throw e
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
async #addBlock(block) {
|
|
349
|
+
const blockMessage = new BlockMessage(block)
|
|
350
|
+
// if (!Buffer.isBuffer(block)) block = Buffer.from(block, 'hex')
|
|
351
|
+
// const transactionJob = async transaction => {
|
|
352
|
+
// try {
|
|
353
|
+
// transaction = await transactionPoolStore.get(transaction)
|
|
354
|
+
// } catch (e) {
|
|
355
|
+
// try {
|
|
356
|
+
// transaction = await peernet.get(transaction, 'transaction')
|
|
357
|
+
// } catch (e) {
|
|
358
|
+
// console.warn(`couldn't resolve ${transaction}`);
|
|
359
|
+
// }
|
|
360
|
+
// }
|
|
361
|
+
// transaction = new TransactionMessage(transaction)
|
|
362
|
+
// return transaction
|
|
363
|
+
// }
|
|
364
|
+
await Promise.all(blockMessage.decoded.transactions
|
|
365
|
+
.map(transaction => transactionPoolStore.delete(transaction.hash)))
|
|
366
|
+
|
|
367
|
+
// let transactions = blockMessage.decoded.transactions.map(tx => transactionJob(tx))
|
|
368
|
+
// transactions = await Promise.all(transactions)
|
|
369
|
+
this.#lastBlock = {hash: blockMessage.hash, ...blockMessage.decoded}
|
|
370
|
+
await blockStore.put(blockMessage.hash, blockMessage.encoded)
|
|
371
|
+
await chainStore.put('lastBlock', new TextEncoder().encode(blockMessage.hash))
|
|
372
|
+
debug(`added block: ${blockMessage.hash}`)
|
|
373
|
+
let promises = []
|
|
374
|
+
let contracts = []
|
|
375
|
+
for (let transaction of blockMessage.decoded.transactions) {
|
|
376
|
+
// await transactionStore.put(transaction.hash, transaction.encoded)
|
|
377
|
+
const index = contracts.indexOf(transaction.to)
|
|
378
|
+
if (index === -1) contracts.push(transaction.to)
|
|
379
|
+
promises.push(this.#executeTransaction(transaction))
|
|
380
|
+
}
|
|
381
|
+
try {
|
|
382
|
+
promises = await Promise.allSettled(promises)
|
|
383
|
+
|
|
384
|
+
// todo finish state
|
|
385
|
+
// for (const contract of contracts) {
|
|
386
|
+
// const state = await this.#machine.get(contract, 'state')
|
|
387
|
+
// // await stateStore.put(contract, state)
|
|
388
|
+
// console.log(state);
|
|
389
|
+
// }
|
|
390
|
+
pubsub.publish('block-processed', blockMessage.decoded)
|
|
391
|
+
} catch (e) {
|
|
392
|
+
console.log(e);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
async participate() {
|
|
400
|
+
// TODO: validate participant
|
|
401
|
+
// hold min amount of 50k ART for 7 days
|
|
402
|
+
// lock the 50k
|
|
403
|
+
// introduce peer-reputation
|
|
404
|
+
// peerReputation(peerId)
|
|
405
|
+
// {bandwith: {up, down}, uptime}
|
|
406
|
+
if (!await this.staticCall(lib.validators, 'has', [peernet.id])) await this.createTransactionFrom(peernet.id, lib.validators, 'addValidator', [peernet.id])
|
|
407
|
+
if (await this.hasTransactionToHandle() && !this.#runningEpoch) return this.#runEpoch()
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
calculateFee(transaction) {
|
|
411
|
+
// excluded from fees
|
|
412
|
+
if (transaction.decoded.to === lib.validators) return 0
|
|
413
|
+
// fee per gb
|
|
414
|
+
return (transaction.encoded.length / 1024) / 1e-6
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async #createBlock() {
|
|
418
|
+
let transactions = await transactionPoolStore.get()
|
|
419
|
+
|
|
420
|
+
if (Object.keys(transactions)?.length === 0 ) return
|
|
421
|
+
let block = {
|
|
422
|
+
transactions: [],
|
|
423
|
+
validators: [],
|
|
424
|
+
fees: 0
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
let _transactions = []
|
|
428
|
+
|
|
429
|
+
// exclude failing tx
|
|
430
|
+
for (let transaction of Object.values(transactions)) {
|
|
431
|
+
transaction = new TransactionMessage(new Uint8Array(transaction, transaction.byteOffset, transaction.byteLength))
|
|
432
|
+
|
|
433
|
+
block.fees += Number(lib.calculateFee(transaction))
|
|
434
|
+
_transactions.push({...transaction.decoded, hash: transaction.hash })
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
transactions = _transactions.sort((a, b) => a.nonce - b.nonce)
|
|
438
|
+
_transactions = []
|
|
439
|
+
for (let transaction of transactions) {
|
|
440
|
+
try {
|
|
441
|
+
await this.#executeTransaction(transaction)
|
|
442
|
+
_transactions.push(transaction)
|
|
443
|
+
} catch (e) {
|
|
444
|
+
transaction = new TransactionMessage(transaction)
|
|
445
|
+
block.fees -= Number(lib.calculateFee(transaction))
|
|
446
|
+
await transactionPoolStore.delete(transaction.hash)
|
|
447
|
+
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
transactions = _transactions
|
|
451
|
+
// don't add empty block
|
|
452
|
+
if (transactions.length === 0) return
|
|
453
|
+
|
|
454
|
+
block.transactions = transactions
|
|
455
|
+
const validators = await this.staticCall(lib.validators, 'validators')
|
|
456
|
+
console.log({validators});
|
|
457
|
+
// block.validators = Object.keys(block.validators).reduce((set, key) => {
|
|
458
|
+
// if (block.validators[key].active) {
|
|
459
|
+
// push({
|
|
460
|
+
// address: key
|
|
461
|
+
// })
|
|
462
|
+
// }
|
|
463
|
+
// }, [])
|
|
464
|
+
const peers = {}
|
|
465
|
+
for (const entry of peernet.peerEntries) {
|
|
466
|
+
peers[entry[0]] = entry[1]
|
|
467
|
+
}
|
|
468
|
+
for (const validator of Object.keys(validators)) {
|
|
469
|
+
if (validators[validator].active) {
|
|
470
|
+
const peer = peers[validator]
|
|
471
|
+
if (peer && peer.connected) {
|
|
472
|
+
let data = new BWRequestMessage()
|
|
473
|
+
const node = await peernet.prepareMessage(validator, data.encoded)
|
|
474
|
+
try {
|
|
475
|
+
const bw = await peer.request(node.encoded)
|
|
476
|
+
console.log(bw);
|
|
477
|
+
block.validators.push({
|
|
478
|
+
address: validator,
|
|
479
|
+
bw: bw.up + bw.down
|
|
480
|
+
})
|
|
481
|
+
} catch(e) {
|
|
482
|
+
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
} else if (peernet.id === validator) {
|
|
486
|
+
block.validators.push({
|
|
487
|
+
address: peernet.id,
|
|
488
|
+
bw: peernet.bw.up + peernet.bw.down
|
|
489
|
+
})
|
|
490
|
+
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
console.log(block.validators);
|
|
497
|
+
|
|
498
|
+
block.reward = 150
|
|
499
|
+
block.validators = block.validators.map(validator => {
|
|
500
|
+
validator.reward = String(Number(block.fees) + block.reward / block.validators.length)
|
|
501
|
+
delete validator.bw
|
|
502
|
+
return validator
|
|
503
|
+
})
|
|
504
|
+
// block.validators = lib.calculateValidatorReward(block.validators, block.fees)
|
|
505
|
+
|
|
506
|
+
block.index = this.lastBlock?.index
|
|
507
|
+
if (block.index === undefined) block.index = 0
|
|
508
|
+
else block.index += 1
|
|
509
|
+
|
|
510
|
+
block.previousHash = this.lastBlock?.hash || '0x0'
|
|
511
|
+
block.timestamp = new Date().getTime()
|
|
512
|
+
|
|
513
|
+
const parts = String(block.fees).split('.')
|
|
514
|
+
let decimals = 0
|
|
515
|
+
if (parts[1]) {
|
|
516
|
+
const potentional = parts[1].split('e')
|
|
517
|
+
if (potentional[0] !== parts[1]) {
|
|
518
|
+
parts[1] = potentional[0]
|
|
519
|
+
decimals = Number(potentional[1]?.replace(/\-|\+/g, '')) + Number(potentional[0].length)
|
|
520
|
+
} else {
|
|
521
|
+
decimals = parts[1].length
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
}
|
|
525
|
+
block.fees = Number.parseFloat(String(block.fees)).toFixed(decimals)
|
|
526
|
+
|
|
527
|
+
try {
|
|
528
|
+
let blockMessage = new BlockMessage(block)
|
|
529
|
+
this.#lastBlock = {...block, hash: blockMessage.hash}
|
|
530
|
+
peernet.publish('add-block', blockMessage.encoded)
|
|
531
|
+
this.#addBlock(blockMessage.encoded)
|
|
532
|
+
} catch (e) {
|
|
533
|
+
throw Error(`invalid block ${block}`)
|
|
534
|
+
}
|
|
535
|
+
// data = await this.#machine.execute(to, method, params)
|
|
536
|
+
// transactionStore.put(message.hash, message.encoded)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
async getNonce(address) {
|
|
540
|
+
let transactions = await transactionPoolStore.get()
|
|
541
|
+
transactions = Object.keys(transactions).map(tx => new TransactionMessage(transactions[tx]))
|
|
542
|
+
transactions = transactions.filter(tx => tx.decoded.from === address)
|
|
543
|
+
transactions = transactions.map(tx => { return {...tx.decoded, hash: tx.hash} })
|
|
544
|
+
if (this.lastBlock && transactions.length === 0) {
|
|
545
|
+
let block = await peernet.get(this.lastBlock.hash)
|
|
546
|
+
block = new BlockMessage(block)
|
|
547
|
+
|
|
548
|
+
// for (let tx of block.decoded?.transactions) {
|
|
549
|
+
// tx = await peernet.get(tx, 'transaction')
|
|
550
|
+
// transactions.push(new TransactionMessage(tx))
|
|
551
|
+
// }
|
|
552
|
+
transactions = transactions.filter(tx => tx.from === address)
|
|
553
|
+
while (transactions.length === 0 && block.decoded.index !== 0) {
|
|
554
|
+
block = await blockStore.get(block.decoded.previousHash)
|
|
555
|
+
block = new BlockMessage(block)
|
|
556
|
+
transactions = block.decoded.transactions.filter(tx => tx.from === address)
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
}
|
|
560
|
+
if (transactions.length === 0) return 0
|
|
561
|
+
|
|
562
|
+
transactions = transactions.sort((a, b) => a.timestamp - b.timestamp)
|
|
563
|
+
return transactions[transactions.length - 1].nonce
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* whenever method = createContract params should hold the contract hash
|
|
568
|
+
*
|
|
569
|
+
* example: [hash]
|
|
570
|
+
* createTransaction('0x0', 'createContract', [hash])
|
|
571
|
+
*
|
|
572
|
+
* @param {String} to - the contract address for the contract to interact with
|
|
573
|
+
* @param {String} method - the method/function to run
|
|
574
|
+
* @param {Array} params - array of paramters to apply to the contract method
|
|
575
|
+
* @param {Number} nonce - total transaction count [optional]
|
|
576
|
+
*/
|
|
577
|
+
createTransaction(to, method, params, nonce) {
|
|
578
|
+
return this.createTransactionFrom(peernet.id, to, method, params, nonce)
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* every tx done is trough contracts so no need for amount
|
|
582
|
+
* data is undefined when nothing is returned
|
|
583
|
+
* error is thrown on error so undefined data doesn't mean there is an error...
|
|
584
|
+
*
|
|
585
|
+
* @param {String} from - the sender address
|
|
586
|
+
* @param {String} to - the contract address for the contract to interact with
|
|
587
|
+
* @param {String} method - the method/function to run
|
|
588
|
+
* @param {Array} params - array of paramters to apply to the contract method
|
|
589
|
+
* @param {Number} nonce - total transaction count [optional]
|
|
590
|
+
*/
|
|
591
|
+
async createTransactionFrom(from, to, method, params, nonce) {
|
|
592
|
+
if (nonce === undefined) {
|
|
593
|
+
nonce = await this.getNonce(from)
|
|
594
|
+
nonce += 1
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
let data
|
|
598
|
+
let message = new TransactionMessage({timestamp: new Date().getTime(), from, to, nonce, method, params})
|
|
599
|
+
try {
|
|
600
|
+
// await transactionPoolStore.put(message.hash, new TextEncoder().encode(JSON.stringify({signature, message: message.encoded})))
|
|
601
|
+
await transactionPoolStore.put(message.hash, message.encoded)
|
|
602
|
+
peernet.publish('add-transaction', message.encoded)
|
|
603
|
+
} catch (e) {
|
|
604
|
+
throw e
|
|
605
|
+
}
|
|
606
|
+
const wait = () => new Promise((resolve, reject) => {
|
|
607
|
+
const completed = result => {
|
|
608
|
+
result.status === 'fulfilled' ? resolve(result.hash) : reject({hash: result.hash, error: result.error})
|
|
609
|
+
|
|
610
|
+
pubsub.unsubscribe(`transaction.completed.${message.hash}`, completed)
|
|
611
|
+
}
|
|
612
|
+
pubsub.subscribe(`transaction.completed.${message.hash}`, completed)
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
if (await this.hasTransactionToHandle() && !this.#runningEpoch) this.#runEpoch()
|
|
616
|
+
return {hash: message.hash, data, fee: lib.calculateFee(message), wait}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
async createContractMessage(creator, contract, constructorParameters = []) {
|
|
620
|
+
return new ContractMessage({
|
|
621
|
+
creator,
|
|
622
|
+
contract,
|
|
623
|
+
constructorParameters
|
|
624
|
+
})
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
async createContractAddress(creator, contract, constructorParameters = []) {
|
|
628
|
+
return this.createContractMessage(creator, contract, constructorParameters)
|
|
629
|
+
.hash
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
*
|
|
634
|
+
* @param {String} contract - a contract string (see plugins/deployContract)
|
|
635
|
+
*/
|
|
636
|
+
async deployContract(contract, params = []) {
|
|
637
|
+
globalThis.msg = {sender: peernet.id, call: this.call}
|
|
638
|
+
|
|
639
|
+
const hash = await this.createContractAddress(creator, contract, params)
|
|
640
|
+
|
|
641
|
+
try {
|
|
642
|
+
const tx = await this.createTransactionFrom(peernet.id, lib.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters])
|
|
643
|
+
} catch (e) {
|
|
644
|
+
throw e
|
|
645
|
+
}
|
|
646
|
+
return this.#machine.addContract(message)
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
#createMessage(sender = peernet.id) {
|
|
650
|
+
return {
|
|
651
|
+
sender: peernet.id,
|
|
652
|
+
call: this.call,
|
|
653
|
+
staticCall: this.staticCall,
|
|
654
|
+
delegate: this.delegate,
|
|
655
|
+
staticDelegate: this.staticDelegate
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
internalCall(sender, contract, method, params) {
|
|
660
|
+
globalThis.msg = this.#createMessage(sender)
|
|
661
|
+
|
|
662
|
+
return this.#machine.execute(contract, method, params)
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
call(contract, method, params) {
|
|
666
|
+
globalThis.msg = this.#createMessage()
|
|
667
|
+
|
|
668
|
+
return this.#machine.execute(contract, method, params)
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
staticCall(contract, method, params) {
|
|
672
|
+
globalThis.msg = this.#createMessage()
|
|
673
|
+
return this.#machine.get(contract, method, params)
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
delegate(contract, method, params) {
|
|
677
|
+
globalThis.msg = this.#createMessage()
|
|
678
|
+
|
|
679
|
+
return this.#machine.execute(contract, method, params)
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
staticDelegate(contract, method, params) {
|
|
683
|
+
globalThis.msg = this.#createMessage()
|
|
684
|
+
|
|
685
|
+
return this.#machine.get(contract, method, params)
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
mint(to, amount) {
|
|
689
|
+
return this.call(lib.nativeToken, 'mint', [to, amount])
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
transfer(from, to, amount) {
|
|
693
|
+
return this.call(lib.nativeToken, 'transfer', [from, to, amount])
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
get balances() {
|
|
697
|
+
return this.staticCall(lib.nativeToken, 'balances')
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
deleteAll() {
|
|
701
|
+
return this.#machine.deleteAll()
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* lookup an address for a registered name using the builtin nameService
|
|
706
|
+
* @check nameService
|
|
707
|
+
*
|
|
708
|
+
* @param {String} - contractName
|
|
709
|
+
* @returns {String} - address
|
|
710
|
+
*
|
|
711
|
+
* @example chain.lookup('myCoolContractName') // qmqsfddfdgfg...
|
|
712
|
+
*/
|
|
713
|
+
lookup(name) {
|
|
714
|
+
return this.call(lib.nameService, 'lookup', [name])
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
</code></pre>
|
|
718
|
+
</article>
|
|
719
|
+
</section>
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
</div>
|
|
725
|
+
|
|
726
|
+
<br class="clear">
|
|
727
|
+
|
|
728
|
+
<footer>
|
|
729
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.10</a>
|
|
730
|
+
</footer>
|
|
731
|
+
|
|
732
|
+
<script src="scripts/linenumber.js"></script>
|
|
733
|
+
<script src="scripts/pagelocation.js"></script>
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
</body>
|
|
738
|
+
</html>
|