@leofcoin/chain 1.4.20 → 1.4.22

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 (76) hide show
  1. package/CHANGELOG.md +14 -14
  2. package/LICENSE +88 -88
  3. package/README.md +4 -4
  4. package/demo/index.html +25 -25
  5. package/examples/contracts/token.js +7 -7
  6. package/package.json +71 -71
  7. package/plugins/bundle.js +18 -18
  8. package/src/chain.js +716 -716
  9. package/src/config/config.js +14 -14
  10. package/src/config/main.js +4 -4
  11. package/src/config/protocol.js +5 -5
  12. package/src/contract.js +51 -51
  13. package/src/fee/config.js +3 -3
  14. package/src/machine.js +215 -215
  15. package/src/node.js +24 -24
  16. package/src/protocol.js +3 -3
  17. package/src/state.js +31 -31
  18. package/src/transaction.js +233 -233
  19. package/src/type.index.d.ts +20 -20
  20. package/src/typer.js +19 -19
  21. package/test/chain.js +120 -109
  22. package/test/contracts/token.js +40 -40
  23. package/test/create-genesis.js +66 -66
  24. package/tsconfig.js +14 -14
  25. package/workers/block-worker.js +40 -40
  26. package/workers/machine-worker.js +218 -218
  27. package/workers/pool-worker.js +28 -28
  28. package/workers/transaction-worker.js +19 -19
  29. package/workers/workers.js +8 -8
  30. package/block-worker.js +0 -1
  31. package/demo/865.browser.js +0 -10
  32. package/demo/chain.browser.js +0 -66842
  33. package/demo/generate-account.browser.js +0 -50
  34. package/demo/messages.browser.js +0 -328
  35. package/demo/multi-wallet.browser.js +0 -15
  36. package/demo/node.browser.js +0 -9858
  37. package/demo/pako.browser.js +0 -6900
  38. package/demo/peernet-swarm.browser.js +0 -839
  39. package/demo/storage.browser.js +0 -3724
  40. package/demo/workers/865.js +0 -10
  41. package/demo/workers/block-worker.js +0 -13175
  42. package/demo/workers/machine-worker.js +0 -13385
  43. package/demo/workers/pool-worker.js +0 -8503
  44. package/demo/workers/transaction-worker.js +0 -8495
  45. package/demo/wrtc.browser.js +0 -28
  46. package/dist/browser/workers/865.js +0 -10
  47. package/dist/browser/workers/block-worker.js +0 -9460
  48. package/dist/browser/workers/machine-worker.js +0 -9670
  49. package/dist/browser/workers/pool-worker.js +0 -4608
  50. package/dist/browser/workers/transaction-worker.js +0 -4600
  51. package/dist/chain.js +0 -10128
  52. package/dist/client-80bc8156.js +0 -491
  53. package/dist/commonjs-7fe3c381.js +0 -270
  54. package/dist/contracts/factory.js +0 -1
  55. package/dist/contracts/name-service.js +0 -1
  56. package/dist/contracts/native-token.js +0 -1
  57. package/dist/contracts/validators.js +0 -1
  58. package/dist/generate-account-445db122.js +0 -46
  59. package/dist/index-57f93805.js +0 -718
  60. package/dist/messages-bce1b91d-81af3b00.js +0 -315
  61. package/dist/module/chain.js +0 -10091
  62. package/dist/module/client-8031ec88.js +0 -489
  63. package/dist/module/commonjs-9005d5c0.js +0 -268
  64. package/dist/module/generate-account-489552b6.js +0 -44
  65. package/dist/module/index-ac2285c4.js +0 -688
  66. package/dist/module/messages-bce1b91d-eaf75d83.js +0 -302
  67. package/dist/module/node.js +0 -7049
  68. package/dist/module/workers/block-worker.js +0 -94
  69. package/dist/module/workers/machine-worker.js +0 -304
  70. package/dist/module/workers/pool-worker.js +0 -55
  71. package/dist/module/workers/transaction-worker.js +0 -47
  72. package/dist/node.js +0 -7061
  73. package/dist/standards/token.js +0 -1
  74. package/dist/workers/machine-worker.js +0 -1
  75. package/dist/workers/pool-worker.js +0 -1
  76. package/dist/workers/transaction-worker.js +0 -1
