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

Align core types with execution spec #733

Merged
merged 13 commits into from
Sep 29, 2024
Merged

Align core types with execution spec #733

merged 13 commits into from
Sep 29, 2024

Conversation

arnetheduck
Copy link
Member

Since these types were written, we've gained an executable spec:

https://github.com/ethereum/execution-specs

This PR aligns some of the types we use with this spec to simplify comparisons and cross-referencing.

Using a distinct type is a tradeoff between nim ergonomics, type safety and the ability to work around nim quirks and stdlib weaknesses.

In particular, it allows us to overload common functions such as hash with correct and performant versions as well as maintain control over string conversions etc at the cost of a little bit of ceremony when instantiating them.

Apart from distinct byte types, Hash32, is introduced in lieu of the existing Hash256, again aligning this commonly used type with the spec which picks bytes rather than bits in the name.

Copy link
Contributor

@kdeme kdeme 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 all in for these changes that align more with the exec. spec., and have consistency with nimbus-eth2.

@arnetheduck arnetheduck force-pushed the wip-types branch 2 times, most recently from a157a70 to f7a788d Compare September 23, 2024 16:51
eth/common/eth_address.nim Outdated Show resolved Hide resolved
@arnetheduck arnetheduck force-pushed the wip-types branch 3 times, most recently from 470c8ac to eed6dd1 Compare September 24, 2024 12:15
Since these types were written, we've gained an executable spec:

https://github.com/ethereum/execution-specs

This PR aligns some of the types we use with this spec to simplify
comparisons and cross-referencing.

Using a `distinct` type is a tradeoff between nim ergonomics, type
safety and the ability to work around nim quirks and stdlib weaknesses.

In particular, it allows us to overload common functions such as `hash`
with correct and performant versions as well as maintain control over
string conversions etc at the cost of a little bit of ceremony when
instantiating them.

Apart from distinct byte types, `Hash32`, is introduced in lieu of the
existing `Hash256`, again aligning this commonly used type with the spec
which picks bytes rather than bits in the name.
eth/common/base.nim Outdated Show resolved Hide resolved

template default*[N](T: type FixedBytes[N]): T =
# Avoid bad codegen where fixed bytes are zeroed byte-by-byte at call site
const def = system.default(T)
Copy link
Contributor

Choose a reason for hiding this comment

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

noticed anonConst in stew/objects, useful pattern to enshrine in general

Copy link
Member Author

Choose a reason for hiding this comment

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

hm, yeah, didn't know about that one - one thing though is that it generates a new, separate const on every instantiation even if it's the same value/type which I'm not too happy about - ie each default(Bytes32) would result in a separately declared constant in the generated code which is not great

Copy link
Contributor

Choose a reason for hiding this comment

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

In theory especially with LTO these could be folded into a single constant. My understanding is that this is a typical linker optimization

Copy link
Member Author

Choose a reason for hiding this comment

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

depends - for that to happen, it must determine that the address of the variable is not used for uniqueness / comparisons which doesn't necessarily happen and/or requires special flags.

https://stackoverflow.com/a/53077229

eth/common/base.nim Outdated Show resolved Hide resolved
eth/common/base.nim Outdated Show resolved Hide resolved
arnetheduck added a commit to status-im/nimbus-eth1 that referenced this pull request Sep 26, 2024
status-im/nim-eth#733 updates `Hash256` to
become an array instead of an object - unfortunately, nim does not allow
constructing arrays with `name()`, so this PR changes it to `default`
which works with both.
arnetheduck added a commit to status-im/nimbus-eth1 that referenced this pull request Sep 26, 2024
* init style for Hash256

status-im/nim-eth#733 updates `Hash256` to
become an array instead of an object - unfortunately, nim does not allow
constructing arrays with `name()`, so this PR changes it to `default`
which works with both.

* lint
arnetheduck added a commit to status-im/nimbus-eth2 that referenced this pull request Sep 26, 2024
Minimal changes neede for compatiblity with
status-im/nim-eth#733 which aligns core types
with execution spec.
@arnetheduck arnetheduck marked this pull request as ready for review September 26, 2024 16:05
@arnetheduck arnetheduck merged commit 6bd6bae into master Sep 29, 2024
18 checks passed
@arnetheduck arnetheduck deleted the wip-types branch September 29, 2024 08:52
arnetheduck added a commit to status-im/nimbus-eth2 that referenced this pull request Sep 29, 2024
Minimal changes needed for compatiblity with
status-im/nim-eth#733 which aligns core types
with execution spec.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants