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