git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 76b626ab5a6d24e48ccefffb8900dc9052b0b36d

Files: 76b626ab5a6d24e48ccefffb8900dc9052b0b36d / environment.js

2073 bytesRaw
1const Graph = require('generic-digraph')
2const MAX_BAL_BYTES = require('./constants.js').MAX_BAL_BYTES
3
4module.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(20),
13 origin: new Uint8Array(20),
14 coinbase: new Uint8Array(20),
15 difficulty: new Uint8Array(20),
16 caller: new Uint8Array(20),
17 callValue: new Uint8Array(MAX_BAL_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
79function hexStr2arrayBuf (string) {
80 const ab = new ArrayBuffer(string.length / 2)
81 const view = new Uint8Array(ab)
82 string = [...string]
83 let temp = ''
84 string.forEach((el, i) => {
85 temp += el
86 if (i % 2) {
87 view[(i + 1) / 2 - 1] = parseInt(temp, 16)
88 temp = ''
89 }
90 })
91 return ab
92}
93

Built with git-ssb-web