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

Fix block time parsing #479

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions odra-casper/rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ prettycli = "0.1.1"
thiserror = "1.0.40"
bytes = "1.5.0"
anyhow = "1.0.86"
humantime = "2.1.0"
10 changes: 7 additions & 3 deletions odra-casper/rpc-client/src/casper_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Client for interacting with Casper node.
use std::time::SystemTime;
use std::{fs, path::PathBuf, str::from_utf8_unchecked, time::Duration};

use anyhow::Context;
Expand Down Expand Up @@ -263,16 +264,19 @@ impl CasperClient {
}
);
let result: serde_json::Value = self.post_request(request);
let result = result["result"]["last_added_block_info"]["timestamp"]
let result = result["last_added_block_info"]["timestamp"]
.as_str()
.unwrap_or_else(|| {
panic!(
"Couldn't get block time - malformed JSON response: {:?}",
result
)
});
let timestamp: u64 = result.parse().expect("Couldn't parse block time");
timestamp
let system_time = humantime::parse_rfc3339_weak(result).expect("Couldn't parse block time");
system_time
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_else(|_| panic!("Couldn't parse block time"))
.as_millis() as u64
Comment on lines +267 to +279
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor error handling in get_block_time to improve robustness.

The current implementation uses multiple unwrap_or_else which could be replaced with a more robust error handling strategy. Consider using a single error handling block at the end of the method to handle potential errors in a unified way. This would make the code cleaner and more maintainable.

- let result = result["last_added_block_info"]["timestamp"]
-     .as_str()
-     .unwrap_or_else(|| {
-         panic!(
-             "Couldn't get block time - malformed JSON response: {:?}",
-             result
-         )
-     });
- let system_time = humantime::parse_rfc3339_weak(result).expect("Couldn't parse block time");
- system_time
-     .duration_since(SystemTime::UNIX_EPOCH)
-     .unwrap_or_else(|_| panic!("Couldn't parse block time"))
-     .as_millis() as u64

+ let timestamp_str = result["last_added_block_info"]["timestamp"]
+     .as_str()
+     .ok_or_else(|| anyhow::anyhow!("Malformed JSON response: {:?}", result))?;
+ let system_time = humantime::parse_rfc3339_weak(timestamp_str)
+     .map_err(|_| anyhow::anyhow!("Couldn't parse block time"))?;
+ let block_time = system_time
+     .duration_since(SystemTime::UNIX_EPOCH)
+     .map_err(|_| anyhow::anyhow!("System time before UNIX EPOCH"))?
+     .as_millis() as u64;
+ block_time
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let result = result["last_added_block_info"]["timestamp"]
.as_str()
.unwrap_or_else(|| {
panic!(
"Couldn't get block time - malformed JSON response: {:?}",
result
)
});
let timestamp: u64 = result.parse().expect("Couldn't parse block time");
timestamp
let system_time = humantime::parse_rfc3339_weak(result).expect("Couldn't parse block time");
system_time
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_else(|_| panic!("Couldn't parse block time"))
.as_millis() as u64
let timestamp_str = result["last_added_block_info"]["timestamp"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("Malformed JSON response: {:?}", result))?;
let system_time = humantime::parse_rfc3339_weak(timestamp_str)
.map_err(|_| anyhow::anyhow!("Couldn't parse block time"))?;
let block_time = system_time
.duration_since(SystemTime::UNIX_EPOCH)
.map_err(|_| anyhow::anyhow!("System time before UNIX EPOCH"))?
.as_millis() as u64;
block_time

}

/// Get the event bytes from storage
Expand Down
Loading