github.com/quinndk/ethereum_read@v0.0.0-20181211143958-29c55eec3237/go-ethereum-master_read/les/peer.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 "crypto/ecdsa" 22 "encoding/binary" 23 "errors" 24 "fmt" 25 "math/big" 26 "sync" 27 "time" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/core/types" 31 "github.com/ethereum/go-ethereum/eth" 32 "github.com/ethereum/go-ethereum/les/flowcontrol" 33 "github.com/ethereum/go-ethereum/light" 34 "github.com/ethereum/go-ethereum/p2p" 35 "github.com/ethereum/go-ethereum/rlp" 36 ) 37 38 var ( 39 errClosed = errors.New("peer set is closed") 40 errAlreadyRegistered = errors.New("peer is already registered") 41 errNotRegistered = errors.New("peer is not registered") 42 ) 43 44 const maxResponseErrors = 50 // number of invalid responses tolerated (makes the protocol less brittle but still avoids spam) 45 46 const ( 47 announceTypeNone = iota 48 announceTypeSimple 49 announceTypeSigned 50 ) 51 52 type peer struct { 53 *p2p.Peer 54 pubKey *ecdsa.PublicKey 55 56 rw p2p.MsgReadWriter 57 58 version int // Protocol version negotiated 59 network uint64 // Network ID being on 60 61 announceType, requestAnnounceType uint64 62 63 id string 64 65 headInfo *announceData 66 lock sync.RWMutex 67 68 announceChn chan announceData 69 sendQueue *execQueue 70 71 poolEntry *poolEntry 72 hasBlock func(common.Hash, uint64) bool 73 responseErrors int 74 75 fcClient *flowcontrol.ClientNode // nil if the peer is server only 76 fcServer *flowcontrol.ServerNode // nil if the peer is client only 77 fcServerParams *flowcontrol.ServerParams 78 fcCosts requestCostTable 79 } 80 81 func newPeer(version int, network uint64, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { 82 id := p.ID() 83 pubKey, _ := id.Pubkey() 84 85 return &peer{ 86 Peer: p, 87 pubKey: pubKey, 88 rw: rw, 89 version: version, 90 network: network, 91 id: fmt.Sprintf("%x", id[:8]), 92 announceChn: make(chan announceData, 20), 93 } 94 } 95 96 func (p *peer) canQueue() bool { 97 return p.sendQueue.canQueue() 98 } 99 100 func (p *peer) queueSend(f func()) { 101 p.sendQueue.queue(f) 102 } 103 104 // Info gathers and returns a collection of metadata known about a peer. 105 func (p *peer) Info() *eth.PeerInfo { 106 return ð.PeerInfo{ 107 Version: p.version, 108 Difficulty: p.Td(), 109 Head: fmt.Sprintf("%x", p.Head()), 110 } 111 } 112 113 // Head retrieves a copy of the current head (most recent) hash of the peer. 114 func (p *peer) Head() (hash common.Hash) { 115 p.lock.RLock() 116 defer p.lock.RUnlock() 117 118 copy(hash[:], p.headInfo.Hash[:]) 119 return hash 120 } 121 122 func (p *peer) HeadAndTd() (hash common.Hash, td *big.Int) { 123 p.lock.RLock() 124 defer p.lock.RUnlock() 125 126 copy(hash[:], p.headInfo.Hash[:]) 127 return hash, p.headInfo.Td 128 } 129 130 func (p *peer) headBlockInfo() blockInfo { 131 p.lock.RLock() 132 defer p.lock.RUnlock() 133 134 return blockInfo{Hash: p.headInfo.Hash, Number: p.headInfo.Number, Td: p.headInfo.Td} 135 } 136 137 // Td retrieves the current total difficulty of a peer. 138 func (p *peer) Td() *big.Int { 139 p.lock.RLock() 140 defer p.lock.RUnlock() 141 142 return new(big.Int).Set(p.headInfo.Td) 143 } 144 145 // waitBefore implements distPeer interface 146 func (p *peer) waitBefore(maxCost uint64) (time.Duration, float64) { 147 return p.fcServer.CanSend(maxCost) 148 } 149 150 func sendRequest(w p2p.MsgWriter, msgcode, reqID, cost uint64, data interface{}) error { 151 type req struct { 152 ReqID uint64 153 Data interface{} 154 } 155 return p2p.Send(w, msgcode, req{reqID, data}) 156 } 157 158 func sendResponse(w p2p.MsgWriter, msgcode, reqID, bv uint64, data interface{}) error { 159 type resp struct { 160 ReqID, BV uint64 161 Data interface{} 162 } 163 return p2p.Send(w, msgcode, resp{reqID, bv, data}) 164 } 165 166 func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 { 167 p.lock.RLock() 168 defer p.lock.RUnlock() 169 170 cost := p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(amount) 171 if cost > p.fcServerParams.BufLimit { 172 cost = p.fcServerParams.BufLimit 173 } 174 return cost 175 } 176 177 // HasBlock checks if the peer has a given block 178 func (p *peer) HasBlock(hash common.Hash, number uint64) bool { 179 p.lock.RLock() 180 hasBlock := p.hasBlock 181 p.lock.RUnlock() 182 return hasBlock != nil && hasBlock(hash, number) 183 } 184 185 // SendAnnounce announces the availability of a number of blocks through 186 // a hash notification. 187 func (p *peer) SendAnnounce(request announceData) error { 188 return p2p.Send(p.rw, AnnounceMsg, request) 189 } 190 191 // SendBlockHeaders sends a batch of block headers to the remote peer. 192 func (p *peer) SendBlockHeaders(reqID, bv uint64, headers []*types.Header) error { 193 return sendResponse(p.rw, BlockHeadersMsg, reqID, bv, headers) 194 } 195 196 // SendBlockBodiesRLP sends a batch of block contents to the remote peer from 197 // an already RLP encoded format. 198 func (p *peer) SendBlockBodiesRLP(reqID, bv uint64, bodies []rlp.RawValue) error { 199 return sendResponse(p.rw, BlockBodiesMsg, reqID, bv, bodies) 200 } 201 202 // SendCodeRLP sends a batch of arbitrary internal data, corresponding to the 203 // hashes requested. 204 func (p *peer) SendCode(reqID, bv uint64, data [][]byte) error { 205 return sendResponse(p.rw, CodeMsg, reqID, bv, data) 206 } 207 208 // SendReceiptsRLP sends a batch of transaction receipts, corresponding to the 209 // ones requested from an already RLP encoded format. 210 func (p *peer) SendReceiptsRLP(reqID, bv uint64, receipts []rlp.RawValue) error { 211 return sendResponse(p.rw, ReceiptsMsg, reqID, bv, receipts) 212 } 213 214 // SendProofs sends a batch of legacy LES/1 merkle proofs, corresponding to the ones requested. 215 func (p *peer) SendProofs(reqID, bv uint64, proofs proofsData) error { 216 return sendResponse(p.rw, ProofsV1Msg, reqID, bv, proofs) 217 } 218 219 // SendProofsV2 sends a batch of merkle proofs, corresponding to the ones requested. 220 func (p *peer) SendProofsV2(reqID, bv uint64, proofs light.NodeList) error { 221 return sendResponse(p.rw, ProofsV2Msg, reqID, bv, proofs) 222 } 223 224 // SendHeaderProofs sends a batch of legacy LES/1 header proofs, corresponding to the ones requested. 225 func (p *peer) SendHeaderProofs(reqID, bv uint64, proofs []ChtResp) error { 226 return sendResponse(p.rw, HeaderProofsMsg, reqID, bv, proofs) 227 } 228 229 // SendHelperTrieProofs sends a batch of HelperTrie proofs, corresponding to the ones requested. 230 func (p *peer) SendHelperTrieProofs(reqID, bv uint64, resp HelperTrieResps) error { 231 return sendResponse(p.rw, HelperTrieProofsMsg, reqID, bv, resp) 232 } 233 234 // SendTxStatus sends a batch of transaction status records, corresponding to the ones requested. 235 func (p *peer) SendTxStatus(reqID, bv uint64, stats []txStatus) error { 236 return sendResponse(p.rw, TxStatusMsg, reqID, bv, stats) 237 } 238 239 // RequestHeadersByHash fetches a batch of blocks' headers corresponding to the 240 // specified header query, based on the hash of an origin block. 241 func (p *peer) RequestHeadersByHash(reqID, cost uint64, origin common.Hash, amount int, skip int, reverse bool) error { 242 p.Log().Debug("Fetching batch of headers", "count", amount, "fromhash", origin, "skip", skip, "reverse", reverse) 243 return sendRequest(p.rw, GetBlockHeadersMsg, reqID, cost, &getBlockHeadersData{Origin: hashOrNumber{Hash: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse}) 244 } 245 246 // RequestHeadersByNumber fetches a batch of blocks' headers corresponding to the 247 // specified header query, based on the number of an origin block. 248 func (p *peer) RequestHeadersByNumber(reqID, cost, origin uint64, amount int, skip int, reverse bool) error { 249 p.Log().Debug("Fetching batch of headers", "count", amount, "fromnum", origin, "skip", skip, "reverse", reverse) 250 return sendRequest(p.rw, GetBlockHeadersMsg, reqID, cost, &getBlockHeadersData{Origin: hashOrNumber{Number: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse}) 251 } 252 253 // RequestBodies fetches a batch of blocks' bodies corresponding to the hashes 254 // specified. 255 func (p *peer) RequestBodies(reqID, cost uint64, hashes []common.Hash) error { 256 p.Log().Debug("Fetching batch of block bodies", "count", len(hashes)) 257 return sendRequest(p.rw, GetBlockBodiesMsg, reqID, cost, hashes) 258 } 259 260 // RequestCode fetches a batch of arbitrary data from a node's known state 261 // data, corresponding to the specified hashes. 262 func (p *peer) RequestCode(reqID, cost uint64, reqs []CodeReq) error { 263 p.Log().Debug("Fetching batch of codes", "count", len(reqs)) 264 return sendRequest(p.rw, GetCodeMsg, reqID, cost, reqs) 265 } 266 267 // RequestReceipts fetches a batch of transaction receipts from a remote node. 268 func (p *peer) RequestReceipts(reqID, cost uint64, hashes []common.Hash) error { 269 p.Log().Debug("Fetching batch of receipts", "count", len(hashes)) 270 return sendRequest(p.rw, GetReceiptsMsg, reqID, cost, hashes) 271 } 272 273 // RequestProofs fetches a batch of merkle proofs from a remote node. 274 func (p *peer) RequestProofs(reqID, cost uint64, reqs []ProofReq) error { 275 p.Log().Debug("Fetching batch of proofs", "count", len(reqs)) 276 switch p.version { 277 case lpv1: 278 return sendRequest(p.rw, GetProofsV1Msg, reqID, cost, reqs) 279 case lpv2: 280 return sendRequest(p.rw, GetProofsV2Msg, reqID, cost, reqs) 281 default: 282 panic(nil) 283 } 284 } 285 286 // RequestHelperTrieProofs fetches a batch of HelperTrie merkle proofs from a remote node. 287 func (p *peer) RequestHelperTrieProofs(reqID, cost uint64, reqs []HelperTrieReq) error { 288 p.Log().Debug("Fetching batch of HelperTrie proofs", "count", len(reqs)) 289 switch p.version { 290 case lpv1: 291 reqsV1 := make([]ChtReq, len(reqs)) 292 for i, req := range reqs { 293 if req.Type != htCanonical || req.AuxReq != auxHeader || len(req.Key) != 8 { 294 return fmt.Errorf("Request invalid in LES/1 mode") 295 } 296 blockNum := binary.BigEndian.Uint64(req.Key) 297 // convert HelperTrie request to old CHT request 298 reqsV1[i] = ChtReq{ChtNum: (req.TrieIdx + 1) * (light.CHTFrequencyClient / light.CHTFrequencyServer), BlockNum: blockNum, FromLevel: req.FromLevel} 299 } 300 return sendRequest(p.rw, GetHeaderProofsMsg, reqID, cost, reqsV1) 301 case lpv2: 302 return sendRequest(p.rw, GetHelperTrieProofsMsg, reqID, cost, reqs) 303 default: 304 panic(nil) 305 } 306 } 307 308 // RequestTxStatus fetches a batch of transaction status records from a remote node. 309 func (p *peer) RequestTxStatus(reqID, cost uint64, txHashes []common.Hash) error { 310 p.Log().Debug("Requesting transaction status", "count", len(txHashes)) 311 return sendRequest(p.rw, GetTxStatusMsg, reqID, cost, txHashes) 312 } 313 314 // SendTxStatus sends a batch of transactions to be added to the remote transaction pool. 315 func (p *peer) SendTxs(reqID, cost uint64, txs types.Transactions) error { 316 p.Log().Debug("Fetching batch of transactions", "count", len(txs)) 317 switch p.version { 318 case lpv1: 319 return p2p.Send(p.rw, SendTxMsg, txs) // old message format does not include reqID 320 case lpv2: 321 return sendRequest(p.rw, SendTxV2Msg, reqID, cost, txs) 322 default: 323 panic(nil) 324 } 325 } 326 327 type keyValueEntry struct { 328 Key string 329 Value rlp.RawValue 330 } 331 type keyValueList []keyValueEntry 332 type keyValueMap map[string]rlp.RawValue 333 334 func (l keyValueList) add(key string, val interface{}) keyValueList { 335 var entry keyValueEntry 336 entry.Key = key 337 if val == nil { 338 val = uint64(0) 339 } 340 enc, err := rlp.EncodeToBytes(val) 341 if err == nil { 342 entry.Value = enc 343 } 344 return append(l, entry) 345 } 346 347 func (l keyValueList) decode() keyValueMap { 348 m := make(keyValueMap) 349 for _, entry := range l { 350 m[entry.Key] = entry.Value 351 } 352 return m 353 } 354 355 func (m keyValueMap) get(key string, val interface{}) error { 356 enc, ok := m[key] 357 if !ok { 358 return errResp(ErrMissingKey, "%s", key) 359 } 360 if val == nil { 361 return nil 362 } 363 return rlp.DecodeBytes(enc, val) 364 } 365 366 func (p *peer) sendReceiveHandshake(sendList keyValueList) (keyValueList, error) { 367 // Send out own handshake in a new thread 368 errc := make(chan error, 1) 369 go func() { 370 errc <- p2p.Send(p.rw, StatusMsg, sendList) 371 }() 372 // In the mean time retrieve the remote status message 373 msg, err := p.rw.ReadMsg() 374 if err != nil { 375 return nil, err 376 } 377 if msg.Code != StatusMsg { 378 return nil, errResp(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, StatusMsg) 379 } 380 if msg.Size > ProtocolMaxMsgSize { 381 return nil, errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) 382 } 383 // Decode the handshake 384 var recvList keyValueList 385 if err := msg.Decode(&recvList); err != nil { 386 return nil, errResp(ErrDecode, "msg %v: %v", msg, err) 387 } 388 if err := <-errc; err != nil { 389 return nil, err 390 } 391 return recvList, nil 392 } 393 394 // Handshake executes the les protocol handshake, negotiating version number, 395 // network IDs, difficulties, head and genesis blocks. 396 func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis common.Hash, server *LesServer) error { 397 p.lock.Lock() 398 defer p.lock.Unlock() 399 400 var send keyValueList 401 send = send.add("protocolVersion", uint64(p.version)) 402 send = send.add("networkId", p.network) 403 send = send.add("headTd", td) 404 send = send.add("headHash", head) 405 send = send.add("headNum", headNum) 406 send = send.add("genesisHash", genesis) 407 if server != nil { 408 send = send.add("serveHeaders", nil) 409 send = send.add("serveChainSince", uint64(0)) 410 send = send.add("serveStateSince", uint64(0)) 411 send = send.add("txRelay", nil) 412 send = send.add("flowControl/BL", server.defParams.BufLimit) 413 send = send.add("flowControl/MRR", server.defParams.MinRecharge) 414 list := server.fcCostStats.getCurrentList() 415 send = send.add("flowControl/MRC", list) 416 p.fcCosts = list.decode() 417 } else { 418 p.requestAnnounceType = announceTypeSimple // set to default until "very light" client mode is implemented 419 send = send.add("announceType", p.requestAnnounceType) 420 } 421 recvList, err := p.sendReceiveHandshake(send) 422 if err != nil { 423 return err 424 } 425 recv := recvList.decode() 426 427 var rGenesis, rHash common.Hash 428 var rVersion, rNetwork, rNum uint64 429 var rTd *big.Int 430 431 if err := recv.get("protocolVersion", &rVersion); err != nil { 432 return err 433 } 434 if err := recv.get("networkId", &rNetwork); err != nil { 435 return err 436 } 437 if err := recv.get("headTd", &rTd); err != nil { 438 return err 439 } 440 if err := recv.get("headHash", &rHash); err != nil { 441 return err 442 } 443 if err := recv.get("headNum", &rNum); err != nil { 444 return err 445 } 446 if err := recv.get("genesisHash", &rGenesis); err != nil { 447 return err 448 } 449 450 if rGenesis != genesis { 451 return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", rGenesis[:8], genesis[:8]) 452 } 453 if rNetwork != p.network { 454 return errResp(ErrNetworkIdMismatch, "%d (!= %d)", rNetwork, p.network) 455 } 456 if int(rVersion) != p.version { 457 return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", rVersion, p.version) 458 } 459 if server != nil { 460 // until we have a proper peer connectivity API, allow LES connection to other servers 461 /*if recv.get("serveStateSince", nil) == nil { 462 return errResp(ErrUselessPeer, "wanted client, got server") 463 }*/ 464 if recv.get("announceType", &p.announceType) != nil { 465 p.announceType = announceTypeSimple 466 } 467 p.fcClient = flowcontrol.NewClientNode(server.fcManager, server.defParams) 468 } else { 469 if recv.get("serveChainSince", nil) != nil { 470 return errResp(ErrUselessPeer, "peer cannot serve chain") 471 } 472 if recv.get("serveStateSince", nil) != nil { 473 return errResp(ErrUselessPeer, "peer cannot serve state") 474 } 475 if recv.get("txRelay", nil) != nil { 476 return errResp(ErrUselessPeer, "peer cannot relay transactions") 477 } 478 params := &flowcontrol.ServerParams{} 479 if err := recv.get("flowControl/BL", ¶ms.BufLimit); err != nil { 480 return err 481 } 482 if err := recv.get("flowControl/MRR", ¶ms.MinRecharge); err != nil { 483 return err 484 } 485 var MRC RequestCostList 486 if err := recv.get("flowControl/MRC", &MRC); err != nil { 487 return err 488 } 489 p.fcServerParams = params 490 p.fcServer = flowcontrol.NewServerNode(params) 491 p.fcCosts = MRC.decode() 492 } 493 494 p.headInfo = &announceData{Td: rTd, Hash: rHash, Number: rNum} 495 return nil 496 } 497 498 // String implements fmt.Stringer. 499 func (p *peer) String() string { 500 return fmt.Sprintf("Peer %s [%s]", p.id, 501 fmt.Sprintf("les/%d", p.version), 502 ) 503 } 504 505 // peerSetNotify is a callback interface to notify services about added or 506 // removed peers 507 type peerSetNotify interface { 508 registerPeer(*peer) 509 unregisterPeer(*peer) 510 } 511 512 // peerSet represents the collection of active peers currently participating in 513 // the Light Ethereum sub-protocol. 514 type peerSet struct { 515 peers map[string]*peer 516 lock sync.RWMutex 517 notifyList []peerSetNotify 518 closed bool 519 } 520 521 // newPeerSet creates a new peer set to track the active participants. 522 func newPeerSet() *peerSet { 523 return &peerSet{ 524 peers: make(map[string]*peer), 525 } 526 } 527 528 // notify adds a service to be notified about added or removed peers 529 func (ps *peerSet) notify(n peerSetNotify) { 530 ps.lock.Lock() 531 ps.notifyList = append(ps.notifyList, n) 532 peers := make([]*peer, 0, len(ps.peers)) 533 for _, p := range ps.peers { 534 peers = append(peers, p) 535 } 536 ps.lock.Unlock() 537 538 for _, p := range peers { 539 n.registerPeer(p) 540 } 541 } 542 543 // Register injects a new peer into the working set, or returns an error if the 544 // peer is already known. 545 func (ps *peerSet) Register(p *peer) error { 546 ps.lock.Lock() 547 if ps.closed { 548 ps.lock.Unlock() 549 return errClosed 550 } 551 if _, ok := ps.peers[p.id]; ok { 552 ps.lock.Unlock() 553 return errAlreadyRegistered 554 } 555 ps.peers[p.id] = p 556 p.sendQueue = newExecQueue(100) 557 peers := make([]peerSetNotify, len(ps.notifyList)) 558 copy(peers, ps.notifyList) 559 ps.lock.Unlock() 560 561 for _, n := range peers { 562 n.registerPeer(p) 563 } 564 return nil 565 } 566 567 // Unregister removes a remote peer from the active set, disabling any further 568 // actions to/from that particular entity. It also initiates disconnection at the networking layer. 569 func (ps *peerSet) Unregister(id string) error { 570 ps.lock.Lock() 571 if p, ok := ps.peers[id]; !ok { 572 ps.lock.Unlock() 573 return errNotRegistered 574 } else { 575 delete(ps.peers, id) 576 peers := make([]peerSetNotify, len(ps.notifyList)) 577 copy(peers, ps.notifyList) 578 ps.lock.Unlock() 579 580 for _, n := range peers { 581 n.unregisterPeer(p) 582 } 583 p.sendQueue.quit() 584 p.Peer.Disconnect(p2p.DiscUselessPeer) 585 return nil 586 } 587 } 588 589 // AllPeerIDs returns a list of all registered peer IDs 590 func (ps *peerSet) AllPeerIDs() []string { 591 ps.lock.RLock() 592 defer ps.lock.RUnlock() 593 594 res := make([]string, len(ps.peers)) 595 idx := 0 596 for id := range ps.peers { 597 res[idx] = id 598 idx++ 599 } 600 return res 601 } 602 603 // Peer retrieves the registered peer with the given id. 604 func (ps *peerSet) Peer(id string) *peer { 605 ps.lock.RLock() 606 defer ps.lock.RUnlock() 607 608 return ps.peers[id] 609 } 610 611 // Len returns if the current number of peers in the set. 612 func (ps *peerSet) Len() int { 613 ps.lock.RLock() 614 defer ps.lock.RUnlock() 615 616 return len(ps.peers) 617 } 618 619 // BestPeer retrieves the known peer with the currently highest total difficulty. 620 func (ps *peerSet) BestPeer() *peer { 621 ps.lock.RLock() 622 defer ps.lock.RUnlock() 623 624 var ( 625 bestPeer *peer 626 bestTd *big.Int 627 ) 628 for _, p := range ps.peers { 629 if td := p.Td(); bestPeer == nil || td.Cmp(bestTd) > 0 { 630 bestPeer, bestTd = p, td 631 } 632 } 633 return bestPeer 634 } 635 636 // AllPeers returns all peers in a list 637 func (ps *peerSet) AllPeers() []*peer { 638 ps.lock.RLock() 639 defer ps.lock.RUnlock() 640 641 list := make([]*peer, len(ps.peers)) 642 i := 0 643 for _, peer := range ps.peers { 644 list[i] = peer 645 i++ 646 } 647 return list 648 } 649 650 // Close disconnects all peers. 651 // No new peers can be registered after Close has returned. 652 func (ps *peerSet) Close() { 653 ps.lock.Lock() 654 defer ps.lock.Unlock() 655 656 for _, p := range ps.peers { 657 p.Disconnect(p2p.DiscQuitting) 658 } 659 ps.closed = true 660 }