Skip to content
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

Add block_gossip Beacon API events #5864

Merged
merged 39 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ab99514
Add bls event
chong-he May 15, 2024
fa1aebe
Update events and types
chong-he May 16, 2024
ecd82f3
Add bls in event
chong-he May 22, 2024
b5adaa5
Event bls
chong-he May 22, 2024
71cde60
tests..rs
chong-he May 23, 2024
10ba9ca
change order
chong-he May 26, 2024
7f54e9c
another tests.rs
chong-he May 27, 2024
1146bc7
Signed BLS
chong-he May 27, 2024
fc8351a
Revert "another tests.rs"
chong-he May 27, 2024
9316cc5
Revert "Signed BLS"
chong-he May 27, 2024
d6971b0
withdrawal_keyparis
chong-he May 27, 2024
34b102c
Fix genesis
chong-he May 27, 2024
078f927
block gossip
chong-he May 29, 2024
365a453
Add definition for BlockGossip
chong-he May 29, 2024
384e79c
Fix block gossip
chong-he May 29, 2024
871b3f3
Tests.rs
chong-he May 30, 2024
98a20d9
Update block and events
chong-he May 30, 2024
1938014
Add bls event
chong-he May 15, 2024
5d0e870
Event bls
chong-he May 22, 2024
fbe2e06
tests..rs
chong-he May 23, 2024
69b9a0d
change order
chong-he May 26, 2024
c85736f
another tests.rs
chong-he May 27, 2024
ae1cc79
Signed BLS
chong-he May 27, 2024
c786e3b
Revert "another tests.rs"
chong-he May 27, 2024
3e1f84d
Revert "Signed BLS"
chong-he May 27, 2024
f454916
block gossip
chong-he May 29, 2024
281c597
Add definition for BlockGossip
chong-he May 29, 2024
a3dfdc2
Fix block gossip
chong-he May 29, 2024
a820c15
Tests.rs
chong-he May 30, 2024
83d3373
Update block and events
chong-he May 30, 2024
e1bcf30
Merge branch 'BeaconAPI-events-block-gossip' of https://github.com/ch…
chong-he May 30, 2024
1ca52ff
Remove tests
chong-he May 30, 2024
863a21b
Tests.rs
chong-he Jun 2, 2024
796d61d
Tests.rs
chong-he Jun 2, 2024
69a1042
Tests.rs
chong-he Jun 3, 2024
dd2cf61
Tests similar to block event
chong-he Jun 3, 2024
f9882a1
Update common/eth2/src/types.rs
chong-he Jun 4, 2024
a19e1d1
Merge remote-tracking branch 'origin/unstable' into BeaconAPI-events-…
chong-he Jun 6, 2024
1022da5
Fix tests
chong-he Jun 6, 2024
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
12 changes: 11 additions & 1 deletion beacon_node/beacon_chain/src/block_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use crate::{
metrics, BeaconChain, BeaconChainError, BeaconChainTypes,
};
use derivative::Derivative;
use eth2::types::{EventKind, PublishBlockRequest};
use eth2::types::{BlockGossip, EventKind, PublishBlockRequest};
use execution_layer::PayloadStatus;
pub use fork_choice::{AttestationFromBlock, PayloadVerificationStatus};
use parking_lot::RwLockReadGuard;
Expand Down Expand Up @@ -975,6 +975,16 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
// Validate the block's execution_payload (if any).
validate_execution_payload_for_gossip(&parent_block, block.message(), chain)?;

// Beacon API block_gossip events
if let Some(event_handler) = chain.event_handler.as_ref() {
if event_handler.has_block_gossip_subscribers() {
event_handler.register(EventKind::BlockGossip(Box::new(BlockGossip {
slot: block.slot(),
block: block_root,
})));
}
}

// Having checked the proposer index and the block root we can cache them.
let consensus_context = ConsensusContext::new(block.slot())
.set_current_block_root(block_root)
Expand Down
15 changes: 15 additions & 0 deletions beacon_node/beacon_chain/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct ServerSentEventHandler<E: EthSpec> {
proposer_slashing_tx: Sender<EventKind<E>>,
attester_slashing_tx: Sender<EventKind<E>>,
bls_to_execution_change_tx: Sender<EventKind<E>>,
block_gossip_tx: Sender<EventKind<E>>,
log: Logger,
}

