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