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.

150 lines
4.6 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. 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. blockStore := NewBlockStore(dbm.NewMemDB())
  16. // Get State
  17. state, _ := sm.GetState(dbm.NewMemDB(), config.GenesisFile())
  18. state.SetLogger(logger.With("module", "state"))
  19. state.Save()
  20. return state, blockStore
  21. }
  22. func newBlockchainReactor(logger log.Logger, maxBlockHeight int64) *BlockchainReactor {
  23. state, blockStore := makeStateAndBlockStore(logger)
  24. // Make the blockchainReactor itself
  25. fastSync := true
  26. bcReactor := NewBlockchainReactor(state.Copy(), nil, 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.NewNopLogger(), maxBlockHeight)
  42. bcr.Start()
  43. defer bcr.Stop()
  44. // Add some peers in
  45. peer := newbcrTestPeer(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. for _, tt := range tests {
  58. reqBlockMsg := &bcBlockRequestMessage{tt.height}
  59. reqBlockBytes := wire.BinaryBytes(struct{ BlockchainMessage }{reqBlockMsg})
  60. bcr.Receive(chID, peer, reqBlockBytes)
  61. value := peer.lastValue()
  62. msg := value.(struct{ BlockchainMessage }).BlockchainMessage
  63. if tt.existent {
  64. if blockMsg, ok := msg.(*bcBlockResponseMessage); !ok {
  65. t.Fatalf("Expected to receive a block response for height %d", tt.height)
  66. } else if blockMsg.Block.Height != tt.height {
  67. t.Fatalf("Expected response to be for height %d, got %d", tt.height, blockMsg.Block.Height)
  68. }
  69. } else {
  70. if noBlockMsg, ok := msg.(*bcNoBlockResponseMessage); !ok {
  71. t.Fatalf("Expected to receive a no block response for height %d", tt.height)
  72. } else if noBlockMsg.Height != tt.height {
  73. t.Fatalf("Expected response to be for height %d, got %d", tt.height, noBlockMsg.Height)
  74. }
  75. }
  76. }
  77. }
  78. //----------------------------------------------
  79. // utility funcs
  80. func makeTxs(height int64) (txs []types.Tx) {
  81. for i := 0; i < 10; i++ {
  82. txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
  83. }
  84. return txs
  85. }
  86. func makeBlock(height int64, state *sm.State) *types.Block {
  87. block, _ := state.MakeBlock(height, makeTxs(height), new(types.Commit))
  88. return block
  89. }
  90. // The Test peer
  91. type bcrTestPeer struct {
  92. cmn.Service
  93. key string
  94. ch chan interface{}
  95. }
  96. var _ p2p.Peer = (*bcrTestPeer)(nil)
  97. func newbcrTestPeer(key string) *bcrTestPeer {
  98. return &bcrTestPeer{
  99. Service: cmn.NewBaseService(nil, "bcrTestPeer", nil),
  100. key: key,
  101. ch: make(chan interface{}, 2),
  102. }
  103. }
  104. func (tp *bcrTestPeer) lastValue() interface{} { return <-tp.ch }
  105. func (tp *bcrTestPeer) TrySend(chID byte, value interface{}) bool {
  106. if _, ok := value.(struct{ BlockchainMessage }).BlockchainMessage.(*bcStatusResponseMessage); ok {
  107. // Discard status response messages since they skew our results
  108. // We only want to deal with:
  109. // + bcBlockResponseMessage
  110. // + bcNoBlockResponseMessage
  111. } else {
  112. tp.ch <- value
  113. }
  114. return true
  115. }
  116. func (tp *bcrTestPeer) Send(chID byte, data interface{}) bool { return tp.TrySend(chID, data) }
  117. func (tp *bcrTestPeer) NodeInfo() *p2p.NodeInfo { return nil }
  118. func (tp *bcrTestPeer) Status() p2p.ConnectionStatus { return p2p.ConnectionStatus{} }
  119. func (tp *bcrTestPeer) Key() string { return tp.key }
  120. func (tp *bcrTestPeer) IsOutbound() bool { return false }
  121. func (tp *bcrTestPeer) IsPersistent() bool { return true }
  122. func (tp *bcrTestPeer) Get(s string) interface{} { return s }
  123. func (tp *bcrTestPeer) Set(string, interface{}) {}