interface.jsView |
---|
13 | 13 | } |
14 | 14 | |
15 | 15 | get exportTable () { |
16 | 16 | let exportMethods = [ |
17 | | - |
18 | | - |
| 17 | + |
19 | 18 | 'useGas', |
20 | | - 'gas', |
21 | | - 'address', |
22 | | - 'balance', |
23 | | - 'origin', |
24 | | - 'caller', |
25 | | - 'callValue', |
26 | | - 'callDataSize', |
| 19 | + 'getGasLeft', |
| 20 | + 'getAddress', |
| 21 | + 'getBalance', |
| 22 | + 'getTxOrigin', |
| 23 | + 'getCaller', |
| 24 | + 'getCallValue', |
| 25 | + 'getCallDataSize', |
27 | 26 | 'callDataCopy', |
28 | | - 'codeSize', |
| 27 | + 'getCodeSize', |
29 | 28 | 'codeCopy', |
30 | | - 'extCodeSize', |
31 | | - 'extCodeCopy', |
32 | | - 'gasPrice', |
33 | | - 'blockHash', |
34 | | - 'coinbase', |
35 | | - 'timestamp', |
36 | | - 'number', |
37 | | - 'difficulty', |
38 | | - 'gasLimit', |
| 29 | + 'getExternalCodeSize', |
| 30 | + 'externalCodeCopy', |
| 31 | + 'getTxGasPrice', |
| 32 | + 'getBlockHash', |
| 33 | + 'getBlockCoinbase', |
| 34 | + 'getBlockTimestamp', |
| 35 | + 'getBlockNumber', |
| 36 | + 'getBlockDifficulty', |
| 37 | + 'getBlockGasLimit', |
39 | 38 | 'log', |
40 | 39 | 'create', |
41 | 40 | 'call', |
| 41 | + 'callCode', |
42 | 42 | 'callDelegate', |
43 | | - 'sstore', |
44 | | - 'sload', |
| 43 | + 'storageStore', |
| 44 | + 'storageLoad', |
45 | 45 | 'return', |
46 | | - 'suicide' |
| 46 | + 'selfDestruct' |
47 | 47 | ] |
48 | 48 | let ret = {} |
49 | 49 | exportMethods.forEach((method) => { |
50 | 50 | ret[method] = this[method].bind(this) |
72 | 72 | |
73 | 73 | * Returns the current amount of gas |
74 | 74 | * @return {integer} |
75 | 75 | */ |
76 | | - gas () { |
| 76 | + getGasLeft () { |
77 | 77 | return this.environment.gasLimit |
78 | 78 | } |
79 | 79 | |
80 | 80 | |
81 | 81 | * Gets address of currently executing account and loads it into memory at |
82 | 82 | * the given offset. |
83 | 83 | * @param {integer} offset |
84 | 84 | */ |
85 | | - address (offset) { |
| 85 | + getAddress (offset) { |
86 | 86 | this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toBuffer()) |
87 | 87 | } |
88 | 88 | |
89 | 89 | |
91 | 91 | * offset. |
92 | 92 | * @param {integer} addressOffset the memory offset to laod the address |
93 | 93 | * @param {integer} resultOffset |
94 | 94 | */ |
95 | | - balance (addressOffset, offset) { |
| 95 | + getBalance (addressOffset, offset) { |
96 | 96 | const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
97 | 97 | |
98 | 98 | const balance = this.environment.parent.environment.getBalance(address) |
99 | 99 | this.setMemory(offset, constants.BALANCE_SIZE_BYTES, balance.toBuffer(constants.BALANCE_SIZE_BYTES)) |
104 | 104 | * given offset. This is the sender of original transaction; it is never an |
105 | 105 | * account with non-empty associated code. |
106 | 106 | * @param {integer} offset |
107 | 107 | */ |
108 | | - origin (offset) { |
| 108 | + getTxOrigin (offset) { |
109 | 109 | this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toBuffer()) |
110 | 110 | } |
111 | 111 | |
112 | 112 | |
113 | 113 | * Gets caller address and loads it into memory at the given offset. This is |
114 | 114 | * the address of the account that is directly responsible for this execution. |
115 | 115 | * @param {integer} offset |
116 | 116 | */ |
117 | | - caller (offset) { |
| 117 | + getCaller (offset) { |
118 | 118 | this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toBuffer()) |
119 | 119 | } |
120 | 120 | |
121 | 121 | |
122 | 122 | * Gets the deposited value by the instruction/transaction responsible for |
123 | 123 | * this execution and loads it into memory at the given location. |
124 | 124 | * @param {integer} offset |
125 | 125 | */ |
126 | | - callValue (offset) { |
| 126 | + getCallValue (offset) { |
127 | 127 | this.setMemory(offset, constants.BALANCE_SIZE_BYTES, this.environment.callValue.toBuffer(constants.BALANCE_SIZE_BYTES)) |
128 | 128 | } |
129 | 129 | |
130 | 130 | |
131 | 131 | * Get size of input data in current environment. This pertains to the input |
132 | 132 | * data passed with the message call instruction or transaction. |
133 | 133 | * @return {integer} |
134 | 134 | */ |
135 | | - callDataSize () { |
| 135 | + getCallDataSize () { |
136 | 136 | return this.environment.callData.length |
137 | 137 | } |
138 | 138 | |
139 | 139 | |
151 | 151 | |
152 | 152 | * Gets the size of code running in current environment. |
153 | 153 | * @return {interger} |
154 | 154 | */ |
155 | | - codeSize () { |
| 155 | + getCodeSize () { |
156 | 156 | return this.environment.code.length |
157 | 157 | } |
158 | 158 | |
159 | 159 | |
161 | 161 | * @param {integer} offset the memory offset |
162 | 162 | * @param {integer} codeOffset the code offset |
163 | 163 | * @param {integer} length the length of code to copy |
164 | 164 | */ |
165 | | - codeCopy (offset, codeOffset, length) { |
| 165 | + codeCopy (resultOffset, codeOffset, length) { |
166 | 166 | const code = new Uint8Array(this.environment.code, codeOffset, length) |
167 | | - this.setMemory(offset, length, code) |
| 167 | + this.setMemory(resultOffset, length, code) |
168 | 168 | } |
169 | 169 | |
170 | 170 | |
171 | 171 | * Get size of an account’s code. |
172 | 172 | * @param {integer} addressOffset the offset in memory to load the address from |
173 | 173 | * @return {integer} |
174 | 174 | */ |
175 | | - extCodeSize (addressOffset) { |
| 175 | + getExternalCodeSize (addressOffset) { |
176 | 176 | const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
177 | 177 | const code = this.environment.getCode(address) |
178 | 178 | return code.length |
179 | 179 | } |
180 | 180 | |
181 | 181 | |
182 | 182 | * Copys the code of an account to memory. |
183 | 183 | * @param {integer} addressOffset the memory offset of the address |
184 | | - * @param {integer} offset the memory offset |
| 184 | + * @param {integer} resultOffset the memory offset |
185 | 185 | * @param {integer} codeOffset the code offset |
186 | 186 | * @param {integer} length the length of code to copy |
187 | 187 | */ |
188 | | - extCodeCopy (addressOffset, offset, codeOffset, length) { |
| 188 | + externalCodeCopy (addressOffset, resultOffset, codeOffset, length) { |
189 | 189 | const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
190 | 190 | let code = this.environment.getCode(address) |
191 | 191 | code = new Uint8Array(code, codeOffset, length) |
192 | | - this.setMemory(offset, length, code) |
| 192 | + this.setMemory(resultOffset, length, code) |
193 | 193 | } |
194 | 194 | |
195 | 195 | |
196 | 196 | * Gets price of gas in current environment. |
197 | 197 | * @return {integer} |
198 | 198 | */ |
199 | | - gasPrice () { |
| 199 | + getTxGasPrice () { |
200 | 200 | return this.environment.gasPrice |
201 | 201 | } |
202 | 202 | |
203 | 203 | |
204 | 204 | * Gets the hash of one of the 256 most recent complete blocks. |
205 | 205 | * @param {integer} number which block to load |
206 | 206 | * @param {integer} offset the offset to load the hash into |
207 | 207 | */ |
208 | | - blockHash (number, offset) { |
| 208 | + getBlockHash (number, offset) { |
209 | 209 | const diff = this.environment.number - number |
210 | 210 | let hash |
211 | 211 | |
212 | 212 | if (diff > 256 || diff <= 0) { |
220 | 220 | |
221 | 221 | * Gets the block’s beneficiary address and loads into memory. |
222 | 222 | * @param offset |
223 | 223 | */ |
224 | | - coinbase (offset) { |
| 224 | + getBlockCoinbase (offset) { |
225 | 225 | this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.coinbase.toBuffer()) |
226 | 226 | } |
227 | 227 | |
228 | 228 | |
229 | 229 | * Get the block’s timestamp. |
230 | 230 | * @return {integer} |
231 | 231 | */ |
232 | | - timestamp () { |
| 232 | + getBlockTimestamp () { |
233 | 233 | return this.environment.timestamp |
234 | 234 | } |
235 | 235 | |
236 | 236 | |
237 | 237 | * Get the block’s number. |
238 | 238 | * @return {integer} |
239 | 239 | */ |
240 | | - number () { |
| 240 | + getBlockNumber () { |
241 | 241 | return this.environment.number |
242 | 242 | } |
243 | 243 | |
244 | 244 | |
245 | 245 | * Get the block’s difficulty. |
246 | 246 | * @return {integer} |
247 | 247 | */ |
248 | | - difficulty () { |
| 248 | + getBlockDifficulty () { |
249 | 249 | return this.environment.difficulty |
250 | 250 | } |
251 | 251 | |
252 | 252 | |
253 | 253 | * Get the block’s gas limit. |
254 | 254 | * @return {integer} |
255 | 255 | */ |
256 | | - gasLimit () { |
| 256 | + getBlockGasLimit () { |
257 | 257 | return this.environment.gasLimit |
258 | 258 | } |
259 | 259 | |
260 | 260 | |
295 | 295 | * @param {integer} gas |
296 | 296 | * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not |
297 | 297 | * TODO: add proper gas counting |
298 | 298 | */ |
299 | | - call (addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength, gas) { |
| 299 | + call (gas, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) { |
300 | 300 | if (gas === undefined) { |
301 | 301 | gas = this.gasLeft() |
302 | 302 | } |
303 | 303 | |
310 | 310 | return errorCode |
311 | 311 | } |
312 | 312 | |
313 | 313 | |
| 314 | + * Message-call into this account with an alternative account’s code. |
| 315 | + * @param {integer} addressOffset the offset to load the address path from |
| 316 | + * @param {integer} valueOffset the offset to load the value from |
| 317 | + * @param {integer} dataOffset the offset to load data from |
| 318 | + * @param {integer} dataLength the length of data |
| 319 | + * @param {integer} resultOffset the offset to store the result data at |
| 320 | + * @param {integer} resultLength |
| 321 | + * @param {integer} gas |
| 322 | + * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not |
| 323 | + * TODO: add proper gas counting |
| 324 | + */ |
| 325 | + callCode (gas, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) { |
| 326 | + |
| 327 | + const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
| 328 | + const value = new U256(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES)) |
| 329 | + const data = this.getMemory(dataOffset, dataLength) |
| 330 | + |
| 331 | + const [result, errorCode] = this.environment.callCode(gas, address, value, data) |
| 332 | + this.setMemory(resultOffset, resultLength, result) |
| 333 | + return errorCode |
| 334 | + } |
| 335 | + |
| 336 | + |
314 | 337 | * Message-call into this account with an alternative account’s code, but |
315 | 338 | * persisting the current values for sender and value. |
316 | 339 | * @param {integer} gas |
317 | 340 | * @param {integer} addressOffset the offset to load the address path from |
335 | 358 | * from Memory |
336 | 359 | * @param {interger} pathOffest the memory offset to load the the path from |
337 | 360 | * @param {interger} valueOffset the memory offset to load the value from |
338 | 361 | */ |
339 | | - sstore (pathOffset, valueOffset) { |
| 362 | + storageStore (pathOffset, valueOffset) { |
340 | 363 | const path = new Buffer(this.getMemory(pathOffset, 32)).toString('hex') |
341 | 364 | |
342 | 365 | const value = this.getMemory(valueOffset, 32).slice(0) |
343 | 366 | const oldValue = this.environment.state.get(path) |
361 | 384 | * reterives a value at a given path in long term storage |
362 | 385 | * @param {interger} pathOffest the memory offset to load the the path from |
363 | 386 | * @param {interger} resultOffset the memory offset to load the value from |
364 | 387 | */ |
365 | | - sload (pathOffset, resultOffset) { |
| 388 | + storageLoad (pathOffset, resultOffset) { |
366 | 389 | const path = new Buffer(this.getMemory(pathOffset, 32)).toString('hex') |
367 | 390 | const result = this.environment.state.get(path) |
368 | 391 | this.setMemory(resultOffset, 32, result) |
369 | 392 | } |
381 | 404 | * Halt execution and register account for later deletion giving the remaining |
382 | 405 | * balance to an address path |
383 | 406 | * @param {integer} offset the offset to load the address from |
384 | 407 | */ |
385 | | - suicide (addressOffset) { |
| 408 | + selfDestruct (addressOffset) { |
386 | 409 | this.environment.suicideAddress = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
387 | 410 | } |
388 | 411 | |
389 | 412 | getMemory (offset, length) { |