Use E for EthSpec globally (#5264)

* Use `E` for `EthSpec` globally

* Fix tests

* Merge branch 'unstable' into e-ethspec

* Merge branch 'unstable' into e-ethspec

# Conflicts:
#	beacon_node/execution_layer/src/engine_api.rs
#	beacon_node/execution_layer/src/engine_api/http.rs
#	beacon_node/execution_layer/src/engine_api/json_structures.rs
#	beacon_node/execution_layer/src/test_utils/handle_rpc.rs
#	beacon_node/store/src/partial_beacon_state.rs
#	consensus/types/src/beacon_block.rs
#	consensus/types/src/beacon_block_body.rs
#	consensus/types/src/beacon_state.rs
#	consensus/types/src/config_and_preset.rs
#	consensus/types/src/execution_payload.rs
#	consensus/types/src/execution_payload_header.rs
#	consensus/types/src/light_client_optimistic_update.rs
#	consensus/types/src/payload.rs
#	lcli/src/parse_ssz.rs
This commit is contained in:
Mac L
2024-04-03 02:12:25 +11:00
committed by GitHub
parent f8fdb71f50
commit 969d12dc6f
230 changed files with 2743 additions and 2792 deletions

View File

@@ -20,20 +20,20 @@ pub trait OutboundCodec<TItem>: Encoder<TItem> + Decoder {
/* Global Inbound Codec */
// This deals with Decoding RPC Requests from other peers and encoding our responses
pub struct BaseInboundCodec<TCodec, TSpec>
pub struct BaseInboundCodec<TCodec, E>
where
TCodec: Encoder<RPCCodedResponse<TSpec>> + Decoder,
TSpec: EthSpec,
TCodec: Encoder<RPCCodedResponse<E>> + Decoder,
E: EthSpec,
{
/// Inner codec for handling various encodings
inner: TCodec,
phantom: PhantomData<TSpec>,
phantom: PhantomData<E>,
}
impl<TCodec, TSpec> BaseInboundCodec<TCodec, TSpec>
impl<TCodec, E> BaseInboundCodec<TCodec, E>
where
TCodec: Encoder<RPCCodedResponse<TSpec>> + Decoder,
TSpec: EthSpec,
TCodec: Encoder<RPCCodedResponse<E>> + Decoder,
E: EthSpec,
{
pub fn new(codec: TCodec) -> Self {
BaseInboundCodec {
@@ -45,22 +45,22 @@ where
/* Global Outbound Codec */
// This deals with Decoding RPC Responses from other peers and encoding our requests
pub struct BaseOutboundCodec<TOutboundCodec, TSpec>
pub struct BaseOutboundCodec<TOutboundCodec, E>
where
TOutboundCodec: OutboundCodec<OutboundRequest<TSpec>>,
TSpec: EthSpec,
TOutboundCodec: OutboundCodec<OutboundRequest<E>>,
E: EthSpec,
{
/// Inner codec for handling various encodings.
inner: TOutboundCodec,
/// Keeps track of the current response code for a chunk.
current_response_code: Option<u8>,
phantom: PhantomData<TSpec>,
phantom: PhantomData<E>,
}
impl<TOutboundCodec, TSpec> BaseOutboundCodec<TOutboundCodec, TSpec>
impl<TOutboundCodec, E> BaseOutboundCodec<TOutboundCodec, E>
where
TSpec: EthSpec,
TOutboundCodec: OutboundCodec<OutboundRequest<TSpec>>,
E: EthSpec,
TOutboundCodec: OutboundCodec<OutboundRequest<E>>,
{
pub fn new(codec: TOutboundCodec) -> Self {
BaseOutboundCodec {
@@ -76,18 +76,14 @@ where
/* Base Inbound Codec */
// This Encodes RPC Responses sent to external peers
impl<TCodec, TSpec> Encoder<RPCCodedResponse<TSpec>> for BaseInboundCodec<TCodec, TSpec>
impl<TCodec, E> Encoder<RPCCodedResponse<E>> for BaseInboundCodec<TCodec, E>
where
TSpec: EthSpec,
TCodec: Decoder + Encoder<RPCCodedResponse<TSpec>>,
E: EthSpec,
TCodec: Decoder + Encoder<RPCCodedResponse<E>>,
{
type Error = <TCodec as Encoder<RPCCodedResponse<TSpec>>>::Error;
type Error = <TCodec as Encoder<RPCCodedResponse<E>>>::Error;
fn encode(
&mut self,
item: RPCCodedResponse<TSpec>,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
fn encode(&mut self, item: RPCCodedResponse<E>, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.clear();
dst.reserve(1);
dst.put_u8(
@@ -99,12 +95,12 @@ where
}
// This Decodes RPC Requests from external peers
impl<TCodec, TSpec> Decoder for BaseInboundCodec<TCodec, TSpec>
impl<TCodec, E> Decoder for BaseInboundCodec<TCodec, E>
where
TSpec: EthSpec,
TCodec: Encoder<RPCCodedResponse<TSpec>> + Decoder<Item = InboundRequest<TSpec>>,
E: EthSpec,
TCodec: Encoder<RPCCodedResponse<E>> + Decoder<Item = InboundRequest<E>>,
{
type Item = InboundRequest<TSpec>;
type Item = InboundRequest<E>;
type Error = <TCodec as Decoder>::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
@@ -115,30 +111,26 @@ where
/* Base Outbound Codec */
// This Encodes RPC Requests sent to external peers
impl<TCodec, TSpec> Encoder<OutboundRequest<TSpec>> for BaseOutboundCodec<TCodec, TSpec>
impl<TCodec, E> Encoder<OutboundRequest<E>> for BaseOutboundCodec<TCodec, E>
where
TSpec: EthSpec,
TCodec: OutboundCodec<OutboundRequest<TSpec>> + Encoder<OutboundRequest<TSpec>>,
E: EthSpec,
TCodec: OutboundCodec<OutboundRequest<E>> + Encoder<OutboundRequest<E>>,
{
type Error = <TCodec as Encoder<OutboundRequest<TSpec>>>::Error;
type Error = <TCodec as Encoder<OutboundRequest<E>>>::Error;
fn encode(
&mut self,
item: OutboundRequest<TSpec>,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
fn encode(&mut self, item: OutboundRequest<E>, dst: &mut BytesMut) -> Result<(), Self::Error> {
self.inner.encode(item, dst)
}
}
// This decodes RPC Responses received from external peers
impl<TCodec, TSpec> Decoder for BaseOutboundCodec<TCodec, TSpec>
impl<TCodec, E> Decoder for BaseOutboundCodec<TCodec, E>
where
TSpec: EthSpec,
TCodec: OutboundCodec<OutboundRequest<TSpec>, CodecErrorType = ErrorType>
+ Decoder<Item = RPCResponse<TSpec>>,
E: EthSpec,
TCodec: OutboundCodec<OutboundRequest<E>, CodecErrorType = ErrorType>
+ Decoder<Item = RPCResponse<E>>,
{
type Item = RPCCodedResponse<TSpec>;
type Item = RPCCodedResponse<E>;
type Error = <TCodec as Decoder>::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
@@ -154,7 +146,7 @@ where
});
let inner_result = {
if RPCCodedResponse::<TSpec>::is_response(response_code) {
if RPCCodedResponse::<E>::is_response(response_code) {
// decode an actual response and mutates the buffer if enough bytes have been read
// returning the result.
self.inner

View File

@@ -10,26 +10,26 @@ use tokio_util::codec::{Decoder, Encoder};
use types::EthSpec;
// Known types of codecs
pub enum InboundCodec<TSpec: EthSpec> {
SSZSnappy(BaseInboundCodec<SSZSnappyInboundCodec<TSpec>, TSpec>),
pub enum InboundCodec<E: EthSpec> {
SSZSnappy(BaseInboundCodec<SSZSnappyInboundCodec<E>, E>),
}
pub enum OutboundCodec<TSpec: EthSpec> {
SSZSnappy(BaseOutboundCodec<SSZSnappyOutboundCodec<TSpec>, TSpec>),
pub enum OutboundCodec<E: EthSpec> {
SSZSnappy(BaseOutboundCodec<SSZSnappyOutboundCodec<E>, E>),
}
impl<T: EthSpec> Encoder<RPCCodedResponse<T>> for InboundCodec<T> {
impl<E: EthSpec> Encoder<RPCCodedResponse<E>> for InboundCodec<E> {
type Error = RPCError;
fn encode(&mut self, item: RPCCodedResponse<T>, dst: &mut BytesMut) -> Result<(), Self::Error> {
fn encode(&mut self, item: RPCCodedResponse<E>, dst: &mut BytesMut) -> Result<(), Self::Error> {
match self {
InboundCodec::SSZSnappy(codec) => codec.encode(item, dst),
}
}
}
impl<TSpec: EthSpec> Decoder for InboundCodec<TSpec> {
type Item = InboundRequest<TSpec>;
impl<E: EthSpec> Decoder for InboundCodec<E> {
type Item = InboundRequest<E>;
type Error = RPCError;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
@@ -39,22 +39,18 @@ impl<TSpec: EthSpec> Decoder for InboundCodec<TSpec> {
}
}
impl<TSpec: EthSpec> Encoder<OutboundRequest<TSpec>> for OutboundCodec<TSpec> {
impl<E: EthSpec> Encoder<OutboundRequest<E>> for OutboundCodec<E> {
type Error = RPCError;
fn encode(
&mut self,
item: OutboundRequest<TSpec>,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
fn encode(&mut self, item: OutboundRequest<E>, dst: &mut BytesMut) -> Result<(), Self::Error> {
match self {
OutboundCodec::SSZSnappy(codec) => codec.encode(item, dst),
}
}
}
impl<T: EthSpec> Decoder for OutboundCodec<T> {
type Item = RPCCodedResponse<T>;
impl<E: EthSpec> Decoder for OutboundCodec<E> {
type Item = RPCCodedResponse<E>;
type Error = RPCError;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {

View File

@@ -28,17 +28,17 @@ const CONTEXT_BYTES_LEN: usize = 4;
/* Inbound Codec */
pub struct SSZSnappyInboundCodec<TSpec: EthSpec> {
pub struct SSZSnappyInboundCodec<E: EthSpec> {
protocol: ProtocolId,
inner: Uvi<usize>,
len: Option<usize>,
/// Maximum bytes that can be sent in one req/resp chunked responses.
max_packet_size: usize,
fork_context: Arc<ForkContext>,
phantom: PhantomData<TSpec>,
phantom: PhantomData<E>,
}
impl<T: EthSpec> SSZSnappyInboundCodec<T> {
impl<E: EthSpec> SSZSnappyInboundCodec<E> {
pub fn new(
protocol: ProtocolId,
max_packet_size: usize,
@@ -60,14 +60,10 @@ impl<T: EthSpec> SSZSnappyInboundCodec<T> {
}
// Encoder for inbound streams: Encodes RPC Responses sent to peers.
impl<TSpec: EthSpec> Encoder<RPCCodedResponse<TSpec>> for SSZSnappyInboundCodec<TSpec> {
impl<E: EthSpec> Encoder<RPCCodedResponse<E>> for SSZSnappyInboundCodec<E> {
type Error = RPCError;
fn encode(
&mut self,
item: RPCCodedResponse<TSpec>,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
fn encode(&mut self, item: RPCCodedResponse<E>, dst: &mut BytesMut) -> Result<(), Self::Error> {
let bytes = match &item {
RPCCodedResponse::Success(resp) => match &resp {
RPCResponse::Status(res) => res.as_ssz_bytes(),
@@ -125,8 +121,8 @@ impl<TSpec: EthSpec> Encoder<RPCCodedResponse<TSpec>> for SSZSnappyInboundCodec<
}
// Decoder for inbound streams: Decodes RPC requests from peers
impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
type Item = InboundRequest<TSpec>;
impl<E: EthSpec> Decoder for SSZSnappyInboundCodec<E> {
type Item = InboundRequest<E>;
type Error = RPCError;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
@@ -175,7 +171,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
}
/* Outbound Codec: Codec for initiating RPC requests */
pub struct SSZSnappyOutboundCodec<TSpec: EthSpec> {
pub struct SSZSnappyOutboundCodec<E: EthSpec> {
inner: Uvi<usize>,
len: Option<usize>,
protocol: ProtocolId,
@@ -184,10 +180,10 @@ pub struct SSZSnappyOutboundCodec<TSpec: EthSpec> {
/// The fork name corresponding to the received context bytes.
fork_name: Option<ForkName>,
fork_context: Arc<ForkContext>,
phantom: PhantomData<TSpec>,
phantom: PhantomData<E>,
}
impl<TSpec: EthSpec> SSZSnappyOutboundCodec<TSpec> {
impl<E: EthSpec> SSZSnappyOutboundCodec<E> {
pub fn new(
protocol: ProtocolId,
max_packet_size: usize,
@@ -210,14 +206,10 @@ impl<TSpec: EthSpec> SSZSnappyOutboundCodec<TSpec> {
}
// Encoder for outbound streams: Encodes RPC Requests to peers
impl<TSpec: EthSpec> Encoder<OutboundRequest<TSpec>> for SSZSnappyOutboundCodec<TSpec> {
impl<E: EthSpec> Encoder<OutboundRequest<E>> for SSZSnappyOutboundCodec<E> {
type Error = RPCError;
fn encode(
&mut self,
item: OutboundRequest<TSpec>,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
fn encode(&mut self, item: OutboundRequest<E>, dst: &mut BytesMut) -> Result<(), Self::Error> {
let bytes = match item {
OutboundRequest::Status(req) => req.as_ssz_bytes(),
OutboundRequest::Goodbye(req) => req.as_ssz_bytes(),
@@ -262,8 +254,8 @@ impl<TSpec: EthSpec> Encoder<OutboundRequest<TSpec>> for SSZSnappyOutboundCodec<
// The majority of the decoding has now been pushed upstream due to the changing specification.
// We prefer to decode blocks and attestations with extra knowledge about the chain to perform
// faster verification checks before decoding entire blocks/attestations.
impl<TSpec: EthSpec> Decoder for SSZSnappyOutboundCodec<TSpec> {
type Item = RPCResponse<TSpec>;
impl<E: EthSpec> Decoder for SSZSnappyOutboundCodec<E> {
type Item = RPCResponse<E>;
type Error = RPCError;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
@@ -287,9 +279,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyOutboundCodec<TSpec> {
// Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of
// packet size for ssz container corresponding to `self.protocol`.
let ssz_limits = self
.protocol
.rpc_response_limits::<TSpec>(&self.fork_context);
let ssz_limits = self.protocol.rpc_response_limits::<E>(&self.fork_context);
if ssz_limits.is_out_of_bounds(length, self.max_packet_size) {
return Err(RPCError::InvalidData(format!(
"RPC response length is out of bounds, length {}, max {}, min {}",
@@ -320,7 +310,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyOutboundCodec<TSpec> {
}
}
impl<TSpec: EthSpec> OutboundCodec<OutboundRequest<TSpec>> for SSZSnappyOutboundCodec<TSpec> {
impl<E: EthSpec> OutboundCodec<OutboundRequest<E>> for SSZSnappyOutboundCodec<E> {
type CodecErrorType = ErrorType;
fn decode_error(
@@ -389,10 +379,10 @@ fn handle_error<T>(
/// Returns `Some(context_bytes)` for encoding RPC responses that require context bytes.
/// Returns `None` when context bytes are not required.
fn context_bytes<T: EthSpec>(
fn context_bytes<E: EthSpec>(
protocol: &ProtocolId,
fork_context: &ForkContext,
resp: &RPCCodedResponse<T>,
resp: &RPCCodedResponse<E>,
) -> Option<[u8; CONTEXT_BYTES_LEN]> {
// Add the context bytes if required
if protocol.has_context_bytes() {
@@ -457,11 +447,11 @@ fn handle_length(
/// Decodes an `InboundRequest` from the byte stream.
/// `decoded_buffer` should be an ssz-encoded bytestream with
// length = length-prefix received in the beginning of the stream.
fn handle_rpc_request<T: EthSpec>(
fn handle_rpc_request<E: EthSpec>(
versioned_protocol: SupportedProtocol,
decoded_buffer: &[u8],
spec: &ChainSpec,
) -> Result<Option<InboundRequest<T>>, RPCError> {
) -> Result<Option<InboundRequest<E>>, RPCError> {
match versioned_protocol {
SupportedProtocol::StatusV1 => Ok(Some(InboundRequest::Status(
StatusMessage::from_ssz_bytes(decoded_buffer)?,
@@ -537,11 +527,11 @@ fn handle_rpc_request<T: EthSpec>(
///
/// For BlocksByRange/BlocksByRoot reponses, decodes the appropriate response
/// according to the received `ForkName`.
fn handle_rpc_response<T: EthSpec>(
fn handle_rpc_response<E: EthSpec>(
versioned_protocol: SupportedProtocol,
decoded_buffer: &[u8],
fork_name: Option<ForkName>,
) -> Result<Option<RPCResponse<T>>, RPCError> {
) -> Result<Option<RPCResponse<E>>, RPCError> {
match versioned_protocol {
SupportedProtocol::StatusV1 => Ok(Some(RPCResponse::Status(
StatusMessage::from_ssz_bytes(decoded_buffer)?,