From 244bf789eafeeebbb21299f0779fbb2d379a176f Mon Sep 17 00:00:00 2001 From: Greg Nazario Date: Mon, 18 Apr 2022 19:34:22 -0700 Subject: [PATCH] [account-address] Make display and hex outputs consistent --- language/evm/move-to-yul/src/generator.rs | 2 +- .../move-core/types/src/account_address.rs | 47 +++++++++++-------- .../transactional-test-runner/src/tasks.rs | 14 +----- .../src/source_package/manifest_parser.rs | 8 +++- 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/language/evm/move-to-yul/src/generator.rs b/language/evm/move-to-yul/src/generator.rs index 7d417a129e1..9286f35b3e5 100644 --- a/language/evm/move-to-yul/src/generator.rs +++ b/language/evm/move-to-yul/src/generator.rs @@ -268,7 +268,7 @@ impl Generator { emit!(ctx.writer, "let $arg{} := ", idx); match arg { MoveValue::Address(addr) => { - emitln!(ctx.writer, "{}", addr.to_hex_literal()); + emitln!(ctx.writer, "{}", addr); } _ => unreachable!( "only address literals are allowed as test arguments currently" diff --git a/language/move-core/types/src/account_address.rs b/language/move-core/types/src/account_address.rs index 6444fdc2997..0cbd4473714 100644 --- a/language/move-core/types/src/account_address.rs +++ b/language/move-core/types/src/account_address.rs @@ -44,8 +44,11 @@ impl AccountAddress { Self(buf) } + /// A shortened version with leading 0s are stripped from the hex pub fn short_str_lossless(&self) -> String { - let hex_str = hex::encode(&self.0).trim_start_matches('0').to_string(); + let hex_str = hex::encode_upper(&self.0) + .trim_start_matches('0') + .to_string(); if hex_str.is_empty() { "0".to_string() } else { @@ -61,6 +64,17 @@ impl AccountAddress { self.0 } + /// Reads hex that starts with a 0x or doesn't start with a 0x + pub fn from_hex_fuzzy(literal: &str) -> Result { + if literal.starts_with("0x") { + Self::from_hex_literal(literal) + } else { + Self::from_hex(literal) + } + } + + /// Reads a hex literal 0xABCD, and requires a 0x in front, but not + /// necessarily [`Self::LENGTH`] in length pub fn from_hex_literal(literal: &str) -> Result { if !literal.starts_with("0x") { return Err(AccountAddressParseError); @@ -81,20 +95,13 @@ impl AccountAddress { } } - pub fn to_hex_literal(&self) -> String { - format!("0x{}", self.short_str_lossless()) - } - + /// Reads hex that must be exactly [`Self::LENGTH`] length pub fn from_hex>(hex: T) -> Result { <[u8; Self::LENGTH]>::from_hex(hex) .map_err(|_| AccountAddressParseError) .map(Self) } - pub fn to_hex(&self) -> String { - self.short_str_lossless() - } - pub fn from_bytes>(bytes: T) -> Result { <[u8; Self::LENGTH]>::try_from(bytes.as_ref()) .map_err(|_| AccountAddressParseError) @@ -118,13 +125,13 @@ impl std::ops::Deref for AccountAddress { impl fmt::Display for AccountAddress { fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result { - write!(f, "{:X}", self) + write!(f, "{:#X}", self) } } impl fmt::Debug for AccountAddress { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:X}", self) + write!(f, "{:#X}", self) } } @@ -206,7 +213,7 @@ impl From<&AccountAddress> for [u8; AccountAddress::LENGTH] { impl From<&AccountAddress> for String { fn from(addr: &AccountAddress) -> String { - addr.to_hex_literal() + addr.to_string() } } @@ -222,7 +229,7 @@ impl FromStr for AccountAddress { type Err = AccountAddressParseError; fn from_str(s: &str) -> Result { - Self::from_hex_literal(s) + Self::from_hex_fuzzy(s) } } @@ -233,7 +240,7 @@ impl<'de> Deserialize<'de> for AccountAddress { { if deserializer.is_human_readable() { let s = ::deserialize(deserializer)?; - AccountAddress::from_hex_literal(&s).map_err(D::Error::custom) + AccountAddress::from_hex_fuzzy(&s).map_err(D::Error::custom) } else { // In order to preserve the Serde data model and help analysis tools, // make sure to wrap our value in a container with the same name @@ -254,7 +261,7 @@ impl Serialize for AccountAddress { S: Serializer, { if serializer.is_human_readable() { - self.to_hex_literal().serialize(serializer) + self.to_string().serialize(serializer) } else { // See comment in deserialize. serializer.serialize_newtype_struct("AccountAddress", &self.0) @@ -290,8 +297,8 @@ mod tests { let address = AccountAddress::from_hex(hex).unwrap(); - assert_eq!(format!("{}", address), upper_hex); - assert_eq!(format!("{:?}", address), upper_hex); + assert_eq!(format!("{}", address), format!("0x{}", upper_hex)); + assert_eq!(format!("{:?}", address), format!("0x{}", upper_hex)); assert_eq!(format!("{:X}", address), upper_hex); assert_eq!(format!("{:x}", address), hex); @@ -305,7 +312,7 @@ mod tests { assert_eq!( address.short_str_lossless(), - "c0f1f95c5b1c5f0eda533eff269000", + "C0F1F95C5B1C5F0EDA533EFF269000", ); } @@ -337,14 +344,14 @@ mod tests { #[test] fn test_from_hex_literal() { - let hex_literal = "0x1"; + let hex_literal = "0x00000000000000000000000000000001"; let hex = "00000000000000000000000000000001"; let address_from_literal = AccountAddress::from_hex_literal(hex_literal).unwrap(); let address = AccountAddress::from_hex(hex).unwrap(); assert_eq!(address_from_literal, address); - assert_eq!(hex_literal, address.to_hex_literal()); + assert_eq!(hex_literal, address.to_string()); // Check other variations of 0x1 assert_eq!(AccountAddress::from_str("0x01").unwrap(), address); diff --git a/language/testing-infra/transactional-test-runner/src/tasks.rs b/language/testing-infra/transactional-test-runner/src/tasks.rs index c7394837506..fcb6f043cc6 100644 --- a/language/testing-infra/transactional-test-runner/src/tasks.rs +++ b/language/testing-infra/transactional-test-runner/src/tasks.rs @@ -23,21 +23,9 @@ pub enum RawAddress { Anonymous(AccountAddress), } -fn parse_address_literal(s: &str) -> Result { - let (array, _) = move_compiler::shared::parse_address(s).ok_or_else(|| { - anyhow!( - "Failed to parse address {} to AccountAddress with Length {}", - s, - AccountAddress::LENGTH - ) - })?; - - Ok(AccountAddress::new(array)) -} - impl RawAddress { pub fn parse(s: &str) -> Result { - if let Ok(addr) = parse_address_literal(s) { + if let Ok(addr) = AccountAddress::from_hex_fuzzy(s) { return Ok(Self::Anonymous(addr)); } let name = diff --git a/language/tools/move-package/src/source_package/manifest_parser.rs b/language/tools/move-package/src/source_package/manifest_parser.rs index 428b7c949be..cc2afdf7c34 100644 --- a/language/tools/move-package/src/source_package/manifest_parser.rs +++ b/language/tools/move-package/src/source_package/manifest_parser.rs @@ -213,7 +213,10 @@ pub fn parse_addresses(tval: TV) -> Result { } else if addresses .insert( ident, - Some(parse_address_literal(entry_str).context("Invalid address")?), + Some( + AccountAddress::from_hex_fuzzy(entry_str) + .context("Invalid address")?, + ), ) .is_some() { @@ -251,7 +254,8 @@ pub fn parse_dev_addresses(tval: TV) -> Result { } else if addresses .insert( ident, - parse_address_literal(entry_str).context("Invalid address")?, + AccountAddress::from_hex_fuzzy(entry_str) + .context("Invalid address")?, ) .is_some() {