Skip to content

Commit 9c0ada5

Browse files
committed
Add BeaconState.update_validator(validator_index, validator)
1 parent 8491513 commit 9c0ada5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

eth/beacon/types/states.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,13 @@ def num_validators(self) -> int:
157157
@property
158158
def num_crosslinks(self) -> int:
159159
return len(self.latest_crosslinks)
160+
161+
def update_validator(self,
162+
validator_index: int,
163+
validator: ValidatorRecord) -> 'BeaconState':
164+
with self.build_changeset() as state_changeset:
165+
validator_registry = list(state_changeset.validator_registry)
166+
validator_registry[validator_index] = validator
167+
state_changeset.validator_registry = tuple(validator_registry)
168+
self = state_changeset.commit()
169+
return self

tests/beacon/types/test_states.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,24 @@ def test_num_crosslink_records(expected,
9999
def test_hash(sample_beacon_state_params):
100100
state = BeaconState(**sample_beacon_state_params)
101101
assert state.root == hash_eth2(rlp.encode(state))
102+
103+
104+
def test_update_validator(sample_beacon_state_params, sample_validator_record_params, max_deposit):
105+
state = BeaconState(**sample_beacon_state_params).copy(
106+
validator_registry=[
107+
mock_validator_record(
108+
pubkey,
109+
max_deposit,
110+
)
111+
for pubkey in range(10)
112+
]
113+
)
114+
115+
new_pubkey = 100
116+
validator_index = 5
117+
validator = state.validator_registry[validator_index].copy(
118+
pubkey=new_pubkey,
119+
)
120+
result_state = state.update_validator(validator_index=validator_index, validator=validator)
121+
assert result_state.validator_registry[validator_index].pubkey == new_pubkey
122+
assert state.validator_registry[validator_index].pubkey != new_pubkey

0 commit comments

Comments
 (0)