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.

206 lines
6.2 KiB

  1. package blockchain
  2. import (
  3. "testing"
  4. cmn "github.com/tendermint/tmlibs/common"
  5. dbm "github.com/tendermint/tmlibs/db"
  6. "github.com/tendermint/tmlibs/log"
  7. cfg "github.com/tendermint/tendermint/config"
  8. "github.com/tendermint/tendermint/p2p"
  9. "github.com/tendermint/tendermint/proxy"
  10. sm "github.com/tendermint/tendermint/state"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func makeStateAndBlockStore(logger log.Logger) (sm.State, *BlockStore) {
  14. config := cfg.ResetTestRoot("blockchain_reactor_test")
  15. // blockDB := dbm.NewDebugDB("blockDB", dbm.NewMemDB())
  16. // stateDB := dbm.NewDebugDB("stateDB", dbm.NewMemDB())
  17. blockDB := dbm.NewMemDB()
  18. stateDB := dbm.NewMemDB()
  19. blockStore := NewBlockStore(blockDB)
  20. state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile())
  21. if err != nil {
  22. panic(cmn.ErrorWrap(err, "error constructing state from genesis file"))
  23. }
  24. return state, blockStore
  25. }
  26. func newBlockchainReactor(logger log.Logger, maxBlockHeight int64) *BlockchainReactor {
  27. state, blockStore := makeStateAndBlockStore(logger)
  28. // Make the blockchainReactor itself
  29. fastSync := true
  30. var nilApp proxy.AppConnConsensus
  31. blockExec := sm.NewBlockExecutor(dbm.NewMemDB(), log.TestingLogger(), nilApp,
  32. types.MockMempool{}, types.MockEvidencePool{})
  33. bcReactor := NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
  34. bcReactor.SetLogger(logger.With("module", "blockchain"))
  35. // Next: we need to set a switch in order for peers to be added in
  36. bcReactor.Switch = p2p.NewSwitch(cfg.DefaultP2PConfig())
  37. // Lastly: let's add some blocks in
  38. for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
  39. firstBlock := makeBlock(blockHeight, state)
  40. secondBlock := makeBlock(blockHeight+1, state)
  41. firstParts := firstBlock.MakePartSet(state.ConsensusParams.BlockGossip.BlockPartSizeBytes)
  42. blockStore.SaveBlock(firstBlock, firstParts, secondBlock.LastCommit)
  43. }
  44. return bcReactor
  45. }
  46. func TestNoBlockResponse(t *testing.T) {
  47. maxBlockHeight := int64(20)
  48. bcr := newBlockchainReactor(log.TestingLogger(), maxBlockHeight)
  49. bcr.Start()
  50. defer bcr.Stop()
  51. // Add some peers in
  52. peer := newbcrTestPeer(p2p.ID(cmn.RandStr(12)))
  53. bcr.AddPeer(peer)
  54. chID := byte(0x01)
  55. tests := []struct {
  56. height int64
  57. existent bool
  58. }{
  59. {maxBlockHeight + 2, false},
  60. {10, true},
  61. {1, true},
  62. {100, false},
  63. }
  64. // receive a request message from peer,
  65. // wait for our response to be received on the peer
  66. for _, tt := range tests {
  67. reqBlockMsg := &bcBlockRequestMessage{tt.height}
  68. reqBlockBytes := cdc.MustMarshalBinaryBare(reqBlockMsg)
  69. bcr.Receive(chID, peer, reqBlockBytes)
  70. msg := peer.lastBlockchainMessage()
  71. if tt.existent {
  72. if blockMsg, ok := msg.(*bcBlockResponseMessage); !ok {
  73. t.Fatalf("Expected to receive a block response for height %d", tt.height)
  74. } else if blockMsg.Block.Height != tt.height {
  75. t.Fatalf("Expected response to be for height %d, got %d", tt.height, blockMsg.Block.Height)
  76. }
  77. } else {
  78. if noBlockMsg, ok := msg.(*bcNoBlockResponseMessage); !ok {
  79. t.Fatalf("Expected to receive a no block response for height %d", tt.height)
  80. } else if noBlockMsg.Height != tt.height {
  81. t.Fatalf("Expected response to be for height %d, got %d", tt.height, noBlockMsg.Height)
  82. }
  83. }
  84. }
  85. }
  86. /*
  87. // NOTE: This is too hard to test without
  88. // an easy way to add test peer to switch
  89. // or without significant refactoring of the module.
  90. // Alternatively we could actually dial a TCP conn but
  91. // that seems extreme.
  92. func TestBadBlockStopsPeer(t *testing.T) {
  93. maxBlockHeight := int64(20)
  94. bcr := newBlockchainReactor(log.TestingLogger(), maxBlockHeight)
  95. bcr.Start()
  96. defer bcr.Stop()
  97. // Add some peers in
  98. peer := newbcrTestPeer(p2p.ID(cmn.RandStr(12)))
  99. // XXX: This doesn't add the peer to anything,
  100. // so it's hard to check that it's later removed
  101. bcr.AddPeer(peer)
  102. assert.True(t, bcr.Switch.Peers().Size() > 0)
  103. // send a bad block from the peer
  104. // default blocks already dont have commits, so should fail
  105. block := bcr.store.LoadBlock(3)
  106. msg := &bcBlockResponseMessage{Block: block}
  107. peer.Send(BlockchainChannel, struct{ BlockchainMessage }{msg})
  108. ticker := time.NewTicker(time.Millisecond * 10)
  109. timer := time.NewTimer(time.Second * 2)
  110. LOOP:
  111. for {
  112. select {
  113. case <-ticker.C:
  114. if bcr.Switch.Peers().Size() == 0 {
  115. break LOOP
  116. }
  117. case <-timer.C:
  118. t.Fatal("Timed out waiting to disconnect peer")
  119. }
  120. }
  121. }
  122. */
  123. //----------------------------------------------
  124. // utility funcs
  125. func makeTxs(height int64) (txs []types.Tx) {
  126. for i := 0; i < 10; i++ {
  127. txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
  128. }
  129. return txs
  130. }
  131. func makeBlock(height int64, state sm.State) *types.Block {
  132. block, _ := state.MakeBlock(height, makeTxs(height), new(types.Commit))
  133. return block
  134. }
  135. // The Test peer
  136. type bcrTestPeer struct {
  137. cmn.BaseService
  138. id p2p.ID
  139. ch chan interface{}
  140. }
  141. var _ p2p.Peer = (*bcrTestPeer)(nil)
  142. func newbcrTestPeer(id p2p.ID) *bcrTestPeer {
  143. bcr := &bcrTestPeer{
  144. id: id,
  145. ch: make(chan interface{}, 2),
  146. }
  147. bcr.BaseService = *cmn.NewBaseService(nil, "bcrTestPeer", bcr)
  148. return bcr
  149. }
  150. func (tp *bcrTestPeer) lastBlockchainMessage() interface{} { return <-tp.ch }
  151. func (tp *bcrTestPeer) TrySend(chID byte, msgBytes []byte) bool {
  152. var msg BlockchainMessage
  153. err := cdc.UnmarshalBinaryBare(msgBytes, &msg)
  154. if err != nil {
  155. panic(cmn.ErrorWrap(err, "Error while trying to parse a BlockchainMessage"))
  156. }
  157. if _, ok := msg.(*bcStatusResponseMessage); ok {
  158. // Discard status response messages since they skew our results
  159. // We only want to deal with:
  160. // + bcBlockResponseMessage
  161. // + bcNoBlockResponseMessage
  162. } else {
  163. tp.ch <- msg
  164. }
  165. return true
  166. }
  167. func (tp *bcrTestPeer) Send(chID byte, msgBytes []byte) bool { return tp.TrySend(chID, msgBytes) }
  168. func (tp *bcrTestPeer) NodeInfo() p2p.NodeInfo { return p2p.NodeInfo{} }
  169. func (tp *bcrTestPeer) Status() p2p.ConnectionStatus { return p2p.ConnectionStatus{} }
  170. func (tp *bcrTestPeer) ID() p2p.ID { return tp.id }
  171. func (tp *bcrTestPeer) IsOutbound() bool { return false }
  172. func (tp *bcrTestPeer) IsPersistent() bool { return true }
  173. func (tp *bcrTestPeer) Get(s string) interface{} { return s }
  174. func (tp *bcrTestPeer) Set(string, interface{}) {}