-
Notifications
You must be signed in to change notification settings - Fork 84
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
imp(ics23): fallible conversion for ProofSpec
, LeafOp
, InnerSpec
#1160
Changes from 3 commits
f9fc039
9dd0569
84f3122
c921a7c
b182566
db28ff2
53f8915
303377c
c8f6efa
75947b1
6608bb9
4047f6f
206c366
a055cfd
35bb2f0
696072e
0183fcb
b370f70
c9b2454
6c465d5
3a035c5
6928051
c39de0d
519f65b
53ac059
3fb22e3
061a032
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -296,7 +296,7 @@ impl TryFrom<RawTmClientState> for ClientState { | |
unbonding_period, | ||
max_clock_drift, | ||
latest_height, | ||
raw.proof_specs.into(), | ||
raw.proof_specs.try_into()?, | ||
raw.upgrade_path, | ||
frozen_height, | ||
allow_update, | ||
|
@@ -411,7 +411,7 @@ pub(crate) mod serde_tests { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use ibc_core_commitment_types::proto::ics23::ProofSpec as Ics23ProofSpec; | ||
// use ibc_core_commitment_types::proto::ics23::ProofSpec as Ics23ProofSpec; | ||
|
||
use super::*; | ||
|
||
|
@@ -556,28 +556,28 @@ mod tests { | |
}, | ||
want_pass: false, | ||
}, | ||
Test { | ||
name: "Invalid (empty) proof specs".to_string(), | ||
params: ClientStateParams { | ||
proof_specs: Vec::<Ics23ProofSpec>::new().into(), | ||
..default_params.clone() | ||
}, | ||
want_pass: false, | ||
}, | ||
Test { | ||
name: "Invalid (empty) proof specs depth range".to_string(), | ||
params: ClientStateParams { | ||
proof_specs: vec![Ics23ProofSpec { | ||
leaf_spec: None, | ||
inner_spec: None, | ||
min_depth: 2, | ||
max_depth: 1, | ||
prehash_key_before_comparison: false, | ||
}].into(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to confirm - this test is removed because I think, it's still ok to leave them here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update: this will fail when unwrapping. so this must be removed. |
||
..default_params | ||
}, | ||
want_pass: false, | ||
}, | ||
// Test { | ||
// name: "Invalid (empty) proof specs".to_string(), | ||
// params: ClientStateParams { | ||
// proof_specs: Vec::<Ics23ProofSpec>::new(), | ||
// ..default_params.clone() | ||
// }, | ||
// want_pass: false, | ||
// }, | ||
// Test { | ||
// name: "Invalid (empty) proof specs depth range".to_string(), | ||
// params: ClientStateParams { | ||
// proof_specs: vec![Ics23ProofSpec { | ||
// leaf_spec: None, | ||
// inner_spec: None, | ||
// min_depth: 2, | ||
// max_depth: 1, | ||
// prehash_key_before_comparison: false, | ||
// }], | ||
// ..default_params | ||
// }, | ||
// want_pass: false, | ||
// }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add these tests? I guess that these failed to be created - because of |
||
] | ||
.into_iter() | ||
.collect(); | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,8 +18,7 @@ impl ProofSpecs { | |||||
vec![ | ||||||
ics23::iavl_spec(), // Format of proofs-iavl (iavl merkle proofs) | ||||||
ics23::tendermint_spec(), // Format of proofs-tendermint (crypto/ merkle SimpleProof) | ||||||
] | ||||||
.into() | ||||||
].try_into().expect("should convert successfully") | ||||||
} | ||||||
|
||||||
pub fn is_empty(&self) -> bool { | ||||||
|
@@ -45,9 +44,15 @@ impl ProofSpecs { | |||||
} | ||||||
} | ||||||
|
||||||
impl From<Vec<RawProofSpec>> for ProofSpecs { | ||||||
fn from(ics23_specs: Vec<RawProofSpec>) -> Self { | ||||||
Self(ics23_specs.into_iter().map(Into::into).collect()) | ||||||
impl TryFrom<Vec<RawProofSpec>> for ProofSpecs { | ||||||
type Error = CommitmentError; | ||||||
fn try_from(ics23_specs: Vec<RawProofSpec>) -> Result<Self, CommitmentError> { | ||||||
let mut specs = Vec::new(); | ||||||
for raw_spec in ics23_specs { | ||||||
let spec = ProofSpec::try_from(raw_spec)?; | ||||||
specs.push(spec); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use let specs = ics23_specs
.into_iter()
.map(ProofSpec::try_from)
.collect::<Result<Vec<_>, _>>()?; |
||||||
Ok(ProofSpecs(specs)) | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -61,15 +66,29 @@ impl From<ProofSpecs> for Vec<RawProofSpec> { | |||||
#[derive(Clone, Debug, PartialEq)] | ||||||
struct ProofSpec(RawProofSpec); | ||||||
|
||||||
impl From<RawProofSpec> for ProofSpec { | ||||||
fn from(spec: RawProofSpec) -> Self { | ||||||
Self(RawProofSpec { | ||||||
leaf_spec: spec.leaf_spec.map(|lop| LeafOp::from(lop).0), | ||||||
inner_spec: spec.inner_spec.map(|ispec| InnerSpec::from(ispec).0), | ||||||
impl TryFrom<RawProofSpec> for ProofSpec { | ||||||
type Error = CommitmentError; | ||||||
fn try_from(spec: RawProofSpec) -> Result<Self, CommitmentError> { | ||||||
if spec.max_depth < spec.min_depth | ||||||
|| spec.min_depth < 0 | ||||||
|| spec.max_depth < 0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add comments about the failure case. example: why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I am not certain about this, given some thought, I guess when min_depth or max_depth is negative, it specify that no lower or upper bound is enforced on the number of allowed InnerOps(proof specs) As a result something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hey sorry for not explaining myself 😅 I know the logic. I suggested adding them in the comments. You can refer to the comments in protobuf definitions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rnbguy yes but do you think
is correct or it should be as I mention? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The last comment is not right. The error case should be following, as you mentioned already, 0 < spec.min_depth && 0 < spec.max_depth && spec.max_depth < spec.min_depth So, if both of |
||||||
{ | ||||||
return Err(CommitmentError::InvalidDepthRange(spec.min_depth, spec.max_depth)); | ||||||
} | ||||||
|
||||||
let leaf_spec = spec.leaf_spec.map(|lop| LeafOp::from(lop)).map(|lop| lop.0); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
let inner_spec = spec.inner_spec | ||||||
.map(|ispec| InnerSpec::try_from(ispec)) | ||||||
tuantran1702 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
.transpose()? | ||||||
.map(|ispec| ispec.0); | ||||||
|
||||||
Ok(Self(RawProofSpec { | ||||||
leaf_spec, | ||||||
inner_spec, | ||||||
max_depth: spec.max_depth, | ||||||
min_depth: spec.min_depth, | ||||||
prehash_key_before_comparison: spec.prehash_key_before_comparison, | ||||||
}) | ||||||
})) | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -119,16 +138,29 @@ impl From<LeafOp> for RawLeafOp { | |||||
#[derive(Clone, Debug, PartialEq)] | ||||||
struct InnerSpec(RawInnerSpec); | ||||||
|
||||||
impl From<RawInnerSpec> for InnerSpec { | ||||||
fn from(inner_spec: RawInnerSpec) -> Self { | ||||||
Self(RawInnerSpec { | ||||||
impl TryFrom<RawInnerSpec> for InnerSpec { | ||||||
type Error = CommitmentError; | ||||||
fn try_from(inner_spec: RawInnerSpec) -> Result<Self, CommitmentError> { | ||||||
if inner_spec.child_size <= 0 { | ||||||
return Err(CommitmentError::InvalidChildSize(inner_spec.child_size)); | ||||||
} | ||||||
if inner_spec.min_prefix_length > inner_spec.max_prefix_length | ||||||
tuantran1702 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|| inner_spec.min_prefix_length < 0 | ||||||
|| inner_spec.max_prefix_length < 0 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add comments about the failure cases. |
||||||
return Err(CommitmentError::InvalidPrefixLengthRange( | ||||||
inner_spec.min_prefix_length, | ||||||
inner_spec.max_prefix_length, | ||||||
)); | ||||||
} | ||||||
|
||||||
Ok(Self(RawInnerSpec { | ||||||
child_order: inner_spec.child_order, | ||||||
child_size: inner_spec.child_size, | ||||||
min_prefix_length: inner_spec.min_prefix_length, | ||||||
max_prefix_length: inner_spec.max_prefix_length, | ||||||
empty_child: inner_spec.empty_child, | ||||||
hash: inner_spec.hash, | ||||||
}) | ||||||
})) | ||||||
} | ||||||
} | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we can leave this test here, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove it after disallowing empty proof specs in
Vec<RawProofSpec>::try_from
.