handler.go 27 KB

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