git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 9c2ae4c92689bb67810feb97c6cc2916824a24cb

use fixed-bn

wanderer committed on 3/21/2017, 11:49:01 PM
Parent: 530ddd254210fc24ecf91434d349a5162a554763

Files changed

EVMinterface.jschanged
deps/block.jschanged
deps/address.jsdeleted
deps/u256.jsdeleted
fakeBlockChain.jschanged
message.jschanged
package.jsonchanged
tests/interfaceRunner.jschanged
EVMinterface.jsView
@@ -4,9 +4,10 @@
44 */
55 const fs = require('fs')
66 const ethUtil = require('ethereumjs-util')
77 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
910 const Message = require('./message.js')
1011 const common = require('./common.js')
1112
1213 const U128_SIZE_BYTES = 16
@@ -45,12 +46,8 @@
4546 static get hostContainer () {
4647 return 'wasm'
4748 }
4849
49- setModule (mod) {
50- this.module = mod
51- }
52-
5350 /**
5451 * Subtracts an amount to the gas counter
5552 * @param {integer} amount the amount to subtract to the gas counter
5653 */
@@ -104,9 +101,9 @@
104101 }))
105102 .catch(() => new Buffer([]))
106103
107104 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())
109106 })
110107 }
111108
112109 /**
@@ -140,9 +137,9 @@
140137 */
141138 getCallValue (offset) {
142139 this.takeGas(2)
143140
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())
145142 }
146143
147144 /**
148145 * Get size of input data in current environment. This pertains to the input
@@ -287,9 +284,9 @@
287284 }
288285
289286 // wait for all the prevouse async ops to finish before running the callback
290287 this.vm.pushOpsQueue(opPromise, cbOffset, hash => {
291- this.setMemory(offset, U256_SIZE_BYTES, hash.toMemory())
288+ this.setMemory(offset, U256_SIZE_BYTES, hash.toBuffer())
292289 })
293290 }
294291
295292 /**
@@ -328,9 +325,9 @@
328325 */
329326 getBlockDifficulty (offset) {
330327 this.takeGas(2)
331328
332- this.setMemory(offset, U256_SIZE_BYTES, this.message.block.difficulty.toMemory())
329+ this.setMemory(offset, U256_SIZE_BYTES, this.message.block.difficulty.toBuffer())
333330 }
334331
335332 /**
336333 * Get the block’s gas limit.
@@ -358,21 +355,21 @@
358355 const data = length ? this.getMemory(dataOffset, length).slice(0) : new Uint8Array([])
359356 const topics = []
360357
361358 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)))
363360 }
364361
365362 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)))
367364 }
368365
369366 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)))
371368 }
372369
373370 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)))
375372 }
376373
377374 this.kernel.sendMessage([this.kernel.root, 'logs'], new Message({
378375 data: data,
@@ -390,9 +387,9 @@
390387 */
391388 create (valueOffset, dataOffset, length, resultOffset, cbIndex) {
392389 this.takeGas(32000)
393390
394- const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
391+ const value = U256.fromBuffer(this.getMemory(valueOffset, U128_SIZE_BYTES))
395392 // if (length) {
396393 // const code = this.getMemory(dataOffset, length).slice(0)
397394 // }
398395
deps/block.jsView
@@ -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
23 const ethUtil = require('ethereumjs-util')
34 const OldBlock = require('ethereumjs-block')
4-const U256 = require('./u256.js')
55
66 module.exports = class Block extends OldBlock {
77 get number () {
88 return ethUtil.bufferToInt(this.header.number)
deps/address.jsView
@@ -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.jsView
@@ -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.jsView
@@ -1,6 +1,6 @@
11 const utils = require('ethereumjs-util')
2-const U256 = require('./deps/u256.js')
2+const U256 = require('fixed-bn.js').U256
33
44 module.exports = {
55 getBlock: (n) => {
66 const hash = utils.sha3(new Buffer(utils.bufferToInt(n).toString()))
message.jsView
@@ -1,5 +1,5 @@
1-const U256 = require('./deps/u256.js')
1+const U128 = require('fixed-bn.js').U128
22
33 module.exports = class Message {
44 constructor (opts = {}) {
55 const defaults = {
@@ -8,10 +8,10 @@
88 from: [],
99 data: new Uint8Array(),
1010 atomic: true,
1111 // resource info
12- gas: new U256(0),
13- gasPrices: new U256(0)
12+ gas: new U128(0),
13+ gasPrices: new U128(0)
1414 }
1515 Object.assign(this, defaults, opts)
1616 this.hops = 0
1717 this._visitedKernels = []
package.jsonView
@@ -44,8 +44,9 @@
4444 "bn.js": "^4.11.6",
4545 "ethereumjs-block": "^1.5.0",
4646 "ethereumjs-tx": "^1.2.5",
4747 "ethereumjs-util": "^5.1.0",
48+ "fixed-bn.js": "0.0.0",
4849 "merkle-trie": "0.2.0",
4950 "prima-wasm-container": "0.0.0"
5051 }
5152 }
tests/interfaceRunner.jsView
@@ -1,21 +1,21 @@
11 const tape = require('tape')
22 const fs = require('fs')
3-const path = require('path')
43 const Vertex = require('merkle-trie')
5-const Address = require('../deps/address')
64 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
87 // TODO remove fakeblockchain
98 const fakeBlockChain = require('../fakeBlockChain.js')
109 const Hypervisor = require('../hypervisor.js')
1110 const Message = require('../message.js')
1211 const common = require('../common')
13-const dir = path.join(__dirname, '/interface')
1412 const EVMinterface = require('../EVMinterface.js')
13+
14+const dir = `${__dirname}/interface`
1515 // get the test names
1616 let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
17-
17+// tests = ['callValue']
1818 runTests(tests)
1919
2020 function runTests (tests) {
2121 for (let testName of tests) {
@@ -57,9 +57,9 @@
5757
5858 const message = new Message()
5959 message.to = ['accounts', envData.caller, common.PARENT, envData.address, 'code']
6060 message.data = new Buffer(envData.callData.slice(2), 'hex')
61- message.value = new U256(envData.callValue)
61+ message.value = new U128(envData.callValue)
6262 message.gas = envData.gasLeft
6363 message.block = block
6464 message.blockchain = fakeBlockChain
6565

Built with git-ssb-web