Skip to content

Commit 72061bb

Browse files
authored
Merge pull request ethereum#4032 from KatyaRyazantseva/random-block-electra
Add randomized execution requests in `random_block_electra`
2 parents 4dfd346 + bdb1757 commit 72061bb

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

tests/core/pyspec/eth2spec/test/helpers/multi_operations.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,116 @@ def run_test_full_random_operations(spec, state, rng=Random(2080)):
257257

258258
yield 'blocks', [signed_block]
259259
yield 'post', state
260+
261+
262+
def get_random_execution_requests(spec, state, rng):
263+
deposits = get_random_deposits_requests(spec, state, rng)
264+
withdrawals = get_random_withdrawals_requests(spec, state, rng)
265+
consolidations = get_random_consolidations_requests(spec, state, rng)
266+
267+
execution_requests = spec.ExecutionRequests(
268+
deposits=deposits,
269+
withdrawals=withdrawals,
270+
consolidations=consolidations
271+
)
272+
273+
return execution_requests
274+
275+
276+
def get_random_deposits_requests(spec, state, rng, num_deposits=None):
277+
if num_deposits is None:
278+
num_deposits = rng.randint(0, spec.MAX_DEPOSIT_REQUESTS_PER_PAYLOAD)
279+
280+
deposit_data_leaves = [spec.DepositData() for _ in range(len(state.validators))]
281+
282+
deposits_requests = []
283+
284+
for i in range(num_deposits):
285+
index = rng.randrange(0, num_deposits)
286+
withdrawal_pubkey = pubkeys[index]
287+
withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(withdrawal_pubkey)[1:]
288+
289+
deposit, _, _ = build_deposit(
290+
spec,
291+
deposit_data_leaves,
292+
pubkeys[index],
293+
privkeys[index],
294+
rng.randint(spec.EFFECTIVE_BALANCE_INCREMENT, 2 * spec.MAX_EFFECTIVE_BALANCE_ELECTRA),
295+
withdrawal_credentials=withdrawal_credentials,
296+
signed=True,
297+
)
298+
299+
deposit_request = spec.DepositRequest(
300+
pubkey=deposit.data.pubkey,
301+
withdrawal_credentials=deposit.data.withdrawal_credentials,
302+
amount=deposit.data.amount,
303+
signature=deposit.data.signature,
304+
index=deposit.data.index,
305+
)
306+
307+
deposits_requests.append(deposit_request)
308+
309+
return deposits_requests
310+
311+
312+
def get_random_withdrawals_requests(spec, state, rng, num_withdrawals=None):
313+
if num_withdrawals is None:
314+
num_withdrawals = rng.randint(0, spec.MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD)
315+
316+
withdrawals_requests = []
317+
318+
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
319+
320+
current_epoch = spec.get_current_epoch(state)
321+
active_validator_indices = spec.get_active_validator_indices(state, current_epoch)
322+
323+
for _ in range(num_withdrawals):
324+
if not active_validator_indices:
325+
break
326+
327+
address = rng.getrandbits(160).to_bytes(20, 'big')
328+
329+
validator_index = rng.choice(active_validator_indices)
330+
validator = state.validators[validator_index]
331+
validator_balance = state.balances[validator_index]
332+
333+
withdrawal_request = spec.WithdrawalRequest(
334+
source_address=address,
335+
validator_pubkey=validator.pubkey,
336+
amount=rng.randint(0, validator_balance),
337+
)
338+
339+
withdrawals_requests.append(withdrawal_request)
340+
341+
return withdrawals_requests
342+
343+
344+
def get_random_consolidations_requests(spec, state, rng, num_consolidations=None):
345+
if num_consolidations is None:
346+
num_consolidations = rng.randint(0, spec.MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD)
347+
348+
consolidations_requests = []
349+
350+
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
351+
352+
current_epoch = spec.get_current_epoch(state)
353+
active_validator_indices = spec.get_active_validator_indices(state, current_epoch)
354+
355+
for _ in range(num_consolidations):
356+
source_address = rng.getrandbits(160).to_bytes(20, 'big')
357+
358+
source_index = rng.choice(active_validator_indices)
359+
target_index = rng.choice(active_validator_indices)
360+
361+
source_validator = state.validators[source_index]
362+
target_validator = state.validators[target_index]
363+
364+
consolidation_request = spec.ConsolidationRequest(
365+
source_address=source_address,
366+
source_pubkey=source_validator.pubkey,
367+
target_pubkey=target_validator.pubkey,
368+
)
369+
370+
consolidations_requests.append(consolidation_request)
371+
372+
return consolidations_requests

tests/core/pyspec/eth2spec/test/utils/randomized_block_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
get_random_bls_to_execution_changes,
1717
get_random_sync_aggregate,
1818
prepare_state_and_get_random_deposits,
19+
get_random_execution_requests,
1920
)
2021
from eth2spec.test.helpers.inactivity_scores import (
2122
randomize_inactivity_scores,
@@ -262,6 +263,7 @@ def random_block_deneb(spec, state, signed_blocks, scenario_state, rng=Random(34
262263

263264
def random_block_electra(spec, state, signed_blocks, scenario_state, rng=Random(3456)):
264265
block = random_block_deneb(spec, state, signed_blocks, scenario_state, rng=rng)
266+
block.body.execution_requests = get_random_execution_requests(spec, state, rng=rng)
265267

266268
return block
267269

0 commit comments

Comments
 (0)