Skip to content

Commit 3beec0f

Browse files
committed
Rename subGas to useGas, mv describeLocation out of eei
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
1 parent 3e899cb commit 3beec0f

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

lib/vm/eei.js

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ module.exports = class EEI {
99
this._runState = runState
1010
}
1111

12-
/**
13-
* NOTE: This is not defined in EEI spec. Should find a
14-
* better alternative.
15-
*/
16-
stop () {
17-
this._runState.stopped = true
18-
}
19-
2012
/**
2113
* Returns address of currently executing account.
2214
*/
@@ -32,6 +24,13 @@ module.exports = class EEI {
3224
return this._runState.origin
3325
}
3426

27+
/**
28+
* Returns price of gas in current environment.
29+
*/
30+
getTxGasPrice () {
31+
return this._runState.gasPrice
32+
}
33+
3534
/**
3635
* Returns the address of the account that is directly
3736
* responsible for this execution.
@@ -90,13 +89,6 @@ module.exports = class EEI {
9089
return this._runState.lastReturned.length
9190
}
9291

93-
/**
94-
* Returns price of gas in current environment.
95-
*/
96-
getTxGasPrice () {
97-
return this._runState.gasPrice
98-
}
99-
10092
/**
10193
* Returns the block’s number.
10294
*/
@@ -139,14 +131,11 @@ module.exports = class EEI {
139131
return this._runState.gasLeft
140132
}
141133

