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 Vertex = require('merkle-trie') |
5 | 7 | const Address = require('./deps/address.js') |
6 | 8 | const U256 = require('./deps/u256.js') |
7 | 9 | const fs = require('fs') |
8 | 10 | const path = require('path') |
230 | 232 | * Get size of an account’s code. |
231 | 233 | * @param {integer} addressOffset the offset in memory to load the address from |
232 | 234 | * @return {integer} |
233 | 235 | */ |
234 | | - getExternalCodeSize (addressOffset) { |
| 236 | + getExternalCodeSize (addressOffset, cbOffset) { |
235 | 237 | this.takeGas(20) |
236 | 238 | |
237 | | - const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) |
238 | | - const code = this.kernel.environment.getCode(address) |
239 | | - return code.length |
| 239 | + const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)).toArray() |
| 240 | + address.push('code') |
| 241 | + const opPromise = this.kernel.environment.state.root.get(address) |
| 242 | + .then(vertex => vertex.value.length) |
| 243 | + .catch(() => 0) |
| 244 | + |
| 245 | + |
| 246 | + this.kernel.pushOpsQueue(opPromise, cbOffset, length => length) |
240 | 247 | } |
241 | 248 | |
242 | 249 | |
243 | 250 | * Copys the code of an account to memory. |
521 | 528 | * @param {interger} valueOffset the memory offset to load the value from |
522 | 529 | */ |
523 | 530 | storageStore (pathOffset, valueOffset, cbIndex) { |
524 | 531 | this.takeGas(5000) |
525 | | - const path = [...this.getMemory(pathOffset, U256_SIZE_BYTES)] |
| 532 | + const path = ['storage', ...this.getMemory(pathOffset, U256_SIZE_BYTES)] |
526 | 533 | |
527 | 534 | const value = this.getMemory(valueOffset, U256_SIZE_BYTES).slice(0) |
528 | 535 | const valIsZero = value.every((i) => i === 0) |
529 | 536 | const opPromise = this.kernel.environment.state.get(path) |
530 | | - .catch(() => { |
531 | | - |
532 | | - |
533 | | - return null |
534 | | - }) |
| 537 | + .then(vertex => vertex.value) |
| 538 | + .catch(() => null) |
535 | 539 | |
536 | 540 | this.kernel.pushOpsQueue(opPromise, cbIndex, oldValue => { |
537 | 541 | if (valIsZero && oldValue) { |
538 | 542 | |
543 | 547 | |
544 | 548 | this.takeGas(15000) |
545 | 549 | } |
546 | 550 | |
547 | | - this.kernel.environment.state.set(path, value) |
| 551 | + this.kernel.environment.state.set(path, new Vertex({ |
| 552 | + value: value |
| 553 | + })) |
548 | 554 | } |
549 | 555 | }) |
550 | 556 | } |
551 | 557 | |
555 | 561 | * @param {interger} resultOffset the memory offset to load the value from |
556 | 562 | */ |
557 | 563 | storageLoad (pathOffset, resultOffset, cbIndex) { |
558 | 564 | this.takeGas(50) |
559 | | - console.log('offset: ' + resultOffset); |
560 | 565 | |
561 | 566 | |
562 | | - const path = [...this.getMemory(pathOffset, U256_SIZE_BYTES)] |
| 567 | + const path = ['storage', ...this.getMemory(pathOffset, U256_SIZE_BYTES)] |
| 568 | + |
563 | 569 | const opPromise = this.kernel.environment.state.get(path) |
564 | | - .catch(() => { |
565 | | - |
566 | | - |
567 | | - return new Uint8Array(32) |
568 | | - }) |
| 570 | + .then(vertex => vertex.value) |
| 571 | + .catch(() => new Uint8Array(32)) |
569 | 572 | |
570 | | - this.kernel.pushOpsQueue(opPromise, cbIndex, result => { |
571 | | - this.setMemory(resultOffset, U256_SIZE_BYTES, result) |
| 573 | + this.kernel.pushOpsQueue(opPromise, cbIndex, value => { |
| 574 | + this.setMemory(resultOffset, U256_SIZE_BYTES, value) |
572 | 575 | }) |
573 | 576 | } |
574 | 577 | |
575 | 578 | |