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.

151 lines
4.8 KiB

  1. package blockchain
  2. import (
  3. "testing"
  4. wire "github.com/tendermint/go-wire"
  5. cmn "github.com/tendermint/tmlibs/common"
  6. dbm "github.com/tendermint/tmlibs/db"
  7. "github.com/tendermint/tmlibs/log"
  8. cfg "github.com/tendermint/tendermint/config"
  9. "github.com/tendermint/tendermint/p2p"
  10. "github.com/tendermint/tendermint/proxy"
  11. sm "github.com/tendermint/tendermint/state"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. func makeStateAndBlockStore(logger log.Logger) (sm.State, *BlockStore) {
  15. config := cfg.ResetTestRoot("blockchain_reactor_test")
  16. blockStore := NewBlockStore(dbm.NewMemDB())
  17. state, _ := sm.LoadStateFromDBOrGenesisFile(dbm.NewMemDB(), config.GenesisFile())
  18. return state, blockStore
  19. }
  20. func newBlockchainReactor(logger log.Logger, maxBlockHeight int64) *BlockchainReactor {
  21. state, blockStore := makeStateAndBlockStore(logger)
  22. // Make the blockchainReactor itself
  23. fastSync := true
  24. var nilApp proxy.AppConnConsensus
  25. blockExec := sm.NewBlockExecutor(dbm.NewMemDB(), log.TestingLogger(), nilApp, types.MockMempool{}, types.MockEvidencePool{})
  26. bcReactor := NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
  27. bcReactor.SetLogger(logger.With("module", "blockchain"))
  28. // Next: we need to set a switch in order for peers to be added in
  29. bcReactor.Switch = p2p.NewSwitch(cfg.DefaultP2PConfig())
  30. // Lastly: let's add some blocks in
  31. for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
  32. firstBlock := makeBlock(blockHeight, state)
  33. secondBlock := makeBlock(blockHeight+1, state)
  34. firstParts := firstBlock.MakePartSet(state.ConsensusParams.BlockGossip.BlockPartSizeBytes)
  35. blockStore.SaveBlock(firstBlock, firstParts, secondBlock.LastCommit)
  36. }
  37. return bcReactor
  38. }
  39. func TestNoBlockMessageResponse(t *testing.T) {
  40. maxBlockHeight := int64(20)
  41. bcr := newBlockchainReactor(log.TestingLogger(), maxBlockHeight)
  42. bcr.Start()
  43. defer bcr.Stop()
  44. // Add some peers in
  45. peer := newbcrTestPeer(p2p.ID(cmn.RandStr(12)))
  46. bcr.AddPeer(peer)
  47. chID := byte(0x01)
  48. tests := []struct {
  49. height int64
  50. existent bool
  51. }{
  52. {maxBlockHeight + 2, false},
  53. {10, true},
  54. {1, true},
  55. {100, false},
  56. }
  57. // receive a request message from peer,
  58. // wait to hear response
  59. for _, tt := range tests {
  60. reqBlockMsg := &bcBlockRequestMessage{tt.height}
  61. reqBlockBytes := wire.BinaryBytes(struct{ BlockchainMessage }{reqBlockMsg})
  62. bcr.Receive(chID, peer, reqBlockBytes)
  63. value := peer.lastValue()
  64. msg := value.(struct{ BlockchainMessage }).BlockchainMessage
  65. if tt.existent {
  66. if blockMsg, ok := msg.(*bcBlockResponseMessage); !ok {
  67. t.Fatalf("Expected to receive a block response for height %d", tt.height)
  68. } else if blockMsg.Block.Height != tt.height {
  69. t.Fatalf("Expected response to be for height %d, got %d", tt.height, blockMsg.Block.Height)
  70. }
  71. } else {
  72. if noBlockMsg, ok := msg.(*bcNoBlockResponseMessage); !ok {
  73. t.Fatalf("Expected to receive a no block response for height %d", tt.height)
  74. } else if noBlockMsg.Height != tt.height {
  75. t.Fatalf("Expected response to be for height %d, got %d", tt.height, noBlockMsg.Height)
  76. }
  77. }
  78. }
  79. }
  80. //----------------------------------------------
  81. // utility funcs
  82. func makeTxs(height int64) (txs []types.Tx) {
  83. for i := 0; i < 10; i++ {
  84. txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
  85. }
  86. return txs
  87. }
  88. func makeBlock(height int64, state sm.State) *types.Block {
  89. block, _ := state.MakeBlock(height, makeTxs(height), new(types.Commit))
  90. return block
  91. }
  92. // The Test peer
  93. type bcrTestPeer struct {
  94. cmn.Service
  95. id p2p.ID
  96. ch chan interface{}
  97. }
  98. var _ p2p.Peer = (*bcrTestPeer)(nil)
  99. func newbcrTestPeer(id p2p.ID) *bcrTestPeer {
  100. return &bcrTestPeer{
  101. Service: cmn.NewBaseService(nil, "bcrTestPeer", nil),
  102. id: id,
  103. ch: make(chan interface{}, 2),
  104. }
  105. }
  106. func (tp *bcrTestPeer) lastValue() interface{} { return <-tp.ch }
  107. func (tp *bcrTestPeer) TrySend(chID byte, value interface{}) bool {
  108. if _, ok := value.(struct{ BlockchainMessage }).BlockchainMessage.(*bcStatusResponseMessage); ok {
  109. // Discard status response messages since they skew our results
  110. // We only want to deal with:
  111. // + bcBlockResponseMessage
  112. // + bcNoBlockResponseMessage
  113. } else {
  114. tp.ch <- value
  115. }
  116. return true
  117. }
  118. func (tp *bcrTestPeer) Send(chID byte, data interface{}) bool { return tp.TrySend(chID, data) }
  119. func (tp *bcrTestPeer) NodeInfo() *p2p.NodeInfo { return nil }
  120. func (tp *bcrTestPeer) Status() p2p.ConnectionStatus { return p2p.ConnectionStatus{} }
  121. func (tp *bcrTestPeer) ID() p2p.ID { return tp.id }
  122. func (tp *bcrTestPeer) IsOutbound() bool { return false }
  123. func (tp *bcrTestPeer) IsPersistent() bool { return true }
  124. func (tp *bcrTestPeer) Get(s string) interface{} { return s }
  125. func (tp *bcrTestPeer) Set(string, interface{}) {}