Svarog c5707f64dc
[NOD-1420] Implement consensusStateManager (#985)
* [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
2020-11-02 16:18:53 +02:00

156 lines
5.6 KiB
Go

package dagtopologymanager
import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
)
// dagTopologyManager exposes methods for querying relationships
// between blocks in the DAG
type dagTopologyManager struct {
reachabilityManager model.ReachabilityManager
blockRelationStore model.BlockRelationStore
databaseContext model.DBReader
}
// New instantiates a new DAGTopologyManager
func New(
databaseContext model.DBReader,
reachabilityManager model.ReachabilityManager,
blockRelationStore model.BlockRelationStore) model.DAGTopologyManager {
return &dagTopologyManager{
databaseContext: databaseContext,
reachabilityManager: reachabilityManager,
blockRelationStore: blockRelationStore,
}
}
// Parents returns the DAG parents of the given blockHash
func (dtm *dagTopologyManager) Parents(blockHash *externalapi.DomainHash) ([]*externalapi.DomainHash, error) {
blockRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, blockHash)
if err != nil {
return nil, err
}
return blockRelations.Parents, nil
}
// Children returns the DAG children of the given blockHash
func (dtm *dagTopologyManager) Children(blockHash *externalapi.DomainHash) ([]*externalapi.DomainHash, error) {
blockRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, blockHash)
if err != nil {
return nil, err
}
return blockRelations.Children, nil
}
// IsParentOf returns true if blockHashA is a direct DAG parent of blockHashB
func (dtm *dagTopologyManager) IsParentOf(blockHashA *externalapi.DomainHash, blockHashB *externalapi.DomainHash) (bool, error) {
blockRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, blockHashB)
if err != nil {
return false, err
}
return isHashInSlice(blockHashA, blockRelations.Parents), nil
}
// IsChildOf returns true if blockHashA is a direct DAG child of blockHashB
func (dtm *dagTopologyManager) IsChildOf(blockHashA *externalapi.DomainHash, blockHashB *externalapi.DomainHash) (bool, error) {
blockRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, blockHashB)
if err != nil {
return false, err
}
return isHashInSlice(blockHashA, blockRelations.Children), nil
}
// IsAncestorOf returns true if blockHashA is a DAG ancestor of blockHashB
func (dtm *dagTopologyManager) IsAncestorOf(blockHashA *externalapi.DomainHash, blockHashB *externalapi.DomainHash) (bool, error) {
return dtm.reachabilityManager.IsDAGAncestorOf(blockHashA, blockHashB)
}
// IsDescendantOf returns true if blockHashA is a DAG descendant of blockHashB
func (dtm *dagTopologyManager) IsDescendantOf(blockHashA *externalapi.DomainHash, blockHashB *externalapi.DomainHash) (bool, error) {
return dtm.reachabilityManager.IsDAGAncestorOf(blockHashB, blockHashA)
}
// IsAncestorOfAny returns true if `blockHash` is an ancestor of at least one of `potentialDescendants`
func (dtm *dagTopologyManager) IsAncestorOfAny(blockHash *externalapi.DomainHash, potentialDescendants []*externalapi.DomainHash) (bool, error) {
for _, potentialDescendant := range potentialDescendants {
isAncestorOf, err := dtm.IsAncestorOf(blockHash, potentialDescendant)
if err != nil {
return false, err
}
if isAncestorOf {
return true, nil
}
}
return false, nil
}
// IsInSelectedParentChainOf returns true if blockHashA is in the selected parent chain of blockHashB
func (dtm *dagTopologyManager) IsInSelectedParentChainOf(blockHashA *externalapi.DomainHash, blockHashB *externalapi.DomainHash) (bool, error) {
return dtm.reachabilityManager.IsReachabilityTreeAncestorOf(blockHashA, blockHashB)
}
func isHashInSlice(hash *externalapi.DomainHash, hashes []*externalapi.DomainHash) bool {
for _, h := range hashes {
if *h == *hash {
return true
}
}
return false
}
func (dtm *dagTopologyManager) SetParents(blockHash *externalapi.DomainHash, parentHashes []*externalapi.DomainHash) error {
// Go over the block's current relations (if they exist), and remove the block from all it's current parents
// Note: In theory we should also remove the block from all it's children, however, in practice no block
// ever has it's relations updated after getting any children, therefore we skip this step
currentRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, blockHash)
if err != nil {
return err
}
if currentRelations != nil {
for _, currentParent := range currentRelations.Parents {
parentRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, currentParent)
if err != nil {
return err
}
for i, parentChild := range parentRelations.Children {
if *parentChild == *blockHash {
parentRelations.Children = append(parentRelations.Children[:i], parentRelations.Children[i+1:]...)
dtm.blockRelationStore.StageBlockRelation(currentParent, parentRelations)
break
}
}
}
}
// Go over all new parents and add block as their child
for _, parent := range parentHashes {
parentRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, parent)
if err != nil {
return err
}
isBlockAlreadyInChildren := false
for _, parentChild := range parentRelations.Children {
if *parentChild == *blockHash {
isBlockAlreadyInChildren = true
break
}
}
if !isBlockAlreadyInChildren {
parentRelations.Children = append(parentRelations.Children, blockHash)
dtm.blockRelationStore.StageBlockRelation(parent, parentRelations)
}
}
// Finally - create the relations for the block itself
dtm.blockRelationStore.StageBlockRelation(blockHash, &model.BlockRelations{
Parents: parentHashes,
Children: []*externalapi.DomainHash{},
})
return nil
}