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: Return BigInt for param() functionality #1846

Closed
holgerd77 opened this issue Apr 7, 2022 · 10 comments · Fixed by #1854
Closed

Common: Return BigInt for param() functionality #1846

holgerd77 opened this issue Apr 7, 2022 · 10 comments · Fixed by #1854

Comments

@holgerd77
Copy link
Member

There are four param-retrieving functions in Common:

  • param()
  • paramByEIP()
  • paramByHardfork()
  • paramByBlock()

These functions currently return a number if available, otherwise null (needs confirmation). When doing a search on usages of the functions it turns out that on 80%+ of the occurrences, the value is used in a BigInt context and therefore transformed to BigInt with a BigInt() constructor. In many of the remaining 20% cases there is an indirect need of a BigInt result (e.g. at the end of some calculation).

It would therefore make a lot of sense to directly return a BigInt on these function calls.

In addition all these functions have an any return type. This is highly suboptimal and the functions should be properly typed along with this transition work.

@jochem-brouwer
Copy link
Member

I vaguely remember that we have some values which are of type string? (Not entirely sure though)

@holgerd77
Copy link
Member Author

Ah, you are right, just found minerReward in byzantium, being "3000000000000000000".

So it seems this is just for big numbers, so this would indeed even some additional argument to go BigInt here. 🙂

@acolytec3 acolytec3 self-assigned this Apr 11, 2022
@acolytec3
Copy link
Contributor

I've started work on this and have a question. I don't think returning null if a param isn't found is a useful return value since Javascript implicitly coerces null to 0 when you apply mathematical operations to it. So in all the places where we might return null, that return value is mostly just used in some arithmetic operation so implicitly is a 0. When we switch the return value to bigint this implicit coercion no longer works and we get all these errors about a return value possibly being 0. To my way of thinking, we should just return either the parameter value or 0 since that's what's implicitly being used across our codebase already. Any thoughts?

@ryanio
Copy link
Contributor

ryanio commented Apr 13, 2022

another option would be to throw if it isn't found. it might be safer, and encourage ensuring only valid parameters are accessed. i imagine it would be more difficult to debug certain situations if a param was being returned as zero rather than alerting that it wasn't found.

@acolytec3
Copy link
Contributor

Possibly and I'll try it out locally and see what side affects it has. It seems like it could cause a much bigger set of needed rewrites to implement try/catch logic across the monorepo since this code is used in lots of places on the assumption it's returning either null or a number already. Will do some more looking.

@ryanio
Copy link
Contributor

ryanio commented Apr 13, 2022

yeah, well if null is handled like that then maybe that achieves the desired affect, but i see your point about null getting coerced to zero. i think handling null is easier than handling a thrown error though. i would think the code could be guarded (e.g. a common if eip enabled check) before accessing params that are only available after an upgrade.

@acolytec3
Copy link
Contributor

Here's a good example of what I'm talking about. The test expects the data fee returned to be zero but two layers down in the call stack, you have this call that expects a return value or 0 (not an error if the specific parameter doesn't exist). It's easy enough to fix this particular instance but I just don't have a sense for if throwing an error really the right solution here. If this example and however many others like it were already working with null being coerced to zero, should we intentionally change the behavior of something to require a try/catch in all use cases. I'll push my commits so far to a PR so we can look at the diff and think through the right approach here.

@holgerd77
Copy link
Member Author

Yeah, I would agree to think that throwing here is overblown. Can't we just return undefined (as it is) instead of null? This generally seems cleaner to me. This might need some code adoptions too, but these then actually should be in place since this is just the reality: a value can be present or not.

Returning 0 seems unclean and even dangerous to me since 0 also is an explicit value statement. In arithmetics this might have no side effects (but there it can very well have, e.g. for division cases), it others it might (will) have side effects in unlucky situations I would assume (even if it didn't show side effects in our existing code base, seems rather pure luck to me than "by-clean-structural-setup").

@holgerd77
Copy link
Member Author

Update: just some additional thought: I guess 0 values are sometimes (often?) just the - somewhat implicit - default values before something is set to a value greater than 0.

It might also help on these situations to add an explicit value 0 to chainstart.

Just as some thought, this might also help to get to an overall consistent solution.

holgerd77 pushed a commit that referenced this issue May 4, 2022
* Common: Return BigInt for param() functionality
Fixes #1846

* Change param getters to throw on non-existent value

* Fixes

* switch return to bigint or undefined

* Switch param getters to return undefined and fix common tests

* tx: changes to accomodate param getters

* More changes

* More adjustments

* Fix NaN bug in opcode fee builder

* Hack to fix EIP-2537 edge case

* client: fix test syntax

* lint

* Return 0 when param doesn't exist

* Spelling, dedup param usage

* Update `paramByEip` to allow undefined

* Hardcode EIP-2537 gas discount array

* uncomment test
holgerd77 pushed a commit that referenced this issue May 4, 2022
* Common: Return BigInt for param() functionality
Fixes #1846

* Change param getters to throw on non-existent value

* Fixes

* switch return to bigint or undefined

* Switch param getters to return undefined and fix common tests

* tx: changes to accomodate param getters

* More changes

* More adjustments

* Fix NaN bug in opcode fee builder

* Hack to fix EIP-2537 edge case

* client: fix test syntax

* lint

* Return 0 when param doesn't exist

* Spelling, dedup param usage

* Update `paramByEip` to allow undefined

* Hardcode EIP-2537 gas discount array

* uncomment test
@holgerd77
Copy link
Member Author

Closed by #1854

g11tech pushed a commit that referenced this issue Jun 2, 2022
* Common: Return BigInt for param() functionality
Fixes #1846

* Change param getters to throw on non-existent value

* Fixes

* switch return to bigint or undefined

* Switch param getters to return undefined and fix common tests

* tx: changes to accomodate param getters

* More changes

* More adjustments

* Fix NaN bug in opcode fee builder

* Hack to fix EIP-2537 edge case

* client: fix test syntax

* lint

* Return 0 when param doesn't exist

* Spelling, dedup param usage

* Update `paramByEip` to allow undefined

* Hardcode EIP-2537 gas discount array

* uncomment test
g11tech pushed a commit that referenced this issue Jun 2, 2022
* Common: Return BigInt for param() functionality
Fixes #1846

* Change param getters to throw on non-existent value

* Fixes

* switch return to bigint or undefined

* Switch param getters to return undefined and fix common tests

* tx: changes to accomodate param getters

* More changes

* More adjustments

* Fix NaN bug in opcode fee builder

* Hack to fix EIP-2537 edge case

* client: fix test syntax

* lint

* Return 0 when param doesn't exist

* Spelling, dedup param usage

* Update `paramByEip` to allow undefined

* Hardcode EIP-2537 gas discount array

* uncomment test
g11tech pushed a commit that referenced this issue Jun 2, 2022
* Common: Return BigInt for param() functionality
Fixes #1846

* Change param getters to throw on non-existent value

* Fixes

* switch return to bigint or undefined

* Switch param getters to return undefined and fix common tests

* tx: changes to accomodate param getters

* More changes

* More adjustments

* Fix NaN bug in opcode fee builder

* Hack to fix EIP-2537 edge case

* client: fix test syntax

* lint

* Return 0 when param doesn't exist

* Spelling, dedup param usage

* Update `paramByEip` to allow undefined

* Hardcode EIP-2537 gas discount array

* uncomment test
g11tech pushed a commit that referenced this issue Jun 3, 2022
* Common: Return BigInt for param() functionality
Fixes #1846

* Change param getters to throw on non-existent value

* Fixes

* switch return to bigint or undefined

* Switch param getters to return undefined and fix common tests

* tx: changes to accomodate param getters

* More changes

* More adjustments

* Fix NaN bug in opcode fee builder

* Hack to fix EIP-2537 edge case

* client: fix test syntax

* lint

* Return 0 when param doesn't exist

* Spelling, dedup param usage

* Update `paramByEip` to allow undefined

* Hardcode EIP-2537 gas discount array

* uncomment test
holgerd77 pushed a commit that referenced this issue Jun 8, 2022
* Common: Return BigInt for param() functionality
Fixes #1846

* Change param getters to throw on non-existent value

* Fixes

* switch return to bigint or undefined

* Switch param getters to return undefined and fix common tests

* tx: changes to accomodate param getters

* More changes

* More adjustments

* Fix NaN bug in opcode fee builder

* Hack to fix EIP-2537 edge case

* client: fix test syntax

* lint

* Return 0 when param doesn't exist

* Spelling, dedup param usage

* Update `paramByEip` to allow undefined

* Hardcode EIP-2537 gas discount array

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

Successfully merging a pull request may close this issue.

4 participants