Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Network sync refactoring (part 6) #11940

Conversation

nazar-pc
Copy link
Contributor

@nazar-pc nazar-pc commented Jul 29, 2022

Yet another one related to #8686

This time I noticed that there are a few traits in the codebase that somewhat match NetworkService and are implemented for it. Also NetworkService itself has way too many methods for all kinds of things that are not used in most cases.

What I did then is sliced NetworkService into many logical traits and made interfaces generic in crates that previously depended this specific implementation.

Next PR will remove sc-network from dependencies of most crates, making Substrate a framework with pluggable networking, not just consensus. API provided by new traits is the same as before and may need to be generalized/cleaned up in the future, but first decoupling of the rest of Substrate from sc-network is needed.

Reviewing commit after commit is recommended. There is no business logic changes here, just moving things around a bit. PR is big, I can cut it in half at any commit if it helps, but all commits are of the same type here. Hopefully trait collection and naming makes sense.

Polkadot companion: paritytech/polkadot#5841
Cumulus companion: paritytech/cumulus#1486

Polkadot address: 1vSxzbyz2cJREAuVWjhXUT1ds8vBzoxn2w4asNpusQKwjJd

@nazar-pc nazar-pc force-pushed the network-sync-refactoring-part-6 branch from 6263608 to 5caf558 Compare July 29, 2022 20:39
@nazar-pc
Copy link
Contributor Author

Fixed documentation references

@nazar-pc
Copy link
Contributor Author

nazar-pc commented Aug 4, 2022

@TriplEight I guess you already noticed this:

Github API says polkadot#5841 is not mergeable

It isn't indeed because CI didn't pass there, but it is physically mergeable (no conflicts against master), so should be allowed to run here as a companion check I think.

@nazar-pc
Copy link
Contributor Author

nazar-pc commented Aug 7, 2022

Hey @bkchr and @arkpar I'd like to get your feedback on this one

@arkpar
Copy link
Member

arkpar commented Aug 8, 2022

Looks good here.

Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

I'm approving this as it looks good. However, we should stop here. There is no need to have Substrate being completely opaque to the underlying network. This mainly just introduces complexity that we don't really need.

The task should really be to make networking agnostic that there exists a syncing protocol. It should not need to care. The networking crate should only provide ways to register protocols and ensure to drive these protocols. One of these protocols is then syncing.

client/service/src/builder.rs Show resolved Hide resolved
client/network/common/src/protocol.rs Show resolved Hide resolved
client/network/common/src/service.rs Outdated Show resolved Hide resolved
client/network/common/src/service.rs Outdated Show resolved Hide resolved
Comment on lines +155 to +156
/// Need a better solution to decide authorized_only, but now just use reserved_only flag for
/// prototyping.
Copy link
Member

Choose a reason for hiding this comment

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

Are these comments copied?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, all of them

Comment on lines +187 to +228
fn add_reserved_peer(&self, peer: String) -> Result<(), String>;

/// Removes a `PeerId` from the list of reserved peers.
fn remove_reserved_peer(&self, peer_id: PeerId);

/// Sets the reserved set of a protocol to the given set of peers.
///
/// Each `Multiaddr` must end with a `/p2p/` component containing the `PeerId`. It can also
/// consist of only `/p2p/<peerid>`.
///
/// The node will start establishing/accepting connections and substreams to/from peers in this
/// set, if it doesn't have any substream open with them yet.
///
/// Note however, if a call to this function results in less peers on the reserved set, they
/// will not necessarily get disconnected (depending on available free slots in the peer set).
/// If you want to also disconnect those removed peers, you will have to call
/// `remove_from_peers_set` on those in addition to updating the reserved set. You can omit
/// this step if the peer set is in reserved only mode.
///
/// Returns an `Err` if one of the given addresses is invalid or contains an
/// invalid peer ID (which includes the local peer ID).
fn set_reserved_peers(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>,
) -> Result<(), String>;

/// Add peers to a peer set.
///
/// Each `Multiaddr` must end with a `/p2p/` component containing the `PeerId`. It can also
/// consist of only `/p2p/<peerid>`.
///
/// Returns an `Err` if one of the given addresses is invalid or contains an
/// invalid peer ID (which includes the local peer ID).
fn add_peers_to_reserved_set(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>,
) -> Result<(), String>;

/// Remove peers from a peer set.
fn remove_peers_from_reserved_set(&self, protocol: Cow<'static, str>, peers: Vec<PeerId>);
Copy link
Member

Choose a reason for hiding this comment

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

Most of these functions are basically duplicated, just with some small differences in their behavior. I think, merging them and controlling the exact behavior with an enum as arguments sounds better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure, having separate names is more convenient IMO. The primary goal in this PR is to not change API if at all possible, just moving things around.

Comment on lines 250 to 251
/// Returns the number of peers we're connected to.
fn num_connected(&self) -> usize;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Returns the number of peers we're connected to.
fn num_connected(&self) -> usize;
/// Returns the number of peers in the sync peer set we're connected to.
fn sync_num_connected(&self) -> usize;

Comment on lines +384 to +385
/// NOTE: Traits can't consume itself, but calling this method second time will return an error.
fn send(&mut self, notification: Vec<u8>) -> Result<(), NotificationSenderError>;
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@nazar-pc nazar-pc Aug 9, 2022

Choose a reason for hiding this comment

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

Comment on lines +393 to +394
async fn ready(&self)
-> Result<Box<dyn NotificationSenderReady + '_>, NotificationSenderError>;
Copy link
Member

Choose a reason for hiding this comment

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

Why not have an associated type as return value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it either doesn't work at all or becomes tricky with trait object when we want to use it.

Copy link
Member

Choose a reason for hiding this comment

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

Then we don't need any trait object? And then we also can make the function consume the object. Sounds like a win win to me :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The challenge is that both before and after this PR various places use trait objects of networking traits (to store them as properties of other structs for instance). Having everything generic throughout is too invasive change with almost unbounded scope unfortunately.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm okay

@bkchr
Copy link
Member

bkchr commented Aug 9, 2022

@nazar-pc and also please provide your polkadot address for a tip :)

@nazar-pc
Copy link
Contributor Author

nazar-pc commented Aug 9, 2022

I'm approving this as it looks good. However, we should stop here. There is no need to have Substrate being completely opaque to the underlying network. This mainly just introduces complexity that we don't really need.

I have just one small upcoming PR where explicit sc-network dependency is replaced in a few places with interfaces from sc-network-common, but no other changes planned as it is already abstracted away completely and basically everywhere (this have been a bit of detour I agree, but we did need this in our protocol). Substrate aims to be modular, sorry if I went too far here 🙈

The task should really be to make networking agnostic that there exists a syncing protocol. It should not need to care. The networking crate should only provide ways to register protocols and ensure to drive these protocols. One of these protocols is then syncing.

Understood.

and also please provide your polkadot address for a tip :)

✔️

@bkchr
Copy link
Member

bkchr commented Aug 9, 2022

I have just one small upcoming PR where explicit sc-network dependency is replaced in a few places with interfaces from sc-network-common, but no other changes planned as it is already abstracted away completely and basically everywhere (this have been a bit of detour I agree, but we did need this in our protocol). Substrate aims to be modular, sorry if I went too far here see_no_evil

This should be fine, especially if you have some need for this :) Just wanted to highlight that we should stop here 😬

@bkchr
Copy link
Member

bkchr commented Aug 9, 2022

/tip medium

@substrate-tip-bot
Copy link

@bkchr A medium tip was successfully submitted for nazar-pc (1vSxzbyz2cJREAuVWjhXUT1ds8vBzoxn2w4asNpusQKwjJd on polkadot).

https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/treasury/tips

@bkchr bkchr added A6-seemsok B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Aug 9, 2022
@bkchr
Copy link
Member

bkchr commented Aug 9, 2022

bot merge

@paritytech-processbot paritytech-processbot bot merged commit fece065 into paritytech:master Aug 9, 2022
paritytech-processbot bot pushed a commit to paritytech/polkadot that referenced this pull request Aug 9, 2022
* Trivial networking changes for Substrate PR paritytech/substrate#11940

* update lockfile for {"substrate"}

Co-authored-by: parity-processbot <>
bkchr pushed a commit to paritytech/cumulus that referenced this pull request Aug 9, 2022
* Trivial networking changes for Substrate PR paritytech/substrate#11940

* Apply formatting rules

* update lockfile for {"polkadot", "substrate"}

Co-authored-by: parity-processbot <>
@nazar-pc nazar-pc deleted the network-sync-refactoring-part-6 branch August 10, 2022 16:24
pepoviola pushed a commit to paritytech/cumulus that referenced this pull request Aug 10, 2022
* Trivial networking changes for Substrate PR paritytech/substrate#11940

* Apply formatting rules

* update lockfile for {"polkadot", "substrate"}

