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