fork: push defect (#888)

This commit is contained in:
Jacek Sieka 2020-04-15 11:21:22 +02:00 committed by GitHub
parent 46ebe9b0aa
commit 8eafa6e094
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 18 deletions

View File

@ -5,6 +5,8 @@
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
{.push raises: [Defect].}
import
# Standard library
std/tables, std/options, std/typetraits,
@ -34,7 +36,7 @@ func compute_deltas(
votes: var openArray[VoteTracker],
old_balances: openarray[Gwei],
new_balances: openarray[Gwei]
): ForkChoiceError {.raises: [Defect].}
): ForkChoiceError
# TODO: raises [Defect] - once https://github.com/nim-lang/Nim/issues/12862 is fixed
# https://github.com/status-im/nim-beacon-chain/pull/865#pullrequestreview-389117232
@ -51,7 +53,7 @@ func initForkChoice*(
justified_epoch: Epoch,
finalized_epoch: Epoch,
finalized_root: Eth2Digest
): Result[ForkChoice, string] {.raises: [Defect].} =
): Result[ForkChoice, string] =
## Initialize a fork choice context
var proto_array = ProtoArray(
prune_threshold: DefaultPruneThreshold,
@ -72,7 +74,7 @@ func initForkChoice*(
return err("Failed to add finalized block to proto_array: " & $err)
return ok(ForkChoice(proto_array: proto_array))
func extend[T](s: var seq[T], minLen: int) {.raises: [Defect].} =
func extend[T](s: var seq[T], minLen: int) =
## Extend a sequence so that it can contains at least `minLen` elements.
## If it's already bigger, the sequence is unmodified.
## The extension is zero-initialized
@ -97,7 +99,7 @@ func process_attestation*(
validator_index: ValidatorIndex,
block_root: Eth2Digest,
target_epoch: Epoch
) {.raises: [Defect].} =
) =
## Add an attestation to the fork choice context
self.votes.extend(validator_index.int + 1)
@ -118,7 +120,7 @@ func process_block*(
state_root: Eth2Digest,
justified_epoch: Epoch,
finalized_epoch: Epoch
): Result[void, string] {.raises: [Defect].} =
): Result[void, string] =
## Add a block to the fork choice context
let err = self.proto_array.on_block(
slot, block_root, some(parent_root), state_root, justified_epoch, finalized_epoch
@ -134,7 +136,7 @@ func find_head*(
justified_root: Eth2Digest,
finalized_epoch: Epoch,
justified_state_balances: seq[Gwei]
): Result[Eth2Digest, string] {.raises: [Defect].} =
): Result[Eth2Digest, string] =
## Returns the new blockchain head
# Compute deltas with previous call
@ -169,7 +171,7 @@ func find_head*(
func maybe_prune*(
self: var ForkChoice, finalized_root: Eth2Digest
): Result[void, string] {.raises: [Defect].} =
): Result[void, string] =
## Prune blocks preceding the finalized root as they are now unneeded.
let err = self.proto_array.maybe_prune(finalized_root)
if err.kind != fcSuccess:
@ -182,7 +184,7 @@ func compute_deltas(
votes: var openArray[VoteTracker],
old_balances: openarray[Gwei],
new_balances: openarray[Gwei]
): ForkChoiceError {.raises: [Defect].} =
): ForkChoiceError =
## Update `deltas`
## between old and new balances
## between votes

View File

@ -6,6 +6,8 @@
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
{.push raises: [Defect].}
import
# Standard library
std/tables, std/options,

View File

@ -5,6 +5,8 @@
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
{.push raises: [Defect].}
import
# Standard library
std/tables, std/options, std/typetraits,
@ -58,9 +60,9 @@ template unsafeGet*[K, V](table: Table[K, V], key: K): V =
# Forward declarations
# ----------------------------------------------------------------------
func maybe_update_best_child_and_descendant(self: var ProtoArray, parent_index: Index, child_index: Index): ForkChoiceError {.raises: [Defect].}
func node_is_viable_for_head(self: ProtoArray, node: ProtoNode): bool {.raises: [Defect].}
func node_leads_to_viable_head(self: ProtoArray, node: ProtoNode): tuple[viable: bool, err: ForkChoiceError] {.raises: [Defect].}
func maybe_update_best_child_and_descendant(self: var ProtoArray, parent_index: Index, child_index: Index): ForkChoiceError
func node_is_viable_for_head(self: ProtoArray, node: ProtoNode): bool
func node_leads_to_viable_head(self: ProtoArray, node: ProtoNode): tuple[viable: bool, err: ForkChoiceError]
# ProtoArray routines
# ----------------------------------------------------------------------
@ -70,7 +72,7 @@ func apply_score_changes*(
deltas: var openarray[Delta],
justified_epoch: Epoch,
finalized_epoch: Epoch
): ForkChoiceError {.raises: [Defect].}=
): ForkChoiceError =
## Iterate backwards through the array, touching all nodes and their parents
## and potentially the best-child of each parent.
##
@ -155,7 +157,7 @@ func on_block*(
state_root: Eth2Digest,
justified_epoch: Epoch,
finalized_epoch: Epoch
): ForkChoiceError {.raises: [Defect].} =
): ForkChoiceError =
## Register a block with the fork choice
## A `none` parent is only valid for Genesis
@ -200,7 +202,7 @@ func find_head*(
self: var ProtoArray,
head: var Eth2Digest,
justified_root: Eth2Digest
): ForkChoiceError {.raises: [Defect].} =
): ForkChoiceError =
## Follows the best-descendant links to find the best-block (i.e. head-block)
##
## ⚠️ Warning
@ -259,7 +261,7 @@ func find_head*(
func maybe_prune*(
self: var ProtoArray,
finalized_root: Eth2Digest
): ForkChoiceError {.raises: [Defect].} =
): ForkChoiceError =
## Update the tree with new finalization information.
## The tree is pruned if and only if:
## - The `finalized_root` and finalized epoch are different from current
@ -340,7 +342,7 @@ func maybe_prune*(
func maybe_update_best_child_and_descendant(
self: var ProtoArray,
parent_index: Index,
child_index: Index): ForkChoiceError {.raises: [Defect].} =
child_index: Index): ForkChoiceError =
## Observe the parent at `parent_index` with respect to the child at `child_index` and
## potentiatlly modify the `parent.best_child` and `parent.best_descendant` values
##
@ -438,7 +440,7 @@ func maybe_update_best_child_and_descendant(
func node_leads_to_viable_head(
self: ProtoArray, node: ProtoNode
): tuple[viable: bool, err: ForkChoiceError] {.raises: [Defect].} =
): tuple[viable: bool, err: ForkChoiceError] =
## Indicates if the node itself or its best-descendant are viable
## for blockchain head
let best_descendant_is_viable_for_head = block:
@ -463,7 +465,7 @@ func node_leads_to_viable_head(
ForkChoiceSuccess
)
func node_is_viable_for_head(self: ProtoArray, node: ProtoNode): bool {.raises: [Defect].} =
func node_is_viable_for_head(self: ProtoArray, node: ProtoNode): bool =
## This is the equivalent of `filter_block_tree` function in eth2 spec
## https://github.com/ethereum/eth2.0-specs/blob/v0.10.0/specs/phase0/fork-choice.md#filter_block_tree
##