git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: c326c8af2a729760e833b861163acade71770ff5

Files: c326c8af2a729760e833b861163acade71770ff5 / environment.js

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

Built with git-ssb-web