index.jsView |
---|
24 | 24 | const Address = require('./address.js') |
25 | 25 | const U256 = require('./u256.js') |
26 | 26 | const Utils = require('./utils.js') |
27 | 27 | const Transaction = require('./transaction.js') |
| 28 | +const Precompile = require('./precompile.js') |
28 | 29 | |
| 30 | +const meteringContract = new Address("0x000000000000000000000000000000000000000A") |
| 31 | +const transcompilerContract = new Address("0x000000000000000000000000000000000000000B") |
| 32 | + |
29 | 33 | module.exports = class Kernel { |
30 | 34 | |
31 | 35 | constructor (environment = new Environment()) { |
32 | 36 | this.environment = environment |
64 | 68 | |
65 | 69 | |
66 | 70 | |
67 | 71 | callHandler (call) { |
| 72 | + |
| 73 | + |
| 74 | + if (call.to.equals(meteringContract)) { |
| 75 | + return Precompile.meteringInjector(call) |
| 76 | + } else if (call.to.equals(transcompilerContract)) { |
| 77 | + return Precompile.transcompiler(call) |
| 78 | + } |
| 79 | + |
68 | 80 | let account = this.environment.state.get(call.to.toString()) |
69 | 81 | if (!account) { |
70 | 82 | throw new Error('Account not found: ' + call.to.toString()) |
71 | 83 | } |
75 | 87 | throw new Error('Contract not found') |
76 | 88 | } |
77 | 89 | |
78 | 90 | if (!Utils.isWASMCode(code)) { |
79 | | - throw new Error('Not an eWASM contract') |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + code = this.callHandler({ to: transcompilerContract, data: code }).returnValue |
80 | 95 | } |
81 | 96 | |
82 | 97 | |
83 | 98 | const environment = new Environment() |
102 | 117 | |
103 | 118 | |
104 | 119 | return { |
105 | 120 | executionOutcome: 1, |
106 | | - gasLeft: new U256(environment.gasLimit), |
| 121 | + gasLeft: new U256(environment.gasLimit), |
107 | 122 | gasRefund: new U256(environment.gasRefund), |
108 | 123 | returnValue: environment.returnValue, |
109 | 124 | selfDestructAddress: environment.selfDestructAddress, |
110 | 125 | logs: environment.logs |
153 | 168 | if (tx.to.isZero()) { |
154 | 169 | if (tx.data.length !== 0) { |
155 | 170 | console.log('This is a contract deployment transaction') |
156 | 171 | |
157 | | - |
158 | | - const code = tx.data |
| 172 | + |
| 173 | + const code = this.callHandler({ to: meteringContract, data: tx.data }).returnValue |
159 | 174 | |
160 | 175 | let address = Utils.newAccountAddress(tx.from, code) |
161 | 176 | |
162 | 177 | this.environment.addAccount(address.toString(), { |