mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-07-04 20:02:30 +00:00
Delete sync rate mechanism (#1356)
Co-authored-by: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com>
This commit is contained in:
parent
3ec1cbe236
commit
2059d6ba56
@ -24,7 +24,6 @@ func (f *FlowContext) OnNewBlock(block *externalapi.DomainBlock,
|
|||||||
log.Debugf("OnNewBlock start for block %s", hash)
|
log.Debugf("OnNewBlock start for block %s", hash)
|
||||||
defer log.Debugf("OnNewBlock end for block %s", hash)
|
defer log.Debugf("OnNewBlock end for block %s", hash)
|
||||||
|
|
||||||
f.UpdateRecentBlockAddedTimesWithLastBlock()
|
|
||||||
unorphaningResults, err := f.UnorphanBlocks(block)
|
unorphaningResults, err := f.UnorphanBlocks(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -36,9 +36,6 @@ type FlowContext struct {
|
|||||||
addressManager *addressmanager.AddressManager
|
addressManager *addressmanager.AddressManager
|
||||||
connectionManager *connmanager.ConnectionManager
|
connectionManager *connmanager.ConnectionManager
|
||||||
|
|
||||||
recentBlockAddedTimes []int64
|
|
||||||
recentBlockAddedTimesMutex sync.Mutex
|
|
||||||
|
|
||||||
timeStarted int64
|
timeStarted int64
|
||||||
|
|
||||||
onBlockAddedToDAGHandler OnBlockAddedToDAGHandler
|
onBlockAddedToDAGHandler OnBlockAddedToDAGHandler
|
||||||
|
@ -156,7 +156,6 @@ func (f *FlowContext) unorphanBlock(orphanHash externalapi.DomainHash) (*externa
|
|||||||
}
|
}
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
f.UpdateRecentBlockAddedTimesWithLastBlock()
|
|
||||||
|
|
||||||
log.Infof("Unorphaned block %s", orphanHash)
|
log.Infof("Unorphaned block %s", orphanHash)
|
||||||
return blockInsertionResult, true, nil
|
return blockInsertionResult, true, nil
|
||||||
|
43
app/protocol/flowcontext/should_mine.go
Normal file
43
app/protocol/flowcontext/should_mine.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package flowcontext
|
||||||
|
|
||||||
|
import "github.com/kaspanet/kaspad/util/mstime"
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxSelectedParentTimeDiffToAllowMiningInMilliSeconds = 300_000
|
||||||
|
)
|
||||||
|
|
||||||
|
// ShouldMine returns whether it's ok to use block template from this node
|
||||||
|
// for mining purposes.
|
||||||
|
func (f *FlowContext) ShouldMine() (bool, error) {
|
||||||
|
peers := f.Peers()
|
||||||
|
if len(peers) == 0 {
|
||||||
|
log.Debugf("The node is not connected, so ShouldMine returns false")
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.IsIBDRunning() {
|
||||||
|
log.Debugf("IBD is running, so ShouldMine returns false")
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
virtualSelectedParent, err := f.domain.Consensus().GetVirtualSelectedParent()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
virtualSelectedParentHeader, err := f.domain.Consensus().GetBlockHeader(virtualSelectedParent)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
now := mstime.Now().UnixMilliseconds()
|
||||||
|
if now-virtualSelectedParentHeader.TimeInMilliseconds() < maxSelectedParentTimeDiffToAllowMiningInMilliSeconds {
|
||||||
|
log.Debugf("The selected tip timestamp is recent (%d), so ShouldMine returns true",
|
||||||
|
virtualSelectedParentHeader.TimeInMilliseconds())
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("The selected tip timestamp is old (%d), so ShouldMine returns false",
|
||||||
|
virtualSelectedParentHeader.TimeInMilliseconds())
|
||||||
|
return false, nil
|
||||||
|
}
|
@ -1,98 +0,0 @@
|
|||||||
package flowcontext
|
|
||||||
|
|
||||||
import "github.com/kaspanet/kaspad/util/mstime"
|
|
||||||
|
|
||||||
const (
|
|
||||||
syncRateWindowInMilliSeconds = 15 * 60 * 1000
|
|
||||||
syncRateMaxDeviation = 0.05
|
|
||||||
maxSelectedParentTimeDiffToAllowMiningInMilliSeconds = 300_000
|
|
||||||
)
|
|
||||||
|
|
||||||
// UpdateRecentBlockAddedTimesWithLastBlock adds current time to list of times when block was added.
|
|
||||||
// We use this list to determine the current sync rate
|
|
||||||
func (f *FlowContext) UpdateRecentBlockAddedTimesWithLastBlock() {
|
|
||||||
f.recentBlockAddedTimesMutex.Lock()
|
|
||||||
defer f.recentBlockAddedTimesMutex.Unlock()
|
|
||||||
|
|
||||||
f.removeOldBlockTimes()
|
|
||||||
f.recentBlockAddedTimes = append(f.recentBlockAddedTimes, mstime.Now().UnixMilliseconds())
|
|
||||||
}
|
|
||||||
|
|
||||||
// removeOldBlockTimes removes from recentBlockAddedTimes block times
|
|
||||||
// older than syncRateWindowInMilliSeconds.
|
|
||||||
// This function is not safe for concurrent use.
|
|
||||||
func (f *FlowContext) removeOldBlockTimes() {
|
|
||||||
now := mstime.Now().UnixMilliseconds()
|
|
||||||
mostRecentBlockToKeep := 0
|
|
||||||
for i, blockAddedTime := range f.recentBlockAddedTimes {
|
|
||||||
if now-syncRateWindowInMilliSeconds < blockAddedTime {
|
|
||||||
mostRecentBlockToKeep = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.recentBlockAddedTimes = f.recentBlockAddedTimes[mostRecentBlockToKeep:]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *FlowContext) isSyncRateBelowMinimum() bool {
|
|
||||||
f.recentBlockAddedTimesMutex.Lock()
|
|
||||||
defer f.recentBlockAddedTimesMutex.Unlock()
|
|
||||||
|
|
||||||
f.removeOldBlockTimes()
|
|
||||||
|
|
||||||
now := mstime.Now().UnixMilliseconds()
|
|
||||||
timeSinceStart := now - f.timeStarted
|
|
||||||
if timeSinceStart <= syncRateWindowInMilliSeconds {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedBlocks := float64(syncRateWindowInMilliSeconds) / float64(f.cfg.NetParams().TargetTimePerBlock.Milliseconds())
|
|
||||||
isSyncRateTooLow := 1-float64(len(f.recentBlockAddedTimes))/expectedBlocks > syncRateMaxDeviation
|
|
||||||
|
|
||||||
if isSyncRateTooLow {
|
|
||||||
log.Debugf("In the last %d seconds, got %d blocks, while at least %f were expected.",
|
|
||||||
syncRateWindowInMilliSeconds/1000, len(f.recentBlockAddedTimes), expectedBlocks*(1-syncRateMaxDeviation))
|
|
||||||
}
|
|
||||||
|
|
||||||
return isSyncRateTooLow
|
|
||||||
}
|
|
||||||
|
|
||||||
// ShouldMine returns whether it's ok to use block template from this node
|
|
||||||
// for mining purposes.
|
|
||||||
func (f *FlowContext) ShouldMine() (bool, error) {
|
|
||||||
peers := f.Peers()
|
|
||||||
if len(peers) == 0 {
|
|
||||||
log.Debugf("The node is not connected, so ShouldMine returns false")
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.isSyncRateBelowMinimum() {
|
|
||||||
log.Debugf("The sync rate is below the minimum, so ShouldMine returns true")
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.IsIBDRunning() {
|
|
||||||
log.Debugf("IBD is running, so ShouldMine returns false")
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
virtualSelectedParent, err := f.domain.Consensus().GetVirtualSelectedParent()
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
virtualSelectedParentHeader, err := f.domain.Consensus().GetBlockHeader(virtualSelectedParent)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
now := mstime.Now().UnixMilliseconds()
|
|
||||||
if now-virtualSelectedParentHeader.TimeInMilliseconds() < maxSelectedParentTimeDiffToAllowMiningInMilliSeconds {
|
|
||||||
log.Debugf("The selected tip timestamp is recent (%d), so ShouldMine returns true",
|
|
||||||
virtualSelectedParentHeader.TimeInMilliseconds())
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("The selected tip timestamp is old (%d), so ShouldMine returns false",
|
|
||||||
virtualSelectedParentHeader.TimeInMilliseconds())
|
|
||||||
return false, nil
|
|
||||||
}
|
|
@ -33,7 +33,6 @@ type RelayInvsContext interface {
|
|||||||
IsIBDRunning() bool
|
IsIBDRunning() bool
|
||||||
TrySetIBDRunning() bool
|
TrySetIBDRunning() bool
|
||||||
UnsetIBDRunning()
|
UnsetIBDRunning()
|
||||||
UpdateRecentBlockAddedTimesWithLastBlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type handleRelayInvsFlow struct {
|
type handleRelayInvsFlow struct {
|
||||||
|
@ -279,8 +279,6 @@ func (flow *handleRelayInvsFlow) processHeader(msgBlockHeader *appmessage.MsgBlo
|
|||||||
return protocolerrors.Wrapf(true, err, "got invalid block %s during IBD", blockHash)
|
return protocolerrors.Wrapf(true, err, "got invalid block %s during IBD", blockHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
flow.UpdateRecentBlockAddedTimesWithLastBlock()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user