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,
|
2024-06-16 03:22:06 +00:00
|
|
|
../../vm_types
|
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
|
|
|
|
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
|
|
|
|
2024-06-08 08:05:00 +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-30 14:06:51 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-16 03:22:06 +00:00
|
|
|
func newChain*(com: CommonRef,
|
2024-06-08 08:05:00 +00:00
|
|
|
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,
|
|
|
|
extraValidation: extraValidation,
|
2024-06-08 08:05:00 +00:00
|
|
|
vmState: vmState
|
2023-10-05 03:04:12 +00:00
|
|
|
)
|
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
|
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,
|
|
|
|
extraValidation: extraValidation,
|
|
|
|
)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public `Chain` getters
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-06-16 03:22:06 +00:00
|
|
|
func vmState*(c: ChainRef): BaseVMState =
|
2023-10-05 03:04:12 +00:00
|
|
|
## Getter
|
|
|
|
c.vmState
|
|
|
|
|
2024-06-16 03:22:06 +00:00
|
|
|
func 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
|
|
|
|
2024-06-16 03:22:06 +00:00
|
|
|
func 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
|
|
|
|
2024-06-16 03:22:06 +00:00
|
|
|
func 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-06-16 03:22:06 +00:00
|
|
|
func 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
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-16 03:22:06 +00:00
|
|
|
func `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-06-16 03:22:06 +00:00
|
|
|
func `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
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|