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

monorepo: remove isTruthy and isFalsy usage #2233

Closed
wants to merge 67 commits into from

Conversation

gabrocheleau
Copy link
Contributor

@gabrocheleau gabrocheleau commented Aug 25, 2022

This PR removes the isTruthy/isFalsy usage from the following packages

  • Block
  • Blockchain
  • Client
  • Common
  • Devp2p
  • Ethash
  • Evm
  • StateManager
  • Trie
  • Tx
  • Util
  • Vm

It also makes minor refactors/improvements when the updated types allow for it.

@codecov
Copy link

codecov bot commented Aug 25, 2022

Codecov Report

Merging #2233 (f721b7b) into master (2d60261) will increase coverage by 0.03%.
The diff coverage is 73.93%.

Impacted file tree graph

Flag Coverage Δ
block 92.91% <92.68%> (+0.13%) ⬆️
blockchain 88.47% <64.70%> (+0.08%) ⬆️
client 86.94% <70.33%> (-0.13%) ⬇️
common 98.09% <84.61%> (-0.01%) ⬇️
devp2p 92.48% <68.33%> (+0.23%) ⬆️
ethash ∅ <ø> (∅)
evm 79.25% <87.50%> (+0.14%) ⬆️
rlp ∅ <ø> (∅)
statemanager 88.47% <75.00%> (+0.30%) ⬆️
trie 89.43% <69.23%> (ø)
tx 97.98% <100.00%> (-0.01%) ⬇️
util 92.32% <20.00%> (-0.01%) ⬇️
vm 85.31% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@gabrocheleau gabrocheleau changed the title monorepo: remove is truthy is falsy monorepo: remove isTruthy and isFalsy usage Aug 25, 2022
Copy link
Contributor

@acolytec3 acolytec3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm almost halfway through but left a couple of comments. Nothing major.

@@ -33,7 +31,7 @@ export class LevelDB implements DB {
try {
value = await this._leveldb.get(key, ENCODING_OPTS)
} catch (error: any) {
if (isTruthy(error.notFound)) {
if (error.notFound !== undefined) {
// not found, returning null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does providing no return value mean we return null or undefined?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No return is treated as undefined by JS

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabrocheleau https://github.com/Level/abstract-level/blob/915ad1317694d0ce8c580b5ab85d81e1e78a3137/abstract-level.js#L309 maybe do an explicit true check here because it should only be true if leveldb really added it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then shouldn't we be explicitly returning null here if we want that? If it were me, I'd remove null from the monorepo entirely but we're not there yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably change that whole block to get the null return and be more correct on the check. With the below code we would just fall through to return value in every case and its default value is null already.

  async get(key: Buffer): Promise<Buffer | null> {
    let value = null
    try {
      value = await this._leveldb.get(key, ENCODING_OPTS)
    } catch (error: any) {
      // https://github.com/Level/abstract-level/blob/915ad1317694d0ce8c580b5ab85d81e1e78a3137/abstract-level.js#L309
      // This should be `true` if the error came from LevelDB
      // so we can check for `NOT true` to identify any non-404 errors
      if (error.notFound !== true) {
        throw error
      }
    }
    return value as Buffer
  }

@acolytec3 @gabrocheleau

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified that catch/try logic and am now explicitly checking for true. I've also typecasted to Buffer | null instead of just Buffer, which matches the return type of the function. In this particular case value is initialized as null, and get() throws when not found, so value would never be undefined.

One side note though is that the typecasting is necessary because get() returns string | Buffer. Are we confident that get won't ever return a string in this particular instance? If not, shouldn't we convert to buffer instead of typecasting?

Copy link
Contributor

@faustbrian faustbrian Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LevelDB generally returns what you insert and the encoding is set to buffer/binary so it'll be forced to always return Buffer. Unless there is a bug in LevelDB you'll always have Buffer after calling get.

@@ -271,7 +274,9 @@ export class Miner {
const txs = await this.service.txPool.txsByPriceAndNonce(baseFeePerGas)
this.config.logger.info(
`Miner: Assembling block from ${txs.length} eligible txs ${
isTruthy(baseFeePerGas) ? `(baseFee: ${baseFeePerGas})` : ''
typeof baseFeePerGas === 'bigint' && baseFeePerGas !== BigInt(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a silly question but we wouldn't the check to be baseFeePerGas > BigInt(0) right? I'm assuming there's reality in which it could be less in the miner's code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand the question, you mean a negative baseFeePerGas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, disregard. I wasn't thinking clearly when I made this comment,

packages/client/lib/miner/miner.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@acolytec3 acolytec3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more small comments, looks great overall though!

packages/devp2p/src/dns/dns.ts Outdated Show resolved Hide resolved
packages/devp2p/src/dpt/server.ts Outdated Show resolved Hide resolved
packages/devp2p/src/protocol/eth.ts Outdated Show resolved Hide resolved
packages/devp2p/src/protocol/protocol.ts Outdated Show resolved Hide resolved
packages/devp2p/src/rlpx/peer.ts Outdated Show resolved Hide resolved
@@ -30,11 +30,6 @@ const debugGas = createDebugLogger('vm:tx:gas')
* @ignore
*/
export async function runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
// tx is required
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some reason we're removing this check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, see comment from Jochem here: #2233 (comment)
basically tx is a non-optional option so opts.tx should never be undefined here

@holgerd77
Copy link
Member

@gabrocheleau thanks for this great work, totally fantastic! 🎉

Can we please have this initial PR split up after this first round of reviews and be submitted package-by-package as being done for the ESLint changes in #2147?

This is otherwise too many code changes on too many files in one PR and at the same time highly sensitive on every code change, which is somewhat of a toxic combination. This otherwise just naturally leads to too superficial reviews.

@gabrocheleau
Copy link
Contributor Author

@gabrocheleau thanks for this great work, totally fantastic! tada

Can we please have this initial PR split up after this first round of reviews and be submitted package-by-package as being done for the ESLint changes in #2147?

This is otherwise too many code changes on too many files in one PR and at the same time highly sensitive on every code change, which is somewhat of a toxic combination. This otherwise just naturally leads to too superficial reviews.

Agreed, done!

@gabrocheleau
Copy link
Contributor Author

Closing in favor of the package-specific PRs

@gabrocheleau gabrocheleau deleted the monorepo/remove-isTruthy-isFalsy branch August 30, 2022 16:36
@holgerd77
Copy link
Member

@gabrocheleau thanks for this great work, totally fantastic! tada
Can we please have this initial PR split up after this first round of reviews and be submitted package-by-package as being done for the ESLint changes in #2147?
This is otherwise too many code changes on too many files in one PR and at the same time highly sensitive on every code change, which is somewhat of a toxic combination. This otherwise just naturally leads to too superficial reviews.

Agreed, done!

🙏 Thanks a lot for the understanding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants