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