handler.go 26 KB

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