git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 1fff96f44dcc50430b7bfd09cd20493184489746

Files: 1fff96f44dcc50430b7bfd09cd20493184489746 / environment.js

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

Built with git-ssb-web