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(other): use type aliases where possible to improve clarity #859

Merged
merged 20 commits into from
Jun 12, 2024

Conversation

zerosnacks
Copy link
Member

@zerosnacks zerosnacks commented Jun 10, 2024

Motivation

Reduces magic values, reduces chance of error and makes it easier for end-users to see what value they are getting e.g. TxHash instead of FixedBytes<32> when watch()-ing a transaction. Main goal is to limit showing internal types and help with consistency / standardize fields / encourage native types (u64 vs U64) and sizes.

Related PR in core: alloy-rs/core#654 + alloy-rs/core#655

Solution

Proposed changes:

  • When in context of:
    • Transaction hash: B256 - Use TxHash
    • Block hash: B256 - Use BlockHash
    • Chain id: u64 - Use ChainId

Not used:

  • Transaction index: u64 - Use TxIndex
  • Block number: u64 - Use BlockNumber
  • Transaction nonce: u64 - Use TxNonce
  • Block timestamp: u64 - Use BlockTimestamp

Should have no impact and no logic changes should be present.

From testing it appears that rust-analyzer does not yet correctly deal with type aliases (long standing issue), as in it forwards the rust-lang/rust-analyzer#1666

ezgif-4-4510c3a5f6

Fortunately it does appear to be supported in RustRover:

ezgif-4-acc41ffdf0

PR also includes a number of QOL fixes

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

crates/genesis/src/lib.rs Outdated Show resolved Hide resolved
crates/genesis/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Member

@mattsse mattsse 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 very much against using aliases for nonce and timestamp, this is just confusing.

@zerosnacks
Copy link
Member Author

@mattsse

I'm very much against using aliases for nonce and timestamp, this is just confusing.

I've removed the number aliases, kept the hash aliases as discussed

Any existing references using the number-type aliases I've kept around, just avoided introducing any new ones.

@zerosnacks zerosnacks marked this pull request as ready for review June 11, 2024 17:53
@mattsse mattsse merged commit aa00cb3 into main Jun 12, 2024
22 checks passed
@mattsse mattsse deleted the zerosnacks/return-alias-of-tx-hash-for-watch branch June 12, 2024 10:59
ben186 pushed a commit to ben186/alloy that referenced this pull request Jul 27, 2024
…oy-rs#859)

* improve type aliasing on untyped types

* prefer type alias

* clean up

* fix typo

* fix typo

* add TxNonce and BlockTimestamp

* fix clippy, keep impl From as is

* do not use TxNonce for EIP1186AccountProofResponse as type is expected to be U64, not u64

* avoid aliasing for genesis as it is often read from file and rarely interacted with dynamically

* fix clippy

* revert consensus, engine related type changes

* prefer import types as long as they do not clash

* fix docs link

* do not use TxNonce and BlockTimestamp aliases

* rule: alias for hash types, not number types

* remove newly added BlockNumber references, leave the old ones as is

* remove TxIndex alias use
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.

2 participants