2021-07-14 15:13:27 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2023-02-14 20:27:17 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
import
|
2022-12-02 04:35:41 +00:00
|
|
|
../../common/common,
|
|
|
|
../../utils/utils,
|
|
|
|
../pow,
|
2023-06-25 13:30:34 +00:00
|
|
|
../clique
|
2022-12-02 04:35:41 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
common
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
type
|
2022-12-02 04:35:41 +00:00
|
|
|
ChainRef* = ref object of RootRef
|
|
|
|
com: CommonRef
|
|
|
|
## common block chain configuration
|
|
|
|
## used throughout entire app
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-09-16 04:23:25 +00:00
|
|
|
validateBlock: bool ##\
|
|
|
|
## If turn off, `persistBlocks` will always return
|
|
|
|
## ValidationResult.OK and disable extraValidation too.
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
extraValidation: bool ##\
|
2021-07-30 14:06:51 +00:00
|
|
|
## Trigger extra validation, currently within `persistBlocks()`
|
|
|
|
## function only.
|
|
|
|
|
|
|
|
verifyFrom: BlockNumber ##\
|
|
|
|
## First block to when `extraValidation` will be applied (only
|
|
|
|
## effective if `extraValidation` is true.)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-30 14:06:51 +00:00
|
|
|
# Private constructor helper
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
proc initChain(c: ChainRef; com: CommonRef; extraValidation: bool) =
|
2022-12-05 11:25:44 +00:00
|
|
|
## Constructor for the `Chain` descriptor object.
|
2022-12-02 04:35:41 +00:00
|
|
|
c.com = com
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-09-16 04:23:25 +00:00
|
|
|
c.validateBlock = true
|
2021-07-30 14:06:51 +00:00
|
|
|
c.extraValidation = extraValidation
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
proc newChain*(com: CommonRef, extraValidation: bool): ChainRef =
|
2022-12-05 11:25:44 +00:00
|
|
|
## Constructor for the `Chain` descriptor object.
|
|
|
|
## The argument `extraValidation` enables extra block
|
2021-07-30 14:06:51 +00:00
|
|
|
## chain validation if set `true`.
|
|
|
|
new result
|
2022-12-05 11:25:44 +00:00
|
|
|
result.initChain(com, extraValidation)
|
2021-07-30 14:06:51 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
proc newChain*(com: CommonRef): ChainRef =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Constructor for the `Chain` descriptor object. All sub-object descriptors
|
|
|
|
## are initialised with defaults. So is extra block chain validation
|
|
|
|
## * `enabled` for PoA networks (such as Goerli)
|
2022-12-02 04:35:41 +00:00
|
|
|
## * `disabled` for non-PaA networks
|
2021-07-30 14:06:51 +00:00
|
|
|
new result
|
2022-12-05 11:25:44 +00:00
|
|
|
result.initChain(com, com.consensus == ConsensusType.POA)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public `Chain` getters
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-12-05 11:25:44 +00:00
|
|
|
proc clique*(c: ChainRef): Clique =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
2022-12-05 11:25:44 +00:00
|
|
|
c.com.poa
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc pow*(c: ChainRef): PowRef =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
2022-12-05 11:25:44 +00:00
|
|
|
c.com.pow
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc db*(c: ChainRef): ChainDBRef =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
2022-12-02 04:35:41 +00:00
|
|
|
c.com.db
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc com*(c: ChainRef): CommonRef =
|
2022-09-16 04:23:25 +00:00
|
|
|
## Getter
|
2022-12-02 04:35:41 +00:00
|
|
|
c.com
|
2022-09-16 04:23:25 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc validateBlock*(c: ChainRef): bool =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
2022-12-02 04:35:41 +00:00
|
|
|
c.validateBlock
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc extraValidation*(c: ChainRef): bool =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
2022-12-02 04:35:41 +00:00
|
|
|
c.extraValidation
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc verifyFrom*(c: ChainRef): BlockNumber =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Getter
|
|
|
|
c.verifyFrom
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc currentBlock*(c: ChainRef): BlockHeader
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2021-08-24 07:34:58 +00:00
|
|
|
## currentBlock retrieves the current head block of the canonical chain.
|
|
|
|
## Ideally the block should be retrieved from the blockchain's internal cache.
|
|
|
|
## but now it's enough to retrieve it from database
|
|
|
|
c.db.getCanonicalHead()
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public `Chain` setters
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-12-02 04:35:41 +00:00
|
|
|
proc `validateBlock=`*(c: ChainRef; validateBlock: bool) =
|
2022-09-16 04:23:25 +00:00
|
|
|
## Setter. If set `true`, the assignment value `validateBlock` enables
|
|
|
|
## block execution, else it will always return ValidationResult.OK
|
|
|
|
c.validateBlock = validateBlock
|
2021-07-30 14:06:51 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc `extraValidation=`*(c: ChainRef; extraValidation: bool) =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Setter. If set `true`, the assignment value `extraValidation` enables
|
|
|
|
## extra block chain validation.
|
|
|
|
c.extraValidation = extraValidation
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc `verifyFrom=`*(c: ChainRef; verifyFrom: BlockNumber) =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Setter. The assignment value `verifyFrom` defines the first block where
|
|
|
|
## validation should start if the `Clique` field `extraValidation` was set
|
|
|
|
## `true`.
|
2022-01-10 09:04:06 +00:00
|
|
|
c.verifyFrom = verifyFrom
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc `verifyFrom=`*(c: ChainRef; verifyFrom: uint64) =
|
2022-01-10 09:04:06 +00:00
|
|
|
## Variant of `verifyFrom=`
|
2021-07-30 14:06:51 +00:00
|
|
|
c.verifyFrom = verifyFrom.u256
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|