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

imp(core-host-types): add new storage paths #1069

Merged
merged 13 commits into from
Feb 1, 2024
264 changes: 246 additions & 18 deletions ibc-core/ics24-host/types/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@
/// The Path enum abstracts out the different sub-paths.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, From, Display)]
pub enum Path {
NextClientSequence(NextClientSequencePath),
NextConnectionSequence(NextConnectionSequencePath),
NextChannelSequence(NextChannelSequencePath),
ClientState(ClientStatePath),
ClientConsensusState(ClientConsensusStatePath),
ClientUpdateTime(ClientUpdateTimePath),
ClientUpdateHeight(ClientUpdateHeightPath),
ClientConnection(ClientConnectionPath),
Connection(ConnectionPath),
Ports(PortPath),
Expand All @@ -37,6 +42,57 @@
UpgradeClient(UpgradeClientPath),
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo

Check warning on line 50 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L48-L50

Added lines #L48 - L50 were not covered by tests
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)

Check warning on line 55 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L55

Added line #L55 was not covered by tests
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)]

Check warning on line 58 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L57-L58

Added lines #L57 - L58 were not covered by tests
#[display(fmt = "nextClientSequence")]
pub struct NextClientSequencePath;

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo

Check warning on line 67 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L65-L67

Added lines #L65 - L67 were not covered by tests
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)

Check warning on line 72 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L72

Added line #L72 was not covered by tests
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)]

Check warning on line 75 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L74-L75

Added lines #L74 - L75 were not covered by tests
#[display(fmt = "nextConnectionSequence")]
pub struct NextConnectionSequencePath;

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo

Check warning on line 84 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L82-L84

Added lines #L82 - L84 were not covered by tests
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)

Check warning on line 89 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L89

Added line #L89 was not covered by tests
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)]

Check warning on line 92 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L91-L92

Added lines #L91 - L92 were not covered by tests
#[display(fmt = "nextChannelSequence")]
pub struct NextChannelSequencePath;

#[cfg_attr(
feature = "parity-scale-codec",
derive(
Expand Down Expand Up @@ -95,6 +151,72 @@
}
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo

Check warning on line 159 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L157-L159

Added lines #L157 - L159 were not covered by tests
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)

Check warning on line 164 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L164

Added line #L164 was not covered by tests
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]

Check warning on line 166 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L166

Added line #L166 was not covered by tests
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)]
#[display(
fmt = "clients/{client_id}/consensusStates/{revision_number}-{revision_height}/processedTime"
)]
pub struct ClientUpdateTimePath {
pub client_id: ClientId,
pub revision_number: u64,
pub revision_height: u64,
}

impl ClientUpdateTimePath {
pub fn new(client_id: ClientId, revision_number: u64, revision_height: u64) -> Self {
Self {
client_id,
revision_number,
revision_height,
}
}

Check warning on line 184 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L178-L184

Added lines #L178 - L184 were not covered by tests
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo

Check warning on line 192 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L190-L192

Added lines #L190 - L192 were not covered by tests
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)

Check warning on line 197 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L197

Added line #L197 was not covered by tests
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]

Check warning on line 199 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L199

Added line #L199 was not covered by tests
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)]
#[display(
fmt = "clients/{client_id}/consensusStates/{revision_number}-{revision_height}/processedHeight"
)]
pub struct ClientUpdateHeightPath {
pub client_id: ClientId,
pub revision_number: u64,
pub revision_height: u64,
}

impl ClientUpdateHeightPath {
pub fn new(client_id: ClientId, revision_number: u64, revision_height: u64) -> Self {
Self {
client_id,
revision_number,
revision_height,
}
}

Check warning on line 217 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L211-L217

Added lines #L211 - L217 were not covered by tests
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
Expand Down Expand Up @@ -414,7 +536,8 @@
fn from_str(s: &str) -> Result<Self, Self::Err> {
let components: Vec<&str> = s.split('/').collect();

parse_client_paths(&components)
parse_next_sequence(&components)
.or_else(|| parse_client_paths(&components))
.or_else(|| parse_connections(&components))
.or_else(|| parse_ports(&components))
.or_else(|| parse_channel_ends(&components))
Expand All @@ -429,6 +552,19 @@
}
}

fn parse_next_sequence(components: &[&str]) -> Option<Path> {
if components.len() != 1 {
return None;
}

match *components.first()? {
"nextClientSequence" => Some(NextClientSequencePath.into()),
"nextConnectionSequence" => Some(NextConnectionSequencePath.into()),
"nextChannelSequence" => Some(NextChannelSequencePath.into()),
_ => None,

Check warning on line 564 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L564

Added line #L564 was not covered by tests
}
}

