Skip to content

Commit 41c66fc

Browse files
committed
fix: revert anemo address tcp change and add checks for primary address format
1 parent 7f2e4f7 commit 41c66fc

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

crates/iota-genesis-builder/src/validator_info.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ impl GenesisValidatorInfo {
133133
}
134134

135135
if let Err(e) = self.info.p2p_address.to_anemo_address() {
136-
bail!("p2p address must be valid anemo address: {e}");
136+
bail!("p2p address must be a valid anemo address: {e}");
137137
}
138-
if let Err(e) = self.info.primary_address.to_anemo_address() {
139-
bail!("primary address must be valid anemo address: {e}");
138+
if !self.info.primary_address.is_loosely_valid_tcp_addr() {
139+
bail!("primary address must be a valid TCP multiaddress");
140140
}
141141

142142
if self.info.commission_rate > 10000 {

crates/iota-network-stack/src/multiaddr.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,19 @@ impl Multiaddr {
5252
}
5353

5454
/// Attempts to convert a multiaddr of the form
55-
/// `/[ip4,ip6,dns]/{}/[udp, tcp]/{port}` into an anemo address
55+
/// `/[ip4,ip6,dns]/{}/udp/{port}` into an anemo address
5656
pub fn to_anemo_address(&self) -> Result<anemo::types::Address, &'static str> {
5757
let mut iter = self.iter();
5858

5959
match (iter.next(), iter.next()) {
60-
(Some(Protocol::Ip4(ipaddr)), Some(Protocol::Udp(port) | Protocol::Tcp(port))) => {
61-
Ok((ipaddr, port).into())
62-
}
63-
(Some(Protocol::Ip6(ipaddr)), Some(Protocol::Udp(port) | Protocol::Tcp(port))) => {
64-
Ok((ipaddr, port).into())
65-
}
66-
(Some(Protocol::Dns(hostname)), Some(Protocol::Udp(port) | Protocol::Tcp(port))) => {
60+
(Some(Protocol::Ip4(ipaddr)), Some(Protocol::Udp(port))) => Ok((ipaddr, port).into()),
61+
(Some(Protocol::Ip6(ipaddr)), Some(Protocol::Udp(port))) => Ok((ipaddr, port).into()),
62+
(Some(Protocol::Dns(hostname)), Some(Protocol::Udp(port))) => {
6763
Ok((hostname.as_ref(), port).into())
6864
}
65+
6966
_ => {
70-
tracing::warn!("unsupported p2p multiaddr: '{self}'");
67+
tracing::warn!("unsupported udp multiaddr: '{self}'");
7168
Err("invalid address")
7269
}
7370
}
@@ -420,15 +417,10 @@ mod test {
420417

421418
#[test]
422419
fn test_to_anemo_address() {
423-
let addr_ip4_udp = Multiaddr(multiaddr!(Ip4([15, 15, 15, 1]), Udp(10500u16)))
424-
.to_anemo_address()
425-
.unwrap();
426-
assert_eq!("15.15.15.1:10500".to_string(), addr_ip4_udp.to_string());
427-
428-
let addr_ip4_tcp = Multiaddr(multiaddr!(Ip4([15, 15, 15, 1]), Tcp(10500u16)))
420+
let addr_ip4 = Multiaddr(multiaddr!(Ip4([15, 15, 15, 1]), Udp(10500u16)))
429421
.to_anemo_address()
430422
.unwrap();
431-
assert_eq!("15.15.15.1:10500".to_string(), addr_ip4_tcp.to_string());
423+
assert_eq!("15.15.15.1:10500".to_string(), addr_ip4.to_string());
432424

433425
let addr_ip6 = Multiaddr(multiaddr!(
434426
Ip6([15, 15, 15, 15, 15, 15, 15, 1]),
@@ -438,14 +430,13 @@ mod test {
438430
.unwrap();
439431
assert_eq!("[f:f:f:f:f:f:f:1]:10500".to_string(), addr_ip6.to_string());
440432

441-
let addr_dns = Multiaddr(multiaddr!(Dns("iota.iota"), Tcp(10501u16)))
433+
let addr_dns = Multiaddr(multiaddr!(Dns("iota.iota"), Udp(10501u16)))
442434
.to_anemo_address()
443435
.unwrap();
444436
assert_eq!("iota.iota:10501".to_string(), addr_dns.to_string());
445437

446-
// Error case
447438
let addr_invalid =
448-
Multiaddr(multiaddr!(Dns("iota.iota"), Sctp(10501u16))).to_anemo_address();
439+
Multiaddr(multiaddr!(Dns("iota.iota"), Tcp(10501u16))).to_anemo_address();
449440
assert!(addr_invalid.is_err());
450441
}
451442

crates/iota-network/src/discovery/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ async fn three_nodes_can_connect_via_discovery() -> Result<()> {
193193
}
194194

195195
#[tokio::test(flavor = "current_thread", start_paused = true)]
196-
async fn peers_are_added_from_reocnfig_channel() -> Result<()> {
196+
async fn peers_are_added_from_reconfig_channel() -> Result<()> {
197197
let (tx_1, rx_1) = create_test_channel();
198198
let config = P2pConfig::default();
199199
let (builder, server) = Builder::new(rx_1).config(config.clone()).build();

crates/iota-types/src/iota_system_state/iota_system_state_inner_v1.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ impl ValidatorMetadataV1 {
160160

161161
let primary_address = Multiaddr::try_from(self.primary_address.clone())
162162
.map_err(|_| E_METADATA_INVALID_PRIMARY_ADDR)?;
163-
primary_address
164-
.to_anemo_address()
165-
.map_err(|_| E_METADATA_INVALID_PRIMARY_ADDR)?;
163+
if !primary_address.is_loosely_valid_tcp_addr() {
164+
return Err(E_METADATA_INVALID_PRIMARY_ADDR);
165+
}
166166

167167
let next_epoch_authority_pubkey = match self.next_epoch_authority_pubkey_bytes.clone() {
168168
None => Ok::<Option<AuthorityPublicKey>, u64>(None),
@@ -243,9 +243,9 @@ impl ValidatorMetadataV1 {
243243
Some(address) => {
244244
let address =
245245
Multiaddr::try_from(address).map_err(|_| E_METADATA_INVALID_PRIMARY_ADDR)?;
246-
address
247-
.to_anemo_address()
248-
.map_err(|_| E_METADATA_INVALID_PRIMARY_ADDR)?;
246+
if !address.is_loosely_valid_tcp_addr() {
247+
return Err(E_METADATA_INVALID_PRIMARY_ADDR);
248+
};
249249

250250
Ok(Some(address))
251251
}

0 commit comments

Comments
 (0)