|
| 1 | +import pytest |
| 2 | + |
| 3 | +from eth_utils import ( |
| 4 | + denoms, |
| 5 | + ValidationError, |
| 6 | +) |
| 7 | + |
| 8 | +from eth.utils import bls |
| 9 | + |
| 10 | +from eth.beacon.constants import ( |
| 11 | + EMPTY_SIGNATURE, |
| 12 | +) |
| 13 | +from eth.beacon.enums import ( |
| 14 | + SignatureDomain, |
| 15 | +) |
| 16 | +from eth.beacon.helpers import ( |
| 17 | + get_domain, |
| 18 | +) |
| 19 | +from eth.beacon.deposits import ( |
| 20 | + process_deposit, |
| 21 | + validate_proof_of_possession, |
| 22 | +) |
| 23 | +from eth.beacon.types.states import BeaconState |
| 24 | +from eth.beacon.types.deposit_input import DepositInput |
| 25 | + |
| 26 | + |
| 27 | +def sign_proof_of_possession(deposit_input, privkey, domain): |
| 28 | + return bls.sign(deposit_input.root, privkey, domain) |
| 29 | + |
| 30 | + |
| 31 | +def make_deposit_input(pubkey, withdrawal_credentials, randao_commitment): |
| 32 | + return DepositInput( |
| 33 | + pubkey=pubkey, |
| 34 | + withdrawal_credentials=withdrawal_credentials, |
| 35 | + randao_commitment=randao_commitment, |
| 36 | + proof_of_possession=EMPTY_SIGNATURE, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize( |
| 41 | + "expected", |
| 42 | + ( |
| 43 | + (True), |
| 44 | + (ValidationError), |
| 45 | + ), |
| 46 | +) |
| 47 | +def test_validate_proof_of_possession(sample_beacon_state_params, pubkeys, privkeys, expected): |
| 48 | + state = BeaconState(**sample_beacon_state_params) |
| 49 | + |
| 50 | + privkey = privkeys[0] |
| 51 | + pubkey = pubkeys[0] |
| 52 | + withdrawal_credentials = b'\x34' * 32 |
| 53 | + randao_commitment = b'\x56' * 32 |
| 54 | + domain = get_domain( |
| 55 | + state.fork_data, |
| 56 | + state.slot, |
| 57 | + SignatureDomain.DOMAIN_DEPOSIT, |
| 58 | + ) |
| 59 | + |
| 60 | + deposit_input = make_deposit_input( |
| 61 | + pubkey=pubkey, |
| 62 | + withdrawal_credentials=withdrawal_credentials, |
| 63 | + randao_commitment=randao_commitment, |
| 64 | + ) |
| 65 | + if expected is True: |
| 66 | + proof_of_possession = sign_proof_of_possession(deposit_input, privkey, domain) |
| 67 | + |
| 68 | + assert validate_proof_of_possession( |
| 69 | + state=state, |
| 70 | + pubkey=pubkey, |
| 71 | + proof_of_possession=proof_of_possession, |
| 72 | + withdrawal_credentials=withdrawal_credentials, |
| 73 | + randao_commitment=randao_commitment, |
| 74 | + ) |
| 75 | + else: |
| 76 | + proof_of_possession = b'\x11' * 32 |
| 77 | + with pytest.raises(ValidationError): |
| 78 | + validate_proof_of_possession( |
| 79 | + state=state, |
| 80 | + pubkey=pubkey, |
| 81 | + proof_of_possession=proof_of_possession, |
| 82 | + withdrawal_credentials=withdrawal_credentials, |
| 83 | + randao_commitment=randao_commitment, |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def test_process_deposit(sample_beacon_state_params, |
| 88 | + zero_balance_validator_ttl, |
| 89 | + privkeys, |
| 90 | + pubkeys): |
| 91 | + state = BeaconState(**sample_beacon_state_params).copy( |
| 92 | + slot=zero_balance_validator_ttl + 1, |
| 93 | + validator_registry=(), |
| 94 | + ) |
| 95 | + |
| 96 | + privkey_1 = privkeys[0] |
| 97 | + pubkey_1 = pubkeys[0] |
| 98 | + deposit = 32 * denoms.gwei |
| 99 | + withdrawal_credentials = b'\x34' * 32 |
| 100 | + randao_commitment = b'\x56' * 32 |
| 101 | + domain = get_domain( |
| 102 | + state.fork_data, |
| 103 | + state.slot, |
| 104 | + SignatureDomain.DOMAIN_DEPOSIT, |
| 105 | + ) |
| 106 | + |
| 107 | + deposit_input = make_deposit_input( |
| 108 | + pubkey=pubkey_1, |
| 109 | + withdrawal_credentials=withdrawal_credentials, |
| 110 | + randao_commitment=randao_commitment, |
| 111 | + ) |
| 112 | + proof_of_possession = sign_proof_of_possession(deposit_input, privkey_1, domain) |
| 113 | + # Add the first validator |
| 114 | + result_state, index = process_deposit( |
| 115 | + state=state, |
| 116 | + pubkey=pubkey_1, |
| 117 | + deposit=deposit, |
| 118 | + proof_of_possession=proof_of_possession, |
| 119 | + withdrawal_credentials=withdrawal_credentials, |
| 120 | + randao_commitment=randao_commitment, |
| 121 | + zero_balance_validator_ttl=zero_balance_validator_ttl, |
| 122 | + ) |
| 123 | + |
| 124 | + assert len(result_state.validator_registry) == 1 |
| 125 | + index = 0 |
| 126 | + assert result_state.validator_registry[0].pubkey == pubkey_1 |
| 127 | + assert result_state.validator_registry[index].withdrawal_credentials == withdrawal_credentials |
| 128 | + assert result_state.validator_registry[index].randao_commitment == randao_commitment |
| 129 | + assert result_state.validator_registry[index].balance == deposit |
| 130 | + # test immutable |
| 131 | + assert len(state.validator_registry) == 0 |
| 132 | + |
| 133 | + # Add the second validator |
| 134 | + privkey_2 = privkeys[1] |
| 135 | + pubkey_2 = pubkeys[1] |
| 136 | + deposit_input = make_deposit_input( |
| 137 | + pubkey=pubkey_2, |
| 138 | + withdrawal_credentials=withdrawal_credentials, |
| 139 | + randao_commitment=randao_commitment, |
| 140 | + ) |
| 141 | + proof_of_possession = sign_proof_of_possession(deposit_input, privkey_2, domain) |
| 142 | + result_state, index = process_deposit( |
| 143 | + state=result_state, |
| 144 | + pubkey=pubkey_2, |
| 145 | + deposit=deposit, |
| 146 | + proof_of_possession=proof_of_possession, |
| 147 | + withdrawal_credentials=withdrawal_credentials, |
| 148 | + randao_commitment=randao_commitment, |
| 149 | + zero_balance_validator_ttl=zero_balance_validator_ttl, |
| 150 | + ) |
| 151 | + assert len(result_state.validator_registry) == 2 |
| 152 | + assert result_state.validator_registry[1].pubkey == pubkey_2 |
| 153 | + |
| 154 | + # Force the first validator exited |
| 155 | + result_state = result_state.copy( |
| 156 | + validator_registry=( |
| 157 | + result_state.validator_registry[0].copy( |
| 158 | + balance=0, |
| 159 | + latest_status_change_slot=0, |
| 160 | + ), |
| 161 | + result_state.validator_registry[1], |
| 162 | + ) |
| 163 | + ) |
| 164 | + |
| 165 | + # Add the third validator |
| 166 | + privkey_3 = privkeys[2] |
| 167 | + pubkey_3 = pubkeys[2] |
| 168 | + deposit_input = make_deposit_input( |
| 169 | + pubkey=pubkey_3, |
| 170 | + withdrawal_credentials=withdrawal_credentials, |
| 171 | + randao_commitment=randao_commitment, |
| 172 | + ) |
| 173 | + proof_of_possession = sign_proof_of_possession(deposit_input, privkey_3, domain) |
| 174 | + result_state, index = process_deposit( |
| 175 | + state=result_state, |
| 176 | + pubkey=pubkey_3, |
| 177 | + deposit=deposit, |
| 178 | + proof_of_possession=proof_of_possession, |
| 179 | + withdrawal_credentials=withdrawal_credentials, |
| 180 | + randao_commitment=randao_commitment, |
| 181 | + zero_balance_validator_ttl=zero_balance_validator_ttl, |
| 182 | + ) |
| 183 | + assert len(result_state.validator_registry) == 2 |
| 184 | + assert result_state.validator_registry[0].pubkey == pubkey_3 |
0 commit comments