github.com/NebulousLabs/Sia@v1.3.7/modules/renter/hostdb/hostweight_test.go (about)

     1  package hostdb
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/NebulousLabs/Sia/build"
     8  	"github.com/NebulousLabs/Sia/modules"
     9  	"github.com/NebulousLabs/Sia/types"
    10  )
    11  
    12  func calculateWeightFromUInt64Price(price uint64) (weight types.Currency) {
    13  	hdb := bareHostDB()
    14  	hdb.blockHeight = 0
    15  	var entry modules.HostDBEntry
    16  	entry.Version = build.Version
    17  	entry.RemainingStorage = 250e3
    18  	entry.StoragePrice = types.NewCurrency64(price).Mul(types.SiacoinPrecision).Div64(4032).Div64(1e9)
    19  	return hdb.calculateHostWeight(entry)
    20  }
    21  
    22  func TestHostWeightDistinctPrices(t *testing.T) {
    23  	if testing.Short() {
    24  		t.SkipNow()
    25  	}
    26  	weight1 := calculateWeightFromUInt64Price(300)
    27  	weight2 := calculateWeightFromUInt64Price(301)
    28  	if weight1.Cmp(weight2) <= 0 {
    29  		t.Log(weight1)
    30  		t.Log(weight2)
    31  		t.Error("Weight of expensive host is not the correct value.")
    32  	}
    33  }
    34  
    35  func TestHostWeightIdenticalPrices(t *testing.T) {
    36  	if testing.Short() {
    37  		t.SkipNow()
    38  	}
    39  	weight1 := calculateWeightFromUInt64Price(42)
    40  	weight2 := calculateWeightFromUInt64Price(42)
    41  	if weight1.Cmp(weight2) != 0 {
    42  		t.Error("Weight of identically priced hosts should be equal.")
    43  	}
    44  }
    45  
    46  func TestHostWeightWithOnePricedZero(t *testing.T) {
    47  	if testing.Short() {
    48  		t.SkipNow()
    49  	}
    50  	weight1 := calculateWeightFromUInt64Price(5)
    51  	weight2 := calculateWeightFromUInt64Price(0)
    52  	if weight1.Cmp(weight2) >= 0 {
    53  		t.Error("Zero-priced host should have higher weight than nonzero-priced host.")
    54  	}
    55  }
    56  
    57  func TestHostWeightWithBothPricesZero(t *testing.T) {
    58  	if testing.Short() {
    59  		t.SkipNow()
    60  	}
    61  	weight1 := calculateWeightFromUInt64Price(0)
    62  	weight2 := calculateWeightFromUInt64Price(0)
    63  	if weight1.Cmp(weight2) != 0 {
    64  		t.Error("Weight of two zero-priced hosts should be equal.")
    65  	}
    66  }
    67  
    68  func TestHostWeightCollateralDifferences(t *testing.T) {
    69  	if testing.Short() {
    70  		t.SkipNow()
    71  	}
    72  	hdb := bareHostDB()
    73  	var entry modules.HostDBEntry
    74  	entry.RemainingStorage = 250e3
    75  	entry.StoragePrice = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
    76  	entry.Collateral = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
    77  	entry2 := entry
    78  	entry2.Collateral = types.NewCurrency64(500).Mul(types.SiacoinPrecision)
    79  
    80  	w1 := hdb.calculateHostWeight(entry)
    81  	w2 := hdb.calculateHostWeight(entry2)
    82  	if w1.Cmp(w2) < 0 {
    83  		t.Error("Larger collateral should have more weight")
    84  	}
    85  }
    86  
    87  func TestHostWeightStorageRemainingDifferences(t *testing.T) {
    88  	if testing.Short() {
    89  		t.SkipNow()
    90  	}
    91  	hdb := bareHostDB()
    92  	var entry modules.HostDBEntry
    93  	entry.RemainingStorage = 250e3
    94  	entry.StoragePrice = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
    95  	entry.Collateral = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
    96  
    97  	entry2 := entry
    98  	entry2.RemainingStorage = 50e3
    99  	w1 := hdb.calculateHostWeight(entry)
   100  	w2 := hdb.calculateHostWeight(entry2)
   101  
   102  	if w1.Cmp(w2) < 0 {
   103  		t.Error("Larger storage remaining should have more weight")
   104  	}
   105  }
   106  
   107  func TestHostWeightVersionDifferences(t *testing.T) {
   108  	if testing.Short() {
   109  		t.SkipNow()
   110  	}
   111  	hdb := bareHostDB()
   112  	var entry modules.HostDBEntry
   113  	entry.RemainingStorage = 250e3
   114  	entry.StoragePrice = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   115  	entry.Collateral = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   116  	entry.Version = "v1.0.4"
   117  
   118  	entry2 := entry
   119  	entry2.Version = "v1.0.3"
   120  	w1 := hdb.calculateHostWeight(entry)
   121  	w2 := hdb.calculateHostWeight(entry2)
   122  
   123  	if w1.Cmp(w2) < 0 {
   124  		t.Error("Higher version should have more weight")
   125  	}
   126  }
   127  
   128  func TestHostWeightLifetimeDifferences(t *testing.T) {
   129  	if testing.Short() {
   130  		t.SkipNow()
   131  	}
   132  	hdb := bareHostDB()
   133  	hdb.blockHeight = 10000
   134  	var entry modules.HostDBEntry
   135  	entry.RemainingStorage = 250e3
   136  	entry.StoragePrice = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   137  	entry.Collateral = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   138  	entry.Version = "v1.0.4"
   139  
   140  	entry2 := entry
   141  	entry2.FirstSeen = 8100
   142  	w1 := hdb.calculateHostWeight(entry)
   143  	w2 := hdb.calculateHostWeight(entry2)
   144  
   145  	if w1.Cmp(w2) < 0 {
   146  		t.Error("Been around longer should have more weight")
   147  	}
   148  }
   149  
   150  func TestHostWeightUptimeDifferences(t *testing.T) {
   151  	if testing.Short() {
   152  		t.SkipNow()
   153  	}
   154  	hdb := bareHostDB()
   155  	hdb.blockHeight = 10000
   156  	var entry modules.HostDBEntry
   157  	entry.RemainingStorage = 250e3
   158  	entry.StoragePrice = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   159  	entry.Collateral = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   160  	entry.Version = "v1.0.4"
   161  	entry.ScanHistory = modules.HostDBScans{
   162  		{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
   163  		{Timestamp: time.Now().Add(time.Hour * -80), Success: true},
   164  		{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
   165  		{Timestamp: time.Now().Add(time.Hour * -40), Success: true},
   166  		{Timestamp: time.Now().Add(time.Hour * -20), Success: true},
   167  	}
   168  
   169  	entry2 := entry
   170  	entry2.ScanHistory = modules.HostDBScans{
   171  		{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
   172  		{Timestamp: time.Now().Add(time.Hour * -80), Success: true},
   173  		{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
   174  		{Timestamp: time.Now().Add(time.Hour * -40), Success: true},
   175  		{Timestamp: time.Now().Add(time.Hour * -20), Success: false},
   176  	}
   177  	w1 := hdb.calculateHostWeight(entry)
   178  	w2 := hdb.calculateHostWeight(entry2)
   179  
   180  	if w1.Cmp(w2) < 0 {
   181  		t.Error("Been around longer should have more weight")
   182  	}
   183  }
   184  
   185  func TestHostWeightUptimeDifferences2(t *testing.T) {
   186  	if testing.Short() {
   187  		t.SkipNow()
   188  	}
   189  	hdb := bareHostDB()
   190  	hdb.blockHeight = 10000
   191  	var entry modules.HostDBEntry
   192  	entry.RemainingStorage = 250e3
   193  	entry.StoragePrice = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   194  	entry.Collateral = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   195  	entry.Version = "v1.0.4"
   196  	entry.ScanHistory = modules.HostDBScans{
   197  		{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
   198  		{Timestamp: time.Now().Add(time.Hour * -80), Success: false},
   199  		{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
   200  		{Timestamp: time.Now().Add(time.Hour * -40), Success: true},
   201  		{Timestamp: time.Now().Add(time.Hour * -20), Success: true},
   202  	}
   203  
   204  	entry2 := entry
   205  	entry2.ScanHistory = modules.HostDBScans{
   206  		{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
   207  		{Timestamp: time.Now().Add(time.Hour * -80), Success: true},
   208  		{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
   209  		{Timestamp: time.Now().Add(time.Hour * -40), Success: false},
   210  		{Timestamp: time.Now().Add(time.Hour * -20), Success: true},
   211  	}
   212  	w1 := hdb.calculateHostWeight(entry)
   213  	w2 := hdb.calculateHostWeight(entry2)
   214  
   215  	if w1.Cmp(w2) < 0 {
   216  		t.Errorf("Been around longer should have more weight\n\t%v\n\t%v", w1, w2)
   217  	}
   218  }
   219  
   220  func TestHostWeightUptimeDifferences3(t *testing.T) {
   221  	if testing.Short() {
   222  		t.SkipNow()
   223  	}
   224  	hdb := bareHostDB()
   225  	hdb.blockHeight = 10000
   226  	var entry modules.HostDBEntry
   227  	entry.RemainingStorage = 250e3
   228  	entry.StoragePrice = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   229  	entry.Collateral = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   230  	entry.Version = "v1.0.4"
   231  	entry.ScanHistory = modules.HostDBScans{
   232  		{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
   233  		{Timestamp: time.Now().Add(time.Hour * -80), Success: false},
   234  		{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
   235  		{Timestamp: time.Now().Add(time.Hour * -40), Success: true},
   236  		{Timestamp: time.Now().Add(time.Hour * -20), Success: true},
   237  	}
   238  
   239  	entry2 := entry
   240  	entry2.ScanHistory = modules.HostDBScans{
   241  		{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
   242  		{Timestamp: time.Now().Add(time.Hour * -80), Success: false},
   243  		{Timestamp: time.Now().Add(time.Hour * -60), Success: false},
   244  		{Timestamp: time.Now().Add(time.Hour * -40), Success: true},
   245  		{Timestamp: time.Now().Add(time.Hour * -20), Success: true},
   246  	}
   247  	w1 := hdb.calculateHostWeight(entry)
   248  	w2 := hdb.calculateHostWeight(entry2)
   249  
   250  	if w1.Cmp(w2) < 0 {
   251  		t.Error("Been around longer should have more weight")
   252  	}
   253  }
   254  
   255  func TestHostWeightUptimeDifferences4(t *testing.T) {
   256  	if testing.Short() {
   257  		t.SkipNow()
   258  	}
   259  	hdb := bareHostDB()
   260  	hdb.blockHeight = 10000
   261  	var entry modules.HostDBEntry
   262  	entry.RemainingStorage = 250e3
   263  	entry.StoragePrice = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   264  	entry.Collateral = types.NewCurrency64(1000).Mul(types.SiacoinPrecision)
   265  	entry.Version = "v1.0.4"
   266  	entry.ScanHistory = modules.HostDBScans{
   267  		{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
   268  		{Timestamp: time.Now().Add(time.Hour * -80), Success: true},
   269  		{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
   270  		{Timestamp: time.Now().Add(time.Hour * -40), Success: true},
   271  		{Timestamp: time.Now().Add(time.Hour * -20), Success: false},
   272  	}
   273  
   274  	entry2 := entry
   275  	entry2.ScanHistory = modules.HostDBScans{
   276  		{Timestamp: time.Now().Add(time.Hour * -100), Success: true},
   277  		{Timestamp: time.Now().Add(time.Hour * -80), Success: true},
   278  		{Timestamp: time.Now().Add(time.Hour * -60), Success: true},
   279  		{Timestamp: time.Now().Add(time.Hour * -40), Success: false},
   280  		{Timestamp: time.Now().Add(time.Hour * -20), Success: false},
   281  	}
   282  	w1 := hdb.calculateHostWeight(entry)
   283  	w2 := hdb.calculateHostWeight(entry2)
   284  
   285  	if w1.Cmp(w2) < 0 {
   286  		t.Error("Been around longer should have more weight")
   287  	}
   288  }