From 28490fb8d39eb94f4ff588c00528514cebcc551e Mon Sep 17 00:00:00 2001 From: Gabriel Rocheleau Date: Mon, 29 Aug 2022 23:48:19 -0400 Subject: [PATCH] vm: remove isTruthy and isFalsy --- packages/vm/src/bloom/index.ts | 8 ++--- packages/vm/src/buildBlock.ts | 9 ++--- packages/vm/src/eei/vmState.ts | 10 +++--- packages/vm/src/runBlock.ts | 14 ++------ packages/vm/src/runTx.ts | 7 +--- packages/vm/src/vm.ts | 4 +-- .../tests/api/EIPs/eip-3074-authcall.spec.ts | 34 ++++--------------- .../vm/tests/api/istanbul/eip-1884.spec.ts | 4 +-- packages/vm/tests/util.ts | 18 ++++------ 9 files changed, 35 insertions(+), 73 deletions(-) diff --git a/packages/vm/src/bloom/index.ts b/packages/vm/src/bloom/index.ts index 0bdc5fd60c..78b8d529b8 100644 --- a/packages/vm/src/bloom/index.ts +++ b/packages/vm/src/bloom/index.ts @@ -1,4 +1,4 @@ -import { isTruthy, zeros } from '@ethereumjs/util' +import { zeros } from '@ethereumjs/util' import { keccak256 } from 'ethereum-cryptography/keccak' const BYTE_SIZE = 256 @@ -67,10 +67,8 @@ export class Bloom { * Bitwise or blooms together. */ or(bloom: Bloom) { - if (isTruthy(bloom)) { - for (let i = 0; i <= BYTE_SIZE; i++) { - this.bitvector[i] = this.bitvector[i] | bloom.bitvector[i] - } + for (let i = 0; i <= BYTE_SIZE; i++) { + this.bitvector[i] = this.bitvector[i] | bloom.bitvector[i] } } } diff --git a/packages/vm/src/buildBlock.ts b/packages/vm/src/buildBlock.ts index 0249657ce5..612c7f3054 100644 --- a/packages/vm/src/buildBlock.ts +++ b/packages/vm/src/buildBlock.ts @@ -2,7 +2,7 @@ import { Block } from '@ethereumjs/block' import { ConsensusType } from '@ethereumjs/common' import { RLP } from '@ethereumjs/rlp' import { Trie } from '@ethereumjs/trie' -import { Address, TypeOutput, isTruthy, toBuffer, toType } from '@ethereumjs/util' +import { Address, TypeOutput, toBuffer, toType } from '@ethereumjs/util' import { Bloom } from './bloom' import { calculateMinerReward, encodeReceipt, rewardAccount } from './runBlock' @@ -104,9 +104,10 @@ export class BlockBuilder { private async rewardMiner() { const minerReward = this.vm._common.param('pow', 'minerReward') const reward = calculateMinerReward(minerReward, 0) - const coinbase = isTruthy(this.headerData.coinbase) - ? new Address(toBuffer(this.headerData.coinbase)) - : Address.zero() + const coinbase = + this.headerData.coinbase !== undefined + ? new Address(toBuffer(this.headerData.coinbase)) + : Address.zero() await rewardAccount(this.vm.eei, coinbase, reward) } diff --git a/packages/vm/src/eei/vmState.ts b/packages/vm/src/eei/vmState.ts index bdf14f4fba..3837a5fef6 100644 --- a/packages/vm/src/eei/vmState.ts +++ b/packages/vm/src/eei/vmState.ts @@ -1,6 +1,6 @@ import { Chain, Common, Hardfork } from '@ethereumjs/common' import { ripemdPrecompileAddress } from '@ethereumjs/evm/dist/precompiles' -import { Account, Address, isTruthy, toBuffer } from '@ethereumjs/util' +import { Account, Address, toBuffer } from '@ethereumjs/util' import { debug as createDebugLogger } from 'debug' import type { EVMStateAccess } from '@ethereumjs/evm/dist/types' @@ -210,12 +210,12 @@ export class VmState implements EVMStateAccess { * Merges a storage map into the last item of the accessed storage stack */ private _accessedStorageMerge( - storageList: Map>[], + storageList: Map | undefined>[], storageMap: Map> ) { const mapTarget = storageList[storageList.length - 1] - if (isTruthy(mapTarget)) { + if (mapTarget !== undefined) { // Note: storageMap is always defined here per definition (TypeScript cannot infer this) for (const [addressString, slotSet] of storageMap) { const addressExists = mapTarget.get(addressString) @@ -255,10 +255,10 @@ export class VmState implements EVMStateAccess { const [balance, code, storage] = state const account = Account.fromAccountData({ balance }) await this.putAccount(addr, account) - if (isTruthy(code)) { + if (code !== undefined) { await this.putContractCode(addr, toBuffer(code)) } - if (isTruthy(storage)) { + if (storage !== undefined) { for (const [key, value] of storage) { await this.putContractStorage(addr, toBuffer(key), toBuffer(value)) } diff --git a/packages/vm/src/runBlock.ts b/packages/vm/src/runBlock.ts index db9cc40487..2e36a3d252 100644 --- a/packages/vm/src/runBlock.ts +++ b/packages/vm/src/runBlock.ts @@ -2,15 +2,7 @@ import { Block } from '@ethereumjs/block' import { ConsensusType, Hardfork } from '@ethereumjs/common' import { RLP } from '@ethereumjs/rlp' import { Trie } from '@ethereumjs/trie' -import { - Account, - Address, - bigIntToBuffer, - bufArrToArr, - intToBuffer, - isTruthy, - short, -} from '@ethereumjs/util' +import { Account, Address, bigIntToBuffer, bufArrToArr, intToBuffer, short } from '@ethereumjs/util' import { debug as createDebugLogger } from 'debug' import { Bloom } from './bloom' @@ -53,8 +45,8 @@ export async function runBlock(this: VM, opts: RunBlockOpts): Promise { - // tx is required - if (isFalsy(opts.tx)) { - throw new Error('invalid input, tx is required') - } - // create a reasonable default if no block is given opts.block = opts.block ?? Block.fromBlockData({}, { common: opts.tx.common }) diff --git a/packages/vm/src/vm.ts b/packages/vm/src/vm.ts index 45c1af2bd2..7ddc324f4a 100644 --- a/packages/vm/src/vm.ts +++ b/packages/vm/src/vm.ts @@ -2,7 +2,7 @@ import { Blockchain } from '@ethereumjs/blockchain' import { Chain, Common } from '@ethereumjs/common' import { EVM, getActivePrecompiles } from '@ethereumjs/evm' import { DefaultStateManager } from '@ethereumjs/statemanager' -import { Account, Address, TypeOutput, isTruthy, toType } from '@ethereumjs/util' +import { Account, Address, TypeOutput, toType } from '@ethereumjs/util' import AsyncEventEmitter = require('async-eventemitter') import { promisify } from 'util' @@ -255,7 +255,7 @@ export class VM { common: (eeiCopy as any)._common, evm: evmCopy, hardforkByBlockNumber: this._hardforkByBlockNumber ? true : undefined, - hardforkByTTD: isTruthy(this._hardforkByTTD) ? this._hardforkByTTD : undefined, + hardforkByTTD: this._hardforkByTTD, }) } diff --git a/packages/vm/tests/api/EIPs/eip-3074-authcall.spec.ts b/packages/vm/tests/api/EIPs/eip-3074-authcall.spec.ts index b5c962c5b4..d3598c24dd 100644 --- a/packages/vm/tests/api/EIPs/eip-3074-authcall.spec.ts +++ b/packages/vm/tests/api/EIPs/eip-3074-authcall.spec.ts @@ -7,7 +7,6 @@ import { bigIntToBuffer, bufferToBigInt, ecsign, - isTruthy, privateToAddress, setLengthLeft, toBuffer, @@ -170,33 +169,14 @@ function MSTORE(position: Buffer, value: Buffer) { * @returns - The bytecode to execute AUTHCALL */ function getAuthCallCode(data: AuthcallData) { - const ZEROS32 = zeros(32) - const gasLimitBuffer = setLengthLeft( - isTruthy(data.gasLimit) ? bigIntToBuffer(data.gasLimit) : ZEROS32, - 32 - ) + const gasLimitBuffer = setLengthLeft(bigIntToBuffer(data.gasLimit ?? BigInt(0)), 32) const addressBuffer = setLengthLeft(data.address.buf, 32) - const valueBuffer = setLengthLeft(isTruthy(data.value) ? bigIntToBuffer(data.value) : ZEROS32, 32) - const valueExtBuffer = setLengthLeft( - isTruthy(data.valueExt) ? bigIntToBuffer(data.valueExt) : ZEROS32, - 32 - ) - const argsOffsetBuffer = setLengthLeft( - isTruthy(data.argsOffset) ? bigIntToBuffer(data.argsOffset) : ZEROS32, - 32 - ) - const argsLengthBuffer = setLengthLeft( - isTruthy(data.argsLength) ? bigIntToBuffer(data.argsLength) : ZEROS32, - 32 - ) - const retOffsetBuffer = setLengthLeft( - isTruthy(data.retOffset) ? bigIntToBuffer(data.retOffset) : ZEROS32, - 32 - ) - const retLengthBuffer = setLengthLeft( - isTruthy(data.retLength) ? bigIntToBuffer(data.retLength) : ZEROS32, - 32 - ) + const valueBuffer = setLengthLeft(bigIntToBuffer(data.value ?? BigInt(0)), 32) + const valueExtBuffer = setLengthLeft(bigIntToBuffer(data.valueExt ?? BigInt(0)), 32) + const argsOffsetBuffer = setLengthLeft(bigIntToBuffer(data.argsOffset ?? BigInt(0)), 32) + const argsLengthBuffer = setLengthLeft(bigIntToBuffer(data.argsLength ?? BigInt(0)), 32) + const retOffsetBuffer = setLengthLeft(bigIntToBuffer(data.retOffset ?? BigInt(0)), 32) + const retLengthBuffer = setLengthLeft(bigIntToBuffer(data.retLength ?? BigInt(0)), 32) const PUSH32 = Buffer.from('7f', 'hex') const AUTHCALL = Buffer.from('f7', 'hex') const order = [ diff --git a/packages/vm/tests/api/istanbul/eip-1884.spec.ts b/packages/vm/tests/api/istanbul/eip-1884.spec.ts index 2d836e79c7..c0d2e7f296 100644 --- a/packages/vm/tests/api/istanbul/eip-1884.spec.ts +++ b/packages/vm/tests/api/istanbul/eip-1884.spec.ts @@ -1,6 +1,6 @@ import { Chain, Common, Hardfork } from '@ethereumjs/common' import { ERROR } from '@ethereumjs/evm/dist/exceptions' -import { Address, bufferToBigInt, isTruthy } from '@ethereumjs/util' +import { Address, bufferToBigInt } from '@ethereumjs/util' import * as tape from 'tape' import { VM } from '../../../src/vm' @@ -27,7 +27,7 @@ tape('Istanbul: EIP-1884', async (t) => { const common = new Common({ chain, hardfork }) const vm = await VM.create({ common }) - const balance = isTruthy(testCase.selfbalance) ? BigInt(testCase.selfbalance) : undefined + const balance = testCase.selfbalance !== undefined ? BigInt(testCase.selfbalance) : undefined const account = createAccount(BigInt(0), balance) await vm.stateManager.putAccount(addr, account) diff --git a/packages/vm/tests/util.ts b/packages/vm/tests/util.ts index c1f523cb53..5effb04bed 100644 --- a/packages/vm/tests/util.ts +++ b/packages/vm/tests/util.ts @@ -12,9 +12,7 @@ import { bigIntToBuffer, bufferToBigInt, bufferToHex, - isFalsy, isHexPrefixed, - isTruthy, setLengthLeft, stripHexPrefix, toBuffer, @@ -118,15 +116,15 @@ export function makeTx( opts?: TxOptions ): FeeMarketEIP1559Transaction | AccessListEIP2930Transaction | Transaction { let tx - if (isTruthy(txData.maxFeePerGas)) { + if (txData.maxFeePerGas !== undefined) { tx = FeeMarketEIP1559Transaction.fromTxData(txData, opts) - } else if (isTruthy(txData.accessLists)) { + } else if (txData.accessLists !== undefined) { tx = AccessListEIP2930Transaction.fromTxData(txData, opts) } else { tx = Transaction.fromTxData(txData, opts) } - if (isTruthy(txData.secretKey)) { + if (txData.secretKey !== undefined) { const privKey = toBuffer(txData.secretKey) return tx.sign(privKey) } @@ -157,7 +155,7 @@ export async function verifyPostConditions(state: any, testData: any, t: tape.Te const address = keyMap[key] delete keyMap[key] - if (isTruthy(testData)) { + if (testData !== undefined) { const promise = verifyAccountPostConditions(state, address, account, testData, t) queue.push(promise) } else { @@ -223,9 +221,7 @@ export function verifyAccountPostConditions( if (key === '0x') { key = '0x00' - acctData.storage['0x00'] = isTruthy(acctData.storage['0x00']) - ? acctData.storage['0x00'] - : acctData.storage['0x'] + acctData.storage['0x00'] = acctData.storage['0x00'] ?? acctData.storage['0x'] delete acctData.storage['0x'] } @@ -259,9 +255,9 @@ export function verifyAccountPostConditions( */ export function verifyGas(results: any, testData: any, t: tape.Test) { const coinbaseAddr = testData.env.currentCoinbase - const preBal = isTruthy(testData.pre[coinbaseAddr]) ? testData.pre[coinbaseAddr].balance : 0 + const preBal = testData.pre[coinbaseAddr] !== undefined ? testData.pre[coinbaseAddr].balance : 0 - if (isFalsy(testData.post[coinbaseAddr])) { + if (testData.post[coinbaseAddr] === undefined) { return }