mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-1579] Remove selected tip hash messages. * [NOD-1579] Start moving IBD stuff into blockrelay. * [NOD-1579] Rename relaytransactions to transactionrelay. * [NOD-1579] Move IBD files into blockrelay. * [NOD-1579] Remove flow stuff from ibd.go. * [NOD-1579] Bring back IsInIBD(). * [NOD-1579] Simplify block relay flow. * [NOD-1579] Check orphan pool for missing parents to avoid unnecessary processing. * [NOD-1579] Implement processOrphan. * [NOD-1579] Implement addToOrphanSetAndRequestMissingParents. * [NOD-1579] Fix TestIBD. * [NOD-1579] Implement isBlockInOrphanResolutionRange. * [NOD-1579] Implement limited block locators. * [NOD-1579] Add some comments. * [NOD-1579] Specifically check for StatusHeaderOnly in blockrelay. * [NOD-1579] Simplify runIBDIfNotRunning. * [NOD-1579] Don't run IBD if it is already running. * [NOD-1579] Fix a comment. * [NOD-1579] Rename mode to syncInfo. * [NOD-1579] Simplify validateAndInsertBlock. * [NOD-1579] Fix bad SyncStateSynced condition. * [NOD-1579] Implement validateAgainstSyncStateAndResolveInsertMode. * [NOD-1579] Use insertModeHeader. * [NOD-1579] Add logs to TrySetIBDRunning and UnsetIBDRunning. * [NOD-1579] Implement and use dequeueIncomingMessageAndSkipInvs. * [NOD-1579] Fix a log. * [NOD-1579] Fix a bug in createBlockLocator. * [NOD-1579] Rename a variable. * [NOD-1579] Fix a slew of bugs in missingBlockBodyHashes and selectedChildIterator. * [NOD-1579] Fix bad chunk size in syncMissingBlockBodies. * [NOD-1579] Remove maxOrphanBlueScoreDiff. * [NOD-1579] Fix merge errors. * [NOD-1579] Remove a debug log. * [NOD-1579] Add logs. * [NOD-1579] Make various go quality tools happy. * [NOD-1579] Fix a typo in a variable name. * [NOD-1579] Fix full blocks over header-only blocks not failing the missing-parents validation. * [NOD-1579] Add an error log about a condition that should never happen. * [NOD-1579] Check all antiPast hashes instead of just the lowHash's anticone to filter for header-only blocks. * [NOD-1579] Remove the nil stuff from GetBlockLocator. * [NOD-1579] Remove superfluous condition in handleRelayInvsFlow.start(). * [NOD-1579] Return a boolean from requestBlock instead of comparing to nil. * [NOD-1579] Fix a bad log.Debugf. * [NOD-1579] Remove a redundant check. * [NOD-1579] Change an info log to a warning log. * [NOD-1579] Move OnNewBlock out of relayBlock. * [NOD-1579] Remove redundant exists check from runIBDIfNotRunning. * [NOD-1579] Fix bad call to OnNewBlock. * [NOD-1579] Remove an impossible check. * [NOD-1579] Added a log. * [NOD-1579] Rename insertModeBlockWithoutUpdatingVirtual to insertModeBlockBody. * [NOD-1579] Add a check for duplicate headers. * [NOD-1579] Added a comment. * [NOD-1579] Tighten a stop condition. * [NOD-1579] Simplify a log. * [NOD-1579] Clarify a log. * [NOD-1579] Move a log.
131 lines
4.1 KiB
Go
131 lines
4.1 KiB
Go
package flowcontext
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// AddOrphan adds the block to the orphan set
|
|
func (f *FlowContext) AddOrphan(orphanBlock *externalapi.DomainBlock) {
|
|
f.orphansMutex.Lock()
|
|
defer f.orphansMutex.Unlock()
|
|
|
|
orphanHash := consensushashing.BlockHash(orphanBlock)
|
|
f.orphans[*orphanHash] = orphanBlock
|
|
|
|
log.Infof("Received a block with missing parents, adding to orphan pool: %s", orphanHash)
|
|
}
|
|
|
|
// IsOrphan returns whether the given blockHash belongs to an orphan block
|
|
func (f *FlowContext) IsOrphan(blockHash *externalapi.DomainHash) bool {
|
|
f.orphansMutex.RLock()
|
|
defer f.orphansMutex.RUnlock()
|
|
|
|
_, ok := f.orphans[*blockHash]
|
|
return ok
|
|
}
|
|
|
|
// UnorphanBlocks removes the block from the orphan set, and remove all of the blocks that are not orphans anymore.
|
|
func (f *FlowContext) UnorphanBlocks(rootBlock *externalapi.DomainBlock) ([]*externalapi.DomainBlock, error) {
|
|
f.orphansMutex.Lock()
|
|
defer f.orphansMutex.Unlock()
|
|
|
|
// Find all the children of rootBlock among the orphans
|
|
// and add them to the process queue
|
|
rootBlockHash := consensushashing.BlockHash(rootBlock)
|
|
processQueue := f.addChildOrphansToProcessQueue(rootBlockHash, []externalapi.DomainHash{})
|
|
|
|
var unorphanedBlocks []*externalapi.DomainBlock
|
|
for len(processQueue) > 0 {
|
|
var orphanHash externalapi.DomainHash
|
|
orphanHash, processQueue = processQueue[0], processQueue[1:]
|
|
orphanBlock := f.orphans[orphanHash]
|
|
|
|
log.Tracef("Considering to unorphan block %s with parents %s",
|
|
orphanHash, orphanBlock.Header.ParentHashes)
|
|
|
|
canBeUnorphaned := true
|
|
for _, orphanBlockParentHash := range orphanBlock.Header.ParentHashes {
|
|
orphanBlockParentInfo, err := f.domain.Consensus().GetBlockInfo(orphanBlockParentHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !orphanBlockParentInfo.Exists || orphanBlockParentInfo.BlockStatus == externalapi.StatusHeaderOnly {
|
|
log.Tracef("Cannot unorphan block %s. It's missing at "+
|
|
"least the following parent: %s", orphanHash, orphanBlockParentHash)
|
|
|
|
canBeUnorphaned = false
|
|
break
|
|
}
|
|
}
|
|
if canBeUnorphaned {
|
|
err := f.unorphanBlock(orphanHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
unorphanedBlocks = append(unorphanedBlocks, orphanBlock)
|
|
processQueue = f.addChildOrphansToProcessQueue(&orphanHash, processQueue)
|
|
}
|
|
}
|
|
|
|
return unorphanedBlocks, nil
|
|
}
|
|
|
|
// addChildOrphansToProcessQueue finds all child orphans of `blockHash`
|
|
// and adds them to the given `processQueue` if they don't already exist
|
|
// inside of it
|
|
// Note that this method does not modify the given `processQueue`
|
|
func (f *FlowContext) addChildOrphansToProcessQueue(blockHash *externalapi.DomainHash,
|
|
processQueue []externalapi.DomainHash) []externalapi.DomainHash {
|
|
|
|
blockChildren := f.findChildOrphansOfBlock(blockHash)
|
|
for _, blockChild := range blockChildren {
|
|
exists := false
|
|
for _, queueOrphan := range processQueue {
|
|
if queueOrphan == blockChild {
|
|
exists = true
|
|
break
|
|
}
|
|
}
|
|
if !exists {
|
|
processQueue = append(processQueue, blockChild)
|
|
}
|
|
}
|
|
return processQueue
|
|
}
|
|
|
|
func (f *FlowContext) findChildOrphansOfBlock(blockHash *externalapi.DomainHash) []externalapi.DomainHash {
|
|
var childOrphans []externalapi.DomainHash
|
|
for orphanHash, orphanBlock := range f.orphans {
|
|
for _, orphanBlockParentHash := range orphanBlock.Header.ParentHashes {
|
|
if *orphanBlockParentHash == *blockHash {
|
|
childOrphans = append(childOrphans, orphanHash)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return childOrphans
|
|
}
|
|
|
|
func (f *FlowContext) unorphanBlock(orphanHash externalapi.DomainHash) error {
|
|
orphanBlock, ok := f.orphans[orphanHash]
|
|
if !ok {
|
|
return errors.Errorf("attempted to unorphan a non-orphan block %s", orphanHash)
|
|
}
|
|
delete(f.orphans, orphanHash)
|
|
|
|
err := f.domain.Consensus().ValidateAndInsertBlock(orphanBlock)
|
|
if err != nil {
|
|
if errors.As(err, &ruleerrors.RuleError{}) {
|
|
log.Infof("Validation failed for orphan block %s: %s", orphanHash, err)
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
log.Infof("Unorphaned block %s", orphanHash)
|
|
return nil
|
|
}
|