first part of spec updating; some constants, structure changes, data type changes from int to uint (#9)

* first part of spec updating; some constants, structure changes, data type changes from int to uint

* Update beacon_chain/datatypes.nim

Co-Authored-By: tersec <tersec@users.noreply.github.com>
This commit is contained in:
tersec 2018-11-09 09:34:30 +00:00 committed by Mamy Ratsimbazafy
parent b63d104e00
commit f389c454d6
4 changed files with 68 additions and 24 deletions

View File

@ -5,6 +5,9 @@
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). # * 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. # at your option. This file may not be copied, modified, or distributed except according to those terms.
# In process of being updated as of spec from 2018-11-05
# https://github.com/ethereum/eth2.0-specs/tree/59f32978d489020770ae50e6d45450103445c6ad
import import
intsets, eth_common, math, stint intsets, eth_common, math, stint
@ -26,14 +29,22 @@ type
Blake2_256_Digest* = Hash256 # TODO change to Blake2b-512[0 ..< 32] see https://github.com/status-im/nim-beacon-chain/issues/3 Blake2_256_Digest* = Hash256 # TODO change to Blake2b-512[0 ..< 32] see https://github.com/status-im/nim-beacon-chain/issues/3
Uint24* = range[0'u32 .. 0xFFFFFF'u32] # TODO: wrap-around Uint24* = range[0'u32 .. 0xFFFFFF'u32] # TODO: wrap-around
SpecialRecord* = object
kind*: SpecialRecordTypes # Kind
data*: seq[byte] # Data
BeaconBlock* = object BeaconBlock* = object
parent_hash*: Blake2_256_Digest # Hash of the parent block slot*: uint64 # Slot number
slot_number*: int64 # Slot number (for the PoS mechanism) randao_reveal*: Blake2_256_Digest # Proposer RANDAO reveal
randao_reveal*: Blake2_256_Digest # Randao commitment reveal pow_chain_reference*: Blake2_256_Digest # Recent PoW chain reference (block hash)
attestations*: seq[AttestationRecord] # Attestation votes ancestor_hashes*: seq[Blake2_256_Digest] # Skip list of previous beacon block hashes
pow_chain_ref*: Blake2_256_Digest # Reference to main chain block # i'th item is most recent ancestor whose
# slot is a multiple of 2**i for
# i == 0, ..., 31
active_state_root*: Blake2_256_Digest # Hash of the active state active_state_root*: Blake2_256_Digest # Hash of the active state
crystallized_state_root*: Blake2_256_Digest # Hash of the crystallized state crystallized_state_root*: Blake2_256_Digest # Hash of the crystallized state
attestations*: seq[AttestationRecord] # Attestation votes
parent_hash*: Blake2_256_Digest # Hash of the parent block
ActiveState* = object ActiveState* = object
pending_attestations*: seq[AttestationRecord] # Attestations that have not yet been processed pending_attestations*: seq[AttestationRecord] # Attestations that have not yet been processed
@ -41,7 +52,7 @@ type
CrystallizedState* = object CrystallizedState* = object
validators*: seq[ValidatorRecord] # List of active validators validators*: seq[ValidatorRecord] # List of active validators
last_state_recalc*: int64 # Last CrystallizedState recalculation last_state_recalc*: uint64 # Last CrystallizedState recalculation
shard_and_committee_for_slots*: seq[seq[ShardAndCommittee]] shard_and_committee_for_slots*: seq[seq[ShardAndCommittee]]
# What active validators are part of the attester set # What active validators are part of the attester set
# at what height, and in what shard. Starts at slot # at what height, and in what shard. Starts at slot
@ -84,6 +95,23 @@ type
justified_block_hash: Blake2_256_Digest justified_block_hash: Blake2_256_Digest
aggregate_sig*: BLSaggregateSig # The actual signature aggregate_sig*: BLSaggregateSig # The actual signature
ValidatorStatusCodes* {.pure.} = enum
PendingActivation = 0
Active = 1
PendingExit = 2
PendingWithdraw = 3
Withdrawn = 4
Penalized = 127
SpecialRecordTypes* {.pure.} = enum
Logout = 0
CasperSlashing = 1
RandaoChange = 2
ValidatorSetDeltaFlags* {.pure.} = enum
Entry = 0
Exit = 1
# Note: # Note:
# We use IntSet from Nim Standard library which are efficient sparse bitsets. # We use IntSet from Nim Standard library which are efficient sparse bitsets.
# See: https://nim-lang.org/docs/intsets.html # See: https://nim-lang.org/docs/intsets.html
@ -100,12 +128,28 @@ type
const const
SHARD_COUNT* = 1024 # a constant referring to the number of shards 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 DEPOSIT_SIZE* = 2^5 # 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* = 16 # seconds
SLOT_DURATION* = 8 # seconds CYCLE_LENGTH* = 64 # slots
CYCLE_LENGTH* = 64 # slots MIN_COMMITTEE_SIZE* = 2^7 # validators; 2018-11-05 version of spec also says:
MIN_DYNASTY_LENGTH* = 256 # slots # See a recommended `MIN_COMMITTEE_SIZE` of 111 here
MIN_COMMITTEE_SIZE* = 128 # (rationale: see recommended minimum 111 here https://vitalik.ca/files/Ithaca201807_Sharding.pdf) # 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). SQRT_E_DROP_TIME* = 2^16 # slots (~12 days); amount of time it takes for the
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. # quadratic leak to cut deposits of non-participating
# validators by ~39.4%
BASE_REWARD_QUOTIENT* = 2^15 # per-slot interest rate assuming all validators are
# participating, assuming total deposits of 1 ETH. It
# corresponds to ~3.88% annual interest assuming 10
# million participating ETH.
MIN_BALANCE* = 2^4 # ETH
MIN_ONLINE_DEPOSIT_SIZE* = 2^4 # ETH
GWEI_PER_ETH* = 10^9 # Gwei/ETH
MIN_VALIDATOR_SET_CHANGE_INTERVAL* = 2^8 # slots (~1.1 hours)
RANDAO_SLOTS_PER_LAYER* = 2^12 # slots (~18 hours)
WITHDRAWAL_PERIOD* = 2^19 # slots (~97 days)
SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD* = 2^16 # slots (~12 days)
MAX_VALIDATOR_CHURN_QUOTIENT* = 2^5 # At most `1/MAX_VALIDATOR_CHURN_QUOTIENT` of the
# validators can change during each validator set
# change.
INITIAL_FORK_VERSION* = 0 # currently behaves like a constant

