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

Encapsulate field parsing #4

Merged
merged 3 commits into from
Sep 29, 2023
Merged

Encapsulate field parsing #4

merged 3 commits into from
Sep 29, 2023

Conversation

tuommaki
Copy link
Collaborator

@tuommaki tuommaki commented Sep 28, 2023

This PR implements TryFrom trait for CommitBlockInfoV1 and encapsulates the Eth token parsing into corresponding type.

@tuommaki tuommaki self-assigned this Sep 28, 2023
@@ -52,10 +35,10 @@ pub async fn init_eth_adapter(http_url: &str) -> (Provider<Http>, Contract) {
fn parse_calldata(commit_blocks_fn: &Function, calldata: &[u8]) -> Result<Vec<CommitBlockInfoV1>> {
let mut parsed_input = commit_blocks_fn
.decode_input(&calldata[4..])
.map_err(|e| ParseError::InvalidCalldata(e.to_string()))?;
.map_err(|e| state::ParseError::InvalidCalldata(e.to_string()))?;
Copy link
Collaborator Author

@tuommaki tuommaki Sep 28, 2023

Choose a reason for hiding this comment

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

These shared error types here are ugly as hell and don't feel right, but let's fix these separately, if that's ok?

Comment on lines +48 to +54
impl TryFrom<&abi::Token> for CommitBlockInfoV1 {
type Error = ParseError;

/// Try to parse Ethereum ABI token into CommitBlockInfoV1.
///
/// * `token` - ABI token of `CommitBlockInfo` type on Ethereum.
fn try_from(token: &abi::Token) -> Result<Self, Self::Error> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess this is what you meant by separating the parsing?

Copy link
Member

Choose a reason for hiding this comment

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

Precisely, this is much nicer and self-contained now!

@tuommaki tuommaki requested a review from zeapoz September 28, 2023 12:55
Copy link
Member

@zeapoz zeapoz left a comment

Choose a reason for hiding this comment

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

LGTM with a few adjustments 👍

src/state.rs Outdated Show resolved Hide resolved
src/state.rs Outdated Show resolved Hide resolved
Comment on lines +93 to 96
match CommitBlockInfoV1::try_from(data) {
Ok(blk) => res.push(blk),
Err(e) => println!("failed to parse commit block info: {}", e),
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
match CommitBlockInfoV1::try_from(data) {
Ok(blk) => res.push(blk),
Err(e) => println!("failed to parse commit block info: {}", e),
}
if let Ok(block) = CommitBlockInfoV1::try_from(data) {
res.push(block);
}

Don't think there's any need to print out the errors, I think we'd rather implement some tracing later down the line and log that error or even panic! since that function should only ever fail on malformed data.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd rather keep this. In my experience these can easily save hours of debugging during development. Often times, some very minor unrelated mistake happens and if there's no observability, one really has to debug weird behaviour until the root cause is found. Having this kind of debug print shortens time of diagnostics a lot. Referring to earlier error handling, I'd leave panic! completely out of this code 🙂

Comment on lines +48 to +54
impl TryFrom<&abi::Token> for CommitBlockInfoV1 {
type Error = ParseError;

/// Try to parse Ethereum ABI token into CommitBlockInfoV1.
///
/// * `token` - ABI token of `CommitBlockInfo` type on Ethereum.
fn try_from(token: &abi::Token) -> Result<Self, Self::Error> {
Copy link
Member

Choose a reason for hiding this comment

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

Precisely, this is much nicer and self-contained now!

Comment on lines +145 to +148
match decompress_bytecode(bytecode) {
Ok(bytecode) => smartcontracts.push(bytecode),
Err(e) => println!("failed to decompress bytecode: {}", e),
};
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
match decompress_bytecode(bytecode) {
Ok(bytecode) => smartcontracts.push(bytecode),
Err(e) => println!("failed to decompress bytecode: {}", e),
};
if let Ok(bytecode) = decompress_bytecode(bytecode) {
smartcontracts.push(bytecode);
}

Ditto

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same as earlier. 🙂

@tuommaki tuommaki merged commit 3ba7ecc into main Sep 29, 2023
4 checks passed
@tuommaki tuommaki deleted the refactor-state-parsing branch September 29, 2023 05:35
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