talelbaz f407c44a8d
[Issue - #1126] - Checking pruning point violation - pruning point in the past. (#1160)
* [NOD-1126]
1. Change function name in BlockValidator interface from: "ValidateProofOfWorkAndDifficulty" to "ValidatePruningPointViolationAndProofOfWorkAndDifficulty".
2. Add to the blockValidator struct the pruningManager (also added to the function "New" Respectively).
3. Added new function "checkPruningPointViolation" of blockValidator type.
4. Add new internal check - "checkPruningPointViolation", on the function "ValidateProofOfWorkAndDifficulty".(The third check).
5. Add new error rule - "ErrPruningPointViolation".

* [Issue-1126]
1. Remove the function "PruningPoint" from PruningManager interface.
2. Changes in blockValidator struct - remove pruningManager, and adding pruningStore.
3. Reads for "pruningPoint" function from pruningStore instead of pruningManager (because of note 1 above) in the functions: * "checkPruningPointViolation" of type blockValidator.
             * "FindNextPruningPoint" of type pruningManager.

* [Issue-1126]
1. Add missing error handling.

* [Issue-1126] Changes in function "checkPruningPointViolation": If header = genesis, stop checking and return nil.

* [Issue-1126] In function "checkPruningPointViolation" - change from a for loop to the "IsAncestorOfAny" function.

* [#1126] "FindNextPruningPoint" - save the pruning point in case the point is the genesis and change code internal order.

* [#1126] "FindNextPruningPoint" - cosmetics change.

* [#1126] "FindNextPruningPoint" - remove "return nil" when there is no pruning point on the if expression.

Co-authored-by: tal <tal@daglabs.com>
2020-11-30 09:57:15 +02:00

88 lines
3.1 KiB
Go

package blockvalidator
import (
"math/big"
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/util"
)
// blockValidator exposes a set of validation classes, after which
// it's possible to determine whether either a block is valid
type blockValidator struct {
powMax *big.Int
skipPoW bool
genesisHash *externalapi.DomainHash
enableNonNativeSubnetworks bool
disableDifficultyAdjustment bool
powMaxBits uint32
difficultyAdjustmentWindowSize uint64
databaseContext model.DBReader
difficultyManager model.DifficultyManager
pastMedianTimeManager model.PastMedianTimeManager
transactionValidator model.TransactionValidator
ghostdagManager model.GHOSTDAGManager
dagTopologyManager model.DAGTopologyManager
dagTraversalManager model.DAGTraversalManager
coinbaseManager model.CoinbaseManager
mergeDepthManager model.MergeDepthManager
pruningStore model.PruningStore
blockStore model.BlockStore
ghostdagDataStore model.GHOSTDAGDataStore
blockHeaderStore model.BlockHeaderStore
blockStatusStore model.BlockStatusStore
}
// New instantiates a new BlockValidator
func New(powMax *big.Int,
skipPoW bool,
genesisHash *externalapi.DomainHash,
enableNonNativeSubnetworks bool,
disableDifficultyAdjustment bool,
difficultyAdjustmentWindowSize uint64,
databaseContext model.DBReader,
difficultyManager model.DifficultyManager,
pastMedianTimeManager model.PastMedianTimeManager,
transactionValidator model.TransactionValidator,
ghostdagManager model.GHOSTDAGManager,
dagTopologyManager model.DAGTopologyManager,
dagTraversalManager model.DAGTraversalManager,
coinbaseManager model.CoinbaseManager,
mergeDepthManager model.MergeDepthManager,
pruningStore model.PruningStore,
blockStore model.BlockStore,
ghostdagDataStore model.GHOSTDAGDataStore,
blockHeaderStore model.BlockHeaderStore,
blockStatusStore model.BlockStatusStore) model.BlockValidator {
return &blockValidator{
powMax: powMax,
skipPoW: skipPoW,
genesisHash: genesisHash,
enableNonNativeSubnetworks: enableNonNativeSubnetworks,
disableDifficultyAdjustment: disableDifficultyAdjustment,
powMaxBits: util.BigToCompact(powMax),
difficultyAdjustmentWindowSize: difficultyAdjustmentWindowSize,
databaseContext: databaseContext,
difficultyManager: difficultyManager,
pastMedianTimeManager: pastMedianTimeManager,
transactionValidator: transactionValidator,
ghostdagManager: ghostdagManager,
dagTopologyManager: dagTopologyManager,
dagTraversalManager: dagTraversalManager,
coinbaseManager: coinbaseManager,
mergeDepthManager: mergeDepthManager,
pruningStore: pruningStore,
blockStore: blockStore,
ghostdagDataStore: ghostdagDataStore,
blockHeaderStore: blockHeaderStore,
blockStatusStore: blockStatusStore,
}
}