[integer-sqrt] add concrete implementation that meets the requirements

This commit is contained in:
Danny Ryan 2018-09-30 18:02:09 -05:00
parent 4cb9790ea8
commit d25f536ec5
1 changed files with 9 additions and 5 deletions

View File

@ -298,13 +298,17 @@ def get_block_hash(active_state, curblock, slot):
`get_block_hash(*, *, h)` should always return the block in the chain at slot `h`, and `get_shards_and_committees_for_slot(*, h)` should not change unless the dynasty changes.
Finally, we abstractly define `integer_sqrt` for use in reward/penalty calculations:
Finally, we define `int_sqrt` that for an input `n` returns `max(n in Z: n**2 <= x)`:
```python
def integer_sqrt(n):
return max(n in Z: n**2 <= x)
def int_sqrt(n):
k = n
while True:
newk = (k + (n // k)) // 2
if newk in (k, k+1): return k
k = newk
```
We leave `integer_sqrt` implementation details to each particular language/framework.
### On startup
@ -384,7 +388,7 @@ For all (`shard_id`, `shard_block_hash`) tuples, compute the total deposit size
Let `time_since_finality = block.slot_number - last_finalized_slot`, and let `B` be the balance of any given validator whose balance we are adjusting, not including any balance changes from this round of state recalculation. Let:
* `total_deposits = sum([v.balance for i, v in enumerate(validators) if i in get_active_validator_indices(validators, current_dynasty)])` and `total_deposits_in_ETH = total_deposits // 10**18`
* `reward_quotient = BASE_REWARD_QUOTIENT * integer_sqrt(total_deposits_in_ETH)` (1/this is the per-slot max interest rate)
* `reward_quotient = BASE_REWARD_QUOTIENT * int_sqrt(total_deposits_in_ETH)` (1/this is the per-slot max interest rate)
* `quadratic_penalty_quotient = (SQRT_E_DROP_TIME / SLOT_DURATION)**2` (after D slots, ~D<sup>2</sup>/2 divided by this is the portion lost by offline validators)
For each slot `S` in the range `last_state_recalc - CYCLE_LENGTH ... last_state_recalc - 1`: