Skip to content

Commit

Permalink
Remove dependency on safe-regex for regex (#3556)
Browse files Browse the repository at this point in the history
* Remove dependency on `safe-regex` for `regex`

* Add changelog entry

* Fix regex for coin
  • Loading branch information
romac authored Aug 23, 2023
1 parent fdc9255 commit 6621d70
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 257 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix build of `ibc-relayer-types` documentation on docs.rs
([\#3549](https://github.com/informalsystems/hermes/issues/3549))
49 changes: 1 addition & 48 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/relayer-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ serde_json = { version = "1" }
erased-serde = { version = "0.3" }
prost = { version = "0.11" }
bytes = { version = "1.4.0" }
safe-regex = { version = "0.2.5" }
subtle-encoding = { version = "0.5" }
flex-error = { version = "0.4.4" }
derive_more = { version = "0.99.17", default-features = false, features = ["from", "into", "display"] }
Expand All @@ -42,6 +41,7 @@ itertools = { version = "0.10.3" }
primitive-types = { version = "0.12.1", default-features = false, features = ["serde_no_std"] }
dyn-clone = "1.0.12"
num-rational = "0.4.1"
regex = "1"

[dependencies.tendermint]
version = "0.33.0"
Expand Down
48 changes: 25 additions & 23 deletions crates/relayer-types/src/applications/transfer/coin.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use ibc_proto::cosmos::base::v1beta1::Coin as ProtoCoin;
use safe_regex::regex;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Error as FmtError, Formatter};
use std::str::{from_utf8, FromStr};
use std::str::FromStr;

use regex::Regex;
use serde::{Deserialize, Serialize};

use ibc_proto::cosmos::base::v1beta1::Coin as ProtoCoin;

use crate::serializers::serde_string;

use super::amount::Amount;
use super::denom::{BaseDenom, PrefixedDenom};
use super::error::Error;
use crate::serializers::serde_string;

/// A `Coin` type with fully qualified `PrefixedDenom`.
pub type PrefixedCoin = Coin<PrefixedDenom>;
Expand Down Expand Up @@ -61,24 +64,23 @@ where
{
type Err = Error;

#[allow(clippy::assign_op_pattern)]
// #[allow(clippy::assign_op_pattern)]
fn from_str(coin_str: &str) -> Result<Self, Error> {
// Denominations can be 3 ~ 128 characters long and support letters, followed by either
// a letter, a number or a separator ('/', ':', '.', '_' or '-').
// Loosely copy the regex from here:
// https://github.com/cosmos/cosmos-sdk/blob/v0.45.5/types/coin.go#L760-L762
let matcher = regex!(br"([0-9]+)([a-zA-Z0-9/:\\._\x2d]+)");

let (m1, m2) = matcher
.match_slices(coin_str.as_bytes())
.ok_or_else(|| Error::invalid_coin(coin_str.to_string()))?;
let regex = Regex::new(
r"^(?<amount>[0-9]+(?:\.[0-9]+)?|\.[0-9]+)\s*(?<denom>[a-zA-Z][a-zA-Z0-9/:._-]{2,127})$",
)
.expect("failed to compile regex");

let amount = from_utf8(m1).map_err(Error::utf8_decode)?.parse()?;
let captures = regex.captures(coin_str).ok_or_else(|| {
Error::invalid_coin(format!("{coin_str} (expected format: <amount><denom>)"))
})?;

let denom = from_utf8(m2)
.map_err(Error::utf8_decode)?
.parse()
.map_err(Into::into)?;
let amount = captures["amount"].parse()?;
let denom = captures["denom"].parse().map_err(Into::into)?;

Ok(Coin { amount, denom })
}
Expand Down Expand Up @@ -134,14 +136,14 @@ mod tests {
}

{
let coin = RawCoin::from_str("1a1")?;
assert_eq!(coin.denom, "a1");
let coin = RawCoin::from_str("1 ab1")?;
assert_eq!(coin.denom, "ab1");
assert_eq!(coin.amount, 1u64.into());
}

{
let coin = RawCoin::from_str("0x1/:.\\_-")?;
assert_eq!(coin.denom, "x1/:.\\_-");
let coin = RawCoin::from_str("0x1/:._-")?;
assert_eq!(coin.denom, "x1/:._-");
assert_eq!(coin.amount, 0u64.into());
}

Expand All @@ -157,16 +159,16 @@ mod tests {
#[test]
fn test_parse_raw_coin_list() -> Result<(), Error> {
{
let coins = RawCoin::from_string_list("123stake,1a1,999den0m")?;
let coins = RawCoin::from_string_list("123stake,1ab1,999de-n0m")?;
assert_eq!(coins.len(), 3);

assert_eq!(coins[0].denom, "stake");
assert_eq!(coins[0].amount, 123u64.into());

assert_eq!(coins[1].denom, "a1");
assert_eq!(coins[1].denom, "ab1");
assert_eq!(coins[1].amount, 1u64.into());

assert_eq!(coins[2].denom, "den0m");
assert_eq!(coins[2].denom, "de-n0m");
assert_eq!(coins[2].amount, 999u64.into());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/relayer-types/src/applications/transfer/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ define_error! {

InvalidCoin
{ coin: String }
| e | { format_args!("invalid coin string: {}", e.coin) },
| e | { format_args!("invalid coin: {}", e.coin) },

Utf8Decode
[ TraceError<Utf8Error> ]
Expand Down
8 changes: 5 additions & 3 deletions crates/relayer-types/src/core/ics24_host/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use std::convert::{From, Infallible};
use std::fmt::{Debug, Display, Error as FmtError, Formatter};
use std::str::FromStr;

use regex::Regex;
use serde::{Deserialize, Serialize};

use super::validate::*;
use crate::core::ics02_client::client_type::ClientType;
use crate::core::ics24_host::error::ValidationError;

use super::validate::*;

/// This type is subject to future changes.
///
/// TODO: ChainId validation is not standardized yet.
Expand Down Expand Up @@ -101,8 +103,8 @@ impl ChainId {
/// assert_eq!(ChainId::is_epoch_format("chainA-1"), true);
/// ```
pub fn is_epoch_format(chain_id: &str) -> bool {
let re = safe_regex::regex!(br".+[^-]-{1}[1-9][0-9]*");
re.is_match(chain_id.as_bytes())
let re = Regex::new(r"^.+[^-]-{1}[1-9][0-9]*$").expect("failed to compile regex");
re.is_match(chain_id)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/relayer-types/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Timestamp {
}

/// Returns a `Timestamp` representation of the current time.
#[cfg(feature = "clock")]
#[cfg(any(test, feature = "clock"))]
pub fn now() -> Timestamp {
Time::now().into()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ signature = "2.1.0"
anyhow = "1.0"
semver = "1.0"
humantime = "2.1.0"
regex = "1.8.1"
regex = "1"
moka = "0.11.3"
uuid = { version = "1.4.0", features = ["v4"] }
bs58 = "0.5.0"
Expand Down
Loading

0 comments on commit 6621d70

Please sign in to comment.