142-
describeLocation () {
143-
var hash = utils.keccak256(this._runState.code).toString('hex')
144-
var address = this._runState.address.toString('hex')
145-
var pc = this._runState.programCounter - 1
146-
return hash + '/' + address + ':' + pc
134+
stop () {
135+
this._runState.stopped = true
147136
}
148137

149-
subGas (amount) {
138+
useGas (amount) {
150139
this._runState.gasLeft.isub(amount)
151140
if (this._runState.gasLeft.ltn(0)) {
152141
this._runState.gasLeft = new BN(0)
@@ -207,7 +196,7 @@ module.exports = class EEI {
207196
const cost = words.mul(fee).add(words.mul(words).div(quadCoeff))
208197

209198
if (cost.gt(this._runState.highestMemCost)) {
210-
this.subGas(cost.sub(this._runState.highestMemCost))
199+
this.useGas(cost.sub(this._runState.highestMemCost))
211200
this._runState.highestMemCost = cost
212201
}
213202

@@ -409,22 +398,22 @@ module.exports = class EEI {
409398
var current = found.current
410399
if (current.equals(value)) {
411400
// If current value equals new value (this is a no-op), 200 gas is deducted.
412-
this.subGas(this._runState, new BN(this._runState._common.param('gasPrices', 'netSstoreNoopGas')))
401+
this.useGas(this._runState, new BN(this._runState._common.param('gasPrices', 'netSstoreNoopGas')))
413402
return
414403
}
415404
// If current value does not equal new value
416405
if (original.equals(current)) {
417406
// If original value equals current value (this storage slot has not been changed by the current execution context)
418407
if (original.length === 0) {
419408
// If original value is 0, 20000 gas is deducted.
420-
return this.subGas(new BN(this._runState._common.param('gasPrices', 'netSstoreInitGas')))
409+
return this.useGas(new BN(this._runState._common.param('gasPrices', 'netSstoreInitGas')))
421410
}
422411
if (value.length === 0) {
423412
// If new value is 0, add 15000 gas to refund counter.
424413
this._runState.gasRefund = this._runState.gasRefund.addn(this._runState._common.param('gasPrices', 'netSstoreClearRefund'))
425414
}
426415
// Otherwise, 5000 gas is deducted.
427-
return this.subGas(new BN(this._runState._common.param('gasPrices', 'netSstoreCleanGas')))
416+
return this.useGas(new BN(this._runState._common.param('gasPrices', 'netSstoreCleanGas')))
428417
}
429418
// If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses.
430419
if (original.length !== 0) {
@@ -447,17 +436,17 @@ module.exports = class EEI {
447436
this._runState.gasRefund = this._runState.gasRefund.addn(this._runState._common.param('gasPrices', 'netSstoreResetRefund'))
448437
}
449438
}
450-
return this.subGas(new BN(this._runState._common.param('gasPrices', 'netSstoreDirtyGas')))
439+
return this.useGas(new BN(this._runState._common.param('gasPrices', 'netSstoreDirtyGas')))
451440
} else {
452441
if (value.length === 0 && !found.length) {
453-
this.subGas(new BN(this._runState._common.param('gasPrices', 'sstoreReset')))
442+
this.useGas(new BN(this._runState._common.param('gasPrices', 'sstoreReset')))
454443
} else if (value.length === 0 && found.length) {
455-
this.subGas(new BN(this._runState._common.param('gasPrices', 'sstoreReset')))
444+
this.useGas(new BN(this._runState._common.param('gasPrices', 'sstoreReset')))
456445
this._runState.gasRefund.iaddn(this._runState._common.param('gasPrices', 'sstoreRefund'))
457446
} else if (value.length !== 0 && !found.length) {
458-
this.subGas(new BN(this._runState._common.param('gasPrices', 'sstoreSet')))
447+
this.useGas(new BN(this._runState._common.param('gasPrices', 'sstoreSet')))
459448
} else if (value.length !== 0 && found.length) {
460-
this.subGas(new BN(this._runState._common.param('gasPrices', 'sstoreReset')))
449+
this.useGas(new BN(this._runState._common.param('gasPrices', 'sstoreReset')))
461450
}
462451
}
463452
}

lib/vm/opFns.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = {
9292
} else {
9393
var bytes = 1 + logTable(exponent)
9494
const gasAmount = new BN(bytes).muln(eei._runState._common.param('gasPrices', 'expByte'))
95-
eei.subGas(gasAmount)
95+
eei.useGas(gasAmount)
9696

9797
var m = BN.red(utils.TWO_POW256)
9898
base = base.toRed(m)
@@ -199,7 +199,7 @@ module.exports = {
199199
SHA3: function (offset, length, eei) {
200200
var data = eei.memLoad(offset, length)
201201
// copy fee
202-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'sha3Word')).imul(length.divCeil(new BN(32))))
202+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'sha3Word')).imul(length.divCeil(new BN(32))))
203203
return new BN(utils.keccak256(data))
204204
},
205205
// 0x30 range - closure state
@@ -255,15 +255,15 @@ module.exports = {
255255
CALLDATACOPY: function (memOffset, dataOffset, dataLength, eei) {
256256
eei.memStore(memOffset, eei.getCallData(), dataOffset, dataLength)
257257
// sub the COPY fee
258-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'copy')).imul(dataLength.divCeil(new BN(32))))
258+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'copy')).imul(dataLength.divCeil(new BN(32))))
259259
},
260260
CODESIZE: function (eei) {
261261
return new BN(eei.getCodeSize())
262262
},
263263
CODECOPY: function (memOffset, codeOffset, length, eei) {
264264
eei.memStore(memOffset, eei.getCode(), codeOffset, length)
265265
// sub the COPY fee
266-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'copy')).imul(length.divCeil(new BN(32))))
266+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'copy')).imul(length.divCeil(new BN(32))))
267267
},
268268
EXTCODESIZE: function (address, eei, cb) {
269269
var stateManager = eei._runState.stateManager
@@ -277,10 +277,10 @@ module.exports = {
277277
var stateManager = eei._runState.stateManager
278278
address = addressToBuffer(address)
279279

280-
// FIXME: for some reason this must come before subGas
280+
// FIXME: for some reason this must come before useGas
281281
eei.subMemUsage(memOffset, length)
282282
// copy fee
283-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'copy')).imul(length.divCeil(new BN(32))))
283+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'copy')).imul(length.divCeil(new BN(32))))
284284

