Update to latest spec

This commit is contained in:
mratsim 2018-09-24 17:44:48 +02:00
parent 6ddab7e9c0
commit ca4082b316
3 changed files with 27 additions and 21 deletions

View File

@ -5,7 +5,7 @@
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import intsets, eth_common, math
import intsets, eth_common, math, stint
# Implementation based on WIP spec https://notes.ethereum.org/SCIg8AH5SA-O4C1G1LYZHQ?view
# ⚠ Spec is updated very often, implementation might quickly be outdated
@ -30,7 +30,7 @@ type
CrystallizedState* = object
validators*: seq[ValidatorRecord] # List of active validators
last_state_recalc*: int64 # Last CrystallizedState recalculation
indices_for_slots*: seq[seq[ShardAndCommittee]]
shard_and_committee_for_slots*: seq[seq[ShardAndCommittee]]
# What active validators are part of the attester set
# at what height, and in what shard. Starts at slot
# last_state_recalc - CYCLE_LENGTH
@ -38,9 +38,7 @@ type
justified_streak*: int16 # Number of consecutive justified slots ending at this one
last_finalized_slot*: int64 # The last finalized slot
current_dynasty*: int64 # The current dynasty
crosslinking_start_shard*: int16 # The next shard that cross-linking assignment will start from
crosslink_records*: seq[CrosslinkRecord] # Records about the most recent crosslink for each shard
total_deposits*: Int256 # Total balance of deposits
dynasty_seed*: Blake2_256_Digest # Used to select the committees for each shard
dynasty_seed_last_reset*: int64 # Last epoch the crosslink seed was reset
@ -53,12 +51,13 @@ type
withdrawal_shard*: int16 # What shard the validator's balance will be sent to after withdrawal
withdrawal_address*: EthAddress # And what address
randao_commitment*: Blake2_256_Digest # The validator's current RANDAO beacon commitment
balance*: int64 # Current balance
balance*: Int128 # Current balance
start_dynasty*: int64 # Dynasty where the validator is inducted
end_dynasty*: int64 # Dynasty where the validator leaves
CrosslinkRecord* = object
dynasty: int64 # What dynasty the crosslink was submitted in
slot: int64 # What slot
hash: Blake2_256_Digest # The block hash
BLSPublicKey* = object
@ -73,7 +72,10 @@ type
# are NOT part of the current chain, in order of oldest to newest
shard_block_hash*: Blake2_256_Digest # Block hash in the shard that we are attesting to
attester_bitfield*: IntSet # Who is participating
justified_slot*: int64
justified_block_hash: Blake2_256_Digest
aggregateSig*: seq[BLSPublicKey] # The actual signature
# Note:
# We use IntSet from Nim Standard library which are efficient sparse bitsets.
# See: https://nim-lang.org/docs/intsets.html
@ -90,9 +92,12 @@ type
const
SHARD_COUNT* = 1024 # a constant referring to the number of shards
DEPOSIT_SIZE* = 32 # You need to deposit 32 ETH to be a validator in Casper
MAX_VALIDATOR_COUNT* = 2^22 # 4_194_304, this means that ~132M ETH can stake at the same time (= MaxValidator Count * DepositSize)
SLOT_DURATION* = 8 # seconds
CYCLE_LENGTH* = 64 # slots
MIN_COMMITTEE_SIZE* = 128 # (rationale: see recommended minimum 111 here https://vitalik.ca/files/Ithaca201807_Sharding.pdf)
SHARD_COUNT* = 1024 # a constant referring to the number of shards
DEPOSIT_SIZE* = 32 # You need to deposit 32 ETH to be a validator in Casper
MAX_VALIDATOR_COUNT* = 2^22 # 4_194_304, this means that ~132M ETH can stake at the same time (= MaxValidator Count * DepositSize)
SLOT_DURATION* = 8 # seconds
CYCLE_LENGTH* = 64 # slots
MIN_DYNASTY_LENGTH* = 256 # slots
MIN_COMMITTEE_SIZE* = 128 # (rationale: see recommended minimum 111 here https://vitalik.ca/files/Ithaca201807_Sharding.pdf)
SQRT_E_DROP_TIME* = 2^20 # a constant set to reflect the amount of time it will take for the quadratic leak to cut nonparticipating validators deposits by ~39.4%. Currently set to 2**20 seconds (~12 days).
BASE_REWARD_QUOTIENT* = 2^15 # this is the per-slot interest rate assuming all validators are participating, assuming total deposits of 1 ETH. Currently set to 2**15 = 32768, corresponding to ~3.88% annual interest assuming 10 million participating ETH.

View File

@ -67,26 +67,27 @@ func get_new_shuffling*(seed: Blake2_256_Digest, validators: seq[ValidatorRecord
slots_per_committee *= 2
result = @[]
for slot, height_indices in shuffle(avs, seed).split(CYCLE_LENGTH):
let shard_indices = height_indices.split(committees_per_slot)
for slot, slot_indices in shuffle(avs, seed).split(CYCLE_LENGTH):
let shard_indices = slot_indices.split(committees_per_slot)
let shard_id_start = crosslinking_start_shard +
slot.int16 * committees_per_slot div slots_per_committee
var committees = newSeq[ShardAndCommittee](shard_indices.len)
for j, indices in shard_indices:
committees[j].shard_id = crosslinking_start_shard +
slot.int16 * committees_per_slot div slots_per_committee + j.int16
committees[j].shard_id = (shard_id_start + j.int16) mod SHARD_COUNT
committees[j].committee = indices
result.add committees
func get_indices_for_slot*(crystallized_state: CrystallizedState,
slot: int64): seq[ShardAndCommittee] {.noInit.}=
func get_shards_and_committees_for_slot*(crystallized_state: CrystallizedState,
slot: int64): seq[ShardAndCommittee] =
# TODO: Spec why is active_state an argument?
let start = crystallized_state.last_state_recalc - CYCLE_LENGTH
assert start <= slot
assert slot < start + CYCLE_LENGTH * 2
result = crystallized_state.indices_for_slots[int slot - start]
result = crystallized_state.shard_and_committee_for_slots[int slot - start]
# TODO, slot is an int64 will be an issue on int32 arch.
# Clarify with EF if light clients will need the beacon chain

View File

@ -20,7 +20,7 @@
{.warning: "The official spec at https://notes.ethereum.org/SCIg8AH5SA-O4C1G1LYZHQ# is not fully defining state transitions.".}
import ./datatypes, ./private/helpers
import ./datatypes, ./private/helpers, intsets
func process_block(active_state: ActiveState, crystallized_state: CrystallizedState, blck: BeaconBlock, slot: int64) =
@ -38,9 +38,9 @@ func process_block(active_state: ActiveState, crystallized_state: CrystallizedSt
val = get_block_hash(active_state, blck, slot - CYCLE_LENGTH + idx)
parent_hashes.add attestation.oblique_parent_hashes
# Let attestation_indices be get_indices_for_slot(crystallized_state, slot)[x], choosing x so that attestation_indices.shard_id equals the shard_id value provided to find the set of validators that is creating this attestation record.
# Let attestation_indices be get_shards_and_committees_for_slot(crystallized_state, slot)[x], choosing x so that attestation_indices.shard_id equals the shard_id value provided to find the set of validators that is creating this attestation record.
let attestation_indices = block:
let shard_and_committees = get_indices_for_slot(crystallized_state, slot)
let shard_and_committees = get_shards_and_committees_for_slot(crystallized_state, slot)
var
x = 1
record_creator = shard_and_committees[0]