Skip to content
49 changes: 10 additions & 39 deletions beacon_node/lighthouse_network/src/rpc/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,33 +922,16 @@ mod tests {
}

fn fork_context(fork_name: ForkName, spec: &ChainSpec) -> ForkContext {
let current_slot = match fork_name {
ForkName::Base => Slot::new(0),
ForkName::Altair => spec
.altair_fork_epoch
.unwrap()
.start_slot(Spec::slots_per_epoch()),
ForkName::Bellatrix => spec
.bellatrix_fork_epoch
.unwrap()
.start_slot(Spec::slots_per_epoch()),
ForkName::Capella => spec
.capella_fork_epoch
.unwrap()
.start_slot(Spec::slots_per_epoch()),
ForkName::Deneb => spec
.deneb_fork_epoch
.unwrap()
.start_slot(Spec::slots_per_epoch()),
ForkName::Electra => spec
.electra_fork_epoch
.unwrap()
.start_slot(Spec::slots_per_epoch()),
ForkName::Fulu => spec
.fulu_fork_epoch
.unwrap()
.start_slot(Spec::slots_per_epoch()),
let current_epoch = match fork_name {
ForkName::Base => Some(Epoch::new(0)),
ForkName::Altair => spec.altair_fork_epoch,
ForkName::Bellatrix => spec.bellatrix_fork_epoch,
ForkName::Capella => spec.capella_fork_epoch,
ForkName::Deneb => spec.deneb_fork_epoch,
ForkName::Electra => spec.electra_fork_epoch,
ForkName::Fulu => spec.fulu_fork_epoch,
};
let current_slot = current_epoch.unwrap().start_slot(Spec::slots_per_epoch());
ForkContext::new::<Spec>(current_slot, Hash256::zero(), spec)
}

Expand All @@ -962,11 +945,7 @@ mod tests {
fn altair_block(spec: &ChainSpec) -> SignedBeaconBlock<Spec> {
// The context bytes are now derived from the block epoch, so we need to have the slot set
// here.
let mut full_block = BeaconBlock::Altair(BeaconBlockAltair::<Spec>::full(spec));
*full_block.slot_mut() = spec
.altair_fork_epoch
.expect("altair fork epoch must be set")
.start_slot(Spec::slots_per_epoch());
let full_block = BeaconBlock::Altair(BeaconBlockAltair::<Spec>::full(spec));
SignedBeaconBlock::from_block(full_block, Signature::empty())
}

Expand Down Expand Up @@ -1010,10 +989,6 @@ mod tests {
// here.
let mut block: BeaconBlockBellatrix<_, FullPayload<Spec>> =
BeaconBlockBellatrix::empty(spec);
block.slot = spec
.bellatrix_fork_epoch
.expect("Bellatrix epoch must be set")
.start_slot(Spec::slots_per_epoch());

let tx = VariableList::from(vec![0; 1024]);
let txs = VariableList::from(std::iter::repeat_n(tx, 5000).collect::<Vec<_>>());
Expand All @@ -1033,10 +1008,6 @@ mod tests {
// here.
let mut block: BeaconBlockBellatrix<_, FullPayload<Spec>> =
BeaconBlockBellatrix::empty(spec);
block.slot = spec
.bellatrix_fork_epoch
.expect("Bellatrix epoch must be set")
.start_slot(Spec::slots_per_epoch());

let tx = VariableList::from(vec![0; 1024]);
let txs = VariableList::from(std::iter::repeat_n(tx, 100000).collect::<Vec<_>>());
Expand Down
52 changes: 26 additions & 26 deletions beacon_node/lighthouse_network/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,41 @@ use tracing::{debug, error, info_span, Instrument};
use tracing_subscriber::EnvFilter;
use types::{
ChainSpec, EnrForkId, Epoch, EthSpec, FixedBytesExtended, ForkContext, ForkName, Hash256,
MinimalEthSpec, Slot,
MinimalEthSpec,
};

type E = MinimalEthSpec;

use lighthouse_network::rpc::config::InboundRateLimiterConfig;
use tempfile::Builder as TempBuilder;

/// Returns a dummy fork context
pub fn fork_context(fork_name: ForkName) -> ForkContext {
/// Returns a chain spec with all forks enabled.
pub fn spec_with_all_forks_enabled() -> ChainSpec {
let mut chain_spec = E::default_spec();
let altair_fork_epoch = Epoch::new(1);
let bellatrix_fork_epoch = Epoch::new(2);
let capella_fork_epoch = Epoch::new(3);
let deneb_fork_epoch = Epoch::new(4);
let electra_fork_epoch = Epoch::new(5);
let fulu_fork_epoch = Epoch::new(6);

chain_spec.altair_fork_epoch = Some(altair_fork_epoch);
chain_spec.bellatrix_fork_epoch = Some(bellatrix_fork_epoch);
chain_spec.capella_fork_epoch = Some(capella_fork_epoch);
chain_spec.deneb_fork_epoch = Some(deneb_fork_epoch);
chain_spec.electra_fork_epoch = Some(electra_fork_epoch);
chain_spec.fulu_fork_epoch = Some(fulu_fork_epoch);
chain_spec.altair_fork_epoch = Some(Epoch::new(1));
chain_spec.bellatrix_fork_epoch = Some(Epoch::new(2));
chain_spec.capella_fork_epoch = Some(Epoch::new(3));
chain_spec.deneb_fork_epoch = Some(Epoch::new(4));
chain_spec.electra_fork_epoch = Some(Epoch::new(5));
chain_spec.fulu_fork_epoch = Some(Epoch::new(6));
chain_spec
Copy link
Member

Choose a reason for hiding this comment

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

might be good to assert that the latest fork's fork_epoch is set or we will need to remember to update this for each fork

Copy link
Member

Choose a reason for hiding this comment

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

Added in 555fb61

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice! Thanks

}

let current_slot = match fork_name {
ForkName::Base => Slot::new(0),
ForkName::Altair => altair_fork_epoch.start_slot(E::slots_per_epoch()),
ForkName::Bellatrix => bellatrix_fork_epoch.start_slot(E::slots_per_epoch()),
ForkName::Capella => capella_fork_epoch.start_slot(E::slots_per_epoch()),
ForkName::Deneb => deneb_fork_epoch.start_slot(E::slots_per_epoch()),
ForkName::Electra => electra_fork_epoch.start_slot(E::slots_per_epoch()),
ForkName::Fulu => fulu_fork_epoch.start_slot(E::slots_per_epoch()),
/// Returns a dummy fork context
pub fn fork_context(fork_name: ForkName, spec: &ChainSpec) -> ForkContext {
let current_epoch = match fork_name {
ForkName::Base => Some(Epoch::new(0)),
ForkName::Altair => spec.altair_fork_epoch,
ForkName::Bellatrix => spec.bellatrix_fork_epoch,
ForkName::Capella => spec.capella_fork_epoch,
ForkName::Deneb => spec.deneb_fork_epoch,
ForkName::Electra => spec.electra_fork_epoch,
ForkName::Fulu => spec.fulu_fork_epoch,
};
ForkContext::new::<E>(current_slot, Hash256::zero(), &chain_spec)
let current_slot = current_epoch
.unwrap_or_else(|| panic!("expect fork {fork_name} to be scheduled"))
.start_slot(E::slots_per_epoch());
ForkContext::new::<E>(current_slot, Hash256::zero(), spec)
}

pub struct Libp2pInstance(
Expand Down Expand Up @@ -122,7 +122,7 @@ pub async fn build_libp2p_instance(
let libp2p_context = lighthouse_network::Context {
config,
enr_fork_id: EnrForkId::default(),
fork_context: Arc::new(fork_context(fork_name)),
fork_context: Arc::new(fork_context(fork_name, &chain_spec)),
chain_spec,
libp2p_registry: None,
};
Expand Down
46 changes: 27 additions & 19 deletions beacon_node/lighthouse_network/tests/rpc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod common;

use crate::common::spec_with_all_forks_enabled;
use common::{build_tracing_subscriber, Protocol};
use lighthouse_network::rpc::{methods::*, RequestType};
use lighthouse_network::service::api_types::AppRequestId;
Expand Down Expand Up @@ -60,7 +61,7 @@ fn test_tcp_status_rpc() {

let rt = Arc::new(Runtime::new().unwrap());

let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());

rt.block_on(async {
// get sender/receiver
Expand Down Expand Up @@ -168,7 +169,7 @@ fn test_tcp_blocks_by_range_chunked_rpc() {

let rt = Arc::new(Runtime::new().unwrap());

let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());

rt.block_on(async {
// get sender/receiver
Expand Down Expand Up @@ -318,7 +319,7 @@ fn test_blobs_by_range_chunked_rpc() {

rt.block_on(async {
// get sender/receiver
let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());
let (mut sender, mut receiver) = common::build_node_pair(
Arc::downgrade(&rt),
ForkName::Deneb,
Expand All @@ -330,13 +331,18 @@ fn test_blobs_by_range_chunked_rpc() {
.await;

// BlobsByRange Request
let deneb_slot = spec
.deneb_fork_epoch
.expect("deneb must be scheduled")
.start_slot(E::slots_per_epoch());
let rpc_request = RequestType::BlobsByRange(BlobsByRangeRequest {
start_slot: 0,
start_slot: deneb_slot.as_u64(),
count: slot_count,
});

// BlocksByRange Response
let blob = BlobSidecar::<E>::empty();
// BlobsByRange Response
let mut blob = BlobSidecar::<E>::empty();
blob.signed_block_header.message.slot = deneb_slot;

let rpc_response = Response::BlobsByRange(Some(Arc::new(blob)));

Expand Down Expand Up @@ -438,7 +444,7 @@ fn test_tcp_blocks_by_range_over_limit() {

let rt = Arc::new(Runtime::new().unwrap());

let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());

rt.block_on(async {
// get sender/receiver
Expand Down Expand Up @@ -545,7 +551,7 @@ fn test_tcp_blocks_by_range_chunked_rpc_terminates_correctly() {

let rt = Arc::new(Runtime::new().unwrap());

let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());

rt.block_on(async {
// get sender/receiver
Expand Down Expand Up @@ -681,7 +687,7 @@ fn test_tcp_blocks_by_range_single_empty_rpc() {

let rt = Arc::new(Runtime::new().unwrap());

let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());

rt.block_on(async {
// get sender/receiver
Expand Down Expand Up @@ -804,14 +810,15 @@ fn test_tcp_blocks_by_root_chunked_rpc() {

let messages_to_send = 6;

let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());
let current_fork_name = ForkName::Bellatrix;

let rt = Arc::new(Runtime::new().unwrap());
// get sender/receiver
rt.block_on(async {
let (mut sender, mut receiver) = common::build_node_pair(
Arc::downgrade(&rt),
ForkName::Bellatrix,
current_fork_name,
spec.clone(),
Protocol::Tcp,
false,
Expand All @@ -831,7 +838,7 @@ fn test_tcp_blocks_by_root_chunked_rpc() {
Hash256::zero(),
Hash256::zero(),
],
spec.max_request_blocks_upper_bound(),
spec.max_request_blocks(current_fork_name),
),
}));

Expand Down Expand Up @@ -934,7 +941,7 @@ fn test_tcp_blocks_by_root_chunked_rpc() {
tokio::select! {
_ = sender_future => {}
_ = receiver_future => {}
_ = sleep(Duration::from_secs(30)) => {
_ = sleep(Duration::from_secs(300)) => {
panic!("Future timed out");
}
}
Expand All @@ -952,14 +959,15 @@ fn test_tcp_blocks_by_root_chunked_rpc_terminates_correctly() {
let messages_to_send: u64 = 10;
let extra_messages_to_send: u64 = 10;

let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());
let current_fork = ForkName::Base;

let rt = Arc::new(Runtime::new().unwrap());
// get sender/receiver
rt.block_on(async {
let (mut sender, mut receiver) = common::build_node_pair(
Arc::downgrade(&rt),
ForkName::Base,
current_fork,
spec.clone(),
Protocol::Tcp,
false,
Expand All @@ -983,7 +991,7 @@ fn test_tcp_blocks_by_root_chunked_rpc_terminates_correctly() {
Hash256::zero(),
Hash256::zero(),
],
spec.max_request_blocks_upper_bound(),
spec.max_request_blocks(current_fork),
),
}));

Expand Down Expand Up @@ -1098,7 +1106,7 @@ fn goodbye_test(log_level: &str, enable_logging: bool, protocol: Protocol) {

let rt = Arc::new(Runtime::new().unwrap());

let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());

// get sender/receiver
rt.block_on(async {
Expand Down Expand Up @@ -1180,7 +1188,7 @@ fn test_delayed_rpc_response() {
// Set up the logging.
build_tracing_subscriber("debug", true);
let rt = Arc::new(Runtime::new().unwrap());
let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());

// Allow 1 token to be use used every 3 seconds.
const QUOTA_SEC: u64 = 3;
Expand Down Expand Up @@ -1314,7 +1322,7 @@ fn test_active_requests() {
// Set up the logging.
build_tracing_subscriber("debug", true);
let rt = Arc::new(Runtime::new().unwrap());
let spec = Arc::new(E::default_spec());
let spec = Arc::new(spec_with_all_forks_enabled());

rt.block_on(async {
// Get sender/receiver.
Expand Down
Loading
Loading