Commit 67053274fc3dc39d5be81c4365673d94bd15a6bf
Merge pull request #41 from ewasm/create
Create: split out create machinism and support deploy/runtime bytecodeAlex Beregszaszi authored on 8/26/2016, 11:31:08 PM
GitHub committed on 8/26/2016, 11:31:08 PM
Parent: 35fc7a2516bd2641e4b9bb32ebdaed256fe57491
Parent: 254f1dd5867137c7707537afbcff63ec3aee6563
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -88,9 +88,10 @@ | ||
88 | 88 | if (!Utils.isWASMCode(code)) { |
89 | 89 | // throw new Error('Not an eWASM contract') |
90 | 90 | |
91 | 91 | // Transcompile code |
92 | - code = this.callHandler({ to: transcompilerContract, data: code }).returnValue | |
92 | + // FIXME: decide if these are the right values here: from: 0, gasLimit: 0, value: 0 | |
93 | + code = this.callHandler({ from: Address.zero(), to: transcompilerContract, gasLimit: 0, value: new U256(0), data: code }).returnValue | |
93 | 94 | } |
94 | 95 | |
95 | 96 | // creats a new Kernel |
96 | 97 | const environment = new Environment() |
@@ -105,9 +106,10 @@ | ||
105 | 106 | environment.callData = call.data |
106 | 107 | environment.callValue = call.value |
107 | 108 | environment.gasLeft = call.gasLimit |
108 | 109 | |
109 | - // environment.setCallHandler(callHandler) | |
110 | + // environment.setCallHandler(this.callHandler) | |
111 | + // environment.setCreateHandler(this.createHandler) | |
110 | 112 | |
111 | 113 | const kernel = new Kernel(this, environment) |
112 | 114 | kernel.codeHandler(code, new Interface(environment)) |
113 | 115 | |
@@ -123,8 +125,35 @@ | ||
123 | 125 | logs: environment.logs |
124 | 126 | } |
125 | 127 | } |
126 | 128 | |
129 | + createHandler (create) { | |
130 | + let code = create.data | |
131 | + | |
132 | + // Inject metering | |
133 | + if (Utils.isWASMCode(code)) { | |
134 | + // FIXME: decide if these are the right values here: from: 0, gasLimit: 0, value: 0 | |
135 | + code = this.callHandler({ from: Address.zero(), to: meteringContract, gasLimit: 0, value: new U256(0), data: code }).returnValue | |
136 | + } | |
137 | + | |
138 | + let address = Utils.newAccountAddress(create.from, code) | |
139 | + | |
140 | + this.environment.addAccount(address.toString(), { | |
141 | + balance: create.value, | |
142 | + code: code | |
143 | + }) | |
144 | + | |
145 | + // Run code and take return value as contract code | |
146 | + // FIXME: decide if these are the right values here: value: 0, data: '' | |
147 | + code = this.callHandler({ from: create.from, to: address, gasLimit: create.gasLimit, value: new U256(0), data: new Uint8Array() }).returnValue | |
148 | + | |
149 | + this.environment.state.get(address.toString()).set('code', code) | |
150 | + | |
151 | + return { | |
152 | + accountCreated: address | |
153 | + } | |
154 | + } | |
155 | + | |
127 | 156 | // run tx; the tx message handler |
128 | 157 | runTx (tx, environment = new Environment()) { |
129 | 158 | this.environment = environment |
130 | 159 | |
@@ -151,23 +180,16 @@ | ||
151 | 180 | if (tx.to.isZero()) { |
152 | 181 | if (tx.data.length !== 0) { |
153 | 182 | console.log('This is a contract deployment transaction') |
154 | 183 | |
155 | - // Inject metering | |
156 | - const code = this.callHandler({ to: meteringContract, data: tx.data }).returnValue | |
184 | + // FIXME: deduct fees | |
157 | 185 | |
158 | - let address = Utils.newAccountAddress(tx.from, code) | |
159 | - | |
160 | - this.environment.addAccount(address.toString(), { | |
161 | - balance: tx.value, | |
162 | - code: code | |
186 | + return this.createHandler({ | |
187 | + from: tx.from, | |
188 | + gasLimit: tx.gasLimit, | |
189 | + value: tx.value, | |
190 | + data: tx.data | |
163 | 191 | }) |
164 | - | |
165 | - // FIXME: deduct fees | |
166 | - | |
167 | - return { | |
168 | - accountCreated: address | |
169 | - } | |
170 | 192 | } |
171 | 193 | } |
172 | 194 | |
173 | 195 | // deduct gasLimit * gasPrice from sender |
Built with git-ssb-web