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.

339 lines
9.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package blockchain
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "reflect"
  7. "sync/atomic"
  8. "time"
  9. "github.com/tendermint/tendermint/binary"
  10. . "github.com/tendermint/tendermint/common"
  11. "github.com/tendermint/tendermint/events"
  12. "github.com/tendermint/tendermint/p2p"
  13. sm "github.com/tendermint/tendermint/state"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. const (
  17. BlockchainChannel = byte(0x40)
  18. defaultChannelCapacity = 100
  19. defaultSleepIntervalMS = 500
  20. trySyncIntervalMS = 100
  21. // stop syncing when last block's time is
  22. // within this much of the system time.
  23. // stopSyncingDurationMinutes = 10
  24. // ask for best height every 10s
  25. statusUpdateIntervalSeconds = 10
  26. // check if we should switch to consensus reactor
  27. switchToConsensusIntervalSeconds = 10
  28. )
  29. type consensusReactor interface {
  30. // for when we switch from blockchain reactor and fast sync to
  31. // the consensus machine
  32. SwitchToConsensus(*sm.State)
  33. }
  34. // BlockchainReactor handles long-term catchup syncing.
  35. type BlockchainReactor struct {
  36. sw *p2p.Switch
  37. state *sm.State
  38. store *BlockStore
  39. pool *BlockPool
  40. sync bool
  41. requestsCh chan BlockRequest
  42. timeoutsCh chan string
  43. lastBlock *types.Block
  44. quit chan struct{}
  45. running uint32
  46. evsw events.Fireable
  47. }
  48. func NewBlockchainReactor(state *sm.State, store *BlockStore, sync bool) *BlockchainReactor {
  49. if state.LastBlockHeight != store.Height() &&
  50. state.LastBlockHeight != store.Height()-1 { // XXX double check this logic.
  51. panic(Fmt("state (%v) and store (%v) height mismatch", state.LastBlockHeight, store.Height()))
  52. }
  53. requestsCh := make(chan BlockRequest, defaultChannelCapacity)
  54. timeoutsCh := make(chan string, defaultChannelCapacity)
  55. pool := NewBlockPool(
  56. store.Height()+1,
  57. requestsCh,
  58. timeoutsCh,
  59. )
  60. bcR := &BlockchainReactor{
  61. state: state,
  62. store: store,
  63. pool: pool,
  64. sync: sync,
  65. requestsCh: requestsCh,
  66. timeoutsCh: timeoutsCh,
  67. quit: make(chan struct{}),
  68. running: uint32(0),
  69. }
  70. return bcR
  71. }
  72. // Implements Reactor
  73. func (bcR *BlockchainReactor) Start(sw *p2p.Switch) {
  74. if atomic.CompareAndSwapUint32(&bcR.running, 0, 1) {
  75. log.Info("Starting BlockchainReactor")
  76. bcR.sw = sw
  77. if bcR.sync {
  78. bcR.pool.Start()
  79. go bcR.poolRoutine()
  80. }
  81. }
  82. }
  83. // Implements Reactor
  84. func (bcR *BlockchainReactor) Stop() {
  85. if atomic.CompareAndSwapUint32(&bcR.running, 1, 0) {
  86. log.Info("Stopping BlockchainReactor")
  87. close(bcR.quit)
  88. bcR.pool.Stop()
  89. }
  90. }
  91. // Implements Reactor
  92. func (bcR *BlockchainReactor) GetChannels() []*p2p.ChannelDescriptor {
  93. return []*p2p.ChannelDescriptor{
  94. &p2p.ChannelDescriptor{
  95. Id: BlockchainChannel,
  96. Priority: 5,
  97. SendQueueCapacity: 100,
  98. },
  99. }
  100. }
  101. // Implements Reactor
  102. func (bcR *BlockchainReactor) AddPeer(peer *p2p.Peer) {
  103. // Send peer our state.
  104. peer.Send(BlockchainChannel, &bcStatusResponseMessage{bcR.store.Height()})
  105. }
  106. // Implements Reactor
  107. func (bcR *BlockchainReactor) RemovePeer(peer *p2p.Peer, reason interface{}) {
  108. // Remove peer from the pool.
  109. bcR.pool.RemovePeer(peer.Key)
  110. }
  111. // Implements Reactor
  112. func (bcR *BlockchainReactor) Receive(chId byte, src *p2p.Peer, msgBytes []byte) {
  113. _, msg, err := DecodeMessage(msgBytes)
  114. if err != nil {
  115. log.Warn("Error decoding message", "error", err)
  116. return
  117. }
  118. log.Info("Received message", "msg", msg)
  119. switch msg := msg.(type) {
  120. case *bcBlockRequestMessage:
  121. // Got a request for a block. Respond with block if we have it.
  122. block := bcR.store.LoadBlock(msg.Height)
  123. if block != nil {
  124. msg := &bcBlockResponseMessage{Block: block}
  125. queued := src.TrySend(BlockchainChannel, msg)
  126. if !queued {
  127. // queue is full, just ignore.
  128. }
  129. } else {
  130. // TODO peer is asking for things we don't have.
  131. }
  132. case *bcBlockResponseMessage:
  133. // Got a block.
  134. bcR.pool.AddBlock(msg.Block, src.Key)
  135. case *bcStatusRequestMessage:
  136. // Send peer our state.
  137. queued := src.TrySend(BlockchainChannel, &bcStatusResponseMessage{bcR.store.Height()})
  138. if !queued {
  139. // sorry
  140. }
  141. case *bcStatusResponseMessage:
  142. // Got a peer status. Unverified.
  143. bcR.pool.SetPeerHeight(src.Key, msg.Height)
  144. default:
  145. log.Warn(Fmt("Unknown message type %v", reflect.TypeOf(msg)))
  146. }
  147. }
  148. // Handle messages from the poolReactor telling the reactor what to do.
  149. // NOTE: Don't sleep in the FOR_LOOP or otherwise slow it down!
  150. // (Except for the SYNC_LOOP, which is the primary purpose and must be synchronous.)
  151. func (bcR *BlockchainReactor) poolRoutine() {
  152. trySyncTicker := time.NewTicker(trySyncIntervalMS * time.Millisecond)
  153. statusUpdateTicker := time.NewTicker(statusUpdateIntervalSeconds * time.Second)
  154. switchToConsensusTicker := time.NewTicker(switchToConsensusIntervalSeconds * time.Second)
  155. FOR_LOOP:
  156. for {
  157. select {
  158. case request := <-bcR.requestsCh: // chan BlockRequest
  159. peer := bcR.sw.Peers().Get(request.PeerId)
  160. if peer == nil {
  161. // We can't assign the request.
  162. continue FOR_LOOP
  163. }
  164. msg := &bcBlockRequestMessage{request.Height}
  165. queued := peer.TrySend(BlockchainChannel, msg)
  166. if !queued {
  167. // We couldn't make the request, send-queue full.
  168. // The pool handles retries, so just let it go.
  169. continue FOR_LOOP
  170. }
  171. case peerId := <-bcR.timeoutsCh: // chan string
  172. // Peer timed out.
  173. peer := bcR.sw.Peers().Get(peerId)
  174. if peer != nil {
  175. bcR.sw.StopPeerForError(peer, errors.New("BlockchainReactor Timeout"))
  176. }
  177. case _ = <-statusUpdateTicker.C:
  178. // ask for status updates
  179. go bcR.BroadcastStatusRequest()
  180. case _ = <-switchToConsensusTicker.C:
  181. // not thread safe access for numUnassigned and numPending but should be fine
  182. // TODO make threadsafe and use exposed functions
  183. outbound, inbound, _ := bcR.sw.NumPeers()
  184. log.Debug("Consensus ticker", "numUnassigned", bcR.pool.numUnassigned, "numPending", bcR.pool.numPending,
  185. "total", len(bcR.pool.requests), "outbound", outbound, "inbound", inbound)
  186. // NOTE: this condition is very strict right now. may need to weaken
  187. // If all `maxPendingRequests` requests are unassigned
  188. // and we have some peers (say >= 3), then we're caught up
  189. maxPending := bcR.pool.numPending == maxPendingRequests
  190. allUnassigned := bcR.pool.numPending == bcR.pool.numUnassigned
  191. enoughPeers := outbound+inbound >= 3
  192. if maxPending && allUnassigned && enoughPeers {
  193. log.Info("Time to switch to consensus reactor!", "height", bcR.pool.height)
  194. bcR.pool.Stop()
  195. conR := bcR.sw.Reactor("CONSENSUS").(consensusReactor)
  196. conR.SwitchToConsensus(bcR.state)
  197. break FOR_LOOP
  198. }
  199. case _ = <-trySyncTicker.C: // chan time
  200. // This loop can be slow as long as it's doing syncing work.
  201. SYNC_LOOP:
  202. for i := 0; i < 10; i++ {
  203. // See if there are any blocks to sync.
  204. first, second := bcR.pool.PeekTwoBlocks()
  205. //log.Debug("TrySync peeked", "first", first, "second", second)
  206. if first == nil || second == nil {
  207. // We need both to sync the first block.
  208. break SYNC_LOOP
  209. }
  210. firstParts := first.MakePartSet()
  211. firstPartsHeader := firstParts.Header()
  212. // Finally, verify the first block using the second's validation.
  213. err := bcR.state.BondedValidators.VerifyValidation(
  214. bcR.state.ChainID, first.Hash(), firstPartsHeader, first.Height, second.Validation)
  215. if err != nil {
  216. log.Debug("error in validation", "error", err)
  217. bcR.pool.RedoRequest(first.Height)
  218. break SYNC_LOOP
  219. } else {
  220. bcR.pool.PopRequest()
  221. err := sm.ExecBlock(bcR.state, first, firstPartsHeader)
  222. if err != nil {
  223. // TODO This is bad, are we zombie?
  224. panic(Fmt("Failed to process committed block: %v", err))
  225. }
  226. bcR.store.SaveBlock(first, firstParts, second.Validation)
  227. bcR.state.Save()
  228. }
  229. }
  230. continue FOR_LOOP
  231. case <-bcR.quit:
  232. break FOR_LOOP
  233. }
  234. }
  235. }
  236. func (bcR *BlockchainReactor) BroadcastStatusResponse() error {
  237. bcR.sw.Broadcast(BlockchainChannel, &bcStatusResponseMessage{bcR.store.Height()})
  238. return nil
  239. }
  240. func (bcR *BlockchainReactor) BroadcastStatusRequest() error {
  241. bcR.sw.Broadcast(BlockchainChannel, &bcStatusRequestMessage{bcR.store.Height()})
  242. return nil
  243. }
  244. // implements events.Eventable
  245. func (bcR *BlockchainReactor) SetFireable(evsw events.Fireable) {
  246. bcR.evsw = evsw
  247. }
  248. //-----------------------------------------------------------------------------
  249. // Messages
  250. const (
  251. msgTypeBlockRequest = byte(0x10)
  252. msgTypeBlockResponse = byte(0x11)
  253. msgTypeStatusResponse = byte(0x20)
  254. msgTypeStatusRequest = byte(0x21)
  255. )
  256. type BlockchainMessage interface{}
  257. var _ = binary.RegisterInterface(
  258. struct{ BlockchainMessage }{},
  259. binary.ConcreteType{&bcBlockRequestMessage{}, msgTypeBlockRequest},
  260. binary.ConcreteType{&bcBlockResponseMessage{}, msgTypeBlockResponse},
  261. binary.ConcreteType{&bcStatusResponseMessage{}, msgTypeStatusResponse},
  262. binary.ConcreteType{&bcStatusRequestMessage{}, msgTypeStatusRequest},
  263. )
  264. func DecodeMessage(bz []byte) (msgType byte, msg BlockchainMessage, err error) {
  265. msgType = bz[0]
  266. n := new(int64)
  267. r := bytes.NewReader(bz)
  268. msg = binary.ReadBinary(struct{ BlockchainMessage }{}, r, n, &err).(struct{ BlockchainMessage }).BlockchainMessage
  269. return
  270. }
  271. //-------------------------------------
  272. type bcBlockRequestMessage struct {
  273. Height uint
  274. }
  275. func (m *bcBlockRequestMessage) String() string {
  276. return fmt.Sprintf("[bcBlockRequestMessage %v]", m.Height)
  277. }
  278. //-------------------------------------
  279. type bcBlockResponseMessage struct {
  280. Block *types.Block
  281. }
  282. func (m *bcBlockResponseMessage) String() string {
  283. return fmt.Sprintf("[bcBlockResponseMessage %v]", m.Block.Height)
  284. }
  285. //-------------------------------------
  286. type bcStatusRequestMessage struct {
  287. Height uint
  288. }
  289. func (m *bcStatusRequestMessage) String() string {
  290. return fmt.Sprintf("[bcStatusRequestMessage %v]", m.Height)
  291. }
  292. //-------------------------------------
  293. type bcStatusResponseMessage struct {
  294. Height uint
  295. }
  296. func (m *bcStatusResponseMessage) String() string {
  297. return fmt.Sprintf("[bcStatusResponseMessage %v]", m.Height)
  298. }