@leofcoin/chain 1.4.6 → 1.4.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "description": "Official javascript implementation",
5
5
  "main": "./dist/node.js",
6
6
  "module": "./dist/chain.esm",
@@ -18,7 +18,7 @@
18
18
  "workers": "cp ./../workers/src/** ./workers",
19
19
  "build": "npm run c && webpack && cp ./dist/*browser.js ./demo && cp -r ./dist/browser/workers/ ./demo",
20
20
  "demo": "jsproject --serve ./demo --port 5478 --open demo",
21
- "test": "node test --openssl-legacy-provider",
21
+ "test": "node --openssl-legacy-provider test",
22
22
  "pack": "webpack"
23
23
  },
24
24
  "np": {
package/src/contract.js CHANGED
@@ -30,7 +30,7 @@ export default class Contract extends Transaction {
30
30
  */
31
31
  async createContractAddress(creator, contract, constructorParameters = []) {
32
32
  contract = await this.createContractMessage(creator, contract, constructorParameters)
33
- return contract.hash
33
+ return contract.hash()
34
34
  }
35
35
 
36
36
  /**
package/src/machine.js CHANGED
@@ -50,7 +50,7 @@ export default class Machine {
50
50
  break
51
51
  }
52
52
  case 'response': {
53
- pubsub.publish(data.id, data.value || true)
53
+ pubsub.publish(data.id, data.value || false)
54
54
  break
55
55
  }
56
56
  }
@@ -122,7 +122,7 @@ export default class Machine {
122
122
  async execute(contract, method, parameters) {
123
123
  try {
124
124
  if (contract === contractFactory && method === 'registerContract') {
125
- if (this.#contracts[parameters[0]]) throw new Error(`duplicate contract @${parameters[0]}`)
125
+ if (await this.has(parameters[0])) throw new Error(`duplicate contract @${parameters[0]}`)
126
126
  let message;
127
127
  if (!await contractStore.has(parameters[0])) {
128
128
  message = await peernet.get(parameters[0], 'contract')
@@ -133,7 +133,7 @@ export default class Machine {
133
133
  message = await contractStore.get(parameters[0])
134
134
  message = await new ContractMessage(message)
135
135
  }
136
- if (!this.#contracts[await message.hash()]) await this.#runContract(message)
136
+ if (!await this.has(await message.hash())) await this.#runContract(message)
137
137
  }
138
138
  } catch (error) {
139
139
  throw new Error(`contract deployment failed for ${parameters[0]}\n${error.message}`)
@@ -159,14 +159,6 @@ export default class Machine {
159
159
 
160
160
  }
161
161
 
162
- addJob(contract, method, parameters, from, nonce) {
163
- if (!this.#nonces[from]) this.#nonces[from] = nonce
164
- if (nonce === this.#nonces[from] + 1) return this.#contracts[contract][method](...parameters)
165
- // return setTimeout(() => {
166
- // return this.addJob(contract, method, params, from, nonce)
167
- // }, 50)
168
- }
169
-
170
162
  get(contract, method, parameters) {
171
163
  return new Promise((resolve, reject) => {
172
164
  const id = randombytes(20).toString()
@@ -187,6 +179,26 @@ export default class Machine {
187
179
  })
188
180
  }
189
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
+
190
202
  async delete(hash) {
191
203
  return contractStore.delete(hash)
192
204
  }