git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit af08dc3e0ce5be82ca5f4be87523656ed6ce5498

VM design according to the Monday call

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

Files changed

index.jschanged
index.jsView
@@ -31,9 +31,9 @@
3131
3232 // handles running code.
3333 // NOTE: it assumes that wasm will raise an exception if something went wrong,
3434 // otherwise execution succeeded
35- static codeHandler (code, ethInterface = new Interface(new Environment())) {
35+ codeHandler (code, ethInterface = new Interface(new Environment())) {
3636 const debugInterface = new DebugInterface(ethInterface.environment)
3737
3838 const instance = Wasm.instantiateModule(code, {
3939 'ethereum': ethInterface.exportTable,
@@ -60,21 +60,41 @@
6060 // loads code from the merkle trie and delegates the message
6161 // Detects if code is EVM or WASM
6262 // Detects if the code injection is needed
6363 // Detects if transcompilation is needed
64- callHandler (path, data) {
64+ callHandler (address, gaslimit, gasprice, value, data) {
65+ var toAccount = this.environment.accounts.get(new Uint8Array(address).toString())
66+ if (!toAccount) {
67+ throw new Error('Account not found')
68+ }
69+
6570 // creats a new Kernel
6671 const environment = new Environment(data)
67- // environment.parent = this
72+ environment.parent = this
6873 const kernel = new Kernel(this, environment)
69- const code = this.environment.state.get(path)
74+ const code = this.environment.state.get(address)
75+
76+ //environment.setCallHandler(callHandler)
77+
7078 if (!code) {
7179 throw new Error('Contract not found')
7280 }
7381 if (!Utils.isWASMCode(code)) {
7482 throw new Error('Not an eWASM contract')
7583 }
7684 kernel.codeHandler(code, new Interface(environment))
85+
86+ // generate new stateroot
87+ //this.environment.state.set(address, { stateRoot: stateRoot })
88+
89+ return {
90+ executionOutcome: 1, // success
91+ gasLeft: 0,
92+ gasRefunds: 0,
93+ returnValue: new ArrayBuffer(),
94+ selfDestructAddress: new Uint8Array(),
95+ logs: []
96+ }
7797 }
7898
7999 // run tx; the tx message handler
80100 runTx (tx, environment = new Environment()) {
@@ -84,22 +104,48 @@
84104 // - ecrecover
85105 // new ethTx(tx).validate(tx)
86106 // - reduce balance
87107
108+ this.environment = environment
109+
88110 // Contract deployment
89- const isDeployment = tx.data && !tx.to;
90- if (isDeployment) {
91- this.environment.accounts.set(new Uint8Array())
111+ //const isDeployment = tx.data && !tx.to;
112+ //if (isDeployment) {
113+ // this.environment.accounts.set(new Uint8Array())
114+ //}
115+
116+ //
117+ // environment.state - the merkle tree
118+ // key: address (20 byte, hex string, without 0x prefix)
119+ // every path has an account
120+ //
121+ // { balance, codeHash, stateRoot }
122+ //
123+
124+ // look up sender
125+ let fromAccount = this.environment.accounts.get(new Uint8Array(tx.form).toString())
126+
127+ // deduct gasLimit * gasPrice from sender
128+ if (fromAccount.balance < (tx.gasLimit * tx.gasPrice)) {
129+ throw new Error('Insufficient account balance')
92130 }
93131
94- var toAccount = this.environment.accounts.get(new Uint8Array(tx.to).toString())
95- var fromAccount = this.environment.accounts.get(new Uint8Array(tx.form).toString())
132+ fromAccount.balance -= ts.gasLimit * tx.gasPrice
96133
97- if (!toAccount) {
98- throw new Error('Account not found')
134+ let ret = this.callHandler(tx.to, tx.gasLimit, tx.gasPrice, tx.value, tx.data)
135+
136+ // refund gas
137+ if (ret.executionOutcome === 1) {
138+ fromAccount.balance += (ret.gasLeft + ret.gasRefund) * tx.gasPrice
99139 }
100140
101- this.callHandler(account.codeHash, environment)
141+ // save new state?
142+
143+ return {
144+ returnValue: ret.returnValue,
145+ gasLeft: ret.gasLeft,
146+ logs: ret.logs
147+ }
102148 }
103149
104150 // run block; the block message handler
105151 runBlock (block, environment = new Environment()) {

Built with git-ssb-web