Skip to content

Commit e38ed84

Browse files
committed
Use spec.PendingPartialWithdrawals(data=entries)
1 parent fca54a0 commit e38ed84

1 file changed

Lines changed: 53 additions & 30 deletions

File tree

  • tests/generators/compliance_runners/state_transition/withdrawal_request

tests/generators/compliance_runners/state_transition/withdrawal_request/materializer.py

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,30 @@
1818

1919
NUM_VALIDATORS = 64
2020
TARGET_INDEX = 0
21-
ABSENT_PUBKEY = pubkeys[NUM_VALIDATORS] # not in a NUM_VALIDATORS-validator genesis
22-
CURRENT_EPOCH = 70 # > SHARD_COMMITTEE_PERIOD (64), for old-enough headroom
21+
ABSENT_PUBKEY = pubkeys[NUM_VALIDATORS] # not in a NUM_VALIDATORS-validator genesis
22+
CURRENT_EPOCH = 70 # > SHARD_COMMITTEE_PERIOD (64), for old-enough headroom
2323
ADDRESS = b"\x22" * 20
2424
OTHER_ADDRESS = b"\x33" * 20
25-
PARTIAL_AMOUNT = 10 ** 9
25+
PARTIAL_AMOUNT = 10**9
2626

2727
_PREFIX = {"CRED_BLS": b"\x00", "CRED_ETH1": b"\x01", "CRED_COMPOUNDING": b"\x02"}
2828

2929
_DIMS = [
30-
"is_full_exit_request", "partial_queue_full",
31-
"validator_pubkey_found", "validator_credential", "source_address_matches",
32-
"validator_active", "validator_exiting", "validator_old_enough",
33-
"has_pending_partial_withdrawal", "sufficient_effective_balance", "has_excess_balance",
34-
"validator_has_execution_credential", "validator_has_compounding_credential",
35-
"outcome", "withdrawal_effected",
30+
"is_full_exit_request",
31+
"partial_queue_full",
32+
"validator_pubkey_found",
33+
"validator_credential",
34+
"source_address_matches",
35+
"validator_active",
36+
"validator_exiting",
37+
"validator_old_enough",
38+
"has_pending_partial_withdrawal",
39+
"sufficient_effective_balance",
40+
"has_excess_balance",
41+
"validator_has_execution_credential",
42+
"validator_has_compounding_credential",
43+
"outcome",
44+
"withdrawal_effected",
3645
]
3746

3847

