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.

157 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. 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.Params.BlockGossipParams.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. prevHash := state.LastBlockID.Hash
  88. prevParts := types.PartSetHeader{}
  89. valHash := state.Validators.Hash()
  90. prevBlockID := types.BlockID{prevHash, prevParts}
  91. block, _ := types.MakeBlock(height, "test_chain", makeTxs(height),
  92. state.LastBlockTotalTx, new(types.Commit),
  93. prevBlockID, valHash, state.AppHash,
  94. state.Params.BlockGossipParams.BlockPartSizeBytes)
  95. return block
  96. }
  97. // The Test peer
  98. type bcrTestPeer struct {
  99. cmn.Service
  100. key string
  101. ch chan interface{}
  102. }
  103. var _ p2p.Peer = (*bcrTestPeer)(nil)
  104. func newbcrTestPeer(key string) *bcrTestPeer {
  105. return &bcrTestPeer{
  106. Service: cmn.NewBaseService(nil, "bcrTestPeer", nil),
  107. key: key,
  108. ch: make(chan interface{}, 2),
  109. }
  110. }
  111. func (tp *bcrTestPeer) lastValue() interface{} { return <-tp.ch }
  112. func (tp *bcrTestPeer) TrySend(chID byte, value interface{}) bool {
  113. if _, ok := value.(struct{ BlockchainMessage }).BlockchainMessage.(*bcStatusResponseMessage); ok {
  114. // Discard status response messages since they skew our results
  115. // We only want to deal with:
  116. // + bcBlockResponseMessage
  117. // + bcNoBlockResponseMessage
  118. } else {
  119. tp.ch <- value
  120. }
  121. return true
  122. }
  123. func (tp *bcrTestPeer) Send(chID byte, data interface{}) bool { return tp.TrySend(chID, data) }
  124. func (tp *bcrTestPeer) NodeInfo() *p2p.NodeInfo { return nil }
  125. func (tp *bcrTestPeer) Status() p2p.ConnectionStatus { return p2p.ConnectionStatus{} }
  126. func (tp *bcrTestPeer) Key() string { return tp.key }
  127. func (tp *bcrTestPeer) IsOutbound() bool { return false }
  128. func (tp *bcrTestPeer) IsPersistent() bool { return true }
  129. func (tp *bcrTestPeer) Get(s string) interface{} { return s }
  130. func (tp *bcrTestPeer) Set(string, interface{}) {}