github.com/luckypickle/go-ethereum-vet@v1.14.2/les/handler.go (about) 1 // Copyright 2016 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 17 // Package les implements the Light Ethereum Subprotocol. 18 package les 19 20 import ( 21 "encoding/binary" 22 "encoding/json" 23 "fmt" 24 "math/big" 25 "net" 26 "sync" 27 "time" 28 29 "github.com/luckypickle/go-ethereum-vet/common" 30 "github.com/luckypickle/go-ethereum-vet/common/mclock" 31 "github.com/luckypickle/go-ethereum-vet/consensus" 32 "github.com/luckypickle/go-ethereum-vet/core" 33 "github.com/luckypickle/go-ethereum-vet/core/rawdb" 34 "github.com/luckypickle/go-ethereum-vet/core/state" 35 "github.com/luckypickle/go-ethereum-vet/core/types" 36 "github.com/luckypickle/go-ethereum-vet/eth/downloader" 37 "github.com/luckypickle/go-ethereum-vet/ethdb" 38 "github.com/luckypickle/go-ethereum-vet/event" 39 "github.com/luckypickle/go-ethereum-vet/light" 40 "github.com/luckypickle/go-ethereum-vet/log" 41 "github.com/luckypickle/go-ethereum-vet/p2p" 42 "github.com/luckypickle/go-ethereum-vet/p2p/discv5" 43 "github.com/luckypickle/go-ethereum-vet/params" 44 "github.com/luckypickle/go-ethereum-vet/rlp" 45 "github.com/luckypickle/go-ethereum-vet/trie" 46 ) 47 48 const ( 49 softResponseLimit = 2 * 1024 * 1024 // Target maximum size of returned blocks, headers or node data. 50 estHeaderRlpSize = 500 // Approximate size of an RLP encoded block header 51 52 ethVersion = 63 // equivalent eth version for the downloader 53 54 MaxHeaderFetch = 192 // Amount of block headers to be fetched per retrieval request 55 MaxBodyFetch = 32 // Amount of block bodies to be fetched per retrieval request 56 MaxReceiptFetch = 128 // Amount of transaction receipts to allow fetching per request 57 MaxCodeFetch = 64 // Amount of contract codes to allow fetching per request 58 MaxProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request 59 MaxHelperTrieProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request 60 MaxTxSend = 64 // Amount of transactions to be send per request 61 MaxTxStatus = 256 // Amount of transactions to queried per request 62 63 disableClientRemovePeer = false 64 ) 65 66 func errResp(code errCode, format string, v ...interface{}) error { 67 return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...)) 68 } 69 70 type BlockChain interface { 71 Config() *params.ChainConfig 72 HasHeader(hash common.Hash, number uint64) bool 73 GetHeader(hash common.Hash, number uint64) *types.Header 74 GetHeaderByHash(hash common.Hash) *types.Header 75 CurrentHeader() *types.Header 76 GetTd(hash common.Hash, number uint64) *big.Int 77 State() (*state.StateDB, error) 78 InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) 79 Rollback(chain []common.Hash) 80 GetHeaderByNumber(number uint64) *types.Header 81 GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) 82 Genesis() *types.Block 83 SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription 84 } 85 86 type txPool interface { 87 AddRemotes(txs []*types.Transaction) []error 88 Status(hashes []common.Hash) []core.TxStatus 89 } 90 91 type ProtocolManager struct { 92 lightSync bool 93 txpool txPool 94 txrelay *LesTxRelay 95 networkId uint64 96 chainConfig *params.ChainConfig 97 blockchain BlockChain 98 chainDb ethdb.Database 99 odr *LesOdr 100 server *LesServer 101 serverPool *serverPool 102 clientPool *freeClientPool 103 lesTopic discv5.Topic 104 reqDist *requestDistributor 105 retriever *retrieveManager 106 107 downloader *downloader.Downloader 108 fetcher *lightFetcher 109 peers *peerSet 110 maxPeers int 111 112 eventMux *event.TypeMux 113 114 // channels for fetcher, syncer, txsyncLoop 115 newPeerCh chan *peer 116 quitSync chan struct{} 117 noMorePeers chan struct{} 118 119 // wait group is used for graceful shutdowns during downloading 120 // and processing 121 wg *sync.WaitGroup 122 } 123 124 // NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable 125 // with the ethereum network. 126 func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, networkId uint64, mux *event.TypeMux, engine consensus.Engine, peers *peerSet, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, txrelay *LesTxRelay, serverPool *serverPool, quitSync chan struct{}, wg *sync.WaitGroup) (*ProtocolManager, error) { 127 // Create the protocol manager with the base fields 128 manager := &ProtocolManager{ 129 lightSync: lightSync, 130 eventMux: mux, 131 blockchain: blockchain, 132 chainConfig: chainConfig, 133 chainDb: chainDb, 134 odr: odr, 135 networkId: networkId, 136 txpool: txpool, 137 txrelay: txrelay, 138 serverPool: serverPool, 139 peers: peers, 140 newPeerCh: make(chan *peer), 141 quitSync: quitSync, 142 wg: wg, 143 noMorePeers: make(chan struct{}), 144 } 145 if odr != nil { 146 manager.retriever = odr.retriever 147 manager.reqDist = odr.retriever.dist 148 } 149 150 removePeer := manager.removePeer 151 if disableClientRemovePeer { 152 removePeer = func(id string) {} 153 } 154 155 if lightSync { 156 manager.downloader = downloader.New(downloader.LightSync, chainDb, manager.eventMux, nil, blockchain, removePeer) 157 manager.peers.notify((*downloaderPeerNotify)(manager)) 158 manager.fetcher = newLightFetcher(manager) 159 } 160 161 return manager, nil 162 } 163 164 // removePeer initiates disconnection from a peer by removing it from the peer set 165 func (pm *ProtocolManager) removePeer(id string) { 166 pm.peers.Unregister(id) 167 } 168 169 func (pm *ProtocolManager) Start(maxPeers int) { 170 pm.maxPeers = maxPeers 171 172 if pm.lightSync { 173 go pm.syncer() 174 } else { 175 pm.clientPool = newFreeClientPool(pm.chainDb, maxPeers, 10000, mclock.System{}) 176 go func() { 177 for range pm.newPeerCh { 178 } 179 }() 180 } 181 } 182 183 func (pm *ProtocolManager) Stop() { 184 // Showing a log message. During download / process this could actually 185 // take between 5 to 10 seconds and therefor feedback is required. 186 log.Info("Stopping light Ethereum protocol") 187 188 // Quit the sync loop. 189 // After this send has completed, no new peers will be accepted. 190 pm.noMorePeers <- struct{}{} 191 192 close(pm.quitSync) // quits syncer, fetcher 193 if pm.clientPool != nil { 194 pm.clientPool.stop() 195 } 196 197 // Disconnect existing sessions. 198 // This also closes the gate for any new registrations on the peer set. 199 // sessions which are already established but not added to pm.peers yet 200 // will exit when they try to register. 201 pm.peers.Close() 202 203 // Wait for any process action 204 pm.wg.Wait() 205 206 log.Info("Light Ethereum protocol stopped") 207 } 208 209 // runPeer is the p2p protocol run function for the given version. 210 func (pm *ProtocolManager) runPeer(version uint, p *p2p.Peer, rw p2p.MsgReadWriter) error { 211 var entry *poolEntry 212 peer := pm.newPeer(int(version), pm.networkId, p, rw) 213 if pm.serverPool != nil { 214 addr := p.RemoteAddr().(*net.TCPAddr) 215 entry = pm.serverPool.connect(peer, addr.IP, uint16(addr.Port)) 216 } 217 peer.poolEntry = entry 218 select { 219 case pm.newPeerCh <- peer: 220 pm.wg.Add(1) 221 defer pm.wg.Done() 222 err := pm.handle(peer) 223 if entry != nil { 224 pm.serverPool.disconnect(entry) 225 } 226 return err 227 case <-pm.quitSync: 228 if entry != nil { 229 pm.serverPool.disconnect(entry) 230 } 231 return p2p.DiscQuitting 232 } 233 } 234 235 func (pm *ProtocolManager) newPeer(pv int, nv uint64, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { 236 return newPeer(pv, nv, p, newMeteredMsgWriter(rw)) 237 } 238 239 // handle is the callback invoked to manage the life cycle of a les peer. When 240 // this function terminates, the peer is disconnected. 241 func (pm *ProtocolManager) handle(p *peer) error { 242 // Ignore maxPeers if this is a trusted peer 243 // In server mode we try to check into the client pool after handshake 244 if pm.lightSync && pm.peers.Len() >= pm.maxPeers && !p.Peer.Info().Network.Trusted { 245 return p2p.DiscTooManyPeers 246 } 247 248 p.Log().Debug("Light Ethereum peer connected", "name", p.Name()) 249 250 // Execute the LES handshake 251 var ( 252 genesis = pm.blockchain.Genesis() 253 head = pm.blockchain.CurrentHeader() 254 hash = head.Hash() 255 number = head.Number.Uint64() 256 td = pm.blockchain.GetTd(hash, number) 257 ) 258 if err := p.Handshake(td, hash, number, genesis.Hash(), pm.server); err != nil { 259 p.Log().Debug("Light Ethereum handshake failed", "err", err) 260 return err 261 } 262 263 if !pm.lightSync && !p.Peer.Info().Network.Trusted { 264 addr, ok := p.RemoteAddr().(*net.TCPAddr) 265 // test peer address is not a tcp address, don't use client pool if can not typecast 266 if ok { 267 id := addr.IP.String() 268 if !pm.clientPool.connect(id, func() { go pm.removePeer(p.id) }) { 269 return p2p.DiscTooManyPeers 270 } 271 defer pm.clientPool.disconnect(id) 272 } 273 } 274 275 if rw, ok := p.rw.(*meteredMsgReadWriter); ok { 276 rw.Init(p.version) 277 } 278 // Register the peer locally 279 if err := pm.peers.Register(p); err != nil { 280 p.Log().Error("Light Ethereum peer registration failed", "err", err) 281 return err 282 } 283 defer func() { 284 if pm.server != nil && pm.server.fcManager != nil && p.fcClient != nil { 285 p.fcClient.Remove(pm.server.fcManager) 286 } 287 pm.removePeer(p.id) 288 }() 289 // Register the peer in the downloader. If the downloader considers it banned, we disconnect 290 if pm.lightSync { 291 p.lock.Lock() 292 head := p.headInfo 293 p.lock.Unlock() 294 if pm.fetcher != nil { 295 pm.fetcher.announce(p, head) 296 } 297 298 if p.poolEntry != nil { 299 pm.serverPool.registered(p.poolEntry) 300 } 301 } 302 303 stop := make(chan struct{}) 304 defer close(stop) 305 go func() { 306 // new block announce loop 307 for { 308 select { 309 case announce := <-p.announceChn: 310 p.SendAnnounce(announce) 311 case <-stop: 312 return 313 } 314 } 315 }() 316 317 // main loop. handle incoming messages. 318 for { 319 if err := pm.handleMsg(p); err != nil { 320 p.Log().Debug("Light Ethereum message handling failed", "err", err) 321 return err 322 } 323 } 324 } 325 326 var reqList = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, GetProofsV1Msg, SendTxMsg, SendTxV2Msg, GetTxStatusMsg, GetHeaderProofsMsg, GetProofsV2Msg, GetHelperTrieProofsMsg} 327 328 // handleMsg is invoked whenever an inbound message is received from a remote 329 // peer. The remote connection is torn down upon returning any error. 330 func (pm *ProtocolManager) handleMsg(p *peer) error { 331 // Read the next message from the remote peer, and ensure it's fully consumed 332 msg, err := p.rw.ReadMsg() 333 if err != nil { 334 return err 335 } 336 p.Log().Trace("Light Ethereum message arrived", "code", msg.Code, "bytes", msg.Size) 337 338 costs := p.fcCosts[msg.Code] 339 reject := func(reqCnt, maxCnt uint64) bool { 340 if p.fcClient == nil || reqCnt > maxCnt { 341 return true 342 } 343 bufValue, _ := p.fcClient.AcceptRequest() 344 cost := costs.baseCost + reqCnt*costs.reqCost 345 if cost > pm.server.defParams.BufLimit { 346 cost = pm.server.defParams.BufLimit 347 } 348 if cost > bufValue { 349 recharge := time.Duration((cost - bufValue) * 1000000 / pm.server.defParams.MinRecharge) 350 p.Log().Error("Request came too early", "recharge", common.PrettyDuration(recharge)) 351 return true 352 } 353 return false 354 } 355 356 if msg.Size > ProtocolMaxMsgSize { 357 return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) 358 } 359 defer msg.Discard() 360 361 var deliverMsg *Msg 362 363 // Handle the message depending on its contents 364 switch msg.Code { 365 case StatusMsg: 366 p.Log().Trace("Received status message") 367 // Status messages should never arrive after the handshake 368 return errResp(ErrExtraStatusMsg, "uncontrolled status message") 369 370 // Block header query, collect the requested headers and reply 371 case AnnounceMsg: 372 p.Log().Trace("Received announce message") 373 if p.requestAnnounceType == announceTypeNone { 374 return errResp(ErrUnexpectedResponse, "") 375 } 376 377 var req announceData 378 if err := msg.Decode(&req); err != nil { 379 return errResp(ErrDecode, "%v: %v", msg, err) 380 } 381 382 if p.requestAnnounceType == announceTypeSigned { 383 if err := req.checkSignature(p.pubKey); err != nil { 384 p.Log().Trace("Invalid announcement signature", "err", err) 385 return err 386 } 387 p.Log().Trace("Valid announcement signature") 388 } 389 390 p.Log().Trace("Announce message content", "number", req.Number, "hash", req.Hash, "td", req.Td, "reorg", req.ReorgDepth) 391 if pm.fetcher != nil { 392 pm.fetcher.announce(p, &req) 393 } 394 395 case GetBlockHeadersMsg: 396 p.Log().Trace("Received block header request") 397 // Decode the complex header query 398 var req struct { 399 ReqID uint64 400 Query getBlockHeadersData 401 } 402 if err := msg.Decode(&req); err != nil { 403 return errResp(ErrDecode, "%v: %v", msg, err) 404 } 405 406 query := req.Query 407 if reject(query.Amount, MaxHeaderFetch) { 408 return errResp(ErrRequestRejected, "") 409 } 410 411 hashMode := query.Origin.Hash != (common.Hash{}) 412 first := true 413 maxNonCanonical := uint64(100) 414 415 // Gather headers until the fetch or network limits is reached 416 var ( 417 bytes common.StorageSize 418 headers []*types.Header 419 unknown bool 420 ) 421 for !unknown && len(headers) < int(query.Amount) && bytes < softResponseLimit { 422 // Retrieve the next header satisfying the query 423 var origin *types.Header 424 if hashMode { 425 if first { 426 first = false 427 origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash) 428 if origin != nil { 429 query.Origin.Number = origin.Number.Uint64() 430 } 431 } else { 432 origin = pm.blockchain.GetHeader(query.Origin.Hash, query.Origin.Number) 433 } 434 } else { 435 origin = pm.blockchain.GetHeaderByNumber(query.Origin.Number) 436 } 437 if origin == nil { 438 break 439 } 440 headers = append(headers, origin) 441 bytes += estHeaderRlpSize 442 443 // Advance to the next header of the query 444 switch { 445 case hashMode && query.Reverse: 446 // Hash based traversal towards the genesis block 447 ancestor := query.Skip + 1 448 if ancestor == 0 { 449 unknown = true 450 } else { 451 query.Origin.Hash, query.Origin.Number = pm.blockchain.GetAncestor(query.Origin.Hash, query.Origin.Number, ancestor, &maxNonCanonical) 452 unknown = (query.Origin.Hash == common.Hash{}) 453 } 454 case hashMode && !query.Reverse: 455 // Hash based traversal towards the leaf block 456 var ( 457 current = origin.Number.Uint64() 458 next = current + query.Skip + 1 459 ) 460 if next <= current { 461 infos, _ := json.MarshalIndent(p.Peer.Info(), "", " ") 462 p.Log().Warn("GetBlockHeaders skip overflow attack", "current", current, "skip", query.Skip, "next", next, "attacker", infos) 463 unknown = true 464 } else { 465 if header := pm.blockchain.GetHeaderByNumber(next); header != nil { 466 nextHash := header.Hash() 467 expOldHash, _ := pm.blockchain.GetAncestor(nextHash, next, query.Skip+1, &maxNonCanonical) 468 if expOldHash == query.Origin.Hash { 469 query.Origin.Hash, query.Origin.Number = nextHash, next 470 } else { 471 unknown = true 472 } 473 } else { 474 unknown = true 475 } 476 } 477 case query.Reverse: 478 // Number based traversal towards the genesis block 479 if query.Origin.Number >= query.Skip+1 { 480 query.Origin.Number -= query.Skip + 1 481 } else { 482 unknown = true 483 } 484 485 case !query.Reverse: 486 // Number based traversal towards the leaf block 487 query.Origin.Number += query.Skip + 1 488 } 489 } 490 491 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + query.Amount*costs.reqCost) 492 pm.server.fcCostStats.update(msg.Code, query.Amount, rcost) 493 return p.SendBlockHeaders(req.ReqID, bv, headers) 494 495 case BlockHeadersMsg: 496 if pm.downloader == nil { 497 return errResp(ErrUnexpectedResponse, "") 498 } 499 500 p.Log().Trace("Received block header response message") 501 // A batch of headers arrived to one of our previous requests 502 var resp struct { 503 ReqID, BV uint64 504 Headers []*types.Header 505 } 506 if err := msg.Decode(&resp); err != nil { 507 return errResp(ErrDecode, "msg %v: %v", msg, err) 508 } 509 p.fcServer.GotReply(resp.ReqID, resp.BV) 510 if pm.fetcher != nil && pm.fetcher.requestedID(resp.ReqID) { 511 pm.fetcher.deliverHeaders(p, resp.ReqID, resp.Headers) 512 } else { 513 err := pm.downloader.DeliverHeaders(p.id, resp.Headers) 514 if err != nil { 515 log.Debug(fmt.Sprint(err)) 516 } 517 } 518 519 case GetBlockBodiesMsg: 520 p.Log().Trace("Received block bodies request") 521 // Decode the retrieval message 522 var req struct { 523 ReqID uint64 524 Hashes []common.Hash 525 } 526 if err := msg.Decode(&req); err != nil { 527 return errResp(ErrDecode, "msg %v: %v", msg, err) 528 } 529 // Gather blocks until the fetch or network limits is reached 530 var ( 531 bytes int 532 bodies []rlp.RawValue 533 ) 534 reqCnt := len(req.Hashes) 535 if reject(uint64(reqCnt), MaxBodyFetch) { 536 return errResp(ErrRequestRejected, "") 537 } 538 for _, hash := range req.Hashes { 539 if bytes >= softResponseLimit { 540 break 541 } 542 // Retrieve the requested block body, stopping if enough was found 543 if number := rawdb.ReadHeaderNumber(pm.chainDb, hash); number != nil { 544 if data := rawdb.ReadBodyRLP(pm.chainDb, hash, *number); len(data) != 0 { 545 bodies = append(bodies, data) 546 bytes += len(data) 547 } 548 } 549 } 550 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 551 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 552 return p.SendBlockBodiesRLP(req.ReqID, bv, bodies) 553 554 case BlockBodiesMsg: 555 if pm.odr == nil { 556 return errResp(ErrUnexpectedResponse, "") 557 } 558 559 p.Log().Trace("Received block bodies response") 560 // A batch of block bodies arrived to one of our previous requests 561 var resp struct { 562 ReqID, BV uint64 563 Data []*types.Body 564 } 565 if err := msg.Decode(&resp); err != nil { 566 return errResp(ErrDecode, "msg %v: %v", msg, err) 567 } 568 p.fcServer.GotReply(resp.ReqID, resp.BV) 569 deliverMsg = &Msg{ 570 MsgType: MsgBlockBodies, 571 ReqID: resp.ReqID, 572 Obj: resp.Data, 573 } 574 575 case GetCodeMsg: 576 p.Log().Trace("Received code request") 577 // Decode the retrieval message 578 var req struct { 579 ReqID uint64 580 Reqs []CodeReq 581 } 582 if err := msg.Decode(&req); err != nil { 583 return errResp(ErrDecode, "msg %v: %v", msg, err) 584 } 585 // Gather state data until the fetch or network limits is reached 586 var ( 587 bytes int 588 data [][]byte 589 ) 590 reqCnt := len(req.Reqs) 591 if reject(uint64(reqCnt), MaxCodeFetch) { 592 return errResp(ErrRequestRejected, "") 593 } 594 for _, req := range req.Reqs { 595 // Retrieve the requested state entry, stopping if enough was found 596 if number := rawdb.ReadHeaderNumber(pm.chainDb, req.BHash); number != nil { 597 if header := rawdb.ReadHeader(pm.chainDb, req.BHash, *number); header != nil { 598 statedb, err := pm.blockchain.State() 599 if err != nil { 600 continue 601 } 602 account, err := pm.getAccount(statedb, header.Root, common.BytesToHash(req.AccKey)) 603 if err != nil { 604 continue 605 } 606 code, _ := statedb.Database().TrieDB().Node(common.BytesToHash(account.CodeHash)) 607 608 data = append(data, code) 609 if bytes += len(code); bytes >= softResponseLimit { 610 break 611 } 612 } 613 } 614 } 615 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 616 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 617 return p.SendCode(req.ReqID, bv, data) 618 619 case CodeMsg: 620 if pm.odr == nil { 621 return errResp(ErrUnexpectedResponse, "") 622 } 623 624 p.Log().Trace("Received code response") 625 // A batch of node state data arrived to one of our previous requests 626 var resp struct { 627 ReqID, BV uint64 628 Data [][]byte 629 } 630 if err := msg.Decode(&resp); err != nil { 631 return errResp(ErrDecode, "msg %v: %v", msg, err) 632 } 633 p.fcServer.GotReply(resp.ReqID, resp.BV) 634 deliverMsg = &Msg{ 635 MsgType: MsgCode, 636 ReqID: resp.ReqID, 637 Obj: resp.Data, 638 } 639 640 case GetReceiptsMsg: 641 p.Log().Trace("Received receipts request") 642 // Decode the retrieval message 643 var req struct { 644 ReqID uint64 645 Hashes []common.Hash 646 } 647 if err := msg.Decode(&req); err != nil { 648 return errResp(ErrDecode, "msg %v: %v", msg, err) 649 } 650 // Gather state data until the fetch or network limits is reached 651 var ( 652 bytes int 653 receipts []rlp.RawValue 654 ) 655 reqCnt := len(req.Hashes) 656 if reject(uint64(reqCnt), MaxReceiptFetch) { 657 return errResp(ErrRequestRejected, "") 658 } 659 for _, hash := range req.Hashes { 660 if bytes >= softResponseLimit { 661 break 662 } 663 // Retrieve the requested block's receipts, skipping if unknown to us 664 var results types.Receipts 665 if number := rawdb.ReadHeaderNumber(pm.chainDb, hash); number != nil { 666 results = rawdb.ReadReceipts(pm.chainDb, hash, *number) 667 } 668 if results == nil { 669 if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash { 670 continue 671 } 672 } 673 // If known, encode and queue for response packet 674 if encoded, err := rlp.EncodeToBytes(results); err != nil { 675 log.Error("Failed to encode receipt", "err", err) 676 } else { 677 receipts = append(receipts, encoded) 678 bytes += len(encoded) 679 } 680 } 681 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 682 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 683 return p.SendReceiptsRLP(req.ReqID, bv, receipts) 684 685 case ReceiptsMsg: 686 if pm.odr == nil { 687 return errResp(ErrUnexpectedResponse, "") 688 } 689 690 p.Log().Trace("Received receipts response") 691 // A batch of receipts arrived to one of our previous requests 692 var resp struct { 693 ReqID, BV uint64 694 Receipts []types.Receipts 695 } 696 if err := msg.Decode(&resp); err != nil { 697 return errResp(ErrDecode, "msg %v: %v", msg, err) 698 } 699 p.fcServer.GotReply(resp.ReqID, resp.BV) 700 deliverMsg = &Msg{ 701 MsgType: MsgReceipts, 702 ReqID: resp.ReqID, 703 Obj: resp.Receipts, 704 } 705 706 case GetProofsV1Msg: 707 p.Log().Trace("Received proofs request") 708 // Decode the retrieval message 709 var req struct { 710 ReqID uint64 711 Reqs []ProofReq 712 } 713 if err := msg.Decode(&req); err != nil { 714 return errResp(ErrDecode, "msg %v: %v", msg, err) 715 } 716 // Gather state data until the fetch or network limits is reached 717 var ( 718 bytes int 719 proofs proofsData 720 ) 721 reqCnt := len(req.Reqs) 722 if reject(uint64(reqCnt), MaxProofsFetch) { 723 return errResp(ErrRequestRejected, "") 724 } 725 for _, req := range req.Reqs { 726 // Retrieve the requested state entry, stopping if enough was found 727 if number := rawdb.ReadHeaderNumber(pm.chainDb, req.BHash); number != nil { 728 if header := rawdb.ReadHeader(pm.chainDb, req.BHash, *number); header != nil { 729 statedb, err := pm.blockchain.State() 730 if err != nil { 731 continue 732 } 733 var trie state.Trie 734 if len(req.AccKey) > 0 { 735 account, err := pm.getAccount(statedb, header.Root, common.BytesToHash(req.AccKey)) 736 if err != nil { 737 continue 738 } 739 trie, _ = statedb.Database().OpenStorageTrie(common.BytesToHash(req.AccKey), account.Root) 740 } else { 741 trie, _ = statedb.Database().OpenTrie(header.Root) 742 } 743 if trie != nil { 744 var proof light.NodeList 745 trie.Prove(req.Key, 0, &proof) 746 747 proofs = append(proofs, proof) 748 if bytes += proof.DataSize(); bytes >= softResponseLimit { 749 break 750 } 751 } 752 } 753 } 754 } 755 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 756 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 757 return p.SendProofs(req.ReqID, bv, proofs) 758 759 case GetProofsV2Msg: 760 p.Log().Trace("Received les/2 proofs request") 761 // Decode the retrieval message 762 var req struct { 763 ReqID uint64 764 Reqs []ProofReq 765 } 766 if err := msg.Decode(&req); err != nil { 767 return errResp(ErrDecode, "msg %v: %v", msg, err) 768 } 769 // Gather state data until the fetch or network limits is reached 770 var ( 771 lastBHash common.Hash 772 statedb *state.StateDB 773 root common.Hash 774 ) 775 reqCnt := len(req.Reqs) 776 if reject(uint64(reqCnt), MaxProofsFetch) { 777 return errResp(ErrRequestRejected, "") 778 } 779 780 nodes := light.NewNodeSet() 781 782 for _, req := range req.Reqs { 783 // Look up the state belonging to the request 784 if statedb == nil || req.BHash != lastBHash { 785 statedb, root, lastBHash = nil, common.Hash{}, req.BHash 786 787 if number := rawdb.ReadHeaderNumber(pm.chainDb, req.BHash); number != nil { 788 if header := rawdb.ReadHeader(pm.chainDb, req.BHash, *number); header != nil { 789 statedb, _ = pm.blockchain.State() 790 root = header.Root 791 } 792 } 793 } 794 if statedb == nil { 795 continue 796 } 797 // Pull the account or storage trie of the request 798 var trie state.Trie 799 if len(req.AccKey) > 0 { 800 account, err := pm.getAccount(statedb, root, common.BytesToHash(req.AccKey)) 801 if err != nil { 802 continue 803 } 804 trie, _ = statedb.Database().OpenStorageTrie(common.BytesToHash(req.AccKey), account.Root) 805 } else { 806 trie, _ = statedb.Database().OpenTrie(root) 807 } 808 if trie == nil { 809 continue 810 } 811 // Prove the user's request from the account or stroage trie 812 trie.Prove(req.Key, req.FromLevel, nodes) 813 if nodes.DataSize() >= softResponseLimit { 814 break 815 } 816 } 817 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 818 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 819 return p.SendProofsV2(req.ReqID, bv, nodes.NodeList()) 820 821 case ProofsV1Msg: 822 if pm.odr == nil { 823 return errResp(ErrUnexpectedResponse, "") 824 } 825 826 p.Log().Trace("Received proofs response") 827 // A batch of merkle proofs arrived to one of our previous requests 828 var resp struct { 829 ReqID, BV uint64 830 Data []light.NodeList 831 } 832 if err := msg.Decode(&resp); err != nil { 833 return errResp(ErrDecode, "msg %v: %v", msg, err) 834 } 835 p.fcServer.GotReply(resp.ReqID, resp.BV) 836 deliverMsg = &Msg{ 837 MsgType: MsgProofsV1, 838 ReqID: resp.ReqID, 839 Obj: resp.Data, 840 } 841 842 case ProofsV2Msg: 843 if pm.odr == nil { 844 return errResp(ErrUnexpectedResponse, "") 845 } 846 847 p.Log().Trace("Received les/2 proofs response") 848 // A batch of merkle proofs arrived to one of our previous requests 849 var resp struct { 850 ReqID, BV uint64 851 Data light.NodeList 852 } 853 if err := msg.Decode(&resp); err != nil { 854 return errResp(ErrDecode, "msg %v: %v", msg, err) 855 } 856 p.fcServer.GotReply(resp.ReqID, resp.BV) 857 deliverMsg = &Msg{ 858 MsgType: MsgProofsV2, 859 ReqID: resp.ReqID, 860 Obj: resp.Data, 861 } 862 863 case GetHeaderProofsMsg: 864 p.Log().Trace("Received headers proof request") 865 // Decode the retrieval message 866 var req struct { 867 ReqID uint64 868 Reqs []ChtReq 869 } 870 if err := msg.Decode(&req); err != nil { 871 return errResp(ErrDecode, "msg %v: %v", msg, err) 872 } 873 // Gather state data until the fetch or network limits is reached 874 var ( 875 bytes int 876 proofs []ChtResp 877 ) 878 reqCnt := len(req.Reqs) 879 if reject(uint64(reqCnt), MaxHelperTrieProofsFetch) { 880 return errResp(ErrRequestRejected, "") 881 } 882 trieDb := trie.NewDatabase(ethdb.NewTable(pm.chainDb, light.ChtTablePrefix)) 883 for _, req := range req.Reqs { 884 if header := pm.blockchain.GetHeaderByNumber(req.BlockNum); header != nil { 885 sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, req.ChtNum*light.CHTFrequencyServer-1) 886 if root := light.GetChtRoot(pm.chainDb, req.ChtNum-1, sectionHead); root != (common.Hash{}) { 887 trie, err := trie.New(root, trieDb) 888 if err != nil { 889 continue 890 } 891 var encNumber [8]byte 892 binary.BigEndian.PutUint64(encNumber[:], req.BlockNum) 893 894 var proof light.NodeList 895 trie.Prove(encNumber[:], 0, &proof) 896 897 proofs = append(proofs, ChtResp{Header: header, Proof: proof}) 898 if bytes += proof.DataSize() + estHeaderRlpSize; bytes >= softResponseLimit { 899 break 900 } 901 } 902 } 903 } 904 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 905 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 906 return p.SendHeaderProofs(req.ReqID, bv, proofs) 907 908 case GetHelperTrieProofsMsg: 909 p.Log().Trace("Received helper trie proof request") 910 // Decode the retrieval message 911 var req struct { 912 ReqID uint64 913 Reqs []HelperTrieReq 914 } 915 if err := msg.Decode(&req); err != nil { 916 return errResp(ErrDecode, "msg %v: %v", msg, err) 917 } 918 // Gather state data until the fetch or network limits is reached 919 var ( 920 auxBytes int 921 auxData [][]byte 922 ) 923 reqCnt := len(req.Reqs) 924 if reject(uint64(reqCnt), MaxHelperTrieProofsFetch) { 925 return errResp(ErrRequestRejected, "") 926 } 927 928 var ( 929 lastIdx uint64 930 lastType uint 931 root common.Hash 932 auxTrie *trie.Trie 933 ) 934 nodes := light.NewNodeSet() 935 for _, req := range req.Reqs { 936 if auxTrie == nil || req.Type != lastType || req.TrieIdx != lastIdx { 937 auxTrie, lastType, lastIdx = nil, req.Type, req.TrieIdx 938 939 var prefix string 940 if root, prefix = pm.getHelperTrie(req.Type, req.TrieIdx); root != (common.Hash{}) { 941 auxTrie, _ = trie.New(root, trie.NewDatabase(ethdb.NewTable(pm.chainDb, prefix))) 942 } 943 } 944 if req.AuxReq == auxRoot { 945 var data []byte 946 if root != (common.Hash{}) { 947 data = root[:] 948 } 949 auxData = append(auxData, data) 950 auxBytes += len(data) 951 } else { 952 if auxTrie != nil { 953 auxTrie.Prove(req.Key, req.FromLevel, nodes) 954 } 955 if req.AuxReq != 0 { 956 data := pm.getHelperTrieAuxData(req) 957 auxData = append(auxData, data) 958 auxBytes += len(data) 959 } 960 } 961 if nodes.DataSize()+auxBytes >= softResponseLimit { 962 break 963 } 964 } 965 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 966 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 967 return p.SendHelperTrieProofs(req.ReqID, bv, HelperTrieResps{Proofs: nodes.NodeList(), AuxData: auxData}) 968 969 case HeaderProofsMsg: 970 if pm.odr == nil { 971 return errResp(ErrUnexpectedResponse, "") 972 } 973 974 p.Log().Trace("Received headers proof response") 975 var resp struct { 976 ReqID, BV uint64 977 Data []ChtResp 978 } 979 if err := msg.Decode(&resp); err != nil { 980 return errResp(ErrDecode, "msg %v: %v", msg, err) 981 } 982 p.fcServer.GotReply(resp.ReqID, resp.BV) 983 deliverMsg = &Msg{ 984 MsgType: MsgHeaderProofs, 985 ReqID: resp.ReqID, 986 Obj: resp.Data, 987 } 988 989 case HelperTrieProofsMsg: 990 if pm.odr == nil { 991 return errResp(ErrUnexpectedResponse, "") 992 } 993 994 p.Log().Trace("Received helper trie proof response") 995 var resp struct { 996 ReqID, BV uint64 997 Data HelperTrieResps 998 } 999 if err := msg.Decode(&resp); err != nil { 1000 return errResp(ErrDecode, "msg %v: %v", msg, err) 1001 } 1002 1003 p.fcServer.GotReply(resp.ReqID, resp.BV) 1004 deliverMsg = &Msg{ 1005 MsgType: MsgHelperTrieProofs, 1006 ReqID: resp.ReqID, 1007 Obj: resp.Data, 1008 } 1009 1010 case SendTxMsg: 1011 if pm.txpool == nil { 1012 return errResp(ErrRequestRejected, "") 1013 } 1014 // Transactions arrived, parse all of them and deliver to the pool 1015 var txs []*types.Transaction 1016 if err := msg.Decode(&txs); err != nil { 1017 return errResp(ErrDecode, "msg %v: %v", msg, err) 1018 } 1019 reqCnt := len(txs) 1020 if reject(uint64(reqCnt), MaxTxSend) { 1021 return errResp(ErrRequestRejected, "") 1022 } 1023 pm.txpool.AddRemotes(txs) 1024 1025 _, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 1026 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 1027 1028 case SendTxV2Msg: 1029 if pm.txpool == nil { 1030 return errResp(ErrRequestRejected, "") 1031 } 1032 // Transactions arrived, parse all of them and deliver to the pool 1033 var req struct { 1034 ReqID uint64 1035 Txs []*types.Transaction 1036 } 1037 if err := msg.Decode(&req); err != nil { 1038 return errResp(ErrDecode, "msg %v: %v", msg, err) 1039 } 1040 reqCnt := len(req.Txs) 1041 if reject(uint64(reqCnt), MaxTxSend) { 1042 return errResp(ErrRequestRejected, "") 1043 } 1044 1045 hashes := make([]common.Hash, len(req.Txs)) 1046 for i, tx := range req.Txs { 1047 hashes[i] = tx.Hash() 1048 } 1049 stats := pm.txStatus(hashes) 1050 for i, stat := range stats { 1051 if stat.Status == core.TxStatusUnknown { 1052 if errs := pm.txpool.AddRemotes([]*types.Transaction{req.Txs[i]}); errs[0] != nil { 1053 stats[i].Error = errs[0].Error() 1054 continue 1055 } 1056 stats[i] = pm.txStatus([]common.Hash{hashes[i]})[0] 1057 } 1058 } 1059 1060 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 1061 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 1062 1063 return p.SendTxStatus(req.ReqID, bv, stats) 1064 1065 case GetTxStatusMsg: 1066 if pm.txpool == nil { 1067 return errResp(ErrUnexpectedResponse, "") 1068 } 1069 // Transactions arrived, parse all of them and deliver to the pool 1070 var req struct { 1071 ReqID uint64 1072 Hashes []common.Hash 1073 } 1074 if err := msg.Decode(&req); err != nil { 1075 return errResp(ErrDecode, "msg %v: %v", msg, err) 1076 } 1077 reqCnt := len(req.Hashes) 1078 if reject(uint64(reqCnt), MaxTxStatus) { 1079 return errResp(ErrRequestRejected, "") 1080 } 1081 bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) 1082 pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) 1083 1084 return p.SendTxStatus(req.ReqID, bv, pm.txStatus(req.Hashes)) 1085 1086 case TxStatusMsg: 1087 if pm.odr == nil { 1088 return errResp(ErrUnexpectedResponse, "") 1089 } 1090 1091 p.Log().Trace("Received tx status response") 1092 var resp struct { 1093 ReqID, BV uint64 1094 Status []txStatus 1095 } 1096 if err := msg.Decode(&resp); err != nil { 1097 return errResp(ErrDecode, "msg %v: %v", msg, err) 1098 } 1099 1100 p.fcServer.GotReply(resp.ReqID, resp.BV) 1101 1102 default: 1103 p.Log().Trace("Received unknown message", "code", msg.Code) 1104 return errResp(ErrInvalidMsgCode, "%v", msg.Code) 1105 } 1106 1107 if deliverMsg != nil { 1108 err := pm.retriever.deliver(p, deliverMsg) 1109 if err != nil { 1110 p.responseErrors++ 1111 if p.responseErrors > maxResponseErrors { 1112 return err 1113 } 1114 } 1115 } 1116 return nil 1117 } 1118 1119 // getAccount retrieves an account from the state based at root. 1120 func (pm *ProtocolManager) getAccount(statedb *state.StateDB, root, hash common.Hash) (state.Account, error) { 1121 trie, err := trie.New(root, statedb.Database().TrieDB()) 1122 if err != nil { 1123 return state.Account{}, err 1124 } 1125 blob, err := trie.TryGet(hash[:]) 1126 if err != nil { 1127 return state.Account{}, err 1128 } 1129 var account state.Account 1130 if err = rlp.DecodeBytes(blob, &account); err != nil { 1131 return state.Account{}, err 1132 } 1133 return account, nil 1134 } 1135 1136 // getHelperTrie returns the post-processed trie root for the given trie ID and section index 1137 func (pm *ProtocolManager) getHelperTrie(id uint, idx uint64) (common.Hash, string) { 1138 switch id { 1139 case htCanonical: 1140 sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, (idx+1)*light.CHTFrequencyClient-1) 1141 return light.GetChtV2Root(pm.chainDb, idx, sectionHead), light.ChtTablePrefix 1142 case htBloomBits: 1143 sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, (idx+1)*light.BloomTrieFrequency-1) 1144 return light.GetBloomTrieRoot(pm.chainDb, idx, sectionHead), light.BloomTrieTablePrefix 1145 } 1146 return common.Hash{}, "" 1147 } 1148 1149 // getHelperTrieAuxData returns requested auxiliary data for the given HelperTrie request 1150 func (pm *ProtocolManager) getHelperTrieAuxData(req HelperTrieReq) []byte { 1151 if req.Type == htCanonical && req.AuxReq == auxHeader && len(req.Key) == 8 { 1152 blockNum := binary.BigEndian.Uint64(req.Key) 1153 hash := rawdb.ReadCanonicalHash(pm.chainDb, blockNum) 1154 return rawdb.ReadHeaderRLP(pm.chainDb, hash, blockNum) 1155 } 1156 return nil 1157 } 1158 1159 func (pm *ProtocolManager) txStatus(hashes []common.Hash) []txStatus { 1160 stats := make([]txStatus, len(hashes)) 1161 for i, stat := range pm.txpool.Status(hashes) { 1162 // Save the status we've got from the transaction pool 1163 stats[i].Status = stat 1164 1165 // If the transaction is unknown to the pool, try looking it up locally 1166 if stat == core.TxStatusUnknown { 1167 if block, number, index := rawdb.ReadTxLookupEntry(pm.chainDb, hashes[i]); block != (common.Hash{}) { 1168 stats[i].Status = core.TxStatusIncluded 1169 stats[i].Lookup = &rawdb.TxLookupEntry{BlockHash: block, BlockIndex: number, Index: index} 1170 } 1171 } 1172 } 1173 return stats 1174 } 1175 1176 // downloaderPeerNotify implements peerSetNotify 1177 type downloaderPeerNotify ProtocolManager 1178 1179 type peerConnection struct { 1180 manager *ProtocolManager 1181 peer *peer 1182 } 1183 1184 func (pc *peerConnection) Head() (common.Hash, *big.Int) { 1185 return pc.peer.HeadAndTd() 1186 } 1187 1188 func (pc *peerConnection) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error { 1189 reqID := genReqID() 1190 rq := &distReq{ 1191 getCost: func(dp distPeer) uint64 { 1192 peer := dp.(*peer) 1193 return peer.GetRequestCost(GetBlockHeadersMsg, amount) 1194 }, 1195 canSend: func(dp distPeer) bool { 1196 return dp.(*peer) == pc.peer 1197 }, 1198 request: func(dp distPeer) func() { 1199 peer := dp.(*peer) 1200 cost := peer.GetRequestCost(GetBlockHeadersMsg, amount) 1201 peer.fcServer.QueueRequest(reqID, cost) 1202 return func() { peer.RequestHeadersByHash(reqID, cost, origin, amount, skip, reverse) } 1203 }, 1204 } 1205 _, ok := <-pc.manager.reqDist.queue(rq) 1206 if !ok { 1207 return light.ErrNoPeers 1208 } 1209 return nil 1210 } 1211 1212 func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error { 1213 reqID := genReqID() 1214 rq := &distReq{ 1215 getCost: func(dp distPeer) uint64 { 1216 peer := dp.(*peer) 1217 return peer.GetRequestCost(GetBlockHeadersMsg, amount) 1218 }, 1219 canSend: func(dp distPeer) bool { 1220 return dp.(*peer) == pc.peer 1221 }, 1222 request: func(dp distPeer) func() { 1223 peer := dp.(*peer) 1224 cost := peer.GetRequestCost(GetBlockHeadersMsg, amount) 1225 peer.fcServer.QueueRequest(reqID, cost) 1226 return func() { peer.RequestHeadersByNumber(reqID, cost, origin, amount, skip, reverse) } 1227 }, 1228 } 1229 _, ok := <-pc.manager.reqDist.queue(rq) 1230 if !ok { 1231 return light.ErrNoPeers 1232 } 1233 return nil 1234 } 1235 1236 func (d *downloaderPeerNotify) registerPeer(p *peer) { 1237 pm := (*ProtocolManager)(d) 1238 pc := &peerConnection{ 1239 manager: pm, 1240 peer: p, 1241 } 1242 pm.downloader.RegisterLightPeer(p.id, ethVersion, pc) 1243 } 1244 1245 func (d *downloaderPeerNotify) unregisterPeer(p *peer) { 1246 pm := (*ProtocolManager)(d) 1247 pm.downloader.UnregisterPeer(p.id) 1248 }