|
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 * |
@@ -302,3 +306,131 @@ def test_pending_consolidation_with_pending_deposit(spec, state): |
302 | 306 | # Pending deposit to the source was not processed. |
303 | 307 | # It should only be processed in the next epoch transition |
304 | 308 | assert state.pending_deposits == [pending_deposit] |
| 309 | + |
| 310 | + |
| 311 | +@with_electra_and_later |
| 312 | +@spec_state_test |
| 313 | +def test_pending_consolidation_source_balance_less_than_max_effective(spec, state): |
| 314 | + current_epoch = spec.get_current_epoch(state) |
| 315 | + source_index = spec.get_active_validator_indices(state, current_epoch)[0] |
| 316 | + target_index = spec.get_active_validator_indices(state, current_epoch)[1] |
| 317 | + # append pending consolidation |
| 318 | + state.pending_consolidations.append( |
| 319 | + spec.PendingConsolidation(source_index=source_index, target_index=target_index) |
| 320 | + ) |
| 321 | + # Set withdrawable epoch to current epoch to allow processing |
| 322 | + state.validators[source_index].withdrawable_epoch = current_epoch |
| 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) |
| 326 | + # Set the source balance to be less than effective_balance |
| 327 | + pre_balance_source = state.validators[source_index].effective_balance - spec.EFFECTIVE_BALANCE_INCREMENT // 8 |
| 328 | + state.balances[source_index] = pre_balance_source |
| 329 | + |
| 330 | + pre_balance_target = state.balances[target_index] |
| 331 | + |
| 332 | + assert state.balances[source_index] < spec.get_max_effective_balance(state.validators[source_index]) |
| 333 | + |
| 334 | + yield from run_epoch_processing_with(spec, state, "process_pending_consolidations") |
| 335 | + |
| 336 | + # Pending consolidation was successfully processed |
| 337 | + assert state.balances[target_index] == pre_balance_target + pre_balance_source |
| 338 | + assert state.balances[source_index] == 0 |
| 339 | + assert state.pending_consolidations == [] |
| 340 | + |
| 341 | + |
| 342 | +@with_electra_and_later |
| 343 | +@spec_state_test |
| 344 | +def test_pending_consolidation_source_balance_greater_than_max_effective(spec, state): |
| 345 | + current_epoch = spec.get_current_epoch(state) |
| 346 | + source_index = spec.get_active_validator_indices(state, current_epoch)[0] |
| 347 | + target_index = spec.get_active_validator_indices(state, current_epoch)[1] |
| 348 | + # append pending consolidation |
| 349 | + state.pending_consolidations.append( |
| 350 | + spec.PendingConsolidation(source_index=source_index, target_index=target_index) |
| 351 | + ) |
| 352 | + # Set withdrawable epoch to current epoch to allow processing |
| 353 | + state.validators[source_index].withdrawable_epoch = current_epoch |
| 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) |
| 384 | + ) |
| 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) |
| 421 | + # Set the source balance to be greater than effective_balance |
| 422 | + excess_source_balance = spec.EFFECTIVE_BALANCE_INCREMENT // 8 |
| 423 | + pre_balance_source = state.validators[source_index].effective_balance + excess_source_balance |
| 424 | + state.balances[source_index] = pre_balance_source |
| 425 | + |
| 426 | + pre_balance_target = state.balances[target_index] |
| 427 | + |
| 428 | + source_max_effective_balance = spec.get_max_effective_balance(state.validators[source_index]) |
| 429 | + assert state.balances[source_index] > source_max_effective_balance |
| 430 | + |
| 431 | + yield from run_epoch_processing_with(spec, state, "process_pending_consolidations") |
| 432 | + |
| 433 | + # Pending consolidation was successfully processed |
| 434 | + assert state.balances[target_index] == pre_balance_target + source_max_effective_balance |
| 435 | + assert state.balances[source_index] == excess_source_balance |
| 436 | + assert state.pending_consolidations == [] |
0 commit comments