mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-23 07:16:47 +00:00

* [NOD-1420] Start working on ConsensusStateManager. Might be redundant due to recent changes * [NOD-1420] Convert model to externalapi in utxo_algerbra helpers * [NOD-1420] Add UTXO-diff algebra * [NOD-1420] Prepare skeleton of calculateAcceptanceDataAndMultiset * [NOD-1420] Added skeleton for AddBlockToVirtual * [NOD-1420] Implement PopulateTransactionWithUTXOEntries * [NOD-1420] Implement restorePastUTXO * [NOD-1420] Implement finality check * [NOD-1420] Move handling of tips to consensusStateManager * [NOD-1420] Implement calculateAcceptanceDataAndMultiset * [NOD-1420] Start implementing resolveBlockStatus * [NOD-1420] Implement resolveBlockStatus * [NOD-1420] Update related fields in end of resolveSingleBlockStatus * [NOD-1420] Start working on selectVirtualParents * [NOD-1420] Implemented BlockHeap * [NOD-1420] Implement selectVirtualParents * [NOD-1420] Implement updateVirtual * [NOD-1420] Added comments where they were missing * [NOD-1420] Place all consensusStateManager functions in correct files * [NOD-1420] Return the missing outpoints from populateTransactionWithUTXOEntriesFromVirtualOrDiff * [NOD-1420] Outpoint.ID -> TransactionID * [NOD-1420] Fix Stringer tests * [NOD-1420] Copy hash.FromString into utils * [NOD-1420] SetParents should return an error * [NOD-1420] Remove all reachabilityManager references from consensusStateManager * [NOD-1420] Remove VirtualData. Get the info from the stores where needed * [NOD-1420] Invert parameters to IsAncestorOf * [NOD-1420] Use model.AcceptanceData * [NOD-1420] Don't return accumulatedMassBefore in error cases * [NOD-1420] Don't expect store functions to return nil when the requested data was found - instead add HasXXX functions * [NOD-1420] addTransactionToMultiset sets isCoinbase properly * [NOD-1420] expected hash string length is externalapi.DomainHashSize * 2 * [NOD-1420] Rename reachabilityTree -> reachabilityManager + updateReindexRoot if isNextVirtualSelectedParent * [NOD-1420] ValidateCoinbaseTransaction in csm.verifyAndBuildUTXO * [NOD-1420] Re-write HAsUTXODiffChild * [NOD-1420] delete past_utxo.go.bak * [NOD-1420] Implement validateCoinbaseTransaction in CSM * [NOD-1420] Imlemented missing functionality in ValidateTransactionAndPopulateWithConsensusData * [NOD-1420] Moved merge depth logic to MergeDepthManager * [NOD-1420] Add logs
115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
package blockprocessor
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (bp *blockProcessor) validateAndInsertBlock(block *externalapi.DomainBlock) error {
|
|
err := bp.validateBlock(block)
|
|
if err != nil {
|
|
bp.discardAllChanges()
|
|
return err
|
|
}
|
|
|
|
// Block validations passed, save whatever DAG data was
|
|
// collected so far
|
|
err = bp.commitAllChanges()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Attempt to add the block to the virtual
|
|
err = bp.consensusStateManager.AddBlockToVirtual(block.Hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return bp.commitAllChanges()
|
|
}
|
|
|
|
func (bp *blockProcessor) validateBlock(block *externalapi.DomainBlock) error {
|
|
// If either in-isolation or proof-of-work validations fail, simply
|
|
// return an error without writing anything in the database.
|
|
// This is to prevent spamming attacks.
|
|
err := bp.validateBlockInIsolationAndProofOfWork(block)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// If in-context validations fail, discard all changes and store the
|
|
// block with StatusInvalid.
|
|
err = bp.validateInContext(block)
|
|
if err != nil {
|
|
if errors.As(err, &ruleerrors.RuleError{}) {
|
|
bp.discardAllChanges()
|
|
bp.blockStatusStore.Stage(block.Hash, externalapi.StatusInvalid)
|
|
commitErr := bp.commitAllChanges()
|
|
if commitErr != nil {
|
|
return commitErr
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (bp *blockProcessor) validateBlockInIsolationAndProofOfWork(block *externalapi.DomainBlock) error {
|
|
err := bp.blockValidator.ValidateHeaderInIsolation(block.Hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = bp.blockValidator.ValidateBodyInIsolation(block.Hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = bp.blockValidator.ValidateProofOfWorkAndDifficulty(block.Hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (bp *blockProcessor) validateInContext(block *externalapi.DomainBlock) error {
|
|
err := bp.dagTopologyManager.SetParents(block.Hash, block.Header.ParentHashes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
bp.blockStore.Stage(block.Hash, block)
|
|
bp.blockHeaderStore.Stage(block.Hash, block.Header)
|
|
|
|
err = bp.blockValidator.ValidateHeaderInContext(block.Hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = bp.blockValidator.ValidateBodyInContext(block.Hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return 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()
|
|
}
|