git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 5548f3620a5008e9db6bdfac769a29fd4616c5f4

Files: 5548f3620a5008e9db6bdfac769a29fd4616c5f4 / environment.js

2631 bytesRaw
1const Vertex = require('merkle-trie')
2const Store = require('merkle-trie/store')
3const U256 = require('./deps/u256.js')
4const Address = require('./deps/address.js')
5const Block = require('./deps/block.js')
6// TODO remove fakeblockchain
7const fakeBlockChain = require('./fakeBlockChain.js')
8
9module.exports = class Environment {
10 constructor (data = {}) {
11 const defaults = {
12 block: new Block(),
13 blockchain: fakeBlockChain,
14 // gas tank
15 gasPrice: 0,
16 gasLeft: 1000000,
17 gasRefund: 0,
18 // call infromation
19 address: new Address('0x0000000000000000000000000000000000000000'),
20 origin: new Address('0x0000000000000000000000000000000000000000'),
21 caller: new Address('0x0000000000000000000000000000000000000000'),
22 callValue: new U256(0),
23 callData: new Uint8Array(),
24 // the ROM
25 code: new Uint8Array(), // the current running code
26 // output calls
27 logs: [],
28 selfDestruct: false,
29 selfDestructAddress: new Address('0x0000000000000000000000000000000000000000'),
30 // more output calls
31 returnValue: new Uint8Array(),
32 state: new Vertex({store: new Store()})
33 }
34 Object.assign(this, defaults, data)
35 }
36
37 isAccountPresent (address) {
38 // const account = this.state.get(address.toString())
39 // if (account) {
40 // return true
41 // } else {
42 // return false
43 // }
44 }
45
46 getBalance (address) {
47 // const account = this.state.get(address.toString())
48 // if (account) {
49 // return account.get('balance')
50 // } else {
51 // return new U256()
52 // }
53 }
54
55 getCode (address) {
56 // const account = this.state.get(address.toString())
57 // if (account) {
58 // return account.get('code')
59 // } else {
60 // return Uint8Array.from(new Buffer([]))
61 // }
62 }
63
64 async getBlockHash (height) {
65 const block = await this.blockchain.getBlock(height)
66 return block.hash()
67 }
68
69 set createHandler (value) {
70 this.createhandler = value
71 }
72
73 set callHandler (value) {
74 this.callhandler = value
75 }
76
77 // kernal
78 create (code, value) {
79 // STUB
80 return [ 1, Address.zero() ]
81 }
82
83 call (gas, address, value, data) {
84 // FIXME: create a child environment here
85 const ret = this.root.messagehandler({
86 from: this.address,
87 to: address,
88 gasLimit: gas,
89 value: value,
90 data: data
91 })
92 return [ !!ret.executionOutcome, ret.returnValue ]
93 }
94
95 callCode (gas, address, value, data) {
96 // STUB
97 return [ 1, new Uint8Array() ]
98 }
99
100 delegateCall (gas, address, data) {
101 // STUB
102 return [ 1, new Uint8Array() ]
103 }
104}
105

Built with git-ssb-web