Files: ed1a622c86d302461688dd47df71585c8e5e74e4 / environment.js
2993 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 | const fakeBlockChain = require('./fakeBlockChain.js') |
7 | |
8 | module.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 | Object.assign(this, defaults, data) |
35 | } |
36 | |
37 | // addAccount (address, trie) { |
38 | // let account = new Vertex() |
39 | // account.set('nonce', trie.nonce || new U256(0)) |
40 | // account.set('balance', trie.balance || new U256(0)) |
41 | // account.set('code', trie.code || new Uint8Array()) |
42 | // account.set('storage', trie.storage || new Map()) |
43 | // this.state.set(address.toString(), account) |
44 | // } |
45 | |
46 | isAccountPresent (address) { |
47 | // const account = this.state.get(address.toString()) |
48 | // if (account) { |
49 | // return true |
50 | // } else { |
51 | // return false |
52 | // } |
53 | } |
54 | |
55 | getBalance (address) { |
56 | // const account = this.state.get(address.toString()) |
57 | // if (account) { |
58 | // return account.get('balance') |
59 | // } else { |
60 | // return new U256() |
61 | // } |
62 | } |
63 | |
64 | getCode (address) { |
65 | // const account = this.state.get(address.toString()) |
66 | // if (account) { |
67 | // return account.get('code') |
68 | // } else { |
69 | // return Uint8Array.from(new Buffer([])) |
70 | // } |
71 | } |
72 | |
73 | getBlockHash (height) { |
74 | // return this.blockchain.getBlock(height).hash() |
75 | return this.root.getBlockAt(height).then(block => block.hash()) |
76 | } |
77 | |
78 | set createHandler (value) { |
79 | this.createhandler = value |
80 | } |
81 | |
82 | set callHandler (value) { |
83 | this.callhandler = value |
84 | } |
85 | |
86 | // kernal |
87 | create (code, value) { |
88 | // STUB |
89 | return [ 1, Address.zero() ] |
90 | } |
91 | |
92 | call (gas, address, value, data) { |
93 | // FIXME: create a child environment here |
94 | const ret = this.root.messagehandler({ |
95 | from: this.address, |
96 | to: address, |
97 | gasLimit: gas, |
98 | value: value, |
99 | data: data |
100 | }) |
101 | return [ !!ret.executionOutcome, ret.returnValue ] |
102 | } |
103 | |
104 | callCode (gas, address, value, data) { |
105 | // STUB |
106 | return [ 1, new Uint8Array() ] |
107 | } |
108 | |
109 | delegateCall (gas, address, data) { |
110 | // STUB |
111 | return [ 1, new Uint8Array() ] |
112 | } |
113 | } |
114 |
Built with git-ssb-web