Commit 9c2ae4c92689bb67810feb97c6cc2916824a24cb
use fixed-bn
wanderer committed on 3/21/2017, 11:49:01 PMParent: 530ddd254210fc24ecf91434d349a5162a554763
Files changed
EVMinterface.js | changed |
deps/block.js | changed |
deps/address.js | deleted |
deps/u256.js | deleted |
fakeBlockChain.js | changed |
message.js | changed |
package.json | changed |
tests/interfaceRunner.js | changed |
EVMinterface.js | ||
---|---|---|
@@ -4,9 +4,10 @@ | ||
4 | 4 | */ |
5 | 5 | const fs = require('fs') |
6 | 6 | const ethUtil = require('ethereumjs-util') |
7 | 7 | const Vertex = require('merkle-trie') |
8 | -const U256 = require('./deps/u256.js') | |
8 | +const U256 = require('fixed-bn.js').U256 | |
9 | +const U128 = require('fixed-bn.js').U128 | |
9 | 10 | const Message = require('./message.js') |
10 | 11 | const common = require('./common.js') |
11 | 12 | |
12 | 13 | const U128_SIZE_BYTES = 16 |
@@ -45,12 +46,8 @@ | ||
45 | 46 | static get hostContainer () { |
46 | 47 | return 'wasm' |
47 | 48 | } |
48 | 49 | |
49 | - setModule (mod) { | |
50 | - this.module = mod | |
51 | - } | |
52 | - | |
53 | 50 | /** |
54 | 51 | * Subtracts an amount to the gas counter |
55 | 52 | * @param {integer} amount the amount to subtract to the gas counter |
56 | 53 | */ |
@@ -104,9 +101,9 @@ | ||
104 | 101 | })) |
105 | 102 | .catch(() => new Buffer([])) |
106 | 103 | |
107 | 104 | this.vm.pushOpsQueue(opPromise, cbIndex, balance => { |
108 | - this.setMemory(offset, U128_SIZE_BYTES, new U256(balance).toMemory(U128_SIZE_BYTES)) | |
105 | + this.setMemory(offset, U128_SIZE_BYTES, new U128(balance).toBuffer()) | |
109 | 106 | }) |
110 | 107 | } |
111 | 108 | |
112 | 109 | /** |
@@ -140,9 +137,9 @@ | ||
140 | 137 | */ |
141 | 138 | getCallValue (offset) { |
142 | 139 | this.takeGas(2) |
143 | 140 | |
144 | - this.setMemory(offset, U128_SIZE_BYTES, this.message.value.toMemory(U128_SIZE_BYTES)) | |
141 | + this.setMemory(offset, U128_SIZE_BYTES, this.message.value.toBuffer()) | |
145 | 142 | } |
146 | 143 | |
147 | 144 | /** |
148 | 145 | * Get size of input data in current environment. This pertains to the input |
@@ -287,9 +284,9 @@ | ||
287 | 284 | } |
288 | 285 | |
289 | 286 | // wait for all the prevouse async ops to finish before running the callback |
290 | 287 | this.vm.pushOpsQueue(opPromise, cbOffset, hash => { |
291 | - this.setMemory(offset, U256_SIZE_BYTES, hash.toMemory()) | |
288 | + this.setMemory(offset, U256_SIZE_BYTES, hash.toBuffer()) | |
292 | 289 | }) |
293 | 290 | } |
294 | 291 | |
295 | 292 | /** |
@@ -328,9 +325,9 @@ | ||
328 | 325 | */ |
329 | 326 | getBlockDifficulty (offset) { |
330 | 327 | this.takeGas(2) |
331 | 328 | |
332 | - this.setMemory(offset, U256_SIZE_BYTES, this.message.block.difficulty.toMemory()) | |
329 | + this.setMemory(offset, U256_SIZE_BYTES, this.message.block.difficulty.toBuffer()) | |
333 | 330 | } |
334 | 331 | |
335 | 332 | /** |
336 | 333 | * Get the block’s gas limit. |
@@ -358,21 +355,21 @@ | ||
358 | 355 | const data = length ? this.getMemory(dataOffset, length).slice(0) : new Uint8Array([]) |
359 | 356 | const topics = [] |
360 | 357 | |
361 | 358 | if (numberOfTopics > 0) { |
362 | - topics.push(U256.fromMemory(this.getMemory(topic1, U256_SIZE_BYTES))) | |
359 | + topics.push(U256.fromBuffer(this.getMemory(topic1, U256_SIZE_BYTES))) | |
363 | 360 | } |
364 | 361 | |
365 | 362 | if (numberOfTopics > 1) { |
366 | - topics.push(U256.fromMemory(this.getMemory(topic2, U256_SIZE_BYTES))) | |
363 | + topics.push(U256.fromBuffer(this.getMemory(topic2, U256_SIZE_BYTES))) | |
367 | 364 | } |
368 | 365 | |
369 | 366 | if (numberOfTopics > 2) { |
370 | - topics.push(U256.fromMemory(this.getMemory(topic3, U256_SIZE_BYTES))) | |
367 | + topics.push(U256.fromBuffer(this.getMemory(topic3, U256_SIZE_BYTES))) | |
371 | 368 | } |
372 | 369 | |
373 | 370 | if (numberOfTopics > 3) { |
374 | - topics.push(U256.fromMemory(this.getMemory(topic4, U256_SIZE_BYTES))) | |
371 | + topics.push(U256.fromBuffer(this.getMemory(topic4, U256_SIZE_BYTES))) | |
375 | 372 | } |
376 | 373 | |
377 | 374 | this.kernel.sendMessage([this.kernel.root, 'logs'], new Message({ |
378 | 375 | data: data, |
@@ -390,9 +387,9 @@ | ||
390 | 387 | */ |
391 | 388 | create (valueOffset, dataOffset, length, resultOffset, cbIndex) { |
392 | 389 | this.takeGas(32000) |
393 | 390 | |
394 | - const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES)) | |
391 | + const value = U256.fromBuffer(this.getMemory(valueOffset, U128_SIZE_BYTES)) | |
395 | 392 | // if (length) { |
396 | 393 | // const code = this.getMemory(dataOffset, length).slice(0) |
397 | 394 | // } |
398 | 395 |
deps/block.js | ||
---|---|---|
@@ -1,8 +1,8 @@ | ||
1 | -const Address = require('./address.js') | |
1 | +const Address = require('fixed-bn.js').Address | |
2 | +const U256 = require('fixed-bn.js').U256 | |
2 | 3 | const ethUtil = require('ethereumjs-util') |
3 | 4 | const OldBlock = require('ethereumjs-block') |
4 | -const U256 = require('./u256.js') | |
5 | 5 | |
6 | 6 | module.exports = class Block extends OldBlock { |
7 | 7 | get number () { |
8 | 8 | return ethUtil.bufferToInt(this.header.number) |
deps/address.js | ||
---|---|---|
@@ -1,46 +1,0 @@ | ||
1 | -const BN = require('bn.js') | |
2 | -const U256 = require('./u256.js') | |
3 | - | |
4 | -module.exports = class Address extends U256 { | |
5 | - constructor (value) { | |
6 | - super(value) | |
7 | - if (this._value.byteLength() > 20) { | |
8 | - throw new Error('Invalid address length: ' + this._value.byteLength() + ' for ' + value) | |
9 | - } | |
10 | - } | |
11 | - | |
12 | - // This assumes Uint8Array in LSB (WASM code) | |
13 | - static fromMemory (value) { | |
14 | - return new Address(new BN(value, 16, 'be')) | |
15 | - } | |
16 | - | |
17 | - // This assumes Uint8Array in LSB (WASM code) | |
18 | - toMemory () { | |
19 | - return this._value.toBuffer('be', 20) | |
20 | - } | |
21 | - | |
22 | - toBuffer () { | |
23 | - return super.toBuffer(20) | |
24 | - } | |
25 | - | |
26 | - toArray () { | |
27 | - return [...this.toBuffer()] | |
28 | - } | |
29 | - | |
30 | - // Needs to be displayed as a hex always | |
31 | - toString () { | |
32 | - return '0x' + this._value.toString('hex', 40) | |
33 | - } | |
34 | - | |
35 | - static zero () { | |
36 | - return new Address('0x0000000000000000000000000000000000000000') | |
37 | - } | |
38 | - | |
39 | - isZero () { | |
40 | - return this._value.isZero() | |
41 | - } | |
42 | - | |
43 | - equals (address) { | |
44 | - return this.toString() === address.toString() | |
45 | - } | |
46 | -} |
deps/u256.js | ||
---|---|---|
@@ -1,69 +1,0 @@ | ||
1 | -const BN = require('bn.js') | |
2 | -const ethUtil = require('ethereumjs-util') | |
3 | - | |
4 | -module.exports = class U256 { | |
5 | - constructor (value) { | |
6 | - // bn.js still doesn't support hex prefixes... | |
7 | - if ((typeof value === 'string') && ethUtil.isHexPrefixed(value)) { | |
8 | - this._value = new BN(ethUtil.stripHexPrefix(value), 16) | |
9 | - } else { | |
10 | - this._value = new BN(value, 10) | |
11 | - } | |
12 | - } | |
13 | - | |
14 | - // This assumes Uint8Array in LSB (WASM code) | |
15 | - static fromMemory (value) { | |
16 | - return new U256(new BN(value, 16, 'be')) | |
17 | - } | |
18 | - | |
19 | - // This assumes Uint8Array in LSB (WASM code) | |
20 | - toMemory (width) { | |
21 | - return this._value.toBuffer('be', width || 32) | |
22 | - } | |
23 | - | |
24 | - toString (radix = 10) { | |
25 | - if (radix === 16) { | |
26 | - return '0x' + this._value.toString(16) | |
27 | - } | |
28 | - return this._value.toString(radix) | |
29 | - } | |
30 | - | |
31 | - toBuffer (width = 32) { | |
32 | - if (width <= 0 || width > 32) { | |
33 | - throw new Error('Invalid U256 width') | |
34 | - } | |
35 | - return this._value.toBuffer('be', width) | |
36 | - } | |
37 | - | |
38 | - toArray () { | |
39 | - return [...this.toBuffer()] | |
40 | - } | |
41 | - | |
42 | - isZero () { | |
43 | - return this._value.isZero() | |
44 | - } | |
45 | - | |
46 | - sub (u256) { | |
47 | - return new U256(this._value.sub(u256._value)) | |
48 | - } | |
49 | - | |
50 | - add (u256) { | |
51 | - return new U256(this._value.add(u256._value)) | |
52 | - } | |
53 | - | |
54 | - mul (u256) { | |
55 | - return new U256(this._value.mul(u256._value)) | |
56 | - } | |
57 | - | |
58 | - div (u256) { | |
59 | - return new U256(this._value.div(u256._value)) | |
60 | - } | |
61 | - | |
62 | - lt (u256) { | |
63 | - return this._value.lt(u256._value) | |
64 | - } | |
65 | - | |
66 | - gt (u256) { | |
67 | - return this._value.gt(u256._value) | |
68 | - } | |
69 | -} |
fakeBlockChain.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | 1 | const utils = require('ethereumjs-util') |
2 | -const U256 = require('./deps/u256.js') | |
2 | +const U256 = require('fixed-bn.js').U256 | |
3 | 3 | |
4 | 4 | module.exports = { |
5 | 5 | getBlock: (n) => { |
6 | 6 | const hash = utils.sha3(new Buffer(utils.bufferToInt(n).toString())) |
message.js | ||
---|---|---|
@@ -1,5 +1,5 @@ | ||
1 | -const U256 = require('./deps/u256.js') | |
1 | +const U128 = require('fixed-bn.js').U128 | |
2 | 2 | |
3 | 3 | module.exports = class Message { |
4 | 4 | constructor (opts = {}) { |
5 | 5 | const defaults = { |
@@ -8,10 +8,10 @@ | ||
8 | 8 | from: [], |
9 | 9 | data: new Uint8Array(), |
10 | 10 | atomic: true, |
11 | 11 | // resource info |
12 | - gas: new U256(0), | |
13 | - gasPrices: new U256(0) | |
12 | + gas: new U128(0), | |
13 | + gasPrices: new U128(0) | |
14 | 14 | } |
15 | 15 | Object.assign(this, defaults, opts) |
16 | 16 | this.hops = 0 |
17 | 17 | this._visitedKernels = [] |
package.json | ||
---|---|---|
@@ -44,8 +44,9 @@ | ||
44 | 44 | "bn.js": "^4.11.6", |
45 | 45 | "ethereumjs-block": "^1.5.0", |
46 | 46 | "ethereumjs-tx": "^1.2.5", |
47 | 47 | "ethereumjs-util": "^5.1.0", |
48 | + "fixed-bn.js": "0.0.0", | |
48 | 49 | "merkle-trie": "0.2.0", |
49 | 50 | "prima-wasm-container": "0.0.0" |
50 | 51 | } |
51 | 52 | } |
tests/interfaceRunner.js | ||
---|---|---|
@@ -1,21 +1,21 @@ | ||
1 | 1 | const tape = require('tape') |
2 | 2 | const fs = require('fs') |
3 | -const path = require('path') | |
4 | 3 | const Vertex = require('merkle-trie') |
5 | -const Address = require('../deps/address') | |
6 | 4 | const Block = require('../deps/block') |
7 | -const U256 = require('../deps/u256') | |
5 | +const U128 = require('fixed-bn.js').U128 | |
6 | +const Address = require('fixed-bn.js').Address | |
8 | 7 | // TODO remove fakeblockchain |
9 | 8 | const fakeBlockChain = require('../fakeBlockChain.js') |
10 | 9 | const Hypervisor = require('../hypervisor.js') |
11 | 10 | const Message = require('../message.js') |
12 | 11 | const common = require('../common') |
13 | -const dir = path.join(__dirname, '/interface') | |
14 | 12 | const EVMinterface = require('../EVMinterface.js') |
13 | + | |
14 | +const dir = `${__dirname}/interface` | |
15 | 15 | // get the test names |
16 | 16 | let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast')) |
17 | - | |
17 | +// tests = ['callValue'] | |
18 | 18 | runTests(tests) |
19 | 19 | |
20 | 20 | function runTests (tests) { |
21 | 21 | for (let testName of tests) { |
@@ -57,9 +57,9 @@ | ||
57 | 57 | |
58 | 58 | const message = new Message() |
59 | 59 | message.to = ['accounts', envData.caller, common.PARENT, envData.address, 'code'] |
60 | 60 | message.data = new Buffer(envData.callData.slice(2), 'hex') |
61 | - message.value = new U256(envData.callValue) | |
61 | + message.value = new U128(envData.callValue) | |
62 | 62 | message.gas = envData.gasLeft |
63 | 63 | message.block = block |
64 | 64 | message.blockchain = fakeBlockChain |
65 | 65 |
Built with git-ssb-web