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