Commit fe80685fa3f7f75e6bcb2fb0e4278298822b4a3e
Merge pull request #39 from ewasm/endian-cleanup
Endian cleanupwanderer authored on 8/25/2016, 2:12:37 AM
GitHub committed on 8/25/2016, 2:12:37 AM
Parent: aa7f348ee71a97e479efcc2d058b302bcf683a0d
Parent: a2d4372bc21bc705e7d5c559b431d90588315e10
Files changed
address.js | changed |
block.js | changed |
debugInterface.js | changed |
environment.js | changed |
index.js | changed |
interface.js | changed |
tests/interface/address.wast | changed |
tests/interface/balance.wast | changed |
tests/interface/caller.wast | changed |
tests/interface/coinbase.wast | changed |
tests/interface/origin.wast | changed |
transaction.js | changed |
u256.js | changed |
address.js | ||
---|---|---|
@@ -1,41 +1,38 @@ | ||
1 | -const ethUtil = require('ethereumjs-util') | |
1 | +const BN = require('bn.js') | |
2 | +const U256 = require('./u256.js') | |
2 | 3 | |
3 | -module.exports = class Address { | |
4 | +module.exports = class Address extends U256 { | |
4 | 5 | constructor (value) { |
5 | - // Special case: duplicate | |
6 | - if (value instanceof Address) { | |
7 | - this._value = new Buffer(value._value) | |
8 | - return | |
6 | + super(value) | |
7 | + if (this._value.byteLength() > 20) { | |
8 | + throw new Error('Invalid address length: ' + this._value.byteLength() + ' for ' + value) | |
9 | 9 | } |
10 | + } | |
10 | 11 | |
11 | - if (value instanceof Uint8Array) { | |
12 | - this._value = new Buffer(value) | |
13 | - } else if (typeof value !== 'string') { | |
14 | - throw new Error('Invalid input to address') | |
15 | - } else if (!ethUtil.isHexPrefixed(value)) { | |
16 | - throw new Error('Invalid address format') | |
17 | - } else { | |
18 | - this._value = new Buffer(ethUtil.stripHexPrefix(value), 'hex') | |
19 | - } | |
12 | + // This assumes Uint8Array in LSB (WASM code) | |
13 | + static fromMemory (value) { | |
14 | + return new Address(new BN(value, 16, 'le')) | |
15 | + } | |
20 | 16 | |
21 | - if (this._value.length !== 20) { | |
22 | - throw new Error('Invalid address length') | |
23 | - } | |
17 | + // This assumes Uint8Array in LSB (WASM code) | |
18 | + toMemory () { | |
19 | + return this._value.toBuffer('le', 20) | |
24 | 20 | } |
25 | 21 | |
26 | 22 | toBuffer () { |
27 | - return this._value | |
23 | + return super.toBuffer(20) | |
28 | 24 | } |
29 | 25 | |
26 | + // Needs to be displayed as a hex always | |
30 | 27 | toString () { |
31 | - return '0x' + this._value.toString('hex') | |
28 | + return '0x' + this._value.toString('hex', 40) | |
32 | 29 | } |
33 | 30 | |
34 | 31 | isZero () { |
35 | - return this._value.equals(ethUtil.zeros(20)) | |
32 | + return this._value.isZero() | |
36 | 33 | } |
37 | 34 | |
38 | 35 | equals (address) { |
39 | - return this._value.toString('hex') === address.toBuffer().toString('hex') | |
36 | + return this.toString() === address.toString() | |
40 | 37 | } |
41 | 38 | } |
block.js | ||
---|---|---|
@@ -25,7 +25,7 @@ | ||
25 | 25 | return ethUtil.bufferToInt(this.header.timestamp) |
26 | 26 | } |
27 | 27 | |
28 | 28 | get coinbase () { |
29 | - return new Address(this.header.coinbase) | |
29 | + return new Address('0x' + this.header.coinbase.toString('hex')) | |
30 | 30 | } |
31 | 31 | } |
debugInterface.js | ||
---|---|---|
@@ -34,15 +34,15 @@ | ||
34 | 34 | } |
35 | 35 | console.error(`op: ${opcode.name} gas: ${this.environment.gasLeft}`) |
36 | 36 | console.log('-------------stack--------------') |
37 | 37 | for (let i = sp; i >= 0; i -= 32) { |
38 | - console.log(`${(sp - i) / 32} ${this.getMemoryBuffer(i).toString('hex')}`) | |
38 | + console.log(`${(sp - i) / 32} ${this.getMemoryBuffer(i).reverse().toString('hex')}`) | |
39 | 39 | } |
40 | 40 | return sp |
41 | 41 | }.bind(this) |
42 | 42 | } |
43 | 43 | } |
44 | 44 | |
45 | 45 | getMemoryBuffer (offset) { |
46 | - return new Buffer(this.module.exports.memory.slice(offset, offset + 32)).reverse() | |
46 | + return new Buffer(this.module.exports.memory.slice(offset, offset + 32)) | |
47 | 47 | } |
48 | 48 | } |
environment.js | ||
---|---|---|
@@ -37,9 +37,8 @@ | ||
37 | 37 | account.set('nonce', trie.nonce || new U256(0)) |
38 | 38 | account.set('balance', trie.balance || new U256(0)) |
39 | 39 | account.set('code', trie.code || new Uint8Array()) |
40 | 40 | account.set('storage', trie.storage || new Map()) |
41 | - | |
42 | 41 | this.state.set(address.toString(), account) |
43 | 42 | } |
44 | 43 | |
45 | 44 | getBalance (address) { |
index.js | ||
---|---|---|
@@ -17,11 +17,9 @@ | ||
17 | 17 | |
18 | 18 | // The Kernel Stores all of its state in the Environment. The Interface is used |
19 | 19 | // to by the VM to retrive infromation from the Environment. |
20 | 20 | const Environment = require('./environment.js') |
21 | - | |
22 | 21 | const DebugInterface = require('./debugInterface.js') |
23 | - | |
24 | 22 | const Address = require('./address.js') |
25 | 23 | const U256 = require('./u256.js') |
26 | 24 | const Utils = require('./utils.js') |
27 | 25 | const Transaction = require('./transaction.js') |
interface.js | ||
---|---|---|
@@ -86,9 +86,9 @@ | ||
86 | 86 | */ |
87 | 87 | getAddress (offset) { |
88 | 88 | this.takeGas(2) |
89 | 89 | |
90 | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toBuffer()) | |
90 | + this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toMemory()) | |
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
94 | 94 | * Gets balance of the given account and loads it into memory at the given |
@@ -98,12 +98,12 @@ | ||
98 | 98 | */ |
99 | 99 | getBalance (addressOffset, offset) { |
100 | 100 | this.takeGas(20) |
101 | 101 | |
102 | - const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
102 | + const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
103 | 103 | // call the parent contract and ask for the balance of one of its child contracts |
104 | 104 | const balance = this.environment.getBalance(address) |
105 | - this.setMemory(offset, constants.BALANCE_SIZE_BYTES, balance.toBuffer(constants.BALANCE_SIZE_BYTES)) | |
105 | + this.setMemory(offset, constants.BALANCE_SIZE_BYTES, balance.toMemory(constants.BALANCE_SIZE_BYTES)) | |
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
109 | 109 | * Gets the execution's origination address and loads it into memory at the |
@@ -113,9 +113,9 @@ | ||
113 | 113 | */ |
114 | 114 | getTxOrigin (offset) { |
115 | 115 | this.takeGas(2) |
116 | 116 | |
117 | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toBuffer()) | |
117 | + this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toMemory()) | |
118 | 118 | } |
119 | 119 | |
120 | 120 | /** |
121 | 121 | * Gets caller address and loads it into memory at the given offset. This is |
@@ -124,9 +124,9 @@ | ||
124 | 124 | */ |
125 | 125 | getCaller (offset) { |
126 | 126 | this.takeGas(2) |
127 | 127 | |
128 | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toBuffer()) | |
128 | + this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toMemory()) | |
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |
132 | 132 | * Gets the deposited value by the instruction/transaction responsible for |
@@ -135,9 +135,9 @@ | ||
135 | 135 | */ |
136 | 136 | getCallValue (offset) { |
137 | 137 | this.takeGas(2) |
138 | 138 | |
139 | - this.setMemory(offset, constants.BALANCE_SIZE_BYTES, this.environment.callValue.toBuffer(constants.BALANCE_SIZE_BYTES)) | |
139 | + this.setMemory(offset, constants.BALANCE_SIZE_BYTES, this.environment.callValue.toMemory(constants.BALANCE_SIZE_BYTES)) | |
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
143 | 143 | * Get size of input data in current environment. This pertains to the input |
@@ -168,9 +168,8 @@ | ||
168 | 168 | * Copys the input data in current environment to memory. This pertains to |
169 | 169 | * the input data passed with the message call instruction or transaction. |
170 | 170 | * @param {integer} offset the offset in memory to load into |
171 | 171 | * @param {integer} dataOffset the offset in the input data |
172 | - * @param {integer} length the length of data to copy | |
173 | 172 | */ |
174 | 173 | callDataCopy256 (offset, dataOffset) { |
175 | 174 | this.takeGas(3) |
176 | 175 | const callData = this.environment.callData.slice(dataOffset, dataOffset + 32) |
@@ -207,9 +206,9 @@ | ||
207 | 206 | */ |
208 | 207 | getExternalCodeSize (addressOffset) { |
209 | 208 | this.takeGas(20) |
210 | 209 | |
211 | - const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
210 | + const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
212 | 211 | const code = this.environment.getCode(address) |
213 | 212 | return code.length |
214 | 213 | } |
215 | 214 | |
@@ -222,9 +221,9 @@ | ||
222 | 221 | */ |
223 | 222 | externalCodeCopy (addressOffset, resultOffset, codeOffset, length) { |
224 | 223 | this.takeGas(20 + ((length / 32) * 3)) |
225 | 224 | |
226 | - const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
225 | + const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
227 | 226 | let code = this.environment.getCode(address) |
228 | 227 | code = new Uint8Array(code, codeOffset, length) |
229 | 228 | this.setMemory(resultOffset, length, code) |
230 | 229 | } |
@@ -254,9 +253,9 @@ | ||
254 | 253 | hash = new U256(0) |
255 | 254 | } else { |
256 | 255 | hash = new U256(this.environment.getBlockHash(number)) |
257 | 256 | } |
258 | - this.setMemory(offset, 32, hash.toBuffer()) | |
257 | + this.setMemory(offset, 32, hash.toMemory()) | |
259 | 258 | } |
260 | 259 | |
261 | 260 | /** |
262 | 261 | * Gets the block’s beneficiary address and loads into memory. |
@@ -264,9 +263,9 @@ | ||
264 | 263 | */ |
265 | 264 | getBlockCoinbase (offset) { |
266 | 265 | this.takeGas(2) |
267 | 266 | |
268 | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.block.coinbase.toBuffer()) | |
267 | + this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.block.coinbase.toMemory()) | |
269 | 268 | } |
270 | 269 | |
271 | 270 | /** |
272 | 271 | * Get the block’s timestamp. |
@@ -294,9 +293,9 @@ | ||
294 | 293 | */ |
295 | 294 | getBlockDifficulty (offset) { |
296 | 295 | this.takeGas(2) |
297 | 296 | |
298 | - this.setMemory(offset, 32, this.environment.block.difficulty.toBuffer()) | |
297 | + this.setMemory(offset, 32, this.environment.block.difficulty.toMemory()) | |
299 | 298 | } |
300 | 299 | |
301 | 300 | /** |
302 | 301 | * Get the block’s gas limit. |
@@ -333,9 +332,9 @@ | ||
333 | 332 | */ |
334 | 333 | create (valueOffset, dataOffset, length) { |
335 | 334 | this.takeGas(32000) |
336 | 335 | |
337 | - const value = new U256(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES)) | |
336 | + const value = U256.fromMemory(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES)) | |
338 | 337 | const data = this.getMemory(dataOffset, length) |
339 | 338 | const result = this.environment.create(value, data) |
340 | 339 | return result |
341 | 340 | } |
@@ -355,10 +354,10 @@ | ||
355 | 354 | // FIXME: count properly |
356 | 355 | this.takeGas(40) |
357 | 356 | |
358 | 357 | // Load the params from mem |
359 | - const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
360 | - const value = new U256(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES)) | |
358 | + const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
359 | + const value = U256.fromMemory(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES)) | |
361 | 360 | const data = this.getMemory(dataOffset, dataLength) |
362 | 361 | // Run the call |
363 | 362 | const [result, errorCode] = this.environment.call(gas, address, value, data) |
364 | 363 | this.setMemory(resultOffset, resultLength, result) |
@@ -380,10 +379,10 @@ | ||
380 | 379 | // FIXME: count properly |
381 | 380 | this.takeGas(40) |
382 | 381 | |
383 | 382 | // Load the params from mem |
384 | - const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
385 | - const value = new U256(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES)) | |
383 | + const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
384 | + const value = U256.fromMemory(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES)) | |
386 | 385 | const data = this.getMemory(dataOffset, dataLength) |
387 | 386 | // Run the call |
388 | 387 | const [result, errorCode] = this.environment.callCode(gas, address, value, data) |
389 | 388 | this.setMemory(resultOffset, resultLength, result) |
@@ -406,9 +405,9 @@ | ||
406 | 405 | // FIXME: count properly |
407 | 406 | this.takeGas(40) |
408 | 407 | |
409 | 408 | const data = this.getMemory(dataOffset, dataLength) |
410 | - const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
409 | + const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
411 | 410 | const [result, errorCode] = this.environment.callDelegate(gas, address, data) |
412 | 411 | this.setMemory(resultOffset, resultLength, result) |
413 | 412 | return errorCode |
414 | 413 | } |
@@ -469,9 +468,9 @@ | ||
469 | 468 | * balance to an address path |
470 | 469 | * @param {integer} offset the offset to load the address from |
471 | 470 | */ |
472 | 471 | selfDestruct (addressOffset) { |
473 | - this.environment.suicideAddress = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
472 | + this.environment.suicideAddress = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
474 | 473 | this.environment.gasRefund += 24000 |
475 | 474 | } |
476 | 475 | |
477 | 476 | getMemory (offset, length) { |
tests/interface/address.wast | ||
---|---|---|
@@ -8,9 +8,9 @@ | ||
8 | 8 | (func |
9 | 9 | (block |
10 | 10 | ;; loads the address into memory |
11 | 11 | (call_import $address (i32.const 0)) |
12 | - (if (i64.eq (i64.load (i32.const 0)) (i64.const 0x72a1048901c1485d)) ;; big enden | |
12 | + (if (i64.eq (i64.load (i32.const 0)) (i64.const 0xbd9c6f4a2d06c47b)) | |
13 | 13 | (return) |
14 | 14 | ) |
15 | 15 | (unreachable) |
16 | 16 | ) |
tests/interface/balance.wast | ||
---|---|---|
@@ -1,7 +1,7 @@ | ||
1 | 1 | ;; address of 5d48c1018904a172886829bbbd9c6f4a2d06c47b has a balance of 0x056bc75e2d63100000 (100 ETH) |
2 | 2 | (module |
3 | - (memory 1 (segment 0 "\5d\48\c1\01\89\04\a1\72\88\68\29\bb\bd\9c\6f\4a\2d\06\c4\7b")) | |
3 | + (memory 1 (segment 0 "\7b\c4\06\2d\4a\6f\9c\bd\bb\29\68\88\72\a1\04\89\01\c1\48\5d")) | |
4 | 4 | (import $balance "ethereum" "getBalance" (param i32 i32)) |
5 | 5 | (export "a" memory) |
6 | 6 | (export "test" 0) |
7 | 7 | (func |
tests/interface/caller.wast | ||
---|---|---|
@@ -8,9 +8,9 @@ | ||
8 | 8 | (func |
9 | 9 | (block |
10 | 10 | ;; loads the caller into memory |
11 | 11 | (call_import $caller (i32.const 0)) |
12 | - (if (i64.eq (i64.load (i32.const 0)) (i64.const 0x72a1048901c1485d)) | |
12 | + (if (i64.eq (i64.load (i32.const 0)) (i64.const 0xbd9c6f4a2d06c47b)) | |
13 | 13 | (return) |
14 | 14 | ) |
15 | 15 | (unreachable) |
16 | 16 | ) |
tests/interface/coinbase.wast | ||
---|---|---|
@@ -8,9 +8,9 @@ | ||
8 | 8 | (func |
9 | 9 | (block |
10 | 10 | ;; loads the coinbase into memory |
11 | 11 | (call_import $coinbase (i32.const 0)) |
12 | - (if (i64.eq (i64.load (i32.const 0)) (i64.const 0x72a1048901c1485d)) ;; big endian | |
12 | + (if (i64.eq (i64.load (i32.const 0)) (i64.const 0xbd9c6f4a2d06c47b)) | |
13 | 13 | (return) |
14 | 14 | ) |
15 | 15 | (unreachable) |
16 | 16 | ) |
tests/interface/origin.wast | ||
---|---|---|
@@ -8,9 +8,9 @@ | ||
8 | 8 | (func |
9 | 9 | (block |
10 | 10 | ;; loads the address into memory |
11 | 11 | (call_import $origin (i32.const 0)) |
12 | - (if (i64.eq (i64.load (i32.const 0)) (i64.const 0x72a1048901c1485d)) | |
12 | + (if (i64.eq (i64.load (i32.const 0)) (i64.const 0xbd9c6f4a2d06c47b)) | |
13 | 13 | (return) |
14 | 14 | ) |
15 | 15 | (unreachable) |
16 | 16 | ) |
transaction.js | ||
---|---|---|
@@ -36,16 +36,16 @@ | ||
36 | 36 | return Uint8Array.from(this._tx.data) |
37 | 37 | } |
38 | 38 | |
39 | 39 | get from () { |
40 | - return new Address(this._tx.getSenderAddress()) | |
40 | + return new Address('0x' + this._tx.getSenderAddress().toString('hex')) | |
41 | 41 | } |
42 | 42 | |
43 | 43 | get to () { |
44 | 44 | if (this._tx.to.length === 0) { |
45 | 45 | return new Address('0x0000000000000000000000000000000000000000') |
46 | 46 | } |
47 | - return new Address(this._tx.to) | |
47 | + return new Address('0x' + this._tx.to.toString('hex')) | |
48 | 48 | } |
49 | 49 | |
50 | 50 | get isSend () { |
51 | 51 | } |
u256.js | ||
---|---|---|
@@ -1,19 +1,27 @@ | ||
1 | 1 | const BN = require('bn.js') |
2 | 2 | const ethUtil = require('ethereumjs-util') |
3 | 3 | |
4 | -module.exports = class U256 { | |
4 | +const U256 = module.exports = class U256 { | |
5 | 5 | constructor (value) { |
6 | - // This is the case when data is copied from WASM | |
7 | - if (value instanceof Uint8Array) { | |
8 | - this._value = new BN(value, 16, 'le') | |
9 | - } else if ((typeof value === 'string') && ethUtil.isHexPrefixed(value)) { | |
6 | + // bn.js still doesn't support hex prefixes... | |
7 | + if ((typeof value === 'string') && ethUtil.isHexPrefixed(value)) { | |
10 | 8 | this._value = new BN(ethUtil.stripHexPrefix(value), 16) |
11 | 9 | } else { |
12 | 10 | this._value = new BN(value, 10) |
13 | 11 | } |
14 | 12 | } |
15 | 13 | |
14 | + // This assumes Uint8Array in LSB (WASM code) | |
15 | + static fromMemory (value) { | |
16 | + return new U256(new BN(value, 16, 'le')) | |
17 | + } | |
18 | + | |
19 | + // This assumes Uint8Array in LSB (WASM code) | |
20 | + toMemory (width) { | |
21 | + return this._value.toBuffer('le', width || 32) | |
22 | + } | |
23 | + | |
16 | 24 | toString (radix = 10) { |
17 | 25 | if (radix === 16) { |
18 | 26 | return '0x' + this._value.toString(16) |
19 | 27 | } |
@@ -23,9 +31,9 @@ | ||
23 | 31 | toBuffer (width) { |
24 | 32 | if (width <= 0 || width > 32) { |
25 | 33 | throw new Error('Invalid U256 width') |
26 | 34 | } |
27 | - return this._value.toBuffer('le', width || 32) | |
35 | + return this._value.toBuffer('be', width || 32) | |
28 | 36 | } |
29 | 37 | |
30 | 38 | sub (u256) { |
31 | 39 | return new U256(this._value.sub(u256._value)) |
Built with git-ssb-web