mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-09-13 21:10:12 +00:00

* [NOD-1119] Removed all p2p server from all the initialization of server * [NOD-1119] Removed any calling for p2p server in main * [NOD-1119] Simplified some functions to not take both dag and dagParams * [NOD-1119] Simplify creation of mempool and rpc server * [NOD-1119] Setup indexes in separate function * [NOD-1119] Some cleanup in NewServer * [NOD-1119] Fix mempool test * [NOD-1119] Fix go format * [NOD-1119] Unexport dag.timeSource * [NOD-1119] Removed server package + renamed the Server object to Kaspad, and made it minimal * [NOD-1119] Delete redundant functions * Unexported kaspad and related methods * [NOD-1119] Unexported newKaspad * [NOD-1119] Revise comments and remove redundant function * [NOD-1119] Make comments of unexported methods lower-case * [NOD-1119] Some more refactoring in newKaspad
61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
package blockdag
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/util/mstime"
|
|
"time"
|
|
)
|
|
|
|
const syncRateWindowDuration = 15 * time.Minute
|
|
|
|
// addBlockProcessingTimestamp adds the last block processing timestamp in order to measure the recent sync rate.
|
|
//
|
|
// This function MUST be called with the DAG state lock held (for writes).
|
|
func (dag *BlockDAG) addBlockProcessingTimestamp() {
|
|
now := mstime.Now()
|
|
dag.recentBlockProcessingTimestamps = append(dag.recentBlockProcessingTimestamps, now)
|
|
dag.removeNonRecentTimestampsFromRecentBlockProcessingTimestamps()
|
|
}
|
|
|
|
// removeNonRecentTimestampsFromRecentBlockProcessingTimestamps removes timestamps older than syncRateWindowDuration
|
|
// from dag.recentBlockProcessingTimestamps
|
|
//
|
|
// This function MUST be called with the DAG state lock held (for writes).
|
|
func (dag *BlockDAG) removeNonRecentTimestampsFromRecentBlockProcessingTimestamps() {
|
|
dag.recentBlockProcessingTimestamps = dag.recentBlockProcessingTimestampsRelevantWindow()
|
|
}
|
|
|
|
func (dag *BlockDAG) recentBlockProcessingTimestampsRelevantWindow() []mstime.Time {
|
|
minTime := mstime.Now().Add(-syncRateWindowDuration)
|
|
windowStartIndex := len(dag.recentBlockProcessingTimestamps)
|
|
for i, processTime := range dag.recentBlockProcessingTimestamps {
|
|
if processTime.After(minTime) {
|
|
windowStartIndex = i
|
|
break
|
|
}
|
|
}
|
|
return dag.recentBlockProcessingTimestamps[windowStartIndex:]
|
|
}
|
|
|
|
// syncRate returns the rate of processed
|
|
// blocks in the last syncRateWindowDuration
|
|
// duration.
|
|
func (dag *BlockDAG) syncRate() float64 {
|
|
dag.RLock()
|
|
defer dag.RUnlock()
|
|
return float64(len(dag.recentBlockProcessingTimestampsRelevantWindow())) / syncRateWindowDuration.Seconds()
|
|
}
|
|
|
|
// IsSyncRateBelowThreshold checks whether the sync rate
|
|
// is below the expected threshold.
|
|
func (dag *BlockDAG) IsSyncRateBelowThreshold(maxDeviation float64) bool {
|
|
if dag.uptime() < syncRateWindowDuration {
|
|
return false
|
|
}
|
|
|
|
return dag.syncRate() < 1/dag.Params.TargetTimePerBlock.Seconds()*maxDeviation
|
|
}
|
|
|
|
func (dag *BlockDAG) uptime() time.Duration {
|
|
return mstime.Now().Sub(dag.startTime)
|
|
}
|