From 36759d6d4ee81aca11f8c23294fa3b36aa5e1ed1 Mon Sep 17 00:00:00 2001 From: mikkeljuhl Date: Sun, 1 Apr 2018 11:48:09 +0200 Subject: [PATCH] Will be squashed: Uptime 2 --- cmd/siac/hostcmd.go | 2 +- cmd/siac/hostdbcmd.go | 8 +++---- modules/renter/hostdb/consts.go | 3 +++ modules/renter/hostdb/hostweight.go | 37 ++++++++++++++++------------- modules/renter/hostdb/scan.go | 16 +------------ 5 files changed, 28 insertions(+), 38 deletions(-) diff --git a/cmd/siac/hostcmd.go b/cmd/siac/hostcmd.go index 1018b7f59e..4a7c1ca4df 100644 --- a/cmd/siac/hostcmd.go +++ b/cmd/siac/hostcmd.go @@ -9,9 +9,9 @@ import ( "text/tabwriter" "github.com/NebulousLabs/Sia/modules" - "github.com/NebulousLabs/Sia/node/api" "github.com/NebulousLabs/Sia/types" + "github.com/NebulousLabs/Sia/node/api" "github.com/spf13/cobra" ) diff --git a/cmd/siac/hostdbcmd.go b/cmd/siac/hostdbcmd.go index 64d2e31059..fb6b956fa4 100644 --- a/cmd/siac/hostdbcmd.go +++ b/cmd/siac/hostdbcmd.go @@ -2,14 +2,12 @@ package main import ( "fmt" + "github.com/NebulousLabs/Sia/modules" + "github.com/NebulousLabs/Sia/node/api" + "github.com/spf13/cobra" "math/big" "os" "text/tabwriter" - - "github.com/spf13/cobra" - - "github.com/NebulousLabs/Sia/modules" - "github.com/NebulousLabs/Sia/node/api" ) const scanHistoryLen = 30 diff --git a/modules/renter/hostdb/consts.go b/modules/renter/hostdb/consts.go index 7a19aeca9c..77927e1e4d 100644 --- a/modules/renter/hostdb/consts.go +++ b/modules/renter/hostdb/consts.go @@ -34,6 +34,9 @@ const ( // scans start getting compressed. minScans = 12 + // halftimeUpDownTime is the halftime used to decay the host up and downtime + halftimeUpDownTime = 30 * 24 * time.Hour + // recentInteractionWeightLimit caps the number of recent interactions as a // percentage of the historic interactions, to be certain that a large // amount of activity in a short period of time does not overwhelm the diff --git a/modules/renter/hostdb/hostweight.go b/modules/renter/hostdb/hostweight.go index 0c97193b88..aaf9702453 100644 --- a/modules/renter/hostdb/hostweight.go +++ b/modules/renter/hostdb/hostweight.go @@ -311,11 +311,9 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 { // host. downtime := entry.HistoricDowntime uptime := entry.HistoricUptime - recentTime := entry.ScanHistory[0].Timestamp - recentBlockHeight := entry.ScanHistory[0].BlockHeight - recentSuccess := entry.ScanHistory[0].Success + recentScan := entry.ScanHistory[0] for _, scan := range entry.ScanHistory[1:] { - if recentTime.After(scan.Timestamp) { + if recentScan.Timestamp.After(scan.Timestamp) { if build.DEBUG { hdb.log.Critical("Host entry scan history not sorted.") } else { @@ -325,19 +323,8 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 { continue } - blockDiff := scan.BlockHeight - recentBlockHeight - timePassed := float64(time.Duration(blockDiff) * 10 * time.Minute) - halftime := float64(1 * time.Hour * 24 * 30) - decay := math.Pow(0.5, timePassed/halftime) - - if recentSuccess { - uptime += time.Duration(timePassed * decay) - } else { - downtime += time.Duration(timePassed * decay) - } - recentTime = scan.Timestamp - recentBlockHeight = scan.BlockHeight - recentSuccess = scan.Success + decayUptimeOrDowntime(&entry, scan, recentScan) + recentScan = scan } // Sanity check against 0 total time. if uptime == 0 && downtime == 0 { @@ -375,6 +362,22 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 { return math.Pow(uptimeRatio, exp) } +// decayHostUpOrDowntime decays a host's historic uptime or historic downtime. +// It also adds the new block height to the historic uptime or historic downtime. +func decayUptimeOrDowntime(entry *modules.HostDBEntry, scan modules.HostDBScan, recentScan modules.HostDBScan) { + blocksPassed := scan.BlockHeight - recentScan.BlockHeight + timePassed := time.Duration(blocksPassed) * 10 * time.Minute + decay := time.Duration(math.Pow(0.5, float64(timePassed)/float64(halftimeUpDownTime))) + + if recentScan.Success { + entry.HistoricUptime *= decay + entry.HistoricUptime += timePassed * decay + } else { + entry.HistoricDowntime *= decay + entry.HistoricDowntime += timePassed * decay + } +} + // calculateHostWeight returns the weight of a host according to the settings of // the host database entry. func (hdb *HostDB) calculateHostWeight(entry modules.HostDBEntry) types.Currency { diff --git a/modules/renter/hostdb/scan.go b/modules/renter/hostdb/scan.go index 10a158258e..88e9c1b5eb 100644 --- a/modules/renter/hostdb/scan.go +++ b/modules/renter/hostdb/scan.go @@ -5,7 +5,6 @@ package hostdb // settings of the hosts. import ( - "math" "net" "time" @@ -208,20 +207,7 @@ func (hdb *HostDB) updateEntry(entry modules.HostDBEntry, netErr error) { // Compress any old scans into the historic values. for len(newEntry.ScanHistory) > minScans && time.Now().Sub(newEntry.ScanHistory[0].Timestamp) > maxHostDowntime { - oldBlockHeight := newEntry.ScanHistory[0].BlockHeight - newBlockHeight := newEntry.ScanHistory[1].BlockHeight - - timePassed := float64(time.Duration(newBlockHeight-oldBlockHeight) * 10 * time.Minute) - halftime := float64(1 * time.Hour * 24 * 30) - decay := math.Pow(0.5, timePassed/halftime) - - if newEntry.ScanHistory[0].Success { - newEntry.HistoricUptime *= time.Duration(decay) - newEntry.HistoricUptime += time.Duration(timePassed * decay) - } else { - newEntry.HistoricDowntime *= time.Duration(decay) - newEntry.HistoricDowntime += time.Duration(timePassed * decay) - } + decayUptimeOrDowntime(&newEntry, newEntry.ScanHistory[1], newEntry.ScanHistory[0]) newEntry.ScanHistory = newEntry.ScanHistory[1:] }