git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 8703ef363b38c0652c4ef86ba883fef73c99b1d9

Files: 8703ef363b38c0652c4ef86ba883fef73c99b1d9 / index.js

9447 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('./address.js')
23const U256 = require('./u256.js')
24const Utils = require('./utils.js')
25const Transaction = require('./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
48 const instance = Wasm.instantiateModule(code, {
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
61 ethInterface.setModule(instance)
62 debugInterface.setModule(instance)
63
64 if (instance.exports.main) {
65 instance.exports.main()
66 }
67 return instance
68 }
69
70 // loads code from the merkle trie and delegates the message
71 // Detects if code is EVM or WASM
72 // Detects if the code injection is needed
73 // Detects if transcompilation is needed
74 callHandler (call) {
75 // FIXME: this is here until these two contracts are compiled to WASM
76 // The two special contracts (precompiles now, but will be real ones later)
77 if (call.to.equals(meteringContract)) {
78 return Precompile.meteringInjector(call)
79 } else if (call.to.equals(transcompilerContract)) {
80 return Precompile.transcompiler(call)
81 } else if (call.to.equals(identityContract)) {
82 return Precompile.identity(call)
83 }
84
85 let account = this.environment.state.get(call.to.toString())
86 if (!account) {
87 throw new Error('Account not found: ' + call.to.toString())
88 }
89
90 let code = Uint8Array.from(account.get('code'))
91 if (code.length === 0) {
92 throw new Error('Contract not found')
93 }
94
95 if (!Utils.isWASMCode(code)) {
96 // throw new Error('Not an eWASM contract')
97
98 // Transcompile code
99 // FIXME: decide if these are the right values here: from: 0, gasLimit: 0, value: 0
100 code = this.callHandler({ from: Address.zero(), to: transcompilerContract, gasLimit: 0, value: new U256(0), data: code }).returnValue
101
102 if (code[0] === 0) {
103 code = code.slice(1)
104 } else {
105 throw new Error('Transcompilation failed: ' + Buffer.from(code).slice(1).toString())
106 }
107 }
108
109 // creats a new Kernel
110 const environment = new Environment()
111 environment.parent = this
112
113 // copy the transaction details
114 environment.code = code
115 environment.address = call.to
116 // FIXME: make distinction between origin and caller
117 environment.origin = call.from
118 environment.caller = call.from
119 environment.callData = call.data
120 environment.callValue = call.value
121 environment.gasLeft = call.gasLimit
122
123 environment.callHandler = this.callHandler.bind(this)
124 environment.createHandler = this.createHandler.bind(this)
125
126 const kernel = new Kernel(environment)
127 kernel.codeHandler(code, new Interface(environment))
128
129 // self destructed
130 if (environment.selfDestruct) {
131 const balance = this.state.get(call.to.toString()).get('balance')
132 const beneficiary = this.state.get(environment.selfDestructAddress)
133 beneficiary.set('balance', beneficiary.get('balance').add(balance))
134 this.state.delete(call.to.toString())
135 }
136
137 // generate new stateroot
138 // this.environment.state.set(address, { stateRoot: stateRoot })
139
140 return {
141 executionOutcome: 1, // success
142 gasLeft: new U256(environment.gasLeft),
143 gasRefund: new U256(environment.gasRefund),
144 returnValue: environment.returnValue,
145 selfDestruct: environment.selfDestruct,
146 selfDestructAddress: environment.selfDestructAddress,
147 logs: environment.logs
148 }
149 }
150
151 createHandler (create) {
152 let code = create.data
153
154 // Inject metering
155 if (Utils.isWASMCode(code)) {
156 // FIXME: decide if these are the right values here: from: 0, gasLimit: 0, value: 0
157 code = this.callHandler({ from: Address.zero(), to: meteringContract, gasLimit: 0, value: new U256(0), data: code }).returnValue
158
159 if (code[0] === 0) {
160 code = code.slice(1)
161 } else {
162 throw new Error('Metering injection failed: ' + Buffer.from(code).slice(1).toString())
163 }
164 }
165
166 let address = Utils.newAccountAddress(create.from, code)
167
168 this.environment.addAccount(address.toString(), {
169 balance: create.value,
170 code: code
171 })
172
173 // Run code and take return value as contract code
174 // FIXME: decide if these are the right values here: value: 0, data: ''
175 code = this.callHandler({ from: create.from, to: address, gasLimit: create.gasLimit, value: new U256(0), data: new Uint8Array() }).returnValue
176
177 // FIXME: special handling for selfdestruct
178
179 this.environment.state.get(address.toString()).set('code', code)
180
181 return {
182 executionOutcome: 1, // success
183 gasLeft: new U256(this.environment.gasLeft),
184 gasRefund: new U256(this.environment.gasRefund),
185 accountCreated: address,
186 logs: this.environment.logs
187 }
188 }
189
190 // run tx; the tx message handler
191 runTx (tx, environment = new Environment()) {
192 this.environment = environment
193
194 if (Buffer.isBuffer(tx) || typeof tx === 'string') {
195 tx = new Transaction(tx)
196 if (!tx.valid) {
197 throw new Error('Invalid transaction signature')
198 }
199 }
200
201 // look up sender
202 let fromAccount = this.environment.state.get(tx.from.toString())
203 if (!fromAccount) {
204 throw new Error('Sender account not found: ' + tx.from.toString())
205 }
206
207 if (fromAccount.get('nonce').gt(tx.nonce)) {
208 throw new Error(`Invalid nonce: ${fromAccount.get('nonce')} > ${tx.nonce}`)
209 }
210
211 fromAccount.set('nonce', fromAccount.get('nonce').add(new U256(1)))
212
213 let isCreation = false
214
215 // Special case: contract deployment
216 if (tx.to.isZero() && (tx.data.length !== 0)) {
217 console.log('This is a contract deployment transaction')
218 isCreation = true
219 }
220
221 // This cost will not be refunded
222 let txCost = 21000 + (isCreation ? 32000 : 0)
223 tx.data.forEach((item) => {
224 if (item === 0) {
225 txCost += 4
226 } else {
227 txCost += 68
228 }
229 })
230
231 if (tx.gasLimit.lt(new U256(txCost))) {
232 throw new Error(`Minimum transaction gas limit not met: ${txCost}`)
233 }
234
235 if (fromAccount.get('balance').lt(tx.gasLimit.mul(tx.gasPrice))) {
236 throw new Error(`Insufficient account balance: ${fromAccount.get('balance').toString()} < ${tx.gasLimit.mul(tx.gasPrice).toString()}`)
237 }
238
239 // deduct gasLimit * gasPrice from sender
240 fromAccount.set('balance', fromAccount.get('balance').sub(tx.gasLimit.mul(tx.gasPrice)))
241
242 const handler = isCreation ? this.createHandler.bind(this) : this.callHandler.bind(this)
243 let ret = handler({
244 to: tx.to,
245 from: tx.from,
246 gasLimit: tx.gasLimit - txCost,
247 value: tx.value,
248 data: tx.data
249 })
250
251 // refund unused gas
252 if (ret.executionOutcome === 1) {
253 fromAccount.set('balance', fromAccount.get('balance').add(tx.gasPrice.mul(ret.gasLeft.add(ret.gasRefund))))
254 }
255
256 // save new state?
257
258 return {
259 executionOutcome: ret.executionOutcome,
260 accountCreated: isCreation ? ret.accountCreated : undefined,
261 returnValue: isCreation ? undefined : ret.returnValue,
262 gasLeft: ret.gasLeft,
263 logs: ret.logs
264 }
265 }
266
267 // run block; the block message handler
268 runBlock (block, environment = new Environment()) {
269 // verify block then run each tx
270 block.tx.forEach((tx) => {
271 this.runTx(tx, environment)
272 })
273 }
274
275 // run blockchain
276 // runBlockchain () {}
277}
278

Built with git-ssb-web