github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/siatest/consensus/consensus_test.go (about) 1 package consensus 2 3 import ( 4 "reflect" 5 "testing" 6 7 "SiaPrime/node" 8 "SiaPrime/siatest" 9 "SiaPrime/types" 10 ) 11 12 // TestApiHeight checks if the consensus api endpoint works 13 func TestApiHeight(t *testing.T) { 14 if testing.Short() { 15 t.SkipNow() 16 } 17 testDir := consensusTestDir(t.Name()) 18 19 // Create a new server 20 testNode, err := siatest.NewNode(node.AllModules(testDir)) 21 if err != nil { 22 t.Fatal(err) 23 } 24 defer func() { 25 if err := testNode.Close(); err != nil { 26 t.Fatal(err) 27 } 28 }() 29 30 // Send GET request 31 cg, err := testNode.ConsensusGet() 32 if err != nil { 33 t.Fatal(err) 34 } 35 height := cg.Height 36 37 // Mine a block 38 if err := testNode.MineBlock(); err != nil { 39 t.Fatal(err) 40 } 41 42 // Request height again and check if it increased 43 cg, err = testNode.ConsensusGet() 44 if err != nil { 45 t.Fatal(err) 46 } 47 if cg.Height != height+1 { 48 t.Fatal("Height should have increased by 1 block") 49 } 50 } 51 52 // TestConsensusBlocksIDGet tests the /consensus/blocks endpoint 53 func TestConsensusBlocksIDGet(t *testing.T) { 54 if testing.Short() { 55 t.SkipNow() 56 } 57 // Create a testgroup 58 groupParams := siatest.GroupParams{ 59 Hosts: 1, 60 Renters: 1, 61 Miners: 1, 62 } 63 tg, err := siatest.NewGroupFromTemplate(consensusTestDir(t.Name()), groupParams) 64 if err != nil { 65 t.Fatal("Failed to create group: ", err) 66 } 67 defer func() { 68 if err := tg.Close(); err != nil { 69 t.Fatal(err) 70 } 71 }() 72 73 testNode := tg.Miners()[0] 74 75 // Send /consensus request 76 endBlock, err := testNode.ConsensusGet() 77 if err != nil { 78 t.Fatal("Failed to call ConsensusGet():", err) 79 } 80 81 // Loop over blocks and compare 82 var i types.BlockHeight 83 var zeroID types.BlockID 84 for i = 0; i <= endBlock.Height; i++ { 85 cbhg, err := testNode.ConsensusBlocksHeightGet(i) 86 if err != nil { 87 t.Fatal("Failed to retrieve block by height:", err) 88 } 89 cbig, err := testNode.ConsensusBlocksIDGet(cbhg.ID) 90 if err != nil { 91 t.Fatal("Failed to retrieve block by ID:", err) 92 } 93 // Confirm blocks received by both endpoints are the same 94 if !reflect.DeepEqual(cbhg, cbig) { 95 t.Fatal("Blocks not equal") 96 } 97 // Confirm Fields were set properly 98 // Ignore ParentID and MinerPayouts for genisis block 99 if cbig.ParentID == zeroID && i != 0 { 100 t.Fatal("ParentID wasn't set correctly") 101 } 102 if len(cbig.MinerPayouts) == 0 && i != 0 { 103 t.Fatal("Block has no miner payouts") 104 } 105 if cbig.Timestamp == types.Timestamp(0) { 106 t.Fatal("Timestamp wasn't set correctly") 107 } 108 if len(cbig.Transactions) == 0 { 109 t.Fatal("Block doesn't have any transactions even though it should") 110 } 111 112 // Verify IDs 113 for _, tx := range cbhg.Transactions { 114 // Building transaction of type Transaction to use as 115 // comparison for ID creation 116 txn := types.Transaction{ 117 SiacoinInputs: tx.SiacoinInputs, 118 FileContractRevisions: tx.FileContractRevisions, 119 StorageProofs: tx.StorageProofs, 120 SiafundInputs: tx.SiafundInputs, 121 MinerFees: tx.MinerFees, 122 ArbitraryData: tx.ArbitraryData, 123 TransactionSignatures: tx.TransactionSignatures, 124 } 125 for _, sco := range tx.SiacoinOutputs { 126 txn.SiacoinOutputs = append(txn.SiacoinOutputs, types.SiacoinOutput{ 127 Value: sco.Value, 128 UnlockHash: sco.UnlockHash, 129 }) 130 } 131 for i, fc := range tx.FileContracts { 132 txn.FileContracts = append(txn.FileContracts, types.FileContract{ 133 FileSize: fc.FileSize, 134 FileMerkleRoot: fc.FileMerkleRoot, 135 WindowStart: fc.WindowStart, 136 WindowEnd: fc.WindowEnd, 137 Payout: fc.Payout, 138 UnlockHash: fc.UnlockHash, 139 RevisionNumber: fc.RevisionNumber, 140 }) 141 for _, vp := range fc.ValidProofOutputs { 142 txn.FileContracts[i].ValidProofOutputs = append(txn.FileContracts[i].ValidProofOutputs, types.SiacoinOutput{ 143 Value: vp.Value, 144 UnlockHash: vp.UnlockHash, 145 }) 146 } 147 for _, mp := range fc.MissedProofOutputs { 148 txn.FileContracts[i].MissedProofOutputs = append(txn.FileContracts[i].MissedProofOutputs, types.SiacoinOutput{ 149 Value: mp.Value, 150 UnlockHash: mp.UnlockHash, 151 }) 152 } 153 } 154 for _, sfo := range tx.SiafundOutputs { 155 txn.SiafundOutputs = append(txn.SiafundOutputs, types.SiafundOutput{ 156 Value: sfo.Value, 157 UnlockHash: sfo.UnlockHash, 158 ClaimStart: types.ZeroCurrency, 159 }) 160 } 161 162 // Verify SiacoinOutput IDs 163 for i, sco := range tx.SiacoinOutputs { 164 if sco.ID != txn.SiacoinOutputID(uint64(i)) { 165 t.Fatalf("SiacoinOutputID not as expected, got %v expected %v", sco.ID, txn.SiacoinOutputID(uint64(i))) 166 } 167 } 168 169 // FileContracts 170 for i, fc := range tx.FileContracts { 171 // Verify FileContract ID 172 fcid := txn.FileContractID(uint64(i)) 173 if fc.ID != fcid { 174 t.Fatalf("FileContract ID not as expected, got %v expected %v", fc.ID, fcid) 175 } 176 // Verify ValidProof IDs 177 for j, vp := range fc.ValidProofOutputs { 178 if vp.ID != fcid.StorageProofOutputID(types.ProofValid, uint64(j)) { 179 t.Fatalf("File Contract ValidProofOutputID not as expected, got %v expected %v", vp.ID, fcid.StorageProofOutputID(types.ProofValid, uint64(j))) 180 } 181 } 182 // Verify MissedProof IDs 183 for j, mp := range fc.MissedProofOutputs { 184 if mp.ID != fcid.StorageProofOutputID(types.ProofMissed, uint64(j)) { 185 t.Fatalf("File Contract MissedProofOutputID not as expected, got %v expected %v", mp.ID, fcid.StorageProofOutputID(types.ProofMissed, uint64(j))) 186 } 187 } 188 } 189 190 // Verify SiafundOutput IDs 191 for i, sfo := range tx.SiafundOutputs { 192 // Failing, switch back to != 193 if sfo.ID != txn.SiafundOutputID(uint64(i)) { 194 t.Fatalf("SiafundOutputID not as expected, got %v expected %v", sfo.ID, txn.SiafundOutputID(uint64(i))) 195 } 196 } 197 } 198 } 199 }