Skip to content

Commit

Permalink
common: apply strict-boolean-expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrocheleau committed Jul 8, 2022
1 parent f8209ad commit dddcc97
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
35 changes: 14 additions & 21 deletions packages/common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class Common extends EventEmitter {
throw new Error(`Chain with ID ${chain} not supported`)
}

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

Expand All @@ -188,7 +188,7 @@ export class Common extends EventEmitter {
this._chainParams = this.setChain(opts.chain)
this.DEFAULT_HARDFORK = this._chainParams.defaultHardfork ?? Hardfork.London
this._hardfork = this.DEFAULT_HARDFORK
if (opts.hardfork) {
if (typeof opts.hardfork !== 'undefined') {
this.setHardfork(opts.hardfork)
}
if (opts.eips) {
Expand Down Expand Up @@ -277,7 +277,7 @@ export class Common extends EventEmitter {
if (blockNumber >= BigInt(hf.block)) {
hardfork = hf.name as Hardfork
}
if (td && hf.td) {
if (td && typeof hf.td !== 'undefined') {
if (td >= BigInt(hf.td)) {
minTdHF = hf.name
} else {
Expand All @@ -288,14 +288,14 @@ export class Common extends EventEmitter {
}
if (td) {
let msgAdd = `block number: ${blockNumber} (-> ${hardfork}), `
if (minTdHF) {
if (typeof 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 (maxTdHF) {
if (typeof 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 @@ -352,7 +352,7 @@ export class Common extends EventEmitter {
`${eip} cannot be activated on hardfork ${this.hardfork()}, minimumHardfork: ${minHF}`
)
}
if (EIPs[eip].requiredEIPs) {
if (typeof EIPs[eip].requiredEIPs !== 'undefined') {
;(EIPs[eip].requiredEIPs as number[]).forEach((elem) => {
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 @@ -404,7 +404,7 @@ export class Common extends EventEmitter {
}
// Paramater-inlining HF file (e.g. istanbul.json)
} else {
if (!hfChanges[1][topic]) {
if (typeof hfChanges[1][topic] === 'undefined') {
throw new Error(`Topic ${topic} not defined`)
}
if (hfChanges[1][topic][name] !== undefined) {
Expand All @@ -413,8 +413,7 @@ export class Common extends EventEmitter {
}
if (hfChanges[0] === hardfork) break
}
if (!value) return BigInt(0)
return BigInt(value)
return BigInt(value ?? 0)
}

/**
Expand Down Expand Up @@ -470,7 +469,7 @@ export class Common extends EventEmitter {
for (const hfChanges of HARDFORK_CHANGES) {
const hf = hfChanges[1]
if (this.gteHardfork(hf['name']) && 'eips' in hf) {
if (hf['eips'].includes(eip)) {
if ((hf['eips'] as number[]).includes(eip)) {
return true
}
}
Expand All @@ -488,7 +487,7 @@ export class Common extends EventEmitter {
blockNumber = toType(blockNumber, TypeOutput.BigInt)
hardfork = hardfork ?? this._hardfork
const hfBlock = this.hardforkBlock(hardfork)
if (hfBlock && blockNumber >= hfBlock) {
if (hfBlock !== null && blockNumber >= hfBlock) {
return true
}
return false
Expand Down Expand Up @@ -572,7 +571,7 @@ export class Common extends EventEmitter {
blockNumber = toType(blockNumber, TypeOutput.BigInt)
hardfork = hardfork ?? this._hardfork
const block = this.hardforkBlock(hardfork)
return block ? block === blockNumber : false
return block !== null ? block === blockNumber : false
}

/**
Expand Down Expand Up @@ -762,10 +761,7 @@ export class Common extends EventEmitter {
}
if (hfChanges[0] === hardfork) break
}
if (value) {
return value
}
return this._chainParams['consensus']!['type']
return value ?? this._chainParams['consensus']!['type']
}

/**
Expand All @@ -787,10 +783,7 @@ export class Common extends EventEmitter {
}
if (hfChanges[0] === hardfork) break
}
if (value) {
return value
}
return this._chainParams['consensus']!['algorithm'] as ConsensusAlgorithm
return value ?? (this._chainParams['consensus']!['algorithm'] as ConsensusAlgorithm)
}

/**
Expand Down Expand Up @@ -818,7 +811,7 @@ export class Common extends EventEmitter {
}
if (hfChanges[0] === hardfork) break
}
if (value) {
if (typeof value !== 'undefined') {
return value
}
const consensusAlgorithm = this.consensusAlgorithm()
Expand Down
2 changes: 1 addition & 1 deletion packages/common/tests/hardforks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ tape('[Common]: Hardfork logic', function (t: tape.Test) {
for (const [chain, genesisHash] of chains) {
c = new Common({ chain })
for (const hf of c.hardforks()) {
if (hf.forkHash && hf.forkHash !== null) {
if (typeof hf.forkHash === 'string') {
const msg = `Verify forkHash calculation for: ${Chain[chain]} -> ${hf.name}`
st.equal(c._calcForkHash(hf.name, genesisHash), hf.forkHash, msg)
}
Expand Down

0 comments on commit dddcc97

Please sign in to comment.