git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 69097f452f01a61429f16530219f9f1dc548b8d3

Files: 69097f452f01a61429f16530219f9f1dc548b8d3 / index.js

9752 bytesRaw
1/**
2 * This implements the Ethereum Kernel
3 * Kernels must implement two methods `codeHandler` and `callHandler` (and `linkHandler` for sharding)
4 * The Kernel Contract handles the following
5 * - Interprocess communications
6 * - Intializing the VM and exposes ROM to it (codeHandler)
7 * - Expose namespace which VM instance exists and Intializes the Environment (callHandler)
8 * - Provides some built in contract (runTx, runBlock)
9 * - Provides resource sharing and limiting via gas
10 *
11 * All State should be stored in the Environment.
12 *
13 */
14
15// The Kernel Exposes this Interface to VM instances it makes
16const Interface = require('./interface.js')
17
18// The Kernel Stores all of its state in the Environment. The Interface is used
19// to by the VM to retrive infromation from the Environment.
20const Environment = require('./environment.js')
21const DebugInterface = require('./debugInterface.js')
22const Address = require('./deps/address.js')
23const U256 = require('./deps/u256.js')
24const Utils = require('./deps/utils.js')
25const Transaction = require('./deps/transaction.js')
26const Precompile = require('./precompile.js')
27
28const identityContract = new Address('0x0000000000000000000000000000000000000004')
29const meteringContract = new Address('0x000000000000000000000000000000000000000A')
30const transcompilerContract = new Address('0x000000000000000000000000000000000000000B')
31
32module.exports = class Kernel {
33 // runs some code in the VM
34 constructor (environment = new Environment()) {
35 this.environment = environment
36
37 this.environment.addAccount(identityContract, {})
38 this.environment.addAccount(meteringContract, {})
39 this.environment.addAccount(transcompilerContract, {})
40 }
41
42 // handles running code.
43 // NOTE: it assumes that wasm will raise an exception if something went wrong,
44 // otherwise execution succeeded
45 codeHandler (code, ethInterface = new Interface(new Environment())) {
46 const debugInterface = new DebugInterface(ethInterface.environment)
47 const module = WebAssembly.Module(code)
48 const imports = {
49 'ethereum': ethInterface.exportTable,
50 'debug': debugInterface.exportTable,
51
52 // export this for Rust
53 // FIXME: remove once Rust has proper imports, see https://github.com/ethereum/evm2.0-design/issues/15
54 'spectest': ethInterface.exportTable,
55
56 // export this for Binaryen
57 // FIXME: remove once C has proper imports, see https://github.com/ethereum/evm2.0-design/issues/16
58 'env': ethInterface.exportTable
59 }
60 // add shims
61 imports.ethereum.useGas = ethInterface.shims.exports.useGas
62 imports.ethereum.getGasLeft = ethInterface.shims.exports.getGasLeft
63 imports.ethereum.call = ethInterface.shims.exports.call
64
65 const instance = WebAssembly.Instance(module, imports)
66
67 ethInterface.setModule(instance)
68 debugInterface.setModule(instance)
69
70 if (instance.exports.main) {
71 instance.exports.main()
72 }
73 return instance
74 }
75
76 // loads code from the merkle trie and delegates the message
77 // Detects if code is EVM or WASM
78 // Detects if the code injection is needed
79 // Detects if transcompilation is needed
80 callHandler (call) {
81 // FIXME: this is here until these two contracts are compiled to WASM
82 // The two special contracts (precompiles now, but will be real ones later)
83 if (call.to.equals(meteringContract)) {
84 return Precompile.meteringInjector(call)
85 } else if (call.to.equals(transcompilerContract)) {
86 return Precompile.transcompiler(call)
87 } else if (call.to.equals(identityContract)) {
88 return Precompile.identity(call)
89 }
90
91 let account = this.environment.state.get(call.to.toString())
92 if (!account) {
93 throw new Error('Account not found: ' + call.to.toString())
94 }
95
96 let code = Uint8Array.from(account.get('code'))
97 if (code.length === 0) {
98 throw new Error('Contract not found')
99 }
100
101 if (!Utils.isWASMCode(code)) {
102 // throw new Error('Not an eWASM contract')
103
104 // Transcompile code
105 // FIXME: decide if these are the right values here: from: 0, gasLimit: 0, value: 0
106 code = this.callHandler({ from: Address.zero(), to: transcompilerContract, gasLimit: 0, value: new U256(0), data: code }).returnValue
107
108 if (code[0] === 0) {
109 code = code.slice(1)
110 } else {
111 throw new Error('Transcompilation failed: ' + Buffer.from(code).slice(1).toString())
112 }
113 }
114
115 // creats a new Kernel
116 const environment = new Environment()
117 environment.parent = this
118
119 // copy the transaction details
120 environment.code = code
121 environment.address = call.to
122 // FIXME: make distinction between origin and caller
123 environment.origin = call.from
124 environment.caller = call.from
125 environment.callData = call.data
126 environment.callValue = call.value
127 environment.gasLeft = call.gasLimit
128
129 environment.callHandler = this.callHandler.bind(this)
130 environment.createHandler = this.createHandler.bind(this)
131
132 const kernel = new Kernel(environment)
133 kernel.codeHandler(code, new Interface(environment))
134
135 // self destructed
136 if (environment.selfDestruct) {
137 const balance = this.state.get(call.to.toString()).get('balance')
138 const beneficiary = this.state.get(environment.selfDestructAddress)
139 beneficiary.set('balance', beneficiary.get('balance').add(balance))
140 this.state.delete(call.to.toString())
141 }
142
143 // generate new stateroot
144 // this.environment.state.set(address, { stateRoot: stateRoot })
145
146 return {
147 executionOutcome: 1, // success
148 gasLeft: new U256(environment.gasLeft),
149 gasRefund: new U256(environment.gasRefund),
150 returnValue: environment.returnValue,
151 selfDestruct: environment.selfDestruct,
152 selfDestructAddress: environment.selfDestructAddress,
153 logs: environment.logs
154 }
155 }
156
157 createHandler (create) {
158 let code = create.data
159
160 // Inject metering
161 if (Utils.isWASMCode(code)) {
162 // FIXME: decide if these are the right values here: from: 0, gasLimit: 0, value: 0
163 code = this.callHandler({ from: Address.zero(), to: meteringContract, gasLimit: 0, value: new U256(0), data: code }).returnValue
164
165 if (code[0] === 0) {
166 code = code.slice(1)
167 } else {
168 throw new Error('Metering injection failed: ' + Buffer.from(code).slice(1).toString())
169 }
170 }
171
172 let address = Utils.newAccountAddress(create.from, code)
173
174 this.environment.addAccount(address.toString(), {
175 balance: create.value,
176 code: code
177 })
178
179 // Run code and take return value as contract code
180 // FIXME: decide if these are the right values here: value: 0, data: ''
181 code = this.callHandler({ from: create.from, to: address, gasLimit: create.gasLimit, value: new U256(0), data: new Uint8Array() }).returnValue
182
183 // FIXME: special handling for selfdestruct
184
185 this.environment.state.get(address.toString()).set('code', code)
186
187 return {
188 executionOutcome: 1, // success
189 gasLeft: new U256(this.environment.gasLeft),
190 gasRefund: new U256(this.environment.gasRefund),
191 accountCreated: address,
192 logs: this.environment.logs
193 }
194 }
195
196 // run tx; the tx message handler
197 runTx (tx, environment = new Environment()) {
198 this.environment = environment
199
200 if (Buffer.isBuffer(tx) || typeof tx === 'string') {
201 tx = new Transaction(tx)
202 if (!tx.valid) {
203 throw new Error('Invalid transaction signature')
204 }
205 }
206
207 // look up sender
208 let fromAccount = this.environment.state.get(tx.from.toString())
209 if (!fromAccount) {
210 throw new Error('Sender account not found: ' + tx.from.toString())
211 }
212
213 if (fromAccount.get('nonce').gt(tx.nonce)) {
214 throw new Error(`Invalid nonce: ${fromAccount.get('nonce')} > ${tx.nonce}`)
215 }
216
217 fromAccount.set('nonce', fromAccount.get('nonce').add(new U256(1)))
218
219 let isCreation = false
220
221 // Special case: contract deployment
222 if (tx.to.isZero() && (tx.data.length !== 0)) {
223 console.log('This is a contract deployment transaction')
224 isCreation = true
225 }
226
227 // This cost will not be refunded
228 let txCost = 21000 + (isCreation ? 32000 : 0)
229 tx.data.forEach((item) => {
230 if (item === 0) {
231 txCost += 4
232 } else {
233 txCost += 68
234 }
235 })
236
237 if (tx.gasLimit.lt(new U256(txCost))) {
238 throw new Error(`Minimum transaction gas limit not met: ${txCost}`)
239 }
240
241 if (fromAccount.get('balance').lt(tx.gasLimit.mul(tx.gasPrice))) {
242 throw new Error(`Insufficient account balance: ${fromAccount.get('balance').toString()} < ${tx.gasLimit.mul(tx.gasPrice).toString()}`)
243 }
244
245 // deduct gasLimit * gasPrice from sender
246 fromAccount.set('balance', fromAccount.get('balance').sub(tx.gasLimit.mul(tx.gasPrice)))
247
248 const handler = isCreation ? this.createHandler.bind(this) : this.callHandler.bind(this)
249 let ret = handler({
250 to: tx.to,
251 from: tx.from,
252 gasLimit: tx.gasLimit - txCost,
253 value: tx.value,
254 data: tx.data
255 })
256
257 // refund unused gas
258 if (ret.executionOutcome === 1) {
259 fromAccount.set('balance', fromAccount.get('balance').add(tx.gasPrice.mul(ret.gasLeft.add(ret.gasRefund))))
260 }
261
262 // save new state?
263
264 return {
265 executionOutcome: ret.executionOutcome,
266 accountCreated: isCreation ? ret.accountCreated : undefined,
267 returnValue: isCreation ? undefined : ret.returnValue,
268 gasLeft: ret.gasLeft,
269 logs: ret.logs
270 }
271 }
272
273 // run block; the block message handler
274 runBlock (block, environment = new Environment()) {
275 // verify block then run each tx
276 block.tx.forEach((tx) => {
277 this.runTx(tx, environment)
278 })
279 }
280
281 // run blockchain
282 // runBlockchain () {}
283}
284

Built with git-ssb-web