View File

@ -80,7 +80,7 @@ func get_new_shuffling*(seed: Blake2_256_Digest, validators: seq[ValidatorRecord
result.add committees result.add committees
func get_shards_and_committees_for_slot*(crystallized_state: CrystallizedState, func get_shards_and_committees_for_slot*(crystallized_state: CrystallizedState,
slot: int64): seq[ShardAndCommittee] = slot: uint64): seq[ShardAndCommittee] =
# TODO: Spec why is active_state an argument? # TODO: Spec why is active_state an argument?
let start = crystallized_state.last_state_recalc - CYCLE_LENGTH let start = crystallized_state.last_state_recalc - CYCLE_LENGTH
@ -88,13 +88,13 @@ func get_shards_and_committees_for_slot*(crystallized_state: CrystallizedState,
assert slot < start + CYCLE_LENGTH * 2 assert slot < start + CYCLE_LENGTH * 2
result = crystallized_state.shard_and_committee_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. # TODO, slot is a uint64; will be an issue on int32 arch.
# Clarify with EF if light clients will need the beacon chain # Clarify with EF if light clients will need the beacon chain
func get_block_hash*(active_state: ActiveState, func get_block_hash*(active_state: ActiveState,
beacon_block: BeaconBlock, slot: int64): Blake2_256_Digest = beacon_block: BeaconBlock, slot: uint64): Blake2_256_Digest =
let sback = beacon_block.slot_number - CYCLE_LENGTH * 2 let sback = beacon_block.slot - CYCLE_LENGTH * 2
assert sback <= slot assert sback <= slot
assert slot < sback + CYCLE_LENGTH * 2 assert slot < sback + CYCLE_LENGTH * 2

View File

@ -26,21 +26,21 @@ import
milagro_crypto # nimble install https://github.com/status-im/nim-milagro-crypto@#master milagro_crypto # nimble install https://github.com/status-im/nim-milagro-crypto@#master
func process_block*(active_state: ActiveState, crystallized_state: CrystallizedState, blck: BeaconBlock, slot: int64) = func process_block*(active_state: ActiveState, crystallized_state: CrystallizedState, blck: BeaconBlock, slot: uint64) =
# TODO: unfinished spec # TODO: unfinished spec
for attestation in blck.attestations: for attestation in blck.attestations:
## Spec changes: Verify that slot <= parent.slot_number and slot >= max(parent.slot_number - CYCLE_LENGTH + 1, 0) ## Spec changes: Verify that slot <= parent.slot_number and slot >= max(parent.slot_number - CYCLE_LENGTH + 1, 0)
## (Outdated) Verify that slot < block.slot_number and slot >= max(block.slot_number - CYCLE_LENGTH, 0) ## (Outdated) Verify that slot < block.slot_number and slot >= max(block.slot_number - CYCLE_LENGTH, 0)
doAssert slot < blck.slot_number doAssert slot < blck.slot
doAssert slot >= max(blck.slot_number - CYCLE_LENGTH, 0) doAssert slot >= max(blck.slot - CYCLE_LENGTH, 0)
# Compute parent_hashes = [get_block_hash(active_state, block, slot - CYCLE_LENGTH + i) # Compute parent_hashes = [get_block_hash(active_state, block, slot - CYCLE_LENGTH + i)
# for i in range(1, CYCLE_LENGTH - len(oblique_parent_hashes) + 1)] + oblique_parent_hashes # for i in range(1, CYCLE_LENGTH - len(oblique_parent_hashes) + 1)] + oblique_parent_hashes
# TODO - don't allocate in tight loop # TODO - don't allocate in tight loop
var parent_hashes = newSeq[Blake2_256_Digest](CYCLE_LENGTH - attestation.oblique_parent_hashes.len) var parent_hashes = newSeq[Blake2_256_Digest](CYCLE_LENGTH - attestation.oblique_parent_hashes.len)
for idx, val in parent_hashes.mpairs: for idx, val in parent_hashes.mpairs:
val = get_block_hash(active_state, blck, slot - CYCLE_LENGTH + idx + 1) val = get_block_hash(active_state, blck, slot - CYCLE_LENGTH + cast[uint64](idx) + 1)
parent_hashes.add attestation.oblique_parent_hashes parent_hashes.add attestation.oblique_parent_hashes
# 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 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.

View File

@ -16,6 +16,6 @@ suite "Block processing":
let actState = ActiveState() let actState = ActiveState()
let crystState = CrystallizedState() let crystState = CrystallizedState()
let blck = BeaconBlock() let blck = BeaconBlock()
let slot = 10 let slot = 10'u
actState.process_block(crystState, blck, slot) actState.process_block(crystState, blck, slot)