EVMinterface.jsView |
---|
1 | 1 | |
2 | 2 | * This is the Ethereum interface that is exposed to the WASM instance which |
3 | 3 | * enables to interact with the Ethereum Environment |
4 | 4 | */ |
| 5 | + |
| 6 | +const fs = require('fs') |
| 7 | +const path = require('path') |
| 8 | +const ethUtil = require('ethereumjs-util') |
5 | 9 | const Vertex = require('merkle-trie') |
6 | 10 | const Address = require('./deps/address.js') |
7 | 11 | const U256 = require('./deps/u256.js') |
8 | | -const fs = require('fs') |
9 | | -const path = require('path') |
10 | 12 | |
11 | 13 | const U128_SIZE_BYTES = 16 |
12 | 14 | const ADDRESS_SIZE_BYTES = 20 |
13 | 15 | const U256_SIZE_BYTES = 32 |
209 | 211 | |
210 | 212 | * Gets the size of code running in current environment. |
211 | 213 | * @return {interger} |
212 | 214 | */ |
213 | | - getCodeSize () { |
| 215 | + getCodeSize (cbIndex) { |
214 | 216 | this.takeGas(2) |
215 | 217 | |
216 | | - return this.kernel.environment.code.length |
| 218 | + const opPromise = this.kernel.environment.state.get('code') |
| 219 | + .then(vertex => vertex.value.length) |
| 220 | + |
| 221 | + |
| 222 | + this.kernel.pushOpsQueue(opPromise, cbIndex, length => length) |
217 | 223 | } |
218 | 224 | |
219 | 225 | |
220 | 226 | * Copys the code running in current environment to memory. |
263 | 269 | * @param {integer} resultOffset the memory offset |
264 | 270 | * @param {integer} codeOffset the code offset |
265 | 271 | * @param {integer} length the length of code to copy |
266 | 272 | */ |
267 | | - externalCodeCopy (addressOffset, resultOffset, codeOffset, length) { |
| 273 | + externalCodeCopy (addressOffset, resultOffset, codeOffset, length, cbIndex) { |
268 | 274 | this.takeGas(20 + Math.ceil(length / 32) * 3) |
269 | 275 | |
| 276 | + const address = [...this.getMemory(addressOffset, ADDRESS_SIZE_BYTES), 'code'] |
| 277 | + let opPromise |
| 278 | + |
270 | 279 | if (length) { |
271 | | - const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) |
272 | | - let code = this.kernel.environment.getCode(address) |
| 280 | + opPromise = this.kernel.environment.state.root |
| 281 | + .get(address) |
| 282 | + .then(vertex => vertex.value) |
| 283 | + .catch(() => []) |
| 284 | + } else { |
| 285 | + opPromise = Promise.resolve([]) |
| 286 | + } |
| 287 | + |
| 288 | + |
| 289 | + this.kernel.pushOpsQueue(opPromise, cbIndex, code => { |
273 | 290 | code = code.slice(codeOffset, codeOffset + length) |
274 | 291 | this.setMemory(resultOffset, length, code) |
275 | | - } |
| 292 | + }) |
276 | 293 | } |
277 | 294 | |
278 | 295 | |
279 | 296 | * Gets price of gas in current environment. |
403 | 420 | * @param {integer} length the data length |
404 | 421 | * @param (integer} resultOffset the offset to write the new contract address to |
405 | 422 | * @return {integer} Return 1 or 0 depending on if the VM trapped on the message or not |
406 | 423 | */ |
407 | | - create (valueOffset, dataOffset, length, resultOffset) { |
| 424 | + create (valueOffset, dataOffset, length, resultOffset, cbIndex) { |
408 | 425 | this.takeGas(32000) |
409 | 426 | |
410 | 427 | const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES)) |
411 | | - if (length) { |
412 | | - const data = this.getMemory(dataOffset, length).slice(0) |
413 | | - |
414 | | - } |
415 | | - let address |
| 428 | + const code = this.getMemory(dataOffset, length).slice(0) |
| 429 | + let opPromise |
| 430 | + |
416 | 431 | if (value.gt(this.kernel.environment.value)) { |
417 | | - address = new Address() |
| 432 | + opPromise = Promise.resolve(new Address()) |
418 | 433 | } else { |
419 | | - address = new Address('0x945304eb96065b2a98b57a48a06ae28d285a71b5') |
| 434 | + |
| 435 | + opPromise = Promise.resolve(ethUtil.generateAddress(this.kernel.environment.from, this.kernel.environment.nonce)) |
420 | 436 | } |
421 | | - this.setMemory(resultOffset, ADDRESS_SIZE_BYTES, address.toMemory()) |
422 | | - |
| 437 | + |
| 438 | + |
| 439 | + this.kernel.pushOpsQueue(opPromise, cbIndex, address => { |
| 440 | + this.setMemory(resultOffset, ADDRESS_SIZE_BYTES, address) |
| 441 | + }) |
423 | 442 | } |
424 | 443 | |
425 | 444 | |
426 | 445 | * Sends a message with arbiatary data to a given address path |
538 | 557 | * @param {interger} pathOffest the memory offset to load the the path from |
539 | 558 | * @param {interger} valueOffset the memory offset to load the value from |
540 | 559 | */ |
541 | 560 | storageStore (pathOffset, valueOffset, cbIndex) { |
| 561 | + console.log('storage store'); |
542 | 562 | this.takeGas(5000) |
543 | 563 | const path = ['storage', ...this.getMemory(pathOffset, U256_SIZE_BYTES)] |
544 | 564 | |
545 | 565 | const value = this.getMemory(valueOffset, U256_SIZE_BYTES).slice(0) |