git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: a1cbfac008b122511f7f6be08a5e1dc164c4a9ad

Files: a1cbfac008b122511f7f6be08a5e1dc164c4a9ad / environment.js

2261 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 // call infromation
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 // the ROM
19 code: new ArrayBuffer(), // the current running code
20 // output calls
21 logs: [],
22 suicideAddress: new ArrayBuffer(),
23 // more output calls
24 returnValue: new ArrayBuffer()
25 }
26
27 this.state = new Graph()
28
29 if (data) {
30 data = JSON.parse(data)
31 } else {
32 data = {}
33 }
34
35 Object.assign(this, defaults, data)
36
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
94function 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