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

Remove chain::Id completely #3173

Merged
merged 9 commits into from
Dec 23, 2024
Merged

Remove chain::Id completely #3173

merged 9 commits into from
Dec 23, 2024

Conversation

sunce86
Copy link
Contributor

@sunce86 sunce86 commented Dec 19, 2024

Description

Matching on chain::Id is cumbersome because it lacks guarantees about its values. For example, chain::Id can be constructed from a u64 or U256 without error handling for unsupported chains.

So to match properly, we need to first convert chain::Id to chain::Chain, and then also handle failure of conversion.

This proposal suggests replacing chain::Id entirely with chain::Chain, which is already well-encapsulated. This change is feasible because all scenarios where chain::Chain cannot be constructed are handled appropriately—using an early .expect() whenever a service is restarted.

Changes

  • Remove chain::Id and use chain::Chain instead
  • Had to add Hardhat chain to make driver tests pass.

How to test

Existing tests should verify no bugs were introduced.

@sunce86 sunce86 self-assigned this Dec 19, 2024
@sunce86 sunce86 requested a review from a team as a code owner December 19, 2024 11:59
@sunce86 sunce86 changed the title Remove chain::Id completely [ON HOLD] Remove chain::Id completely Dec 19, 2024
@sunce86 sunce86 changed the title [ON HOLD] Remove chain::Id completely Remove chain::Id completely Dec 19, 2024
@squadgazzz
Copy link
Contributor

Had to add Hardhat chain to make driver tests pass.

I didn't quite get from the description and the code where the hardhat chain id comes from.

@@ -290,6 +294,7 @@ impl Error {
}
Error::GasPrice(_) => false,
Error::AccessList(_) => true,
Error::UnsupportedChain => false,
Copy link
Contributor

Choose a reason for hiding this comment

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

A new error variant gets added which can never appear here and yet we have to handle this case. This is a code smell and indicates that these functions should return different errors.
Something for a future refactor, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can drop UnsupportedChain error, as it can appear only during application startup when wrong configuration is used (url parameter) and instead just panic with meaningful message in Rpc::new() function when Chain::try_from() fails.

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 don't have a strong opinion here. If you look at how the same constructor is used in autopilot, you can see that .expect() is used when the Chain::try_from is called in run.rs (so when we are aware of our surrounding and we explicitly want to exit), and error type is used inside Rpc::new because we don't know when and how this function will be called.

But I also agree that this error is rarely hit. If we agree to panic everywhere, then we can remove ChainIdNotSupported error from chain crate altogether. This is dangerous since we might want to try to build the Chain object from unreliable source that could give garbage, but it's not the case now.

Copy link
Contributor

Choose a reason for hiding this comment

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

So another option would be to introduce new error type dedicated for Rpc object, for instance RpcError, which will contain only one variant ChainIdNotSupported, and caller of the constructor will decide if want to panic or map error to other type.
Alternatively constructor can return boolean type error as there is only one function which can fail with one error code.

Copy link
Contributor

Choose a reason for hiding this comment

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

So another option would be to introduce new error type dedicated for Rpc object, for instance RpcError, which will contain only one variant ChainIdNotSupported, and caller of the constructor will decide if want to panic or map error to other type.

That seems reasonable. @sunce86 do you mind implementing it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I created a new error type

crates/shared/src/sources/mod.rs Outdated Show resolved Hide resolved
crates/chain/src/lib.rs Outdated Show resolved Hide resolved
@sunce86
Copy link
Contributor Author

sunce86 commented Dec 19, 2024

Had to add Hardhat chain to make driver tests pass.

I didn't quite get from the description and the code where the hardhat chain id comes from.

https://hardhat.org/hardhat-network/docs/reference#chainid

crates/chain/src/lib.rs Show resolved Hide resolved
@@ -290,6 +294,7 @@ impl Error {
}
Error::GasPrice(_) => false,
Error::AccessList(_) => true,
Error::UnsupportedChain => false,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can drop UnsupportedChain error, as it can appear only during application startup when wrong configuration is used (url parameter) and instead just panic with meaningful message in Rpc::new() function when Chain::try_from() fails.

crates/driver/src/infra/config/file/load.rs Outdated Show resolved Hide resolved
@squadgazzz
Copy link
Contributor

squadgazzz commented Dec 20, 2024

hardhat.org/hardhat-network/docs/reference#chainid

I mean, why do we need It in the code now? Some of the tests use this chain id implicitly?

@sunce86
Copy link
Contributor Author

sunce86 commented Dec 20, 2024

hardhat.org/hardhat-network/docs/reference#chainid

I mean, why do we need It in the code now? Some of the tests use this chain id implicitly?

Driver tests do. ☝️

Copy link
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

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

LGTM

@sunce86 sunce86 enabled auto-merge (squash) December 23, 2024 13:55
@sunce86 sunce86 merged commit 104722a into main Dec 23, 2024
10 of 11 checks passed
@sunce86 sunce86 deleted the remove-chain-id branch December 23, 2024 14:00
@github-actions github-actions bot locked and limited conversation to collaborators Dec 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants