Skip to content

Commit e4bfe71

Browse files
committed
Thread through ChainSpec
1 parent 063b79c commit e4bfe71

File tree

11 files changed

+69
-34
lines changed

11 files changed

+69
-34
lines changed

beacon_node/beacon_chain/src/data_availability_checker/overflow_lru_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ mod pending_components_tests {
11781178
let mut rng = StdRng::seed_from_u64(0xDEADBEEF0BAD5EEDu64);
11791179
let spec = test_spec::<E>();
11801180
let (block, blobs_vec) =
1181-
generate_rand_block_and_blobs::<E>(ForkName::Deneb, NumBlobs::Random, &mut rng);
1181+
generate_rand_block_and_blobs::<E>(ForkName::Deneb, NumBlobs::Random, &mut rng, &spec);
11821182
let max_len = spec.max_blobs_per_block(block.epoch()) as usize;
11831183
let mut blobs: RuntimeFixedList<Option<Arc<BlobSidecar<E>>>> =
11841184
RuntimeFixedList::default(max_len);

beacon_node/beacon_chain/src/test_utils.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ where
511511

512512
pub fn mock_execution_layer_with_config(mut self) -> Self {
513513
let mock = mock_execution_layer_from_parts::<E>(
514-
self.spec.as_ref().expect("cannot build without spec"),
514+
self.spec.clone().expect("cannot build without spec"),
515515
self.runtime.task_executor.clone(),
516516
);
517517
self.execution_layer = Some(mock.el.clone());
@@ -611,7 +611,7 @@ where
611611
}
612612

613613
pub fn mock_execution_layer_from_parts<E: EthSpec>(
614-
spec: &ChainSpec,
614+
spec: Arc<ChainSpec>,
615615
task_executor: TaskExecutor,
616616
) -> MockExecutionLayer<E> {
617617
let shanghai_time = spec.capella_fork_epoch.map(|epoch| {
@@ -624,7 +624,7 @@ pub fn mock_execution_layer_from_parts<E: EthSpec>(
624624
HARNESS_GENESIS_TIME + spec.seconds_per_slot * E::slots_per_epoch() * epoch.as_u64()
625625
});
626626

627-
let kzg = get_kzg(spec);
627+
let kzg = get_kzg(&spec);
628628

629629
MockExecutionLayer::new(
630630
task_executor,
@@ -633,7 +633,7 @@ pub fn mock_execution_layer_from_parts<E: EthSpec>(
633633
cancun_time,
634634
prague_time,
635635
Some(JwtKey::from_slice(&DEFAULT_JWT_SECRET).unwrap()),
636-
spec.clone(),
636+
spec,
637637
Some(kzg),
638638
)
639639
}
@@ -2816,11 +2816,12 @@ pub fn generate_rand_block_and_blobs<E: EthSpec>(
28162816
fork_name: ForkName,
28172817
num_blobs: NumBlobs,
28182818
rng: &mut impl Rng,
2819+
spec: &ChainSpec,
28192820
) -> (SignedBeaconBlock<E, FullPayload<E>>, Vec<BlobSidecar<E>>) {
28202821
let inner = map_fork_name!(fork_name, BeaconBlock, <_>::random_for_test(rng));
28212822

28222823
let mut block = SignedBeaconBlock::from_block(inner, types::Signature::random_for_test(rng));
2823-
2824+
let max_blobs = spec.max_blobs_per_block(block.epoch()) as usize;
28242825
let mut blob_sidecars = vec![];
28252826

28262827
let bundle = match block {
@@ -2830,8 +2831,7 @@ pub fn generate_rand_block_and_blobs<E: EthSpec>(
28302831
// Get either zero blobs or a random number of blobs between 1 and Max Blobs.
28312832
let payload: &mut FullPayloadDeneb<E> = &mut message.body.execution_payload;
28322833
let num_blobs = match num_blobs {
2833-
// TODO(pawan): thread the chainspec value here
2834-
NumBlobs::Random => rng.gen_range(1..=6),
2834+
NumBlobs::Random => rng.gen_range(1..=max_blobs),
28352835
NumBlobs::Number(n) => n,
28362836
NumBlobs::None => 0,
28372837
};
@@ -2851,8 +2851,7 @@ pub fn generate_rand_block_and_blobs<E: EthSpec>(
28512851
// Get either zero blobs or a random number of blobs between 1 and Max Blobs.
28522852
let payload: &mut FullPayloadElectra<E> = &mut message.body.execution_payload;
28532853
let num_blobs = match num_blobs {
2854-
// TODO(pawan): thread the chainspec value here
2855-
NumBlobs::Random => rng.gen_range(1..=6),
2854+
NumBlobs::Random => rng.gen_range(1..=max_blobs),
28562855
NumBlobs::Number(n) => n,
28572856
NumBlobs::None => 0,
28582857
};
@@ -2906,7 +2905,7 @@ pub fn generate_rand_block_and_data_columns<E: EthSpec>(
29062905
DataColumnSidecarList<E>,
29072906
) {
29082907
let kzg = get_kzg(spec);
2909-
let (block, blobs) = generate_rand_block_and_blobs(fork_name, num_blobs, rng);
2908+
let (block, blobs) = generate_rand_block_and_blobs(fork_name, num_blobs, rng, spec);
29102909
let blob_refs = blobs.iter().map(|b| &b.blob).collect::<Vec<_>>();
29112910
let data_columns = blobs_to_data_column_sidecars(&blob_refs, &block, &kzg, spec).unwrap();
29122911

beacon_node/execution_layer/src/engine_api/http.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,8 @@ mod test {
13211321

13221322
impl Tester {
13231323
pub fn new(with_auth: bool) -> Self {
1324-
let server = MockServer::unit_testing();
1324+
let spec = Arc::new(MainnetEthSpec::default_spec());
1325+
let server = MockServer::unit_testing(spec);
13251326

13261327
let rpc_url = SensitiveUrl::parse(&server.url()).unwrap();
13271328
let echo_url = SensitiveUrl::parse(&format!("{}/echo", server.url())).unwrap();

beacon_node/execution_layer/src/test_utils/execution_block_generator.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ pub struct ExecutionBlockGenerator<E: EthSpec> {
153153
pub blobs_bundles: HashMap<PayloadId, BlobsBundle<E>>,
154154
pub kzg: Option<Arc<Kzg>>,
155155
rng: Arc<Mutex<StdRng>>,
156+
spec: Arc<ChainSpec>,
156157
}
157158

158159
fn make_rng() -> Arc<Mutex<StdRng>> {
@@ -162,13 +163,15 @@ fn make_rng() -> Arc<Mutex<StdRng>> {
162163
}
163164

164165
impl<E: EthSpec> ExecutionBlockGenerator<E> {
166+
#[allow(clippy::too_many_arguments)]
165167
pub fn new(
166168
terminal_total_difficulty: Uint256,
167169
terminal_block_number: u64,
168170
terminal_block_hash: ExecutionBlockHash,
169171
shanghai_time: Option<u64>,
170172
cancun_time: Option<u64>,
171173
prague_time: Option<u64>,
174+
spec: Arc<ChainSpec>,
172175
kzg: Option<Arc<Kzg>>,
173176
) -> Self {
174177
let mut gen = Self {
@@ -188,6 +191,7 @@ impl<E: EthSpec> ExecutionBlockGenerator<E> {
188191
blobs_bundles: <_>::default(),
189192
kzg,
190193
rng: make_rng(),
194+
spec,
191195
};
192196

193197
gen.insert_pow_block(0).unwrap();
@@ -671,8 +675,11 @@ impl<E: EthSpec> ExecutionBlockGenerator<E> {
671675
if execution_payload.fork_name().deneb_enabled() {
672676
// get random number between 0 and Max Blobs
673677
let mut rng = self.rng.lock();
674-
// TODO(pawan): thread the chainspec value here somehow
675-
let num_blobs = rng.gen::<usize>() % (6 + 1);
678+
let max_blobs = self
679+
.spec
680+
.max_blobs_per_block_by_fork(execution_payload.fork_name())
681+
as usize;
682+
let num_blobs = rng.gen::<usize>() % (max_blobs + 1);
676683
let (bundle, transactions) = generate_blobs(num_blobs)?;
677684
for tx in Vec::from(transactions) {
678685
execution_payload
@@ -875,6 +882,7 @@ mod test {
875882
const TERMINAL_DIFFICULTY: u64 = 10;
876883
const TERMINAL_BLOCK: u64 = 10;
877884
const DIFFICULTY_INCREMENT: u64 = 1;
885+
let spec = Arc::new(MainnetEthSpec::default_spec());
878886

879887
let mut generator: ExecutionBlockGenerator<MainnetEthSpec> = ExecutionBlockGenerator::new(
880888
Uint256::from(TERMINAL_DIFFICULTY),
@@ -883,6 +891,7 @@ mod test {
883891
None,
884892
None,
885893
None,
894+
spec,
886895
None,
887896
);
888897

beacon_node/execution_layer/src/test_utils/mock_execution_layer.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct MockExecutionLayer<E: EthSpec> {
1313
pub server: MockServer<E>,
1414
pub el: ExecutionLayer<E>,
1515
pub executor: TaskExecutor,
16-
pub spec: ChainSpec,
16+
pub spec: Arc<ChainSpec>,
1717
}
1818

1919
impl<E: EthSpec> MockExecutionLayer<E> {
@@ -29,7 +29,7 @@ impl<E: EthSpec> MockExecutionLayer<E> {
2929
None,
3030
None,
3131
Some(JwtKey::from_slice(&DEFAULT_JWT_SECRET).unwrap()),
32-
spec,
32+
Arc::new(spec),
3333
None,
3434
)
3535
}
@@ -42,7 +42,7 @@ impl<E: EthSpec> MockExecutionLayer<E> {
4242
cancun_time: Option<u64>,
4343
prague_time: Option<u64>,
4444
jwt_key: Option<JwtKey>,
45-
spec: ChainSpec,
45+
spec: Arc<ChainSpec>,
4646
kzg: Option<Arc<Kzg>>,
4747
) -> Self {
4848
let handle = executor.handle().unwrap();
@@ -57,6 +57,7 @@ impl<E: EthSpec> MockExecutionLayer<E> {
5757
shanghai_time,
5858
cancun_time,
5959
prague_time,
60+
spec.clone(),
6061
kzg,
6162
);
6263

@@ -320,7 +321,7 @@ impl<E: EthSpec> MockExecutionLayer<E> {
320321

321322
pub async fn with_terminal_block<'a, U, V>(self, func: U) -> Self
322323
where
323-
U: Fn(ChainSpec, ExecutionLayer<E>, Option<ExecutionBlock>) -> V,
324+
U: Fn(Arc<ChainSpec>, ExecutionLayer<E>, Option<ExecutionBlock>) -> V,
324325
V: Future<Output = ()>,
325326
{
326327
let terminal_block_number = self

beacon_node/execution_layer/src/test_utils/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::marker::PhantomData;
2121
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
2222
use std::sync::{Arc, LazyLock};
2323
use tokio::{runtime, sync::oneshot};
24-
use types::{EthSpec, ExecutionBlockHash, Uint256};
24+
use types::{ChainSpec, EthSpec, ExecutionBlockHash, Uint256};
2525
use warp::{http::StatusCode, Filter, Rejection};
2626

2727
use crate::EngineCapabilities;
@@ -107,7 +107,7 @@ pub struct MockServer<E: EthSpec> {
107107
}
108108

109109
impl<E: EthSpec> MockServer<E> {
110-
pub fn unit_testing() -> Self {
110+
pub fn unit_testing(chain_spec: Arc<ChainSpec>) -> Self {
111111
Self::new(
112112
&runtime::Handle::current(),
113113
JwtKey::from_slice(&DEFAULT_JWT_SECRET).unwrap(),
@@ -117,13 +117,15 @@ impl<E: EthSpec> MockServer<E> {
117117
None, // FIXME(capella): should this be the default?
118118
None, // FIXME(deneb): should this be the default?
119119
None, // FIXME(electra): should this be the default?
120+
chain_spec,
120121
None,
121122
)
122123
}
123124

124125
pub fn new_with_config(
125126
handle: &runtime::Handle,
126127
config: MockExecutionConfig,
128+
spec: Arc<ChainSpec>,
127129
kzg: Option<Arc<Kzg>>,
128130
) -> Self {
129131
let MockExecutionConfig {
@@ -145,6 +147,7 @@ impl<E: EthSpec> MockServer<E> {
145147
shanghai_time,
146148
cancun_time,
147149
prague_time,
150+
spec,
148151
kzg,
149152
);
150153

@@ -208,6 +211,7 @@ impl<E: EthSpec> MockServer<E> {
208211
shanghai_time: Option<u64>,
209212
cancun_time: Option<u64>,
210213
prague_time: Option<u64>,
214+
spec: Arc<ChainSpec>,
211215
kzg: Option<Arc<Kzg>>,
212216
) -> Self {
213217
Self::new_with_config(
@@ -222,6 +226,7 @@ impl<E: EthSpec> MockServer<E> {
222226
cancun_time,
223227
prague_time,
224228
},
229+
spec,
225230
kzg,
226231
)
227232
}

beacon_node/network/src/sync/block_sidecar_coupling.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ mod tests {
256256
let peer_id = PeerId::random();
257257
let mut rng = XorShiftRng::from_seed([42; 16]);
258258
let blocks = (0..4)
259-
.map(|_| generate_rand_block_and_blobs::<E>(ForkName::Base, NumBlobs::None, &mut rng).0)
259+
.map(|_| {
260+
generate_rand_block_and_blobs::<E>(ForkName::Base, NumBlobs::None, &mut rng, &spec)
261+
.0
262+
})
260263
.collect::<Vec<_>>();
261264
let max_len = spec.max_blobs_per_block(blocks.first().unwrap().epoch()) as usize;
262265
let mut info =
@@ -281,7 +284,13 @@ mod tests {
281284
let blocks = (0..4)
282285
.map(|_| {
283286
// Always generate some blobs.
284-
generate_rand_block_and_blobs::<E>(ForkName::Deneb, NumBlobs::Number(3), &mut rng).0
287+
generate_rand_block_and_blobs::<E>(
288+
ForkName::Deneb,
289+
NumBlobs::Number(3),
290+
&mut rng,
291+
&spec,
292+
)
293+
.0
285294
})
286295
.collect::<Vec<_>>();
287296
let max_len = spec.max_blobs_per_block(blocks.first().unwrap().epoch()) as usize;

beacon_node/network/src/sync/tests/lookups.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl TestRig {
216216
) -> (SignedBeaconBlock<E>, Vec<BlobSidecar<E>>) {
217217
let fork_name = self.fork_name;
218218
let rng = &mut self.rng;
219-
generate_rand_block_and_blobs::<E>(fork_name, num_blobs, rng)
219+
generate_rand_block_and_blobs::<E>(fork_name, num_blobs, rng, &self.spec)
220220
}
221221

222222
fn rand_block_and_data_columns(
@@ -1331,8 +1331,10 @@ impl TestRig {
13311331

13321332
#[test]
13331333
fn stable_rng() {
1334+
let spec = types::MainnetEthSpec::default_spec();
13341335
let mut rng = XorShiftRng::from_seed([42; 16]);
1335-
let (block, _) = generate_rand_block_and_blobs::<E>(ForkName::Base, NumBlobs::None, &mut rng);
1336+
let (block, _) =
1337+
generate_rand_block_and_blobs::<E>(ForkName::Base, NumBlobs::None, &mut rng, &spec);
13361338
assert_eq!(
13371339
block.canonical_root(),
13381340
Hash256::from_slice(

consensus/types/src/chain_spec.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,14 +606,15 @@ impl ChainSpec {
606606
}
607607
}
608608

609-
/// Returns the deneb preset value if peerdas epoch hasn't hit.
610-
/// Otherwise, returns the value obtained from the config.yaml.
609+
/// Return the value of `MAX_BLOBS_PER_BLOCK` appropriate for the fork at `epoch`.
611610
pub fn max_blobs_per_block(&self, epoch: Epoch) -> u64 {
612-
if self.is_peer_das_enabled_for_epoch(epoch) {
613-
self.max_blobs_per_block
614-
} else {
615-
default_max_blobs_per_block()
616-
}
611+
self.max_blobs_per_block_by_fork(self.fork_name_at_epoch(epoch))
612+
}
613+
614+
/// Return the value of `MAX_BLOBS_PER_BLOCK` appropriate for `fork`.
615+
pub fn max_blobs_per_block_by_fork(&self, _fork_name: ForkName) -> u64 {
616+
// TODO(electra): add Electra blobs per block change here
617+
self.max_blobs_per_block
617618
}
618619

619620
pub fn data_columns_per_subnet(&self) -> usize {

lcli/src/mock_el.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use execution_layer::{
99
};
1010
use std::net::Ipv4Addr;
1111
use std::path::PathBuf;
12+
use std::sync::Arc;
1213
use types::*;
1314

1415
pub fn run<E: EthSpec>(mut env: Environment<E>, matches: &ArgMatches) -> Result<(), String> {
@@ -21,7 +22,7 @@ pub fn run<E: EthSpec>(mut env: Environment<E>, matches: &ArgMatches) -> Result<
2122
let prague_time = parse_optional(matches, "prague-time")?;
2223

2324
let handle = env.core_context().executor.handle().unwrap();
24-
let spec = &E::default_spec();
25+
let spec = Arc::new(E::default_spec());
2526
let jwt_key = JwtKey::from_slice(&DEFAULT_JWT_SECRET).unwrap();
2627
std::fs::write(jwt_path, hex::encode(DEFAULT_JWT_SECRET)).unwrap();
2728

@@ -39,7 +40,7 @@ pub fn run<E: EthSpec>(mut env: Environment<E>, matches: &ArgMatches) -> Result<
3940
prague_time,
4041
};
4142
let kzg = None;
42-
let server: MockServer<E> = MockServer::new_with_config(&handle, config, kzg);
43+
let server: MockServer<E> = MockServer::new_with_config(&handle, config, spec, kzg);
4344

4445
if all_payloads_valid {
4546
eprintln!(

0 commit comments

Comments
 (0)