Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/blockchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ impl BlockChainServer {
signature,
};

// Self-deliver: store our own attestation locally for aggregation.
// Gossipsub does not deliver messages back to the sender, so without
// this the aggregator never sees its own validator's signature in
// gossip_signatures and it is excluded from aggregated proofs.
if self.is_aggregator {
let _ = store::on_gossip_attestation(&mut self.store, signed_attestation.clone())
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can we avoid this clone? we can do this in another PR

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think we can: #271

.inspect_err(|err| {
warn!(%slot, %validator_id, %err, "Self-delivery of attestation failed")
});
}

// Publish to gossip network
if let Some(ref p2p) = self.p2p {
let _ = p2p.publish_attestation(signed_attestation).inspect_err(
Expand Down
14 changes: 12 additions & 2 deletions crates/net/p2p/src/gossipsub/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,18 @@ pub async fn publish_attestation(server: &mut P2PServer, attestation: SignedAtte
.cloned()
.unwrap_or_else(|| attestation_subnet_topic(subnet_id));

// Publish to the attestation subnet topic
server.swarm_handle.publish(topic, compressed);
// Publish to the attestation subnet topic.
// Aggregators are subscribed to the subnet, so gossipsub uses mesh (not fanout).
// In small networks where no other node subscribes, this returns
// NoPeersSubscribedToTopic. Use best-effort to suppress the expected warning;
// the aggregator already self-delivered its attestation locally.
if server.is_aggregator {
server
.swarm_handle
.publish_ignore_no_peers(topic, compressed);
} else {
server.swarm_handle.publish(topic, compressed);
}
info!(
%slot,
validator,
Expand Down
4 changes: 4 additions & 0 deletions crates/net/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub struct BuiltSwarm {
pub(crate) block_topic: libp2p::gossipsub::IdentTopic,
pub(crate) aggregation_topic: libp2p::gossipsub::IdentTopic,
pub(crate) bootnode_addrs: HashMap<PeerId, Multiaddr>,
pub(crate) is_aggregator: bool,
}

/// Build and configure the libp2p swarm, dial bootnodes, subscribe to topics.
Expand Down Expand Up @@ -257,6 +258,7 @@ pub fn build_swarm(
block_topic,
aggregation_topic,
bootnode_addrs,
is_aggregator: config.is_aggregator,
})
}

Expand All @@ -280,6 +282,7 @@ impl P2P {
attestation_committee_count: built.attestation_committee_count,
block_topic: built.block_topic,
aggregation_topic: built.aggregation_topic,
is_aggregator: built.is_aggregator,
connected_peers: HashSet::new(),
pending_requests: HashMap::new(),
request_id_map: HashMap::new(),
Expand Down Expand Up @@ -314,6 +317,7 @@ pub struct P2PServer {
pub(crate) attestation_committee_count: u64,
pub(crate) block_topic: libp2p::gossipsub::IdentTopic,
pub(crate) aggregation_topic: libp2p::gossipsub::IdentTopic,
pub(crate) is_aggregator: bool,

pub(crate) connected_peers: HashSet<PeerId>,
pub(crate) pending_requests: HashMap<H256, PendingRequest>,
Expand Down
40 changes: 33 additions & 7 deletions crates/net/p2p/src/swarm_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libp2p::{
Multiaddr, PeerId, StreamProtocol,
futures::StreamExt,
gossipsub::PublishError,
request_response::{self, OutboundRequestId},
swarm::SwarmEvent,
};
Expand All @@ -13,6 +14,8 @@ pub enum SwarmCommand {
Publish {
topic: libp2p::gossipsub::IdentTopic,
data: Vec<u8>,
/// When true, suppress NoPeersSubscribedToTopic errors (other errors still warn).
ignore_no_peers: bool,
},
Dial(Multiaddr),
SendRequest {
Expand All @@ -37,7 +40,25 @@ impl SwarmHandle {
pub fn publish(&self, topic: libp2p::gossipsub::IdentTopic, data: Vec<u8>) {
let _ = self
.cmd_tx
.send(SwarmCommand::Publish { topic, data })
.send(SwarmCommand::Publish {
topic,
data,
ignore_no_peers: false,
})
.inspect_err(|_| warn!("Swarm adapter closed, cannot publish"));
}

/// Publish, suppressing NoPeersSubscribedToTopic errors. Used when the sender
/// is also subscribed to the topic (e.g., aggregator publishing its own
/// attestation to a subnet it subscribes to) and no other peer subscribes.
pub fn publish_ignore_no_peers(&self, topic: libp2p::gossipsub::IdentTopic, data: Vec<u8>) {
let _ = self
.cmd_tx
.send(SwarmCommand::Publish {
topic,
data,
ignore_no_peers: true,
})
.inspect_err(|_| warn!("Swarm adapter closed, cannot publish"));
}

Expand Down Expand Up @@ -123,12 +144,17 @@ async fn swarm_loop(

fn execute_command(swarm: &mut libp2p::Swarm<Behaviour>, cmd: SwarmCommand) {
match cmd {
SwarmCommand::Publish { topic, data } => {
let _ = swarm
.behaviour_mut()
.gossipsub
.publish(topic, data)
.inspect_err(|err| warn!(%err, "Swarm adapter: publish failed"));
SwarmCommand::Publish {
topic,
data,
ignore_no_peers,
} => {
let result = swarm.behaviour_mut().gossipsub.publish(topic, data);
if let Err(err) = result
&& !(ignore_no_peers && matches!(err, PublishError::NoPeersSubscribedToTopic))
{
warn!(%err, "Swarm adapter: publish failed");
}
}
SwarmCommand::Dial(addr) => {
let _ = swarm
Expand Down
Loading