Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
sim: Track messages received in simulator
cloudhead committed 2 years ago
commit d1f4161ee9d8c55b7e9f7c42b28a9b3277470032
parent 286c639e262c4d1aefe6c40af92c32518f807d28
1 file changed +15 -2
modified radicle-node/src/test/simulator.rs
@@ -63,7 +63,7 @@ pub enum Input {
    },
    /// Disconnected from peer.
    Disconnected(NodeId, Rc<DisconnectReason>),
-
    /// Received a message from a remote peer.
+
    /// Received messages from a remote peer.
    Received(NodeId, Vec<Message>),
    /// Fetch completed for a node.
    Fetched(RepoId, NodeId, Rc<Result<fetch::FetchResult, FetchError>>),
@@ -175,6 +175,8 @@ pub struct Simulation<S, G> {
    inbox: Inbox,
    /// Events emitted during simulation.
    events: BTreeMap<NodeId, VecDeque<Event>>,
+
    /// Messages received during simulation.
+
    messages: Vec<Message>,
    /// Priority events that should happen immediately.
    priority: VecDeque<Scheduled>,
    /// Simulated latencies between nodes.
@@ -207,6 +209,7 @@ impl<S: WriteStorage + 'static, G: Signer> Simulation<S, G> {
                messages: BTreeMap::new(),
            },
            events: BTreeMap::new(),
+
            messages: Vec::new(),
            priority: VecDeque::new(),
            partitions: BTreeSet::new(),
            latencies: BTreeMap::new(),
@@ -246,6 +249,11 @@ impl<S: WriteStorage + 'static, G: Signer> Simulation<S, G> {
        self.events.entry(*node).or_default().drain(..)
    }

+
    /// Get all messages received by nodes during the simulation.
+
    pub fn messages(&mut self) -> &[Message] {
+
        &self.messages
+
    }
+

    /// Get the latency between two nodes. The minimum latency between nodes is 1 millisecond.
    pub fn latency(&self, from: NodeId, to: NodeId) -> LocalDuration {
        self.latencies
@@ -294,6 +302,10 @@ impl<S: WriteStorage + 'static, G: Signer> Simulation<S, G> {
    {
        let mut nodes: BTreeMap<_, _> = peers.into_iter().map(|p| (p.id(), p)).collect();

+
        self.messages.clear();
+
        self.events.clear();
+
        self.start_time = self.time;
+

        while self.step_(&mut nodes) {
            if !pred(self) {
                break;
@@ -406,9 +418,10 @@ impl<S: WriteStorage + 'static, G: Signer> Simulation<S, G> {
                    }
                    Input::Wake => p.wake(),
                    Input::Received(id, msgs) => {
-
                        for msg in msgs {
+
                        for msg in msgs.clone() {
                            p.received_message(id, msg);
                        }
+
                        self.messages.extend(msgs);
                    }
                    Input::Fetched(rid, nid, result) => {
                        let result = Rc::try_unwrap(result).unwrap();