Files: 439177b212e72f0e65ae91c034b8d4d8c9c85d9a / environment.js
3164 bytesRaw
1 | const Vertex = require('merkle-trie') |
2 | const Store = require('merkle-trie/store') |
3 | const U256 = require('./deps/u256.js') |
4 | const Address = require('./deps/address.js') |
5 | const Block = require('./deps/block.js') |
6 | // TODO remove fakeblockchain |
7 | const fakeBlockChain = require('./fakeBlockChain.js') |
8 | |
9 | module.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 | // 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 | const block = await this.blockchain.getBlock(height) |
79 | return block.hash() |
80 | } |
81 | |
82 | set createHandler (value) { |
83 | this.createhandler = value |
84 | } |
85 | |
86 | set callHandler (value) { |
87 | this.callhandler = value |
88 | } |
89 | |
90 | // kernal |
91 | create (code, value) { |
92 | // STUB |
93 | return [ 1, Address.zero() ] |
94 | } |
95 | |
96 | call (gas, address, value, data) { |
97 | // FIXME: create a child environment here |
98 | const ret = this.root.messagehandler({ |
99 | from: this.address, |
100 | to: address, |
101 | gasLimit: gas, |
102 | value: value, |
103 | data: data |
104 | }) |
105 | return [ !!ret.executionOutcome, ret.returnValue ] |
106 | } |
107 | |
108 | callCode (gas, address, value, data) { |
109 | // STUB |
110 | return [ 1, new Uint8Array() ] |
111 | } |
112 | |
113 | delegateCall (gas, address, data) { |
114 | // STUB |
115 | return [ 1, new Uint8Array() ] |
116 | } |
117 | } |
118 |
Built with git-ssb-web