Files: 7ffc2533d01f9b747d1e93544fa339e5573f9b12 / environment.js
2302 bytesRaw
1 | const constants = require('./constants.js') |
2 | |
3 | module.exports = class Environment { |
4 | constructor (data) { |
5 | const defaults = { |
6 | // gas tank |
7 | gasPrice: 0, |
8 | gasLimit: 1000000, // The gas limit for the block |
9 | gasRefund: 0, |
10 | // call infromation |
11 | address: new Uint8Array(constants.ADDRESS_SIZE_BYTES), |
12 | origin: new Uint8Array(constants.ADDRESS_SIZE_BYTES), |
13 | coinbase: new Uint8Array(constants.ADDRESS_SIZE_BYTES), |
14 | difficulty: new Uint8Array(20), |
15 | caller: new Uint8Array(constants.ADDRESS_SIZE_BYTES), |
16 | callValue: new Uint8Array(constants.BALANCE_SIZE_BYTES), |
17 | callData: new ArrayBuffer(), |
18 | // the ROM |
19 | code: new ArrayBuffer(), // the current running code |
20 | // output calls |
21 | logs: [], |
22 | selfDestructAddress: new ArrayBuffer(), |
23 | // more output calls |
24 | returnValue: new ArrayBuffer() |
25 | } |
26 | |
27 | const self = this |
28 | this.state = new Map() |
29 | |
30 | if (data) { |
31 | data = JSON.parse(data) |
32 | } else { |
33 | data = {} |
34 | } |
35 | |
36 | Object.assign(this, defaults, data) |
37 | if (data.accounts) { |
38 | data.accounts.forEach((account) => { |
39 | self.state.set(new Uint8Array(account[0]).toString(), account[1]) |
40 | }) |
41 | } |
42 | |
43 | if (data.address) { |
44 | this.address = new Uint8Array(data.address) |
45 | } |
46 | |
47 | if (data.origin) { |
48 | this.origin = new Uint8Array(data.origin) |
49 | } |
50 | |
51 | if (data.caller) { |
52 | this.caller = new Uint8Array(data.caller) |
53 | } |
54 | |
55 | if (data.callValue) { |
56 | this.callValue = new Uint8Array(data.callValue) |
57 | } |
58 | |
59 | if (data.callData) { |
60 | this.callData = hexStr2arrayBuf(data.callData) |
61 | } |
62 | } |
63 | |
64 | getBalance (address) { |
65 | return this.state.get(address.toString()).balance |
66 | } |
67 | |
68 | getCode (address) { |
69 | // STUB |
70 | } |
71 | |
72 | getBlockHash (height) { |
73 | // STUB |
74 | } |
75 | |
76 | // kernal |
77 | create (code, value) { |
78 | // STUB |
79 | } |
80 | |
81 | call (gas, address, value, data) { |
82 | // STUB |
83 | return // result |
84 | } |
85 | |
86 | delegateCall (gas, address, data) { |
87 | // STUB |
88 | return // result |
89 | } |
90 | } |
91 | |
92 | function hexStr2arrayBuf (string) { |
93 | const ab = new ArrayBuffer(string.length / 2) |
94 | const view = new Uint8Array(ab) |
95 | string = [...string] |
96 | let temp = '' |
97 | string.forEach((el, i) => { |
98 | temp += el |
99 | if (i % 2) { |
100 | view[(i + 1) / 2 - 1] = parseInt(temp, 16) |
101 | temp = '' |
102 | } |
103 | }) |
104 | return ab |
105 | } |
106 |
Built with git-ssb-web