@@ -51,7 +60,8 @@ class WithdrawalRequestMaterializer(Materializer):
5160
def _base_state(self) -> Any:
5261
spec = self.spec
5362
state = create_genesis_state(
54-
spec, validator_balances=[spec.MAX_EFFECTIVE_BALANCE] * NUM_VALIDATORS,
63+
spec,
64+
validator_balances=[spec.MAX_EFFECTIVE_BALANCE] * NUM_VALIDATORS,
5565
activation_threshold=spec.MAX_EFFECTIVE_BALANCE,
5666
)
5767
state.slot = spec.Slot(CURRENT_EPOCH * spec.SLOTS_PER_EPOCH)
@@ -61,15 +71,14 @@ def _epochs(self, active: bool, exiting: bool, old_enough: bool) -> tuple[int, i
6171
"""(activation_epoch, exit_epoch) realizing the lifecycle triple at CURRENT_EPOCH."""
6272
spec = self.spec
6373
far = int(spec.FAR_FUTURE_EPOCH)
64-
activation = 0 if old_enough else CURRENT_EPOCH - 10 # <= C-64 vs in (C-64, C]
74+
activation = 0 if old_enough else CURRENT_EPOCH - 10 # <= C-64 vs in (C-64, C]
6575
if active:
66-
exit_epoch = (CURRENT_EPOCH + 10) if exiting else far # future exit still active
76+
exit_epoch = (CURRENT_EPOCH + 10) if exiting else far # future exit still active
77+
elif exiting:
78+
exit_epoch = CURRENT_EPOCH - 1 # exited (epoch >= exit)
6779
else:
68-
if exiting:
69-
exit_epoch = CURRENT_EPOCH - 1 # exited (epoch >= exit)
70-
else:
71-
activation = CURRENT_EPOCH + 10 # not yet activated
72-
exit_epoch = far
80+
activation = CURRENT_EPOCH + 10 # not yet activated
81+
exit_epoch = far
7382
return activation, exit_epoch
7483

7584
def materialize_solution(self, sol: Any) -> tuple[dict, list[TestCasePart]]:
@@ -93,7 +102,8 @@ def materialize_solution(self, sol: Any) -> tuple[dict, list[TestCasePart]]:
93102
v.activation_epoch = spec.Epoch(activation)
94103
v.exit_epoch = spec.Epoch(exit_epoch)
95104
v.effective_balance = spec.Gwei(
96-
spec.MIN_ACTIVATION_BALANCE if _s(sol, "sufficient_effective_balance") == "T"
105+
spec.MIN_ACTIVATION_BALANCE
106+
if _s(sol, "sufficient_effective_balance") == "T"
97107
else spec.MIN_ACTIVATION_BALANCE - 1
98108
)
99109

@@ -102,18 +112,24 @@ def materialize_solution(self, sol: Any) -> tuple[dict, list[TestCasePart]]:
102112
pending_for_target = found and _s(sol, "has_pending_partial_withdrawal") == "T"
103113
entries = []
104114
if pending_for_target:
105-
entries.append(spec.PendingPartialWithdrawal(
106-
validator_index=spec.ValidatorIndex(TARGET_INDEX), amount=spec.Gwei(1),
107-
withdrawable_epoch=spec.Epoch(CURRENT_EPOCH),
108-
))
115+
entries.append(
116+
spec.PendingPartialWithdrawal(
117+
validator_index=spec.ValidatorIndex(TARGET_INDEX),
118+
amount=spec.Gwei(1),
119+
withdrawable_epoch=spec.Epoch(CURRENT_EPOCH),
120+
)
121+
)
109122
if _b(sol, "partial_queue_full"):
110123
filler_index = spec.ValidatorIndex(1)
111124
while len(entries) < int(spec.PENDING_PARTIAL_WITHDRAWALS_LIMIT):
112-
entries.append(spec.PendingPartialWithdrawal(
113-
validator_index=filler_index, amount=spec.Gwei(1),
114-
withdrawable_epoch=spec.Epoch(CURRENT_EPOCH),
115-
))
116-
pre.pending_partial_withdrawals = type(pre.pending_partial_withdrawals)(*entries)
125+
entries.append(
126+
spec.PendingPartialWithdrawal(
127+
validator_index=filler_index,
128+
amount=spec.Gwei(1),
129+
withdrawable_epoch=spec.Epoch(CURRENT_EPOCH),
130+
)
131+
)
132+
pre.pending_partial_withdrawals = spec.PendingPartialWithdrawals(data=entries)
117133

118134
if found:
119135
pending_amount = 1 if pending_for_target else 0
@@ -125,15 +141,22 @@ def materialize_solution(self, sol: Any) -> tuple[dict, list[TestCasePart]]:
125141

126142
request = spec.WithdrawalRequest(
127143
source_address=spec.ExecutionAddress(source_address),
128-
validator_pubkey=spec.BLSPubkey(pre.validators[TARGET_INDEX].pubkey if found else ABSENT_PUBKEY),
144+
validator_pubkey=spec.BLSPubkey(
145+
pre.validators[TARGET_INDEX].pubkey if found else ABSENT_PUBKEY
146+
),
129147
amount=spec.Gwei(0) if is_full else spec.Gwei(PARTIAL_AMOUNT),
130148
)
131149

132150
post = pre.copy()
133151
spec.process_withdrawal_request(post, request) # never raises
134152

135-
claimed = {n: (_b(sol, n) if isinstance(getattr(sol, n), bool) else _s(sol, n)) for n in _DIMS}
136-
meta = {"description": f"process_withdrawal_request: {claimed['outcome']}", "claimed": claimed}
153+
claimed = {
154+
n: (_b(sol, n) if isinstance(getattr(sol, n), bool) else _s(sol, n)) for n in _DIMS
155+
}
156+
meta = {
157+
"description": f"process_withdrawal_request: {claimed['outcome']}",
158+
"claimed": claimed,
159+
}
137160
parts = [
138161
("pre", "ssz", pre.encode_bytes()),
139162
("withdrawal_request", "ssz", request.encode_bytes()),

0 commit comments

Comments
 (0)