handler.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package eth
  17. import (
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "math"
  22. "math/big"
  23. "sync"
  24. "sync/atomic"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/consensus"
  28. "github.com/ethereum/go-ethereum/consensus/misc"
  29. "github.com/ethereum/go-ethereum/core"
  30. "github.com/ethereum/go-ethereum/core/types"
  31. "github.com/ethereum/go-ethereum/eth/downloader"
  32. "github.com/ethereum/go-ethereum/eth/fetcher"
  33. "github.com/ethereum/go-ethereum/ethdb"
  34. "github.com/ethereum/go-ethereum/event"
  35. "github.com/ethereum/go-ethereum/log"
  36. "github.com/ethereum/go-ethereum/p2p"
  37. "github.com/ethereum/go-ethereum/p2p/discover"
  38. "github.com/ethereum/go-ethereum/params"
  39. "github.com/ethereum/go-ethereum/rlp"
  40. )
  41. const (
  42. softResponseLimit = 2 * 1024 * 1024 // Target maximum size of returned blocks, headers or node data.
  43. estHeaderRlpSize = 500 // Approximate size of an RLP encoded block header
  44. )
  45. var (
  46. daoChallengeTimeout = 15 * time.Second // Time allowance for a node to reply to the DAO handshake challenge
  47. )
  48. // errIncompatibleConfig is returned if the requested protocols and configs are
  49. // not compatible (low protocol version restrictions and high requirements).
  50. var errIncompatibleConfig = errors.New("incompatible configuration")
  51. func errResp(code errCode, format string, v ...interface{}) error {
  52. return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
  53. }
  54. type ProtocolManager struct {
  55. networkId int
  56. fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
  57. acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
  58. txpool txPool
  59. blockchain *core.BlockChain
  60. chaindb ethdb.Database
  61. chainconfig *params.ChainConfig
  62. maxPeers int
  63. downloader *downloader.Downloader
  64. fetcher *fetcher.Fetcher
  65. peers *peerSet
  66. SubProtocols []p2p.Protocol
  67. eventMux *event.TypeMux
  68. txSub *event.TypeMuxSubscription
  69. minedBlockSub *event.TypeMuxSubscription
  70. // channels for fetcher, syncer, txsyncLoop
  71. newPeerCh chan *peer
  72. txsyncCh chan *txsync
  73. quitSync chan struct{}
  74. noMorePeers chan struct{}
  75. lesServer LesServer
  76. // wait group is used for graceful shutdowns during downloading
  77. // and processing
  78. wg sync.WaitGroup
  79. }
  80. // NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
  81. // with the ethereum network.
  82. func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, networkId int, maxPeers int, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) {
  83. // Create the protocol manager with the base fields
  84. manager := &ProtocolManager{
  85. networkId: networkId,
  86. eventMux: mux,
  87. txpool: txpool,
  88. blockchain: blockchain,
  89. chaindb: chaindb,
  90. chainconfig: config,
  91. maxPeers: maxPeers,
  92. peers: newPeerSet(),
  93. newPeerCh: make(chan *peer),
  94. noMorePeers: make(chan struct{}),
  95. txsyncCh: make(chan *txsync),
  96. quitSync: make(chan struct{}),
  97. }
  98. // Figure out whether to allow fast sync or not
  99. if mode == downloader.FastSync && blockchain.CurrentBlock().NumberU64() > 0 {
  100. log.Warn("Blockchain not empty, fast sync disabled")
  101. mode = downloader.FullSync
  102. }
  103. if mode == downloader.FastSync {
  104. manager.fastSync = uint32(1)
  105. }
  106. // Initiate a sub-protocol for every implemented version we can handle
  107. manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions))
  108. for i, version := range ProtocolVersions {
  109. // Skip protocol version if incompatible with the mode of operation
  110. if mode == downloader.FastSync && version < eth63 {
  111. continue
  112. }
  113. // Compatible; initialise the sub-protocol
  114. version := version // Closure for the run
  115. manager.SubProtocols = append(manager.SubProtocols, p2p.Protocol{
  116. Name: ProtocolName,
  117. Version: version,
  118. Length: ProtocolLengths[i],
  119. Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
  120. peer := manager.newPeer(int(version), p, rw)
  121. select {
  122. case manager.newPeerCh <- peer:
  123. manager.wg.Add(1)
  124. defer manager.wg.Done()
  125. return manager.handle(peer)
  126. case <-manager.quitSync:
  127. return p2p.DiscQuitting
  128. }
  129. },
  130. NodeInfo: func() interface{} {
  131. return manager.NodeInfo()
  132. },
  133. PeerInfo: func(id discover.NodeID) interface{} {
  134. if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
  135. return p.Info()
  136. }
  137. return nil
  138. },
  139. })
  140. }
  141. if len(manager.SubProtocols) == 0 {
  142. return nil, errIncompatibleConfig
  143. }
  144. // Construct the different synchronisation mechanisms
  145. manager.downloader = downloader.New(mode, chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeaderByHash,
  146. blockchain.GetBlockByHash, blockchain.CurrentHeader, blockchain.CurrentBlock, blockchain.CurrentFastBlock, blockchain.FastSyncCommitHead,
  147. blockchain.GetTdByHash, blockchain.InsertHeaderChain, manager.blockchain.InsertChain, blockchain.InsertReceiptChain, blockchain.Rollback,
  148. manager.removePeer)
  149. validator := func(header *types.Header) error {
  150. return engine.VerifyHeader(blockchain, header, true)
  151. }
  152. heighter := func() uint64 {
  153. return blockchain.CurrentBlock().NumberU64()
  154. }
  155. inserter := func(blocks types.Blocks) (int, error) {
  156. atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
  157. return manager.blockchain.InsertChain(blocks)
  158. }
  159. manager.fetcher = fetcher.New(blockchain.GetBlockByHash, validator, manager.BroadcastBlock, heighter, inserter, manager.removePeer)
  160. return manager, nil
  161. }
  162. func (pm *ProtocolManager) removePeer(id string) {
  163. // Short circuit if the peer was already removed
  164. peer := pm.peers.Peer(id)
  165. if peer == nil {
  166. return
  167. }
  168. log.Debug("Removing Ethereum peer", "peer", id)
  169. // Unregister the peer from the downloader and Ethereum peer set
  170. pm.downloader.UnregisterPeer(id)
  171. if err := pm.peers.Unregister(id); err != nil {
  172. log.Error("Peer removal failed", "peer", id, "err", err)
  173. }
  174. // Hard disconnect at the networking layer
  175. if peer != nil {
  176. peer.Peer.Disconnect(p2p.DiscUselessPeer)
  177. }
  178. }
  179. func (pm *ProtocolManager) Start() {
  180. // broadcast transactions
  181. pm.txSub = pm.eventMux.Subscribe(core.TxPreEvent{})
  182. go pm.txBroadcastLoop()
  183. // broadcast mined blocks
  184. pm.minedBlockSub = pm.eventMux.Subscribe(core.NewMinedBlockEvent{})
  185. go pm.minedBroadcastLoop()
  186. // start sync handlers
  187. go pm.syncer()
  188. go pm.txsyncLoop()
  189. }
  190. func (pm *ProtocolManager) Stop() {
  191. log.Info("Stopping Ethereum protocol")
  192. pm.txSub.Unsubscribe() // quits txBroadcastLoop
  193. pm.minedBlockSub.Unsubscribe() // quits blockBroadcastLoop
  194. // Quit the sync loop.
  195. // After this send has completed, no new peers will be accepted.
  196. pm.noMorePeers <- struct{}{}
  197. // Quit fetcher, txsyncLoop.
  198. close(pm.quitSync)
  199. // Disconnect existing sessions.
  200. // This also closes the gate for any new registrations on the peer set.
  201. // sessions which are already established but not added to pm.peers yet
  202. // will exit when they try to register.
  203. pm.peers.Close()
  204. // Wait for all peer handler goroutines and the loops to come down.
  205. pm.wg.Wait()
  206. log.Info("Ethereum protocol stopped")
  207. }
  208. func (pm *ProtocolManager) newPeer(pv int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
  209. return newPeer(pv, p, newMeteredMsgWriter(rw))
  210. }
  211. // handle is the callback invoked to manage the life cycle of an eth peer. When
  212. // this function terminates, the peer is disconnected.
  213. func (pm *ProtocolManager) handle(p *peer) error {
  214. if pm.peers.Len() >= pm.maxPeers {
  215. return p2p.DiscTooManyPeers
  216. }
  217. p.Log().Debug("Ethereum peer connected", "name", p.Name())
  218. // Execute the Ethereum handshake
  219. td, head, genesis := pm.blockchain.Status()
  220. if err := p.Handshake(pm.networkId, td, head, genesis); err != nil {
  221. p.Log().Debug("Ethereum handshake failed", "err", err)
  222. return err
  223. }
  224. if rw, ok := p.rw.(*meteredMsgReadWriter); ok {
  225. rw.Init(p.version)
  226. }
  227. // Register the peer locally
  228. if err := pm.peers.Register(p); err != nil {
  229. p.Log().Error("Ethereum peer registration failed", "err", err)
  230. return err
  231. }
  232. defer pm.removePeer(p.id)
  233. // Register the peer in the downloader. If the downloader considers it banned, we disconnect
  234. if err := pm.downloader.RegisterPeer(p.id, p.version, p.Head, p.RequestHeadersByHash, p.RequestHeadersByNumber, p.RequestBodies, p.RequestReceipts, p.RequestNodeData); err != nil {
  235. return err
  236. }
  237. // Propagate existing transactions. new transactions appearing
  238. // after this will be sent via broadcasts.
  239. pm.syncTransactions(p)
  240. // If we're DAO hard-fork aware, validate any remote peer with regard to the hard-fork
  241. if daoBlock := pm.chainconfig.DAOForkBlock; daoBlock != nil {
  242. // Request the peer's DAO fork header for extra-data validation
  243. if err := p.RequestHeadersByNumber(daoBlock.Uint64(), 1, 0, false); err != nil {
  244. return err
  245. }
  246. // Start a timer to disconnect if the peer doesn't reply in time
  247. p.forkDrop = time.AfterFunc(daoChallengeTimeout, func() {
  248. p.Log().Debug("Timed out DAO fork-check, dropping")
  249. pm.removePeer(p.id)
  250. })
  251. // Make sure it's cleaned up if the peer dies off
  252. defer func() {
  253. if p.forkDrop != nil {
  254. p.forkDrop.Stop()
  255. p.forkDrop = nil
  256. }
  257. }()
  258. }
  259. // main loop. handle incoming messages.
  260. for {
  261. if err := pm.handleMsg(p); err != nil {
  262. p.Log().Debug("Ethereum message handling failed", "err", err)
  263. return err
  264. }
  265. }
  266. }
  267. // handleMsg is invoked whenever an inbound message is received from a remote
  268. // peer. The remote connection is torn down upon returning any error.
  269. func (pm *ProtocolManager) handleMsg(p *peer) error {
  270. // Read the next message from the remote peer, and ensure it's fully consumed
  271. msg, err := p.rw.ReadMsg()
  272. if err != nil {
  273. return err
  274. }
  275. if msg.Size > ProtocolMaxMsgSize {
  276. return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize)
  277. }
  278. defer msg.Discard()
  279. // Handle the message depending on its contents
  280. switch {
  281. case msg.Code == StatusMsg:
  282. // Status messages should never arrive after the handshake
  283. return errResp(ErrExtraStatusMsg, "uncontrolled status message")
  284. // Block header query, collect the requested headers and reply
  285. case msg.Code == GetBlockHeadersMsg:
  286. // Decode the complex header query
  287. var query getBlockHeadersData
  288. if err := msg.Decode(&query); err != nil {
  289. return errResp(ErrDecode, "%v: %v", msg, err)
  290. }
  291. hashMode := query.Origin.Hash != (common.Hash{})
  292. // Gather headers until the fetch or network limits is reached
  293. var (
  294. bytes common.StorageSize
  295. headers []*types.Header
  296. unknown bool
  297. )
  298. for !unknown && len(headers) < int(query.Amount) && bytes < softResponseLimit && len(headers) < downloader.MaxHeaderFetch {
  299. // Retrieve the next header satisfying the query
  300. var origin *types.Header
  301. if hashMode {
  302. origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash)
  303. } else {
  304. origin = pm.blockchain.GetHeaderByNumber(query.Origin.Number)
  305. }
  306. if origin == nil {
  307. break
  308. }
  309. number := origin.Number.Uint64()
  310. headers = append(headers, origin)
  311. bytes += estHeaderRlpSize
  312. // Advance to the next header of the query
  313. switch {
  314. case query.Origin.Hash != (common.Hash{}) && query.Reverse:
  315. // Hash based traversal towards the genesis block
  316. for i := 0; i < int(query.Skip)+1; i++ {
  317. if header := pm.blockchain.GetHeader(query.Origin.Hash, number); header != nil {
  318. query.Origin.Hash = header.ParentHash
  319. number--
  320. } else {
  321. unknown = true
  322. break
  323. }
  324. }
  325. case query.Origin.Hash != (common.Hash{}) && !query.Reverse:
  326. // Hash based traversal towards the leaf block
  327. var (
  328. current = origin.Number.Uint64()
  329. next = current + query.Skip + 1
  330. )
  331. if next <= current {
  332. infos, _ := json.MarshalIndent(p.Peer.Info(), "", " ")
  333. p.Log().Warn("GetBlockHeaders skip overflow attack", "current", current, "skip", query.Skip, "next", next, "attacker", infos)
  334. unknown = true
  335. } else {
  336. if header := pm.blockchain.GetHeaderByNumber(next); header != nil {
  337. if pm.blockchain.GetBlockHashesFromHash(header.Hash(), query.Skip+1)[query.Skip] == query.Origin.Hash {
  338. query.Origin.Hash = header.Hash()
  339. } else {
  340. unknown = true
  341. }
  342. } else {
  343. unknown = true
  344. }
  345. }
  346. case query.Reverse:
  347. // Number based traversal towards the genesis block
  348. if query.Origin.Number >= query.Skip+1 {
  349. query.Origin.Number -= (query.Skip + 1)
  350. } else {
  351. unknown = true
  352. }
  353. case !query.Reverse:
  354. // Number based traversal towards the leaf block
  355. query.Origin.Number += (query.Skip + 1)
  356. }
  357. }
  358. return p.SendBlockHeaders(headers)
  359. case msg.Code == BlockHeadersMsg:
  360. // A batch of headers arrived to one of our previous requests
  361. var headers []*types.Header
  362. if err := msg.Decode(&headers); err != nil {
  363. return errResp(ErrDecode, "msg %v: %v", msg, err)
  364. }
  365. // If no headers were received, but we're expending a DAO fork check, maybe it's that
  366. if len(headers) == 0 && p.forkDrop != nil {
  367. // Possibly an empty reply to the fork header checks, sanity check TDs
  368. verifyDAO := true
  369. // If we already have a DAO header, we can check the peer's TD against it. If
  370. // the peer's ahead of this, it too must have a reply to the DAO check
  371. if daoHeader := pm.blockchain.GetHeaderByNumber(pm.chainconfig.DAOForkBlock.Uint64()); daoHeader != nil {
  372. if _, td := p.Head(); td.Cmp(pm.blockchain.GetTd(daoHeader.Hash(), daoHeader.Number.Uint64())) >= 0 {
  373. verifyDAO = false
  374. }
  375. }
  376. // If we're seemingly on the same chain, disable the drop timer
  377. if verifyDAO {
  378. p.Log().Debug("Seems to be on the same side of the DAO fork")
  379. p.forkDrop.Stop()
  380. p.forkDrop = nil
  381. return nil
  382. }
  383. }
  384. // Filter out any explicitly requested headers, deliver the rest to the downloader
  385. filter := len(headers) == 1
  386. if filter {
  387. // If it's a potential DAO fork check, validate against the rules
  388. if p.forkDrop != nil && pm.chainconfig.DAOForkBlock.Cmp(headers[0].Number) == 0 {
  389. // Disable the fork drop timer
  390. p.forkDrop.Stop()
  391. p.forkDrop = nil
  392. // Validate the header and either drop the peer or continue
  393. if err := misc.VerifyDAOHeaderExtraData(pm.chainconfig, headers[0]); err != nil {
  394. p.Log().Debug("Verified to be on the other side of the DAO fork, dropping")
  395. return err
  396. }
  397. p.Log().Debug("Verified to be on the same side of the DAO fork")
  398. return nil
  399. }
  400. // Irrelevant of the fork checks, send the header to the fetcher just in case
  401. headers = pm.fetcher.FilterHeaders(headers, time.Now())
  402. }
  403. if len(headers) > 0 || !filter {
  404. err := pm.downloader.DeliverHeaders(p.id, headers)
  405. if err != nil {
  406. log.Debug("Failed to deliver headers", "err", err)
  407. }
  408. }
  409. case msg.Code == GetBlockBodiesMsg:
  410. // Decode the retrieval message
  411. msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
  412. if _, err := msgStream.List(); err != nil {
  413. return err
  414. }
  415. // Gather blocks until the fetch or network limits is reached
  416. var (
  417. hash common.Hash
  418. bytes int
  419. bodies []rlp.RawValue
  420. )
  421. for bytes < softResponseLimit && len(bodies) < downloader.MaxBlockFetch {
  422. // Retrieve the hash of the next block
  423. if err := msgStream.Decode(&hash); err == rlp.EOL {
  424. break
  425. } else if err != nil {
  426. return errResp(ErrDecode, "msg %v: %v", msg, err)
  427. }
  428. // Retrieve the requested block body, stopping if enough was found
  429. if data := pm.blockchain.GetBodyRLP(hash); len(data) != 0 {
  430. bodies = append(bodies, data)
  431. bytes += len(data)
  432. }
  433. }
  434. return p.SendBlockBodiesRLP(bodies)
  435. case msg.Code == BlockBodiesMsg:
  436. // A batch of block bodies arrived to one of our previous requests
  437. var request blockBodiesData
  438. if err := msg.Decode(&request); err != nil {
  439. return errResp(ErrDecode, "msg %v: %v", msg, err)
  440. }
  441. // Deliver them all to the downloader for queuing
  442. trasactions := make([][]*types.Transaction, len(request))
  443. uncles := make([][]*types.Header, len(request))
  444. for i, body := range request {
  445. trasactions[i] = body.Transactions
  446. uncles[i] = body.Uncles
  447. }
  448. // Filter out any explicitly requested bodies, deliver the rest to the downloader
  449. filter := len(trasactions) > 0 || len(uncles) > 0
  450. if filter {
  451. trasactions, uncles = pm.fetcher.FilterBodies(trasactions, uncles, time.Now())
  452. }
  453. if len(trasactions) > 0 || len(uncles) > 0 || !filter {
  454. err := pm.downloader.DeliverBodies(p.id, trasactions, uncles)
  455. if err != nil {
  456. log.Debug("Failed to deliver bodies", "err", err)
  457. }
  458. }
  459. case p.version >= eth63 && msg.Code == GetNodeDataMsg:
  460. // Decode the retrieval message
  461. msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
  462. if _, err := msgStream.List(); err != nil {
  463. return err
  464. }
  465. // Gather state data until the fetch or network limits is reached
  466. var (
  467. hash common.Hash
  468. bytes int
  469. data [][]byte
  470. )
  471. for bytes < softResponseLimit && len(data) < downloader.MaxStateFetch {
  472. // Retrieve the hash of the next state entry
  473. if err := msgStream.Decode(&hash); err == rlp.EOL {
  474. break
  475. } else if err != nil {
  476. return errResp(ErrDecode, "msg %v: %v", msg, err)
  477. }
  478. // Retrieve the requested state entry, stopping if enough was found
  479. if entry, err := pm.chaindb.Get(hash.Bytes()); err == nil {
  480. data = append(data, entry)
  481. bytes += len(entry)
  482. }
  483. }
  484. return p.SendNodeData(data)
  485. case p.version >= eth63 && msg.Code == NodeDataMsg:
  486. // A batch of node state data arrived to one of our previous requests
  487. var data [][]byte
  488. if err := msg.Decode(&data); err != nil {
  489. return errResp(ErrDecode, "msg %v: %v", msg, err)
  490. }
  491. // Deliver all to the downloader
  492. if err := pm.downloader.DeliverNodeData(p.id, data); err != nil {
  493. log.Debug("Failed to deliver node state data", "err", err)
  494. }
  495. case p.version >= eth63 && msg.Code == GetReceiptsMsg:
  496. // Decode the retrieval message
  497. msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
  498. if _, err := msgStream.List(); err != nil {
  499. return err
  500. }
  501. // Gather state data until the fetch or network limits is reached
  502. var (
  503. hash common.Hash
  504. bytes int
  505. receipts []rlp.RawValue
  506. )
  507. for bytes < softResponseLimit && len(receipts) < downloader.MaxReceiptFetch {
  508. // Retrieve the hash of the next block
  509. if err := msgStream.Decode(&hash); err == rlp.EOL {
  510. break
  511. } else if err != nil {
  512. return errResp(ErrDecode, "msg %v: %v", msg, err)
  513. }
  514. // Retrieve the requested block's receipts, skipping if unknown to us
  515. results := core.GetBlockReceipts(pm.chaindb, hash, core.GetBlockNumber(pm.chaindb, hash))
  516. if results == nil {
  517. if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
  518. continue
  519. }
  520. }
  521. // If known, encode and queue for response packet
  522. if encoded, err := rlp.EncodeToBytes(results); err != nil {
  523. log.Error("Failed to encode receipt", "err", err)
  524. } else {
  525. receipts = append(receipts, encoded)
  526. bytes += len(encoded)
  527. }
  528. }
  529. return p.SendReceiptsRLP(receipts)
  530. case p.version >= eth63 && msg.Code == ReceiptsMsg:
  531. // A batch of receipts arrived to one of our previous requests
  532. var receipts [][]*types.Receipt
  533. if err := msg.Decode(&receipts); err != nil {
  534. return errResp(ErrDecode, "msg %v: %v", msg, err)
  535. }
  536. // Deliver all to the downloader
  537. if err := pm.downloader.DeliverReceipts(p.id, receipts); err != nil {
  538. log.Debug("Failed to deliver receipts", "err", err)
  539. }
  540. case msg.Code == NewBlockHashesMsg:
  541. var announces newBlockHashesData
  542. if err := msg.Decode(&announces); err != nil {
  543. return errResp(ErrDecode, "%v: %v", msg, err)
  544. }
  545. // Mark the hashes as present at the remote node
  546. for _, block := range announces {
  547. p.MarkBlock(block.Hash)
  548. }
  549. // Schedule all the unknown hashes for retrieval
  550. unknown := make(newBlockHashesData, 0, len(announces))
  551. for _, block := range announces {
  552. if !pm.blockchain.HasBlock(block.Hash) {
  553. unknown = append(unknown, block)
  554. }
  555. }
  556. for _, block := range unknown {
  557. pm.fetcher.Notify(p.id, block.Hash, block.Number, time.Now(), p.RequestOneHeader, p.RequestBodies)
  558. }
  559. case msg.Code == NewBlockMsg:
  560. // Retrieve and decode the propagated block
  561. var request newBlockData
  562. if err := msg.Decode(&request); err != nil {
  563. return errResp(ErrDecode, "%v: %v", msg, err)
  564. }
  565. request.Block.ReceivedAt = msg.ReceivedAt
  566. request.Block.ReceivedFrom = p
  567. // Mark the peer as owning the block and schedule it for import
  568. p.MarkBlock(request.Block.Hash())
  569. pm.fetcher.Enqueue(p.id, request.Block)
  570. // Assuming the block is importable by the peer, but possibly not yet done so,
  571. // calculate the head hash and TD that the peer truly must have.
  572. var (
  573. trueHead = request.Block.ParentHash()
  574. trueTD = new(big.Int).Sub(request.TD, request.Block.Difficulty())
  575. )
  576. // Update the peers total difficulty if better than the previous
  577. if _, td := p.Head(); trueTD.Cmp(td) > 0 {
  578. p.SetHead(trueHead, trueTD)
  579. // Schedule a sync if above ours. Note, this will not fire a sync for a gap of
  580. // a singe block (as the true TD is below the propagated block), however this
  581. // scenario should easily be covered by the fetcher.
  582. currentBlock := pm.blockchain.CurrentBlock()
  583. if trueTD.Cmp(pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())) > 0 {
  584. go pm.synchronise(p)
  585. }
  586. }
  587. case msg.Code == TxMsg:
  588. // Transactions arrived, make sure we have a valid and fresh chain to handle them
  589. if atomic.LoadUint32(&pm.acceptTxs) == 0 {
  590. break
  591. }
  592. // Transactions can be processed, parse all of them and deliver to the pool
  593. var txs []*types.Transaction
  594. if err := msg.Decode(&txs); err != nil {
  595. return errResp(ErrDecode, "msg %v: %v", msg, err)
  596. }
  597. for i, tx := range txs {
  598. // Validate and mark the remote transaction
  599. if tx == nil {
  600. return errResp(ErrDecode, "transaction %d is nil", i)
  601. }
  602. p.MarkTransaction(tx.Hash())
  603. }
  604. pm.txpool.AddBatch(txs)
  605. default:
  606. return errResp(ErrInvalidMsgCode, "%v", msg.Code)
  607. }
  608. return nil
  609. }
  610. // BroadcastBlock will either propagate a block to a subset of it's peers, or
  611. // will only announce it's availability (depending what's requested).
  612. func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
  613. hash := block.Hash()
  614. peers := pm.peers.PeersWithoutBlock(hash)
  615. // If propagation is requested, send to a subset of the peer
  616. if propagate {
  617. // Calculate the TD of the block (it's not imported yet, so block.Td is not valid)
  618. var td *big.Int
  619. if parent := pm.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent != nil {
  620. td = new(big.Int).Add(block.Difficulty(), pm.blockchain.GetTd(block.ParentHash(), block.NumberU64()-1))
  621. } else {
  622. log.Error("Propagating dangling block", "number", block.Number(), "hash", hash)
  623. return
  624. }
  625. // Send the block to a subset of our peers
  626. transfer := peers[:int(math.Sqrt(float64(len(peers))))]
  627. for _, peer := range transfer {
  628. peer.SendNewBlock(block, td)
  629. }
  630. log.Trace("Propagated block", "hash", hash, "recipients", len(transfer), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
  631. }
  632. // Otherwise if the block is indeed in out own chain, announce it
  633. if pm.blockchain.HasBlock(hash) {
  634. for _, peer := range peers {
  635. peer.SendNewBlockHashes([]common.Hash{hash}, []uint64{block.NumberU64()})
  636. }
  637. log.Trace("Announced block", "hash", hash, "recipients", len(peers), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
  638. }
  639. }
  640. // BroadcastTx will propagate a transaction to all peers which are not known to
  641. // already have the given transaction.
  642. func (pm *ProtocolManager) BroadcastTx(hash common.Hash, tx *types.Transaction) {
  643. // Broadcast transaction to a batch of peers not knowing about it
  644. peers := pm.peers.PeersWithoutTx(hash)
  645. //FIXME include this again: peers = peers[:int(math.Sqrt(float64(len(peers))))]
  646. for _, peer := range peers {
  647. peer.SendTransactions(types.Transactions{tx})
  648. }
  649. log.Trace("Broadcast transaction", "hash", hash, "recipients", len(peers))
  650. }
  651. // Mined broadcast loop
  652. func (self *ProtocolManager) minedBroadcastLoop() {
  653. // automatically stops if unsubscribe
  654. for obj := range self.minedBlockSub.Chan() {
  655. switch ev := obj.Data.(type) {
  656. case core.NewMinedBlockEvent:
  657. self.BroadcastBlock(ev.Block, true) // First propagate block to peers
  658. self.BroadcastBlock(ev.Block, false) // Only then announce to the rest
  659. }
  660. }
  661. }
  662. func (self *ProtocolManager) txBroadcastLoop() {
  663. // automatically stops if unsubscribe
  664. for obj := range self.txSub.Chan() {
  665. event := obj.Data.(core.TxPreEvent)
  666. self.BroadcastTx(event.Tx.Hash(), event.Tx)
  667. }
  668. }
  669. // EthNodeInfo represents a short summary of the Ethereum sub-protocol metadata known
  670. // about the host peer.
  671. type EthNodeInfo struct {
  672. Network int `json:"network"` // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3)
  673. Difficulty *big.Int `json:"difficulty"` // Total difficulty of the host's blockchain
  674. Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block
  675. Head common.Hash `json:"head"` // SHA3 hash of the host's best owned block
  676. }
  677. // NodeInfo retrieves some protocol metadata about the running host node.
  678. func (self *ProtocolManager) NodeInfo() *EthNodeInfo {
  679. currentBlock := self.blockchain.CurrentBlock()
  680. return &EthNodeInfo{
  681. Network: self.networkId,
  682. Difficulty: self.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64()),
  683. Genesis: self.blockchain.Genesis().Hash(),
  684. Head: currentBlock.Hash(),
  685. }
  686. }