From ae08197f19c5e979c4c0b9bf3cb926190c08a759 Mon Sep 17 00:00:00 2001 From: Scorbajio Date: Sat, 27 Apr 2024 04:33:25 -0700 Subject: [PATCH] Remove unnecessary boolean comparisons when using isActivatedEIP (#3377) * Remove unnecessary boolean comparisons when using isActivatedEIP * Fix lint issues * Fix errors --- packages/block/src/block.ts | 6 ++-- packages/block/src/header.ts | 26 +++++++-------- packages/blockchain/src/blockchain.ts | 4 +-- packages/client/src/miner/miner.ts | 2 +- packages/evm/src/evm.ts | 4 +-- packages/evm/src/opcodes/EIP2929.ts | 12 +++---- packages/evm/src/opcodes/functions.ts | 2 +- packages/evm/src/opcodes/gas.ts | 48 +++++++++++++-------------- packages/tx/src/eip1559Transaction.ts | 2 +- packages/tx/src/eip4844Transaction.ts | 4 +-- packages/vm/src/buildBlock.ts | 8 ++--- packages/vm/src/runBlock.ts | 8 ++--- packages/vm/src/runTx.ts | 34 +++++++------------ packages/vm/test/api/runTx.spec.ts | 7 ++-- 14 files changed, 78 insertions(+), 89 deletions(-) diff --git a/packages/block/src/block.ts b/packages/block/src/block.ts index fe1a4df2b1..3f1fb972e3 100644 --- a/packages/block/src/block.ts +++ b/packages/block/src/block.ts @@ -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!) { @@ -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) { @@ -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}`) } diff --git a/packages/block/src/header.ts b/packages/block/src/header.ts index 9d25f2a9da..a2e5e41852 100644 --- a/packages/block/src/header.ts +++ b/packages/block/src/header.ts @@ -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' ) @@ -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) @@ -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) @@ -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) @@ -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' ) @@ -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!) } @@ -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 diff --git a/packages/blockchain/src/blockchain.ts b/packages/blockchain/src/blockchain.ts index acf4c64d76..db36e87733 100644 --- a/packages/blockchain/src/blockchain.ts +++ b/packages/blockchain/src/blockchain.ts @@ -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) @@ -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}`) diff --git a/packages/client/src/miner/miner.ts b/packages/client/src/miner/miner.ts index b70aa6e77d..f4fe4d78c5 100644 --- a/packages/client/src/miner/miner.ts +++ b/packages/client/src/miner/miner.ts @@ -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() } diff --git a/packages/evm/src/evm.ts b/packages/evm/src/evm.ts index a254246006..ac5664c581 100644 --- a/packages/evm/src/evm.ts +++ b/packages/evm/src/evm.ts @@ -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 @@ -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) } diff --git a/packages/evm/src/opcodes/EIP2929.ts b/packages/evm/src/opcodes/EIP2929.ts index d5c0d0712b..f249c1e7dd 100644 --- a/packages/evm/src/opcodes/EIP2929.ts +++ b/packages/evm/src/opcodes/EIP2929.ts @@ -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)) { @@ -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) @@ -54,7 +54,7 @@ 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) @@ -62,10 +62,10 @@ export function accessStorageEIP2929( // 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 @@ -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') diff --git a/packages/evm/src/opcodes/functions.ts b/packages/evm/src/opcodes/functions.ts index 1efde4d4b2..ee2066e3e6 100644 --- a/packages/evm/src/opcodes/functions.ts +++ b/packages/evm/src/opcodes/functions.ts @@ -627,7 +627,7 @@ export const handlers: Map = 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( diff --git a/packages/evm/src/opcodes/gas.ts b/packages/evm/src/opcodes/gas.ts index ad4a10f1ac..5f6dd26605 100644 --- a/packages/evm/src/opcodes/gas.ts +++ b/packages/evm/src/opcodes/gas.ts @@ -91,7 +91,7 @@ export const dynamicGasHandlers: Map { const address = addresstoBytes(runState.stack.peek()[0]) let charge2929Gas = true - if (common.isActivatedEIP(6800) === true) { + if (common.isActivatedEIP(6800)) { const balanceAddress = new Address(address) const coldAccessGas = runState.env.accessWitness!.touchAddressOnReadAndComputeGas( balanceAddress, @@ -103,7 +103,7 @@ export const dynamicGasHandlers: Map { } // Typed transaction specific setup tasks - if ( - opts.tx.supports(Capability.EIP2718TypedTransaction) && - this.common.isActivatedEIP(2718) === true - ) { + if (opts.tx.supports(Capability.EIP2718TypedTransaction) && this.common.isActivatedEIP(2718)) { // Is it an Access List transaction? - if (this.common.isActivatedEIP(2930) === false) { + if (!this.common.isActivatedEIP(2930)) { await this.evm.journal.revert() const msg = _errorMsg( 'Cannot run transaction: EIP 2930 is not activated.', @@ -152,10 +149,7 @@ export async function runTx(this: VM, opts: RunTxOpts): Promise { ) throw new Error(msg) } - if ( - opts.tx.supports(Capability.EIP1559FeeMarket) && - this.common.isActivatedEIP(1559) === false - ) { + if (opts.tx.supports(Capability.EIP1559FeeMarket) && !this.common.isActivatedEIP(1559)) { await this.evm.journal.revert() const msg = _errorMsg( 'Cannot run transaction: EIP 1559 is not activated.', @@ -190,7 +184,7 @@ export async function runTx(this: VM, opts: RunTxOpts): Promise { } throw e } finally { - if (this.common.isActivatedEIP(2929) === true) { + if (this.common.isActivatedEIP(2929)) { this.evm.journal.cleanJournal() } this.evm.stateManager.originalStorageCache.clear() @@ -245,7 +239,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise { ) } - if (this.common.isActivatedEIP(2929) === true) { + if (this.common.isActivatedEIP(2929)) { // Add origin and precompiles to warm addresses const activePrecompiles = this.evm.precompiles for (const [addressStr] of activePrecompiles.entries()) { @@ -256,7 +250,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise { // Note: in case we create a contract, we do this in EVMs `_executeCreate` (this is also correct in inner calls, per the EIP) this.evm.journal.addAlwaysWarmAddress(bytesToUnprefixedHex(tx.to.bytes)) } - if (this.common.isActivatedEIP(3651) === true) { + if (this.common.isActivatedEIP(3651)) { this.evm.journal.addAlwaysWarmAddress(bytesToUnprefixedHex(block.header.coinbase.bytes)) } } @@ -280,7 +274,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise { debugGas(`Subtracting base fee (${txBaseFee}) from gasLimit (-> ${gasLimit})`) } - if (this.common.isActivatedEIP(1559) === true) { + if (this.common.isActivatedEIP(1559)) { // EIP-1559 spec: // Ensure that the user was willing to at least pay the base fee // assert transaction.max_fee_per_gas >= block.base_fee_per_gas @@ -315,10 +309,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise { debug(`Sender's pre-tx balance is ${balance}`) } // EIP-3607: Reject transactions from senders with deployed code - if ( - this.common.isActivatedEIP(3607) === true && - !equalsBytes(fromAccount.codeHash, KECCAK256_NULL) - ) { + if (this.common.isActivatedEIP(3607) && !equalsBytes(fromAccount.codeHash, KECCAK256_NULL)) { const msg = _errorMsg('invalid sender address, address is not EOA (EIP-3607)', this, block, tx) throw new Error(msg) } @@ -428,7 +419,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise { } else { // Have to cast as legacy tx since EIP1559 tx does not have gas price gasPrice = (tx).gasPrice - if (this.common.isActivatedEIP(1559) === true) { + if (this.common.isActivatedEIP(1559)) { const baseFee = block.header.baseFeePerGas! inclusionFeePerGas = (tx).gasPrice - baseFee } @@ -582,10 +573,9 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise { minerAccount = new Account() } // add the amount spent on gas to the miner's account - results.minerValue = - this.common.isActivatedEIP(1559) === true - ? results.totalGasSpent * inclusionFeePerGas! - : results.amountSpent + results.minerValue = this.common.isActivatedEIP(1559) + ? results.totalGasSpent * inclusionFeePerGas! + : results.amountSpent minerAccount.balance += results.minerValue if (this.common.isActivatedEIP(6800)) { diff --git a/packages/vm/test/api/runTx.spec.ts b/packages/vm/test/api/runTx.spec.ts index e33415454d..d072511525 100644 --- a/packages/vm/test/api/runTx.spec.ts +++ b/packages/vm/test/api/runTx.spec.ts @@ -254,10 +254,9 @@ describe('runTx() -> successful API parameter usage', async () => { ? tx.maxPriorityFeePerGas : tx.maxFeePerGas - baseFee : tx.gasPrice - baseFee - const expectedCoinbaseBalance = - common.isActivatedEIP(1559) === true - ? result.totalGasSpent * inclusionFeePerGas - : result.amountSpent + const expectedCoinbaseBalance = common.isActivatedEIP(1559) + ? result.totalGasSpent * inclusionFeePerGas + : result.amountSpent assert.equal( coinbaseAccount!.balance,