@leofcoin/chain 1.3.3 → 1.3.5

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 (46) hide show
  1. package/dist/browser/workers/machine-worker.js +0 -13
  2. package/dist/chain.js +155 -133
  3. package/dist/contracts/factory.js +1 -1
  4. package/dist/contracts/{nameService.js → name-service.js} +1 -1
  5. package/dist/contracts/native-token.js +1 -0
  6. package/dist/contracts/validators.js +1 -1
  7. package/dist/module/chain.js +152 -130
  8. package/dist/module/workers/machine-worker.js +0 -6
  9. package/dist/standards/token.js +1 -1
  10. package/dist/workers/machine-worker.js +1 -1
  11. package/package.json +20 -2
  12. package/rollup.config.js +4 -4
  13. package/src/chain.js +122 -104
  14. package/src/contracts/factory.js +58 -15
  15. package/src/contracts/{nameService.js → name-service.js} +3 -5
  16. package/src/contracts/{nativeToken.js → native-token.js} +2 -2
  17. package/src/contracts/{powerToken.js → power-token.js} +1 -1
  18. package/src/contracts/proxies/{factoryProxy.js → factory-proxy.js} +1 -1
  19. package/src/contracts/proxies/{nameServiceProxy.js → name-service-proxy.js} +1 -1
  20. package/src/contracts/proxies/{nativeTokenProxy.js → native-token-proxy.js} +1 -1
  21. package/src/contracts/proxies/{validatorsProxy.js → validators-proxy.js} +1 -1
  22. package/src/contracts/proxies/{votingProxy.js → voting-proxy.js} +1 -1
  23. package/src/contracts/{proxyManager.js → proxy-manager.js} +1 -1
  24. package/src/contracts/validators.js +35 -25
  25. package/src/fee/config.js +1 -1
  26. package/src/machine.js +30 -26
  27. package/src/standards/{proxyManager.js → proxy-manager.js} +0 -0
  28. package/src/standards/{Proxy.js → proxy.js} +4 -8
  29. package/src/standards/roles.js +7 -5
  30. package/src/standards/token.js +2 -2
  31. package/src/standards/voting.js +1 -0
  32. package/src/transactions/transaction.js +1 -3
  33. package/src/transactions/validator.js +1 -1
  34. package/src/typer.js +1 -1
  35. package/dist/865.browser.js +0 -10
  36. package/dist/chain.browser.js +0 -59745
  37. package/dist/contracts/nativeToken.js +0 -1
  38. package/dist/generate-account.browser.js +0 -50
  39. package/dist/messages.browser.js +0 -328
  40. package/dist/multi-wallet.browser.js +0 -15
  41. package/dist/node.browser.js +0 -9858
  42. package/dist/pako.browser.js +0 -6900
  43. package/dist/peernet-swarm.browser.js +0 -839
  44. package/dist/storage.browser.js +0 -3724
  45. package/dist/wrtc.browser.js +0 -28
  46. 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'
@@ -25,26 +25,31 @@ 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
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
37
  break
38
+ }
37
39
 
38
- case 'debug':
39
- data.messages.forEach(message => debug(message))
40
+ case 'debug': {
41
+ for (const message of data.messages) debug(message)
40
42
  break
41
- case 'machine-ready':
43
+ }
44
+ case 'machine-ready': {
42
45
  this.lastBlock = data.lastBlock
43
46
  pubsub.publish('machine.ready', true)
44
47
  break
45
- case 'response':
48
+ }
49
+ case 'response': {
46
50
  pubsub.publish(data.id, data.value)
47
51
  break
52
+ }
48
53
  }
49
54
 
50
55
  }
@@ -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
  }
@@ -101,15 +106,14 @@ export default class Machine {
101
106
  * @params {ContractMessage} - contractMessage
102
107
  */
103
108
  async addContract(contractMessage) {
104
- if (!await contractStore.has(contractMessage.hash)) {
105
- await contractStore.put(contractMessage.hash, contractMessage.encoded)
106
- await this.#runContract(contractMessage)
107
- return contractMessage.hash
108
- }
109
- throw new Error('duplicate contract')
109
+ if (await contractStore.has(await contractMessage.hash)) throw new Error('duplicate contract')
110
+
111
+ await contractStore.put(await contractMessage.hash, contractMessage.encoded)
112
+ await this.#runContract(contractMessage)
113
+ return contractMessage.hash
110
114
  }
111
115
 
112
- async execute(contract, method, params) {
116
+ async execute(contract, method, parameters) {
113
117
  return new Promise((resolve, reject) => {
114
118
  const id = randomBytes(20).toString('hex')
115
119
  const message = message => {
@@ -123,22 +127,22 @@ export default class Machine {
123
127
  input: {
124
128
  contract,
125
129
  method,
126
- params
130
+ params: parameters
127
131
  }
128
132
  })
129
133
  })
130
134
 
131
135
  }
132
136
 
133
- addJob(contract, method, params, from, nonce) {
137
+ addJob(contract, method, parameters, from, nonce) {
134
138
  if (!this.#nonces[from]) this.#nonces[from] = nonce
135
- if (nonce === this.#nonces[from] + 1) return this.#contracts[contract][method](...params)
139
+ if (nonce === this.#nonces[from] + 1) return this.#contracts[contract][method](...parameters)
136
140
  // return setTimeout(() => {
137
141
  // return this.addJob(contract, method, params, from, nonce)
138
142
  // }, 50)
139
143
  }
140
144
 
141
- get(contract, method, params) {
145
+ get(contract, method, parameters) {
142
146
  return new Promise((resolve, reject) => {
143
147
  const id = randomBytes(20).toString()
144
148
  const message = message => {
@@ -151,7 +155,7 @@ export default class Machine {
151
155
  input: {
152
156
  contract,
153
157
  method,
154
- params
158
+ params: parameters
155
159
  }
156
160
  })
157
161
  })
@@ -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) {
@@ -81,11 +81,11 @@ export default class Token extends Roles {
81
81
  this.#increaseBalance(to, amount)
82
82
  }
83
83
 
84
- burn(to, amount) {
84
+ burn(from, amount) {
85
85
  if (!this.hasRole(msg.sender, 'BURN')) throw new Error('not allowed')
86
86
 
87
87
  this.#totalSupply = this.#totalSupply.sub(amount)
88
- this.#decreaseBalance(to, amount)
88
+ this.#decreaseBalance(from, amount)
89
89
  }
90
90
 
91
91
  #beforeTransfer(from, to, amount) {
@@ -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
- }])