git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 0edfe5b10a3a58691d782ae18e443689a66cf95d

Files: 0edfe5b10a3a58691d782ae18e443689a66cf95d / environment.js

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

Built with git-ssb-web