Skip to content

Commit

Permalink
Merge pull request #4032 from KatyaRyazantseva/random-block-electra
Browse files Browse the repository at this point in the history
Add randomized execution requests in `random_block_electra`
  • Loading branch information
jtraglia authored Dec 4, 2024
2 parents 4dfd346 + bdb1757 commit 72061bb
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
113 changes: 113 additions & 0 deletions tests/core/pyspec/eth2spec/test/helpers/multi_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,116 @@ def run_test_full_random_operations(spec, state, rng=Random(2080)):

yield 'blocks', [signed_block]
yield 'post', state


def get_random_execution_requests(spec, state, rng):
deposits = get_random_deposits_requests(spec, state, rng)
withdrawals = get_random_withdrawals_requests(spec, state, rng)
consolidations = get_random_consolidations_requests(spec, state, rng)

execution_requests = spec.ExecutionRequests(
deposits=deposits,
withdrawals=withdrawals,
consolidations=consolidations
)

return execution_requests


def get_random_deposits_requests(spec, state, rng, num_deposits=None):
if num_deposits is None:
num_deposits = rng.randint(0, spec.MAX_DEPOSIT_REQUESTS_PER_PAYLOAD)

deposit_data_leaves = [spec.DepositData() for _ in range(len(state.validators))]

deposits_requests = []

for i in range(num_deposits):
index = rng.randrange(0, num_deposits)
withdrawal_pubkey = pubkeys[index]
withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(withdrawal_pubkey)[1:]

deposit, _, _ = build_deposit(
spec,
deposit_data_leaves,
pubkeys[index],
privkeys[index],
rng.randint(spec.EFFECTIVE_BALANCE_INCREMENT, 2 * spec.MAX_EFFECTIVE_BALANCE_ELECTRA),
withdrawal_credentials=withdrawal_credentials,
signed=True,
)

deposit_request = spec.DepositRequest(
pubkey=deposit.data.pubkey,
withdrawal_credentials=deposit.data.withdrawal_credentials,
amount=deposit.data.amount,
signature=deposit.data.signature,
index=deposit.data.index,
)

deposits_requests.append(deposit_request)

return deposits_requests


def get_random_withdrawals_requests(spec, state, rng, num_withdrawals=None):
if num_withdrawals is None:
num_withdrawals = rng.randint(0, spec.MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD)

withdrawals_requests = []

state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH

current_epoch = spec.get_current_epoch(state)
active_validator_indices = spec.get_active_validator_indices(state, current_epoch)

for _ in range(num_withdrawals):
if not active_validator_indices:
break

address = rng.getrandbits(160).to_bytes(20, 'big')

validator_index = rng.choice(active_validator_indices)
validator = state.validators[validator_index]
validator_balance = state.balances[validator_index]

withdrawal_request = spec.WithdrawalRequest(
source_address=address,
validator_pubkey=validator.pubkey,
amount=rng.randint(0, validator_balance),
)

withdrawals_requests.append(withdrawal_request)

return withdrawals_requests


def get_random_consolidations_requests(spec, state, rng, num_consolidations=None):
if num_consolidations is None:
num_consolidations = rng.randint(0, spec.MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD)

consolidations_requests = []

state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH

current_epoch = spec.get_current_epoch(state)
active_validator_indices = spec.get_active_validator_indices(state, current_epoch)

for _ in range(num_consolidations):
source_address = rng.getrandbits(160).to_bytes(20, 'big')

source_index = rng.choice(active_validator_indices)
target_index = rng.choice(active_validator_indices)

source_validator = state.validators[source_index]
target_validator = state.validators[target_index]

consolidation_request = spec.ConsolidationRequest(
source_address=source_address,
source_pubkey=source_validator.pubkey,
target_pubkey=target_validator.pubkey,
)

consolidations_requests.append(consolidation_request)

return consolidations_requests
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
get_random_bls_to_execution_changes,
get_random_sync_aggregate,
prepare_state_and_get_random_deposits,
get_random_execution_requests,
)
from eth2spec.test.helpers.inactivity_scores import (
randomize_inactivity_scores,
Expand Down Expand Up @@ -262,6 +263,7 @@ def random_block_deneb(spec, state, signed_blocks, scenario_state, rng=Random(34

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

return block

Expand Down

0 comments on commit 72061bb

Please sign in to comment.