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