2021-03-11 10:10:57 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * 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
|
2021-08-18 18:57:58 +00:00
|
|
|
chronicles, chronos,
|
2021-08-12 13:08:20 +00:00
|
|
|
../spec/datatypes/base,
|
2021-12-21 18:56:08 +00:00
|
|
|
../beacon_clock,
|
2021-12-06 09:49:01 +00:00
|
|
|
../consensus_object_pools/[blockchain_dag, block_quarantine, attestation_pool]
|
2021-03-11 10:10:57 +00:00
|
|
|
|
|
|
|
# TODO: Move to "consensus_object_pools" folder
|
|
|
|
|
|
|
|
type
|
|
|
|
ConsensusManager* = object
|
|
|
|
expectedSlot: Slot
|
|
|
|
expectedBlockReceived: Future[bool]
|
|
|
|
|
|
|
|
# Validated & Verified
|
|
|
|
# ----------------------------------------------------------------
|
2021-06-01 11:13:40 +00:00
|
|
|
dag*: ChainDAGRef
|
2021-03-11 10:10:57 +00:00
|
|
|
attestationPool*: ref AttestationPool
|
|
|
|
|
|
|
|
# Missing info
|
|
|
|
# ----------------------------------------------------------------
|
2021-12-06 09:49:01 +00:00
|
|
|
quarantine*: ref Quarantine
|
2021-03-11 10:10:57 +00:00
|
|
|
|
|
|
|
# Initialization
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc new*(T: type ConsensusManager,
|
2021-06-01 11:13:40 +00:00
|
|
|
dag: ChainDAGRef,
|
2021-03-11 10:10:57 +00:00
|
|
|
attestationPool: ref AttestationPool,
|
2021-12-06 09:49:01 +00:00
|
|
|
quarantine: ref Quarantine
|
2021-03-11 10:10:57 +00:00
|
|
|
): ref ConsensusManager =
|
|
|
|
(ref ConsensusManager)(
|
2021-06-01 11:13:40 +00:00
|
|
|
dag: dag,
|
2021-03-11 10:10:57 +00:00
|
|
|
attestationPool: attestationPool,
|
|
|
|
quarantine: quarantine
|
|
|
|
)
|
|
|
|
|
|
|
|
# Consensus Management
|
|
|
|
# -----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc checkExpectedBlock(self: var ConsensusManager) =
|
|
|
|
if self.expectedBlockReceived == nil:
|
|
|
|
return
|
|
|
|
|
2021-06-01 11:13:40 +00:00
|
|
|
if self.dag.head.slot < self.expectedSlot:
|
2021-03-11 10:10:57 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.expectedBlockReceived.complete(true)
|
|
|
|
self.expectedBlockReceived = nil # Don't keep completed futures around!
|
|
|
|
|
|
|
|
proc expectBlock*(self: var ConsensusManager, expectedSlot: Slot): Future[bool] =
|
|
|
|
## Return a future that will complete when a head is selected whose slot is
|
|
|
|
## equal or greater than the given slot, or a new expectation is created
|
|
|
|
if self.expectedBlockReceived != nil:
|
|
|
|
# Reset the old future to not leave it hanging.. an alternative would be to
|
|
|
|
# cancel it, but it doesn't make any practical difference for now
|
|
|
|
self.expectedBlockReceived.complete(false)
|
|
|
|
|
|
|
|
let fut = newFuture[bool]("ConsensusManager.expectBlock")
|
|
|
|
self.expectedSlot = expectedSlot
|
|
|
|
self.expectedBlockReceived = fut
|
|
|
|
|
|
|
|
# It might happen that by the time we're expecting a block, it might have
|
|
|
|
# already been processed!
|
|
|
|
self.checkExpectedBlock()
|
|
|
|
|
|
|
|
return fut
|
|
|
|
|
|
|
|
proc updateHead*(self: var ConsensusManager, wallSlot: Slot) =
|
|
|
|
## Trigger fork choice and update the DAG with the new head block
|
|
|
|
## This does not automatically prune the DAG after finalization
|
|
|
|
## `pruneFinalized` must be called for pruning.
|
|
|
|
|
|
|
|
# Grab the new head according to our latest attestation data
|
2021-12-21 18:56:08 +00:00
|
|
|
let newHead = self.attestationPool[].selectHead(wallSlot.toBeaconTime)
|
2021-03-11 10:10:57 +00:00
|
|
|
if newHead.isNil():
|
|
|
|
warn "Head selection failed, using previous head",
|
2021-06-01 11:13:40 +00:00
|
|
|
head = shortLog(self.dag.head), wallSlot
|
2021-03-11 10:10:57 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Store the new head in the chain DAG - this may cause epochs to be
|
|
|
|
# justified and finalized
|
2021-12-06 09:49:01 +00:00
|
|
|
self.dag.updateHead(newHead, self.quarantine[])
|
2021-03-11 10:10:57 +00:00
|
|
|
|
|
|
|
self.checkExpectedBlock()
|
|
|
|
|
|
|
|
proc pruneStateCachesAndForkChoice*(self: var ConsensusManager) =
|
|
|
|
## Prune unneeded and invalidated data after finalization
|
|
|
|
## - the DAG state checkpoints
|
|
|
|
## - the DAG EpochRef
|
|
|
|
## - the attestation pool/fork choice
|
|
|
|
|
|
|
|
# Cleanup DAG & fork choice if we have a finalized head
|
2021-06-01 11:13:40 +00:00
|
|
|
if self.dag.needStateCachesAndForkChoicePruning():
|
|
|
|
self.dag.pruneStateCachesDAG()
|
2021-03-11 10:10:57 +00:00
|
|
|
self.attestationPool[].prune()
|