Commit 0a07ab91671ed72fa98fdba917116ac2cbe4ab81
Kernel: check that a transaction meets the minimum costs
Alex Beregszaszi committed on 8/28/2016, 2:54:06 PMParent: 08ad07cf77557b320e766cc5a1f9bc0554ad824d
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -198,15 +198,8 @@ | ||
198 | 198 | }) |
199 | 199 | } |
200 | 200 | } |
201 | 201 | |
202 | - // deduct gasLimit * gasPrice from sender | |
203 | - if (fromAccount.get('balance').lt(tx.gasLimit.mul(tx.gasPrice))) { | |
204 | - throw new Error(`Insufficient account balance: ${fromAccount.get('balance').toString()} < ${tx.gasLimit.mul(tx.gasPrice).toString()}`) | |
205 | - } | |
206 | - | |
207 | - fromAccount.set('balance', fromAccount.get('balance').sub(tx.gasLimit.mul(tx.gasPrice))) | |
208 | - | |
209 | 202 | // This cost will not be refunded |
210 | 203 | let txCost = 21000 |
211 | 204 | tx.data.forEach((item) => { |
212 | 205 | if (item === 0) { |
@@ -215,17 +208,28 @@ | ||
215 | 208 | txCost += 68 |
216 | 209 | } |
217 | 210 | }) |
218 | 211 | |
212 | + if (tx.gasLimit.lt(new U256(txCost))) { | |
213 | + throw new Error(`Minimum transaction gas limit not met: ${txCost}`) | |
214 | + } | |
215 | + | |
216 | + if (fromAccount.get('balance').lt(tx.gasLimit.mul(tx.gasPrice))) { | |
217 | + throw new Error(`Insufficient account balance: ${fromAccount.get('balance').toString()} < ${tx.gasLimit.mul(tx.gasPrice).toString()}`) | |
218 | + } | |
219 | + | |
220 | + // deduct gasLimit * gasPrice from sender | |
221 | + fromAccount.set('balance', fromAccount.get('balance').sub(tx.gasLimit.mul(tx.gasPrice))) | |
222 | + | |
219 | 223 | let ret = this.callHandler({ |
220 | 224 | to: tx.to, |
221 | 225 | from: tx.from, |
222 | 226 | gasLimit: tx.gasLimit - txCost, |
223 | 227 | value: tx.value, |
224 | 228 | data: tx.data |
225 | 229 | }) |
226 | 230 | |
227 | - // refund gas | |
231 | + // refund unused gas | |
228 | 232 | if (ret.executionOutcome === 1) { |
229 | 233 | fromAccount.set('balance', fromAccount.get('balance').add(tx.gasPrice.mul(ret.gasLeft.add(ret.gasRefund)))) |
230 | 234 | } |
231 | 235 |
Built with git-ssb-web