From d25f536ec5b581e903b3420403acc502c7c104fd Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Sun, 30 Sep 2018 18:02:09 -0500 Subject: [PATCH] [integer-sqrt] add concrete implementation that meets the requirements --- specs/casper_sharding_v2.1.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/specs/casper_sharding_v2.1.md b/specs/casper_sharding_v2.1.md index eb6ce5ec3..7812d038d 100644 --- a/specs/casper_sharding_v2.1.md +++ b/specs/casper_sharding_v2.1.md @@ -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, ~D2/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`: