Skip to content

Commit

Permalink
ibc: storage keys refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Jan 19, 2024
1 parent 0b2085c commit ef90ed1
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions crates/ibc/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ use namada_core::types::storage::{DbKeySeg, Key, KeySeg};
use sha2::{Digest, Sha256};
use thiserror::Error;

const CLIENTS_COUNTER: &str = "clients/counter";
const CONNECTIONS_COUNTER: &str = "connections/counter";
const CHANNELS_COUNTER: &str = "channelEnds/counter";
const CLIENTS_COUNTER_PREFIX: &str = "clients";
const CONNECTIONS_COUNTER_PREFIX: &str = "connections";
const CHANNELS_COUNTER_PREFIX: &str = "channelEnds";
const COUNTER_SEG: &str = "counter";
const DENOM: &str = "ibc_denom";

#[allow(missing_docs)]
Expand Down Expand Up @@ -52,20 +53,20 @@ pub fn ibc_key(path: impl AsRef<str>) -> Result<Key> {

/// Returns a key of the IBC client counter
pub fn client_counter_key() -> Key {
let path = CLIENTS_COUNTER.to_owned();
let path = format!("{}/{}", CLIENTS_COUNTER_PREFIX, COUNTER_SEG);
ibc_key(path).expect("Creating a key for the client counter shouldn't fail")
}

/// Returns a key of the IBC connection counter
pub fn connection_counter_key() -> Key {
let path = CONNECTIONS_COUNTER.to_owned();
let path = format!("{}/{}", CONNECTIONS_COUNTER_PREFIX, COUNTER_SEG);
ibc_key(path)
.expect("Creating a key for the connection counter shouldn't fail")
}

/// Returns a key of the IBC channel counter
pub fn channel_counter_key() -> Key {
let path = CHANNELS_COUNTER.to_owned();
let path = format!("{}/{}", CHANNELS_COUNTER_PREFIX, COUNTER_SEG);
ibc_key(path)
.expect("Creating a key for the channel counter shouldn't fail")
}
Expand Down Expand Up @@ -449,3 +450,15 @@ pub fn is_ibc_denom_key(key: &Key) -> Option<(String, String)> {
_ => None,
}
}

/// Returns true if the given key is for an IBC counter for clients,
/// connections, or channelEnds
pub fn is_ibc_counter_key(key: &Key) -> bool {
matches!(&key.segments[..],
[DbKeySeg::AddressSeg(addr), DbKeySeg::StringSeg(prefix), DbKeySeg::StringSeg(counter)]
if addr == &Address::Internal(InternalAddress::Ibc)
&& (prefix == CLIENTS_COUNTER_PREFIX
|| prefix == CONNECTIONS_COUNTER_PREFIX
|| prefix == CHANNELS_COUNTER_PREFIX) && counter == COUNTER_SEG
)
}

0 comments on commit ef90ed1

Please sign in to comment.