git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 0a07ab91671ed72fa98fdba917116ac2cbe4ab81

Kernel: check that a transaction meets the minimum costs

Alex Beregszaszi committed on 8/28/2016, 2:54:06 PM
Parent: 08ad07cf77557b320e766cc5a1f9bc0554ad824d

Files changed

index.jschanged
index.jsView
@@ -198,15 +198,8 @@
198198 })
199199 }
200200 }
201201
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-
209202 // This cost will not be refunded
210203 let txCost = 21000
211204 tx.data.forEach((item) => {
212205 if (item === 0) {
@@ -215,17 +208,28 @@
215208 txCost += 68
216209 }
217210 })
218211
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+
219223 let ret = this.callHandler({
220224 to: tx.to,
221225 from: tx.from,
222226 gasLimit: tx.gasLimit - txCost,
223227 value: tx.value,
224228 data: tx.data
225229 })
226230
227- // refund gas
231+ // refund unused gas
228232 if (ret.executionOutcome === 1) {
229233 fromAccount.set('balance', fromAccount.get('balance').add(tx.gasPrice.mul(ret.gasLeft.add(ret.gasRefund))))
230234 }
231235

Built with git-ssb-web