From 23024afb5e6eca38d2fc87ef5941a91ca1490138 Mon Sep 17 00:00:00 2001 From: mikkeljuhl Date: Fri, 27 Apr 2018 08:22:16 +0200 Subject: [PATCH] Move updateUptime to scan.go; don't apply decay when calculating host weight --- modules/renter/hostdb/consts.go | 4 ++-- modules/renter/hostdb/hostweight.go | 32 ++++++++++------------------- modules/renter/hostdb/scan.go | 19 ++++++++++++++++- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/modules/renter/hostdb/consts.go b/modules/renter/hostdb/consts.go index d686381225..2fc6b891b0 100644 --- a/modules/renter/hostdb/consts.go +++ b/modules/renter/hostdb/consts.go @@ -34,8 +34,8 @@ const ( // scans start getting compressed. minScans = 12 - // halftimeUpDownTime is the halftime used to decay the host up and downtime - halftimeUpDownTime = 30 * 24 * time.Hour + // uptimeHalflife is the halftime used to decay the host up and downtime + uptimeHalflife = 30 * 24 * time.Hour // recentInteractionWeightLimit caps the number of recent interactions as a // percentage of the historic interactions, to be certain that a large diff --git a/modules/renter/hostdb/hostweight.go b/modules/renter/hostdb/hostweight.go index aaf9702453..1848d70b4f 100644 --- a/modules/renter/hostdb/hostweight.go +++ b/modules/renter/hostdb/hostweight.go @@ -1,13 +1,12 @@ package hostdb import ( - "math" - "math/big" - "time" - "github.com/NebulousLabs/Sia/build" "github.com/NebulousLabs/Sia/modules" "github.com/NebulousLabs/Sia/types" + "math" + "math/big" + "time" ) var ( @@ -323,7 +322,14 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 { continue } - decayUptimeOrDowntime(&entry, scan, recentScan) + blocksPassed := scan.BlockHeight - recentScan.BlockHeight + timePassed := time.Duration(blocksPassed) * 10 * time.Minute + + if recentScan.Success { + uptime += timePassed + } else { + downtime += timePassed + } recentScan = scan } // Sanity check against 0 total time. @@ -362,22 +368,6 @@ 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 13edae56f2..c96988887d 100644 --- a/modules/renter/hostdb/scan.go +++ b/modules/renter/hostdb/scan.go @@ -13,8 +13,25 @@ import ( "github.com/NebulousLabs/Sia/encoding" "github.com/NebulousLabs/Sia/modules" "github.com/NebulousLabs/fastrand" + "math" ) +// updateUptime decays a host's historic uptime or historic downtime. +// It also adds the new block height to the historic uptime or historic downtime. +func updateUptime(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(uptimeHalflife))) + entry.HistoricUptime *= decay + entry.HistoricDowntime *= decay + + if recentScan.Success { + entry.HistoricUptime += timePassed * decay + } else { + entry.HistoricDowntime += timePassed * decay + } +} + // queueScan will add a host to the queue to be scanned. func (hdb *HostDB) queueScan(entry modules.HostDBEntry) { // If this entry is already in the scan pool, can return immediately. @@ -207,7 +224,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 { - decayUptimeOrDowntime(&newEntry, newEntry.ScanHistory[1], newEntry.ScanHistory[0]) + updateUptime(&newEntry, newEntry.ScanHistory[1], newEntry.ScanHistory[0]) newEntry.ScanHistory = newEntry.ScanHistory[1:] }