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.

300 lines
8.7 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. package consensus
  2. import (
  3. "sync"
  4. "testing"
  5. "time"
  6. "github.com/tendermint/tendermint/p2p"
  7. "github.com/tendermint/tendermint/types"
  8. . "github.com/tendermint/tmlibs/common"
  9. "github.com/tendermint/tmlibs/events"
  10. )
  11. func init() {
  12. config = ResetConfig("consensus_byzantine_test")
  13. }
  14. //----------------------------------------------
  15. // byzantine failures
  16. // 4 validators. 1 is byzantine. The other three are partitioned into A (1 val) and B (2 vals).
  17. // byzantine validator sends conflicting proposals into A and B,
  18. // and prevotes/precommits on both of them.
  19. // B sees a commit, A doesn't.
  20. // Byzantine validator refuses to prevote.
  21. // Heal partition and ensure A sees the commit
  22. func TestByzantine(t *testing.T) {
  23. N := 4
  24. logger := consensusLogger()
  25. css := randConsensusNet(N, "consensus_byzantine_test", newMockTickerFunc(false), newCounter)
  26. // give the byzantine validator a normal ticker
  27. css[0].SetTimeoutTicker(NewTimeoutTicker())
  28. switches := make([]*p2p.Switch, N)
  29. p2pLogger := logger.With("module", "p2p")
  30. for i := 0; i < N; i++ {
  31. switches[i] = p2p.NewSwitch(config.P2P)
  32. switches[i].SetLogger(p2pLogger.With("validator", i))
  33. }
  34. reactors := make([]p2p.Reactor, N)
  35. defer func() {
  36. for _, r := range reactors {
  37. if rr, ok := r.(*ByzantineReactor); ok {
  38. rr.reactor.Switch.Stop()
  39. } else {
  40. r.(*ConsensusReactor).Switch.Stop()
  41. }
  42. }
  43. }()
  44. eventChans := make([]chan interface{}, N)
  45. eventLogger := logger.With("module", "events")
  46. for i := 0; i < N; i++ {
  47. if i == 0 {
  48. css[i].privValidator = NewByzantinePrivValidator(css[i].privValidator.(*types.PrivValidator))
  49. // make byzantine
  50. css[i].decideProposal = func(j int) func(int, int) {
  51. return func(height, round int) {
  52. byzantineDecideProposalFunc(t, height, round, css[j], switches[j])
  53. }
  54. }(i)
  55. css[i].doPrevote = func(height, round int) {}
  56. }
  57. eventSwitch := events.NewEventSwitch()
  58. eventSwitch.SetLogger(eventLogger.With("validator", i))
  59. _, err := eventSwitch.Start()
  60. if err != nil {
  61. t.Fatalf("Failed to start switch: %v", err)
  62. }
  63. eventChans[i] = subscribeToEvent(eventSwitch, "tester", types.EventStringNewBlock(), 1)
  64. conR := NewConsensusReactor(css[i], true) // so we dont start the consensus states
  65. conR.SetLogger(logger.With("validator", i))
  66. conR.SetEventSwitch(eventSwitch)
  67. var conRI p2p.Reactor
  68. conRI = conR
  69. if i == 0 {
  70. conRI = NewByzantineReactor(conR)
  71. }
  72. reactors[i] = conRI
  73. }
  74. p2p.MakeConnectedSwitches(config.P2P, N, func(i int, s *p2p.Switch) *p2p.Switch {
  75. // ignore new switch s, we already made ours
  76. switches[i].AddReactor("CONSENSUS", reactors[i])
  77. return switches[i]
  78. }, func(sws []*p2p.Switch, i, j int) {
  79. // the network starts partitioned with globally active adversary
  80. if i != 0 {
  81. return
  82. }
  83. p2p.Connect2Switches(sws, i, j)
  84. })
  85. // start the state machines
  86. byzR := reactors[0].(*ByzantineReactor)
  87. s := byzR.reactor.conS.GetState()
  88. byzR.reactor.SwitchToConsensus(s)
  89. for i := 1; i < N; i++ {
  90. cr := reactors[i].(*ConsensusReactor)
  91. cr.SwitchToConsensus(cr.conS.GetState())
  92. }
  93. // byz proposer sends one block to peers[0]
  94. // and the other block to peers[1] and peers[2].
  95. // note peers and switches order don't match.
  96. peers := switches[0].Peers().List()
  97. ind0 := getSwitchIndex(switches, peers[0])
  98. ind1 := getSwitchIndex(switches, peers[1])
  99. ind2 := getSwitchIndex(switches, peers[2])
  100. // connect the 2 peers in the larger partition
  101. p2p.Connect2Switches(switches, ind1, ind2)
  102. // wait for someone in the big partition to make a block
  103. select {
  104. case <-eventChans[ind2]:
  105. }
  106. t.Log("A block has been committed. Healing partition")
  107. // connect the partitions
  108. p2p.Connect2Switches(switches, ind0, ind1)
  109. p2p.Connect2Switches(switches, ind0, ind2)
  110. // wait till everyone makes the first new block
  111. // (one of them already has)
  112. wg := new(sync.WaitGroup)
  113. wg.Add(2)
  114. for i := 1; i < N-1; i++ {
  115. go func(j int) {
  116. <-eventChans[j]
  117. wg.Done()
  118. }(i)
  119. }
  120. done := make(chan struct{})
  121. go func() {
  122. wg.Wait()
  123. close(done)
  124. }()
  125. tick := time.NewTicker(time.Second * 10)
  126. select {
  127. case <-done:
  128. case <-tick.C:
  129. for i, reactor := range reactors {
  130. t.Log(Fmt("Consensus Reactor %v", i))
  131. t.Log(Fmt("%v", reactor))
  132. }
  133. t.Fatalf("Timed out waiting for all validators to commit first block")
  134. }
  135. }
  136. //-------------------------------
  137. // byzantine consensus functions
  138. func byzantineDecideProposalFunc(t *testing.T, height, round int, cs *ConsensusState, sw *p2p.Switch) {
  139. // byzantine user should create two proposals and try to split the vote.
  140. // Avoid sending on internalMsgQueue and running consensus state.
  141. // Create a new proposal block from state/txs from the mempool.
  142. block1, blockParts1 := cs.createProposalBlock()
  143. polRound, polBlockID := cs.Votes.POLInfo()
  144. proposal1 := types.NewProposal(height, round, blockParts1.Header(), polRound, polBlockID)
  145. cs.privValidator.SignProposal(cs.state.ChainID, proposal1) // byzantine doesnt err
  146. // Create a new proposal block from state/txs from the mempool.
  147. block2, blockParts2 := cs.createProposalBlock()
  148. polRound, polBlockID = cs.Votes.POLInfo()
  149. proposal2 := types.NewProposal(height, round, blockParts2.Header(), polRound, polBlockID)
  150. cs.privValidator.SignProposal(cs.state.ChainID, proposal2) // byzantine doesnt err
  151. block1Hash := block1.Hash()
  152. block2Hash := block2.Hash()
  153. // broadcast conflicting proposals/block parts to peers
  154. peers := sw.Peers().List()
  155. t.Logf("Byzantine: broadcasting conflicting proposals to %d peers", len(peers))
  156. for i, peer := range peers {
  157. if i < len(peers)/2 {
  158. go sendProposalAndParts(height, round, cs, peer, proposal1, block1Hash, blockParts1)
  159. } else {
  160. go sendProposalAndParts(height, round, cs, peer, proposal2, block2Hash, blockParts2)
  161. }
  162. }
  163. }
  164. func sendProposalAndParts(height, round int, cs *ConsensusState, peer *p2p.Peer, proposal *types.Proposal, blockHash []byte, parts *types.PartSet) {
  165. // proposal
  166. msg := &ProposalMessage{Proposal: proposal}
  167. peer.Send(DataChannel, struct{ ConsensusMessage }{msg})
  168. // parts
  169. for i := 0; i < parts.Total(); i++ {
  170. part := parts.GetPart(i)
  171. msg := &BlockPartMessage{
  172. Height: height, // This tells peer that this part applies to us.
  173. Round: round, // This tells peer that this part applies to us.
  174. Part: part,
  175. }
  176. peer.Send(DataChannel, struct{ ConsensusMessage }{msg})
  177. }
  178. // votes
  179. cs.mtx.Lock()
  180. prevote, _ := cs.signVote(types.VoteTypePrevote, blockHash, parts.Header())
  181. precommit, _ := cs.signVote(types.VoteTypePrecommit, blockHash, parts.Header())
  182. cs.mtx.Unlock()
  183. peer.Send(VoteChannel, struct{ ConsensusMessage }{&VoteMessage{prevote}})
  184. peer.Send(VoteChannel, struct{ ConsensusMessage }{&VoteMessage{precommit}})
  185. }
  186. //----------------------------------------
  187. // byzantine consensus reactor
  188. type ByzantineReactor struct {
  189. Service
  190. reactor *ConsensusReactor
  191. }
  192. func NewByzantineReactor(conR *ConsensusReactor) *ByzantineReactor {
  193. return &ByzantineReactor{
  194. Service: conR,
  195. reactor: conR,
  196. }
  197. }
  198. func (br *ByzantineReactor) SetSwitch(s *p2p.Switch) { br.reactor.SetSwitch(s) }
  199. func (br *ByzantineReactor) GetChannels() []*p2p.ChannelDescriptor { return br.reactor.GetChannels() }
  200. func (br *ByzantineReactor) AddPeer(peer *p2p.Peer) {
  201. if !br.reactor.IsRunning() {
  202. return
  203. }
  204. // Create peerState for peer
  205. peerState := NewPeerState(peer)
  206. peer.Data.Set(types.PeerStateKey, peerState)
  207. // Send our state to peer.
  208. // If we're fast_syncing, broadcast a RoundStepMessage later upon SwitchToConsensus().
  209. if !br.reactor.fastSync {
  210. br.reactor.sendNewRoundStepMessages(peer)
  211. }
  212. }
  213. func (br *ByzantineReactor) RemovePeer(peer *p2p.Peer, reason interface{}) {
  214. br.reactor.RemovePeer(peer, reason)
  215. }
  216. func (br *ByzantineReactor) Receive(chID byte, peer *p2p.Peer, msgBytes []byte) {
  217. br.reactor.Receive(chID, peer, msgBytes)
  218. }
  219. //----------------------------------------
  220. // byzantine privValidator
  221. type ByzantinePrivValidator struct {
  222. Address []byte `json:"address"`
  223. types.Signer `json:"-"`
  224. mtx sync.Mutex
  225. }
  226. // Return a priv validator that will sign anything
  227. func NewByzantinePrivValidator(pv *types.PrivValidator) *ByzantinePrivValidator {
  228. return &ByzantinePrivValidator{
  229. Address: pv.Address,
  230. Signer: pv.Signer,
  231. }
  232. }
  233. func (privVal *ByzantinePrivValidator) GetAddress() []byte {
  234. return privVal.Address
  235. }
  236. func (privVal *ByzantinePrivValidator) SignVote(chainID string, vote *types.Vote) error {
  237. privVal.mtx.Lock()
  238. defer privVal.mtx.Unlock()
  239. // Sign
  240. vote.Signature = privVal.Sign(types.SignBytes(chainID, vote))
  241. return nil
  242. }
  243. func (privVal *ByzantinePrivValidator) SignProposal(chainID string, proposal *types.Proposal) error {
  244. privVal.mtx.Lock()
  245. defer privVal.mtx.Unlock()
  246. // Sign
  247. proposal.Signature = privVal.Sign(types.SignBytes(chainID, proposal))
  248. return nil
  249. }
  250. func (privVal *ByzantinePrivValidator) String() string {
  251. return Fmt("PrivValidator{%X}", privVal.Address)
  252. }