@leofcoin/chain 1.3.1 → 1.3.4

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 (48) hide show
  1. package/demo/chain.browser.js +17 -13
  2. package/demo/peernet-swarm.browser.js +8 -9
  3. package/demo/workers/machine-worker.js +30 -11
  4. package/dist/browser/workers/machine-worker.js +18 -12
  5. package/dist/chain.js +149 -134
  6. package/dist/contracts/factory.js +1 -1
  7. package/dist/contracts/{nameService.js → name-service.js} +1 -1
  8. package/dist/contracts/native-token.js +1 -0
  9. package/dist/contracts/validators.js +1 -1
  10. package/dist/module/chain.js +146 -131
  11. package/dist/module/workers/machine-worker.js +18 -12
  12. package/dist/standards/token.js +1 -1
  13. package/dist/workers/machine-worker.js +1 -1
  14. package/package.json +20 -2
  15. package/rollup.config.js +4 -4
  16. package/src/chain.js +112 -102
  17. package/src/contracts/factory.js +58 -15
  18. package/src/contracts/{nameService.js → name-service.js} +3 -5
  19. package/src/contracts/{nativeToken.js → native-token.js} +2 -2
  20. package/src/contracts/{powerToken.js → power-token.js} +1 -1
  21. package/src/contracts/proxies/{factoryProxy.js → factory-proxy.js} +1 -1
  22. package/src/contracts/proxies/{nameServiceProxy.js → name-service-proxy.js} +1 -1
  23. package/src/contracts/proxies/{nativeTokenProxy.js → native-token-proxy.js} +1 -1
  24. package/src/contracts/proxies/{validatorsProxy.js → validators-proxy.js} +1 -1
  25. package/src/contracts/proxies/{votingProxy.js → voting-proxy.js} +1 -1
  26. package/src/contracts/{proxyManager.js → proxy-manager.js} +1 -1
  27. package/src/contracts/validators.js +35 -25
  28. package/src/fee/config.js +1 -1
  29. package/src/machine.js +34 -29
  30. package/src/standards/{proxyManager.js → proxy-manager.js} +0 -0
  31. package/src/standards/{Proxy.js → proxy.js} +4 -8
  32. package/src/standards/roles.js +7 -5
  33. package/src/standards/voting.js +1 -0
  34. package/src/transactions/transaction.js +1 -3
  35. package/src/transactions/validator.js +1 -1
  36. package/src/typer.js +1 -1
  37. package/dist/865.browser.js +0 -10
  38. package/dist/chain.browser.js +0 -59741
  39. package/dist/contracts/nativeToken.js +0 -1
  40. package/dist/generate-account.browser.js +0 -50
  41. package/dist/messages.browser.js +0 -328
  42. package/dist/multi-wallet.browser.js +0 -15
  43. package/dist/node.browser.js +0 -9858
  44. package/dist/pako.browser.js +0 -6900
  45. package/dist/peernet-swarm.browser.js +0 -840
  46. package/dist/storage.browser.js +0 -3724
  47. package/dist/wrtc.browser.js +0 -28
  48. package/src/standards/Voting.js +0 -3