285285
stateManager.getContractCode(address, function (err, code) {
286286
if (err) return cb(err)
@@ -322,7 +322,7 @@ module.exports = {
322322

323323
eei.memStore(memOffset, utils.toBuffer(eei.getReturnDataSize()), returnDataOffset, length, false)
324324
// sub the COPY fee
325-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'copy')).mul(length.divCeil(new BN(32))))
325+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'copy')).mul(length.divCeil(new BN(32))))
326326
},
327327
GASPRICE: function (eei) {
328328
return new BN(eei.getTxGasPrice())
@@ -419,27 +419,27 @@ module.exports = {
419419
},
420420
JUMP: function (dest, eei) {
421421
if (dest.gtn(eei.getCodeSize())) {
422-
eei.trap(ERROR.INVALID_JUMP + ' at ' + eei.describeLocation())
422+
eei.trap(ERROR.INVALID_JUMP + ' at ' + describeLocation(eei.getCode(), eei.getAddress(), eei._runState.programCounter))
423423
}
424424

425425
dest = dest.toNumber()
426426

427427
if (!eei.jumpIsValid(dest)) {
428-
eei.trap(ERROR.INVALID_JUMP + ' at ' + eei.describeLocation())
428+
eei.trap(ERROR.INVALID_JUMP + ' at ' + describeLocation(eei.getCode(), eei.getAddress(), eei._runState.programCounter))
429429
}
430430

431431
eei._runState.programCounter = dest
432432
},
433433
JUMPI: function (dest, cond, eei) {
434434
if (!cond.isZero()) {
435435
if (dest.gtn(eei.getCodeSize())) {
436-
eei.trap(ERROR.INVALID_JUMP + ' at ' + eei.describeLocation())
436+
eei.trap(ERROR.INVALID_JUMP + ' at ' + describeLocation(eei.getCode(), eei.getAddress(), eei._runState.programCounter))
437437
}
438438

439439
dest = dest.toNumber()
440440

441441
if (!eei.jumpIsValid(dest)) {
442-
eei.trap(ERROR.INVALID_JUMP + ' at ' + eei.describeLocation())
442+
eei.trap(ERROR.INVALID_JUMP + ' at ' + describeLocation(eei.getCode(), eei.getAddress(), eei._runState.programCounter))
443443
}
444444

445445
eei._runState.programCounter = dest
@@ -502,7 +502,7 @@ module.exports = {
502502

503503
const numOfTopics = eei._runState.opCode - 0xa0
504504
const mem = eei.memLoad(memOffset, memLength)
505-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'logTopic')).imuln(numOfTopics).iadd(memLength.muln(eei._runState._common.param('gasPrices', 'logData'))))
505+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'logTopic')).imuln(numOfTopics).iadd(memLength.muln(eei._runState._common.param('gasPrices', 'logData'))))
506506

507507
// add address
508508
var log = [eei.getAddress()]
@@ -564,7 +564,7 @@ module.exports = {
564564
}
565565

566566
// Deduct gas costs for hashing
567-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'sha3Word')).imul(length.divCeil(new BN(32))))
567+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'sha3Word')).imul(length.divCeil(new BN(32))))
568568
eei.checkCallMemCost(options, localOpts)
569569
eei.checkOutOfGas(options)
570570
eei.makeCall(options, localOpts, done)
@@ -595,7 +595,7 @@ module.exports = {
595595
}
596596

597597
if (!value.isZero()) {
598-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'callValueTransfer')))
598+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'callValueTransfer')))
599599
}
600600

601601
stateManager.accountIsEmpty(toAddress, function (err, empty) {
@@ -607,7 +607,7 @@ module.exports = {
607607
if (empty) {
608608
if (!value.isZero()) {
609609
try {
610-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'callNewAccount')))
610+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'callNewAccount')))
611611
} catch (e) {
612612
done(e.error)
613613
return
@@ -653,7 +653,7 @@ module.exports = {
653653
}
654654

655655
if (!value.isZero()) {
656-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'callValueTransfer')))
656+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'callValueTransfer')))
657657
}
658658

659659
eei.checkCallMemCost(options, localOpts)
@@ -790,7 +790,7 @@ module.exports = {
790790
if ((new BN(contract.balance)).gtn(0)) {
791791
if (empty) {
792792
try {
793-
eei.subGas(new BN(eei._runState._common.param('gasPrices', 'callNewAccount')))
793+
eei.useGas(new BN(eei._runState._common.param('gasPrices', 'callNewAccount')))
794794
} catch (e) {
795795
cb(e.error)
796796
return
@@ -825,3 +825,10 @@ module.exports = {
825825
})
826826
}
827827
}
828+
829+
function describeLocation (code, address, pc) {
830+
const hash = utils.keccak256(code).toString('hex')
831+
address = address.toString('hex')
832+
pc = pc - 1
833+
return hash + '/' + address + ':' + pc
834+
}

0 commit comments

Comments
 (0)