diff --git a/beacon_chain/fork_choice/fork_choice.nim b/beacon_chain/fork_choice/fork_choice.nim index 6064060078..7dcbb87bdd 100644 --- a/beacon_chain/fork_choice/fork_choice.nim +++ b/beacon_chain/fork_choice/fork_choice.nim @@ -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 @@ -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, @@ -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 @@ -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) @@ -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 @@ -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 @@ -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: @@ -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 diff --git a/beacon_chain/fork_choice/proto_array.nim b/beacon_chain/fork_choice/proto_array.nim index ace9cac76d..8548a641d4 100644 --- a/beacon_chain/fork_choice/proto_array.nim +++ b/beacon_chain/fork_choice/proto_array.nim @@ -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 # ---------------------------------------------------------------------- @@ -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. ## @@ -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 @@ -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 @@ -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 @@ -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 ## @@ -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: @@ -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 ## diff --git a/tests/fork_choice/interpreter.nim b/tests/fork_choice/interpreter.nim index 2d51fb8f16..987ab36c99 100644 --- a/tests/fork_choice/interpreter.nim +++ b/tests/fork_choice/interpreter.nim @@ -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