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