Skip to content

Commit

Permalink
add runtime variable list type
Browse files Browse the repository at this point in the history
  • Loading branch information
realbigsean committed Oct 12, 2023
1 parent 38e7172 commit d0fe2ce
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 20 deletions.
11 changes: 6 additions & 5 deletions beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use std::io::{Read, Write};
use std::marker::PhantomData;
use std::sync::Arc;
use tokio_util::codec::{Decoder, Encoder};
use types::{light_client_bootstrap::LightClientBootstrap, BlobSidecar};
use types::{light_client_bootstrap::LightClientBootstrap, BlobSidecar, ChainSpec};
use types::{
EthSpec, ForkContext, ForkName, Hash256, SignedBeaconBlock, SignedBeaconBlockAltair,
EthSpec, ForkContext, ForkName, Hash256, RuntimeVariableList, SignedBeaconBlock, SignedBeaconBlockAltair,
SignedBeaconBlockBase, SignedBeaconBlockCapella, SignedBeaconBlockDeneb,
SignedBeaconBlockMerge,
};
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
let n = reader.get_ref().get_ref().position();
self.len = None;
let _read_bytes = src.split_to(n as usize);
handle_rpc_request(self.protocol.versioned_protocol, &decoded_buffer)
handle_rpc_request(self.protocol.versioned_protocol, &decoded_buffer, &self.fork_context)
}
Err(e) => handle_error(e, reader.get_ref().get_ref().position(), max_compressed_len),
}
Expand Down Expand Up @@ -455,6 +455,7 @@ fn handle_length(
fn handle_rpc_request<T: EthSpec>(
versioned_protocol: SupportedProtocol,
decoded_buffer: &[u8],
fork_context: &ForkContext
) -> Result<Option<InboundRequest<T>>, RPCError> {
match versioned_protocol {
SupportedProtocol::StatusV1 => Ok(Some(InboundRequest::Status(
Expand All @@ -471,12 +472,12 @@ fn handle_rpc_request<T: EthSpec>(
))),
SupportedProtocol::BlocksByRootV2 => Ok(Some(InboundRequest::BlocksByRoot(
BlocksByRootRequest::V2(BlocksByRootRequestV2 {
block_roots: VariableList::from_ssz_bytes(decoded_buffer)?,
block_roots: RuntimeVariableList::from_ssz_bytes(decoded_buffer, 1)?,
}),
))),
SupportedProtocol::BlocksByRootV1 => Ok(Some(InboundRequest::BlocksByRoot(
BlocksByRootRequest::V1(BlocksByRootRequestV1 {
block_roots: VariableList::from_ssz_bytes(decoded_buffer)?,
block_roots: RuntimeVariableList::from_ssz_bytes(decoded_buffer, 1)?,
}),
))),
SupportedProtocol::BlobsByRangeV1 => Ok(Some(InboundRequest::BlobsByRange(
Expand Down
13 changes: 7 additions & 6 deletions beacon_node/lighthouse_network/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::types::{EnrAttestationBitfield, EnrSyncCommitteeBitfield};
use regex::bytes::Regex;
use serde::Serialize;
use ssz::Encode;
use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use ssz_types::{
typenum::{U1024, U128, U256, U768},
Expand All @@ -18,7 +18,7 @@ use types::blob_sidecar::BlobIdentifier;
use types::consts::deneb::MAX_BLOBS_PER_BLOCK;
use types::{
blob_sidecar::BlobSidecar, light_client_bootstrap::LightClientBootstrap, Epoch, EthSpec,
Hash256, SignedBeaconBlock, Slot,
Hash256, SignedBeaconBlock, Slot, RuntimeVariableList
};

/// Maximum number of blocks in a single request.
Expand Down Expand Up @@ -343,23 +343,24 @@ impl OldBlocksByRangeRequest {
}
}


/// Request a number of beacon block bodies from a peer.
#[superstruct(
variants(V1, V2),
variant_attributes(derive(Encode, Decode, Clone, Debug, PartialEq))
variant_attributes(derive(Clone, Debug, PartialEq))
)]
#[derive(Clone, Debug, PartialEq)]
pub struct BlocksByRootRequest {
/// The list of beacon block bodies being requested.
pub block_roots: VariableList<Hash256, MaxRequestBlocks>,
pub block_roots: RuntimeVariableList<Hash256>,
}

impl BlocksByRootRequest {
pub fn new(block_roots: VariableList<Hash256, MaxRequestBlocks>) -> Self {
pub fn new(block_roots: RuntimeVariableList<Hash256>) -> Self {
Self::V2(BlocksByRootRequestV2 { block_roots })
}

pub fn new_v1(block_roots: VariableList<Hash256, MaxRequestBlocks>) -> Self {
pub fn new_v1(block_roots: RuntimeVariableList<Hash256>) -> Self {
Self::V1(BlocksByRootRequestV1 { block_roots })
}
}
Expand Down
21 changes: 12 additions & 9 deletions beacon_node/network/src/sync/block_lookups/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::ops::IndexMut;
use std::sync::Arc;
use std::time::Duration;
use types::blob_sidecar::{BlobIdentifier, FixedBlobSidecarList};
use types::{BlobSidecar, EthSpec, Hash256, SignedBeaconBlock};
use types::{BlobSidecar, ChainSpec, EthSpec, Hash256, RuntimeVariableList ,SignedBeaconBlock};

#[derive(Debug, Copy, Clone)]
pub enum ResponseType {
Expand Down Expand Up @@ -89,11 +89,11 @@ pub trait RequestState<L: Lookup, T: BeaconChainTypes> {
/* Request building methods */

/// Construct a new request.
fn build_request(&mut self) -> Result<(PeerShouldHave, Self::RequestType), LookupRequestError> {
fn build_request(&mut self, spec: &ChainSpec) -> Result<(PeerShouldHave, Self::RequestType), LookupRequestError> {
// Verify and construct request.
self.too_many_attempts()?;
let peer = self.get_peer()?;
let request = self.new_request();
let request = self.new_request(spec)?;
Ok((peer, request))
}

Expand All @@ -110,7 +110,7 @@ pub trait RequestState<L: Lookup, T: BeaconChainTypes> {
}

// Construct request.
let (peer_id, request) = self.build_request()?;
let (peer_id, request) = self.build_request(&cx.chain.spec)?;

// Update request state.
self.get_state_mut().state = State::Downloading { peer_id };
Expand Down Expand Up @@ -164,7 +164,7 @@ pub trait RequestState<L: Lookup, T: BeaconChainTypes> {
}

/// Initialize `Self::RequestType`.
fn new_request(&self) -> Self::RequestType;
fn new_request(&self, spec: &ChainSpec) -> Result<Self::RequestType, LookupRequestError>;

/// Send the request to the network service.
fn make_request(
Expand Down Expand Up @@ -272,8 +272,11 @@ impl<L: Lookup, T: BeaconChainTypes> RequestState<L, T> for BlockRequestState<L>
type VerifiedResponseType = Arc<SignedBeaconBlock<T::EthSpec>>;
type ReconstructedResponseType = RpcBlock<T::EthSpec>;

fn new_request(&self) -> BlocksByRootRequest {
BlocksByRootRequest::new(VariableList::from(vec![self.requested_block_root]))
fn new_request(&self, spec: &ChainSpec) -> Result<BlocksByRootRequest, LookupRequestError> {
Ok(BlocksByRootRequest::new(RuntimeVariableList::new(vec![self.requested_block_root], 1)
.map_err(|_e|{
LookupRequestError::SszError("invalid request length")
})?))
}

fn make_request(
Expand Down Expand Up @@ -376,10 +379,10 @@ impl<L: Lookup, T: BeaconChainTypes> RequestState<L, T> for BlobRequestState<L,
type VerifiedResponseType = FixedBlobSidecarList<T::EthSpec>;
type ReconstructedResponseType = FixedBlobSidecarList<T::EthSpec>;

fn new_request(&self) -> BlobsByRootRequest {
fn new_request(&self, spec: &ChainSpec) -> Result<BlobsByRootRequest, LookupRequestError> {
let blob_id_vec: Vec<BlobIdentifier> = self.requested_ids.clone().into();
let blob_ids = VariableList::from(blob_id_vec);
BlobsByRootRequest { blob_ids }
Ok(BlobsByRootRequest { blob_ids })
}

fn make_request(
Expand Down
3 changes: 3 additions & 0 deletions beacon_node/network/src/sync/block_lookups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
RequestError::SendFailed(_) => {
// Probably shutting down, nothing to do here. Drop the request
}
RequestError::SszError(e) => {
warn!(self.log, "Invalid parent request constructed"; "reason" => %e);
}
RequestError::ChainTooLong => {
self.failed_chains.insert(parent_lookup.chain_hash());
// This indicates faulty peers.
Expand Down
3 changes: 3 additions & 0 deletions beacon_node/network/src/sync/block_lookups/parent_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub enum RequestError {
cannot_process: bool,
},
NoPeers,
SszError(&'static str)
}

impl<T: BeaconChainTypes> ParentLookup<T> {
Expand Down Expand Up @@ -262,6 +263,7 @@ impl From<LookupRequestError> for RequestError {
}
E::NoPeers => RequestError::NoPeers,
E::SendFailed(msg) => RequestError::SendFailed(msg),
E::SszError(msg) => RequestError::SszError(msg),
}
}
}
Expand Down Expand Up @@ -289,6 +291,7 @@ impl RequestError {
}
RequestError::TooManyAttempts { cannot_process: _ } => "too_many_downloading_attempts",
RequestError::NoPeers => "no_peers",
RequestError::SszError(e) => e,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub enum LookupRequestError {
},
NoPeers,
SendFailed(&'static str),
SszError(&'static str),
}

pub struct SingleBlockLookup<L: Lookup, T: BeaconChainTypes> {
Expand Down
2 changes: 2 additions & 0 deletions consensus/types/src/fork_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct ForkContext {
current_fork: RwLock<ForkName>,
fork_to_digest: HashMap<ForkName, [u8; 4]>,
digest_to_fork: HashMap<[u8; 4], ForkName>,
spec: ChainSpec,
}

impl ForkContext {
Expand Down Expand Up @@ -73,6 +74,7 @@ impl ForkContext {
current_fork: RwLock::new(spec.fork_name_at_slot::<T>(current_slot)),
fork_to_digest,
digest_to_fork,
spec: spec.clone(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions consensus/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub mod sqlite;
pub mod blob_sidecar;
pub mod sidecar;
pub mod signed_blob;
pub mod runtime_var_list;

use ethereum_types::{H160, H256};

Expand Down Expand Up @@ -168,6 +169,7 @@ pub use crate::preset::{AltairPreset, BasePreset, BellatrixPreset, CapellaPreset
pub use crate::proposer_preparation_data::ProposerPreparationData;
pub use crate::proposer_slashing::ProposerSlashing;
pub use crate::relative_epoch::{Error as RelativeEpochError, RelativeEpoch};
pub use crate::runtime_var_list::RuntimeVariableList;
pub use crate::selection_proof::SelectionProof;
pub use crate::shuffling_id::AttestationShufflingId;
pub use crate::signed_aggregate_and_proof::SignedAggregateAndProof;
Expand Down
40 changes: 40 additions & 0 deletions consensus/types/src/runtime_var_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use ssz::{Decode, Encode};
use ssz_derive::Encode;

#[derive(Debug, Clone, PartialEq, Encode)]
#[ssz(struct_behaviour = "transparent")]
pub struct RuntimeVariableList<T: Encode> {
vec: Vec<T>,
#[ssz(skip_serializing, skip_deserializing)]
max_len: usize,
}

impl<T: Encode + Decode + Clone> RuntimeVariableList<T> {
pub fn new(vec: Vec<T>, max_len: usize) -> Result<Self, ssz_types::Error> {
if vec.len() <= max_len {
Ok(Self { vec, max_len })
} else {
Err(ssz_types::Error::OutOfBounds {
i: vec.len(),
len: max_len,
})
}
}

pub fn to_vec(&self)-> Vec<T> {
self.vec.clone()
}

pub fn len(&self) -> usize {
self.vec.len()
}

pub fn from_ssz_bytes(bytes: &[u8], max_len: usize) -> Result<Self, ssz::DecodeError> {
let vec = if bytes.is_empty() {
vec![]
} else {
ssz::decode_list_of_variable_length_items(bytes, Some(max_len))?
};
Ok(Self { vec, max_len })
}
}

0 comments on commit d0fe2ce

Please sign in to comment.