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