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.

350 lines
10 KiB

10 years ago
10 years ago
8 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. cfg "github.com/tendermint/go-config"
  10. "github.com/tendermint/go-p2p"
  11. "github.com/tendermint/go-wire"
  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. config cfg.Config
  39. state *sm.State
  40. proxyAppConn proxy.AppConnConsensus // same as consensus.proxyAppConn
  41. store *BlockStore
  42. pool *BlockPool
  43. fastSync bool
  44. requestsCh chan BlockRequest
  45. timeoutsCh chan string
  46. lastBlock *types.Block
  47. evsw types.EventSwitch
  48. }
  49. func NewBlockchainReactor(config cfg.Config, state *sm.State, proxyAppConn proxy.AppConnConsensus, store *BlockStore, fastSync bool) *BlockchainReactor {
  50. if state.LastBlockHeight == store.Height()-1 {
  51. store.height -= 1 // XXX HACK, make this better
  52. }
  53. if state.LastBlockHeight != store.Height() {
  54. PanicSanity(Fmt("state (%v) and store (%v) height mismatch", state.LastBlockHeight, store.Height()))
  55. }
  56. requestsCh := make(chan BlockRequest, defaultChannelCapacity)
  57. timeoutsCh := make(chan string, defaultChannelCapacity)
  58. pool := NewBlockPool(
  59. store.Height()+1,
  60. requestsCh,
  61. timeoutsCh,
  62. )
  63. bcR := &BlockchainReactor{
  64. config: config,
  65. state: state,
  66. proxyAppConn: proxyAppConn,
  67. store: store,
  68. pool: pool,
  69. fastSync: fastSync,
  70. requestsCh: requestsCh,
  71. timeoutsCh: timeoutsCh,
  72. }
  73. bcR.BaseReactor = *p2p.NewBaseReactor(log, "BlockchainReactor", bcR)
  74. return bcR
  75. }
  76. func (bcR *BlockchainReactor) OnStart() error {
  77. bcR.BaseReactor.OnStart()
  78. if bcR.fastSync {
  79. _, err := bcR.pool.Start()
  80. if err != nil {
  81. return err
  82. }
  83. go bcR.poolRoutine()
  84. }
  85. return nil
  86. }
  87. func (bcR *BlockchainReactor) OnStop() {
  88. bcR.BaseReactor.OnStop()
  89. bcR.pool.Stop()
  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, struct{ BlockchainMessage }{&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.Debug("Receive", "src", src, "chID", chID, "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, struct{ BlockchainMessage }{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(src.Key, msg.Block, len(msgBytes))
  135. case *bcStatusRequestMessage:
  136. // Send peer our state.
  137. queued := src.TrySend(BlockchainChannel, struct{ BlockchainMessage }{&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.Switch.Peers().Get(request.PeerID)
  160. if peer == nil {
  161. continue FOR_LOOP // Peer has since been disconnected.
  162. }
  163. msg := &bcBlockRequestMessage{request.Height}
  164. queued := peer.TrySend(BlockchainChannel, struct{ BlockchainMessage }{msg})
  165. if !queued {
  166. // We couldn't make the request, send-queue full.
  167. // The pool handles timeouts, just let it go.
  168. continue FOR_LOOP
  169. }
  170. case peerID := <-bcR.timeoutsCh: // chan string
  171. // Peer timed out.
  172. peer := bcR.Switch.Peers().Get(peerID)
  173. if peer != nil {
  174. bcR.Switch.StopPeerForError(peer, errors.New("BlockchainReactor Timeout"))
  175. }
  176. case _ = <-statusUpdateTicker.C:
  177. // ask for status updates
  178. go bcR.BroadcastStatusRequest()
  179. case _ = <-switchToConsensusTicker.C:
  180. height, numPending, _ := bcR.pool.GetStatus()
  181. outbound, inbound, _ := bcR.Switch.NumPeers()
  182. log.Info("Consensus ticker", "numPending", numPending, "total", len(bcR.pool.requesters),
  183. "outbound", outbound, "inbound", inbound)
  184. if bcR.pool.IsCaughtUp() {
  185. log.Notice("Time to switch to consensus reactor!", "height", height)
  186. bcR.pool.Stop()
  187. conR := bcR.Switch.Reactor("CONSENSUS").(consensusReactor)
  188. conR.SwitchToConsensus(bcR.state)
  189. break FOR_LOOP
  190. }
  191. case _ = <-trySyncTicker.C: // chan time
  192. // This loop can be slow as long as it's doing syncing work.
  193. SYNC_LOOP:
  194. for i := 0; i < 10; i++ {
  195. // See if there are any blocks to sync.
  196. first, second := bcR.pool.PeekTwoBlocks()
  197. //log.Info("TrySync peeked", "first", first, "second", second)
  198. if first == nil || second == nil {
  199. // We need both to sync the first block.
  200. break SYNC_LOOP
  201. }
  202. firstParts := first.MakePartSet(bcR.config.GetInt("block_part_size"))
  203. firstPartsHeader := firstParts.Header()
  204. // Finally, verify the first block using the second's commit
  205. // NOTE: we can probably make this more efficient, but note that calling
  206. // first.Hash() doesn't verify the tx contents, so MakePartSet() is
  207. // currently necessary.
  208. err := bcR.state.Validators.VerifyCommit(
  209. bcR.state.ChainID, types.BlockID{first.Hash(), firstPartsHeader}, first.Height, second.LastCommit)
  210. if err != nil {
  211. log.Info("error in validation", "error", err)
  212. bcR.pool.RedoRequest(first.Height)
  213. break SYNC_LOOP
  214. } else {
  215. bcR.pool.PopRequest()
  216. // TODO: use ApplyBlock instead of Exec/Commit/SetAppHash/Save
  217. err := bcR.state.ExecBlock(bcR.evsw, bcR.proxyAppConn, first, firstPartsHeader)
  218. if err != nil {
  219. // TODO This is bad, are we zombie?
  220. PanicQ(Fmt("Failed to process committed block (%d:%X): %v", first.Height, first.Hash(), err))
  221. }
  222. // NOTE: we could improve performance if we
  223. // didn't make the app commit to disk every block
  224. // ... but we would need a way to get the hash without it persisting
  225. res := bcR.proxyAppConn.CommitSync()
  226. if res.IsErr() {
  227. // TODO Handle gracefully.
  228. PanicQ(Fmt("Failed to commit block at application: %v", res))
  229. }
  230. bcR.store.SaveBlock(first, firstParts, second.LastCommit)
  231. bcR.state.AppHash = res.Data
  232. bcR.state.Save()
  233. }
  234. }
  235. continue FOR_LOOP
  236. case <-bcR.Quit:
  237. break FOR_LOOP
  238. }
  239. }
  240. }
  241. func (bcR *BlockchainReactor) BroadcastStatusResponse() error {
  242. bcR.Switch.Broadcast(BlockchainChannel, struct{ BlockchainMessage }{&bcStatusResponseMessage{bcR.store.Height()}})
  243. return nil
  244. }
  245. func (bcR *BlockchainReactor) BroadcastStatusRequest() error {
  246. bcR.Switch.Broadcast(BlockchainChannel, struct{ BlockchainMessage }{&bcStatusRequestMessage{bcR.store.Height()}})
  247. return nil
  248. }
  249. // implements events.Eventable
  250. func (bcR *BlockchainReactor) SetEventSwitch(evsw types.EventSwitch) {
  251. bcR.evsw = evsw
  252. }
  253. //-----------------------------------------------------------------------------
  254. // Messages
  255. const (
  256. msgTypeBlockRequest = byte(0x10)
  257. msgTypeBlockResponse = byte(0x11)
  258. msgTypeStatusResponse = byte(0x20)
  259. msgTypeStatusRequest = byte(0x21)
  260. )
  261. type BlockchainMessage interface{}
  262. var _ = wire.RegisterInterface(
  263. struct{ BlockchainMessage }{},
  264. wire.ConcreteType{&bcBlockRequestMessage{}, msgTypeBlockRequest},
  265. wire.ConcreteType{&bcBlockResponseMessage{}, msgTypeBlockResponse},
  266. wire.ConcreteType{&bcStatusResponseMessage{}, msgTypeStatusResponse},
  267. wire.ConcreteType{&bcStatusRequestMessage{}, msgTypeStatusRequest},
  268. )
  269. // TODO: ensure that bz is completely read.
  270. func DecodeMessage(bz []byte) (msgType byte, msg BlockchainMessage, err error) {
  271. msgType = bz[0]
  272. n := int(0)
  273. r := bytes.NewReader(bz)
  274. msg = wire.ReadBinary(struct{ BlockchainMessage }{}, r, maxBlockchainResponseSize, &n, &err).(struct{ BlockchainMessage }).BlockchainMessage
  275. if err != nil && n != len(bz) {
  276. err = errors.New("DecodeMessage() had bytes left over.")
  277. }
  278. return
  279. }
  280. //-------------------------------------
  281. type bcBlockRequestMessage struct {
  282. Height int
  283. }
  284. func (m *bcBlockRequestMessage) String() string {
  285. return fmt.Sprintf("[bcBlockRequestMessage %v]", m.Height)
  286. }
  287. //-------------------------------------
  288. // NOTE: keep up-to-date with maxBlockchainResponseSize
  289. type bcBlockResponseMessage struct {
  290. Block *types.Block
  291. }
  292. func (m *bcBlockResponseMessage) String() string {
  293. return fmt.Sprintf("[bcBlockResponseMessage %v]", m.Block.Height)
  294. }
  295. //-------------------------------------
  296. type bcStatusRequestMessage struct {
  297. Height int
  298. }
  299. func (m *bcStatusRequestMessage) String() string {
  300. return fmt.Sprintf("[bcStatusRequestMessage %v]", m.Height)
  301. }
  302. //-------------------------------------
  303. type bcStatusResponseMessage struct {
  304. Height int
  305. }
  306. func (m *bcStatusResponseMessage) String() string {
  307. return fmt.Sprintf("[bcStatusResponseMessage %v]", m.Height)
  308. }