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.

395 lines
12 KiB

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