Skip to content

Commit

Permalink
Remove unnecessary boolean comparisons when using isActivatedEIP (#3377)
Browse files Browse the repository at this point in the history
* Remove unnecessary boolean comparisons when using isActivatedEIP

* Fix lint issues

* Fix errors
  • Loading branch information
scorbajio authored Apr 27, 2024
1 parent 2a1124c commit ae08197
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 89 deletions.
6 changes: 3 additions & 3 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ export class Block {
// eslint-disable-next-line prefer-const
for (let [i, tx] of this.transactions.entries()) {
const errs = tx.getValidationErrors()
if (this.common.isActivatedEIP(1559) === true) {
if (this.common.isActivatedEIP(1559)) {
if (tx.supports(Capability.EIP1559FeeMarket)) {
tx = tx as FeeMarketEIP1559Transaction
if (tx.maxFeePerGas < this.header.baseFeePerGas!) {
Expand All @@ -572,7 +572,7 @@ export class Block {
}
}
}
if (this.common.isActivatedEIP(4844) === true) {
if (this.common.isActivatedEIP(4844)) {
if (tx instanceof BlobEIP4844Transaction) {
blobGasUsed += BigInt(tx.numBlobs()) * blobGasPerBlob
if (blobGasUsed > blobGasLimit) {
Expand All @@ -587,7 +587,7 @@ export class Block {
}
}

if (this.common.isActivatedEIP(4844) === true) {
if (this.common.isActivatedEIP(4844)) {
if (blobGasUsed !== this.header.blobGasUsed) {
errors.push(`invalid blobGasUsed expected=${this.header.blobGasUsed} actual=${blobGasUsed}`)
}
Expand Down
26 changes: 13 additions & 13 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class BlockHeader {
* EIP-4399: After merge to PoS, `mixHash` supplanted as `prevRandao`
*/
get prevRandao() {
if (this.common.isActivatedEIP(4399) === false) {
if (!this.common.isActivatedEIP(4399)) {
const msg = this._errorMsg(
'The prevRandao parameter can only be accessed when EIP-4399 is activated'
)
Expand Down Expand Up @@ -361,7 +361,7 @@ export class BlockHeader {
}

// Validation for EIP-1559 blocks
if (this.common.isActivatedEIP(1559) === true) {
if (this.common.isActivatedEIP(1559)) {
if (typeof this.baseFeePerGas !== 'bigint') {
const msg = this._errorMsg('EIP1559 block has no base fee field')
throw new Error(msg)
Expand All @@ -380,7 +380,7 @@ export class BlockHeader {
}
}

if (this.common.isActivatedEIP(4895) === true) {
if (this.common.isActivatedEIP(4895)) {
if (this.withdrawalsRoot === undefined) {
const msg = this._errorMsg('EIP4895 block has no withdrawalsRoot field')
throw new Error(msg)
Expand All @@ -393,7 +393,7 @@ export class BlockHeader {
}
}

if (this.common.isActivatedEIP(4788) === true) {
if (this.common.isActivatedEIP(4788)) {
if (this.parentBeaconBlockRoot === undefined) {
const msg = this._errorMsg('EIP4788 block has no parentBeaconBlockRoot field')
throw new Error(msg)
Expand Down Expand Up @@ -550,7 +550,7 @@ export class BlockHeader {
* Calculates the base fee for a potential next block
*/
public calcNextBaseFee(): bigint {
if (this.common.isActivatedEIP(1559) === false) {
if (!this.common.isActivatedEIP(1559)) {
const msg = this._errorMsg(
'calcNextBaseFee() can only be called with EIP1559 being activated'
)
Expand Down Expand Up @@ -671,26 +671,26 @@ export class BlockHeader {
this.nonce,
]

if (this.common.isActivatedEIP(1559) === true) {
if (this.common.isActivatedEIP(1559)) {
rawItems.push(bigIntToUnpaddedBytes(this.baseFeePerGas!))
}

if (this.common.isActivatedEIP(4895) === true) {
if (this.common.isActivatedEIP(4895)) {
rawItems.push(this.withdrawalsRoot!)
}

// in kaunstinen 2 verkle is scheduled after withdrawals, will eventually be post deneb hopefully
if (this.common.isActivatedEIP(6800) === true) {
if (this.common.isActivatedEIP(6800)) {
// execution witness is not mandatory part of the the block so nothing to push here
// but keep this comment segment for clarity regarding the same and move it according as per the
// HF sequence eventually planned
}

if (this.common.isActivatedEIP(4844) === true) {
if (this.common.isActivatedEIP(4844)) {
rawItems.push(bigIntToUnpaddedBytes(this.blobGasUsed!))
rawItems.push(bigIntToUnpaddedBytes(this.excessBlobGas!))
}
if (this.common.isActivatedEIP(4788) === true) {
if (this.common.isActivatedEIP(4788)) {
rawItems.push(this.parentBeaconBlockRoot!)
}

Expand Down Expand Up @@ -950,14 +950,14 @@ export class BlockHeader {
mixHash: bytesToHex(this.mixHash),
nonce: bytesToHex(this.nonce),
}
if (this.common.isActivatedEIP(1559) === true) {
if (this.common.isActivatedEIP(1559)) {
jsonDict.baseFeePerGas = bigIntToHex(this.baseFeePerGas!)
}
if (this.common.isActivatedEIP(4844) === true) {
if (this.common.isActivatedEIP(4844)) {
jsonDict.blobGasUsed = bigIntToHex(this.blobGasUsed!)
jsonDict.excessBlobGas = bigIntToHex(this.excessBlobGas!)
}
if (this.common.isActivatedEIP(4788) === true) {
if (this.common.isActivatedEIP(4788)) {
jsonDict.parentBeaconBlockRoot = bytesToHex(this.parentBeaconBlockRoot!)
}
return jsonDict
Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ export class Blockchain implements BlockchainInterface {
}

// check blockchain dependent EIP1559 values
if (header.common.isActivatedEIP(1559) === true) {
if (header.common.isActivatedEIP(1559)) {
// check if the base fee is correct
let expectedBaseFee
const londonHfBlock = this.common.hardforkBlock(Hardfork.London)
Expand All @@ -678,7 +678,7 @@ export class Blockchain implements BlockchainInterface {
}
}

if (header.common.isActivatedEIP(4844) === true) {
if (header.common.isActivatedEIP(4844)) {
const expectedExcessBlobGas = parentHeader.calcNextExcessBlobGas()
if (header.excessBlobGas !== expectedExcessBlobGas) {
throw new Error(`expected blob gas: ${expectedExcessBlobGas}, got: ${header.excessBlobGas}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/miner/miner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class Miner {
this.config.chainCommon.paramByEIP('gasConfig', 'initialBaseFee', 1559) ?? BIGINT_0
// Set initial EIP1559 block gas limit to 2x parent gas limit per logic in `block.validateGasLimit`
gasLimit = gasLimit * BIGINT_2
} else if (this.config.chainCommon.isActivatedEIP(1559) === true) {
} else if (this.config.chainCommon.isActivatedEIP(1559)) {
baseFeePerGas = parentBlock.header.calcNextBaseFee()
}

Expand Down
4 changes: 2 additions & 2 deletions packages/evm/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ export class EVM implements EVMInterface {
// fee for size of the return value
let totalGas = result.executionGasUsed
let returnFee = BIGINT_0
if (!result.exceptionError && this.common.isActivatedEIP(6800) === false) {
if (!result.exceptionError && !this.common.isActivatedEIP(6800)) {
returnFee =
BigInt(result.returnValue.length) * BigInt(this.common.param('gasPrices', 'createData'))
totalGas = totalGas + returnFee
Expand Down Expand Up @@ -905,7 +905,7 @@ export class EVM implements EVMInterface {

await this._emit('beforeMessage', message)

if (!message.to && this.common.isActivatedEIP(2929) === true) {
if (!message.to && this.common.isActivatedEIP(2929)) {
message.code = message.data
this.journal.addWarmedAddress((await this._generateAddress(message)).bytes)
}
Expand Down
12 changes: 6 additions & 6 deletions packages/evm/src/opcodes/EIP2929.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function accessAddressEIP2929(
chargeGas = true,
isSelfdestructOrAuthcall = false
): bigint {
if (common.isActivatedEIP(2929) === false) return BIGINT_0
if (!common.isActivatedEIP(2929)) return BIGINT_0

// Cold
if (!runState.interpreter.journal.isWarmedAddress(address)) {
Expand All @@ -29,7 +29,7 @@ export function accessAddressEIP2929(
// CREATE, CREATE2 opcodes have the address warmed for free.
// selfdestruct beneficiary address reads are charged an *additional* cold access
// if verkle not activated
if (chargeGas && common.isActivatedEIP(6800) === false) {
if (chargeGas && !common.isActivatedEIP(6800)) {
return common.param('gasPrices', 'coldaccountaccess')
}
// Warm: (selfdestruct beneficiary address reads are not charged when warm)
Expand All @@ -54,18 +54,18 @@ export function accessStorageEIP2929(
common: Common,
chargeGas = true
): bigint {
if (common.isActivatedEIP(2929) === false) return BIGINT_0
if (!common.isActivatedEIP(2929)) return BIGINT_0

const address = runState.interpreter.getAddress().bytes
const slotIsCold = !runState.interpreter.journal.isWarmedStorage(address, key)

// Cold (SLOAD and SSTORE)
if (slotIsCold) {
runState.interpreter.journal.addWarmedStorage(address, key)
if (chargeGas && common.isActivatedEIP(6800) === false) {
if (chargeGas && !common.isActivatedEIP(6800)) {
return common.param('gasPrices', 'coldsload')
}
} else if (chargeGas && (!isSstore || common.isActivatedEIP(6800) === true)) {
} else if (chargeGas && (!isSstore || common.isActivatedEIP(6800))) {
return common.param('gasPrices', 'warmstorageread')
}
return BIGINT_0
Expand All @@ -88,7 +88,7 @@ export function adjustSstoreGasEIP2929(
costName: string,
common: Common
): bigint {
if (common.isActivatedEIP(2929) === false) return defaultCost
if (!common.isActivatedEIP(2929)) return defaultCost

const address = runState.interpreter.getAddress().bytes
const warmRead = common.param('gasPrices', 'warmstorageread')
Expand Down
2 changes: 1 addition & 1 deletion packages/evm/src/opcodes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ export const handlers: Map<number, OpHandler> = new Map([
)
const key = setLengthLeft(bigIntToBytes(number % historyServeWindow), 32)

if (common.isActivatedEIP(6800) === true) {
if (common.isActivatedEIP(6800)) {
const { treeIndex, subIndex } = getTreeIndexesForStorageSlot(number)
// create witnesses and charge gas
const statelessGas = runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
Expand Down
Loading

0 comments on commit ae08197

Please sign in to comment.