1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Env, StdResult};
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use std::str::FromStr;

use crate::{AbstractError, AbstractResult};

pub const MAX_CHAIN_NAME_LENGTH: usize = 20;
pub const MIN_CHAIN_NAME_LENGTH: usize = 3;

#[cw_serde]
#[derive(Eq, PartialOrd, Ord)]
/// The name of a chain, aka the chain-id without the post-fix number.
/// ex. `cosmoshub-4` -> `cosmoshub`, `juno-1` -> `juno`
pub struct ChainName(String);

impl ChainName {
    // Construct the chain name from the environment (chain-id)
    pub fn new(env: &Env) -> Self {
        let chain_id = &env.block.chain_id;
        // split on the last -
        // `cosmos-testnet-53159`
        // -> `cosmos-testnet` and `53159`
        let parts: Vec<&str> = chain_id.rsplitn(2, '-').collect();
        // the parts vector should look like [53159, cosmos-tesnet], because we are using rsplitn
        Self(parts[1].to_string())
    }

    pub fn from_string(value: String) -> AbstractResult<Self> {
        let chain_name = Self(value);
        chain_name.verify()?;
        Ok(chain_name)
    }

    /// verify the formatting of the chain name
    pub fn verify(&self) -> AbstractResult<()> {
        // check length
        if self.0.is_empty()
            || self.0.len() < MIN_CHAIN_NAME_LENGTH
            || self.0.len() > MAX_CHAIN_NAME_LENGTH
        {
            return Err(AbstractError::FormattingError {
                object: "chain-seq".into(),
                expected: format!("between {MIN_CHAIN_NAME_LENGTH} and {MAX_CHAIN_NAME_LENGTH}"),
                actual: self.0.len().to_string(),
            });
        // check character set
        } else if !self.0.chars().all(|c| c.is_ascii_lowercase() || c == '-') {
            return Err(crate::AbstractError::FormattingError {
                object: "chain_name".into(),
                expected: "chain-name".into(),
                actual: self.0.clone(),
            });
        }
        Ok(())
    }

    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    // used for key implementation
    pub(crate) fn str_ref(&self) -> &String {
        &self.0
    }

    pub fn into_string(self) -> String {
        self.0
    }

    /// Only use this if you are sure that the string is valid (e.g. from storage)
    pub(crate) fn _from_str(value: &str) -> Self {
        Self(value.to_string())
    }

    /// Only use this if you are sure that the string is valid (e.g. from storage)
    pub(crate) fn _from_string(value: String) -> Self {
        Self(value)
    }
}

impl ToString for ChainName {
    fn to_string(&self) -> String {
        self.0.clone()
    }
}

impl FromStr for ChainName {
    type Err = AbstractError;
    fn from_str(value: &str) -> AbstractResult<Self> {
        let chain_name = Self(value.to_string());
        chain_name.verify()?;
        Ok(chain_name)
    }
}

impl<'a> PrimaryKey<'a> for &ChainName {
    type Prefix = ();

    type SubPrefix = ();

    type Suffix = Self;

    type SuperSuffix = Self;

    fn key(&self) -> Vec<cw_storage_plus::Key> {
        self.0.key()
    }
}

impl<'a> Prefixer<'a> for &ChainName {
    fn prefix(&self) -> Vec<Key> {
        self.0.prefix()
    }
}

impl KeyDeserialize for &ChainName {
    type Output = ChainName;

    #[inline(always)]
    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        Ok(ChainName(String::from_vec(value)?))
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use cosmwasm_std::testing::mock_env;
    use speculoos::prelude::*;

    #[test]
    fn test_namespace() {
        let namespace = ChainName::new(&mock_env());
        assert_that!(namespace.as_str()).is_equal_to("cosmos-testnet");
    }

    #[test]
    fn test_from_string() {
        let namespace = ChainName::from_string("test-me".to_string()).unwrap();
        assert_that!(namespace.as_str()).is_equal_to("test-me");
    }

    #[test]
    fn test_from_str() {
        let namespace = ChainName::from_str("test-too").unwrap();
        assert_that!(namespace.as_str()).is_equal_to("test-too");
    }

    #[test]
    fn test_to_string() {
        let namespace = ChainName::from_str("test").unwrap();
        assert_that!(namespace.to_string()).is_equal_to("test".to_string());
    }

    #[test]
    fn test_from_str_long() {
        let namespace = ChainName::from_str("test-a-b-c-d-e-f").unwrap();
        assert_that!(namespace.as_str()).is_equal_to("test-a-b-c-d-e-f");
    }

    #[test]
    fn string_key_works() {
        let k = &ChainName::from_str("test-abc").unwrap();
        let path = k.key();
        assert_eq!(1, path.len());
        assert_eq!(b"test-abc", path[0].as_ref());

        let joined = k.joined_key();
        assert_eq!(joined, b"test-abc")
    }

    // Failures

    #[test]
    fn local_empty_fails() {
        ChainName::from_str("").unwrap_err();
    }

    #[test]
    fn local_too_short_fails() {
        ChainName::from_str("a").unwrap_err();
    }

    #[test]
    fn local_too_long_fails() {
        ChainName::from_str(&"a".repeat(MAX_CHAIN_NAME_LENGTH + 1)).unwrap_err();
    }

    #[test]
    fn local_uppercase_fails() {
        ChainName::from_str("AAAAA").unwrap_err();
    }

    #[test]
    fn local_non_alphanumeric_fails() {
        ChainName::from_str("a_aoeuoau").unwrap_err();
    }
}