github.com/Synthesix/Sia@v1.3.3-0.20180413141344-f863baeed3ca/modules/explorer/update_test.go (about) 1 package explorer 2 3 import ( 4 "testing" 5 6 "github.com/Synthesix/Sia/modules" 7 "github.com/Synthesix/Sia/types" 8 ) 9 10 func (et *explorerTester) currentFacts() (facts modules.BlockFacts, exists bool) { 11 var height types.BlockHeight 12 err := et.explorer.db.View(dbGetInternal(internalBlockHeight, &height)) 13 if err != nil { 14 exists = false 15 return 16 } 17 return et.explorer.BlockFacts(height) 18 } 19 20 // TestIntegrationExplorerFileContractMetrics checks that the siacoin 21 // transfer volume metric is working correctly. 22 func TestIntegrationExplorerFileContractMetrics(t *testing.T) { 23 if testing.Short() { 24 t.Skip() 25 } 26 27 et, err := createExplorerTester(t.Name()) 28 if err != nil { 29 t.Fatal(err) 30 } 31 // Propel explorer tester past the hardfork height. 32 for i := 0; i < 10; i++ { 33 _, err = et.miner.AddBlock() 34 if err != nil { 35 t.Fatal(err) 36 } 37 } 38 facts, ok := et.currentFacts() 39 if !ok { 40 t.Fatal("couldn't get current facts") 41 } 42 if !facts.ActiveContractCost.IsZero() { 43 t.Error("fresh explorer has nonzero active contract cost") 44 } 45 if facts.ActiveContractCount != 0 { 46 t.Error("active contract count should initialize to zero") 47 } 48 if !facts.ActiveContractSize.IsZero() { 49 t.Error("active contract size should initialize to zero") 50 } 51 52 // Put a file contract into the chain, and check that the explorer 53 // correctly does all of the counting. 54 builder := et.wallet.StartTransaction() 55 builder.FundSiacoins(types.NewCurrency64(5e9)) 56 fcOutputs := []types.SiacoinOutput{{Value: types.NewCurrency64(4805e6)}} 57 fc := types.FileContract{ 58 FileSize: 5e3, 59 WindowStart: et.cs.Height() + 2, 60 WindowEnd: et.cs.Height() + 3, 61 Payout: types.NewCurrency64(5e9), 62 ValidProofOutputs: fcOutputs, 63 MissedProofOutputs: fcOutputs, 64 } 65 _ = builder.AddFileContract(fc) 66 txns, err := builder.Sign(true) 67 if err != nil { 68 t.Fatal(err) 69 } 70 err = et.tpool.AcceptTransactionSet(txns) 71 if err != nil { 72 t.Fatal(err) 73 } 74 _, err = et.miner.AddBlock() 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 // Check that the stats have updated to represent the file contract. 80 facts, ok = et.currentFacts() 81 if !ok { 82 t.Fatal("couldn't get current facts") 83 } 84 if !facts.ActiveContractCost.Equals64(5e9) { 85 t.Error("active resources providing wrong file contract cost") 86 } 87 if facts.ActiveContractCount != 1 { 88 t.Error("active contract count does not read correctly") 89 } 90 if !facts.ActiveContractSize.Equals64(5e3) { 91 t.Error("active contract size is not correctly reported") 92 } 93 if !facts.TotalContractCost.Equals64(5e9) { 94 t.Error("total cost is not tallied correctly") 95 } 96 if facts.FileContractCount != 1 { 97 t.Error("total contract count is not accurate") 98 } 99 if !facts.TotalContractSize.Equals64(5e3) { 100 t.Error("total contract size is not accurate") 101 } 102 103 // Put a second file into the explorer to check that multiple files are 104 // handled well. 105 builder = et.wallet.StartTransaction() 106 builder.FundSiacoins(types.NewCurrency64(1e9)) 107 fcOutputs = []types.SiacoinOutput{{Value: types.NewCurrency64(961e6)}} 108 fc = types.FileContract{ 109 FileSize: 15e3, 110 WindowStart: et.cs.Height() + 2, 111 WindowEnd: et.cs.Height() + 3, 112 Payout: types.NewCurrency64(1e9), 113 ValidProofOutputs: fcOutputs, 114 MissedProofOutputs: fcOutputs, 115 } 116 _ = builder.AddFileContract(fc) 117 txns, err = builder.Sign(true) 118 if err != nil { 119 t.Fatal(err) 120 } 121 err = et.tpool.AcceptTransactionSet(txns) 122 if err != nil { 123 t.Fatal(err) 124 } 125 _, err = et.miner.AddBlock() 126 if err != nil { 127 t.Fatal(err) 128 } 129 130 // Check that the stats have updated to represent the file contracts. 131 facts, ok = et.currentFacts() 132 if !ok { 133 t.Fatal("couldn't get current facts") 134 } 135 if !facts.ActiveContractCost.Equals64(6e9) { 136 t.Error("active resources providing wrong file contract cost") 137 } 138 if facts.ActiveContractCount != 2 { 139 t.Error("active contract count does not read correctly") 140 } 141 if !facts.ActiveContractSize.Equals64(20e3) { 142 t.Error("active contract size is not correctly reported") 143 } 144 if !facts.TotalContractCost.Equals64(6e9) { 145 t.Error("total cost is not tallied correctly") 146 } 147 if facts.FileContractCount != 2 { 148 t.Error("total contract count is not accurate") 149 } 150 if !facts.TotalContractSize.Equals64(20e3) { 151 t.Error("total contract size is not accurate") 152 } 153 154 // Expire the first file contract but not the second. 155 _, err = et.miner.AddBlock() 156 if err != nil { 157 t.Fatal(err) 158 } 159 160 // Check that the stats have updated to reflect the expired file contract. 161 facts, ok = et.currentFacts() 162 if !ok { 163 t.Fatal("couldn't get current facts") 164 } 165 if !facts.ActiveContractCost.Equals64(1e9) { 166 t.Error("active resources providing wrong file contract cost", facts.ActiveContractCost) 167 } 168 if facts.ActiveContractCount != 1 { 169 t.Error("active contract count does not read correctly") 170 } 171 if !facts.ActiveContractSize.Equals64(15e3) { 172 t.Error("active contract size is not correctly reported") 173 } 174 if !facts.TotalContractCost.Equals64(6e9) { 175 t.Error("total cost is not tallied correctly") 176 } 177 if facts.FileContractCount != 2 { 178 t.Error("total contract count is not accurate") 179 } 180 if !facts.TotalContractSize.Equals64(20e3) { 181 t.Error("total contract size is not accurate") 182 } 183 184 // Reorg the block explorer to a blank state, see that all of the file 185 // contract statistics got removed. 186 187 // TODO: broken by new block facts model 188 189 // err = et.reorgToBlank() 190 // if err != nil { 191 // t.Fatal(err) 192 // } 193 // facts, ok = et.currentFacts() 194 // if !ok { 195 // t.Fatal("couldn't get current facts") 196 // } 197 // if !facts.ActiveContractCost.IsZero() { 198 // t.Error("post reorg active contract cost should be zero, got", facts.ActiveContractCost) 199 // } 200 // if facts.ActiveContractCount != 0 { 201 // t.Error("post reorg active contract count should be zero, got", facts.ActiveContractCount) 202 // } 203 // if !facts.TotalContractCost.IsZero() { 204 // t.Error("post reorg total contract cost should be zero, got", facts.TotalContractCost) 205 // } 206 // if facts.FileContractCount != 0 { 207 // t.Error("post reorg file contract count should be zero, got", facts.FileContractCount) 208 // } 209 }