diff --git a/app/protocol/flowcontext/blocks.go b/app/protocol/flowcontext/blocks.go index 96965d24f..657221669 100644 --- a/app/protocol/flowcontext/blocks.go +++ b/app/protocol/flowcontext/blocks.go @@ -24,7 +24,6 @@ func (f *FlowContext) OnNewBlock(block *externalapi.DomainBlock, log.Debugf("OnNewBlock start for block %s", hash) defer log.Debugf("OnNewBlock end for block %s", hash) - f.UpdateRecentBlockAddedTimesWithLastBlock() unorphaningResults, err := f.UnorphanBlocks(block) if err != nil { return err diff --git a/app/protocol/flowcontext/flow_context.go b/app/protocol/flowcontext/flow_context.go index 1846148fd..2af8e5f86 100644 --- a/app/protocol/flowcontext/flow_context.go +++ b/app/protocol/flowcontext/flow_context.go @@ -36,9 +36,6 @@ type FlowContext struct { addressManager *addressmanager.AddressManager connectionManager *connmanager.ConnectionManager - recentBlockAddedTimes []int64 - recentBlockAddedTimesMutex sync.Mutex - timeStarted int64 onBlockAddedToDAGHandler OnBlockAddedToDAGHandler diff --git a/app/protocol/flowcontext/orphans.go b/app/protocol/flowcontext/orphans.go index d7a17ab58..4ef23eb28 100644 --- a/app/protocol/flowcontext/orphans.go +++ b/app/protocol/flowcontext/orphans.go @@ -156,7 +156,6 @@ func (f *FlowContext) unorphanBlock(orphanHash externalapi.DomainHash) (*externa } return nil, false, err } - f.UpdateRecentBlockAddedTimesWithLastBlock() log.Infof("Unorphaned block %s", orphanHash) return blockInsertionResult, true, nil diff --git a/app/protocol/flowcontext/should_mine.go b/app/protocol/flowcontext/should_mine.go new file mode 100644 index 000000000..7f0b4b628 --- /dev/null +++ b/app/protocol/flowcontext/should_mine.go @@ -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 +} diff --git a/app/protocol/flowcontext/sync_rate.go b/app/protocol/flowcontext/sync_rate.go deleted file mode 100644 index 79d488c35..000000000 --- a/app/protocol/flowcontext/sync_rate.go +++ /dev/null @@ -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 -} diff --git a/app/protocol/flows/blockrelay/handle_relay_invs.go b/app/protocol/flows/blockrelay/handle_relay_invs.go index f4a81a52f..8b91c5788 100644 --- a/app/protocol/flows/blockrelay/handle_relay_invs.go +++ b/app/protocol/flows/blockrelay/handle_relay_invs.go @@ -33,7 +33,6 @@ type RelayInvsContext interface { IsIBDRunning() bool TrySetIBDRunning() bool UnsetIBDRunning() - UpdateRecentBlockAddedTimesWithLastBlock() } type handleRelayInvsFlow struct { diff --git a/app/protocol/flows/blockrelay/ibd.go b/app/protocol/flows/blockrelay/ibd.go index eea862ace..8ff8a9c36 100644 --- a/app/protocol/flows/blockrelay/ibd.go +++ b/app/protocol/flows/blockrelay/ibd.go @@ -279,8 +279,6 @@ func (flow *handleRelayInvsFlow) processHeader(msgBlockHeader *appmessage.MsgBlo return protocolerrors.Wrapf(true, err, "got invalid block %s during IBD", blockHash) } - flow.UpdateRecentBlockAddedTimesWithLastBlock() - return nil }