Expand Down Expand Up @@ -51,6 +52,7 @@ impl<E: EthSpec> ServerSentEventHandler<E> {
let (proposer_slashing_tx, _) = broadcast::channel(capacity);
let (attester_slashing_tx, _) = broadcast::channel(capacity);
let (bls_to_execution_change_tx, _) = broadcast::channel(capacity);
let (block_gossip_tx, _) = broadcast::channel(capacity);

Self {
attestation_tx,
Expand All @@ -69,6 +71,7 @@ impl<E: EthSpec> ServerSentEventHandler<E> {
proposer_slashing_tx,
attester_slashing_tx,
bls_to_execution_change_tx,
block_gossip_tx,
log,
}
}
Expand Down Expand Up @@ -147,6 +150,10 @@ impl<E: EthSpec> ServerSentEventHandler<E> {
.bls_to_execution_change_tx
.send(kind)
.map(|count| log_count("bls to execution change", count)),
EventKind::BlockGossip(_) => self
.block_gossip_tx
.send(kind)
.map(|count| log_count("block gossip", count)),
};
if let Err(SendError(event)) = result {
trace!(self.log, "No receivers registered to listen for event"; "event" => ?event);
Expand Down Expand Up @@ -217,6 +224,10 @@ impl<E: EthSpec> ServerSentEventHandler<E> {
self.bls_to_execution_change_tx.subscribe()
}

pub fn subscribe_block_gossip(&self) -> Receiver<EventKind<E>> {
self.block_gossip_tx.subscribe()
}

pub fn has_attestation_subscribers(&self) -> bool {
self.attestation_tx.receiver_count() > 0
}
Expand Down Expand Up @@ -272,4 +283,8 @@ impl<E: EthSpec> ServerSentEventHandler<E> {
pub fn has_bls_to_execution_change_subscribers(&self) -> bool {
self.bls_to_execution_change_tx.receiver_count() > 0
}

pub fn has_block_gossip_subscribers(&self) -> bool {
self.block_gossip_tx.receiver_count() > 0
}
}
3 changes: 3 additions & 0 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4367,6 +4367,9 @@ pub fn serve<T: BeaconChainTypes>(
api_types::EventTopic::BlsToExecutionChange => {
event_handler.subscribe_bls_to_execution_change()
}
api_types::EventTopic::BlockGossip => {
event_handler.subscribe_block_gossip()
}
};

receivers.push(
Expand Down
30 changes: 30 additions & 0 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct ApiTester {
chain: Arc<BeaconChain<EphemeralHarnessType<E>>>,
client: BeaconNodeHttpClient,
next_block: PublishBlockRequest<E>,
block_gossip: PublishBlockRequest<E>,
reorg_block: PublishBlockRequest<E>,
attestations: Vec<Attestation<E>>,
contribution_and_proofs: Vec<SignedContributionAndProof<E>>,
Expand Down Expand Up @@ -174,6 +175,11 @@ impl ApiTester {
.await;
let next_block = PublishBlockRequest::from(next_block);

let (next_block_gossip, _next_state_gossip) = harness
.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap())
.await;
let block_gossip = PublishBlockRequest::from(next_block_gossip);

// `make_block` adds random graffiti, so this will produce an alternate block
let (reorg_block, _reorg_state) = harness
.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap() + 1)
Expand Down Expand Up @@ -286,6 +292,7 @@ impl ApiTester {
chain,
client,
next_block,
block_gossip,
reorg_block,
attestations,
contribution_and_proofs,
Expand Down Expand Up @@ -319,6 +326,11 @@ impl ApiTester {
.await;
let next_block = PublishBlockRequest::from(next_block);

let (next_block_gossip, _next_state_gossip) = harness
.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap())
.await;
let block_gossip = PublishBlockRequest::from(next_block_gossip);

// `make_block` adds random graffiti, so this will produce an alternate block
let (reorg_block, _reorg_state) = harness
.make_block(head.beacon_state.clone(), harness.chain.slot().unwrap())
Expand Down Expand Up @@ -373,6 +385,7 @@ impl ApiTester {
chain,
client,
next_block,
block_gossip,
reorg_block,
attestations,
contribution_and_proofs: vec![],
Expand Down Expand Up @@ -5219,6 +5232,7 @@ impl ApiTester {
EventTopic::Attestation,
EventTopic::VoluntaryExit,
EventTopic::Block,
EventTopic::BlockGossip,
EventTopic::Head,
EventTopic::FinalizedCheckpoint,
EventTopic::AttesterSlashing,
Expand Down Expand Up @@ -5340,6 +5354,22 @@ impl ApiTester {
&[expected_block, expected_head, expected_finalized]
);

// Test a block gossip event
self.client
.post_beacon_blocks(&self.block_gossip)
.await
.unwrap();

let block_gossip_events =
poll_events(&mut events_future, 1, Duration::from_millis(10000)).await;
assert_eq!(
block_gossip_events.as_slice(),
&[EventKind::BlockGossip(Box::new(BlockGossip {
slot: next_slot,
block: block_root,
}))]
);
michaelsproul marked this conversation as resolved.
Show resolved Hide resolved

// Test a reorg event
let mut chain_reorg_event_future = self
.client
Expand Down
14 changes: 14 additions & 0 deletions common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,12 @@ pub struct SseHead {
pub execution_optimistic: bool,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]

chong-he marked this conversation as resolved.
Show resolved Hide resolved
pub struct BlockGossip {
pub slot: Slot,
pub block: Hash256,
}
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct SseChainReorg {
pub slot: Slot,
Expand Down Expand Up @@ -1083,6 +1089,7 @@ pub enum EventKind<E: EthSpec> {
ProposerSlashing(Box<ProposerSlashing>),
AttesterSlashing(Box<AttesterSlashing<E>>),
BlsToExecutionChange(Box<SignedBlsToExecutionChange>),
BlockGossip(Box<BlockGossip>),
}

impl<E: EthSpec> EventKind<E> {
Expand All @@ -1105,6 +1112,7 @@ impl<E: EthSpec> EventKind<E> {
EventKind::ProposerSlashing(_) => "proposer_slashing",
EventKind::AttesterSlashing(_) => "attester_slashing",
EventKind::BlsToExecutionChange(_) => "bls_to_execution_change",
EventKind::BlockGossip(_) => "block_gossip",
}
}

Expand Down Expand Up @@ -1200,6 +1208,9 @@ impl<E: EthSpec> EventKind<E> {
ServerError::InvalidServerSentEvent(format!("Bls To Execution Change: {:?}", e))
})?,
)),
"block_gossip" => Ok(EventKind::BlockGossip(serde_json::from_str(data).map_err(
|e| ServerError::InvalidServerSentEvent(format!("Block Gossip: {:?}", e)),
)?)),
_ => Err(ServerError::InvalidServerSentEvent(
"Could not parse event tag".to_string(),
)),
Expand Down Expand Up @@ -1234,6 +1245,7 @@ pub enum EventTopic {
AttesterSlashing,
ProposerSlashing,
BlsToExecutionChange,
BlockGossip,
}

impl FromStr for EventTopic {
Expand All @@ -1258,6 +1270,7 @@ impl FromStr for EventTopic {
"attester_slashing" => Ok(EventTopic::AttesterSlashing),
"proposer_slashing" => Ok(EventTopic::ProposerSlashing),
"bls_to_execution_change" => Ok(EventTopic::BlsToExecutionChange),
"block_gossip" => Ok(EventTopic::BlockGossip),
_ => Err("event topic cannot be parsed.".to_string()),
}
}
Expand All @@ -1283,6 +1296,7 @@ impl fmt::Display for EventTopic {
EventTopic::AttesterSlashing => write!(f, "attester_slashing"),
EventTopic::ProposerSlashing => write!(f, "proposer_slashing"),
EventTopic::BlsToExecutionChange => write!(f, "bls_to_execution_change"),
EventTopic::BlockGossip => write!(f, "block_gossip"),
}
}
}
Expand Down
Loading