snarkos_node_router/outbound.rs
1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::{Router, messages::Message};
17use snarkvm::prelude::Network;
18
19use std::net::SocketAddr;
20
21pub trait Outbound<N: Network> {
22 /// Returns a reference to the router.
23 fn router(&self) -> &Router<N>;
24
25 /// Returns `true` if the node is synced up to the latest block (within the given tolerance).
26 fn is_block_synced(&self) -> bool;
27
28 /// Returns the number of blocks this node is behind the greatest peer height,
29 /// or `None` if not connected to peers yet.
30 fn num_blocks_behind(&self) -> Option<u32>;
31
32 /// Sends the given message to every connected peer, excluding the sender and any specified peer IPs.
33 fn propagate(&self, message: Message<N>, excluded_peers: &[SocketAddr]) {
34 // TODO (howardwu): Serialize large messages once only.
35 // // Perform ahead-of-time, non-blocking serialization just once for applicable objects.
36 // if let Message::UnconfirmedSolution(ref mut message) = message {
37 // if let Ok(serialized_solution) = Data::serialize(message.solution.clone()).await {
38 // let _ = std::mem::replace(&mut message.solution, Data::Buffer(serialized_solution));
39 // } else {
40 // error!("Solution serialization is bugged");
41 // }
42 // } else if let Message::UnconfirmedTransaction(ref mut message) = message {
43 // if let Ok(serialized_transaction) = Data::serialize(message.transaction.clone()).await {
44 // let _ = std::mem::replace(&mut message.transaction, Data::Buffer(serialized_transaction));
45 // } else {
46 // error!("Transaction serialization is bugged");
47 // }
48 // }
49
50 // Prepare the peers to send to.
51 let connected_peers =
52 self.router().filter_connected_peers(|peer| !excluded_peers.contains(&peer.listener_addr));
53
54 // Iterate through all peers that are not the sender and excluded peers.
55 for addr in connected_peers.iter().map(|peer| peer.listener_addr) {
56 self.router().send(addr, message.clone());
57 }
58 }
59
60 /// Sends the given message to every connected validator, excluding the sender and any specified IPs.
61 fn propagate_to_validators(&self, message: Message<N>, excluded_peers: &[SocketAddr]) {
62 // TODO (howardwu): Serialize large messages once only.
63 // // Perform ahead-of-time, non-blocking serialization just once for applicable objects.
64 // if let Message::UnconfirmedSolution(ref mut message) = message {
65 // if let Ok(serialized_solution) = Data::serialize(message.solution.clone()).await {
66 // let _ = std::mem::replace(&mut message.solution, Data::Buffer(serialized_solution));
67 // } else {
68 // error!("Solution serialization is bugged");
69 // }
70 // } else if let Message::UnconfirmedTransaction(ref mut message) = message {
71 // if let Ok(serialized_transaction) = Data::serialize(message.transaction.clone()).await {
72 // let _ = std::mem::replace(&mut message.transaction, Data::Buffer(serialized_transaction));
73 // } else {
74 // error!("Transaction serialization is bugged");
75 // }
76 // }
77
78 // Prepare the peers to send to.
79 let connected_validators = self.router().filter_connected_peers(|peer| {
80 peer.node_type.is_validator() && !excluded_peers.contains(&peer.listener_addr)
81 });
82
83 // Iterate through all validators that are not the sender and excluded validators.
84 for listener_addr in connected_validators.iter().map(|peer| peer.listener_addr) {
85 self.router().send(listener_addr, message.clone());
86 }
87 }
88}