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.

158 lines
4.9 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.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. 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.LastConsensusHash,
  95. state.Params.BlockGossip.BlockPartSizeBytes)
  96. return block
  97. }
  98. // The Test peer
  99. type bcrTestPeer struct {
  100. cmn.Service
  101. key string
  102. ch chan interface{}
  103. }
  104. var _ p2p.Peer = (*bcrTestPeer)(nil)
  105. func newbcrTestPeer(key string) *bcrTestPeer {
  106. return &bcrTestPeer{
  107. Service: cmn.NewBaseService(nil, "bcrTestPeer", nil),
  108. key: key,
  109. ch: make(chan interface{}, 2),
  110. }
  111. }
  112. func (tp *bcrTestPeer) lastValue() interface{} { return <-tp.ch }
  113. func (tp *bcrTestPeer) TrySend(chID byte, value interface{}) bool {
  114. if _, ok := value.(struct{ BlockchainMessage }).BlockchainMessage.(*bcStatusResponseMessage); ok {
  115. // Discard status response messages since they skew our results
  116. // We only want to deal with:
  117. // + bcBlockResponseMessage
  118. // + bcNoBlockResponseMessage
  119. } else {
  120. tp.ch <- value
  121. }
  122. return true
  123. }
  124. func (tp *bcrTestPeer) Send(chID byte, data interface{}) bool { return tp.TrySend(chID, data) }
  125. func (tp *bcrTestPeer) NodeInfo() *p2p.NodeInfo { return nil }
  126. func (tp *bcrTestPeer) Status() p2p.ConnectionStatus { return p2p.ConnectionStatus{} }
  127. func (tp *bcrTestPeer) Key() string { return tp.key }
  128. func (tp *bcrTestPeer) IsOutbound() bool { return false }
  129. func (tp *bcrTestPeer) IsPersistent() bool { return true }
  130. func (tp *bcrTestPeer) Get(s string) interface{} { return s }
  131. func (tp *bcrTestPeer) Set(string, interface{}) {}