diff --git a/app/protocol/flowcontext/blocks.go b/app/protocol/flowcontext/blocks.go index 1ab87e81e..96965d24f 100644 --- a/app/protocol/flowcontext/blocks.go +++ b/app/protocol/flowcontext/blocks.go @@ -24,7 +24,7 @@ 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() + f.UpdateRecentBlockAddedTimesWithLastBlock() unorphaningResults, err := f.UnorphanBlocks(block) if err != nil { return err diff --git a/app/protocol/flowcontext/orphans.go b/app/protocol/flowcontext/orphans.go index f655c4bcf..d7a17ab58 100644 --- a/app/protocol/flowcontext/orphans.go +++ b/app/protocol/flowcontext/orphans.go @@ -156,7 +156,7 @@ func (f *FlowContext) unorphanBlock(orphanHash externalapi.DomainHash) (*externa } return nil, false, err } - f.updateRecentBlockAddedTimesWithLastBlock() + f.UpdateRecentBlockAddedTimesWithLastBlock() log.Infof("Unorphaned block %s", orphanHash) return blockInsertionResult, true, nil diff --git a/app/protocol/flowcontext/sync_rate.go b/app/protocol/flowcontext/sync_rate.go index b7c0f2d2d..fedbc27ed 100644 --- a/app/protocol/flowcontext/sync_rate.go +++ b/app/protocol/flowcontext/sync_rate.go @@ -3,12 +3,14 @@ package flowcontext import "github.com/kaspanet/kaspad/util/mstime" const ( - syncRateWindowInMilliSeconds = 60_000 + syncRateWindowInMilliSeconds = 15 * 60 * 1000 syncRateMaxDeviation = 0.05 maxSelectedParentTimeDiffToAllowMiningInMilliSeconds = 300_000 ) -func (f *FlowContext) updateRecentBlockAddedTimesWithLastBlock() { +// 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() @@ -44,7 +46,14 @@ func (f *FlowContext) isSyncRateBelowMinimum() bool { } expectedBlocks := float64(syncRateWindowInMilliSeconds) / float64(f.cfg.NetParams().TargetTimePerBlock.Milliseconds()) - return 1-float64(len(f.recentBlockAddedTimes))/expectedBlocks > syncRateMaxDeviation + 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 diff --git a/app/protocol/flows/blockrelay/handle_relay_invs.go b/app/protocol/flows/blockrelay/handle_relay_invs.go index 8b91c5788..f4a81a52f 100644 --- a/app/protocol/flows/blockrelay/handle_relay_invs.go +++ b/app/protocol/flows/blockrelay/handle_relay_invs.go @@ -33,6 +33,7 @@ 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 9d70cc3cb..0e266d0ed 100644 --- a/app/protocol/flows/blockrelay/ibd.go +++ b/app/protocol/flows/blockrelay/ibd.go @@ -1,6 +1,8 @@ package blockrelay import ( + "time" + "github.com/kaspanet/kaspad/app/appmessage" "github.com/kaspanet/kaspad/app/protocol/common" "github.com/kaspanet/kaspad/app/protocol/protocolerrors" @@ -8,7 +10,6 @@ import ( "github.com/kaspanet/kaspad/domain/consensus/ruleerrors" "github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing" "github.com/pkg/errors" - "time" ) func (flow *handleRelayInvsFlow) runIBDIfNotRunning(highHash *externalapi.DomainHash) error { @@ -257,6 +258,9 @@ func (flow *handleRelayInvsFlow) processHeader(msgBlockHeader *appmessage.MsgBlo return protocolerrors.Wrapf(true, err, "got invalid block %s during IBD", blockHash) } + + flow.UpdateRecentBlockAddedTimesWithLastBlock() + return nil }