Skip to content

net: Strip previous p2p prefix before concatenating addresses#11078

Merged
bkchr merged 4 commits intomasterfrom
lenxv/multiaddr-concat
Feb 22, 2026
Merged

net: Strip previous p2p prefix before concatenating addresses#11078
bkchr merged 4 commits intomasterfrom
lenxv/multiaddr-concat

Conversation

@lexnv
Copy link
Copy Markdown
Contributor

@lexnv lexnv commented Feb 16, 2026

This PR ensures that a MultiaddrWithPeerId will always remain valid after a concatenating process.

The litep2p code will concatanate the provided multiaddr-with-peerID regardless if the address already contains a p2p/.. prefix:

fn add_reserved_peer(&self, peer: MultiaddrWithPeerId) -> Result<(), String> {
let _ = self.cmd_tx.unbounded_send(NetworkServiceCommand::AddPeersToReservedSet {
protocol: self.block_announce_protocol.clone(),
peers: HashSet::from_iter([peer.concat().into()]),
});
Ok(())
}

This then can lead to addresses for reserved peers to never be dialed by the network backend as they are considered invalid.

This has been discovered by running the networking benchmarks:

network_service1
.add_reserved_peer(MultiaddrWithPeerId {
multiaddr: listen_address2,
peer_id: peer_id2,
})
.unwrap();

Part of the cleanups for:

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
@lexnv lexnv self-assigned this Feb 16, 2026
@lexnv lexnv added T0-node This PR/Issue is related to the topic “node”. A4-backport-stable2509 Pull request must be backported to the stable2509 release branch A4-backport-stable2512 Pull request must be backported to the stable2512 release branch labels Feb 16, 2026
Comment on lines +174 to +176
if matches!(addr.iter().last(), Some(multiaddr::Protocol::P2p(_))) {
addr.pop();
}
Copy link
Copy Markdown
Contributor

@dmitry-markin dmitry-markin Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be difficult to ensure that self.multiaddr never contains the peer ID in the first place?

We should investigate how peer IDs end up in the address that is supposed to be without a peer ID.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Litep2p appends the /p2p prefix on all listen addresses on initialization:

https://github.com/paritytech/litep2p/blob/5ec893959bc64a479545a3a6b6a5090fa84c796b/src/lib.rs#L334

I don't believe this will cause issues since we are dialing from the transport manager either way:

  • a different peer cannot trick us into dialing since we compare the addresses stripped of /p2p prefix
  • don't believe other protocols make use of this

I would still keep this one as a tiny defensive check for future proofing

Copy link
Copy Markdown
Contributor

@AndreiEres AndreiEres left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a networking expert but LGTM

let proto = multiaddr::Protocol::P2p(From::from(self.peer_id));
self.multiaddr.clone().with(proto)
let mut addr = self.multiaddr.clone();
if matches!(addr.iter().last(), Some(multiaddr::Protocol::P2p(_))) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth adding a comment why we do it

Co-authored-by: Bastian Köcher <git@kchr.de>
@lexnv lexnv enabled auto-merge February 20, 2026 10:33
@bkchr bkchr removed the A4-backport-stable2509 Pull request must be backported to the stable2509 release branch label Feb 20, 2026
@bkchr
Copy link
Copy Markdown
Member

bkchr commented Feb 20, 2026

/cmd prdoc --audience node_dev --bump patch

@lexnv lexnv added this pull request to the merge queue Feb 20, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 20, 2026
@bkchr bkchr added this pull request to the merge queue Feb 22, 2026
Merged via the queue into master with commit e30a412 Feb 22, 2026
242 of 245 checks passed
@bkchr bkchr deleted the lenxv/multiaddr-concat branch February 22, 2026 20:37
paritytech-release-backport-bot bot pushed a commit that referenced this pull request Feb 22, 2026
This PR ensures that a `MultiaddrWithPeerId` will always remain valid
after a concatenating process.

The litep2p code will concatanate the provided multiaddr-with-peerID
regardless if the address already contains a `p2p/..` prefix:

https://github.com/paritytech/polkadot-sdk/blob/e85be1c07adedcc86554affa4f6af536c4a2efc1/substrate/client/network/src/litep2p/service.rs#L427-L434

This then can lead to addresses for reserved peers to never be dialed by
the network backend as they are considered invalid.

This has been discovered by running the networking benchmarks:

https://github.com/paritytech/polkadot-sdk/blob/e85be1c07adedcc86554affa4f6af536c4a2efc1/substrate/client/network/benches/notifications_protocol.rs#L143-L148

Part of the cleanups for:
- #10425

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
(cherry picked from commit e30a412)
@paritytech-release-backport-bot
Copy link
Copy Markdown

Successfully created backport PR for stable2512:

EgorPopelyaev pushed a commit that referenced this pull request Feb 27, 2026
Backport #11078 into `stable2512` from lexnv.

See the
[documentation](https://github.com/paritytech/polkadot-sdk/blob/master/docs/BACKPORT.md)
on how to use this bot.

<!--
  # To be used by other automation, do not modify:
  original-pr-number: #${pull_number}
-->

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@lexnv lexnv added the A4-backport-stable2506 Pull request must be backported to the stable2506 release branch label Mar 9, 2026
paritytech-release-backport-bot bot pushed a commit that referenced this pull request Mar 9, 2026
This PR ensures that a `MultiaddrWithPeerId` will always remain valid
after a concatenating process.

The litep2p code will concatanate the provided multiaddr-with-peerID
regardless if the address already contains a `p2p/..` prefix:

https://github.com/paritytech/polkadot-sdk/blob/e85be1c07adedcc86554affa4f6af536c4a2efc1/substrate/client/network/src/litep2p/service.rs#L427-L434

This then can lead to addresses for reserved peers to never be dialed by
the network backend as they are considered invalid.

This has been discovered by running the networking benchmarks:

https://github.com/paritytech/polkadot-sdk/blob/e85be1c07adedcc86554affa4f6af536c4a2efc1/substrate/client/network/benches/notifications_protocol.rs#L143-L148

Part of the cleanups for:
- #10425

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
(cherry picked from commit e30a412)
@paritytech-release-backport-bot
Copy link
Copy Markdown

Successfully created backport PR for stable2506:

@paritytech-release-backport-bot
Copy link
Copy Markdown

Backport failed for stable2512, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin stable2512
git worktree add -d .worktree/backport-11078-to-stable2512 origin/stable2512
cd .worktree/backport-11078-to-stable2512
git switch --create backport-11078-to-stable2512
git cherry-pick -x e30a412c6cd957afedbb3157dac9cf4b424b0fa6

EgorPopelyaev pushed a commit that referenced this pull request Mar 9, 2026
Backport #11078 into `stable2506` from lexnv.

See the
[documentation](https://github.com/paritytech/polkadot-sdk/blob/master/docs/BACKPORT.md)
on how to use this bot.

<!--
  # To be used by other automation, do not modify:
  original-pr-number: #${pull_number}
-->

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
arturgontijo pushed a commit to moonbeam-foundation/polkadot-sdk that referenced this pull request Apr 1, 2026
Backport paritytech#11078 into `stable2512` from lexnv.

See the
[documentation](https://github.com/paritytech/polkadot-sdk/blob/master/docs/BACKPORT.md)
on how to use this bot.

<!--
  # To be used by other automation, do not modify:
  original-pr-number: #${pull_number}
-->

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A4-backport-stable2506 Pull request must be backported to the stable2506 release branch A4-backport-stable2512 Pull request must be backported to the stable2512 release branch T0-node This PR/Issue is related to the topic “node”.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants