Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmus-kirk committed Mar 12, 2024
1 parent e9b7158 commit 5a77437
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) mod timestamp_millis {
) -> Result<chrono::DateTime<chrono::Utc>, D::Error> {
let millis = i64::deserialize(des)?;
Ok(chrono::DateTime::<chrono::Utc>::from(std::time::UNIX_EPOCH)
+ chrono::TimeDelta::try_milliseconds(millis).expect("Timestamp out of bounds!"))
+ chrono::TimeDelta::try_milliseconds(millis).ok_or(serde::de::Error::custom("Timestamp out of bounds!"))?)
}
}

Expand Down Expand Up @@ -121,6 +121,6 @@ pub(crate) mod duration_millis {
des: D,
) -> Result<chrono::Duration, D::Error> {
let millis = i64::deserialize(des)?;
Ok(chrono::Duration::try_milliseconds(millis).expect("Duration out of bounds!"))
Ok(chrono::Duration::try_milliseconds(millis).ok_or(serde::de::Error::custom("Duration out of bounds!"))?)
}
}
14 changes: 9 additions & 5 deletions src/v2/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,13 @@ impl TryFrom<EncryptedBalance> for super::types::AccountEncryptedAmount {
}
}

impl From<Duration> for chrono::Duration {
fn from(value: Duration) -> Self {
chrono::TimeDelta::try_milliseconds(value.value as i64).expect("Duration out of bounds")
impl TryFrom<Duration> for chrono::Duration {
type Error = tonic::Status;

fn try_from(value: Duration) -> Result<Self, Self::Error> {
chrono::TimeDelta::try_milliseconds(value.value as i64).ok_or(
tonic::Status::internal("Unexpected response for 'Duration': Duration out of bounds.")
)
}
}

Expand Down Expand Up @@ -2174,7 +2178,7 @@ impl TryFrom<ConsensusInfo> for super::types::queries::ConsensusInfo {
.map(|v| v.try_into())
.transpose()?,
finalization_count: value.finalization_count.into(),
epoch_duration: value.epoch_duration.require()?.into(),
epoch_duration: value.epoch_duration.require()?.try_into()?,
blocks_verified_count: value.blocks_verified_count.into(),
slot_duration: value.slot_duration.map(Into::into),
genesis_time: value.genesis_time.require()?.try_into()?,
Expand Down Expand Up @@ -2202,7 +2206,7 @@ impl TryFrom<ConsensusInfo> for super::types::queries::ConsensusInfo {
None
} else {
Some(ConcordiumBFTDetails {
current_timeout_duration: value.current_timeout_duration.require()?.into(),
current_timeout_duration: value.current_timeout_duration.require()?.try_into()?,
current_round: value.current_round.require()?.into(),
current_epoch: value.current_epoch.require()?.into(),
trigger_block_time: value.trigger_block_time.require()?.try_into()?,
Expand Down
2 changes: 1 addition & 1 deletion src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ impl TryFrom<generated::NodeInfo> for types::NodeInfo {
let version = semver::Version::parse(&node_info.peer_version)?;
let local_time = chrono::DateTime::<chrono::Utc>::from(std::time::UNIX_EPOCH)
+ chrono::TimeDelta::try_milliseconds(node_info.local_time.require()?.value as i64)
.expect("Node local time out of bounds!");
.ok_or_else(|| anyhow::anyhow!("Node local time out of bounds!"))?;
let uptime = types::DurationSeconds::from(node_info.peer_uptime.require()?.value).into();
let network_info = node_info.network_info.require()?.try_into()?;
let details = match node_info.details.require()? {
Expand Down

0 comments on commit 5a77437

Please sign in to comment.