Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: remove isTruthy and isFalsy #2255

Merged
merged 3 commits into from
Aug 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions packages/common/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TypeOutput, intToBuffer, isFalsy, isTruthy, toType } from '@ethereumjs/util'
import { TypeOutput, intToBuffer, toType } from '@ethereumjs/util'
import { buf as crc32Buffer } from 'crc-32'
import { EventEmitter } from 'events'

Expand Down Expand Up @@ -178,7 +178,7 @@ export class Common extends EventEmitter {
throw new Error(`Chain with ID ${chain} not supported`)
}

if (isTruthy(initializedChains[chain])) {
if (initializedChains[chain] !== undefined) {
return initializedChains[chain] as ChainConfig
}

Expand All @@ -191,7 +191,7 @@ export class Common extends EventEmitter {
this._chainParams = this.setChain(opts.chain)
this.DEFAULT_HARDFORK = this._chainParams.defaultHardfork ?? Hardfork.Merge
this._hardfork = this.DEFAULT_HARDFORK
if (isTruthy(opts.hardfork)) {
if (opts.hardfork !== undefined) {
this.setHardfork(opts.hardfork)
}
if (opts.eips) {
Expand Down Expand Up @@ -285,7 +285,7 @@ export class Common extends EventEmitter {
if (blockNumber >= BigInt(hf.block)) {
hardfork = hf.name as Hardfork
}
if (td && isTruthy(hf.ttd)) {
if (td && (typeof hf.ttd === 'string' || typeof hf.ttd === 'bigint')) {
if (td >= BigInt(hf.ttd)) {
minTdHF = hf.name
} else {
Expand All @@ -296,14 +296,14 @@ export class Common extends EventEmitter {
}
if (td) {
let msgAdd = `block number: ${blockNumber} (-> ${hardfork}), `
if (isTruthy(minTdHF)) {
if (minTdHF !== undefined) {
if (!this.hardforkGteHardfork(hardfork, minTdHF)) {
const msg = 'HF determined by block number is lower than the minimum total difficulty HF'
msgAdd += `total difficulty: ${td} (-> ${minTdHF})`
throw new Error(`${msg}: ${msgAdd}`)
}
}
if (isTruthy(maxTdHF)) {
if (maxTdHF !== undefined) {
if (!this.hardforkGteHardfork(maxTdHF, hardfork)) {
const msg = 'Maximum HF determined by total difficulty is lower than the block number HF'
msgAdd += `total difficulty: ${td} (-> ${maxTdHF})`
Expand Down Expand Up @@ -360,7 +360,7 @@ export class Common extends EventEmitter {
`${eip} cannot be activated on hardfork ${this.hardfork()}, minimumHardfork: ${minHF}`
)
}
if (isTruthy(EIPs[eip].requiredEIPs)) {
if (EIPs[eip].requiredEIPs !== undefined) {
for (const elem of EIPs[eip].requiredEIPs) {
if (!(eips.includes(elem) || this.isActivatedEIP(elem))) {
throw new Error(`${eip} requires EIP ${elem}, but is not included in the EIP list`)
Expand Down Expand Up @@ -408,11 +408,11 @@ export class Common extends EventEmitter {
const hfEIPs = hfChanges[1]['eips']
for (const eip of hfEIPs) {
const valueEIP = this.paramByEIP(topic, name, eip)
value = valueEIP !== undefined ? valueEIP : value
value = typeof valueEIP === 'bigint' ? valueEIP : value
}
// Parameter-inlining HF file (e.g. istanbul.json)
} else {
if (isFalsy(hfChanges[1][topic])) {
if (hfChanges[1][topic] === undefined) {
throw new Error(`Topic ${topic} not defined`)
}
if (hfChanges[1][topic][name] !== undefined) {
Expand Down Expand Up @@ -495,7 +495,7 @@ export class Common extends EventEmitter {
blockNumber = toType(blockNumber, TypeOutput.BigInt)
hardfork = hardfork ?? this._hardfork
const hfBlock = this.hardforkBlock(hardfork)
if (isTruthy(hfBlock) && blockNumber >= hfBlock) {
if (typeof hfBlock === 'bigint' && hfBlock !== BigInt(0) && blockNumber >= hfBlock) {
return true
}
return false
Expand Down Expand Up @@ -597,7 +597,7 @@ export class Common extends EventEmitter {
blockNumber = toType(blockNumber, TypeOutput.BigInt)
hardfork = hardfork ?? this._hardfork
const block = this.hardforkBlock(hardfork)
return isTruthy(block) ? block === blockNumber : false
return typeof block === 'bigint' && block !== BigInt(0) ? block === blockNumber : false
}

/**
Expand Down Expand Up @@ -809,9 +809,7 @@ export class Common extends EventEmitter {
}
if (hfChanges[0] === hardfork) break
}
return isTruthy(value)
? value
: (this._chainParams['consensus']['algorithm'] as ConsensusAlgorithm)
return value ?? (this._chainParams['consensus']['algorithm'] as ConsensusAlgorithm)
}

/**
Expand Down Expand Up @@ -839,9 +837,7 @@ export class Common extends EventEmitter {
}
if (hfChanges[0] === hardfork) break
}
return isTruthy(value)
? value
: this._chainParams['consensus'][this.consensusAlgorithm() as ConsensusAlgorithm]!
return value ?? this._chainParams['consensus'][this.consensusAlgorithm() as ConsensusAlgorithm]!
}

/**
Expand Down