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