kaspad/domain/consensus/processes/blockprocessor/validateandinsertblock.go
Svarog 281944762d
[NOD-1500] Glue between domain and application (#1007)
* [NOD-1500] Added Domain type and Constructor

* [NOD-1500] Replaced dag+txpool with domain in flowContext

* [NOD-1500] Replaced dag+txpool with domain in flowContext

* [NOD-1500] Converters: domain objects from/to appmessage

* [NOD-1500] Convert hashes to DomainHashes in appmessages

* [NOD-1500] Remove references to daghash in dagconfig

* [NOD-1500] Fixed all appmessage usages of hashes

* [NOD-1500] Update all RPC to use domain

* [NOD-1500] Big chunk of protocol flows re-wired to domain

* [NOD-1500] Finished re-wiring all protocol flows to new Domain

* [NOD-1500] Fix some mempool and kaspaminer compilation errors

* [NOD-1500] Deleted util/{block,tx,daghash} and dbaccess

* [NOD-1500] util.CoinbaseTransactionIndex -> transactionhelper.CoinbaseTransactionIndex

* [NOD-1500] Fix txsigner

* [NOD-1500] Removed all references to util/subnetworkid

* [NOD-1500] Update RpcGetBlock related messages

* [NOD-1500] Many more compilation fixes

* [NOD-1500] Return full list of missing blocks for orphan resolution

* [NOD-1500] Fixed handshake

* [NOD-1500] Fixed flowcontext compilation

* [NOD-1500] Update users of StartIBDIfRequired to handle error

* [NOD-1500] Removed some more fields from RPC

* [NOD-1500] Fix the getBlockTemplate flow

* [NOD-1500] Fix HandleGetCurrentNetwork

* [NOD-1500] Remove redundant code

* [NOD-1500] Remove obsolete notifications

* [NOD-1500] Split MiningManager and Consensus to separate fields in Domain

* [NOD-1500] Update two wrong references to location of txscript

* [NOD-1500] Added comments

* [NOD-1500] Fix some tests

* [NOD-1500] Removed serialization logic from appmessage

* [NOD-1500] Rename database/serialization/messages.proto to dbobjects.proto

* [NOD-1500] Delete integration tests

* [NOD-1500] Remove txsort

* [NOD-1500] Fix tiny bug

* [NOD-1500] Remove rogue dependancy on bchd

* [NOD-1500] Some stylistic fixes
2020-11-08 11:55:54 +02:00

281 lines
6.8 KiB
Go

package blockprocessor
import (
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensusserialization"
"github.com/pkg/errors"
)
func (bp *blockProcessor) validateAndInsertBlock(block *externalapi.DomainBlock) error {
mode, err := bp.syncManager.GetSyncInfo()
if err != nil {
return err
}
hash := consensusserialization.HeaderHash(block.Header)
if mode.State == externalapi.SyncStateMissingUTXOSet {
headerTipsPruningPoint, err := bp.consensusStateManager.HeaderTipsPruningPoint()
if err != nil {
return err
}
if *hash != *headerTipsPruningPoint {
return errors.Errorf("cannot insert blocks other than the header pruning point "+
"while in %s mode", mode.State)
}
mode.State = externalapi.SyncStateMissingBlockBodies
}
if mode.State == externalapi.SyncStateHeadersFirst && len(block.Transactions) != 0 {
mode.State = externalapi.SyncStateNormal
log.Warnf("block %s contains transactions while validating in header only mode", hash)
}
err = bp.checkBlockStatus(hash, mode)
if err != nil {
return err
}
err = bp.validateBlock(block, mode)
if err != nil {
bp.discardAllChanges()
return err
}
hasHeader, err := bp.hasHeader(hash)
if err != nil {
return err
}
if !hasHeader {
if mode.State == externalapi.SyncStateMissingBlockBodies {
return errors.Wrapf(ruleerrors.ErrMissingBlockHeaderInIBD, "no block header is stored for block %s. "+
"Every block we get during %s mode should have a pre-stored header", mode.State, hash)
}
err = bp.reachabilityManager.AddBlock(hash)
if err != nil {
return err
}
err = bp.headerTipsManager.AddHeaderTip(hash)
if err != nil {
return err
}
}
if mode.State == externalapi.SyncStateHeadersFirst {
bp.blockStatusStore.Stage(hash, externalapi.StatusHeaderOnly)
} else {
bp.blockStatusStore.Stage(hash, externalapi.StatusUTXOPendingVerification)
}
// Block validations passed, save whatever DAG data was
// collected so far
err = bp.commitAllChanges()
if err != nil {
return err
}
oldHeadersSelectedTip, err := bp.headerTipsManager.SelectedTip()
if err != nil {
return err
}
if mode.State == externalapi.SyncStateHeadersFirst {
err = bp.headerTipsManager.AddHeaderTip(hash)
if err != nil {
return err
}
} else if mode.State == externalapi.SyncStateNormal {
// Attempt to add the block to the virtual
err = bp.consensusStateManager.AddBlockToVirtual(hash)
if err != nil {
return err
}
tips, err := bp.consensusStateStore.Tips(bp.databaseContext)
if err != nil {
return err
}
bp.headerTipsStore.Stage(tips)
}
err = bp.updateReachabilityReindexRoot(oldHeadersSelectedTip)
if err != nil {
return err
}
// Trigger pruning, which will check if the pruning point changed and delete the data if it did.
err = bp.pruningManager.FindNextPruningPoint()
if err != nil {
return err
}
return bp.commitAllChanges()
}
func (bp *blockProcessor) updateReachabilityReindexRoot(oldHeadersSelectedTip *externalapi.DomainHash) error {
headersSelectedTip, err := bp.headerTipsManager.SelectedTip()
if err != nil {
return err
}
if *headersSelectedTip == *oldHeadersSelectedTip {
return nil
}
return bp.reachabilityManager.UpdateReindexRoot(headersSelectedTip)
}
func (bp *blockProcessor) checkBlockStatus(hash *externalapi.DomainHash, mode *externalapi.SyncInfo) error {
exists, err := bp.blockStatusStore.Exists(bp.databaseContext, hash)
if err != nil {
return err
}
if !exists {
return nil
}
status, err := bp.blockStatusStore.Get(bp.databaseContext, hash)
if err != nil {
return err
}
if status == externalapi.StatusInvalid {
return errors.Wrapf(ruleerrors.ErrKnownInvalid, "block %s is a known invalid block", hash)
}
if mode.State == externalapi.SyncStateHeadersFirst || status != externalapi.StatusHeaderOnly {
return errors.Wrapf(ruleerrors.ErrDuplicateBlock, "block %s already exists", hash)
}
return nil
}
func (bp *blockProcessor) validateBlock(block *externalapi.DomainBlock, mode *externalapi.SyncInfo) error {
// If any validation until (included) proof-of-work fails, simply
// return an error without writing anything in the database.
// This is to prevent spamming attacks.
err := bp.validatePreProofOfWork(block)
if err != nil {
return err
}
blockHash := consensusserialization.BlockHash(block)
err = bp.blockValidator.ValidateProofOfWorkAndDifficulty(blockHash)
if err != nil {
return err
}
// If in-context validations fail, discard all changes and store the
// block with StatusInvalid.
err = bp.validatePostProofOfWork(block, mode)
if err != nil {
if errors.As(err, &ruleerrors.RuleError{}) {
bp.discardAllChanges()
hash := consensusserialization.BlockHash(block)
bp.blockStatusStore.Stage(hash, externalapi.StatusInvalid)
commitErr := bp.commitAllChanges()
if commitErr != nil {
return commitErr
}
}
return err
}
return nil
}
func (bp *blockProcessor) validatePreProofOfWork(block *externalapi.DomainBlock) error {
blockHash := consensusserialization.BlockHash(block)
hasHeader, err := bp.hasHeader(blockHash)
if err != nil {
return err
}
if hasHeader {
return nil
}
err = bp.blockValidator.ValidateHeaderInIsolation(blockHash)
if err != nil {
return err
}
return nil
}
func (bp *blockProcessor) validatePostProofOfWork(block *externalapi.DomainBlock, mode *externalapi.SyncInfo) error {
blockHash := consensusserialization.BlockHash(block)
if mode.State != externalapi.SyncStateHeadersFirst {
bp.blockStore.Stage(blockHash, block)
err := bp.blockValidator.ValidateBodyInIsolation(blockHash)
if err != nil {
return err
}
}
hasHeader, err := bp.hasHeader(blockHash)
if err != nil {
return err
}
if !hasHeader {
bp.blockHeaderStore.Stage(blockHash, block.Header)
err := bp.dagTopologyManager.SetParents(blockHash, block.Header.ParentHashes)
if err != nil {
return err
}
err = bp.blockValidator.ValidateHeaderInContext(blockHash)
if err != nil {
return err
}
}
return nil
}
func (bp *blockProcessor) hasHeader(blockHash *externalapi.DomainHash) (bool, error) {
exists, err := bp.blockStatusStore.Exists(bp.databaseContext, blockHash)
if err != nil {
return false, err
}
if !exists {
return false, nil
}
status, err := bp.blockStatusStore.Get(bp.databaseContext, blockHash)
if err != nil {
return false, err
}
return status == externalapi.StatusHeaderOnly, nil
}
func (bp *blockProcessor) discardAllChanges() {
for _, store := range bp.stores {
store.Discard()
}
}
func (bp *blockProcessor) commitAllChanges() error {
dbTx, err := bp.databaseContext.Begin()
if err != nil {
return err
}
for _, store := range bp.stores {
err = store.Commit(dbTx)
if err != nil {
return err
}
}
return dbTx.Commit()
}