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 Apr 3, 2018
1 parent ef9e811 commit d21257b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/siac/hostcmd.go
Expand Up @@ -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"
)

Expand Down
8 changes: 3 additions & 5 deletions cmd/siac/hostdbcmd.go
Expand Up @@ -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
Expand Down
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
3 changes: 3 additions & 0 deletions modules/renter/hostdb/consts.go
Expand Up @@ -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
Expand Down
32 changes: 22 additions & 10 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 @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 4 additions & 8 deletions modules/renter/hostdb/scan.go
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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:]
}

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 d21257b

Please sign in to comment.