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
128 lines
3.9 KiB
Go
128 lines
3.9 KiB
Go
package consensusstatemanager
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/processes/consensusstatemanager/utxoalgebra"
|
|
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (csm *consensusStateManager) resolveBlockStatus(blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error) {
|
|
// get list of all blocks in the selected parent chain that have not yet resolved their status
|
|
unverifiedBlocks, selectedParentStatus, err := csm.getUnverifiedChainBlocksAndSelectedParentStatus(blockHash)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
// resolve the unverified blocks' statuses in opposite order
|
|
for i := len(unverifiedBlocks); i >= 0; i++ {
|
|
unverifiedBlockHash := unverifiedBlocks[i]
|
|
|
|
var blockStatus externalapi.BlockStatus
|
|
if selectedParentStatus == externalapi.StatusDisqualifiedFromChain {
|
|
blockStatus = externalapi.StatusDisqualifiedFromChain
|
|
} else {
|
|
blockStatus, err = csm.resolveSingleBlockStatus(unverifiedBlockHash)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
csm.blockStatusStore.Stage(unverifiedBlockHash, blockStatus)
|
|
selectedParentStatus = blockStatus
|
|
}
|
|
|
|
return 0, nil
|
|
}
|
|
|
|
func (csm *consensusStateManager) getUnverifiedChainBlocksAndSelectedParentStatus(blockHash *externalapi.DomainHash) (
|
|
[]*externalapi.DomainHash, externalapi.BlockStatus, error) {
|
|
|
|
unverifiedBlocks := []*externalapi.DomainHash{blockHash}
|
|
currentHash := blockHash
|
|
for {
|
|
ghostdagData, err := csm.ghostdagDataStore.Get(csm.databaseContext, currentHash)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
selectedParentStatus, err := csm.blockStatusStore.Get(csm.databaseContext, ghostdagData.SelectedParent)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if selectedParentStatus != externalapi.StatusUTXOPendingVerification {
|
|
return unverifiedBlocks, selectedParentStatus, nil
|
|
}
|
|
|
|
unverifiedBlocks = append(unverifiedBlocks, ghostdagData.SelectedParent)
|
|
|
|
currentHash = ghostdagData.SelectedParent
|
|
}
|
|
}
|
|
|
|
func (csm *consensusStateManager) resolveSingleBlockStatus(blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error) {
|
|
pastUTXODiff, acceptanceData, multiset, err := csm.calculatePastUTXOAndAcceptanceData(blockHash)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
csm.acceptanceDataStore.Stage(blockHash, acceptanceData)
|
|
|
|
block, err := csm.blockStore.Block(csm.databaseContext, blockHash)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
err = csm.verifyAndBuildUTXO(block, blockHash, pastUTXODiff, acceptanceData, multiset)
|
|
if err != nil {
|
|
if errors.As(err, &ruleerrors.RuleError{}) {
|
|
return externalapi.StatusDisqualifiedFromChain, nil
|
|
}
|
|
return 0, err
|
|
}
|
|
|
|
csm.multisetStore.Stage(blockHash, multiset)
|
|
csm.utxoDiffStore.Stage(blockHash, pastUTXODiff, nil)
|
|
|
|
err = csm.updateParentDiffs(blockHash, pastUTXODiff)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return externalapi.StatusValid, nil
|
|
}
|
|
func (csm *consensusStateManager) updateParentDiffs(
|
|
blockHash *externalapi.DomainHash, pastUTXODiff *model.UTXODiff) error {
|
|
parentHashes, err := csm.dagTopologyManager.Parents(blockHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, parentHash := range parentHashes {
|
|
// skip all parents that already have a utxo-diff child
|
|
parentHasUTXODiffChild, err := csm.utxoDiffStore.HasUTXODiffChild(csm.databaseContext, parentHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if parentHasUTXODiffChild {
|
|
continue
|
|
}
|
|
|
|
// parents that till now didn't have a utxo-diff child - were actually virtual's diffParents.
|
|
// Update them to have the new block as their utxo-diff child
|
|
parentCurrentDiff, err := csm.utxoDiffStore.UTXODiff(csm.databaseContext, parentHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
parentNewDiff, err := utxoalgebra.DiffFrom(pastUTXODiff, parentCurrentDiff)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
csm.utxoDiffStore.Stage(parentHash, parentNewDiff, blockHash)
|
|
}
|
|
|
|
return nil
|
|
}
|