|
9 | 9 | from eth2spec.test.helpers.state import ( |
10 | 10 | next_epoch_with_full_participation, |
11 | 11 | ) |
| 12 | +from eth2spec.test.helpers.withdrawals import ( |
| 13 | + set_eth1_withdrawal_credential_with_balance, |
| 14 | + set_compounding_withdrawal_credential_with_balance, |
| 15 | +) |
12 | 16 |
|
13 | 17 | # *********************** |
14 | 18 | # * CONSOLIDATION TESTS * |
@@ -316,11 +320,9 @@ def test_pending_consolidation_source_balance_less_than_max_effective(spec, stat |
316 | 320 | ) |
317 | 321 | # Set withdrawable epoch to current epoch to allow processing |
318 | 322 | state.validators[source_index].withdrawable_epoch = current_epoch |
319 | | - # Set the target withdrawal credential to eth1 |
320 | | - eth1_withdrawal_credential = ( |
321 | | - spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20 |
322 | | - ) |
323 | | - state.validators[target_index].withdrawal_credentials = eth1_withdrawal_credential |
| 323 | + # Set source and target withdrawal credential to eth1 |
| 324 | + set_eth1_withdrawal_credential_with_balance(spec, state, source_index) |
| 325 | + set_eth1_withdrawal_credential_with_balance(spec, state, target_index) |
324 | 326 | # Set the source balance to be less than effective_balance |
325 | 327 | pre_balance_source = state.validators[source_index].effective_balance - spec.EFFECTIVE_BALANCE_INCREMENT // 8 |
326 | 328 | state.balances[source_index] = pre_balance_source |
@@ -349,11 +351,73 @@ def test_pending_consolidation_source_balance_greater_than_max_effective(spec, s |
349 | 351 | ) |
350 | 352 | # Set withdrawable epoch to current epoch to allow processing |
351 | 353 | state.validators[source_index].withdrawable_epoch = current_epoch |
352 | | - # Set the target withdrawal credential to eth1 |
353 | | - eth1_withdrawal_credential = ( |
354 | | - spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20 |
| 354 | + # Set source and target withdrawal credential to eth1 |
| 355 | + set_eth1_withdrawal_credential_with_balance(spec, state, source_index) |
| 356 | + set_eth1_withdrawal_credential_with_balance(spec, state, target_index) |
| 357 | + # Set the source balance to be greater than effective_balance |
| 358 | + excess_source_balance = spec.EFFECTIVE_BALANCE_INCREMENT // 8 |
| 359 | + pre_balance_source = state.validators[source_index].effective_balance + excess_source_balance |
| 360 | + state.balances[source_index] = pre_balance_source |
| 361 | + |
| 362 | + pre_balance_target = state.balances[target_index] |
| 363 | + |
| 364 | + source_max_effective_balance = spec.get_max_effective_balance(state.validators[source_index]) |
| 365 | + assert state.balances[source_index] > source_max_effective_balance |
| 366 | + |
| 367 | + yield from run_epoch_processing_with(spec, state, "process_pending_consolidations") |
| 368 | + |
| 369 | + # Pending consolidation was successfully processed |
| 370 | + assert state.balances[target_index] == pre_balance_target + source_max_effective_balance |
| 371 | + assert state.balances[source_index] == excess_source_balance |
| 372 | + assert state.pending_consolidations == [] |
| 373 | + |
| 374 | + |
| 375 | +@with_electra_and_later |
| 376 | +@spec_state_test |
| 377 | +def test_pending_consolidation_source_balance_less_than_max_effective_compounding(spec, state): |
| 378 | + current_epoch = spec.get_current_epoch(state) |
| 379 | + source_index = spec.get_active_validator_indices(state, current_epoch)[0] |
| 380 | + target_index = spec.get_active_validator_indices(state, current_epoch)[1] |
| 381 | + # append pending consolidation |
| 382 | + state.pending_consolidations.append( |
| 383 | + spec.PendingConsolidation(source_index=source_index, target_index=target_index) |
355 | 384 | ) |
356 | | - state.validators[target_index].withdrawal_credentials = eth1_withdrawal_credential |
| 385 | + # Set withdrawable epoch to current epoch to allow processing |
| 386 | + state.validators[source_index].withdrawable_epoch = current_epoch |
| 387 | + # Set source and target withdrawal credential to compounding |
| 388 | + set_compounding_withdrawal_credential_with_balance(spec, state, source_index) |
| 389 | + set_compounding_withdrawal_credential_with_balance(spec, state, target_index) |
| 390 | + # Set the source balance to be less than effective_balance |
| 391 | + pre_balance_source = state.validators[source_index].effective_balance - spec.EFFECTIVE_BALANCE_INCREMENT // 8 |
| 392 | + state.balances[source_index] = pre_balance_source |
| 393 | + |
| 394 | + pre_balance_target = state.balances[target_index] |
| 395 | + |
| 396 | + assert state.balances[source_index] < spec.get_max_effective_balance(state.validators[source_index]) |
| 397 | + |
| 398 | + yield from run_epoch_processing_with(spec, state, "process_pending_consolidations") |
| 399 | + |
| 400 | + # Pending consolidation was successfully processed |
| 401 | + assert state.balances[target_index] == pre_balance_target + pre_balance_source |
| 402 | + assert state.balances[source_index] == 0 |
| 403 | + assert state.pending_consolidations == [] |
| 404 | + |
| 405 | + |
| 406 | +@with_electra_and_later |
| 407 | +@spec_state_test |
| 408 | +def test_pending_consolidation_source_balance_greater_than_max_effective_compounding(spec, state): |
| 409 | + current_epoch = spec.get_current_epoch(state) |
| 410 | + source_index = spec.get_active_validator_indices(state, current_epoch)[0] |
| 411 | + target_index = spec.get_active_validator_indices(state, current_epoch)[1] |
| 412 | + # append pending consolidation |
| 413 | + state.pending_consolidations.append( |
| 414 | + spec.PendingConsolidation(source_index=source_index, target_index=target_index) |
| 415 | + ) |
| 416 | + # Set withdrawable epoch to current epoch to allow processing |
| 417 | + state.validators[source_index].withdrawable_epoch = current_epoch |
| 418 | + # Set source and target withdrawal credential to compounding |
| 419 | + set_compounding_withdrawal_credential_with_balance(spec, state, source_index) |
| 420 | + set_compounding_withdrawal_credential_with_balance(spec, state, target_index) |
357 | 421 | # Set the source balance to be greater than effective_balance |
358 | 422 | excess_source_balance = spec.EFFECTIVE_BALANCE_INCREMENT // 8 |
359 | 423 | pre_balance_source = state.validators[source_index].effective_balance + excess_source_balance |
|
0 commit comments