fn parse_client_paths(components: &[&str]) -> Option<Path> {
let first = match components.first() {
Some(f) => *f,
Expand All @@ -450,17 +586,13 @@
"connections" => Some(ClientConnectionPath(client_id).into()),
_ => None,
}
} else if components.len() == 4 {
if "consensusStates" != components[2] {
return None;
} else if components.len() == 4 || components.len() == 5 {
match components[2] {
"consensusStates" => {}
_ => return None,

Check warning on line 592 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L592

Added line #L592 was not covered by tests
}

let epoch_height = match components.last() {
Some(eh) => *eh,
None => return None,
};

let epoch_height: Vec<&str> = epoch_height.split('-').collect();
let epoch_height: Vec<&str> = components[3].split('-').collect();

if epoch_height.len() != 2 {
return None;
Expand All @@ -479,14 +611,36 @@
Err(_) => return None,
};

Some(
ClientConsensusStatePath {
client_id,
revision_number,
revision_height,
}
.into(),
)
match components.len() {
4 => Some(
ClientConsensusStatePath {
client_id,
revision_number,
revision_height,
}
.into(),
),
5 => match components[4] {
"processedTime" => Some(
ClientUpdateTimePath {
client_id,
revision_number,
revision_height,
}
.into(),
),
"processedHeight" => Some(
ClientUpdateHeightPath {
client_id,
revision_number,
revision_height,
}
.into(),
),
_ => None,

Check warning on line 640 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L640

Added line #L640 was not covered by tests
},
_ => None,

Check warning on line 642 in ibc-core/ics24-host/types/src/path.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics24-host/types/src/path.rs#L642

Added line #L642 was not covered by tests
}
} else {
None
}
Expand Down Expand Up @@ -840,6 +994,28 @@

use super::*;

#[test]
fn test_parse_next_sequence_fn() {
let path = "nextClientSequence";

assert!(matches!(
Path::from_str(path),
Ok(Path::NextClientSequence(NextClientSequencePath))
));

let path = "nextConnectionSequence";
assert!(matches!(
Path::from_str(path),
Ok(Path::NextConnectionSequence(NextConnectionSequencePath))
));

let path = "nextChannelSequence";
assert!(matches!(
Path::from_str(path),
Ok(Path::NextChannelSequence(NextChannelSequencePath))
));
}

#[test]
fn invalid_path_doesnt_parse() {
let invalid_path = Path::from_str("clients/clientType");
Expand Down Expand Up @@ -870,6 +1046,33 @@
);
}

#[test]
fn test_parse_client_update_paths_fn() {
let path = "clients/07-tendermint-0/consensusStates/15-31/processedTime";
let components: Vec<&str> = path.split('/').collect();

assert_eq!(
parse_client_paths(&components),
Some(Path::ClientUpdateTime(ClientUpdateTimePath {
client_id: ClientId::default(),
revision_number: 15,
revision_height: 31,
}))
);

let path = "clients/07-tendermint-0/consensusStates/15-31/processedHeight";
let components: Vec<&str> = path.split('/').collect();

assert_eq!(
parse_client_paths(&components),
Some(Path::ClientUpdateHeight(ClientUpdateHeightPath {
client_id: ClientId::default(),
revision_number: 15,
revision_height: 31,
}))
);
}

#[test]
fn client_state_path_parses() {
let path = "clients/07-tendermint-0/clientState";
Expand All @@ -882,6 +1085,31 @@
);
}

#[test]
fn test_parse_client_update_paths_parses() {
Farhad-Shabani marked this conversation as resolved.
Show resolved Hide resolved
let path = "clients/07-tendermint-0/consensusStates/15-31/processedTime";

assert_eq!(
Path::from_str(path).ok(),
Some(Path::ClientUpdateTime(ClientUpdateTimePath {
client_id: ClientId::default(),
revision_number: 15,
revision_height: 31,
}))
);

let path = "clients/07-tendermint-0/consensusStates/15-31/processedHeight";

assert_eq!(
Path::from_str(path).ok(),
Some(Path::ClientUpdateHeight(ClientUpdateHeightPath {
client_id: ClientId::default(),
revision_number: 15,
revision_height: 31,
}))
);
}

#[test]
fn client_consensus_state_path_parses() {
let path = "clients/07-tendermint-0/consensusStates/15-31";
Expand Down
Loading