1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Ethereum-Altair-parity per-validator base reward formula
//! (DSL-081) and downstream flag-delta math (DSL-082..086).
//!
//! Traces to: [SPEC §8.3](../../../docs/resources/SPEC.md),
//! catalogue rows
//! [DSL-081..086](../../../docs/requirements/domains/participation/specs/).
//!
//! # Scope (incremental)
//!
//! First commit lands `base_reward` (DSL-081). Later DSLs add:
//!
//! - DSL-082: `compute_flag_deltas` reward on hit
//! - DSL-083: penalty on miss (source + target; head exempt)
//! - DSL-084: inactivity-leak bias
//! - DSL-085: proposer-reward slice
//! - DSL-086: epoch-boundary `apply_deltas`
use ;
use crate;
use crateParticipationTracker;
use crateEffectiveBalanceView;
/// Ethereum Altair base reward per validator per epoch.
///
/// Implements [DSL-081](../../../docs/requirements/domains/participation/specs/DSL-081.md).
/// Traces to SPEC §8.3.
///
/// # Formula
///
/// ```text
/// base_reward = effective_balance * BASE_REWARD_FACTOR / isqrt(total_active_balance)
/// ```
///
/// Scales INVERSELY with the integer sqrt of total active
/// stake — smaller networks pay more per validator; larger
/// networks dilute the reward. Matches Ethereum mainnet
/// `get_base_reward` at parity.
///
/// # Overflow + saturation
///
/// Intermediate multiplication runs in `u128` to avoid overflow
/// on realistic stake sizes (32e9 mojos × 64 = 2^47, well within
/// u64 — but the generalised formula is `total_balance × factor`
/// which can push into the 2^70+ range at giant network scale).
/// Division truncates toward zero; `saturating_as_u64` caps the
/// return at `u64::MAX` for any extreme input.
///
/// # Zero guard
///
/// `total_active_balance == 0` → `isqrt == 0` → division would
/// panic. Guard returns 0: at network boot (no stakers), the
/// reward is genuinely undefined, and 0 is the least-surprising
/// value for consumers.
///
/// # Why u128 isqrt
///
/// `u128::isqrt` is stable since Rust 1.84 and produces the
/// exact integer sqrt without any dep. The SPEC pseudo-code uses
/// `num_integer::Roots::sqrt`; our stdlib form is equivalent and
/// avoids a crate pull for one call site.
/// Per-validator reward + penalty pair produced by
/// `compute_flag_deltas` at epoch boundary.
///
/// Traces to [SPEC §8.3](../../../docs/resources/SPEC.md),
/// catalogue row [DSL-082](../../../docs/requirements/domains/participation/specs/DSL-082.md).
///
/// Consumers apply `reward` as a credit and `penalty` as a debit
/// via `ValidatorEntry::credit_stake` / `slash_absolute`
/// respectively at the epoch boundary (DSL-086).
/// Compute per-validator reward / penalty deltas for the
/// just-finished epoch (the one now in `tracker.previous_epoch`).
///
/// Implements DSL-082 (reward-on-hit) + DSL-083 (penalty-on-
/// miss, HEAD-exempt) + DSL-084 (stall zeroes rewards).
///
/// # Reward weights (DSL-082)
///
/// ```text
/// SOURCE hit → reward += base * TIMELY_SOURCE_WEIGHT / WEIGHT_DENOMINATOR
/// TARGET hit → reward += base * TIMELY_TARGET_WEIGHT / WEIGHT_DENOMINATOR
/// HEAD hit → reward += base * TIMELY_HEAD_WEIGHT / WEIGHT_DENOMINATOR
/// ```
///
/// Mainnet weights: 14 / 26 / 14 (sum 54 / 64; the 2 unassigned
/// weights represent the unused sync-committee slot).
///
/// # Iteration
///
/// Iterates `0..tracker.validator_count()` in index order. One
/// `FlagDelta` per slot — even for validators with all-zero
/// flags, the delta IS emitted (with `reward == penalty == 0`)
/// so downstream apply code sees a deterministic per-validator
/// record count.
/// Proposer inclusion bonus for a single included attestation.
///
/// Implements [DSL-085](../../../docs/requirements/domains/participation/specs/DSL-085.md).
/// Traces to SPEC §8.4.
///
/// # Formula
///
/// ```text
/// proposer_reward = attester_base_reward
/// * PROPOSER_WEIGHT
/// / (WEIGHT_DENOMINATOR - PROPOSER_WEIGHT)
/// = attester_base * 8 / 56
/// ```
///
/// Paid to the block proposer for each attestation they are
/// first to include. Small fraction of the attester's base
/// reward — the validator who signed earns the lion's share
/// via `compute_flag_deltas` (DSL-082).
///
/// # Saturating arithmetic
///
/// Multiplication uses `saturating_mul` so extreme inputs
/// (validator with astronomical effective balance × max factor)
/// do not panic. Division is exact integer truncation.
///
/// # Denominator
///
/// `WEIGHT_DENOMINATOR - PROPOSER_WEIGHT = 64 - 8 = 56` is the
/// fixed reference denominator. Non-zero by construction (since
/// `PROPOSER_WEIGHT = 8 < WEIGHT_DENOMINATOR = 64`).