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