Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Use BlockHeight to calculate uptime/downtime instead of timestamps.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkeljuhl committed Mar 28, 2018
1 parent ef9e811 commit 6a3d643
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
5 changes: 3 additions & 2 deletions modules/renter.go
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions modules/renter/hostdb/hostweight.go
Expand Up @@ -3,6 +3,7 @@ package hostdb
import (
"math"
"math/big"
"time"

"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/modules"
Expand Down Expand Up @@ -311,6 +312,7 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
downtime := entry.HistoricDowntime
uptime := entry.HistoricUptime
recentTime := entry.ScanHistory[0].Timestamp
recentBlockHeight := entry.ScanHistory[0].BlockHeight
recentSuccess := entry.ScanHistory[0].Success
for _, scan := range entry.ScanHistory[1:] {
if recentTime.After(scan.Timestamp) {
Expand All @@ -322,12 +324,19 @@ func (hdb *HostDB) uptimeAdjustments(entry modules.HostDBEntry) float64 {
// Ignore the unsorted scan entry.
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 += scan.Timestamp.Sub(recentTime)
uptime += time.Duration(timePassed * decay)
} else {
downtime += scan.Timestamp.Sub(recentTime)
downtime += time.Duration(timePassed * decay)
}
recentTime = scan.Timestamp
recentBlockHeight = scan.BlockHeight
recentSuccess = scan.Success
}
// Sanity check against 0 total time.
Expand Down
20 changes: 15 additions & 5 deletions modules/renter/hostdb/scan.go
Expand Up @@ -5,6 +5,7 @@ package hostdb
// settings of the hosts.

import (
"math"
"net"
"time"

Expand Down Expand Up @@ -177,7 +178,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
Expand All @@ -193,7 +194,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)
Expand All @@ -206,11 +208,19 @@ 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)
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 += timePassed
newEntry.HistoricUptime *= time.Duration(decay)
newEntry.HistoricUptime += time.Duration(timePassed * decay)
} else {
newEntry.HistoricDowntime += timePassed
newEntry.HistoricDowntime *= time.Duration(decay)
newEntry.HistoricDowntime += time.Duration(timePassed * decay)
}
newEntry.ScanHistory = newEntry.ScanHistory[1:]
}
Expand Down
4 changes: 2 additions & 2 deletions modules/renter/hostdb/scan_test.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6a3d643

Please sign in to comment.