From deaddc1fc0bdd70f9521f58e9b4017a3f32cb8bd Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Tue, 6 Oct 2020 18:32:17 +0300 Subject: [PATCH] Address review comments --- .../block_pools/block_pools_types.nim | 3 +++ beacon_chain/block_pools/chain_dag.nim | 21 ++++++++++++------- beacon_chain/conf.nim | 17 +++++++++------ beacon_chain/spec/weak_subjectivity.nim | 17 +-------------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/beacon_chain/block_pools/block_pools_types.nim b/beacon_chain/block_pools/block_pools_types.nim index d174002a1..b9c4681fe 100644 --- a/beacon_chain/block_pools/block_pools_types.nim +++ b/beacon_chain/block_pools/block_pools_types.nim @@ -98,6 +98,9 @@ type ## Directed acyclic graph of blocks pointing back to a finalized block on the chain we're ## interested in - we call that block the tail + genesis*: BlockRef ##\ + ## The genesis block of the network + tail*: BlockRef ##\ ## The earliest finalized block we know about diff --git a/beacon_chain/block_pools/chain_dag.nim b/beacon_chain/block_pools/chain_dag.nim index 7d2aa1539..48bbe7e73 100644 --- a/beacon_chain/block_pools/chain_dag.nim +++ b/beacon_chain/block_pools/chain_dag.nim @@ -300,10 +300,22 @@ proc init*(T: type ChainDAGRef, tailRef = BlockRef.init(tailRoot, tailBlock.message) headRoot = headBlockRoot.get() + let genesisRef = if tailBlock.message.slot == GENESIS_SLOT: + tailRef + else: + let + genesisBlockRoot = db.getGenesisBlockRoot() + genesisBlock = db.getBlock(genesisBlockRoot).expect( + "preInit should have initialized the database with a genesis block") + BlockRef.init(genesisBlockRoot, genesisBlock.message) + var blocks = {tailRef.root: tailRef}.toTable() headRef: BlockRef + if genesisRef != tailRef: + blocks[genesisRef.root] = genesisRef + if headRoot != tailRoot: var curRef: BlockRef @@ -363,6 +375,7 @@ proc init*(T: type ChainDAGRef, blocks: blocks, tail: tailRef, head: headRef, + genesis: genesisRef, db: db, heads: @[headRef], headState: tmpState[], @@ -892,13 +905,7 @@ proc setTailState*(dag: ChainDAGRef, discard proc getGenesisBlockData*(dag: ChainDAGRef): BlockData = - let - genesisBlockRoot = dag.db.getGenesisBlockRoot() - # The database should be seeded with the genesis block root by `preInit`: - genesisBlock = dag.db.getBlock(genesisBlockRoot).get() - - BlockData(data: genesisBlock, - refs: BlockRef(root: genesisBlockRoot, slot: GENESIS_SLOT)) + dag.get(dag.genesis) proc getGenesisBlockSlot*(dag: ChainDAGRef): BlockSlot = let blockData = dag.getGenesisBlockData() diff --git a/beacon_chain/conf.nim b/beacon_chain/conf.nim index 6e5f5241a..475997a95 100644 --- a/beacon_chain/conf.nim +++ b/beacon_chain/conf.nim @@ -1,12 +1,12 @@ {.push raises: [Defect].} import - os, options, unicode, + strutils, os, options, unicode, chronicles, chronicles/options as chroniclesOptions, confutils, confutils/defs, confutils/std/net, stew/shims/net as stewNet, stew/io2, unicodedb/properties, normalize, json_serialization, web3/[ethtypes, confutils_defs], - spec/[crypto, keystore, digest, datatypes, network, weak_subjectivity], + spec/[crypto, keystore, digest, datatypes, network], network_metadata export @@ -144,7 +144,7 @@ type weakSubjectivityCheckpoint* {. desc: "Weak subjectivity checkpoint in the format block_root:epoch_number" - name: "weak-subjectivity-checkpoint" }: Option[WeakSubjectivityCheckpoint] + name: "weak-subjectivity-checkpoint" }: Option[Checkpoint] finalizedCheckpointState* {. desc: "SSZ file specifying a recent finalized state" @@ -463,11 +463,16 @@ func parseCmdArg*(T: type GraffitiBytes, input: TaintedString): T func completeCmdArg*(T: type GraffitiBytes, input: TaintedString): seq[string] = return @[] -func parseCmdArg*(T: type WeakSubjectivityCheckpoint, input: TaintedString): T +func parseCmdArg*(T: type Checkpoint, input: TaintedString): T {.raises: [ValueError, Defect].} = - init(T, input) + let sepIdx = find(input.string, ':') + if sepIdx == -1: + raise newException(ValueError, + "The weak subjectivity checkpoint must be provided in the `block_root:epoch_number` format") + T(root: Eth2Digest.fromHex(input[0 ..< sepIdx]), + epoch: parseBiggestUInt(input[sepIdx .. ^1]).Epoch) -func completeCmdArg*(T: type WeakSubjectivityCheckpoint, input: TaintedString): seq[string] = +func completeCmdArg*(T: type Checkpoint, input: TaintedString): seq[string] = return @[] proc isPrintable(rune: Rune): bool = diff --git a/beacon_chain/spec/weak_subjectivity.nim b/beacon_chain/spec/weak_subjectivity.nim index c8fc8498b..95c01b3d3 100644 --- a/beacon_chain/spec/weak_subjectivity.nim +++ b/beacon_chain/spec/weak_subjectivity.nim @@ -1,5 +1,4 @@ import - strutils, datatypes, digest, helpers {.push raises: [Defect].} @@ -7,20 +6,6 @@ import const SAFETY_DECAY* = 10'u64 -type - WeakSubjectivityCheckpoint* = object - root*: Eth2Digest - epoch*: Epoch - -func init*(T: type WeakSubjectivityCheckpoint, str: TaintedString): T - {.raises: [ValueError, Defect].} = - let sepIdx = find(str.string, ':') - if sepIdx == -1: - raise newException(ValueError, - "The weak subjectivity checkpoint must be provided in the `block_root:epoch_number` format") - T(root: Eth2Digest.fromHex(str[0 ..< sepIdx]), - epoch: parseBiggestUInt(str[sepIdx .. ^1]).Epoch) - # https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/weak-subjectivity.md#calculating-the-weak-subjectivity-period func compute_weak_subjectivity_period*(state: BeaconState): uint64 = var weak_subjectivity_period = MIN_VALIDATOR_WITHDRAWABILITY_DELAY @@ -34,7 +19,7 @@ func compute_weak_subjectivity_period*(state: BeaconState): uint64 = # https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/weak-subjectivity.md#checking-for-stale-weak-subjectivity-checkpoint func is_within_weak_subjectivity_period*(current_slot: Slot, ws_state: BeaconState, - ws_checkpoint: WeakSubjectivityCheckpoint): bool = + ws_checkpoint: Checkpoint): bool = # Clients may choose to validate the input state against the input Weak Subjectivity Checkpoint doAssert ws_state.latest_block_header.state_root == ws_checkpoint.root doAssert compute_epoch_at_slot(ws_state.slot) == ws_checkpoint.epoch