github.com/NebulousLabs/Sia@v1.3.7/modules/renter/hostdb/persist_test.go (about) 1 package hostdb 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/NebulousLabs/Sia/modules" 8 ) 9 10 // quitAfterLoadDeps will quit startup in newHostDB 11 type quitAfterLoadDeps struct { 12 modules.ProductionDependencies 13 } 14 15 // Send a disrupt signal to the quitAfterLoad codebreak. 16 func (*quitAfterLoadDeps) Disrupt(s string) bool { 17 if s == "quitAfterLoad" { 18 return true 19 } 20 return false 21 } 22 23 // TestSaveLoad tests that the hostdb can save and load itself. 24 func TestSaveLoad(t *testing.T) { 25 if testing.Short() { 26 t.SkipNow() 27 } 28 t.Parallel() 29 hdbt, err := newHDBTester(t.Name()) 30 if err != nil { 31 t.Fatal(err) 32 } 33 34 // Mine two blocks to put the hdb height at 2. 35 for i := 0; i < 2; i++ { 36 _, err := hdbt.miner.AddBlock() 37 if err != nil { 38 t.Fatal(err) 39 } 40 } 41 42 // Verify that the hdb height is 2. 43 if hdbt.hdb.blockHeight != 2 { 44 t.Fatal("test setup incorrect, hdb height needs to be 2 for remainder of test") 45 } 46 47 // Add fake hosts and a fake consensus change. The fake consensus change 48 // would normally be detected and routed around, but we stunt the loading 49 // process to only load the persistent fields. 50 var host1, host2, host3 modules.HostDBEntry 51 host1.FirstSeen = 1 52 host2.FirstSeen = 2 53 host3.FirstSeen = 3 54 host1.PublicKey.Key = []byte("foo") 55 host2.PublicKey.Key = []byte("bar") 56 host3.PublicKey.Key = []byte("baz") 57 hdbt.hdb.hostTree.Insert(host1) 58 hdbt.hdb.hostTree.Insert(host2) 59 hdbt.hdb.hostTree.Insert(host3) 60 61 // Save, close, and reload. 62 hdbt.hdb.mu.Lock() 63 hdbt.hdb.lastChange = modules.ConsensusChangeID{1, 2, 3} 64 stashedLC := hdbt.hdb.lastChange 65 err = hdbt.hdb.saveSync() 66 hdbt.hdb.mu.Unlock() 67 if err != nil { 68 t.Fatal(err) 69 } 70 err = hdbt.hdb.Close() 71 if err != nil { 72 t.Fatal(err) 73 } 74 hdbt.hdb, err = NewCustomHostDB(hdbt.gateway, hdbt.cs, filepath.Join(hdbt.persistDir, modules.RenterDir), &quitAfterLoadDeps{}) 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 // Last change should have been reloaded. 80 hdbt.hdb.mu.Lock() 81 lastChange := hdbt.hdb.lastChange 82 hdbt.hdb.mu.Unlock() 83 if lastChange != stashedLC { 84 t.Error("wrong consensus change ID was loaded:", hdbt.hdb.lastChange) 85 } 86 87 // Check that AllHosts was loaded. 88 h1, ok0 := hdbt.hdb.hostTree.Select(host1.PublicKey) 89 h2, ok1 := hdbt.hdb.hostTree.Select(host2.PublicKey) 90 h3, ok2 := hdbt.hdb.hostTree.Select(host3.PublicKey) 91 if !ok0 || !ok1 || !ok2 || len(hdbt.hdb.hostTree.All()) != 3 { 92 t.Error("allHosts was not restored properly", ok0, ok1, ok2, len(hdbt.hdb.hostTree.All())) 93 } 94 if h1.FirstSeen != 1 { 95 t.Error("h1 block height loaded incorrectly") 96 } 97 if h2.FirstSeen != 2 { 98 t.Error("h1 block height loaded incorrectly") 99 } 100 if h3.FirstSeen != 2 { 101 t.Error("h1 block height loaded incorrectly") 102 } 103 } 104 105 // TestRescan tests that the hostdb will rescan the blockchain properly, picking 106 // up new hosts which appear in an alternate past. 107 func TestRescan(t *testing.T) { 108 if testing.Short() { 109 t.SkipNow() 110 } 111 _, err := newHDBTester(t.Name()) 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 t.Skip("create two consensus sets with blocks + announcements") 117 }