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

Light client updates by range rpc and add LightClientHeader #3886

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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: 12 additions & 0 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,18 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
Ok(self.store.get_state(state_root, slot)?)
}

/// Returns stored light client update at the given sync_committee_period, if any.
///
/// ## Errors
///
/// May return a database error.
pub fn get_light_client_update(
&self,
sync_committee_period: u64,
) -> Result<Option<LightClientUpdate<T::EthSpec>>, Error> {
Ok(self.store.get_light_client_update(sync_committee_period)?)
}

/// Return the sync committee at `slot + 1` from the canonical chain.
///
/// This is useful when dealing with sync committee messages, because messages are signed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use slot_clock::SlotClock;
use std::time::Duration;
use strum::AsRefStr;
use types::{
light_client_update::Error as LightClientUpdateError, LightClientFinalityUpdate, Slot,
light_client_update::Error as LightClientUpdateError, LightClientFinalityUpdate,
LightClientUpdate, Slot,
};

/// Returned when a light client finality update was not successfully verified. It might not have been verified for
Expand Down Expand Up @@ -69,7 +70,7 @@ impl<T: BeaconChainTypes> VerifiedLightClientFinalityUpdate<T> {
chain: &BeaconChain<T>,
seen_timestamp: Duration,
) -> Result<Self, Error> {
let gossiped_finality_slot = light_client_finality_update.finalized_header.slot;
let gossiped_finality_slot = light_client_finality_update.finalized_header.beacon.slot;
let one_third_slot_duration = Duration::new(chain.spec.seconds_per_slot / 3, 0);
let signature_slot = light_client_finality_update.signature_slot;
let start_time = chain.slot_clock.start_of(signature_slot);
Expand All @@ -90,7 +91,7 @@ impl<T: BeaconChainTypes> VerifiedLightClientFinalityUpdate<T> {
.get_blinded_block(&finalized_block_root)?
.ok_or(Error::FailedConstructingUpdate)?;
let latest_seen_finality_update_slot = match latest_seen_finality_update.as_ref() {
Some(update) => update.finalized_header.slot,
Some(update) => update.finalized_header.beacon.slot,
None => Slot::new(0),
};

Expand All @@ -112,13 +113,15 @@ impl<T: BeaconChainTypes> VerifiedLightClientFinalityUpdate<T> {
}

let head_state = &head.snapshot.beacon_state;
let finality_update = LightClientFinalityUpdate::new(
let update = LightClientUpdate::new(
&chain.spec,
head_state,
head_block,
&head_block.clone_as_blinded(),
&mut attested_state,
&finalized_block,
&attested_block,
Some(finalized_block),
)?;
let finality_update = LightClientFinalityUpdate::from_light_client_update(update);

// verify that the gossiped finality update is the same as the locally constructed one.
if finality_update != light_client_finality_update {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use slot_clock::SlotClock;
use std::time::Duration;
use strum::AsRefStr;
use types::{
light_client_update::Error as LightClientUpdateError, LightClientOptimisticUpdate, Slot,
light_client_update::Error as LightClientUpdateError, LightClientOptimisticUpdate,
LightClientUpdate, Slot,
};

/// Returned when a light client optimistic update was not successfully verified. It might not have been verified for
Expand Down Expand Up @@ -73,7 +74,7 @@ impl<T: BeaconChainTypes> VerifiedLightClientOptimisticUpdate<T> {
chain: &BeaconChain<T>,
seen_timestamp: Duration,
) -> Result<Self, Error> {
let gossiped_optimistic_slot = light_client_optimistic_update.attested_header.slot;
let gossiped_optimistic_slot = light_client_optimistic_update.attested_header.beacon.slot;
let one_third_slot_duration = Duration::new(chain.spec.seconds_per_slot / 3, 0);
let signature_slot = light_client_optimistic_update.signature_slot;
let start_time = chain.slot_clock.start_of(signature_slot);
Expand All @@ -86,11 +87,11 @@ impl<T: BeaconChainTypes> VerifiedLightClientOptimisticUpdate<T> {
.get_blinded_block(&attested_block_root)?
.ok_or(Error::FailedConstructingUpdate)?;

let attested_state = chain
let mut attested_state = chain
.get_state(&attested_block.state_root(), Some(attested_block.slot()))?
.ok_or(Error::FailedConstructingUpdate)?;
let latest_seen_optimistic_update_slot = match latest_seen_optimistic_update.as_ref() {
Some(update) => update.attested_header.slot,
Some(update) => update.attested_header.beacon.slot,
None => Slot::new(0),
};

Expand All @@ -115,14 +116,23 @@ impl<T: BeaconChainTypes> VerifiedLightClientOptimisticUpdate<T> {
// otherwise queue
let canonical_root = light_client_optimistic_update
.attested_header
.beacon
.canonical_root();

if canonical_root != head_block.message().parent_root() {
return Err(Error::UnknownBlockParentRoot(canonical_root));
}

let optimistic_update =
LightClientOptimisticUpdate::new(&chain.spec, head_block, &attested_state)?;
let head_state = &head.snapshot.beacon_state;
let update = LightClientUpdate::new(
&chain.spec,
head_state,
&head_block.clone_as_blinded(),
&mut attested_state,
&attested_block,
None,
)?;
let optimistic_update = LightClientOptimisticUpdate::from_light_client_update(update);

// verify that the gossiped optimistic update is the same as the locally constructed one.
if optimistic_update != light_client_optimistic_update {
Expand Down
1 change: 1 addition & 0 deletions beacon_node/beacon_chain/src/schema_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub fn migrate_schema<T: BeaconChainTypes>(

Ok(())
}
(SchemaVersion(14), SchemaVersion(15)) => db.store_schema_version_atomically(to, vec![]),
// Anything else is an error.
(_, _) => Err(HotColdDBError::UnsupportedSchemaVersion {
target_version: to,
Expand Down
3 changes: 3 additions & 0 deletions beacon_node/lighthouse_network/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
Protocol::BlocksByRange => PeerAction::MidToleranceError,
Protocol::BlocksByRoot => PeerAction::MidToleranceError,
Protocol::LightClientBootstrap => PeerAction::LowToleranceError,
Protocol::LightClientUpdatesByRange => PeerAction::LowToleranceError,
Protocol::Goodbye => PeerAction::LowToleranceError,
Protocol::MetaData => PeerAction::LowToleranceError,
Protocol::Status => PeerAction::LowToleranceError,
Expand All @@ -519,6 +520,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
Protocol::BlocksByRoot => return,
Protocol::Goodbye => return,
Protocol::LightClientBootstrap => return,
Protocol::LightClientUpdatesByRange => return,
Protocol::MetaData => PeerAction::LowToleranceError,
Protocol::Status => PeerAction::LowToleranceError,
}
Expand All @@ -534,6 +536,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
Protocol::BlocksByRange => PeerAction::MidToleranceError,
Protocol::BlocksByRoot => PeerAction::MidToleranceError,
Protocol::LightClientBootstrap => return,
Protocol::LightClientUpdatesByRange => return,
Protocol::Goodbye => return,
Protocol::MetaData => return,
Protocol::Status => return,
Expand Down
14 changes: 13 additions & 1 deletion beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use std::sync::Arc;
use tokio_util::codec::{Decoder, Encoder};
use types::{
light_client_bootstrap::LightClientBootstrap, EthSpec, ForkContext, ForkName, Hash256,
SignedBeaconBlock, SignedBeaconBlockAltair, SignedBeaconBlockBase, SignedBeaconBlockMerge,
LightClientUpdate, SignedBeaconBlock, SignedBeaconBlockAltair, SignedBeaconBlockBase,
SignedBeaconBlockMerge,
};
use unsigned_varint::codec::Uvi;

Expand Down Expand Up @@ -71,6 +72,7 @@ impl<TSpec: EthSpec> Encoder<RPCCodedResponse<TSpec>> for SSZSnappyInboundCodec<
RPCResponse::BlocksByRange(res) => res.as_ssz_bytes(),
RPCResponse::BlocksByRoot(res) => res.as_ssz_bytes(),
RPCResponse::LightClientBootstrap(res) => res.as_ssz_bytes(),
RPCResponse::LightClientUpdatesByRange(res) => res.as_ssz_bytes(),
RPCResponse::Pong(res) => res.data.as_ssz_bytes(),
RPCResponse::MetaData(res) =>
// Encode the correct version of the MetaData response based on the negotiated version.
Expand Down Expand Up @@ -232,6 +234,7 @@ impl<TSpec: EthSpec> Encoder<OutboundRequest<TSpec>> for SSZSnappyOutboundCodec<
OutboundRequest::Ping(req) => req.as_ssz_bytes(),
OutboundRequest::MetaData(_) => return Ok(()), // no metadata to encode
OutboundRequest::LightClientBootstrap(req) => req.as_ssz_bytes(),
OutboundRequest::LightClientUpdatesByRange(req) => req.as_ssz_bytes(),
};
// SSZ encoded bytes should be within `max_packet_size`
if bytes.len() > self.max_packet_size {
Expand Down Expand Up @@ -479,6 +482,9 @@ fn handle_v1_request<T: EthSpec>(
root: Hash256::from_ssz_bytes(decoded_buffer)?,
},
))),
Protocol::LightClientUpdatesByRange => Ok(Some(InboundRequest::LightClientUpdatesByRange(
LightClientUpdatesByRangeRequest::from_ssz_bytes(decoded_buffer)?,
))),
// MetaData requests return early from InboundUpgrade and do not reach the decoder.
// Handle this case just for completeness.
Protocol::MetaData => {
Expand Down Expand Up @@ -553,6 +559,9 @@ fn handle_v1_response<T: EthSpec>(
Protocol::LightClientBootstrap => Ok(Some(RPCResponse::LightClientBootstrap(
LightClientBootstrap::from_ssz_bytes(decoded_buffer)?,
))),
Protocol::LightClientUpdatesByRange => Ok(Some(RPCResponse::LightClientUpdatesByRange(
Arc::new(LightClientUpdate::from_ssz_bytes(decoded_buffer)?),
))),
}
}

Expand Down Expand Up @@ -879,6 +888,9 @@ mod tests {
OutboundRequest::LightClientBootstrap(bootstrap) => {
assert_eq!(decoded, InboundRequest::LightClientBootstrap(bootstrap))
}
OutboundRequest::LightClientUpdatesByRange(urange) => {
assert_eq!(decoded, InboundRequest::LightClientUpdatesByRange(urange))
}
}
}
}
Expand Down
38 changes: 35 additions & 3 deletions beacon_node/lighthouse_network/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ use regex::bytes::Regex;
use serde::Serialize;
use ssz_derive::{Decode, Encode};
use ssz_types::{
typenum::{U1024, U256},
typenum::{U1024, U128, U256},
VariableList,
};
use std::ops::Deref;
use std::sync::Arc;
use strum::IntoStaticStr;
use superstruct::superstruct;
use types::{
light_client_bootstrap::LightClientBootstrap, Epoch, EthSpec, Hash256, SignedBeaconBlock, Slot,
light_client_bootstrap::LightClientBootstrap, Epoch, EthSpec, Hash256, LightClientUpdate,
SignedBeaconBlock, Slot,
};

/// Maximum number of blocks in a single request.
pub type MaxRequestBlocks = U1024;
pub const MAX_REQUEST_BLOCKS: u64 = 1024;

/// Maximum number of light client updates in a single request.
pub type MaxRequestLightClientUpdates = U128;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this used?

pub const MAX_REQUEST_LIGHT_CLIENT_UPDATES: u64 = 128;

/// Maximum length of error message.
pub type MaxErrorLen = U256;
pub const MAX_ERROR_LEN: u64 = 256;
Expand Down Expand Up @@ -248,6 +253,10 @@ pub enum RPCResponse<T: EthSpec> {
/// A response to a get LIGHTCLIENT_BOOTSTRAP request.
LightClientBootstrap(LightClientBootstrap<T>),

/// A response to a get LIGHTCLIENT_UPDATES_BY_RANGE request. A None signifies
/// the end of the batch.
LightClientUpdatesByRange(Arc<LightClientUpdate<T>>),

/// A PONG response to a PING request.
Pong(Ping),

Expand All @@ -263,6 +272,9 @@ pub enum ResponseTermination {

/// Blocks by root stream termination.
BlocksByRoot,

/// Light client updates by range stream termination.
LightClientUpdatesByRange,
}

/// The structured response containing a result/code indicating success or failure
Expand All @@ -284,6 +296,13 @@ pub struct LightClientBootstrapRequest {
pub root: Hash256,
}

/// Request `LightClientUpdate`s by range for lightclients peers.
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
pub struct LightClientUpdatesByRangeRequest {
pub start_period: u64,
pub count: u64,
}

/// The code assigned to an erroneous `RPCResponse`.
#[derive(Debug, Clone, Copy, PartialEq, IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
Expand Down Expand Up @@ -333,6 +352,7 @@ impl<T: EthSpec> RPCCodedResponse<T> {
RPCResponse::Pong(_) => false,
RPCResponse::MetaData(_) => false,
RPCResponse::LightClientBootstrap(_) => false,
RPCResponse::LightClientUpdatesByRange(_) => true,
},
RPCCodedResponse::Error(_, _) => true,
// Stream terminations are part of responses that have chunks
Expand Down Expand Up @@ -368,6 +388,7 @@ impl<T: EthSpec> RPCResponse<T> {
RPCResponse::Pong(_) => Protocol::Ping,
RPCResponse::MetaData(_) => Protocol::MetaData,
RPCResponse::LightClientBootstrap(_) => Protocol::LightClientBootstrap,
RPCResponse::LightClientUpdatesByRange(_) => Protocol::LightClientUpdatesByRange,
}
}
}
Expand Down Expand Up @@ -404,7 +425,18 @@ impl<T: EthSpec> std::fmt::Display for RPCResponse<T> {
RPCResponse::Pong(ping) => write!(f, "Pong: {}", ping.data),
RPCResponse::MetaData(metadata) => write!(f, "Metadata: {}", metadata.seq_number()),
RPCResponse::LightClientBootstrap(bootstrap) => {
write!(f, "LightClientBootstrap Slot: {}", bootstrap.header.slot)
write!(
f,
"LightClientBootstrap Slot: {}",
bootstrap.header.beacon.slot
)
}
RPCResponse::LightClientUpdatesByRange(update) => {
write!(
f,
"LightClientUpdatesByRange: Signature slot: {}",
update.signature_slot
)
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion beacon_node/lighthouse_network/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub(crate) use protocol::{InboundRequest, RPCProtocol};
pub use handler::SubstreamId;
pub use methods::{
BlocksByRangeRequest, BlocksByRootRequest, GoodbyeReason, LightClientBootstrapRequest,
MaxRequestBlocks, RPCResponseErrorCode, ResponseTermination, StatusMessage, MAX_REQUEST_BLOCKS,
LightClientUpdatesByRangeRequest, MaxRequestBlocks, RPCResponseErrorCode, ResponseTermination,
StatusMessage, MAX_REQUEST_BLOCKS, MAX_REQUEST_LIGHT_CLIENT_UPDATES,
};
pub(crate) use outbound::OutboundRequest;
pub use protocol::{max_rpc_size, Protocol, RPCError};
Expand Down Expand Up @@ -132,6 +133,11 @@ impl<Id: ReqId, TSpec: EthSpec> RPC<Id, TSpec> {
Duration::from_secs(10),
)
.n_every(Protocol::BlocksByRoot, 128, Duration::from_secs(10))
.n_every(
Protocol::LightClientUpdatesByRange,
methods::MAX_REQUEST_LIGHT_CLIENT_UPDATES,
Duration::from_secs(10),
)
.build()
.expect("Configuration parameters are valid");
RPC {
Expand Down Expand Up @@ -301,6 +307,9 @@ where
match end {
ResponseTermination::BlocksByRange => Protocol::BlocksByRange,
ResponseTermination::BlocksByRoot => Protocol::BlocksByRoot,
ResponseTermination::LightClientUpdatesByRange => {
Protocol::LightClientUpdatesByRange
}
},
),
},
Expand Down
Loading