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.

192 lines
5.1 KiB

10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
7 years ago
10 years ago
7 years ago
10 years ago
7 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package mempool
  2. import (
  3. "bytes"
  4. "fmt"
  5. "reflect"
  6. "time"
  7. abci "github.com/tendermint/abci/types"
  8. wire "github.com/tendermint/go-wire"
  9. "github.com/tendermint/tmlibs/clist"
  10. "github.com/tendermint/tmlibs/log"
  11. cfg "github.com/tendermint/tendermint/config"
  12. "github.com/tendermint/tendermint/p2p"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. const (
  16. MempoolChannel = byte(0x30)
  17. maxMempoolMessageSize = 1048576 // 1MB TODO make it configurable
  18. peerCatchupSleepIntervalMS = 100 // If peer is behind, sleep this amount
  19. )
  20. // MempoolReactor handles mempool tx broadcasting amongst peers.
  21. type MempoolReactor struct {
  22. p2p.BaseReactor
  23. config *cfg.MempoolConfig
  24. Mempool *Mempool
  25. }
  26. // NewMempoolReactor returns a new MempoolReactor with the given config and mempool.
  27. func NewMempoolReactor(config *cfg.MempoolConfig, mempool *Mempool) *MempoolReactor {
  28. memR := &MempoolReactor{
  29. config: config,
  30. Mempool: mempool,
  31. }
  32. memR.BaseReactor = *p2p.NewBaseReactor("MempoolReactor", memR)
  33. return memR
  34. }
  35. // SetLogger sets the Logger on the reactor and the underlying Mempool.
  36. func (memR *MempoolReactor) SetLogger(l log.Logger) {
  37. memR.Logger = l
  38. memR.Mempool.SetLogger(l)
  39. }
  40. // GetChannels implements Reactor.
  41. // It returns the list of channels for this reactor.
  42. func (memR *MempoolReactor) GetChannels() []*p2p.ChannelDescriptor {
  43. return []*p2p.ChannelDescriptor{
  44. {
  45. ID: MempoolChannel,
  46. Priority: 5,
  47. },
  48. }
  49. }
  50. // AddPeer implements Reactor.
  51. // It starts a broadcast routine ensuring all txs are forwarded to the given peer.
  52. func (memR *MempoolReactor) AddPeer(peer p2p.Peer) {
  53. go memR.broadcastTxRoutine(peer)
  54. }
  55. // RemovePeer implements Reactor.
  56. func (memR *MempoolReactor) RemovePeer(peer p2p.Peer, reason interface{}) {
  57. // broadcast routine checks if peer is gone and returns
  58. }
  59. // Receive implements Reactor.
  60. // It adds any received transactions to the mempool.
  61. func (memR *MempoolReactor) Receive(chID byte, src p2p.Peer, msgBytes []byte) {
  62. _, msg, err := DecodeMessage(msgBytes)
  63. if err != nil {
  64. memR.Logger.Error("Error decoding message", "src", src, "chId", chID, "msg", msg, "err", err, "bytes", msgBytes)
  65. memR.Switch.StopPeerForError(src, err)
  66. return
  67. }
  68. memR.Logger.Debug("Receive", "src", src, "chId", chID, "msg", msg)
  69. switch msg := msg.(type) {
  70. case *TxMessage:
  71. err := memR.Mempool.CheckTx(msg.Tx, nil)
  72. if err != nil {
  73. memR.Logger.Info("Could not check tx", "tx", msg.Tx, "err", err)
  74. }
  75. // broadcasting happens from go routines per peer
  76. default:
  77. memR.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg)))
  78. }
  79. }
  80. // BroadcastTx is an alias for Mempool.CheckTx. Broadcasting itself happens in peer routines.
  81. func (memR *MempoolReactor) BroadcastTx(tx types.Tx, cb func(*abci.Response)) error {
  82. return memR.Mempool.CheckTx(tx, cb)
  83. }
  84. // PeerState describes the state of a peer.
  85. type PeerState interface {
  86. GetHeight() int64
  87. }
  88. // Send new mempool txs to peer.
  89. func (memR *MempoolReactor) broadcastTxRoutine(peer p2p.Peer) {
  90. if !memR.config.Broadcast {
  91. return
  92. }
  93. var next *clist.CElement
  94. for {
  95. // This happens because the CElement we were looking at got garbage
  96. // collected (removed). That is, .NextWait() returned nil. Go ahead and
  97. // start from the beginning.
  98. if next == nil {
  99. select {
  100. case <-memR.Mempool.TxsWaitChan(): // Wait until a tx is available
  101. if next = memR.Mempool.TxsFront(); next == nil {
  102. continue
  103. }
  104. case <-peer.Quit():
  105. return
  106. case <-memR.Quit():
  107. return
  108. }
  109. }
  110. memTx := next.Value.(*mempoolTx)
  111. // make sure the peer is up to date
  112. height := memTx.Height()
  113. if peerState_i := peer.Get(types.PeerStateKey); peerState_i != nil {
  114. peerState := peerState_i.(PeerState)
  115. if peerState.GetHeight() < height-1 { // Allow for a lag of 1 block
  116. time.Sleep(peerCatchupSleepIntervalMS * time.Millisecond)
  117. continue
  118. }
  119. }
  120. // send memTx
  121. msg := &TxMessage{Tx: memTx.tx}
  122. success := peer.Send(MempoolChannel, struct{ MempoolMessage }{msg})
  123. if !success {
  124. time.Sleep(peerCatchupSleepIntervalMS * time.Millisecond)
  125. continue
  126. }
  127. select {
  128. case <-next.NextWaitChan():
  129. // see the start of the for loop for nil check
  130. next = next.Next()
  131. case <-peer.Quit():
  132. return
  133. case <-memR.Quit():
  134. return
  135. }
  136. }
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Messages
  140. const (
  141. msgTypeTx = byte(0x01)
  142. )
  143. // MempoolMessage is a message sent or received by the MempoolReactor.
  144. type MempoolMessage interface{}
  145. var _ = wire.RegisterInterface(
  146. struct{ MempoolMessage }{},
  147. wire.ConcreteType{&TxMessage{}, msgTypeTx},
  148. )
  149. // DecodeMessage decodes a byte-array into a MempoolMessage.
  150. func DecodeMessage(bz []byte) (msgType byte, msg MempoolMessage, err error) {
  151. msgType = bz[0]
  152. n := new(int)
  153. r := bytes.NewReader(bz)
  154. msg = wire.ReadBinary(struct{ MempoolMessage }{}, r, maxMempoolMessageSize, n, &err).(struct{ MempoolMessage }).MempoolMessage
  155. return
  156. }
  157. //-------------------------------------
  158. // TxMessage is a MempoolMessage containing a transaction.
  159. type TxMessage struct {
  160. Tx types.Tx
  161. }
  162. // String returns a string representation of the TxMessage.
  163. func (m *TxMessage) String() string {
  164. return fmt.Sprintf("[TxMessage %v]", m.Tx)
  165. }