@@ -81,6 +81,10 @@ pub const TEST_DATA_COLUMN_SIDECARS_SSZ: &[u8] =
8181// a different value.
8282pub const DEFAULT_TARGET_AGGREGATORS : u64 = u64:: MAX ;
8383
84+ // Minimum and maximum number of blobs to generate in each slot when using the `NumBlobs::Random` option (default).
85+ const DEFAULT_MIN_BLOBS : usize = 1 ;
86+ const DEFAULT_MAX_BLOBS : usize = 2 ;
87+
8488static KZG : LazyLock < Arc < Kzg > > = LazyLock :: new ( || {
8589 let kzg = Kzg :: new_from_trusted_setup ( & get_trusted_setup ( ) ) . expect ( "should create kzg" ) ;
8690 Arc :: new ( kzg)
@@ -172,23 +176,28 @@ fn make_rng() -> Mutex<StdRng> {
172176 Mutex :: new ( StdRng :: seed_from_u64 ( 0x0DDB1A5E5BAD5EEDu64 ) )
173177}
174178
175- /// Return a `ChainSpec` suitable for test usage.
176- ///
177- /// If the `fork_from_env` feature is enabled, read the fork to use from the FORK_NAME environment
178- /// variable. Otherwise use the default spec.
179- pub fn test_spec < E : EthSpec > ( ) -> ChainSpec {
180- let mut spec = if cfg ! ( feature = "fork_from_env" ) {
179+ pub fn fork_name_from_env ( ) -> Option < ForkName > {
180+ if cfg ! ( feature = "fork_from_env" ) {
181181 let fork_name = std:: env:: var ( FORK_NAME_ENV_VAR ) . unwrap_or_else ( |e| {
182182 panic ! (
183183 "{} env var must be defined when using fork_from_env: {:?}" ,
184184 FORK_NAME_ENV_VAR , e
185185 )
186186 } ) ;
187- let fork = ForkName :: from_str ( fork_name. as_str ( ) ) . unwrap ( ) ;
188- fork. make_genesis_spec ( E :: default_spec ( ) )
187+ Some ( ForkName :: from_str ( fork_name. as_str ( ) ) . unwrap ( ) )
189188 } else {
190- E :: default_spec ( )
191- } ;
189+ None
190+ }
191+ }
192+
193+ /// Return a `ChainSpec` suitable for test usage.
194+ ///
195+ /// If the `fork_from_env` feature is enabled, read the fork to use from the FORK_NAME environment
196+ /// variable. Otherwise use the default spec.
197+ pub fn test_spec < E : EthSpec > ( ) -> ChainSpec {
198+ let mut spec = fork_name_from_env ( )
199+ . map ( |fork| fork. make_genesis_spec ( E :: default_spec ( ) ) )
200+ . unwrap_or_else ( || E :: default_spec ( ) ) ;
192201
193202 // Set target aggregators to a high value by default.
194203 spec. target_aggregators_per_committee = DEFAULT_TARGET_AGGREGATORS ;
@@ -3245,96 +3254,49 @@ pub enum NumBlobs {
32453254 None ,
32463255}
32473256
3257+ macro_rules! add_blob_transactions {
3258+ ( $message: expr, $payload_type: ty, $num_blobs: expr, $rng: expr, $fork_name: expr) => { {
3259+ let num_blobs = match $num_blobs {
3260+ NumBlobs :: Random => $rng. random_range( DEFAULT_MIN_BLOBS ..=DEFAULT_MAX_BLOBS ) ,
3261+ NumBlobs :: Number ( n) => n,
3262+ NumBlobs :: None => 0 ,
3263+ } ;
3264+ let ( bundle, transactions) =
3265+ execution_layer:: test_utils:: generate_blobs:: <E >( num_blobs, $fork_name) . unwrap( ) ;
3266+
3267+ let payload: & mut $payload_type = & mut $message. body. execution_payload;
3268+ payload. execution_payload. transactions = <_>:: default ( ) ;
3269+ for tx in Vec :: from( transactions) {
3270+ payload. execution_payload. transactions. push( tx) . unwrap( ) ;
3271+ }
3272+ $message. body. blob_kzg_commitments = bundle. commitments. clone( ) ;
3273+ bundle
3274+ } } ;
3275+ }
3276+
32483277pub fn generate_rand_block_and_blobs < E : EthSpec > (
32493278 fork_name : ForkName ,
32503279 num_blobs : NumBlobs ,
32513280 rng : & mut impl Rng ,
3252- spec : & ChainSpec ,
32533281) -> ( SignedBeaconBlock < E , FullPayload < E > > , Vec < BlobSidecar < E > > ) {
32543282 let inner = map_fork_name ! ( fork_name, BeaconBlock , <_>:: random_for_test( rng) ) ;
32553283
32563284 let mut block = SignedBeaconBlock :: from_block ( inner, types:: Signature :: random_for_test ( rng) ) ;
3257- let max_blobs = spec. max_blobs_per_block ( block. epoch ( ) ) as usize ;
32583285 let mut blob_sidecars = vec ! [ ] ;
32593286
32603287 let bundle = match block {
32613288 SignedBeaconBlock :: Deneb ( SignedBeaconBlockDeneb {
32623289 ref mut message, ..
3263- } ) => {
3264- // Get either zero blobs or a random number of blobs between 1 and Max Blobs.
3265- let payload: & mut FullPayloadDeneb < E > = & mut message. body . execution_payload ;
3266- let num_blobs = match num_blobs {
3267- NumBlobs :: Random => rng. random_range ( 1 ..=max_blobs) ,
3268- NumBlobs :: Number ( n) => n,
3269- NumBlobs :: None => 0 ,
3270- } ;
3271- let ( bundle, transactions) =
3272- execution_layer:: test_utils:: generate_blobs :: < E > ( num_blobs, fork_name) . unwrap ( ) ;
3273-
3274- payload. execution_payload . transactions = <_ >:: default ( ) ;
3275- for tx in Vec :: from ( transactions) {
3276- payload. execution_payload . transactions . push ( tx) . unwrap ( ) ;
3277- }
3278- message. body . blob_kzg_commitments = bundle. commitments . clone ( ) ;
3279- bundle
3280- }
3290+ } ) => add_blob_transactions ! ( message, FullPayloadDeneb <E >, num_blobs, rng, fork_name) ,
32813291 SignedBeaconBlock :: Electra ( SignedBeaconBlockElectra {
32823292 ref mut message, ..
3283- } ) => {
3284- // Get either zero blobs or a random number of blobs between 1 and Max Blobs.
3285- let payload: & mut FullPayloadElectra < E > = & mut message. body . execution_payload ;
3286- let num_blobs = match num_blobs {
3287- NumBlobs :: Random => rng. random_range ( 1 ..=max_blobs) ,
3288- NumBlobs :: Number ( n) => n,
3289- NumBlobs :: None => 0 ,
3290- } ;
3291- let ( bundle, transactions) =
3292- execution_layer:: test_utils:: generate_blobs :: < E > ( num_blobs, fork_name) . unwrap ( ) ;
3293- payload. execution_payload . transactions = <_ >:: default ( ) ;
3294- for tx in Vec :: from ( transactions) {
3295- payload. execution_payload . transactions . push ( tx) . unwrap ( ) ;
3296- }
3297- message. body . blob_kzg_commitments = bundle. commitments . clone ( ) ;
3298- bundle
3299- }
3293+ } ) => add_blob_transactions ! ( message, FullPayloadElectra <E >, num_blobs, rng, fork_name) ,
33003294 SignedBeaconBlock :: Fulu ( SignedBeaconBlockFulu {
33013295 ref mut message, ..
3302- } ) => {
3303- // Get either zero blobs or a random number of blobs between 1 and Max Blobs.
3304- let payload: & mut FullPayloadFulu < E > = & mut message. body . execution_payload ;
3305- let num_blobs = match num_blobs {
3306- NumBlobs :: Random => rng. random_range ( 1 ..=max_blobs) ,
3307- NumBlobs :: Number ( n) => n,
3308- NumBlobs :: None => 0 ,
3309- } ;
3310- let ( bundle, transactions) =
3311- execution_layer:: test_utils:: generate_blobs :: < E > ( num_blobs, fork_name) . unwrap ( ) ;
3312- payload. execution_payload . transactions = <_ >:: default ( ) ;
3313- for tx in Vec :: from ( transactions) {
3314- payload. execution_payload . transactions . push ( tx) . unwrap ( ) ;
3315- }
3316- message. body . blob_kzg_commitments = bundle. commitments . clone ( ) ;
3317- bundle
3318- }
3296+ } ) => add_blob_transactions ! ( message, FullPayloadFulu <E >, num_blobs, rng, fork_name) ,
33193297 SignedBeaconBlock :: Gloas ( SignedBeaconBlockGloas {
33203298 ref mut message, ..
3321- } ) => {
3322- // Get either zero blobs or a random number of blobs between 1 and Max Blobs.
3323- let payload: & mut FullPayloadGloas < E > = & mut message. body . execution_payload ;
3324- let num_blobs = match num_blobs {
3325- NumBlobs :: Random => rng. random_range ( 1 ..=max_blobs) ,
3326- NumBlobs :: Number ( n) => n,
3327- NumBlobs :: None => 0 ,
3328- } ;
3329- let ( bundle, transactions) =
3330- execution_layer:: test_utils:: generate_blobs :: < E > ( num_blobs, fork_name) . unwrap ( ) ;
3331- payload. execution_payload . transactions = <_ >:: default ( ) ;
3332- for tx in Vec :: from ( transactions) {
3333- payload. execution_payload . transactions . push ( tx) . unwrap ( ) ;
3334- }
3335- message. body . blob_kzg_commitments = bundle. commitments . clone ( ) ;
3336- bundle
3337- }
3299+ } ) => add_blob_transactions ! ( message, FullPayloadGloas <E >, num_blobs, rng, fork_name) ,
33383300 _ => return ( block, blob_sidecars) ,
33393301 } ;
33403302
@@ -3375,7 +3337,7 @@ pub fn generate_rand_block_and_data_columns<E: EthSpec>(
33753337 SignedBeaconBlock < E , FullPayload < E > > ,
33763338 DataColumnSidecarList < E > ,
33773339) {
3378- let ( block, _blobs) = generate_rand_block_and_blobs ( fork_name, num_blobs, rng, spec ) ;
3340+ let ( block, _blobs) = generate_rand_block_and_blobs ( fork_name, num_blobs, rng) ;
33793341 let data_columns = generate_data_column_sidecars_from_block ( & block, spec) ;
33803342 ( block, data_columns)
33813343}
0 commit comments