mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
Limit the orphan collection (#1238)
* Limit the orphan collection. * Fix grammar in a comment. * Fix a bad log.
This commit is contained in:
parent
bd5f4e8c6a
commit
843edc4ba5
@ -7,6 +7,12 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// maxOrphans is the maximum amount of orphans allowed in the
|
||||
// orphans collection. This number is an approximation of how
|
||||
// many orphans there can possibly be on average. It is based
|
||||
// on: 2^orphanResolutionRange * PHANTOM K.
|
||||
const maxOrphans = 600
|
||||
|
||||
// AddOrphan adds the block to the orphan set
|
||||
func (f *FlowContext) AddOrphan(orphanBlock *externalapi.DomainBlock) {
|
||||
f.orphansMutex.Lock()
|
||||
@ -15,9 +21,24 @@ func (f *FlowContext) AddOrphan(orphanBlock *externalapi.DomainBlock) {
|
||||
orphanHash := consensushashing.BlockHash(orphanBlock)
|
||||
f.orphans[*orphanHash] = orphanBlock
|
||||
|
||||
if len(f.orphans) > maxOrphans {
|
||||
log.Debugf("Orphan collection size exceeded. Evicting a random orphan")
|
||||
f.evictRandomOrphan()
|
||||
}
|
||||
|
||||
log.Infof("Received a block with missing parents, adding to orphan pool: %s", orphanHash)
|
||||
}
|
||||
|
||||
func (f *FlowContext) evictRandomOrphan() {
|
||||
var toEvict externalapi.DomainHash
|
||||
for hash := range f.orphans {
|
||||
toEvict = hash
|
||||
break
|
||||
}
|
||||
delete(f.orphans, toEvict)
|
||||
log.Debugf("Evicted %s from the orphan collection", toEvict)
|
||||
}
|
||||
|
||||
// IsOrphan returns whether the given blockHash belongs to an orphan block
|
||||
func (f *FlowContext) IsOrphan(blockHash *externalapi.DomainHash) bool {
|
||||
f.orphansMutex.RLock()
|
||||
|
Loading…
x
Reference in New Issue
Block a user