From 9e5c6c947c1e74e8106fdc76ccea2a595b60388e Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Thu, 9 Nov 2023 13:45:22 -0400 Subject: [PATCH 1/3] fix(cheatcodes): support non-0x prefixed hex strings as uints for envuint --- crates/cheatcodes/src/env.rs | 15 ++++++++++++++- testdata/cheats/Env.t.sol | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/cheatcodes/src/env.rs b/crates/cheatcodes/src/env.rs index d9022b16d4c1..da87157e17a2 100644 --- a/crates/cheatcodes/src/env.rs +++ b/crates/cheatcodes/src/env.rs @@ -33,7 +33,7 @@ impl Cheatcode for envBool_0Call { impl Cheatcode for envUint_0Call { fn apply(&self, _state: &mut Cheatcodes) -> Result { let Self { name } = self; - env::(name, None) + env_uint(name, None).map_err(|e| fmt_err!("Could not parse U256 environment variable {name}. Make sure it's a valid uint, and ideally 0x-prefixed if its using hex: {e}")) } } @@ -246,6 +246,19 @@ where } } +/// Utility function to add special parsing for non-0x prefixed hex strings as uints. +fn env_uint(key: &str, default: Option) -> Result { + match (get_env(key), default) { + (Ok(val), _) => Ok(string::parse::(&val).unwrap_or( + U256::from_str_radix(&val, 16) + .map_err(|e| fmt_err!("Could not parse Uint: {e}"))? + .abi_encode(), + )), + (Err(_), Some(default)) => Ok(default.abi_encode()), + (Err(e), None) => Err(e), + } +} + fn env_array(key: &str, delim: &str, default: Option<&[T]>) -> Result where T: SolValue + std::str::FromStr, diff --git a/testdata/cheats/Env.t.sol b/testdata/cheats/Env.t.sol index 44e5f5db71fa..fb550b95b07a 100644 --- a/testdata/cheats/Env.t.sol +++ b/testdata/cheats/Env.t.sol @@ -36,7 +36,7 @@ contract EnvTest is DSTest { "0x01", "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", "0x0000000000000000000000000000000000000000000000000000000000000000", - "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" ]; uint256[numEnvUintTests] memory expected = [ type(uint256).min, From 65bc0a89423e5a611c3e5426494ddf620cd85251 Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Thu, 9 Nov 2023 13:49:05 -0400 Subject: [PATCH 2/3] chore: remove default --- crates/cheatcodes/src/env.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/cheatcodes/src/env.rs b/crates/cheatcodes/src/env.rs index da87157e17a2..e197306f0d43 100644 --- a/crates/cheatcodes/src/env.rs +++ b/crates/cheatcodes/src/env.rs @@ -33,7 +33,7 @@ impl Cheatcode for envBool_0Call { impl Cheatcode for envUint_0Call { fn apply(&self, _state: &mut Cheatcodes) -> Result { let Self { name } = self; - env_uint(name, None).map_err(|e| fmt_err!("Could not parse U256 environment variable {name}. Make sure it's a valid uint, and ideally 0x-prefixed if its using hex: {e}")) + env_uint(name).map_err(|e| fmt_err!("Could not parse U256 environment variable {name}. Make sure it's a valid uint, and ideally 0x-prefixed if its using hex: {e}")) } } @@ -247,15 +247,14 @@ where } /// Utility function to add special parsing for non-0x prefixed hex strings as uints. -fn env_uint(key: &str, default: Option) -> Result { - match (get_env(key), default) { - (Ok(val), _) => Ok(string::parse::(&val).unwrap_or( +fn env_uint(key: &str) -> Result { + match get_env(key) { + Ok(val) => Ok(string::parse::(&val).unwrap_or( U256::from_str_radix(&val, 16) .map_err(|e| fmt_err!("Could not parse Uint: {e}"))? .abi_encode(), )), - (Err(_), Some(default)) => Ok(default.abi_encode()), - (Err(e), None) => Err(e), + Err(e) => Err(e), } } From 5cdc2aa01be9e14f3f33f3b9b86af3b82c9c820c Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Thu, 9 Nov 2023 14:50:49 -0400 Subject: [PATCH 3/3] chore: only augment error message --- crates/cheatcodes/src/env.rs | 14 +------------- testdata/cheats/Env.t.sol | 6 +++--- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/crates/cheatcodes/src/env.rs b/crates/cheatcodes/src/env.rs index e197306f0d43..58460ff86333 100644 --- a/crates/cheatcodes/src/env.rs +++ b/crates/cheatcodes/src/env.rs @@ -33,7 +33,7 @@ impl Cheatcode for envBool_0Call { impl Cheatcode for envUint_0Call { fn apply(&self, _state: &mut Cheatcodes) -> Result { let Self { name } = self; - env_uint(name).map_err(|e| fmt_err!("Could not parse U256 environment variable {name}. Make sure it's a valid uint, and ideally 0x-prefixed if its using hex: {e}")) + env::(name, None).map_err(|e| fmt_err!("Could not parse U256 environment variable {name}. Make sure it's a valid uint, or a 0x-prefixed hex string: {e}")) } } @@ -246,18 +246,6 @@ where } } -/// Utility function to add special parsing for non-0x prefixed hex strings as uints. -fn env_uint(key: &str) -> Result { - match get_env(key) { - Ok(val) => Ok(string::parse::(&val).unwrap_or( - U256::from_str_radix(&val, 16) - .map_err(|e| fmt_err!("Could not parse Uint: {e}"))? - .abi_encode(), - )), - Err(e) => Err(e), - } -} - fn env_array(key: &str, delim: &str, default: Option<&[T]>) -> Result where T: SolValue + std::str::FromStr, diff --git a/testdata/cheats/Env.t.sol b/testdata/cheats/Env.t.sol index fb550b95b07a..a9d3ef8b93e6 100644 --- a/testdata/cheats/Env.t.sol +++ b/testdata/cheats/Env.t.sol @@ -33,15 +33,15 @@ contract EnvTest is DSTest { string[numEnvUintTests] memory values = [ "0", "115792089237316195423570985008687907853269984665640564039457584007913129639935", - "0x01", + "0x0a", "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", "0x0000000000000000000000000000000000000000000000000000000000000000", - "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" + "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" ]; uint256[numEnvUintTests] memory expected = [ type(uint256).min, type(uint256).max, - 1, + 10, 77814517325470205911140941194401928579557062014761831930645393041380819009408, type(uint256).min, type(uint256).max