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

Refactor(eth-connector): Use Result return values instead of panicking #355

Merged
merged 1 commit into from
Nov 18, 2021

Conversation

birchmd
Copy link
Member

@birchmd birchmd commented Nov 16, 2021

The purpose of this PR is to remove panic calls (sdk_unwrap, assert, etc) from the connector logic. The reasons for this are:

  1. Continue to remove references to NEAR host functions from core logic. When we have removed all the host functions then we will be able to compile a standalone binary from the engine (as required for the standalone engine project).
  2. Panics should only happen at the edge of applications (unless they are fatal errors) where there is no other way to handle the error. In smart contract development this principle is often ignored (or alternatively all errors are considered fatal) because panicking in a contract only fails the current transaction. However in the standalone engine a panic will bring down the whole process which will be a big problem for the relayer waiting for an FFI response.
  3. Explicitly defining errors in the types of functions can make it easier to understand the logic of a module (especially something complicated like the eth-connector). It is clear in the types which calls are fallible and why.

@birchmd birchmd added A-connector Area: Issues that relate to the connector. A-standalone Area: the standalone engine EVM labels Nov 16, 2021
@mrLSD
Copy link
Member

mrLSD commented Nov 17, 2021

I have concerns about Option<...> type results instead of panics.
When we panic, we have details about the incident. For example:

assert_eq!(val.len(), 2);

will return details of what was wrong with length.
But if you just return None you lose that details, and I believe helpful to debug critical cases.

I suggest not to use Option as an error return at all, and not to ignore the errors that used to be in the assertion. And return Result type instead. It will extend our error flow.
Also detailed errors is good to debug incidents. Without this, out of the blue, we can create problems for ourselves.

About removing panics to error types - awesome idea!

@birchmd
Copy link
Member Author

birchmd commented Nov 17, 2021

Thanks for the suggestion @mrLSD . I have added error types for the message parsing functions.

Copy link
Contributor

@joshuajbouw joshuajbouw left a comment

Choose a reason for hiding this comment

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

So far, really good. Lots of QOL changes which is very welcomed. Just a few requests on my behalf.

engine/src/admin_controlled.rs Outdated Show resolved Hide resolved
engine/src/admin_controlled.rs Outdated Show resolved Hide resolved
engine/src/connector.rs Outdated Show resolved Hide resolved
engine/src/connector.rs Outdated Show resolved Hide resolved
engine/src/connector.rs Outdated Show resolved Hide resolved
engine/src/connector.rs Show resolved Hide resolved
engine/src/connector.rs Outdated Show resolved Hide resolved
engine/src/deposit_event.rs Outdated Show resolved Hide resolved
engine/src/engine.rs Outdated Show resolved Hide resolved
engine/src/lib.rs Outdated Show resolved Hide resolved
@birchmd
Copy link
Member Author

birchmd commented Nov 18, 2021

@joshuajbouw comments addresses. PTAL

@birchmd birchmd merged commit 3744d21 into develop Nov 18, 2021
@birchmd birchmd deleted the sdk-refactor-panic branch November 18, 2021 14:45
Copy link
Member

@mrLSD mrLSD left a comment

Choose a reason for hiding this comment

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

@birchmd LGTM!
Minor comments, that can be added to some another PR.

impl TryFrom<JsonValue> for StorageBalanceOfCallArgs {
type Error = error::ParseTypeFromJsonError;

fn try_from(v: JsonValue) -> Result<Self, error::ParseTypeFromJsonError> {
Copy link
Member

Choose a reason for hiding this comment

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

The correct way is: Result<Self, Self::Error>q

impl TryFrom<JsonValue> for TransferCallCallArgs {
type Error = error::ParseTypeFromJsonError;

fn try_from(v: JsonValue) -> Result<Self, error::ParseTypeFromJsonError> {
Copy link
Member

Choose a reason for hiding this comment

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

The correct way is: Result<Self, Self::Error>q

@@ -234,6 +243,7 @@ impl<I: IO + Copy> FungibleTokenOps<I> {
if let Some(memo) = memo {
sdk::log(&crate::prelude::format!("Memo: {}", memo));
Copy link
Member

Choose a reason for hiding this comment

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

Is it correct? May be: sdk::log!?

artob pushed a commit that referenced this pull request Dec 10, 2021
* Feat(engine): London hard fork support (#244)
* Fix(exit precompile): Address to refund in case of error is an argument (#311)
* Feat(engine): Make engine parametric in storage access (#314)
* Test verifying the EVM log returns the correct address (#341)
* Remove sdk::current_account_id usage from engine-precompiles (#346)
* Remove Default trait bound from engine IO (#342)
* Remove some sdk usage from core logic (#347)
* Factor out blockchain environment variable access as a trait (#349)
* Factor out NEAR promise host functions into a trait (#353)
* Borsh deserialized value field for call args (#351)
* Refactor(eth-connector): Use Result return values instead of panicking (#355)
* Gate all NEAR host functions behind the contract feature (#356)
* Bump @openzeppelin/contracts from 4.3.2 to 4.3.3 in /etc/eth-contracts
* Chore: Newtypes for gas (#344)
* Feat(standalone): Standalone (#345)
* Minor fixes to sdk refactor (#359)
* Refactor(engine): Move submit logic into engine module (#366)
* Feat(standalone): Storage backend (#375)
* NEAR random numbers from solidity contract (#368)
* Feat(standalone): EVM tracing via SputnikVM (#383)
* Feat(standalone): Bootstrap storage from relayer and state snapshots (#379)
* Feat(standalone): Structures and logic for keeping storage in sync with the blockchain (#382)
* Feat(standalone): Capture geth-like tracing from SputnikVM events (#384)
* Connector cleanup (#374)
* Remove betanet
* Fix(engine): original_storage bug fix; more tracing tests (#390)
* Increase NEAR Gas for ft_on_transfer (#389)

Co-authored-by: Andrew Bednoff <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Evgeny Ukhanov <[email protected]>
Co-authored-by: Marcelo Fornet <[email protected]>
Co-authored-by: Michael Birch <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-connector Area: Issues that relate to the connector. A-standalone Area: the standalone engine EVM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants