Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subnet service #115

Draft
wants to merge 5 commits into
base: unstable
Choose a base branch
from
Draft

Subnet service #115

wants to merge 5 commits into from

Conversation

dknopik
Copy link
Member

@dknopik dknopik commented Jan 23, 2025

Proposed Changes

Small service that gets notified when a validator gets added or removed and passes on instructions to subscribe/unsubscribe/look for peers

@dknopik dknopik changed the base branch from stable to unstable January 23, 2025 16:13
Copy link

@diegomrsantos diegomrsantos left a comment

Choose a reason for hiding this comment

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

Thank you for starting this! I added some comments.

{
error!(?err, subnet = *subnet, "can't subscribe");
}
self.swarm
Copy link

@diegomrsantos diegomrsantos Jan 23, 2025

Choose a reason for hiding this comment

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

Interestingly, Lighthouse doesn't do that. There could be a check somewhere else (PeerManager?) that keeps the target number of peers for each subnet.

anchor/network/src/subnet.rs Outdated Show resolved Hide resolved
anchor/network/src/subnet.rs Outdated Show resolved Hide resolved
@dknopik
Copy link
Member Author

dknopik commented Jan 28, 2025

Pushed a new design based on #117.

Also pulled out the logic from network into its own crate, but kept the full logic (mapping updates to subnets) in one place. The reasoning behind that is that I think we should avoid bothering the network create with events as much as possible to avoid computation in the network service if possible.

Comment on lines +60 to +61
executor.spawn(subnet_tracker(tx, db, subnet_count), "subnet_tracker");
SubnetTracker { events: rx }

Choose a reason for hiding this comment

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

What is subnet_tracker doing? It feels a bit confusing to me, it's being called before the SubnetTrackerinstance is created right after.

Still,
}

async fn subnet_tracker(

Choose a reason for hiding this comment

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

Maybe this could be a start function called on a SubnetTracker instance?

Choose a reason for hiding this comment

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

Something like

pub struct SubnetTracker {
    db: watch::Receiver<NetworkState>,
    subnet_count: usize,
    events_tx: mpsc::Sender<SubnetEvent>,
    events_rx: mpsc::Receiver<SubnetEvent>,
}

impl SubnetTracker {
    pub fn new(db: watch::Receiver<NetworkState>, subnet_count: usize) -> Self {
        let (tx, rx) = mpsc::channel(1);
        Self {
            db,
            subnet_count,
            events_tx: tx,
            events_rx: rx,
        }
    }

    /// Spawn the background task that processes network state updates.
    pub fn start(&self, executor: &TaskExecutor) {
        let db = self.db.clone();          // watchers can be cloned
        let tx = self.events_tx.clone();
        let subnet_count = self.subnet_count;

        executor.spawn(async move {
            run_subnet_tracker_loop(tx, db, subnet_count).await;
        }, "subnet_tracker");
    }

    /// Receive subnet events from the background task.
    pub async fn recv(&mut self) -> Option<SubnetEvent> {
        self.events_rx.recv().await
    }
}

async fn run_subnet_tracker_loop(
    tx: mpsc::Sender<SubnetEvent>,
    mut db: watch::Receiver<NetworkState>,
    subnet_count: usize
) {
    // The same logic as the existing `subnet_tracker` function
}

Wdyt? It might make things easier to understand and test. Or it could just be my OOP mind.

Copy link
Member Author

Choose a reason for hiding this comment

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

Some arguments against this:

  • Holding a sender unnecessarily will prevent us from detecting that the actual subnet_tracker has exited.
  • All the data in the struct (except for the receiver) is unnecessary once the subnet_tracker has started, and then still carried around uselessly.
  • I argue that this actually makes testing harder and increases coupling. The test_tracker at the bottom of the file shows nicely that it is trivial with the current setup to create a mock that does not require a db: watch::Receiver<NetworkState>.

Some alternatives:

  • Rename SubnetTracker to SubnetTrackerHandle or SubnetTrackingReceiver or something like that to make it a bit clearer
  • No struct at all, just pass the mpsc::Receiver<SubnetEvent> around.

Choose a reason for hiding this comment

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

I agree, SubnetTracker isn't doing much currently, getting rid of it would probably make it simpler than my suggestion.

Co-authored-by: diegomrsantos <diegomrsantos@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants