-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VM: fix several internal todos (#1375)
- Loading branch information
Showing
11 changed files
with
306 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,89 @@ | ||
import Common from '@ethereumjs/common' | ||
import { BN } from 'ethereumjs-util' | ||
import { RunState } from './../interpreter' | ||
|
||
/** | ||
* Adjusts gas usage and refunds of SStore ops per EIP-1283 (Constantinople) | ||
* | ||
* @param {RunState} runState | ||
* @param {any} found | ||
* @param {Buffer} currentStorage | ||
* @param {Buffer} originalStorage | ||
* @param {Buffer} value | ||
* @param {Common} common | ||
*/ | ||
export function updateSstoreGasEIP1283(runState: RunState, found: any, value: Buffer) { | ||
const { original, current } = found | ||
if (current.equals(value)) { | ||
export function updateSstoreGasEIP1283( | ||
runState: RunState, | ||
currentStorage: Buffer, | ||
originalStorage: Buffer, | ||
value: Buffer, | ||
common: Common | ||
) { | ||
if (currentStorage.equals(value)) { | ||
// If current value equals new value (this is a no-op), 200 gas is deducted. | ||
runState.eei.useGas( | ||
new BN(runState._common.param('gasPrices', 'netSstoreNoopGas')), | ||
new BN(common.param('gasPrices', 'netSstoreNoopGas')), | ||
'EIP-1283 -> netSstoreNoopGas' | ||
) | ||
return | ||
} | ||
// If current value does not equal new value | ||
if (original.equals(current)) { | ||
if (originalStorage.equals(currentStorage)) { | ||
// If original value equals current value (this storage slot has not been changed by the current execution context) | ||
if (original.length === 0) { | ||
if (originalStorage.length === 0) { | ||
// If original value is 0, 20000 gas is deducted. | ||
return runState.eei.useGas( | ||
new BN(runState._common.param('gasPrices', 'netSstoreInitGas')), | ||
new BN(common.param('gasPrices', 'netSstoreInitGas')), | ||
'EIP-1283 -> netSstoreInitGas' | ||
) | ||
} | ||
if (value.length === 0) { | ||
// If new value is 0, add 15000 gas to refund counter. | ||
runState.eei.refundGas( | ||
new BN(runState._common.param('gasPrices', 'netSstoreClearRefund')), | ||
new BN(common.param('gasPrices', 'netSstoreClearRefund')), | ||
'EIP-1283 -> netSstoreClearRefund' | ||
) | ||
} | ||
// Otherwise, 5000 gas is deducted. | ||
return runState.eei.useGas( | ||
new BN(runState._common.param('gasPrices', 'netSstoreCleanGas')), | ||
new BN(common.param('gasPrices', 'netSstoreCleanGas')), | ||
'EIP-1283 -> netSstoreCleanGas' | ||
) | ||
} | ||
// If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses. | ||
if (original.length !== 0) { | ||
if (originalStorage.length !== 0) { | ||
// If original value is not 0 | ||
if (current.length === 0) { | ||
if (currentStorage.length === 0) { | ||
// If current value is 0 (also means that new value is not 0), remove 15000 gas from refund counter. We can prove that refund counter will never go below 0. | ||
runState.eei.subRefund( | ||
new BN(runState._common.param('gasPrices', 'netSstoreClearRefund')), | ||
new BN(common.param('gasPrices', 'netSstoreClearRefund')), | ||
'EIP-1283 -> netSstoreClearRefund' | ||
) | ||
} else if (value.length === 0) { | ||
// If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter. | ||
runState.eei.refundGas( | ||
new BN(runState._common.param('gasPrices', 'netSstoreClearRefund')), | ||
new BN(common.param('gasPrices', 'netSstoreClearRefund')), | ||
'EIP-1283 -> netSstoreClearRefund' | ||
) | ||
} | ||
} | ||
if (original.equals(value)) { | ||
if (originalStorage.equals(value)) { | ||
// If original value equals new value (this storage slot is reset) | ||
if (original.length === 0) { | ||
if (originalStorage.length === 0) { | ||
// If original value is 0, add 19800 gas to refund counter. | ||
runState.eei.refundGas( | ||
new BN(runState._common.param('gasPrices', 'netSstoreResetClearRefund')), | ||
new BN(common.param('gasPrices', 'netSstoreResetClearRefund')), | ||
'EIP-1283 -> netSstoreResetClearRefund' | ||
) | ||
} else { | ||
// Otherwise, add 4800 gas to refund counter. | ||
runState.eei.refundGas( | ||
new BN(runState._common.param('gasPrices', 'netSstoreResetRefund')), | ||
new BN(common.param('gasPrices', 'netSstoreResetRefund')), | ||
'EIP-1283 -> netSstoreResetRefund' | ||
) | ||
} | ||
} | ||
return runState.eei.useGas( | ||
new BN(runState._common.param('gasPrices', 'netSstoreDirtyGas')), | ||
new BN(common.param('gasPrices', 'netSstoreDirtyGas')), | ||
'EIP-1283 -> netSstoreDirtyGas' | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.