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

chore(util): enable strict-boolean-expressions rule for eslint #2148

Closed
wants to merge 8 commits into from
Prev Previous commit
Next Next commit
chore(blockchain): enable strict-boolean-expressions rule for eslint
  • Loading branch information
faustbrian committed Aug 16, 2022
commit 120a141e9eb75896a4eaa7bbb0319d0b05ae721f
15 changes: 14 additions & 1 deletion packages/blockchain/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
module.exports = require('../../config/eslint.js')
module.exports = {
extends: '../../config/eslint.js',
rules: {
'@typescript-eslint/strict-boolean-expressions': [
'error',
{
allowString: false,
allowNumber: false,
allowNullableObject: false,
allowAny: false,
},
],
},
}
38 changes: 20 additions & 18 deletions packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
@@ -98,7 +98,7 @@ export class Blockchain implements BlockchainInterface {
* {@link BlockchainOptions}.
*/
protected constructor(opts: BlockchainOptions = {}) {
if (opts.common) {
if (opts.common !== undefined) {
this._common = opts.common
} else {
const DEFAULT_CHAIN = Chain.Mainnet
@@ -114,10 +114,10 @@ export class Blockchain implements BlockchainInterface {
this._validateBlocks = opts.validateBlocks ?? true
this._customGenesisState = opts.genesisState

this.db = opts.db ? opts.db : new MemoryLevel()
this.db = opts.db !== undefined ? opts.db : new MemoryLevel()
this.dbManager = new DBManager(this.db, this._common)

if (opts.consensus) {
if (opts.consensus !== undefined) {
this.consensus = opts.consensus
} else {
switch (this._common.consensusAlgorithm()) {
@@ -154,7 +154,7 @@ export class Blockchain implements BlockchainInterface {

this._lock = new Semaphore(1)

if (opts.genesisBlock && !opts.genesisBlock.isGenesis()) {
if (opts.genesisBlock !== undefined && !opts.genesisBlock.isGenesis()) {
throw 'supplied block is not a genesis block'
}
}
@@ -200,7 +200,7 @@ export class Blockchain implements BlockchainInterface {
}
}

if (!genesisBlock) {
if (genesisBlock === undefined) {
let stateRoot
if (this._common.chainId() === BigInt(1) && this._customGenesisState === undefined) {
// For mainnet use the known genesis stateRoot to quicken setup
@@ -216,15 +216,15 @@ export class Blockchain implements BlockchainInterface {

// If the DB has a genesis block, then verify that the genesis block in the
// DB is indeed the Genesis block generated or assigned.
if (dbGenesisBlock && !genesisBlock.hash().equals(dbGenesisBlock.hash())) {
if (dbGenesisBlock !== undefined && !genesisBlock.hash().equals(dbGenesisBlock.hash())) {
throw new Error(
'The genesis block in the DB has a different hash than the provided genesis block.'
)
}

const genesisHash = genesisBlock.hash()

if (!dbGenesisBlock) {
if (dbGenesisBlock === undefined) {
// If there is no genesis block put the genesis block in the DB.
// For that TD, the BlockOrHeader, and the Lookups have to be saved.
const dbOps: DBOp[] = []
@@ -344,7 +344,7 @@ export class Blockchain implements BlockchainInterface {
*/
async getCanonicalHeadHeader(): Promise<BlockHeader> {
return await this.runWithLock<BlockHeader>(async () => {
if (!this._headHeaderHash) throw new Error('No head header set')
if (this._headHeaderHash === undefined) throw new Error('No head header set')
const block = await this._getBlock(this._headHeaderHash)
return block.header
})
@@ -355,7 +355,7 @@ export class Blockchain implements BlockchainInterface {
*/
async getCanonicalHeadBlock(): Promise<Block> {
return this.runWithLock<Block>(async () => {
if (!this._headBlockHash) throw new Error('No head block set')
if (this._headBlockHash === undefined) throw new Error('No head block set')
const block = this._getBlock(this._headBlockHash)
return block
})
@@ -463,10 +463,10 @@ export class Blockchain implements BlockchainInterface {
}

// set total difficulty in the current context scope
if (this._headHeaderHash) {
if (this._headHeaderHash !== undefined) {
currentTd.header = await this.getTotalDifficulty(this._headHeaderHash)
}
if (this._headBlockHash) {
if (this._headBlockHash !== undefined) {
currentTd.block = await this.getTotalDifficulty(this._headBlockHash)
}

@@ -758,7 +758,7 @@ export class Blockchain implements BlockchainInterface {
}
i++
const nextBlockNumber = block.header.number + BigInt(reverse ? -1 : 1)
if (i !== 0 && skip && i % (skip + 1) !== 0) {
if (i !== 0 && skip > 0 && i % (skip + 1) !== 0) {
return await nextBlock(nextBlockNumber)
}
blocks.push(block)
@@ -883,7 +883,7 @@ export class Blockchain implements BlockchainInterface {
ops.push(DBOp.del(DBTarget.HashToNumber, { blockHash }))
ops.push(DBOp.del(DBTarget.TotalDifficulty, { blockHash, blockNumber }))

if (!headHash) {
if (headHash === null) {
return
}

@@ -939,7 +939,8 @@ export class Blockchain implements BlockchainInterface {
try {
const nextBlock = await this._getBlock(nextBlockNumber)
this._heads[name] = nextBlock.hash()
const reorg = lastBlock ? lastBlock.hash().equals(nextBlock.header.parentHash) : false
const reorg =
lastBlock !== undefined ? lastBlock.hash().equals(nextBlock.header.parentHash) : false
lastBlock = nextBlock
await onBlock(nextBlock, reorg)
nextBlockNumber++
@@ -978,7 +979,7 @@ export class Blockchain implements BlockchainInterface {
* @param newHeader - the new block header
*/
private async findCommonAncestor(newHeader: BlockHeader) {
if (!this._headHeaderHash) throw new Error('No head header set')
if (this._headHeaderHash === undefined) throw new Error('No head header set')
const ancestorHeaders = new Set<BlockHeader>()

let { header } = await this._getBlock(this._headHeaderHash)
@@ -1228,7 +1229,8 @@ export class Blockchain implements BlockchainInterface {
* The genesis {@link Block} for the blockchain.
*/
get genesisBlock(): Block {
if (!this._genesisBlock) throw new Error('genesis block not set (init may not be finished)')
if (this._genesisBlock === undefined)
throw new Error('genesis block not set (init may not be finished)')
return this._genesisBlock
}

@@ -1246,7 +1248,7 @@ export class Blockchain implements BlockchainInterface {
stateRoot,
}
if (common.consensusType() === 'poa') {
if (common.genesis().extraData && common.chainName() !== 'kovan') {
if (common.genesis().extraData !== undefined && common.chainName() !== 'kovan') {
// Ensure exta data is populated from genesis data if provided
header.extraData = common.genesis().extraData
} else {
@@ -1262,7 +1264,7 @@ export class Blockchain implements BlockchainInterface {
* All values are provided as hex-prefixed strings.
*/
genesisState(): GenesisState {
if (this._customGenesisState) {
if (this._customGenesisState !== undefined) {
return this._customGenesisState
}
// Use require statements here in favor of import statements
14 changes: 7 additions & 7 deletions packages/blockchain/src/consensus/clique.ts
Original file line number Diff line number Diff line change
@@ -120,7 +120,7 @@ export class CliqueConsensus implements Consensus {
}

async validateConsensus(block: Block): Promise<void> {
if (!this.blockchain) {
if (this.blockchain === undefined) {
throw new Error('blockchain not provided')
}

@@ -151,7 +151,7 @@ export class CliqueConsensus implements Consensus {
}

async validateDifficulty(header: BlockHeader): Promise<void> {
if (!this.blockchain) {
if (this.blockchain === undefined) {
throw new Error('blockchain not provided')
}

@@ -213,15 +213,15 @@ export class CliqueConsensus implements Consensus {
* @hidden
*/
private async cliqueUpdateSignerStates(signerState?: CliqueSignerState) {
if (signerState) {
if (signerState !== undefined) {
this._cliqueLatestSignerStates.push(signerState)
}

// trim to CLIQUE_SIGNER_HISTORY_BLOCK_LIMIT
const limit = this.CLIQUE_SIGNER_HISTORY_BLOCK_LIMIT
const blockSigners = this._cliqueLatestBlockSigners
const lastBlockNumber = blockSigners[blockSigners.length - 1]?.[0]
if (lastBlockNumber) {
if (typeof lastBlockNumber === 'bigint') {
const blockLimit = lastBlockNumber - BigInt(limit)
const states = this._cliqueLatestSignerStates
const lastItem = states[states.length - 1]
@@ -257,7 +257,7 @@ export class CliqueConsensus implements Consensus {
*/
private async cliqueUpdateVotes(header?: BlockHeader) {
// Block contains a vote on a new signer
if (header && !header.coinbase.isZero()) {
if (header !== undefined && !header.coinbase.isZero()) {
const signer = header.cliqueSigner()
const beneficiary = header.coinbase
const nonce = header.nonce
@@ -377,7 +377,7 @@ export class CliqueConsensus implements Consensus {
const limit = this.CLIQUE_SIGNER_HISTORY_BLOCK_LIMIT
const blockSigners = this._cliqueLatestBlockSigners
const lastBlockNumber = blockSigners[blockSigners.length - 1]?.[0]
if (lastBlockNumber) {
if (typeof lastBlockNumber === 'bigint') {
const lastEpochBlockNumber =
lastBlockNumber -
(lastBlockNumber %
@@ -475,7 +475,7 @@ export class CliqueConsensus implements Consensus {
* @hidden
*/
private async cliqueUpdateLatestBlockSigners(header?: BlockHeader) {
if (header) {
if (header !== undefined) {
if (header.isGenesis()) {
return
}
4 changes: 2 additions & 2 deletions packages/blockchain/src/consensus/ethash.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ export class EthashConsensus implements Consensus {
}

async validateConsensus(block: Block): Promise<void> {
if (!this._ethash) {
if (this._ethash === undefined) {
throw new Error('blockchain not provided')
}
const valid = await this._ethash.verifyPOW(block)
@@ -33,7 +33,7 @@ export class EthashConsensus implements Consensus {
* @param header - header of block to be checked
*/
async validateDifficulty(header: BlockHeader) {
if (!this.blockchain) {
if (this.blockchain === undefined) {
throw new Error('blockchain not provided')
}
const parentHeader = (await this.blockchain.getBlock(header.parentHash)).header
3 changes: 2 additions & 1 deletion packages/blockchain/src/db/helpers.ts
Original file line number Diff line number Diff line change
@@ -45,7 +45,8 @@ function DBSetBlockOrHeader(blockBody: Block | BlockHeader): DBOp[] {

if (
isGenesis ||
(blockBody instanceof Block && (blockBody.transactions.length || blockBody.uncleHeaders.length))
(blockBody instanceof Block &&
(blockBody.transactions.length > 0 || blockBody.uncleHeaders.length > 0))
) {
const bodyValue = Buffer.from(RLP.encode(bufArrToArr(blockBody.raw()).slice(1)))
dbOps.push(
4 changes: 2 additions & 2 deletions packages/blockchain/src/db/manager.ts
Original file line number Diff line number Diff line change
@@ -192,10 +192,10 @@ export class DBManager {
}

let value = this._cache[cacheString].get(dbKey)
if (!value) {
if (value === undefined) {
value = await this._db.get(dbKey, dbOpts)

if (value) {
if (value !== undefined) {
this._cache[cacheString].set(dbKey, value)
}
}
4 changes: 2 additions & 2 deletions packages/blockchain/test/clique.spec.ts
Original file line number Diff line number Diff line change
@@ -146,12 +146,12 @@ tape('Clique: Initialization', (t) => {
let coinbase = Address.zero()
let nonce = CLIQUE_NONCE_DROP
let extraData = EXTRA_DATA
if (beneficiary) {
if (beneficiary !== undefined) {
coinbase = beneficiary[0].address
if (beneficiary[1]) {
nonce = CLIQUE_NONCE_AUTH
}
} else if (checkpointSigners) {
} else if (checkpointSigners !== undefined) {
extraData = Buffer.concat([
Buffer.alloc(32),
...checkpointSigners.map((s) => s.address.toBuffer()),
24 changes: 14 additions & 10 deletions packages/blockchain/test/reorg.spec.ts
Original file line number Diff line number Diff line change
@@ -159,36 +159,38 @@ tape('reorg tests', (t) => {

let signerStates = (blockchain.consensus as CliqueConsensus)._cliqueLatestSignerStates
t.ok(
!signerStates.find(
signerStates.find(
(s: any) => s[0] === BigInt(2) && s[1].find((a: Address) => a.equals(beneficiary1))
),
) === undefined,
'should not find reorged signer state'
)

let signerVotes = (blockchain.consensus as CliqueConsensus)._cliqueLatestVotes
t.ok(
!signerVotes.find(
signerVotes.find(
(v: any) =>
v[0] === BigInt(2) &&
v[1][0].equals(block1_low.header.cliqueSigner()) &&
v[1][1].equals(beneficiary1) &&
v[1][2].equals(CLIQUE_NONCE_AUTH)
),
) === undefined,
'should not find reorged clique vote'
)

let blockSigners = (blockchain.consensus as CliqueConsensus)._cliqueLatestBlockSigners
t.ok(
!blockSigners.find(
blockSigners.find(
(s: any) => s[0] === BigInt(1) && s[1].equals(block1_low.header.cliqueSigner())
),
) === undefined,
'should not find reorged block signer'
)

signerStates = (blockchain.consensus as CliqueConsensus)._cliqueLatestSignerStates
t.ok(
!!signerStates.find(
(s: any) => s[0] === BigInt(3) && s[1].find((a: Address) => a.equals(beneficiary2))
!(
signerStates.find(
(s: any) => s[0] === BigInt(3) && s[1].find((a: Address) => a.equals(beneficiary2))
) === undefined
),
'should find reorged signer state'
)
@@ -198,8 +200,10 @@ tape('reorg tests', (t) => {

blockSigners = (blockchain.consensus as CliqueConsensus)._cliqueLatestBlockSigners
t.ok(
!!blockSigners.find(
(s: any) => s[0] === BigInt(3) && s[1].equals(block3_high.header.cliqueSigner())
!(
blockSigners.find(
(s: any) => s[0] === BigInt(3) && s[1].equals(block3_high.header.cliqueSigner())
) === undefined
),
'should find reorged block signer'
)
4 changes: 2 additions & 2 deletions packages/blockchain/test/util.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import { Blockchain } from '../src'
import type { Level } from 'level'

export const generateBlocks = (numberOfBlocks: number, existingBlocks?: Block[]): Block[] => {
const blocks = existingBlocks ? existingBlocks : []
const blocks = existingBlocks !== undefined ? existingBlocks : []

const gasLimit = 8000000
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
@@ -42,7 +42,7 @@ export const generateBlocks = (numberOfBlocks: number, existingBlocks?: Block[])
}

export const generateBlockchain = async (numberOfBlocks: number, genesis?: Block): Promise<any> => {
const existingBlocks: Block[] = genesis ? [genesis] : []
const existingBlocks: Block[] = genesis !== undefined ? [genesis] : []
const blocks = generateBlocks(numberOfBlocks, existingBlocks)

const blockchain = await Blockchain.create({