Repository navigation
feat(lip2p/drand): introduce drand libp2p module #7748
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
Open
EclesioMeloJunior
wants to merge
4
commits into
fetch-beacon-gossipsub-3-drand-protobuf
from
fetch-beacon-gossipsub-4a-drand-validate
+519
−22
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8237f46
feat(lip2p/drand): introduce drand libp2p module
EclesioMeloJunior 154f7a4
chore: address comment
EclesioMeloJunior 09afbf0
chore: enable gossipsub validation
EclesioMeloJunior a279ebc
chore: remove useless code & fix build
EclesioMeloJunior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright 2019-2026 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use std::sync::LazyLock; | ||
|
|
||
| use libp2p::gossipsub::MessageAcceptance; | ||
| use prometheus_client::{ | ||
| encoding::EncodeLabelSet, | ||
| metrics::{counter::Counter, family::Family}, | ||
| }; | ||
|
|
||
| /// Why a gossiped `drand` entry got its verdict. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub(super) enum Reason { | ||
| Verified, | ||
| InvalidSignature, | ||
| Malformed, | ||
| Future, | ||
| Stale, | ||
| NoSlot, | ||
| NoBeacon, | ||
| } | ||
|
|
||
| impl Reason { | ||
| fn as_str(self) -> &'static str { | ||
| match self { | ||
| Self::Verified => "verified", | ||
| Self::InvalidSignature => "invalid_signature", | ||
| Self::Malformed => "malformed", | ||
| Self::Future => "future", | ||
| Self::Stale => "stale", | ||
| Self::NoSlot => "no_slot", | ||
| Self::NoBeacon => "no_beacon", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)] | ||
| struct VerdictLabels { | ||
| verdict: &'static str, | ||
| reason: &'static str, | ||
| } | ||
|
|
||
| // Exported as `drand_gossip_entries_total`. | ||
| static DRAND_GOSSIP_ENTRIES: LazyLock<Family<VerdictLabels, Counter>> = LazyLock::new(|| { | ||
| let metric = Family::default(); | ||
| crate::metrics::default_registry().register( | ||
| "drand_gossip_entries", | ||
| "drand beacon entries received over gossipsub, by validation verdict and reason", | ||
| metric.clone(), | ||
| ); | ||
| metric | ||
| }); | ||
|
|
||
| pub(super) fn record(acceptance: &MessageAcceptance, reason: Reason) { | ||
| let verdict = match acceptance { | ||
| MessageAcceptance::Accept => "accept", | ||
| MessageAcceptance::Reject => "reject", | ||
| MessageAcceptance::Ignore => "ignore", | ||
| }; | ||
| DRAND_GOSSIP_ENTRIES | ||
| .get_or_create(&VerdictLabels { | ||
| verdict, | ||
| reason: reason.as_str(), | ||
| }) | ||
| .inc(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright 2019-2026 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| mod metrics; | ||
| #[cfg(test)] | ||
| pub(in crate::libp2p) mod test_utils; | ||
| mod validator; | ||
|
|
||
| pub(in crate::libp2p) use validator::DrandGossipValidator; | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use libp2p::{ | ||
| PeerId, | ||
| gossipsub::{MessageId, TopicHash}, | ||
| }; | ||
|
|
||
| use super::service::Verdict; | ||
| use crate::beacon::{Beacon as _, BeaconSchedule}; | ||
|
|
||
| /// `gossipsub` topic prefix of a `drand` network, followed by its chain hash. | ||
| const PUBSUB_DRAND_STR: &str = "/drand/pubsub/v0.0.0"; | ||
|
|
||
| /// The unchained beacon's `drand` topic and its validator. | ||
| pub struct DrandGossip { | ||
| pub(in crate::libp2p) topic: TopicHash, | ||
| pub(in crate::libp2p) verdicts: flume::Receiver<Verdict>, | ||
| validator: DrandGossipValidator, | ||
| } | ||
|
|
||
| impl DrandGossip { | ||
| /// `None` without an unchained beacon. `schedule` must be the one block validation uses, as | ||
| /// gossip fills its cache. | ||
| pub fn new(schedule: Arc<BeaconSchedule>) -> Option<Self> { | ||
| let beacon = schedule.unchained_beacon()?; | ||
| let topic = TopicHash::from_raw(format!("{PUBSUB_DRAND_STR}/{}", beacon.chain_hash()?)); | ||
| let (verdicts_tx, verdicts) = flume::unbounded(); | ||
| Some(Self { | ||
| topic, | ||
| verdicts, | ||
| validator: DrandGossipValidator::new(schedule, verdicts_tx), | ||
| }) | ||
| } | ||
|
|
||
| /// Validates a topic message; the verdict arrives on [`Self::verdicts`]. | ||
| pub(in crate::libp2p) fn on_message(&self, message_id: MessageId, source: PeerId, data: &[u8]) { | ||
| self.validator | ||
| .handle(data, message_id, source, unix_now_secs()); | ||
| } | ||
| } | ||
|
|
||
| fn unix_now_secs() -> u64 { | ||
| chrono::Utc::now().timestamp().max(0) as u64 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright 2019-2026 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use libp2p::gossipsub::MessageAcceptance; | ||
|
|
||
| use crate::beacon::{ | ||
| Beacon as _, BeaconPoint, BeaconSchedule, tests::fake_drand_signer::FakeDrandSigner, | ||
| }; | ||
|
|
||
| /// On the test chain, every epoch draws from a round ending in 2. | ||
| pub(in crate::libp2p) const EPOCH_ROUND: u64 = 12; | ||
|
|
||
| pub(in crate::libp2p) fn fake_drand_schedule() -> (FakeDrandSigner, Arc<BeaconSchedule>) { | ||
| let drand = FakeDrandSigner::new(); | ||
| let beacon = drand.beacon(); | ||
| ( | ||
| drand, | ||
| Arc::new(BeaconSchedule(vec![BeaconPoint::new(0, beacon)])), | ||
| ) | ||
| } | ||
|
|
||
| /// Unix time right after `round` is produced by the fake `drand` chain. | ||
| pub(in crate::libp2p) fn time_of(schedule: &BeaconSchedule, round: u64) -> u64 { | ||
| schedule | ||
| .unchained_beacon() | ||
| .and_then(|beacon| beacon.beacon_round_timestamp(round)) | ||
| .expect("fake drand beacon has round timestamps") | ||
| + 1 | ||
| } | ||
|
|
||
| pub(in crate::libp2p) fn is_cached(schedule: &BeaconSchedule, round: u64) -> bool { | ||
| schedule | ||
| .unchained_beacon() | ||
| .is_some_and(|beacon| beacon.is_cached(round)) | ||
| } | ||
|
|
||
| // `MessageAcceptance` doesn't implement `PartialEq`. | ||
| pub(in crate::libp2p) fn assert_verdict(actual: &MessageAcceptance, expected: MessageAcceptance) { | ||
| assert_eq!( | ||
| std::mem::discriminant(actual), | ||
| std::mem::discriminant(&expected), | ||
| "verdict {actual:?}, expected {expected:?}" | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.