From 42ad0ee36ded0a0f28c0463dc8a109b5c351f932 Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Wed, 7 Feb 2024 19:42:28 +0900 Subject: [PATCH] fix to return an error for missing proto value instead of calling unwrap Signed-off-by: Jun Kimura --- crates/ibc/src/client_state.rs | 17 ++++++++---- crates/ibc/src/errors.rs | 8 ++++++ crates/ibc/src/header.rs | 16 ++++++++--- crates/ibc/src/misbehaviour.rs | 26 +++++++++++++----- crates/ibc/src/types.rs | 49 ++++++++++++++++++++++++---------- crates/ibc/src/update.rs | 1 - 6 files changed, 87 insertions(+), 30 deletions(-) diff --git a/crates/ibc/src/client_state.rs b/crates/ibc/src/client_state.rs index 75d80eb..e93ca65 100644 --- a/crates/ibc/src/client_state.rs +++ b/crates/ibc/src/client_state.rs @@ -625,7 +625,9 @@ impl, }, + /// proto missing field error: `{0}` + ProtoMissingFieldError(String), +} + +impl Error { + pub fn proto_missing(s: &str) -> Self { + Error::ProtoMissingFieldError(s.to_string()) + } } impl From for ClientError { diff --git a/crates/ibc/src/header.rs b/crates/ibc/src/header.rs index 3e6d6c6..986402e 100644 --- a/crates/ibc/src/header.rs +++ b/crates/ibc/src/header.rs @@ -55,10 +55,18 @@ impl Protobuf for Header TryFrom for Header { type Error = Error; fn try_from(value: RawHeader) -> Result { - let trusted_sync_committee = value.trusted_sync_committee.unwrap(); - let consensus_update = value.consensus_update.unwrap(); - let execution_update = value.execution_update.unwrap(); - let account_update = value.account_update.unwrap(); + let trusted_sync_committee = value + .trusted_sync_committee + .ok_or(Error::proto_missing("trusted_sync_committee"))?; + let consensus_update = value + .consensus_update + .ok_or(Error::proto_missing("consensus_update"))?; + let execution_update = value + .execution_update + .ok_or(Error::proto_missing("execution_update"))?; + let account_update = value + .account_update + .ok_or(Error::proto_missing("account_update"))?; Ok(Self { trusted_sync_committee: trusted_sync_committee.try_into()?, diff --git a/crates/ibc/src/misbehaviour.rs b/crates/ibc/src/misbehaviour.rs index 375a0a4..1acf42e 100644 --- a/crates/ibc/src/misbehaviour.rs +++ b/crates/ibc/src/misbehaviour.rs @@ -63,13 +63,20 @@ impl TryFrom fn try_from(value: RawFinalizedHeaderMisbehaviour) -> Result { Ok(Self { client_id: ClientId::from_str(&value.client_id)?, - trusted_sync_committee: value.trusted_sync_committee.unwrap().try_into()?, + trusted_sync_committee: value + .trusted_sync_committee + .ok_or(Error::proto_missing("trusted_sync_committee"))? + .try_into()?, data: MisbehaviourData::FinalizedHeader(FinalizedHeaderMisbehaviour { consensus_update_1: convert_proto_to_consensus_update( - value.consensus_update_1.unwrap(), + value + .consensus_update_1 + .ok_or(Error::proto_missing("consensus_update_1"))?, )?, consensus_update_2: convert_proto_to_consensus_update( - value.consensus_update_2.unwrap(), + value + .consensus_update_2 + .ok_or(Error::proto_missing("consensus_update_2"))?, )?, }), }) @@ -83,13 +90,20 @@ impl TryFrom fn try_from(value: RawNextSyncCommitteeMisbehaviour) -> Result { Ok(Self { client_id: ClientId::from_str(&value.client_id)?, - trusted_sync_committee: value.trusted_sync_committee.unwrap().try_into()?, + trusted_sync_committee: value + .trusted_sync_committee + .ok_or(Error::proto_missing("trusted_sync_committee"))? + .try_into()?, data: MisbehaviourData::NextSyncCommittee(NextSyncCommitteeMisbehaviour { consensus_update_1: convert_proto_to_consensus_update( - value.consensus_update_1.unwrap(), + value + .consensus_update_1 + .ok_or(Error::proto_missing("consensus_update_1"))?, )?, consensus_update_2: convert_proto_to_consensus_update( - value.consensus_update_2.unwrap(), + value + .consensus_update_2 + .ok_or(Error::proto_missing("consensus_update_2"))?, )?, }), }) diff --git a/crates/ibc/src/types.rs b/crates/ibc/src/types.rs index 8fa9433..b2a0b67 100644 --- a/crates/ibc/src/types.rs +++ b/crates/ibc/src/types.rs @@ -35,24 +35,32 @@ impl TryFrom type Error = Error; fn try_from(value: ProtoTrustedSyncCommittee) -> Result { + let trusted_height = value + .trusted_height + .as_ref() + .ok_or(Error::proto_missing("trusted_height"))?; Ok(TrustedSyncCommittee { height: Height::new( - value.trusted_height.as_ref().unwrap().revision_number, - value.trusted_height.as_ref().unwrap().revision_height, + trusted_height.revision_number, + trusted_height.revision_height, )?, sync_committee: SyncCommittee { pubkeys: Vector::::from_iter( value .sync_committee .as_ref() - .unwrap() + .ok_or(Error::proto_missing("sync_committee"))? .pubkeys .clone() .into_iter() - .map(|pk| PublicKey::try_from(pk).unwrap()), + .map(|pk| pk.try_into()) + .collect::, _>>()?, ), aggregate_pubkey: PublicKey::try_from( - value.sync_committee.unwrap().aggregate_pubkey, + value + .sync_committee + .ok_or(Error::proto_missing("sync_committee"))? + .aggregate_pubkey, )?, }, is_next: value.is_next, @@ -250,10 +258,18 @@ pub(crate) fn convert_consensus_update_to_proto( consensus_update: ProtoLightClientUpdate, ) -> Result, Error> { - let attested_header = - convert_proto_to_header(consensus_update.attested_header.as_ref().unwrap())?; - let finalized_header = - convert_proto_to_header(consensus_update.finalized_header.as_ref().unwrap())?; + let attested_header = convert_proto_to_header( + consensus_update + .attested_header + .as_ref() + .ok_or(Error::proto_missing("attested_header"))?, + )?; + let finalized_header = convert_proto_to_header( + consensus_update + .finalized_header + .as_ref() + .ok_or(Error::proto_missing("finalized_header"))?, + )?; let light_client_update = LightClientUpdate { attested_header, @@ -261,7 +277,7 @@ pub(crate) fn convert_proto_to_consensus_update, _>>()?, ), aggregate_pubkey: PublicKey::try_from( consensus_update .next_sync_committee - .unwrap() + .ok_or(Error::proto_missing("next_sync_committee"))? .aggregate_pubkey, )?, }, @@ -293,7 +310,11 @@ pub(crate) fn convert_proto_to_consensus_update