Files: 78a0cadc66db0c286938c220db7175c0da6eee02 / testEnvironment.js
1548 bytesRaw
1 | const Environment = require('./environment.js') |
2 | const U256 = require('./u256.js') |
3 | const Address = require('./address.js') |
4 | const BN = require('bn.js') |
5 | |
6 | module.exports = class TestEnvironment extends Environment { |
7 | constructor (data) { |
8 | super() |
9 | |
10 | if (typeof data === 'string') { |
11 | data = JSON.parse(data) |
12 | } |
13 | |
14 | let self = this |
15 | |
16 | if (data.accounts) { |
17 | data.accounts.forEach((account) => { |
18 | let tmp = account[1] |
19 | self.state.set(new Address(new Uint8Array(account[0])).toString(), { |
20 | balance: new U256(new BN(tmp.balance, 16, 'le')) |
21 | }) |
22 | }) |
23 | } |
24 | |
25 | if (data.address) { |
26 | self.address = new Address(new Uint8Array(data.address)) |
27 | } |
28 | |
29 | if (data.origin) { |
30 | self.origin = new Address(new Uint8Array(data.origin)) |
31 | } |
32 | |
33 | if (data.caller) { |
34 | self.caller = new Address(new Uint8Array(data.caller)) |
35 | } |
36 | |
37 | if (data.coinbase) { |
38 | self.coinbase = new Uint8Array(data.coinbase) |
39 | } |
40 | |
41 | if (data.callValue) { |
42 | self.callValue = new U256(data.callValue) |
43 | } |
44 | |
45 | if (data.callData) { |
46 | self.callData = hexStr2arrayBuf(data.callData) |
47 | } |
48 | |
49 | if (data.gasPrice) { |
50 | self.gasPrice = data.gasPrice |
51 | } |
52 | |
53 | if (data.gasLimit) { |
54 | self.gasLimit = data.gasLimit |
55 | } |
56 | } |
57 | } |
58 | |
59 | function hexStr2arrayBuf (string) { |
60 | const view = new Uint8Array(string.length / 2) |
61 | string = [...string] |
62 | let temp = '' |
63 | string.forEach((el, i) => { |
64 | temp += el |
65 | if (i % 2) { |
66 | view[(i + 1) / 2 - 1] = parseInt(temp, 16) |
67 | temp = '' |
68 | } |
69 | }) |
70 | return view |
71 | } |
72 |
Built with git-ssb-web