Co-authored-by: parity-processbot <>
bkchr pushed a commit to paritytech/cumulus that referenced this pull request Aug 18, 2022
* changes to read json spec in test binary

* add zombienet tests

* fmt

* use {{COL_IMAGE}} and clean config

* add comment and use relay image from env

* use test-parachain image from pr

* fix warns

* fix warns

* fmt

* typo

* fix ci to use zombienet image

* fix spawn nodes for test

* reorg test

* add within to test

* remove check for full node collators is up

* add tests for pov, mirate solo to para, sync blocks

* bump zombienet image

* add job dep with artifacts

* add sleep for test

* fix after merge

* fmt

* bump zombienet version

* changes from clap

* use base/shared params

* fmt

* debug ci

* add upgrade test

* update js test for debug

* less debug in test

* print assertion

* fix upgrade test

* Collator key only needed if we run as collator

* [Fix] Benchmark build artifact folder creation (#1518)

* Trivial networking changes for Substrate PR #11940 (#1486)

* Trivial networking changes for Substrate PR paritytech/substrate#11940

* Apply formatting rules

* update lockfile for {"polkadot", "substrate"}

Co-authored-by: parity-processbot <>

* bump zombienet version

* update network def for test

* typo

Co-authored-by: Sebastian Kunert <[email protected]>
Co-authored-by: Roman Useinov <[email protected]>
Co-authored-by: Nazar Mokrynskyi <[email protected]>
DaviRain-Su pushed a commit to octopus-network/substrate that referenced this pull request Aug 23, 2022
* Extract `NetworkKVProvider` trait in `sc-authority-discovery` and remove unnecessary dependency

* Extract `NetworkSyncForkRequest` trait in `sc-finality-grandpa`

* Relax requirements on `SyncOracle` trait, remove extra native methods from `NetworkService` that are already provided by trait impls

* Move `NetworkSigner` trait from `sc-authority-discovery` into `sc-network-common` and de-duplicate methods on `NetworkService`

* Move `NetworkKVProvider` trait from `sc-authority-discovery` into `sc-network-common` and de-duplicate methods on `NetworkService`

* Minimize `sc-authority-discovery` dependency on `sc-network`

* Move `NetworkSyncForkRequest` trait from `sc-finality-grandpa` to `sc-network-common` and de-duplicate methods in `NetworkService`

* Extract `NetworkStatusProvider` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkPeers` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkEventStream` trait and de-duplicate methods on `NetworkService`

* Move more methods from `NetworkService` into `NetworkPeers` trait

* Move `NetworkStateInfo` trait into `sc-network-common`

* Extract `NetworkNotification` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkRequest` trait and de-duplicate methods on `NetworkService`

* Remove `NetworkService::local_peer_id()`, it is already provided by `NetworkStateInfo` impl

* Extract `NetworkTransaction` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkBlock` trait and de-duplicate methods on `NetworkService`

* Remove dependencies on `NetworkService` from most of the methods of `sc-service`

* Address simple review comments
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* Extract `NetworkKVProvider` trait in `sc-authority-discovery` and remove unnecessary dependency

* Extract `NetworkSyncForkRequest` trait in `sc-finality-grandpa`

* Relax requirements on `SyncOracle` trait, remove extra native methods from `NetworkService` that are already provided by trait impls

* Move `NetworkSigner` trait from `sc-authority-discovery` into `sc-network-common` and de-duplicate methods on `NetworkService`

* Move `NetworkKVProvider` trait from `sc-authority-discovery` into `sc-network-common` and de-duplicate methods on `NetworkService`

* Minimize `sc-authority-discovery` dependency on `sc-network`

* Move `NetworkSyncForkRequest` trait from `sc-finality-grandpa` to `sc-network-common` and de-duplicate methods in `NetworkService`

* Extract `NetworkStatusProvider` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkPeers` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkEventStream` trait and de-duplicate methods on `NetworkService`

* Move more methods from `NetworkService` into `NetworkPeers` trait

* Move `NetworkStateInfo` trait into `sc-network-common`

* Extract `NetworkNotification` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkRequest` trait and de-duplicate methods on `NetworkService`

* Remove `NetworkService::local_peer_id()`, it is already provided by `NetworkStateInfo` impl

* Extract `NetworkTransaction` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkBlock` trait and de-duplicate methods on `NetworkService`

* Remove dependencies on `NetworkService` from most of the methods of `sc-service`

* Address simple review comments
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants