Skip to content

Commit

Permalink
use raises defect everywhere in fork choice and fix process_attestati…
Browse files Browse the repository at this point in the history
…on test
  • Loading branch information
mratsim committed Apr 7, 2020
1 parent 1921c13 commit e332828
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
16 changes: 8 additions & 8 deletions beacon_chain/fork_choice/fork_choice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func compute_deltas(
votes: var openArray[VoteTracker],
old_balances: openarray[Gwei],
new_balances: openarray[Gwei]
): ForkChoiceError {.raises: [].}
): ForkChoiceError {.raises: [Defect].}
# 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

Expand All @@ -51,7 +51,7 @@ func initForkChoice*(
justified_epoch: Epoch,
finalized_epoch: Epoch,
finalized_root: Eth2Digest
): Result[ForkChoice, string] {.raises: [].} =
): Result[ForkChoice, string] {.raises: [Defect].} =
## Initialize a fork choice context
var proto_array = ProtoArray(
prune_threshold: DefaultPruneThreshold,
Expand All @@ -72,7 +72,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: [].} =
func extend[T](s: var seq[T], minLen: int) {.raises: [Defect].} =
## 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
Expand All @@ -97,7 +97,7 @@ func process_attestation*(
validator_index: ValidatorIndex,
block_root: Eth2Digest,
target_epoch: Epoch
) {.raises: [].} =
) {.raises: [Defect].} =
## Add an attestation to the fork choice context
self.votes.extend(validator_index.int + 1)

Expand All @@ -118,7 +118,7 @@ func process_block*(
state_root: Eth2Digest,
justified_epoch: Epoch,
finalized_epoch: Epoch
): Result[void, string] {.raises: [].} =
): Result[void, string] {.raises: [Defect].} =
## 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
Expand All @@ -134,7 +134,7 @@ func find_head*(
justified_root: Eth2Digest,
finalized_epoch: Epoch,
justified_state_balances: seq[Gwei]
): Result[Eth2Digest, string] {.raises: [].} =
): Result[Eth2Digest, string] {.raises: [Defect].} =
## Returns the new blockchain head

# Compute deltas with previous call
Expand Down Expand Up @@ -169,7 +169,7 @@ func find_head*(

func maybe_prune*(
self: var ForkChoice, finalized_root: Eth2Digest
): Result[void, string] {.raises: [].} =
): Result[void, string] {.raises: [Defect].} =
## Prune blocks preceding the finalized root as they are now unneeded.
let err = self.proto_array.maybe_prune(finalized_root)
if err.kind != fcSuccess:
Expand All @@ -182,7 +182,7 @@ func compute_deltas(
votes: var openArray[VoteTracker],
old_balances: openarray[Gwei],
new_balances: openarray[Gwei]
): ForkChoiceError {.raises: [].} =
): ForkChoiceError {.raises: [Defect].} =
## Update `deltas`
## between old and new balances
## between votes
Expand Down
20 changes: 10 additions & 10 deletions beacon_chain/fork_choice/proto_array.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,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: [].}
func node_is_viable_for_head(self: ProtoArray, node: ProtoNode): bool {.raises: [].}
func node_leads_to_viable_head(self: ProtoArray, node: ProtoNode): tuple[viable: bool, err: ForkChoiceError] {.raises: [].}
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].}

# ProtoArray routines
# ----------------------------------------------------------------------
Expand All @@ -70,7 +70,7 @@ func apply_score_changes*(
deltas: var openarray[Delta],
justified_epoch: Epoch,
finalized_epoch: Epoch
): ForkChoiceError {.raises: [].}=
): ForkChoiceError {.raises: [Defect].}=
## Iterate backwards through the array, touching all nodes and their parents
## and potentially the best-child of each parent.
##
Expand Down Expand Up @@ -155,7 +155,7 @@ func on_block*(
state_root: Eth2Digest,
justified_epoch: Epoch,
finalized_epoch: Epoch
): ForkChoiceError {.raises: [].} =
): ForkChoiceError {.raises: [Defect].} =
## Register a block with the fork choice
## A `none` parent is only valid for Genesis

Expand Down Expand Up @@ -200,7 +200,7 @@ func find_head*(
self: var ProtoArray,
head: var Eth2Digest,
justified_root: Eth2Digest
): ForkChoiceError {.raises: [].} =
): ForkChoiceError {.raises: [Defect].} =
## Follows the best-descendant links to find the best-block (i.e. head-block)
##
## ⚠️ Warning
Expand Down Expand Up @@ -259,7 +259,7 @@ func find_head*(
func maybe_prune*(
self: var ProtoArray,
finalized_root: Eth2Digest
): ForkChoiceError {.raises: [].} =
): ForkChoiceError {.raises: [Defect].} =
## 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
Expand Down Expand Up @@ -340,7 +340,7 @@ func maybe_prune*(
func maybe_update_best_child_and_descendant(
self: var ProtoArray,
parent_index: Index,
child_index: Index): ForkChoiceError {.raises: [].} =
child_index: Index): ForkChoiceError {.raises: [Defect].} =
## 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
##
Expand Down Expand Up @@ -438,7 +438,7 @@ func maybe_update_best_child_and_descendant(

func node_leads_to_viable_head(
self: ProtoArray, node: ProtoNode
): tuple[viable: bool, err: ForkChoiceError] {.raises: [].} =
): tuple[viable: bool, err: ForkChoiceError] {.raises: [Defect].} =
## Indicates if the node itself or its best-descendant are viable
## for blockchain head
let best_descendant_is_viable_for_head = block:
Expand All @@ -463,7 +463,7 @@ func node_leads_to_viable_head(
ForkChoiceSuccess
)

func node_is_viable_for_head(self: ProtoArray, node: ProtoNode): bool {.raises: [].} =
func node_is_viable_for_head(self: ProtoArray, node: ProtoNode): bool {.raises: [Defect].} =
## 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
##
Expand Down
3 changes: 1 addition & 2 deletions tests/fork_choice/interpreter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ func apply(ctx: var ForkChoice, id: int, op: Operation) =
doAssert r.isOk(), &"process_block (op #{id}) returned an error: {r.error}"
debugEcho " Processed block 0x", op.root, " with parent 0x", op.parent_root, " and justified epoch ", op.blk_justified_epoch
of ProcessAttestation:
let r = ctx.process_attestation(
ctx.process_attestation(
validator_index = op.validator_index,
block_root = op.block_root,
target_epoch = op.target_epoch
)
doAssert r.isOk(), &"process_attestation (op #{id}) returned an error: {r.error}"
debugEcho " Processed att target 0x", op.block_root, " from validator ", op.validator_index, " for epoch ", op.target_epoch
of Prune:
ctx.proto_array.prune_threshold = op.prune_threshold
Expand Down

0 comments on commit e332828

Please sign in to comment.