github.com/Synthesix/Sia@v1.3.3-0.20180413141344-f863baeed3ca/siatest/consensus/consensus_test.go (about) 1 package consensus 2 3 import ( 4 "testing" 5 6 "github.com/Synthesix/Sia/node" 7 "github.com/Synthesix/Sia/siatest" 8 "github.com/Synthesix/Sia/types" 9 ) 10 11 // TestApiHeight checks if the consensus api endpoint works 12 func TestApiHeight(t *testing.T) { 13 if testing.Short() { 14 t.SkipNow() 15 } 16 testdir, err := siatest.TestDir(t.Name()) 17 if err != nil { 18 t.Fatal(err) 19 } 20 21 // Create a new server 22 testNode, err := siatest.NewNode(node.AllModules(testdir)) 23 if err != nil { 24 t.Fatal(err) 25 } 26 defer func() { 27 if err := testNode.Close(); err != nil { 28 t.Fatal(err) 29 } 30 }() 31 32 // Send GET request 33 cg, err := testNode.ConsensusGet() 34 if err != nil { 35 t.Fatal(err) 36 } 37 height := cg.Height 38 39 // Mine a block 40 if err := testNode.MineBlock(); err != nil { 41 t.Fatal(err) 42 } 43 44 // Request height again and check if it increased 45 cg, err = testNode.ConsensusGet() 46 if err != nil { 47 t.Fatal(err) 48 } 49 if cg.Height != height+1 { 50 t.Fatal("Height should have increased by 1 block") 51 } 52 } 53 54 // TestConsensusBlocksIDGet tests the /consensus/blocks endpoint 55 func TestConsensusBlocksIDGet(t *testing.T) { 56 if testing.Short() { 57 t.SkipNow() 58 } 59 testdir, err := siatest.TestDir(t.Name()) 60 if err != nil { 61 t.Fatal(err) 62 } 63 64 // Create a new server 65 testNode, err := siatest.NewNode(node.AllModules(testdir)) 66 if err != nil { 67 t.Fatal(err) 68 } 69 defer func() { 70 if err := testNode.Close(); err != nil { 71 t.Fatal(err) 72 } 73 }() 74 75 // Send /consensus request 76 cg, err := testNode.ConsensusGet() 77 if err != nil { 78 t.Fatal(err) 79 } 80 81 // Get block by id 82 block, err := testNode.ConsensusBlocksIDGet(cg.CurrentBlock) 83 if err != nil { 84 t.Fatal("Failed to retrieve block", err) 85 } 86 // Make sure all of the fields are initialized and not empty 87 var zeroID types.BlockID 88 if block.ParentID == zeroID { 89 t.Fatal("ParentID wasn't set correctly") 90 } 91 if block.Timestamp == types.Timestamp(0) { 92 t.Fatal("Timestamp wasn't set correctly") 93 } 94 if len(block.MinerPayouts) == 0 { 95 t.Fatal("Block has no miner payouts") 96 } 97 if len(block.Transactions) == 0 { 98 t.Fatal("Block doesn't have any transactions even though it should") 99 } 100 101 // Get same block by height 102 block2, err := testNode.ConsensusBlocksHeightGet(cg.Height) 103 if err != nil { 104 t.Fatal("Failed to retrieve block", err) 105 } 106 // block and block2 should be the same 107 if block.ParentID != block2.ParentID { 108 t.Fatal("ParentIDs don't match") 109 } 110 if block.Timestamp != block2.Timestamp { 111 t.Fatal("Timestamps don't match") 112 } 113 if len(block.MinerPayouts) != len(block2.MinerPayouts) { 114 t.Fatal("MinerPayouts don't match") 115 } 116 if len(block.Transactions) != len(block2.Transactions) { 117 t.Fatal("Transactions don't match") 118 } 119 }