@@ -1,4 +1,6 @@
1
- export default class Validators {
1
+ import Roles from './../standards/roles'
2
+
3
+ export default class Validators extends Roles {
2
4
  /**
3
5
  * string
4
6
  */
@@ -7,43 +9,46 @@ export default class Validators {
7
9
  * uint
8
10
  */
9
11
  #totalValidators = 0
12
+
13
+ #activeValidators = 0
10
14
  /**
11
15
  * Object => string(address) => Object
12
16
  */
13
17
  #validators = {}
14
18
 
15
- #owner
16
-
17
19
  #currency
18
20
 
19
21
  #minimumBalance
20
22
 
21
23
  get state() {
22
24
  return {
23
- owner: this.#owner,
25
+ ...super.state,
24
26
  minimumBalance: this.#minimumBalance,
25
27
  currency: this.#currency,
26
28
  totalValidators: this.#totalValidators,
29
+ activeValidators: this.#activeValidators,
27
30
  validators: this.#validators
28
31
  }
29
32
  }
30
33
 
31
34
  constructor(tokenAddress, state) {
35
+ super(state?.roles)
32
36
  if (state) {
33
- this.#owner = state.owner
34
37
  this.#minimumBalance = state.minimumBalance
35
38
  this.#currency = state.currency
36
39
 
37
40
  this.#totalValidators = state.totalValidators
41
+ this.#activeValidators = state.activeValidators
38
42
  this.#validators = state.validators
39
43
  } else {
40
- this.#owner = msg.sender
41
- this.#minimumBalance = 50000
44
+ this.#minimumBalance = 50_000
42
45
  this.#currency = tokenAddress
43
46
 
44
47
  this.#totalValidators += 1
48
+ this.#activeValidators += 1
45
49
  this.#validators[msg.sender] = {
46
- firstSeen: new Date().getTime(),
50
+ firstSeen: Date.now(),
51
+ lastSeen: Date.now(),
47
52
  active: true
48
53
  }
49
54
  }
@@ -54,10 +59,6 @@ export default class Validators {
54
59
  return this.#name
55
60
  }
56
61
 
57
- get owner() {
58
- return this.#owner
59
- }
60
-
61
62
  get currency() {
62
63
  return this.#currency
63
64
  }
@@ -74,12 +75,8 @@ export default class Validators {
74
75
  return this.#minimumBalance
75
76
  }
76
77
 
77
- changeOwner(owner) {
78
- if (msg.sender !== this.#owner) throw new Error('not an owner')
79
- }
80
-
81
78
  changeCurrency(currency) {
82
- if (msg.sender !== this.#owner) throw new Error('not an owner')
79
+ if (!this.hasRole(msg.sender, 'OWNER')) throw new Error('not an owner')
83
80
  this.#currency = currency
84
81
  }
85
82
 
@@ -87,33 +84,46 @@ export default class Validators {
87
84
  return Boolean(this.#validators[validator] !== undefined)
88
85
  }
89
86
 
87
+ #isAllowed(address) {
88
+ if (msg.sender !== address && !this.hasRole(msg.sender, 'OWNER')) throw new Error('sender is not the validator or owner')
89
+ return true
90
+ }
91
+
90
92
  async addValidator(validator) {
93
+ this.#isAllowed(validator)
91
94
  if (this.has(validator)) throw new Error('already a validator')
92
- const balance = await msg.staticCall(this.currency, 'balanceOf', [msg.sender])
95
+
96
+ const balance = await msg.staticCall(this.currency, 'balanceOf', [validator])
93
97
 
94
98
  if (balance < this.minimumBalance) throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
95
99
 
96
100
  this.#totalValidators += 1
101
+ this.#activeValidators += 1
97
102
  this.#validators[validator] = {
98
- firstSeen: new Date().getTime(),
103
+ firstSeen: Date.now(),
104
+ lastSeen: Date.now(),
99
105
  active: true
100
106
  }
101
107
  }
102
108
 
103
109
  removeValidator(validator) {
110
+ this.#isAllowed(validator)
104
111
  if (!this.has(validator)) throw new Error('validator not found')
105
-
112
+
106
113
  this.#totalValidators -= 1
114
+ if (this.#validators[validator].active) this.#activeValidators -= 1
107
115
  delete this.#validators[validator]
108
116
  }
109
117
 
110
118
  async updateValidator(validator, active) {
119
+ this.#isAllowed(validator)
111
120
  if (!this.has(validator)) throw new Error('validator not found')
112
- const balance = await msg.staticCall(this.currency, 'balanceOf', [msg.sender])
113
- if (balance < this.minimumBalance && this.#validators[validator].active) this.#validators[validator].active = false
114
-
115
- if (balance < this.minimumBalance) throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
116
-
121
+ const balance = await msg.staticCall(this.currency, 'balanceOf', [validator])
122
+ if (balance < this.minimumBalance && active) throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
123
+ if (this.#validators[validator].active === active) throw new Error(`already ${active ? 'activated' : 'deactivated'}`)
124
+ if (active) this.#activeValidators += 1
125
+ else this.#activeValidators -= 1
126
+ /** minimum balance always needs to be met */
117
127
  this.#validators[validator].active = active
118
128
  }
119
129
  }
package/src/fee/config.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export default {
2
- deployment: 0.000001
2
+ deployment: 0.000_001
3
3
  }
package/src/machine.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { contractFactory, nativeToken, validators, nameService } from './../../addresses/src/addresses.js'
2
2
  import { formatBytes } from './../../utils/src/utils'
3
- import { randomBytes } from 'crypto'
4
- import { join } from 'path'
3
+ import { randomBytes } from 'node:crypto'
4
+ import { join } from 'node:path'
5
5
  import EasyWorker from '@vandeurenglenn/easy-worker'
6
6
 
7
7
  // import State from './state'
@@ -11,8 +11,8 @@ export default class Machine {
11
11
  #nonces = {}
12
12
  lastBlock = {index: 0, hash: '0x0', previousHash: '0x0'}
13
13
 
14
- constructor() {
15
- return this.#init()
14
+ constructor(blocks) {
15
+ return this.#init(blocks)
16
16
  }
17
17
 
18
18
  #createMessage(sender = peernet.selectedAccount) {
@@ -25,31 +25,36 @@ export default class Machine {
25
25
 
26
26
  async #onmessage(data) {
27
27
  switch (data.type) {
28
- case 'contractError':
28
+ case 'contractError': {
29
29
  console.warn(`removing contract ${await data.hash}`);
30
30
  await contractStore.delete(await data.hash)
31
- break
31
+ break
32
+ }
32
33
 
33
- case 'executionError':
34
+ case 'executionError': {
34
35
  // console.warn(`error executing transaction ${data.message}`);
35
36
  pubsub.publish(data.id, {error: data.message})
36
- break
37
+ break
38
+ }
37
39
 
38
- case 'debug':
39
- data.messages.forEach(message => debug(message))
40
- break
41
- case 'machine-ready':
40
+ case 'debug': {
41
+ for (const message of data.messages) debug(message)
42
+ break
43
+ }
44
+ case 'machine-ready': {
42
45
  this.lastBlock = data.lastBlock
43
46
  pubsub.publish('machine.ready', true)
44
- break
45
- case 'response':
47
+ break
48
+ }
49
+ case 'response': {
46
50
  pubsub.publish(data.id, data.value)
47
- break
51
+ break
52
+ }
48
53
  }
49
54
 
50
55
  }
51
56
 
52
- async #init() {
57
+ async #init(blocks) {
53
58
  return new Promise(async (resolve) => {
54
59
  pubsub.subscribe('machine.ready', () => {
55
60
  resolve(this)
@@ -58,7 +63,7 @@ export default class Machine {
58
63
  this.worker = await new EasyWorker(join(__dirname, './workers/machine-worker.js'), {serialization: 'advanced', type:'module'})
59
64
  this.worker.onmessage(this.#onmessage.bind(this))
60
65
 
61
- const blocks = await blockStore.values()
66
+ // const blocks = await blockStore.values()
62
67
  const contracts = await Promise.all([
63
68
  contractStore.get(contractFactory),
64
69
  contractStore.get(nativeToken),
@@ -80,19 +85,19 @@ export default class Machine {
80
85
  }
81
86
 
82
87
  async #runContract(contractMessage) {
83
- const params = contractMessage.decoded.constructorParameters
88
+ const parameters = contractMessage.decoded.constructorParameters
84
89
  try {
85
90
 
86
- const func = new Function(contractMessage.decoded.contract)
87
- const Contract = func()
91
+ const function_ = new Function(contractMessage.decoded.contract)
92
+ const Contract = function_()
88
93
 
89
94
  globalThis.msg = this.#createMessage(contractMessage.decoded.creator)
90
95
  // globalThis.msg = {sender: contractMessage.decoded.creator}
91
- this.#contracts[await contractMessage.hash] = await new Contract(...params)
96
+ this.#contracts[await contractMessage.hash] = await new Contract(...parameters)
92
97
  debug(`loaded contract: ${await contractMessage.hash}`);
93
98
  debug(`size: ${formatBytes(contractMessage.encoded.length)}`);
94
- } catch (e) {
95
- console.log(e);
99
+ } catch (error) {
100
+ console.log(error);
96
101
  console.warn(`removing contract ${await contractMessage.hash}`);
97
102
  await contractStore.delete(await contractMessage.hash, contractMessage.encoded)
98
103
  }
@@ -109,7 +114,7 @@ export default class Machine {
109
114
  throw new Error('duplicate contract')
110
115
  }
111
116
 
112
- async execute(contract, method, params) {
117
+ async execute(contract, method, parameters) {
113
118
  return new Promise((resolve, reject) => {
114
119
  const id = randomBytes(20).toString('hex')
115
120
  const message = message => {
@@ -123,22 +128,22 @@ export default class Machine {
123
128
  input: {
124
129
  contract,
125
130
  method,
126
- params
131
+ params: parameters
127
132
  }
128
133
  })
129
134
  })
130
135
 
131
136
  }
132
137
 
133
- addJob(contract, method, params, from, nonce) {
138
+ addJob(contract, method, parameters, from, nonce) {
134
139
  if (!this.#nonces[from]) this.#nonces[from] = nonce
135
- if (nonce === this.#nonces[from] + 1) return this.#contracts[contract][method](...params)
140
+ if (nonce === this.#nonces[from] + 1) return this.#contracts[contract][method](...parameters)
136
141
  // return setTimeout(() => {
137
142
  // return this.addJob(contract, method, params, from, nonce)
138
143
  // }, 50)
139
144
  }
140
145
 
141
- get(contract, method, params) {
146
+ get(contract, method, parameters) {
142
147
  return new Promise((resolve, reject) => {
143
148
  const id = randomBytes(20).toString()
144
149
  const message = message => {
@@ -151,7 +156,7 @@ export default class Machine {
151
156
  input: {
152
157
  contract,
153
158
  method,
154
- params
159
+ params: parameters
155
160
  }
156
161
  })
157
162
  })
@@ -6,11 +6,7 @@ export default class Proxy extends Roles {
6
6
 
7
7
  constructor(proxyManager, state) {
8
8
  super(state?.roles)
9
- if (state) {
10
- this.#proxyManager = state.proxyManager
11
- } else {
12
- this.#proxyManager = proxyManager
13
- }
9
+ this.#proxyManager = state ? state.proxyManager : proxyManager;
14
10
  }
15
11
 
16
12
  get state() {
@@ -35,8 +31,8 @@ export default class Proxy extends Roles {
35
31
  this.#proxyManager = address
36
32
  }
37
33
 
38
- fallback(method, params) {
39
- if (msg.sender === this.proxyManager) return this[method](...params)
40
- return msg.internalCall(msg.sender, this.#implementation, method, params)
34
+ fallback(method, parameters) {
35
+ if (msg.sender === this.proxyManager) return this[method](...parameters)
36
+ return msg.internalCall(msg.sender, this.#implementation, method, parameters)
41
37
  }
42
38
  }
@@ -4,6 +4,7 @@ export default class Roles {
4
4
  * Object => Array
5
5
  */
6
6
  #roles = {
7
+ 'IMPLEMENTATION_MANAGER': [],
7
8
  'OWNER': [],
8
9
  'MINT': [],
9
10
  'BURN': []
@@ -12,17 +13,18 @@ export default class Roles {
12
13
  constructor(roles) {
13
14
  // allow devs to set their own roles but always keep the default ones included
14
15
  // also allows roles to be loaded from the stateStore
15
- // carefull when includin gthe roles make sure to add the owner
16
- // since no roles are granted by default when using custom roles
16
+ // carefull when including the roles make sure to add the owner
17
+ // because no roles are granted by default when using custom roles
17
18
  if (roles) {
18
19
  if (roles instanceof Object) {
19
20
  this.#roles = {...roles, ...this.#roles}
20
21
  } else {
21
- throw new Error(`expected roles to be an object`)
22
+ throw new TypeError(`expected roles to be an object`)
22
23
  }
23
24
  } else {
24
- // no roles given so default to the msg sender
25
+ // no roles given so fallback to default to the msg sender
25
26
  this.#grantRole(msg.sender, 'OWNER')
27
+ this.#grantRole(msg.sender, 'IMPLEMENTATION_MANAGER')
26
28
  }
27
29
  }
28
30
 
@@ -35,7 +37,7 @@ export default class Roles {
35
37
  }
36
38
 
37
39
  hasRole(address, role) {
38
- return this.#roles[role] ? this.#roles[role].indexOf(address) !== -1 : false
40
+ return this.#roles[role] ? this.#roles[role].includes(address) : false
39
41
  }
40
42
 
41
43
  #grantRole(address, role) {
@@ -0,0 +1 @@
1
+ export default class ArtOnlineVoting {}
@@ -1,3 +1 @@
1
- export default () => {
2
-
3
- }
1
+ export default () => {}
@@ -19,7 +19,7 @@ export default class Validator {
19
19
  async validateTransaction(hash, transaction) {
20
20
  delete transaction.hash
21
21
  transaction = await create(hasher.hash(transaction))
22
- if (transaction !== hash) throw Error('invalid hash')
22
+ if (transaction !== hash) throw new Error('invalid hash')
23
23
  // for (const input of transaction) {
24
24
  // input
25
25
  // }
package/src/typer.js CHANGED
@@ -11,7 +11,7 @@ export default {
11
11
  isType: (type, value) => {
12
12
  type = type.toLowercase()
13
13
  if (type === 'string') return typeof(value) === type
14
- if (type === 'number') return !isNaN(Number(value))
14
+ if (type === 'number') return !Number.isNaN(Number(value))
15
15
  if (type === 'address') return isAddress(value)
16
16
  if (type === 'BigNumber') return BigNumber.isBigNumber(value)
17
17
  },
@@ -1,10 +0,0 @@
1
- (self["webpackChunk_leofcoin_chain"] = self["webpackChunk_leofcoin_chain"] || []).push([[865],{
2
-
3
- /***/ 3865:
4
- /***/ (function() {
5
-
6
- /* (ignored) */
7
-
8
- /***/ })
9
-
10
- }])