From 6e1945fc5d2b2025d8ec7eb9b3f2b1eed94967ad Mon Sep 17 00:00:00 2001 From: Pawan Dhananjay Date: Thu, 21 Nov 2024 04:34:45 +0530 Subject: [PATCH] Avoid computing rpc_blob_limits multiple times (#6595) * Compute blob rpc limits in static block * Fix min size * Use MainnetEthSpec in rpc tests * Revert MainnetEthSpec; add another constant for blob size minimal --- .../lighthouse_network/src/rpc/protocol.rs | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/beacon_node/lighthouse_network/src/rpc/protocol.rs b/beacon_node/lighthouse_network/src/rpc/protocol.rs index d0dbffe9326..57c2795b04f 100644 --- a/beacon_node/lighthouse_network/src/rpc/protocol.rs +++ b/beacon_node/lighthouse_network/src/rpc/protocol.rs @@ -18,11 +18,11 @@ use tokio_util::{ }; use types::{ BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockCapella, BeaconBlockElectra, - BlobSidecar, ChainSpec, DataColumnSidecar, EmptyBlock, EthSpec, ForkContext, ForkName, - LightClientBootstrap, LightClientBootstrapAltair, LightClientFinalityUpdate, + BlobSidecar, ChainSpec, DataColumnSidecar, EmptyBlock, EthSpec, EthSpecId, ForkContext, + ForkName, LightClientBootstrap, LightClientBootstrapAltair, LightClientFinalityUpdate, LightClientFinalityUpdateAltair, LightClientOptimisticUpdate, - LightClientOptimisticUpdateAltair, LightClientUpdate, MainnetEthSpec, Signature, - SignedBeaconBlock, + LightClientOptimisticUpdateAltair, LightClientUpdate, MainnetEthSpec, MinimalEthSpec, + Signature, SignedBeaconBlock, }; // Note: Hardcoding the `EthSpec` type for `SignedBeaconBlock` as min/max values is @@ -105,6 +105,20 @@ pub static SIGNED_BEACON_BLOCK_ELECTRA_MAX: LazyLock = LazyLock::new(|| { + ssz::BYTES_PER_LENGTH_OFFSET }); // Length offset for the blob commitments field. +pub static BLOB_SIDECAR_SIZE: LazyLock = + LazyLock::new(BlobSidecar::::max_size); + +pub static BLOB_SIDECAR_SIZE_MINIMAL: LazyLock = + LazyLock::new(BlobSidecar::::max_size); + +pub static DATA_COLUMNS_SIDECAR_MIN: LazyLock = LazyLock::new(|| { + DataColumnSidecar::::empty() + .as_ssz_bytes() + .len() +}); +pub static DATA_COLUMNS_SIDECAR_MAX: LazyLock = + LazyLock::new(DataColumnSidecar::::max_size); + pub static ERROR_TYPE_MIN: LazyLock = LazyLock::new(|| { VariableList::::from(Vec::::new()) .as_ssz_bytes() @@ -597,8 +611,8 @@ impl ProtocolId { Protocol::BlocksByRoot => rpc_block_limits_by_fork(fork_context.current_fork()), Protocol::BlobsByRange => rpc_blob_limits::(), Protocol::BlobsByRoot => rpc_blob_limits::(), - Protocol::DataColumnsByRoot => rpc_data_column_limits::(), - Protocol::DataColumnsByRange => rpc_data_column_limits::(), + Protocol::DataColumnsByRoot => rpc_data_column_limits(), + Protocol::DataColumnsByRange => rpc_data_column_limits(), Protocol::Ping => RpcLimits::new( ::ssz_fixed_len(), ::ssz_fixed_len(), @@ -668,17 +682,18 @@ impl ProtocolId { } pub fn rpc_blob_limits() -> RpcLimits { - RpcLimits::new( - BlobSidecar::::empty().as_ssz_bytes().len(), - BlobSidecar::::max_size(), - ) + match E::spec_name() { + EthSpecId::Minimal => { + RpcLimits::new(*BLOB_SIDECAR_SIZE_MINIMAL, *BLOB_SIDECAR_SIZE_MINIMAL) + } + EthSpecId::Mainnet | EthSpecId::Gnosis => { + RpcLimits::new(*BLOB_SIDECAR_SIZE, *BLOB_SIDECAR_SIZE) + } + } } -pub fn rpc_data_column_limits() -> RpcLimits { - RpcLimits::new( - DataColumnSidecar::::empty().as_ssz_bytes().len(), - DataColumnSidecar::::max_size(), - ) +pub fn rpc_data_column_limits() -> RpcLimits { + RpcLimits::new(*DATA_COLUMNS_SIDECAR_MIN, *DATA_COLUMNS_SIDECAR_MAX) } /* Inbound upgrade */