@@ -1,14 +1,14 @@
1
- import main from './main'
2
- import protocol from './protocol'
3
-
4
- // export default async () => {
5
- // let config = { ...main, protocol }
6
- // try {
7
- // let data = await read(main.configPath)
8
- // data = JSON.parse(data.toString())
9
- // config = { ...config, ...data }
10
- // } catch (e) {
11
- // await write(main.configPath, JSON.stringify(config, null, '\t'))
12
- // }
13
- // return config
14
- // }
1
+ import main from './main'
2
+ import protocol from './protocol'
3
+
4
+ // export default async () => {
5
+ // let config = { ...main, protocol }
6
+ // try {
7
+ // let data = await read(main.configPath)
8
+ // data = JSON.parse(data.toString())
9
+ // config = { ...config, ...data }
10
+ // } catch (e) {
11
+ // await write(main.configPath, JSON.stringify(config, null, '\t'))
12
+ // }
13
+ // return config
14
+ // }
@@ -1,4 +1,4 @@
1
- export default {
2
- datadir: '',
3
- configPath: ''
4
- }
1
+ export default {
2
+ datadir: '',
3
+ configPath: ''
4
+ }
@@ -1,5 +1,5 @@
1
- export default {
2
- block: {
3
- size: 1024
4
- }
5
- }
1
+ export default {
2
+ block: {
3
+ size: 1024
4
+ }
5
+ }
package/src/contract.js CHANGED
@@ -1,52 +1,52 @@
1
- import Transaction from "./transaction.js";
2
- import { createContractMessage } from '@leofcoin/lib'
3
- import addresses from "@leofcoin/addresses"
4
-
5
- /**
6
- * @extends {Transaction}
7
- */
8
- export default class Contract extends Transaction {
9
- constructor() {
10
- super()
11
- }
12
-
13
- /**
14
- *
15
- * @param {Address} creator
16
- * @param {String} contract
17
- * @param {Array} constructorParameters
18
- * @returns lib.createContractMessage
19
- */
20
- async createContractMessage(creator, contract, constructorParameters = []) {
21
- return createContractMessage(creator, contract, constructorParameters)
22
- }
23
-
24
- /**
25
- *
26
- * @param {Address} creator
27
- * @param {String} contract
28
- * @param {Array} constructorParameters
29
- * @returns {Address}
30
- */
31
- async createContractAddress(creator, contract, constructorParameters = []) {
32
- contract = await this.createContractMessage(creator, contract, constructorParameters)
33
- return contract.hash()
34
- }
35
-
36
- /**
37
- *
38
- * @param {String} contract
39
- * @param {Array} parameters
40
- * @returns
41
- */
42
- async deployContract(contract, constructorParameters = []) {
43
- const message = await createContractMessage(peernet.selectedAccount, contract, constructorParameters)
44
- try {
45
- await contractStore.put(await message.hash(), message.encoded)
46
- } catch (error) {
47
- throw error
48
- }
49
- return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash()])
50
- }
51
-
1
+ import Transaction from "./transaction.js";
2
+ import { createContractMessage } from '@leofcoin/lib'
3
+ import addresses from "@leofcoin/addresses"
4
+
5
+ /**
6
+ * @extends {Transaction}
7
+ */
8
+ export default class Contract extends Transaction {
9
+ constructor() {
10
+ super()
11
+ }
12
+
13
+ /**
14
+ *
15
+ * @param {Address} creator
16
+ * @param {String} contract
17
+ * @param {Array} constructorParameters
18
+ * @returns lib.createContractMessage
19
+ */
20
+ async createContractMessage(creator, contract, constructorParameters = []) {
21
+ return createContractMessage(creator, contract, constructorParameters)
22
+ }
23
+
24
+ /**
25
+ *
26
+ * @param {Address} creator
27
+ * @param {String} contract
28
+ * @param {Array} constructorParameters
29
+ * @returns {Address}
30
+ */
31
+ async createContractAddress(creator, contract, constructorParameters = []) {
32
+ contract = await this.createContractMessage(creator, contract, constructorParameters)
33
+ return contract.hash()
34
+ }
35
+
36
+ /**
37
+ *
38
+ * @param {String} contract
39
+ * @param {Array} parameters
40
+ * @returns
41
+ */
42
+ async deployContract(contract, constructorParameters = []) {
43
+ const message = await createContractMessage(peernet.selectedAccount, contract, constructorParameters)
44
+ try {
45
+ await contractStore.put(await message.hash(), message.encoded)
46
+ } catch (error) {
47
+ throw error
48
+ }
49
+ return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash()])
50
+ }
51
+
52
52
  }
