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.

389 lines
11 KiB

9 years ago
  1. package consensus
  2. import (
  3. "bytes"
  4. "fmt"
  5. "sort"
  6. "testing"
  7. "time"
  8. dbm "github.com/tendermint/go-db"
  9. bc "github.com/tendermint/tendermint/blockchain"
  10. _ "github.com/tendermint/tendermint/config/tendermint_test"
  11. "github.com/tendermint/tendermint/events"
  12. mempl "github.com/tendermint/tendermint/mempool"
  13. "github.com/tendermint/tendermint/proxy"
  14. sm "github.com/tendermint/tendermint/state"
  15. "github.com/tendermint/tendermint/types"
  16. "github.com/tendermint/tmsp/example/golang"
  17. )
  18. var chainID string
  19. var ensureTimeout = time.Duration(2)
  20. func init() {
  21. chainID = config.GetString("chain_id")
  22. }
  23. type validatorStub struct {
  24. Height int
  25. Round int
  26. *types.PrivValidator
  27. }
  28. func NewValidatorStub(privValidator *types.PrivValidator) *validatorStub {
  29. return &validatorStub{
  30. PrivValidator: privValidator,
  31. }
  32. }
  33. func (vs *validatorStub) signVote(voteType byte, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
  34. vote := &types.Vote{
  35. Height: vs.Height,
  36. Round: vs.Round,
  37. Type: voteType,
  38. BlockHash: hash,
  39. BlockPartsHeader: header,
  40. }
  41. err := vs.PrivValidator.SignVote(chainID, vote)
  42. return vote, err
  43. }
  44. // convenienve function for testing
  45. func signVote(vs *validatorStub, voteType byte, hash []byte, header types.PartSetHeader) *types.Vote {
  46. v, err := vs.signVote(voteType, hash, header)
  47. if err != nil {
  48. panic(fmt.Errorf("failed to sign vote: %v", err))
  49. }
  50. return v
  51. }
  52. // create proposal block from cs1 but sign it with vs
  53. func decideProposal(cs1 *ConsensusState, cs2 *validatorStub, height, round int) (proposal *types.Proposal, block *types.Block) {
  54. block, blockParts := cs1.createProposalBlock()
  55. if block == nil { // on error
  56. panic("error creating proposal block")
  57. }
  58. // Make proposal
  59. proposal = types.NewProposal(height, round, blockParts.Header(), cs1.Votes.POLRound())
  60. if err := cs2.SignProposal(chainID, proposal); err != nil {
  61. panic(err)
  62. }
  63. return
  64. }
  65. //-------------------------------------------------------------------------------
  66. // utils
  67. /*
  68. func nilRound(t *testing.T, cs1 *ConsensusState, vss ...*validatorStub) {
  69. cs1.mtx.Lock()
  70. height, round := cs1.Height, cs1.Round
  71. cs1.mtx.Unlock()
  72. waitFor(t, cs1, height, round, RoundStepPrevote)
  73. signAddVoteToFromMany(types.VoteTypePrevote, cs1, nil, cs1.ProposalBlockParts.Header(), vss...)
  74. waitFor(t, cs1, height, round, RoundStepPrecommit)
  75. signAddVoteToFromMany(types.VoteTypePrecommit, cs1, nil, cs1.ProposalBlockParts.Header(), vss...)
  76. waitFor(t, cs1, height, round+1, RoundStepNewRound)
  77. }
  78. */
  79. // NOTE: this switches the propser as far as `perspectiveOf` is concerned,
  80. // but for simplicity we return a block it generated.
  81. func changeProposer(t *testing.T, perspectiveOf *ConsensusState, newProposer *validatorStub) *types.Block {
  82. _, v1 := perspectiveOf.Validators.GetByAddress(perspectiveOf.privValidator.Address)
  83. v1.Accum, v1.VotingPower = 0, 0
  84. if updated := perspectiveOf.Validators.Update(v1); !updated {
  85. t.Fatal("failed to update validator")
  86. }
  87. _, v2 := perspectiveOf.Validators.GetByAddress(newProposer.Address)
  88. v2.Accum, v2.VotingPower = 100, 100
  89. if updated := perspectiveOf.Validators.Update(v2); !updated {
  90. t.Fatal("failed to update validator")
  91. }
  92. // make the proposal
  93. propBlock, _ := perspectiveOf.createProposalBlock()
  94. if propBlock == nil {
  95. t.Fatal("Failed to create proposal block with cs2")
  96. }
  97. return propBlock
  98. }
  99. func fixVotingPower(t *testing.T, cs1 *ConsensusState, addr2 []byte) {
  100. _, v1 := cs1.Validators.GetByAddress(cs1.privValidator.Address)
  101. _, v2 := cs1.Validators.GetByAddress(addr2)
  102. v1.Accum, v1.VotingPower = v2.Accum, v2.VotingPower
  103. if updated := cs1.Validators.Update(v1); !updated {
  104. t.Fatal("failed to update validator")
  105. }
  106. }
  107. func addVoteToFromMany(to *ConsensusState, votes []*types.Vote, froms ...*validatorStub) {
  108. if len(votes) != len(froms) {
  109. panic("len(votes) and len(froms) must match")
  110. }
  111. for i, from := range froms {
  112. addVoteToFrom(to, from, votes[i])
  113. }
  114. }
  115. func addVoteToFrom(to *ConsensusState, from *validatorStub, vote *types.Vote) {
  116. valIndex, _ := to.Validators.GetByAddress(from.PrivValidator.Address)
  117. to.peerMsgQueue <- msgInfo{msg: &VoteMessage{valIndex, vote}}
  118. // added, err := to.TryAddVote(valIndex, vote, "")
  119. /*
  120. if _, ok := err.(*types.ErrVoteConflictingSignature); ok {
  121. // let it fly
  122. } else if !added {
  123. fmt.Println("to, from, vote:", to.Height, from.Height, vote.Height)
  124. panic(fmt.Sprintln("Failed to add vote. Err:", err))
  125. } else if err != nil {
  126. panic(fmt.Sprintln("Failed to add vote:", err))
  127. }*/
  128. }
  129. func signVoteMany(voteType byte, hash []byte, header types.PartSetHeader, vss ...*validatorStub) []*types.Vote {
  130. votes := make([]*types.Vote, len(vss))
  131. for i, vs := range vss {
  132. votes[i] = signVote(vs, voteType, hash, header)
  133. }
  134. return votes
  135. }
  136. // add vote to one cs from another
  137. func signAddVoteToFromMany(voteType byte, to *ConsensusState, hash []byte, header types.PartSetHeader, froms ...*validatorStub) {
  138. for _, from := range froms {
  139. vote := signVote(from, voteType, hash, header)
  140. addVoteToFrom(to, from, vote)
  141. }
  142. }
  143. func signAddVoteToFrom(voteType byte, to *ConsensusState, from *validatorStub, hash []byte, header types.PartSetHeader) *types.Vote {
  144. vote := signVote(from, voteType, hash, header)
  145. addVoteToFrom(to, from, vote)
  146. return vote
  147. }
  148. func ensureNoNewStep(stepCh chan interface{}) {
  149. timeout := time.NewTicker(ensureTimeout * time.Second)
  150. select {
  151. case <-timeout.C:
  152. break
  153. case <-stepCh:
  154. panic("We should be stuck waiting for more votes, not moving to the next step")
  155. }
  156. }
  157. /*
  158. func ensureNoNewStep(t *testing.T, cs *ConsensusState) {
  159. timeout := time.NewTicker(ensureTimeout * time.Second)
  160. select {
  161. case <-timeout.C:
  162. break
  163. case <-cs.NewStepCh():
  164. panic("We should be stuck waiting for more votes, not moving to the next step")
  165. }
  166. }
  167. func ensureNewStep(t *testing.T, cs *ConsensusState) *RoundState {
  168. timeout := time.NewTicker(ensureTimeout * time.Second)
  169. select {
  170. case <-timeout.C:
  171. panic("We should have gone to the next step, not be stuck waiting")
  172. case rs := <-cs.NewStepCh():
  173. return rs
  174. }
  175. }
  176. func waitFor(t *testing.T, cs *ConsensusState, height int, round int, step RoundStepType) {
  177. for {
  178. rs := ensureNewStep(t, cs)
  179. if CompareHRS(rs.Height, rs.Round, rs.Step, height, round, step) < 0 {
  180. continue
  181. } else {
  182. break
  183. }
  184. }
  185. }
  186. */
  187. func incrementHeight(vss ...*validatorStub) {
  188. for _, vs := range vss {
  189. vs.Height += 1
  190. }
  191. }
  192. func incrementRound(vss ...*validatorStub) {
  193. for _, vs := range vss {
  194. vs.Round += 1
  195. }
  196. }
  197. func validatePrevote(t *testing.T, cs *ConsensusState, round int, privVal *validatorStub, blockHash []byte) {
  198. prevotes := cs.Votes.Prevotes(round)
  199. var vote *types.Vote
  200. if vote = prevotes.GetByAddress(privVal.Address); vote == nil {
  201. panic("Failed to find prevote from validator")
  202. }
  203. if blockHash == nil {
  204. if vote.BlockHash != nil {
  205. panic(fmt.Sprintf("Expected prevote to be for nil, got %X", vote.BlockHash))
  206. }
  207. } else {
  208. if !bytes.Equal(vote.BlockHash, blockHash) {
  209. panic(fmt.Sprintf("Expected prevote to be for %X, got %X", blockHash, vote.BlockHash))
  210. }
  211. }
  212. }
  213. func validateLastPrecommit(t *testing.T, cs *ConsensusState, privVal *validatorStub, blockHash []byte) {
  214. votes := cs.LastCommit
  215. var vote *types.Vote
  216. if vote = votes.GetByAddress(privVal.Address); vote == nil {
  217. panic("Failed to find precommit from validator")
  218. }
  219. if !bytes.Equal(vote.BlockHash, blockHash) {
  220. panic(fmt.Sprintf("Expected precommit to be for %X, got %X", blockHash, vote.BlockHash))
  221. }
  222. }
  223. func validatePrecommit(t *testing.T, cs *ConsensusState, thisRound, lockRound int, privVal *validatorStub, votedBlockHash, lockedBlockHash []byte) {
  224. precommits := cs.Votes.Precommits(thisRound)
  225. var vote *types.Vote
  226. if vote = precommits.GetByAddress(privVal.Address); vote == nil {
  227. panic("Failed to find precommit from validator")
  228. }
  229. if votedBlockHash == nil {
  230. if vote.BlockHash != nil {
  231. panic("Expected precommit to be for nil")
  232. }
  233. } else {
  234. if !bytes.Equal(vote.BlockHash, votedBlockHash) {
  235. panic("Expected precommit to be for proposal block")
  236. }
  237. }
  238. if lockedBlockHash == nil {
  239. if cs.LockedRound != lockRound || cs.LockedBlock != nil {
  240. panic(fmt.Sprintf("Expected to be locked on nil at round %d. Got locked at round %d with block %v", lockRound, cs.LockedRound, cs.LockedBlock))
  241. }
  242. } else {
  243. if cs.LockedRound != lockRound || !bytes.Equal(cs.LockedBlock.Hash(), lockedBlockHash) {
  244. panic(fmt.Sprintf("Expected block to be locked on round %d, got %d. Got locked block %X, expected %X", lockRound, cs.LockedRound, cs.LockedBlock.Hash(), lockedBlockHash))
  245. }
  246. }
  247. }
  248. func validatePrevoteAndPrecommit(t *testing.T, cs *ConsensusState, thisRound, lockRound int, privVal *validatorStub, votedBlockHash, lockedBlockHash []byte) {
  249. // verify the prevote
  250. validatePrevote(t, cs, thisRound, privVal, votedBlockHash)
  251. // verify precommit
  252. cs.mtx.Lock()
  253. validatePrecommit(t, cs, thisRound, lockRound, privVal, votedBlockHash, lockedBlockHash)
  254. cs.mtx.Unlock()
  255. }
  256. func simpleConsensusState(nValidators int) (*ConsensusState, []*validatorStub) {
  257. // Get State
  258. state, privVals := randGenesisState(nValidators, false, 10)
  259. // fmt.Println(state.Validators)
  260. vss := make([]*validatorStub, nValidators)
  261. // make consensus state for lead validator
  262. // Get BlockStore
  263. blockDB := dbm.NewMemDB()
  264. blockStore := bc.NewBlockStore(blockDB)
  265. // one for mempool, one for consensus
  266. app := example.NewCounterApplication(false)
  267. appCMem := app.Open()
  268. appCCon := app.Open()
  269. proxyAppCtxMem := proxy.NewLocalAppContext(appCMem)
  270. proxyAppCtxCon := proxy.NewLocalAppContext(appCCon)
  271. // Make Mempool
  272. mempool := mempl.NewMempool(proxyAppCtxMem)
  273. // Make ConsensusReactor
  274. cs := NewConsensusState(state, proxyAppCtxCon, blockStore, mempool)
  275. cs.SetPrivValidator(privVals[0])
  276. evsw := events.NewEventSwitch()
  277. cs.SetEventSwitch(evsw)
  278. evsw.Start()
  279. // start the transition routines
  280. // cs.startRoutines()
  281. for i := 0; i < nValidators; i++ {
  282. vss[i] = NewValidatorStub(privVals[i])
  283. }
  284. // since cs1 starts at 1
  285. incrementHeight(vss[1:]...)
  286. return cs, vss
  287. }
  288. func subscribeToVoter(cs *ConsensusState, addr []byte) chan interface{} {
  289. voteCh0 := cs.evsw.SubscribeToEvent("tester", types.EventStringVote(), 0)
  290. voteCh := make(chan interface{})
  291. go func() {
  292. for {
  293. v := <-voteCh0
  294. vote := v.(*types.EventDataVote)
  295. // we only fire for our own votes
  296. if bytes.Equal(addr, vote.Address) {
  297. voteCh <- v
  298. }
  299. }
  300. }()
  301. return voteCh
  302. }
  303. func randGenesisState(numValidators int, randPower bool, minPower int64) (*sm.State, []*types.PrivValidator) {
  304. db := dbm.NewMemDB()
  305. genDoc, privValidators := randGenesisDoc(numValidators, randPower, minPower)
  306. s0 := sm.MakeGenesisState(db, genDoc)
  307. s0.Save()
  308. return s0, privValidators
  309. }
  310. func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.GenesisDoc, []*types.PrivValidator) {
  311. validators := make([]types.GenesisValidator, numValidators)
  312. privValidators := make([]*types.PrivValidator, numValidators)
  313. for i := 0; i < numValidators; i++ {
  314. val, privVal := types.RandValidator(randPower, minPower)
  315. validators[i] = types.GenesisValidator{
  316. PubKey: val.PubKey,
  317. Amount: val.VotingPower,
  318. }
  319. privValidators[i] = privVal
  320. }
  321. sort.Sort(types.PrivValidatorsByAddress(privValidators))
  322. return &types.GenesisDoc{
  323. GenesisTime: time.Now(),
  324. ChainID: config.GetString("chain_id"),
  325. Validators: validators,
  326. }, privValidators
  327. }
  328. func startTestRound(cs *ConsensusState, height, round int) {
  329. cs.enterNewRound(height, round)
  330. cs.startRoutines(0)
  331. }