You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package consensus
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. func TestSetupRound(t *testing.T) {
  8. cs, privValidators := randConsensusState()
  9. val0 := privValidators[0]
  10. // Add a vote, precommit, and commit by val0.
  11. voteTypes := []byte{types.VoteTypePrevote, types.VoteTypePrecommit, types.VoteTypeCommit}
  12. for _, voteType := range voteTypes {
  13. vote := &types.Vote{Height: 1, Round: 0, Type: voteType} // nil vote
  14. err := val0.SignVote(vote)
  15. if err != nil {
  16. t.Error("Error signing vote: %v", err)
  17. }
  18. cs.AddVote(val0.Address, vote)
  19. }
  20. // Ensure that vote appears in RoundState.
  21. rs0 := cs.GetRoundState()
  22. if vote := rs0.Prevotes.GetByAddress(val0.Address); vote == nil || vote.Type != types.VoteTypePrevote {
  23. t.Errorf("Expected to find prevote but got %v", vote)
  24. }
  25. if vote := rs0.Precommits.GetByAddress(val0.Address); vote == nil || vote.Type != types.VoteTypePrecommit {
  26. t.Errorf("Expected to find precommit but got %v", vote)
  27. }
  28. if vote := rs0.Commits.GetByAddress(val0.Address); vote == nil || vote.Type != types.VoteTypeCommit {
  29. t.Errorf("Expected to find commit but got %v", vote)
  30. }
  31. // Setup round 1 (next round)
  32. cs.SetupNewRound(1, 1)
  33. <-cs.NewStepCh()
  34. // Now the commit should be copied over to prevotes and precommits.
  35. rs1 := cs.GetRoundState()
  36. if vote := rs1.Prevotes.GetByAddress(val0.Address); vote == nil || vote.Type != types.VoteTypeCommit {
  37. t.Errorf("Expected to find commit but got %v", vote)
  38. }
  39. if vote := rs1.Precommits.GetByAddress(val0.Address); vote == nil || vote.Type != types.VoteTypeCommit {
  40. t.Errorf("Expected to find commit but got %v", vote)
  41. }
  42. if vote := rs1.Commits.GetByAddress(val0.Address); vote == nil || vote.Type != types.VoteTypeCommit {
  43. t.Errorf("Expected to find commit but got %v", vote)
  44. }
  45. }
  46. func TestRunActionProposeNoPrivValidator(t *testing.T) {
  47. cs, _ := randConsensusState()
  48. cs.RunActionPropose(1, 0)
  49. rs := cs.GetRoundState()
  50. if rs.Proposal != nil {
  51. t.Error("Expected to make no proposal, since no privValidator")
  52. }
  53. }
  54. func TestRunActionPropose(t *testing.T) {
  55. cs, privValidators := randConsensusState()
  56. val0 := privValidators[0]
  57. cs.SetPrivValidator(val0)
  58. cs.RunActionPropose(1, 0)
  59. rs := cs.GetRoundState()
  60. // Check that Proposal, ProposalBlock, ProposalBlockParts are set.
  61. if rs.Proposal == nil {
  62. t.Error("rs.Proposal should be set")
  63. }
  64. if rs.ProposalBlock == nil {
  65. t.Error("rs.ProposalBlock should be set")
  66. }
  67. if rs.ProposalBlockParts.Total() == 0 {
  68. t.Error("rs.ProposalBlockParts should be set")
  69. }
  70. }
  71. func checkRoundState(t *testing.T, rs *RoundState,
  72. height uint, round uint, step RoundStep) {
  73. if rs.Height != height {
  74. t.Errorf("rs.Height should be %v, got %v", height, rs.Height)
  75. }
  76. if rs.Round != round {
  77. t.Errorf("rs.Round should be %v, got %v", round, rs.Round)
  78. }
  79. if rs.Step != step {
  80. t.Errorf("rs.Step should be %v, got %v", step, rs.Step)
  81. }
  82. }
  83. func TestRunActionPrecommitCommitFinalize(t *testing.T) {
  84. cs, privValidators := randConsensusState()
  85. val0 := privValidators[0]
  86. cs.SetPrivValidator(val0)
  87. cs.RunActionPrecommit(1, 0)
  88. <-cs.NewStepCh() // TODO: test this value too.
  89. if cs.Precommits.GetByAddress(val0.Address) != nil {
  90. t.Errorf("RunActionPrecommit should return nil without a proposal")
  91. }
  92. cs.RunActionPropose(1, 0)
  93. <-cs.NewStepCh() // TODO: test this value too.
  94. cs.RunActionPrecommit(1, 0)
  95. <-cs.NewStepCh() // TODO: test this value too.
  96. if cs.Precommits.GetByAddress(val0.Address) != nil {
  97. t.Errorf("RunActionPrecommit should return nil, not enough prevotes")
  98. }
  99. // Add at least +2/3 prevotes.
  100. for i := 0; i < 7; i++ {
  101. vote := &types.Vote{
  102. Height: 1,
  103. Round: 0,
  104. Type: types.VoteTypePrevote,
  105. BlockHash: cs.ProposalBlock.Hash(),
  106. BlockParts: cs.ProposalBlockParts.Header(),
  107. }
  108. err := privValidators[i].SignVote(vote)
  109. if err != nil {
  110. t.Error("Error signing vote: %v", err)
  111. }
  112. cs.AddVote(privValidators[i].Address, vote)
  113. }
  114. // Test RunActionPrecommit success:
  115. cs.RunActionPrecommit(1, 0)
  116. <-cs.NewStepCh() // TODO: test this value too.
  117. if cs.Precommits.GetByAddress(val0.Address) == nil {
  118. t.Errorf("RunActionPrecommit should have succeeded")
  119. }
  120. checkRoundState(t, cs.GetRoundState(), 1, 0, RoundStepPrecommit)
  121. // Add at least +2/3 precommits.
  122. for i := 0; i < 7; i++ {
  123. if bytes.Equal(privValidators[i].Address, val0.Address) {
  124. if cs.Precommits.GetByAddress(val0.Address) == nil {
  125. t.Errorf("Proposer should already have signed a precommit vote")
  126. }
  127. continue
  128. }
  129. vote := &types.Vote{
  130. Height: 1,
  131. Round: 0,
  132. Type: types.VoteTypePrecommit,
  133. BlockHash: cs.ProposalBlock.Hash(),
  134. BlockParts: cs.ProposalBlockParts.Header(),
  135. }
  136. err := privValidators[i].SignVote(vote)
  137. if err != nil {
  138. t.Error("Error signing vote: %v", err)
  139. }
  140. added, _, err := cs.AddVote(privValidators[i].Address, vote)
  141. if !added || err != nil {
  142. t.Errorf("Error adding precommit: %v", err)
  143. }
  144. }
  145. // Test RunActionCommit success:
  146. cs.RunActionCommit(1)
  147. <-cs.NewStepCh() // TODO: test this value too.
  148. if cs.Commits.GetByAddress(val0.Address) == nil {
  149. t.Errorf("RunActionCommit should have succeeded")
  150. }
  151. checkRoundState(t, cs.GetRoundState(), 1, 0, RoundStepCommit)
  152. // cs.CommitTime should still be zero
  153. if !cs.CommitTime.IsZero() {
  154. t.Errorf("Expected CommitTime to yet be zero")
  155. }
  156. // Add at least +2/3 commits.
  157. for i := 0; i < 7; i++ {
  158. if bytes.Equal(privValidators[i].Address, val0.Address) {
  159. if cs.Commits.GetByAddress(val0.Address) == nil {
  160. t.Errorf("Proposer should already have signed a commit vote")
  161. }
  162. continue
  163. }
  164. vote := &types.Vote{
  165. Height: 1,
  166. Round: uint(i), // Doesn't matter what round
  167. Type: types.VoteTypeCommit,
  168. BlockHash: cs.ProposalBlock.Hash(),
  169. BlockParts: cs.ProposalBlockParts.Header(),
  170. }
  171. err := privValidators[i].SignVote(vote)
  172. if err != nil {
  173. t.Error("Error signing vote: %v", err)
  174. }
  175. added, _, err := cs.AddVote(privValidators[i].Address, vote)
  176. if !added || err != nil {
  177. t.Errorf("Error adding commit: %v", err)
  178. }
  179. }
  180. // Test TryFinalizeCommit:
  181. cs.TryFinalizeCommit(1)
  182. <-cs.NewStepCh() // TODO: test this value too.
  183. checkRoundState(t, cs.GetRoundState(), 2, 0, RoundStepNewHeight)
  184. }