package/src/fee/config.js CHANGED
@@ -1,3 +1,3 @@
1
- export default {
2
- deployment: 0.000_001
3
- }
1
+ export default {
2
+ deployment: 0.000_001
3
+ }
package/src/machine.js CHANGED
@@ -1,215 +1,215 @@
1
- import { contractFactory, nativeToken, validators, nameService } from '@leofcoin/addresses'
2
- import { randombytes } from '@leofcoin/crypto'
3
- import EasyWorker from '@vandeurenglenn/easy-worker'
4
- import { ContractMessage } from '@leofcoin/messages'
5
- // import State from './state'
6
-
7
- export default class Machine {
8
- #contracts = {}
9
- #nonces = {}
10
- lastBlock = {index: 0, hash: '0x0', previousHash: '0x0'}
11
-
12
- constructor(blocks) {
13
- return this.#init(blocks)
14
- }
15
-
16
- #createMessage(sender = peernet.selectedAccount) {
17
- return {
18
- sender,
19
- call: this.execute,
20
- staticCall: this.get.bind(this)
21
- }
22
- }
23
-
24
- async #onmessage(data) {
25
- switch (data.type) {
26
- case 'contractError': {
27
- console.warn(`removing contract ${await data.hash()}`);
28
- await contractStore.delete(await data.hash())
29
- break
30
- }
31
-
32
- case 'initError': {
33
- console.error(`init error: ${data.message}`);
34
- break
35
- }
36
-
37
- case 'executionError': {
38
- // console.warn(`error executing transaction ${data.message}`);
39
- pubsub.publish(data.id, {error: data.message})
40
- break
41
- }
42
-
43
- case 'debug': {
44
- for (const message of data.messages) debug(message)
45
- break
46
- }
47
- case 'machine-ready': {
48
- this.lastBlock = data.lastBlock
49
- pubsub.publish('machine.ready', true)
50
- break
51
- }
52
- case 'response': {
53
- pubsub.publish(data.id, data.value || false)
54
- break
55
- }
56
- }
57
-
58
- }
59
-
60
- async #init(blocks) {
61
- return new Promise(async (resolve) => {
62
- const machineReady = () => {
63
- pubsub.unsubscribe('machine.ready', machineReady)
64
- resolve(this)
65
- }
66
- pubsub.subscribe('machine.ready', machineReady)
67
-
68
- this.worker = await new EasyWorker('node_modules/@leofcoin/workers/src/machine-worker.js', {serialization: 'advanced', type:'module'})
69
- this.worker.onmessage(this.#onmessage.bind(this))
70
-
71
- // const blocks = await blockStore.values()
72
- const contracts = await Promise.all([
73
- contractStore.get(contractFactory),
74
- contractStore.get(nativeToken),
75
- contractStore.get(validators),
76
- contractStore.get(nameService)
77
- ])
78
-
79
- const message = {
80
- type: 'init',
81
- input: {
82
- contracts,
83
- blocks,
84
- peerid: peernet.peerId
85
- }
86
- }
87
- this.worker.postMessage(message)
88
- })
89
-
90
- }
91
-
92
- async #runContract(contractMessage) {
93
- const hash = await contractMessage.hash()
94
- return new Promise((resolve, reject) => {
95
- const id = randombytes(20).toString('hex')
96
- const onmessage = message => {
97
- pubsub.unsubscribe(id, onmessage)
98
- if (message?.error) reject(message.error)
99
- else resolve(message)
100
- }
101
- pubsub.subscribe(id, onmessage)
102
- this.worker.postMessage({
103
- type: 'run',
104
- id,
105
- input: {
106
- decoded: contractMessage.decoded,
107
- encoded: contractMessage.encoded,
108
- hash
109
- }
110
- })
111
- })
112
-
113
- }
114
-
115
- /**
116
- *
117
- * @param {Address} contract
118
- * @param {String} method
119
- * @param {Array} parameters
120
- * @returns Promise<message>
121
- */
122
- async execute(contract, method, parameters) {
123
- try {
124
- if (contract === contractFactory && method === 'registerContract') {
125
- if (await this.has(parameters[0])) throw new Error(`duplicate contract @${parameters[0]}`)
126
- let message;
127
- if (!await contractStore.has(parameters[0])) {
128
- message = await peernet.get(parameters[0], 'contract')
129
- message = await new ContractMessage(message)
130
- await contractStore.put(await message.hash(), message.encoded)
131
- }
132
- if (!message) {
133
- message = await contractStore.get(parameters[0])
134
- message = await new ContractMessage(message)
135
- }
136
- if (!await this.has(await message.hash())) await this.#runContract(message)
137
- }
138
- } catch (error) {
139
- throw new Error(`contract deployment failed for ${parameters[0]}\n${error.message}`)
140
- }
141
- return new Promise((resolve, reject) => {
142
- const id = randombytes(20).toString('hex')
143
- const onmessage = message => {
144
- pubsub.unsubscribe(id, onmessage)
145
- if (message?.error) reject(message.error)
146
- else resolve(message)
147
- }
148
- pubsub.subscribe(id, onmessage)
149
- this.worker.postMessage({
150
- type: 'execute',
151
- id,
152
- input: {
153
- contract,
154
- method,
155
- params: parameters
156
- }
157
- })
158
- })
159
-
160
- }
161
-
162
- get(contract, method, parameters) {
163
- return new Promise((resolve, reject) => {
164
- const id = randombytes(20).toString()
165
- const onmessage = message => {
166
- pubsub.unsubscribe(id, onmessage)
167
- resolve(message)
168
- }
169
- pubsub.subscribe(id, onmessage)
170
- this.worker.postMessage({
171
- type: 'get',
172
- id,
173
- input: {
174
- contract,
175
- method,
176
- params: parameters
177
- }
178
- })
179
- })
180
- }
181
-
182
-
183
- async has(address) {
184
- return new Promise((resolve, reject) => {
185
- const id = randombytes(20).toString('hex')
186
- const onmessage = message => {
187
- pubsub.unsubscribe(id, onmessage)
188
- if (message?.error) reject(message.error)
189
- else resolve(message)
190
- }
191
- pubsub.subscribe(id, onmessage)
192
- this.worker.postMessage({
193
- type: 'has',
194
- id,
195
- input: {
196
- address
197
- }
198
- })
199
- })
200
- }
201
-
202
- async delete(hash) {
203
- return contractStore.delete(hash)
204
- }
205
-
206
- /**
207
- *
208
- * @returns Promise
209
- */
210
- async deleteAll() {
211
- let hashes = await contractStore.get()
212
- hashes = Object.keys(hashes).map(hash => this.delete(hash))
213
- return Promise.all(hashes)
214
- }
215
- }
1
+ import { contractFactory, nativeToken, validators, nameService } from '@leofcoin/addresses'
2
+ import { randombytes } from '@leofcoin/crypto'
3
+ import EasyWorker from '@vandeurenglenn/easy-worker'
4
+ import { ContractMessage } from '@leofcoin/messages'
5
+ // import State from './state'
6
+
7
+ export default class Machine {
8
+ #contracts = {}
9
+ #nonces = {}
10
+ lastBlock = {index: 0, hash: '0x0', previousHash: '0x0'}
11
+
12
+ constructor(blocks) {
13
+ return this.#init(blocks)
14
+ }
15
+
16
+ #createMessage(sender = peernet.selectedAccount) {
17
+ return {
18
+ sender,
19
+ call: this.execute,
20
+ staticCall: this.get.bind(this)
21
+ }
22
+ }
23
+
24
+ async #onmessage(data) {
25
+ switch (data.type) {
26
+ case 'contractError': {
27
+ console.warn(`removing contract ${await data.hash()}`);
28
+ await contractStore.delete(await data.hash())
29
+ break
30
+ }
31
+
32
+ case 'initError': {
33
+ console.error(`init error: ${data.message}`);
34
+ break
35
+ }
36
+
37
+ case 'executionError': {
38
+ // console.warn(`error executing transaction ${data.message}`);
39
+ pubsub.publish(data.id, {error: data.message})
40
+ break
41
+ }
42
+
43
+ case 'debug': {
44
+ for (const message of data.messages) debug(message)
45
+ break
46
+ }
47
+ case 'machine-ready': {
48
+ this.lastBlock = data.lastBlock
49
+ pubsub.publish('machine.ready', true)
50
+ break
51
+ }
52
+ case 'response': {
53
+ pubsub.publish(data.id, data.value || false)
54
+ break
55
+ }
56
+ }
57
+
58
+ }
59
+
60
+ async #init(blocks) {
61
+ return new Promise(async (resolve) => {
62
+ const machineReady = () => {
63
+ pubsub.unsubscribe('machine.ready', machineReady)
64
+ resolve(this)
65
+ }
66
+ pubsub.subscribe('machine.ready', machineReady)
67
+
68
+ this.worker = await new EasyWorker('node_modules/@leofcoin/workers/src/machine-worker.js', {serialization: 'advanced', type:'module'})
69
+ this.worker.onmessage(this.#onmessage.bind(this))
70
+
71
+ // const blocks = await blockStore.values()
72
+ const contracts = await Promise.all([
73
+ contractStore.get(contractFactory),
74
+ contractStore.get(nativeToken),
75
+ contractStore.get(validators),
76
+ contractStore.get(nameService)
77
+ ])
78
+
79
+ const message = {
80
+ type: 'init',
81
+ input: {
82
+ contracts,
83
+ blocks,
84
+ peerid: peernet.peerId
85
+ }
86
+ }
87
+ this.worker.postMessage(message)
88
+ })
89
+
90
+ }
91
+
92
+ async #runContract(contractMessage) {
93
+ const hash = await contractMessage.hash()
94
+ return new Promise((resolve, reject) => {
95
+ const id = randombytes(20).toString('hex')
96
+ const onmessage = message => {
97
+ pubsub.unsubscribe(id, onmessage)
98
+ if (message?.error) reject(message.error)
99
+ else resolve(message)
100
+ }
101
+ pubsub.subscribe(id, onmessage)
102
+ this.worker.postMessage({
103
+ type: 'run',
104
+ id,
105
+ input: {
106
+ decoded: contractMessage.decoded,
107
+ encoded: contractMessage.encoded,
108
+ hash
109
+ }
110
+ })
111
+ })
112
+
113
+ }
114
+
115
+ /**
116
+ *
117
+ * @param {Address} contract
118
+ * @param {String} method
119
+ * @param {Array} parameters
120
+ * @returns Promise<message>
121
+ */
122
+ async execute(contract, method, parameters) {
123
+ try {
124
+ if (contract === contractFactory && method === 'registerContract') {
125
+ if (await this.has(parameters[0])) throw new Error(`duplicate contract @${parameters[0]}`)
126
+ let message;
127
+ if (!await contractStore.has(parameters[0])) {
128
+ message = await peernet.get(parameters[0], 'contract')
129
+ message = await new ContractMessage(message)
130
+ await contractStore.put(await message.hash(), message.encoded)
131
+ }
132
+ if (!message) {
133
+ message = await contractStore.get(parameters[0])
134
+ message = await new ContractMessage(message)
135
+ }
136
+ if (!await this.has(await message.hash())) await this.#runContract(message)
137
+ }
138
+ } catch (error) {
139
+ throw new Error(`contract deployment failed for ${parameters[0]}\n${error.message}`)
140
+ }
141
+ return new Promise((resolve, reject) => {
142
+ const id = randombytes(20).toString('hex')
143
+ const onmessage = message => {
144
+ pubsub.unsubscribe(id, onmessage)
145
+ if (message?.error) reject(message.error)
146
+ else resolve(message)
147
+ }
148
+ pubsub.subscribe(id, onmessage)
149
+ this.worker.postMessage({
150
+ type: 'execute',
151
+ id,
152
+ input: {
153
+ contract,
154
+ method,
155
+ params: parameters
156
+ }
157
+ })
158
+ })
159
+
160
+ }
161
+
162
+ get(contract, method, parameters) {
163
+ return new Promise((resolve, reject) => {
164
+ const id = randombytes(20).toString()
165
+ const onmessage = message => {
166
+ pubsub.unsubscribe(id, onmessage)
167
+ resolve(message)
168
+ }
169
+ pubsub.subscribe(id, onmessage)
170
+ this.worker.postMessage({
171
+ type: 'get',
172
+ id,
173
+ input: {
174
+ contract,
175
+ method,
176
+ params: parameters
177
+ }
178
+ })
179
+ })
180
+ }
181
+
182
+
183
+ async has(address) {
184
+ return new Promise((resolve, reject) => {
185
+ const id = randombytes(20).toString('hex')
186
+ const onmessage = message => {
187
+ pubsub.unsubscribe(id, onmessage)
188
+ if (message?.error) reject(message.error)
189
+ else resolve(message)
190
+ }
191
+ pubsub.subscribe(id, onmessage)
192
+ this.worker.postMessage({
193
+ type: 'has',
194
+ id,
195
+ input: {
196
+ address
197
+ }
198
+ })
199
+ })
200
+ }
201
+
202
+ async delete(hash) {
203
+ return contractStore.delete(hash)
204
+ }
205
+
206
+ /**
207
+ *
208
+ * @returns Promise
209
+ */
210
+ async deleteAll() {
211
+ let hashes = await contractStore.get()
212
+ hashes = Object.keys(hashes).map(hash => this.delete(hash))
213
+ return Promise.all(hashes)
214
+ }
215
+ }
package/src/node.js CHANGED
@@ -1,24 +1,24 @@
1
- // import config from './config/config'
2
- import Peernet from '@leofcoin/peernet'
3
- import nodeConfig from '@leofcoin/lib/node-config';
4
- import networks from '@leofcoin/networks';
5
-
6
- export default class Node {
7
- constructor() {
8
- return this._init()
9
- }
10
-
11
- async _init(config = {
12
- network: 'leofcoin:peach',
13
- networkName: 'leofcoin:peach',
14
- networkVersion: 'peach',
15
- stars: networks.leofcoin.peach.stars
16
- }) {
17
- globalThis.Peernet ? await new globalThis.Peernet(config) : await new Peernet(config)
18
- await nodeConfig(config)
19
-
20
- return this
21
- // this.config = await config()
22
- }
23
-
24
- }
1
+ // import config from './config/config'
2
+ import Peernet from '@leofcoin/peernet'
3
+ import nodeConfig from '@leofcoin/lib/node-config';
4
+ import networks from '@leofcoin/networks';
5
+
6
+ export default class Node {
7
+ constructor() {
8
+ return this._init()
9
+ }
10
+
11
+ async _init(config = {
12
+ network: 'leofcoin:peach',
13
+ networkName: 'leofcoin:peach',
14
+ networkVersion: 'peach',
15
+ stars: networks.leofcoin.peach.stars
16
+ }) {
17
+ globalThis.Peernet ? await new globalThis.Peernet(config) : await new Peernet(config)
18
+ await nodeConfig(config)
19
+
20
+ return this
21
+ // this.config = await config()
22
+ }
23
+
24
+ }
package/src/protocol.js CHANGED
@@ -1,4 +1,4 @@
1
- export default class Protocol {
2
- limit = 1800
3
- transactionLimit = 1800
1
+ export default class Protocol {
2
+ limit = 1800
3
+ transactionLimit = 1800
4
4
  }