handler.go 27 KB

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