2021-07-14 15:13:27 +00:00
|
|
|
# Nimbus
|
2024-02-13 09:49:41 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-07-14 15:13:27 +00:00
|
|
|
# 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,
|
2023-10-05 03:04:12 +00:00
|
|
|
../../vm_types,
|
2024-05-25 14:12:14 +00:00
|
|
|
../pow
|
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.
|
|
|
|
|
2024-02-13 09:49:41 +00:00
|
|
|
generateWitness: bool ##\
|
|
|
|
## Enable generation of block witness, currently within `persistBlocks()`
|
|
|
|
## function only.
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
verifyFrom: BlockNumber ##\
|
|
|
|
## First block to when `extraValidation` will be applied (only
|
|
|
|
## effective if `extraValidation` is true.)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2023-10-05 03:04:12 +00:00
|
|
|
vmState: BaseVMState
|
|
|
|
## If it's not nil, block validation will use this
|
|
|
|
## If it's nil, a new vmState state will be created.
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-10-05 03:04:12 +00:00
|
|
|
proc newChain*(com: CommonRef,
|
|
|
|
extraValidation: bool, vmState = BaseVMState(nil)): 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`.
|
2023-10-05 03:04:12 +00:00
|
|
|
ChainRef(
|
|
|
|
com: com,
|
|
|
|
validateBlock: true,
|
|
|
|
extraValidation: extraValidation,
|
|
|
|
vmState: vmState,
|
|
|
|
)
|
2021-07-30 14:06:51 +00:00
|
|
|
|
2024-05-25 14:12:14 +00:00
|
|
|
func 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
|
2024-05-25 14:12:14 +00:00
|
|
|
let extraValidation = com.consensus == ConsensusType.POS
|
2023-10-05 03:04:12 +00:00
|
|
|
ChainRef(
|
|
|
|
com: com,
|
|
|
|
validateBlock: true,
|
|
|
|
extraValidation: extraValidation,
|
|
|
|
)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public `Chain` getters
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-10-05 03:04:12 +00:00
|
|
|
proc vmState*(c: ChainRef): BaseVMState =
|
|
|
|
## Getter
|
|
|
|
c.vmState
|
|
|
|
|
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
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc db*(c: ChainRef): CoreDbRef =
|
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
|
|
|
|
2024-02-13 09:49:41 +00:00
|
|
|
proc generateWitness*(c: ChainRef): bool =
|
|
|
|
## Getter
|
|
|
|
c.generateWitness
|
|
|
|
|
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
|
|
|
|
|
2024-02-13 09:49:41 +00:00
|
|
|
proc `generateWitness=`*(c: ChainRef; generateWitness: bool) =
|
|
|
|
## Setter. If set `true`, the assignment value `generateWitness` enables
|
|
|
|
## block witness generation.
|
|
|
|
c.generateWitness = generateWitness
|
|
|
|
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|