gitlab.com/jokerrs1/Sia@v1.3.2/modules/host/update_test.go (about) 1 package host 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 "testing" 7 8 "github.com/NebulousLabs/Sia/crypto" 9 "github.com/NebulousLabs/Sia/modules" 10 "github.com/NebulousLabs/Sia/types" 11 "github.com/NebulousLabs/fastrand" 12 ) 13 14 // TestStorageProof checks that the host can create and submit a storage proof. 15 func TestStorageProof(t *testing.T) { 16 if testing.Short() { 17 t.SkipNow() 18 } 19 t.Parallel() 20 ht, err := newHostTester("TestStorageProof") 21 if err != nil { 22 t.Fatal(err) 23 } 24 defer ht.Close() 25 26 // create a file contract 27 fc := types.FileContract{ 28 WindowStart: types.MaturityDelay + 3, 29 WindowEnd: 1000, 30 Payout: types.NewCurrency64(1), 31 UnlockHash: types.UnlockConditions{}.UnlockHash(), 32 ValidProofOutputs: []types.SiacoinOutput{{Value: types.NewCurrency64(1)}, {Value: types.NewCurrency64(0)}}, 33 MissedProofOutputs: []types.SiacoinOutput{{Value: types.NewCurrency64(1)}, {Value: types.NewCurrency64(0)}}, 34 } 35 txnBuilder := ht.wallet.StartTransaction() 36 err = txnBuilder.FundSiacoins(fc.Payout) 37 if err != nil { 38 t.Fatal(err) 39 } 40 txnBuilder.AddFileContract(fc) 41 signedTxnSet, err := txnBuilder.Sign(true) 42 if err != nil { 43 t.Fatal(err) 44 } 45 fcid := signedTxnSet[len(signedTxnSet)-1].FileContractID(0) 46 47 // generate data 48 const dataSize = 777 49 data := fastrand.Bytes(dataSize) 50 root := crypto.MerkleRoot(data) 51 err = ioutil.WriteFile(filepath.Join(ht.host.persistDir, "foo"), data, 0777) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 // create revision 57 rev := types.FileContractRevision{ 58 ParentID: fcid, 59 UnlockConditions: types.UnlockConditions{}, 60 NewFileSize: dataSize, 61 NewWindowStart: fc.WindowStart, 62 NewFileMerkleRoot: root, 63 NewWindowEnd: fc.WindowEnd, 64 NewValidProofOutputs: fc.ValidProofOutputs, 65 NewMissedProofOutputs: fc.MissedProofOutputs, 66 NewRevisionNumber: 1, 67 } 68 _ = types.Transaction{ 69 FileContractRevisions: []types.FileContractRevision{rev}, 70 } 71 72 /* 73 // create obligation 74 obligation := &contractObligation{ 75 ID: fcid, 76 OriginTransaction: types.Transaction{ 77 FileContracts: []types.FileContract{fc}, 78 }, 79 Path: filepath.Join(ht.host.persistDir, "foo"), 80 } 81 ht.host.obligationsByID[fcid] = obligation 82 ht.host.addActionItem(fc.WindowStart+1, obligation) 83 84 // submit both to tpool 85 err = ht.tpool.AcceptTransactionSet(append(signedTxnSet, revTxn)) 86 if err != nil { 87 t.Fatal(err) 88 } 89 _, err = ht.miner.AddBlock() 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 // storage proof will be submitted after mining one more block 95 _, err = ht.miner.AddBlock() 96 if err != nil { 97 t.Fatal(err) 98 } 99 */ 100 } 101 102 // TestInitRescan probes the initRescan function, verifying that it works in 103 // the naive case. The rescan is triggered manually. 104 func TestInitRescan(t *testing.T) { 105 if testing.Short() { 106 t.SkipNow() 107 } 108 t.Parallel() 109 ht, err := newHostTester("TestInitRescan") 110 if err != nil { 111 t.Fatal(err) 112 } 113 defer ht.Close() 114 115 // Check that the host's persistent variables have incorporated the first 116 // few blocks. 117 if ht.host.recentChange == (modules.ConsensusChangeID{}) || ht.host.blockHeight == 0 { 118 t.Fatal("host variables do not indicate that the host is tracking the consensus set correctly") 119 } 120 oldChange := ht.host.recentChange 121 oldHeight := ht.host.blockHeight 122 123 // Corrupt the variables and perform a rescan to see if they reset 124 // correctly. 125 ht.host.recentChange[0]++ 126 ht.host.blockHeight += 100e3 127 ht.cs.Unsubscribe(ht.host) 128 err = ht.host.initRescan() 129 if err != nil { 130 t.Fatal(err) 131 } 132 if oldChange != ht.host.recentChange || oldHeight != ht.host.blockHeight { 133 t.Error("consensus tracking variables were not reset correctly after rescan") 134 } 135 } 136 137 // TestIntegrationAutoRescan checks that a rescan is triggered during New if 138 // the consensus set becomes desynchronized. 139 func TestIntegrationAutoRescan(t *testing.T) { 140 if testing.Short() { 141 t.SkipNow() 142 } 143 t.Parallel() 144 ht, err := newHostTester("TestIntegrationAutoRescan") 145 if err != nil { 146 t.Fatal(err) 147 } 148 defer ht.Close() 149 150 // Check that the host's persistent variables have incorporated the first 151 // few blocks. 152 if ht.host.recentChange == (modules.ConsensusChangeID{}) || ht.host.blockHeight == 0 { 153 t.Fatal("host variables do not indicate that the host is tracking the consensus set correctly") 154 } 155 oldChange := ht.host.recentChange 156 oldHeight := ht.host.blockHeight 157 158 // Corrupt the variables, then close the host. 159 ht.host.recentChange[0]++ 160 ht.host.blockHeight += 100e3 161 err = ht.host.Close() // host saves upon closing 162 if err != nil { 163 t.Fatal(err) 164 } 165 166 // Create a new host and check that the persist variables have correctly 167 // reset. 168 h, err := New(ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir)) 169 if err != nil { 170 t.Fatal(err) 171 } 172 if oldChange != h.recentChange || oldHeight != h.blockHeight { 173 t.Error("consensus tracking variables were not reset correctly after rescan") 174 } 175 176 // Set ht.host to 'h' so that the 'ht.Close()' method will close everything 177 // cleanly. 178 ht.host = h 179 }