git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit cfa65ab834c034419a449d152e4615b6171278c3

Kernel: introduce stubs for metering and transcompiler contracts

Alex Beregszaszi committed on 8/23/2016, 12:27:46 AM
Parent: 8a21ea4613a5b638dc13e168c6f3452b0cd8d3f4

Files changed

index.jschanged
precompile.jsadded
index.jsView
@@ -24,9 +24,13 @@
2424 const Address = require('./address.js')
2525 const U256 = require('./u256.js')
2626 const Utils = require('./utils.js')
2727 const Transaction = require('./transaction.js')
28+const Precompile = require('./precompile.js')
2829
30+const meteringContract = new Address("0x000000000000000000000000000000000000000A")
31+const transcompilerContract = new Address("0x000000000000000000000000000000000000000B")
32+
2933 module.exports = class Kernel {
3034 // runs some code in the VM
3135 constructor (environment = new Environment()) {
3236 this.environment = environment
@@ -64,8 +68,16 @@
6468 // Detects if code is EVM or WASM
6569 // Detects if the code injection is needed
6670 // Detects if transcompilation is needed
6771 callHandler (call) {
72+ // FIXME: this is here until these two contracts are compiled to WASM
73+ // The two special contracts (precompiles now, but will be real ones later)
74+ if (call.to.equals(meteringContract)) {
75+ return Precompile.meteringInjector(call)
76+ } else if (call.to.equals(transcompilerContract)) {
77+ return Precompile.transcompiler(call)
78+ }
79+
6880 let account = this.environment.state.get(call.to.toString())
6981 if (!account) {
7082 throw new Error('Account not found: ' + call.to.toString())
7183 }
@@ -75,9 +87,12 @@
7587 throw new Error('Contract not found')
7688 }
7789
7890 if (!Utils.isWASMCode(code)) {
79- throw new Error('Not an eWASM contract')
91+ // throw new Error('Not an eWASM contract')
92+
93+ // Transcompile code
94+ code = this.callHandler({ to: transcompilerContract, data: code }).returnValue
8095 }
8196
8297 // creats a new Kernel
8398 const environment = new Environment()
@@ -102,9 +117,9 @@
102117 //this.environment.state.set(address, { stateRoot: stateRoot })
103118
104119 return {
105120 executionOutcome: 1, // success
106- gasLeft: new U256(environment.gasLimit), // this starts as the limit and results as the gas left
121+ gasLeft: new U256(environment.gasLimit), // this starts as the limit and results as the gas left
107122 gasRefund: new U256(environment.gasRefund),
108123 returnValue: environment.returnValue,
109124 selfDestructAddress: environment.selfDestructAddress,
110125 logs: environment.logs
@@ -153,10 +168,10 @@
153168 if (tx.to.isZero()) {
154169 if (tx.data.length !== 0) {
155170 console.log('This is a contract deployment transaction')
156171
157- // FIXME: do metering injection here
158- const code = tx.data
172+ // Inject metering
173+ const code = this.callHandler({ to: meteringContract, data: tx.data }).returnValue
159174
160175 let address = Utils.newAccountAddress(tx.from, code)
161176
162177 this.environment.addAccount(address.toString(), {
precompile.jsView
@@ -1,0 +1,13 @@
1+module.exports.meteringInjector = function (call) {
2+ console.log('Executing metering injector')
3+ return {
4+ returnValue: call.data.slice(0)
5+ }
6+}
7+
8+module.exports.transcompiler = function (call) {
9+ console.log('Executing transcompiler')
10+ return {
11+ returnValue: call.data.slice(0)
12+ }
13+}

Built with git-ssb-web