-
Notifications
You must be signed in to change notification settings - Fork 200
feat(drand): fetch drand beacon entry from gossipsub #7544
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
21
commits into
main
Choose a base branch
from
fetch-beacon-gossipsub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c995f2b
wip
EclesioMeloJunior f113fa5
chore: implement gosssip subscription + watchdog
EclesioMeloJunior 360d542
chore: rever the metrics changes
EclesioMeloJunior e5c47e3
chore: rever the metrics changes
EclesioMeloJunior 377152b
chore: use block delay in secs
EclesioMeloJunior cd68fd4
Merge branch 'fetch-beacon-gossipsub' of github.com:ChainSafe/forest …
EclesioMeloJunior bd4985a
Merge branch 'main' of github.com:ChainSafe/forest into fetch-beacon-…
EclesioMeloJunior ac6700e
chore: introduce resubscription
EclesioMeloJunior 0f17015
chore: add tests
EclesioMeloJunior f146c60
chore: update changelog
EclesioMeloJunior 3d19243
chore: fix lint
EclesioMeloJunior 030e607
Merge branch 'main' of github.com:ChainSafe/forest into fetch-beacon-…
EclesioMeloJunior 67c88e3
Merge branch 'main' of github.com:ChainSafe/forest into fetch-beacon-…
EclesioMeloJunior 2031c75
Merge branch 'main' into fetch-beacon-gossipsub
EclesioMeloJunior 2f8061a
chore: remove changelog entry, place proto permalink, scoped const,
EclesioMeloJunior 426f2b0
chore: run `beacon.entry(...)` under cancellation_token.run_until_can…
EclesioMeloJunior 5533930
Merge branch 'fetch-beacon-gossipsub' of github.com:ChainSafe/forest …
EclesioMeloJunior ab1030f
chore: start the watchdog ticker correctly
EclesioMeloJunior 370cee6
Merge branch 'main' into fetch-beacon-gossipsub
EclesioMeloJunior 5296c11
Merge branch 'main' into fetch-beacon-gossipsub
EclesioMeloJunior 7012c15
Merge branch 'main' into fetch-beacon-gossipsub
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package drand_pb; | ||
|
|
||
| // https://github.com/drand/drand/blob/v2.1.7/protobuf/drand/api.proto#L42-L53 | ||
| message PublicRandResponse { | ||
|
EclesioMeloJunior marked this conversation as resolved.
|
||
| uint64 round = 1; | ||
| bytes signature = 2; | ||
| } | ||
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,55 @@ | ||
| // Copyright 2019-2026 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
| // Automatically generated rust module for 'drand_pb.proto' file | ||
| // Command: `pb-rs -s -D proto/drand_pb.proto`, See <https://crates.io/crates/pb-rs> | ||
|
|
||
| #![allow(non_snake_case)] | ||
| #![allow(non_upper_case_globals)] | ||
| #![allow(non_camel_case_types)] | ||
| #![allow(unused_imports)] | ||
| #![allow(unknown_lints)] | ||
| #![allow(clippy::all)] | ||
| #![allow(clippy::assigning_clones)] | ||
| #![cfg_attr(rustfmt, rustfmt_skip)] | ||
|
|
||
|
|
||
| use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; | ||
| use quick_protobuf::sizeofs::*; | ||
| use super::*; | ||
|
|
||
| #[allow(clippy::derive_partial_eq_without_eq)] | ||
| #[derive(Debug, Default, PartialEq, Clone)] | ||
| pub struct PublicRandResponse { | ||
| pub round: u64, | ||
| pub signature: Vec<u8>, | ||
| } | ||
|
|
||
| impl<'a> MessageRead<'a> for PublicRandResponse { | ||
| fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> { | ||
| let mut msg = Self::default(); | ||
| while !r.is_eof() { | ||
| match r.next_tag(bytes) { | ||
| Ok(8) => msg.round = r.read_uint64(bytes)?, | ||
| Ok(18) => msg.signature = r.read_bytes(bytes)?.to_owned(), | ||
| Ok(t) => { r.read_unknown(bytes, t)?; } | ||
| Err(e) => return Err(e), | ||
| } | ||
| } | ||
| Ok(msg) | ||
| } | ||
| } | ||
|
|
||
| impl MessageWrite for PublicRandResponse { | ||
| fn get_size(&self) -> usize { | ||
| 0 | ||
| + if self.round == 0u64 { 0 } else { 1 + sizeof_varint(*(&self.round) as u64) } | ||
| + if self.signature.is_empty() { 0 } else { 1 + sizeof_len((&self.signature).len()) } | ||
| } | ||
|
|
||
| fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> { | ||
| if self.round != 0u64 { w.write_with_tag(8, |w| w.write_uint64(*&self.round))?; } | ||
| if !self.signature.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.signature))?; } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
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,95 @@ | ||
| use crate::beacon::{Beacon, BeaconEntry, ChainInfo, DrandBeacon, DrandConfig, DrandNetwork}; | ||
| use blstrs::{G1Projective, G2Projective, Scalar}; | ||
| use group::{Curve, Group}; | ||
|
|
||
| pub const FAKE_DRAND_GENESIS_TIME: i32 = 1_692_803_367; | ||
| pub const FAKE_DRAND_PERIOD: i32 = 3; | ||
|
|
||
| pub const TEST_FIL_GENESIS_TIME: u64 = 1_598_306_400; | ||
| pub const TEST_FIL_BLOCK_DELAY: u64 = 30; | ||
|
|
||
| pub struct FakeDrand { | ||
| secret: Scalar, | ||
| config: DrandConfig<'static>, | ||
| } | ||
|
|
||
| impl FakeDrand { | ||
| pub fn new(servers: Vec<url::Url>, period: i32, genesis_time: i32) -> Self { | ||
| let secret = Scalar::from(0xC0FFEEu64); | ||
| let public = G2Projective::generator() * secret; | ||
| let public_key = hex::encode(public.to_affine().to_compressed()); | ||
| Self { | ||
| secret, | ||
| config: DrandConfig { | ||
| servers, | ||
| chain_info: ChainInfo { | ||
| public_key: public_key.into(), | ||
| period, | ||
| genesis_time, | ||
| hash: "0011".repeat(16).into(), | ||
| group_hash: "00".repeat(32).into(), | ||
| }, | ||
| network_type: DrandNetwork::Quicknet, // unchained | ||
| // The fixture builds beacons directly; registering a collector here | ||
| // would clash with the one the real quicknet config registers. | ||
| register_metrics: false, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // sign H(round) on G1, exactly what `verify_entries` checks for unchained. | ||
| pub fn entry(&self, round: u64) -> BeaconEntry { | ||
| let msg = BeaconEntry::message_unchained(round); | ||
| let point = | ||
| G1Projective::hash_to_curve(msg.as_ref(), crate::beacon::signatures::CSUITE_G1, &[]); | ||
| let point = point * self.secret; | ||
| BeaconEntry::new(round, point.to_affine().to_compressed().to_vec()) | ||
| } | ||
|
|
||
| // encode PublicRandResponse to protobuf | ||
| pub fn to_protobuf(&self, round: u64) -> Vec<u8> { | ||
| let entry = self.entry(round); | ||
| let mut out = Vec::new(); | ||
| let mut w = quick_protobuf::Writer::new(&mut out); | ||
| quick_protobuf::MessageWrite::write_message( | ||
| &crate::beacon::drand_pb::PublicRandResponse { | ||
| round, | ||
| signature: entry.signature().to_vec(), | ||
| }, | ||
| &mut w, | ||
| ) | ||
| .unwrap(); | ||
| out | ||
| } | ||
|
|
||
| pub fn to_json(&self, round: u64) -> serde_json::Value { | ||
| let entry = self.entry(round); | ||
| serde_json::json!({ | ||
| "round": round, | ||
| "randomness": "00".repeat(32), | ||
| "signature": hex::encode(entry.signature()), | ||
| "previous_signature": null, | ||
| }) | ||
| } | ||
|
|
||
| pub fn beacon(&self, genesis_ts: u64, block_delay: u64) -> DrandBeacon { | ||
| DrandBeacon::new(genesis_ts, block_delay, &self.config) | ||
| } | ||
|
|
||
| pub fn chain_info_hash(&self) -> String { | ||
| self.config.chain_info.hash.to_string() | ||
| } | ||
| } | ||
|
|
||
| // just test the secret and public keys are correctly validating | ||
| #[test] | ||
| fn fake_drand_entries_verify() { | ||
| let d = FakeDrand::new(vec![], FAKE_DRAND_PERIOD, FAKE_DRAND_GENESIS_TIME); | ||
| let beacon = d.beacon(TEST_FIL_GENESIS_TIME, TEST_FIL_BLOCK_DELAY); | ||
| let entries: Vec<_> = (1..=5).map(|r| d.entry(r)).collect(); | ||
| assert!( | ||
| beacon | ||
| .verify_entries(&entries, &BeaconEntry::default()) | ||
| .unwrap() | ||
| ); | ||
| } |
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.