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.go b/modules/renter.go index 87cbc2f07d..817f59c0a6 100644 --- a/modules/renter.go +++ b/modules/renter.go @@ -119,8 +119,9 @@ type HostDBEntry struct { // HostDBScan represents a single scan event. type HostDBScan struct { - Timestamp time.Time `json:"timestamp"` - Success bool `json:"success"` + Timestamp time.Time `json:"timestamp"` + BlockHeight types.BlockHeight `json:"blockHeight"` + Success bool `json:"success"` } // HostScoreBreakdown provides a piece-by-piece explanation of why a host has 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 6aec7319f9..aaf9702453 100644 --- a/modules/renter/hostdb/hostweight.go +++ b/modules/renter/hostdb/hostweight.go @@ -3,6 +3,7 @@ package hostdb import ( "math" "math/big" + "time" "github.com/NebulousLabs/Sia/build" "github.com/NebulousLabs/Sia/modules" @@ -310,10 +311,9 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 { // host. downtime := entry.HistoricDowntime uptime := entry.HistoricUptime - recentTime := entry.ScanHistory[0].Timestamp - 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 { @@ -322,13 +322,9 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 { // Ignore the unsorted scan entry. continue } - if recentSuccess { - uptime += scan.Timestamp.Sub(recentTime) - } else { - downtime += scan.Timestamp.Sub(recentTime) - } - recentTime = scan.Timestamp - recentSuccess = scan.Success + + decayUptimeOrDowntime(&entry, scan, recentScan) + recentScan = scan } // Sanity check against 0 total time. if uptime == 0 && downtime == 0 { @@ -366,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 7cdd4e6be7..88e9c1b5eb 100644 --- a/modules/renter/hostdb/scan.go +++ b/modules/renter/hostdb/scan.go @@ -177,7 +177,7 @@ func (hdb *HostDB) updateEntry(entry modules.HostDBEntry, netErr error) { // Before appending, make sure that the scan we just performed is // timestamped after the previous scan performed. It may not be if the // system clock has changed. - newEntry.ScanHistory = append(newEntry.ScanHistory, modules.HostDBScan{Timestamp: newTimestamp, Success: netErr == nil}) + newEntry.ScanHistory = append(newEntry.ScanHistory, modules.HostDBScan{Timestamp: newTimestamp, BlockHeight: hdb.blockHeight, Success: netErr == nil}) } // Check whether any of the recent scans demonstrate uptime. The pruning and @@ -193,7 +193,8 @@ func (hdb *HostDB) updateEntry(entry modules.HostDBEntry, netErr error) { // If the host has been offline for too long, delete the host from the // hostdb. Only delete if there have been enough scans over a long enough // period to be confident that the host really is offline for good. - if time.Now().Sub(newEntry.ScanHistory[0].Timestamp) > maxHostDowntime && !recentUptime && len(newEntry.ScanHistory) >= minScans { + + if newEntry.HistoricUptime+newEntry.HistoricDowntime > maxHostDowntime && !recentUptime && len(newEntry.ScanHistory) >= minScans { err := hdb.hostTree.Remove(newEntry.PublicKey) if err != nil { hdb.log.Println("ERROR: unable to remove host newEntry which has had a ton of downtime:", err) @@ -206,12 +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 { - timePassed := newEntry.ScanHistory[1].Timestamp.Sub(newEntry.ScanHistory[0].Timestamp) - if newEntry.ScanHistory[0].Success { - newEntry.HistoricUptime += timePassed - } else { - newEntry.HistoricDowntime += timePassed - } + decayUptimeOrDowntime(&newEntry, newEntry.ScanHistory[1], newEntry.ScanHistory[0]) newEntry.ScanHistory = newEntry.ScanHistory[1:] } diff --git a/modules/renter/hostdb/scan_test.go b/modules/renter/hostdb/scan_test.go index 2695e65c46..77bd4a64d9 100644 --- a/modules/renter/hostdb/scan_test.go +++ b/modules/renter/hostdb/scan_test.go @@ -136,7 +136,7 @@ func TestUpdateEntry(t *testing.T) { if !exists { t.Fatal("Entry did not get inserted into the host tree") } - updatedEntry.ScanHistory = append([]modules.HostDBScan{{Timestamp: time.Now().Add(maxHostDowntime * -1).Add(time.Hour * -1)}}, updatedEntry.ScanHistory...) + updatedEntry.ScanHistory = append([]modules.HostDBScan{{Timestamp: time.Now().Add(maxHostDowntime * -1).Add(time.Hour * -1), BlockHeight: hdbt.hdb.blockHeight}}, updatedEntry.ScanHistory...) err = hdbt.hdb.hostTree.Modify(updatedEntry) if err != nil { t.Fatal(err) @@ -164,7 +164,7 @@ func TestUpdateEntry(t *testing.T) { if !exists { t.Fatal("Entry did not get inserted into the host tree") } - updatedEntry.ScanHistory = append([]modules.HostDBScan{{Success: true, Timestamp: time.Now().Add(time.Hour * 24 * 11 * -1)}}, updatedEntry.ScanHistory...) + updatedEntry.ScanHistory = append([]modules.HostDBScan{{Success: true, BlockHeight: hdbt.hdb.blockHeight, Timestamp: time.Now().Add(time.Hour * 24 * 11 * -1)}}, updatedEntry.ScanHistory...) err = hdbt.hdb.hostTree.Modify(updatedEntry) if err != nil { t.Fatal(err)