github.com/bloxroute-labs/bor@v0.1.4/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
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"math/big"
    23  	"math/rand"
    24  	"sync"
    25  	"sync/atomic"
    26  	"time"
    27  
    28  	"github.com/maticnetwork/bor/common"
    29  	"github.com/maticnetwork/bor/common/mclock"
    30  	"github.com/maticnetwork/bor/core"
    31  	"github.com/maticnetwork/bor/core/types"
    32  	"github.com/maticnetwork/bor/eth"
    33  	"github.com/maticnetwork/bor/les/flowcontrol"
    34  	"github.com/maticnetwork/bor/light"
    35  	"github.com/maticnetwork/bor/p2p"
    36  	"github.com/maticnetwork/bor/params"
    37  	"github.com/maticnetwork/bor/rlp"
    38  )
    39  
    40  var (
    41  	errClosed            = errors.New("peer set is closed")
    42  	errAlreadyRegistered = errors.New("peer is already registered")
    43  	errNotRegistered     = errors.New("peer is not registered")
    44  )
    45  
    46  const (
    47  	maxRequestErrors  = 20 // number of invalid requests tolerated (makes the protocol less brittle but still avoids spam)
    48  	maxResponseErrors = 50 // number of invalid responses tolerated (makes the protocol less brittle but still avoids spam)
    49  )
    50  
    51  // capacity limitation for parameter updates
    52  const (
    53  	allowedUpdateBytes = 100000                // initial/maximum allowed update size
    54  	allowedUpdateRate  = time.Millisecond * 10 // time constant for recharging one byte of allowance
    55  )
    56  
    57  const (
    58  	freezeTimeBase    = time.Millisecond * 700 // fixed component of client freeze time
    59  	freezeTimeRandom  = time.Millisecond * 600 // random component of client freeze time
    60  	freezeCheckPeriod = time.Millisecond * 100 // buffer value recheck period after initial freeze time has elapsed
    61  )
    62  
    63  // if the total encoded size of a sent transaction batch is over txSizeCostLimit
    64  // per transaction then the request cost is calculated as proportional to the
    65  // encoded size instead of the transaction count
    66  const txSizeCostLimit = 0x4000
    67  
    68  const (
    69  	announceTypeNone = iota
    70  	announceTypeSimple
    71  	announceTypeSigned
    72  )
    73  
    74  type peer struct {
    75  	*p2p.Peer
    76  	rw p2p.MsgReadWriter
    77  
    78  	version int    // Protocol version negotiated
    79  	network uint64 // Network ID being on
    80  
    81  	announceType uint64
    82  
    83  	// Checkpoint relative fields
    84  	checkpoint       params.TrustedCheckpoint
    85  	checkpointNumber uint64
    86  
    87  	id string
    88  
    89  	headInfo *announceData
    90  	lock     sync.RWMutex
    91  
    92  	sendQueue *execQueue
    93  
    94  	errCh chan error
    95  	// responseLock ensures that responses are queued in the same order as
    96  	// RequestProcessed is called
    97  	responseLock  sync.Mutex
    98  	responseCount uint64
    99  	invalidCount  uint32
   100  
   101  	poolEntry      *poolEntry
   102  	hasBlock       func(common.Hash, uint64, bool) bool
   103  	responseErrors int
   104  	updateCounter  uint64
   105  	updateTime     mclock.AbsTime
   106  	frozen         uint32 // 1 if client is in frozen state
   107  
   108  	fcClient *flowcontrol.ClientNode // nil if the peer is server only
   109  	fcServer *flowcontrol.ServerNode // nil if the peer is client only
   110  	fcParams flowcontrol.ServerParams
   111  	fcCosts  requestCostTable
   112  
   113  	trusted                 bool
   114  	onlyAnnounce            bool
   115  	chainSince, chainRecent uint64
   116  	stateSince, stateRecent uint64
   117  }
   118  
   119  func newPeer(version int, network uint64, trusted bool, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
   120  	return &peer{
   121  		Peer:    p,
   122  		rw:      rw,
   123  		version: version,
   124  		network: network,
   125  		id:      fmt.Sprintf("%x", p.ID().Bytes()),
   126  		trusted: trusted,
   127  		errCh:   make(chan error, 1),
   128  	}
   129  }
   130  
   131  // rejectUpdate returns true if a parameter update has to be rejected because
   132  // the size and/or rate of updates exceed the capacity limitation
   133  func (p *peer) rejectUpdate(size uint64) bool {
   134  	now := mclock.Now()
   135  	if p.updateCounter == 0 {
   136  		p.updateTime = now
   137  	} else {
   138  		dt := now - p.updateTime
   139  		r := uint64(dt / mclock.AbsTime(allowedUpdateRate))
   140  		if p.updateCounter > r {
   141  			p.updateCounter -= r
   142  			p.updateTime += mclock.AbsTime(allowedUpdateRate * time.Duration(r))
   143  		} else {
   144  			p.updateCounter = 0
   145  			p.updateTime = now
   146  		}
   147  	}
   148  	p.updateCounter += size
   149  	return p.updateCounter > allowedUpdateBytes
   150  }
   151  
   152  // freezeClient temporarily puts the client in a frozen state which means all
   153  // unprocessed and subsequent requests are dropped. Unfreezing happens automatically
   154  // after a short time if the client's buffer value is at least in the slightly positive
   155  // region. The client is also notified about being frozen/unfrozen with a Stop/Resume
   156  // message.
   157  func (p *peer) freezeClient() {
   158  	if p.version < lpv3 {
   159  		// if Stop/Resume is not supported then just drop the peer after setting
   160  		// its frozen status permanently
   161  		atomic.StoreUint32(&p.frozen, 1)
   162  		p.Peer.Disconnect(p2p.DiscUselessPeer)
   163  		return
   164  	}
   165  	if atomic.SwapUint32(&p.frozen, 1) == 0 {
   166  		go func() {
   167  			p.SendStop()
   168  			time.Sleep(freezeTimeBase + time.Duration(rand.Int63n(int64(freezeTimeRandom))))
   169  			for {
   170  				bufValue, bufLimit := p.fcClient.BufferStatus()
   171  				if bufLimit == 0 {
   172  					return
   173  				}
   174  				if bufValue <= bufLimit/8 {
   175  					time.Sleep(freezeCheckPeriod)
   176  				} else {
   177  					atomic.StoreUint32(&p.frozen, 0)
   178  					p.SendResume(bufValue)
   179  					break
   180  				}
   181  			}
   182  		}()
   183  	}
   184  }
   185  
   186  // freezeServer processes Stop/Resume messages from the given server
   187  func (p *peer) freezeServer(frozen bool) {
   188  	var f uint32
   189  	if frozen {
   190  		f = 1
   191  	}
   192  	if atomic.SwapUint32(&p.frozen, f) != f && frozen {
   193  		p.sendQueue.clear()
   194  	}
   195  }
   196  
   197  // isFrozen returns true if the client is frozen or the server has put our
   198  // client in frozen state
   199  func (p *peer) isFrozen() bool {
   200  	return atomic.LoadUint32(&p.frozen) != 0
   201  }
   202  
   203  func (p *peer) canQueue() bool {
   204  	return p.sendQueue.canQueue() && !p.isFrozen()
   205  }
   206  
   207  func (p *peer) queueSend(f func()) {
   208  	p.sendQueue.queue(f)
   209  }
   210  
   211  // Info gathers and returns a collection of metadata known about a peer.
   212  func (p *peer) Info() *eth.PeerInfo {
   213  	return &eth.PeerInfo{
   214  		Version:    p.version,
   215  		Difficulty: p.Td(),
   216  		Head:       fmt.Sprintf("%x", p.Head()),
   217  	}
   218  }
   219  
   220  // Head retrieves a copy of the current head (most recent) hash of the peer.
   221  func (p *peer) Head() (hash common.Hash) {
   222  	p.lock.RLock()
   223  	defer p.lock.RUnlock()
   224  
   225  	copy(hash[:], p.headInfo.Hash[:])
   226  	return hash
   227  }
   228  
   229  func (p *peer) HeadAndTd() (hash common.Hash, td *big.Int) {
   230  	p.lock.RLock()
   231  	defer p.lock.RUnlock()
   232  
   233  	copy(hash[:], p.headInfo.Hash[:])
   234  	return hash, p.headInfo.Td
   235  }
   236  
   237  func (p *peer) headBlockInfo() blockInfo {
   238  	p.lock.RLock()
   239  	defer p.lock.RUnlock()
   240  
   241  	return blockInfo{Hash: p.headInfo.Hash, Number: p.headInfo.Number, Td: p.headInfo.Td}
   242  }
   243  
   244  // Td retrieves the current total difficulty of a peer.
   245  func (p *peer) Td() *big.Int {
   246  	p.lock.RLock()
   247  	defer p.lock.RUnlock()
   248  
   249  	return new(big.Int).Set(p.headInfo.Td)
   250  }
   251  
   252  // waitBefore implements distPeer interface
   253  func (p *peer) waitBefore(maxCost uint64) (time.Duration, float64) {
   254  	return p.fcServer.CanSend(maxCost)
   255  }
   256  
   257  // updateCapacity updates the request serving capacity assigned to a given client
   258  // and also sends an announcement about the updated flow control parameters
   259  func (p *peer) updateCapacity(cap uint64) {
   260  	p.responseLock.Lock()
   261  	defer p.responseLock.Unlock()
   262  
   263  	p.fcParams = flowcontrol.ServerParams{MinRecharge: cap, BufLimit: cap * bufLimitRatio}
   264  	p.fcClient.UpdateParams(p.fcParams)
   265  	var kvList keyValueList
   266  	kvList = kvList.add("flowControl/MRR", cap)
   267  	kvList = kvList.add("flowControl/BL", cap*bufLimitRatio)
   268  	p.queueSend(func() { p.SendAnnounce(announceData{Update: kvList}) })
   269  }
   270  
   271  func sendRequest(w p2p.MsgWriter, msgcode, reqID, cost uint64, data interface{}) error {
   272  	type req struct {
   273  		ReqID uint64
   274  		Data  interface{}
   275  	}
   276  	return p2p.Send(w, msgcode, req{reqID, data})
   277  }
   278  
   279  // reply struct represents a reply with the actual data already RLP encoded and
   280  // only the bv (buffer value) missing. This allows the serving mechanism to
   281  // calculate the bv value which depends on the data size before sending the reply.
   282  type reply struct {
   283  	w              p2p.MsgWriter
   284  	msgcode, reqID uint64
   285  	data           rlp.RawValue
   286  }
   287  
   288  // send sends the reply with the calculated buffer value
   289  func (r *reply) send(bv uint64) error {
   290  	type resp struct {
   291  		ReqID, BV uint64
   292  		Data      rlp.RawValue
   293  	}
   294  	return p2p.Send(r.w, r.msgcode, resp{r.reqID, bv, r.data})
   295  }
   296  
   297  // size returns the RLP encoded size of the message data
   298  func (r *reply) size() uint32 {
   299  	return uint32(len(r.data))
   300  }
   301  
   302  func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 {
   303  	p.lock.RLock()
   304  	defer p.lock.RUnlock()
   305  
   306  	costs := p.fcCosts[msgcode]
   307  	if costs == nil {
   308  		return 0
   309  	}
   310  	cost := costs.baseCost + costs.reqCost*uint64(amount)
   311  	if cost > p.fcParams.BufLimit {
   312  		cost = p.fcParams.BufLimit
   313  	}
   314  	return cost
   315  }
   316  
   317  func (p *peer) GetTxRelayCost(amount, size int) uint64 {
   318  	p.lock.RLock()
   319  	defer p.lock.RUnlock()
   320  
   321  	costs := p.fcCosts[SendTxV2Msg]
   322  	if costs == nil {
   323  		return 0
   324  	}
   325  	cost := costs.baseCost + costs.reqCost*uint64(amount)
   326  	sizeCost := costs.baseCost + costs.reqCost*uint64(size)/txSizeCostLimit
   327  	if sizeCost > cost {
   328  		cost = sizeCost
   329  	}
   330  
   331  	if cost > p.fcParams.BufLimit {
   332  		cost = p.fcParams.BufLimit
   333  	}
   334  	return cost
   335  }
   336  
   337  // HasBlock checks if the peer has a given block
   338  func (p *peer) HasBlock(hash common.Hash, number uint64, hasState bool) bool {
   339  	var head, since, recent uint64
   340  	p.lock.RLock()
   341  	if p.headInfo != nil {
   342  		head = p.headInfo.Number
   343  	}
   344  	if hasState {
   345  		since = p.stateSince
   346  		recent = p.stateRecent
   347  	} else {
   348  		since = p.chainSince
   349  		recent = p.chainRecent
   350  	}
   351  	hasBlock := p.hasBlock
   352  	p.lock.RUnlock()
   353  	return head >= number && number >= since && (recent == 0 || number+recent+4 > head) && hasBlock != nil && hasBlock(hash, number, hasState)
   354  }
   355  
   356  // SendAnnounce announces the availability of a number of blocks through
   357  // a hash notification.
   358  func (p *peer) SendAnnounce(request announceData) error {
   359  	return p2p.Send(p.rw, AnnounceMsg, request)
   360  }
   361  
   362  // SendStop notifies the client about being in frozen state
   363  func (p *peer) SendStop() error {
   364  	return p2p.Send(p.rw, StopMsg, struct{}{})
   365  }
   366  
   367  // SendResume notifies the client about getting out of frozen state
   368  func (p *peer) SendResume(bv uint64) error {
   369  	return p2p.Send(p.rw, ResumeMsg, bv)
   370  }
   371  
   372  // ReplyBlockHeaders creates a reply with a batch of block headers
   373  func (p *peer) ReplyBlockHeaders(reqID uint64, headers []*types.Header) *reply {
   374  	data, _ := rlp.EncodeToBytes(headers)
   375  	return &reply{p.rw, BlockHeadersMsg, reqID, data}
   376  }
   377  
   378  // ReplyBlockBodiesRLP creates a reply with a batch of block contents from
   379  // an already RLP encoded format.
   380  func (p *peer) ReplyBlockBodiesRLP(reqID uint64, bodies []rlp.RawValue) *reply {
   381  	data, _ := rlp.EncodeToBytes(bodies)
   382  	return &reply{p.rw, BlockBodiesMsg, reqID, data}
   383  }
   384  
   385  // ReplyCode creates a reply with a batch of arbitrary internal data, corresponding to the
   386  // hashes requested.
   387  func (p *peer) ReplyCode(reqID uint64, codes [][]byte) *reply {
   388  	data, _ := rlp.EncodeToBytes(codes)
   389  	return &reply{p.rw, CodeMsg, reqID, data}
   390  }
   391  
   392  // ReplyReceiptsRLP creates a reply with a batch of transaction receipts, corresponding to the
   393  // ones requested from an already RLP encoded format.
   394  func (p *peer) ReplyReceiptsRLP(reqID uint64, receipts []rlp.RawValue) *reply {
   395  	data, _ := rlp.EncodeToBytes(receipts)
   396  	return &reply{p.rw, ReceiptsMsg, reqID, data}
   397  }
   398  
   399  // ReplyProofsV2 creates a reply with a batch of merkle proofs, corresponding to the ones requested.
   400  func (p *peer) ReplyProofsV2(reqID uint64, proofs light.NodeList) *reply {
   401  	data, _ := rlp.EncodeToBytes(proofs)
   402  	return &reply{p.rw, ProofsV2Msg, reqID, data}
   403  }
   404  
   405  // ReplyHelperTrieProofs creates a reply with a batch of HelperTrie proofs, corresponding to the ones requested.
   406  func (p *peer) ReplyHelperTrieProofs(reqID uint64, resp HelperTrieResps) *reply {
   407  	data, _ := rlp.EncodeToBytes(resp)
   408  	return &reply{p.rw, HelperTrieProofsMsg, reqID, data}
   409  }
   410  
   411  // ReplyTxStatus creates a reply with a batch of transaction status records, corresponding to the ones requested.
   412  func (p *peer) ReplyTxStatus(reqID uint64, stats []light.TxStatus) *reply {
   413  	data, _ := rlp.EncodeToBytes(stats)
   414  	return &reply{p.rw, TxStatusMsg, reqID, data}
   415  }
   416  
   417  // RequestHeadersByHash fetches a batch of blocks' headers corresponding to the
   418  // specified header query, based on the hash of an origin block.
   419  func (p *peer) RequestHeadersByHash(reqID, cost uint64, origin common.Hash, amount int, skip int, reverse bool) error {
   420  	p.Log().Debug("Fetching batch of headers", "count", amount, "fromhash", origin, "skip", skip, "reverse", reverse)
   421  	return sendRequest(p.rw, GetBlockHeadersMsg, reqID, cost, &getBlockHeadersData{Origin: hashOrNumber{Hash: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse})
   422  }
   423  
   424  // RequestHeadersByNumber fetches a batch of blocks' headers corresponding to the
   425  // specified header query, based on the number of an origin block.
   426  func (p *peer) RequestHeadersByNumber(reqID, cost, origin uint64, amount int, skip int, reverse bool) error {
   427  	p.Log().Debug("Fetching batch of headers", "count", amount, "fromnum", origin, "skip", skip, "reverse", reverse)
   428  	return sendRequest(p.rw, GetBlockHeadersMsg, reqID, cost, &getBlockHeadersData{Origin: hashOrNumber{Number: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse})
   429  }
   430  
   431  // RequestBodies fetches a batch of blocks' bodies corresponding to the hashes
   432  // specified.
   433  func (p *peer) RequestBodies(reqID, cost uint64, hashes []common.Hash) error {
   434  	p.Log().Debug("Fetching batch of block bodies", "count", len(hashes))
   435  	return sendRequest(p.rw, GetBlockBodiesMsg, reqID, cost, hashes)
   436  }
   437  
   438  // RequestCode fetches a batch of arbitrary data from a node's known state
   439  // data, corresponding to the specified hashes.
   440  func (p *peer) RequestCode(reqID, cost uint64, reqs []CodeReq) error {
   441  	p.Log().Debug("Fetching batch of codes", "count", len(reqs))
   442  	return sendRequest(p.rw, GetCodeMsg, reqID, cost, reqs)
   443  }
   444  
   445  // RequestReceipts fetches a batch of transaction receipts from a remote node.
   446  func (p *peer) RequestReceipts(reqID, cost uint64, hashes []common.Hash) error {
   447  	p.Log().Debug("Fetching batch of receipts", "count", len(hashes))
   448  	return sendRequest(p.rw, GetReceiptsMsg, reqID, cost, hashes)
   449  }
   450  
   451  // RequestProofs fetches a batch of merkle proofs from a remote node.
   452  func (p *peer) RequestProofs(reqID, cost uint64, reqs []ProofReq) error {
   453  	p.Log().Debug("Fetching batch of proofs", "count", len(reqs))
   454  	return sendRequest(p.rw, GetProofsV2Msg, reqID, cost, reqs)
   455  }
   456  
   457  // RequestHelperTrieProofs fetches a batch of HelperTrie merkle proofs from a remote node.
   458  func (p *peer) RequestHelperTrieProofs(reqID, cost uint64, reqs []HelperTrieReq) error {
   459  	p.Log().Debug("Fetching batch of HelperTrie proofs", "count", len(reqs))
   460  	return sendRequest(p.rw, GetHelperTrieProofsMsg, reqID, cost, reqs)
   461  }
   462  
   463  // RequestTxStatus fetches a batch of transaction status records from a remote node.
   464  func (p *peer) RequestTxStatus(reqID, cost uint64, txHashes []common.Hash) error {
   465  	p.Log().Debug("Requesting transaction status", "count", len(txHashes))
   466  	return sendRequest(p.rw, GetTxStatusMsg, reqID, cost, txHashes)
   467  }
   468  
   469  // SendTxStatus creates a reply with a batch of transactions to be added to the remote transaction pool.
   470  func (p *peer) SendTxs(reqID, cost uint64, txs rlp.RawValue) error {
   471  	p.Log().Debug("Sending batch of transactions", "size", len(txs))
   472  	return sendRequest(p.rw, SendTxV2Msg, reqID, cost, txs)
   473  }
   474  
   475  type keyValueEntry struct {
   476  	Key   string
   477  	Value rlp.RawValue
   478  }
   479  type keyValueList []keyValueEntry
   480  type keyValueMap map[string]rlp.RawValue
   481  
   482  func (l keyValueList) add(key string, val interface{}) keyValueList {
   483  	var entry keyValueEntry
   484  	entry.Key = key
   485  	if val == nil {
   486  		val = uint64(0)
   487  	}
   488  	enc, err := rlp.EncodeToBytes(val)
   489  	if err == nil {
   490  		entry.Value = enc
   491  	}
   492  	return append(l, entry)
   493  }
   494  
   495  func (l keyValueList) decode() (keyValueMap, uint64) {
   496  	m := make(keyValueMap)
   497  	var size uint64
   498  	for _, entry := range l {
   499  		m[entry.Key] = entry.Value
   500  		size += uint64(len(entry.Key)) + uint64(len(entry.Value)) + 8
   501  	}
   502  	return m, size
   503  }
   504  
   505  func (m keyValueMap) get(key string, val interface{}) error {
   506  	enc, ok := m[key]
   507  	if !ok {
   508  		return errResp(ErrMissingKey, "%s", key)
   509  	}
   510  	if val == nil {
   511  		return nil
   512  	}
   513  	return rlp.DecodeBytes(enc, val)
   514  }
   515  
   516  func (p *peer) sendReceiveHandshake(sendList keyValueList) (keyValueList, error) {
   517  	// Send out own handshake in a new thread
   518  	errc := make(chan error, 1)
   519  	go func() {
   520  		errc <- p2p.Send(p.rw, StatusMsg, sendList)
   521  	}()
   522  	// In the mean time retrieve the remote status message
   523  	msg, err := p.rw.ReadMsg()
   524  	if err != nil {
   525  		return nil, err
   526  	}
   527  	if msg.Code != StatusMsg {
   528  		return nil, errResp(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, StatusMsg)
   529  	}
   530  	if msg.Size > ProtocolMaxMsgSize {
   531  		return nil, errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize)
   532  	}
   533  	// Decode the handshake
   534  	var recvList keyValueList
   535  	if err := msg.Decode(&recvList); err != nil {
   536  		return nil, errResp(ErrDecode, "msg %v: %v", msg, err)
   537  	}
   538  	if err := <-errc; err != nil {
   539  		return nil, err
   540  	}
   541  	return recvList, nil
   542  }
   543  
   544  // Handshake executes the les protocol handshake, negotiating version number,
   545  // network IDs, difficulties, head and genesis blocks.
   546  func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis common.Hash, server *LesServer) error {
   547  	p.lock.Lock()
   548  	defer p.lock.Unlock()
   549  
   550  	var send keyValueList
   551  	send = send.add("protocolVersion", uint64(p.version))
   552  	send = send.add("networkId", p.network)
   553  	send = send.add("headTd", td)
   554  	send = send.add("headHash", head)
   555  	send = send.add("headNum", headNum)
   556  	send = send.add("genesisHash", genesis)
   557  	if server != nil {
   558  		if !server.onlyAnnounce {
   559  			send = send.add("serveHeaders", nil)
   560  			send = send.add("serveChainSince", uint64(0))
   561  			send = send.add("serveStateSince", uint64(0))
   562  
   563  			// If local ethereum node is running in archive mode, advertise ourselves we have
   564  			// all version state data. Otherwise only recent state is available.
   565  			stateRecent := uint64(core.TriesInMemory - 4)
   566  			if server.archiveMode {
   567  				stateRecent = 0
   568  			}
   569  			send = send.add("serveRecentState", stateRecent)
   570  			send = send.add("txRelay", nil)
   571  		}
   572  		send = send.add("flowControl/BL", server.defParams.BufLimit)
   573  		send = send.add("flowControl/MRR", server.defParams.MinRecharge)
   574  		var costList RequestCostList
   575  		if server.costTracker != nil {
   576  			costList = server.costTracker.makeCostList(server.costTracker.globalFactor())
   577  		} else {
   578  			costList = testCostList(server.testCost)
   579  		}
   580  		send = send.add("flowControl/MRC", costList)
   581  		p.fcCosts = costList.decode(ProtocolLengths[uint(p.version)])
   582  		p.fcParams = server.defParams
   583  
   584  		if server.protocolManager != nil && server.protocolManager.reg != nil && server.protocolManager.reg.isRunning() {
   585  			cp, height := server.protocolManager.reg.stableCheckpoint()
   586  			if cp != nil {
   587  				send = send.add("checkpoint/value", cp)
   588  				send = send.add("checkpoint/registerHeight", height)
   589  			}
   590  		}
   591  	} else {
   592  		//on client node
   593  		p.announceType = announceTypeSimple
   594  		if p.trusted {
   595  			p.announceType = announceTypeSigned
   596  		}
   597  		send = send.add("announceType", p.announceType)
   598  	}
   599  
   600  	recvList, err := p.sendReceiveHandshake(send)
   601  	if err != nil {
   602  		return err
   603  	}
   604  	recv, size := recvList.decode()
   605  	if p.rejectUpdate(size) {
   606  		return errResp(ErrRequestRejected, "")
   607  	}
   608  
   609  	var rGenesis, rHash common.Hash
   610  	var rVersion, rNetwork, rNum uint64
   611  	var rTd *big.Int
   612  
   613  	if err := recv.get("protocolVersion", &rVersion); err != nil {
   614  		return err
   615  	}
   616  	if err := recv.get("networkId", &rNetwork); err != nil {
   617  		return err
   618  	}
   619  	if err := recv.get("headTd", &rTd); err != nil {
   620  		return err
   621  	}
   622  	if err := recv.get("headHash", &rHash); err != nil {
   623  		return err
   624  	}
   625  	if err := recv.get("headNum", &rNum); err != nil {
   626  		return err
   627  	}
   628  	if err := recv.get("genesisHash", &rGenesis); err != nil {
   629  		return err
   630  	}
   631  
   632  	if rGenesis != genesis {
   633  		return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", rGenesis[:8], genesis[:8])
   634  	}
   635  	if rNetwork != p.network {
   636  		return errResp(ErrNetworkIdMismatch, "%d (!= %d)", rNetwork, p.network)
   637  	}
   638  	if int(rVersion) != p.version {
   639  		return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", rVersion, p.version)
   640  	}
   641  
   642  	if server != nil {
   643  		// until we have a proper peer connectivity API, allow LES connection to other servers
   644  		/*if recv.get("serveStateSince", nil) == nil {
   645  			return errResp(ErrUselessPeer, "wanted client, got server")
   646  		}*/
   647  		if recv.get("announceType", &p.announceType) != nil {
   648  			//set default announceType on server side
   649  			p.announceType = announceTypeSimple
   650  		}
   651  		p.fcClient = flowcontrol.NewClientNode(server.fcManager, server.defParams)
   652  	} else {
   653  		//mark OnlyAnnounce server if "serveHeaders", "serveChainSince", "serveStateSince" or "txRelay" fields don't exist
   654  		if recv.get("serveChainSince", &p.chainSince) != nil {
   655  			p.onlyAnnounce = true
   656  		}
   657  		if recv.get("serveRecentChain", &p.chainRecent) != nil {
   658  			p.chainRecent = 0
   659  		}
   660  		if recv.get("serveStateSince", &p.stateSince) != nil {
   661  			p.onlyAnnounce = true
   662  		}
   663  		if recv.get("serveRecentState", &p.stateRecent) != nil {
   664  			p.stateRecent = 0
   665  		}
   666  		if recv.get("txRelay", nil) != nil {
   667  			p.onlyAnnounce = true
   668  		}
   669  
   670  		if p.onlyAnnounce && !p.trusted {
   671  			return errResp(ErrUselessPeer, "peer cannot serve requests")
   672  		}
   673  
   674  		var sParams flowcontrol.ServerParams
   675  		if err := recv.get("flowControl/BL", &sParams.BufLimit); err != nil {
   676  			return err
   677  		}
   678  		if err := recv.get("flowControl/MRR", &sParams.MinRecharge); err != nil {
   679  			return err
   680  		}
   681  		var MRC RequestCostList
   682  		if err := recv.get("flowControl/MRC", &MRC); err != nil {
   683  			return err
   684  		}
   685  		p.fcParams = sParams
   686  		p.fcServer = flowcontrol.NewServerNode(sParams, &mclock.System{})
   687  		p.fcCosts = MRC.decode(ProtocolLengths[uint(p.version)])
   688  
   689  		recv.get("checkpoint/value", &p.checkpoint)
   690  		recv.get("checkpoint/registerHeight", &p.checkpointNumber)
   691  
   692  		if !p.onlyAnnounce {
   693  			for msgCode := range reqAvgTimeCost {
   694  				if p.fcCosts[msgCode] == nil {
   695  					return errResp(ErrUselessPeer, "peer does not support message %d", msgCode)
   696  				}
   697  			}
   698  		}
   699  	}
   700  	p.headInfo = &announceData{Td: rTd, Hash: rHash, Number: rNum}
   701  	return nil
   702  }
   703  
   704  // updateFlowControl updates the flow control parameters belonging to the server
   705  // node if the announced key/value set contains relevant fields
   706  func (p *peer) updateFlowControl(update keyValueMap) {
   707  	if p.fcServer == nil {
   708  		return
   709  	}
   710  	params := p.fcParams
   711  	updateParams := false
   712  	if update.get("flowControl/BL", &params.BufLimit) == nil {
   713  		updateParams = true
   714  	}
   715  	if update.get("flowControl/MRR", &params.MinRecharge) == nil {
   716  		updateParams = true
   717  	}
   718  	if updateParams {
   719  		p.fcParams = params
   720  		p.fcServer.UpdateParams(params)
   721  	}
   722  	var MRC RequestCostList
   723  	if update.get("flowControl/MRC", &MRC) == nil {
   724  		costUpdate := MRC.decode(ProtocolLengths[uint(p.version)])
   725  		for code, cost := range costUpdate {
   726  			p.fcCosts[code] = cost
   727  		}
   728  	}
   729  }
   730  
   731  // String implements fmt.Stringer.
   732  func (p *peer) String() string {
   733  	return fmt.Sprintf("Peer %s [%s]", p.id,
   734  		fmt.Sprintf("les/%d", p.version),
   735  	)
   736  }
   737  
   738  // peerSetNotify is a callback interface to notify services about added or
   739  // removed peers
   740  type peerSetNotify interface {
   741  	registerPeer(*peer)
   742  	unregisterPeer(*peer)
   743  }
   744  
   745  // peerSet represents the collection of active peers currently participating in
   746  // the Light Ethereum sub-protocol.
   747  type peerSet struct {
   748  	peers      map[string]*peer
   749  	lock       sync.RWMutex
   750  	notifyList []peerSetNotify
   751  	closed     bool
   752  }
   753  
   754  // newPeerSet creates a new peer set to track the active participants.
   755  func newPeerSet() *peerSet {
   756  	return &peerSet{
   757  		peers: make(map[string]*peer),
   758  	}
   759  }
   760  
   761  // notify adds a service to be notified about added or removed peers
   762  func (ps *peerSet) notify(n peerSetNotify) {
   763  	ps.lock.Lock()
   764  	ps.notifyList = append(ps.notifyList, n)
   765  	peers := make([]*peer, 0, len(ps.peers))
   766  	for _, p := range ps.peers {
   767  		peers = append(peers, p)
   768  	}
   769  	ps.lock.Unlock()
   770  
   771  	for _, p := range peers {
   772  		n.registerPeer(p)
   773  	}
   774  }
   775  
   776  // Register injects a new peer into the working set, or returns an error if the
   777  // peer is already known.
   778  func (ps *peerSet) Register(p *peer) error {
   779  	ps.lock.Lock()
   780  	if ps.closed {
   781  		ps.lock.Unlock()
   782  		return errClosed
   783  	}
   784  	if _, ok := ps.peers[p.id]; ok {
   785  		ps.lock.Unlock()
   786  		return errAlreadyRegistered
   787  	}
   788  	ps.peers[p.id] = p
   789  	p.sendQueue = newExecQueue(100)
   790  	peers := make([]peerSetNotify, len(ps.notifyList))
   791  	copy(peers, ps.notifyList)
   792  	ps.lock.Unlock()
   793  
   794  	for _, n := range peers {
   795  		n.registerPeer(p)
   796  	}
   797  	return nil
   798  }
   799  
   800  // Unregister removes a remote peer from the active set, disabling any further
   801  // actions to/from that particular entity. It also initiates disconnection at the networking layer.
   802  func (ps *peerSet) Unregister(id string) error {
   803  	ps.lock.Lock()
   804  	if p, ok := ps.peers[id]; !ok {
   805  		ps.lock.Unlock()
   806  		return errNotRegistered
   807  	} else {
   808  		delete(ps.peers, id)
   809  		peers := make([]peerSetNotify, len(ps.notifyList))
   810  		copy(peers, ps.notifyList)
   811  		ps.lock.Unlock()
   812  
   813  		for _, n := range peers {
   814  			n.unregisterPeer(p)
   815  		}
   816  
   817  		p.sendQueue.quit()
   818  		p.Peer.Disconnect(p2p.DiscUselessPeer)
   819  
   820  		return nil
   821  	}
   822  }
   823  
   824  // AllPeerIDs returns a list of all registered peer IDs
   825  func (ps *peerSet) AllPeerIDs() []string {
   826  	ps.lock.RLock()
   827  	defer ps.lock.RUnlock()
   828  
   829  	res := make([]string, len(ps.peers))
   830  	idx := 0
   831  	for id := range ps.peers {
   832  		res[idx] = id
   833  		idx++
   834  	}
   835  	return res
   836  }
   837  
   838  // Peer retrieves the registered peer with the given id.
   839  func (ps *peerSet) Peer(id string) *peer {
   840  	ps.lock.RLock()
   841  	defer ps.lock.RUnlock()
   842  
   843  	return ps.peers[id]
   844  }
   845  
   846  // Len returns if the current number of peers in the set.
   847  func (ps *peerSet) Len() int {
   848  	ps.lock.RLock()
   849  	defer ps.lock.RUnlock()
   850  
   851  	return len(ps.peers)
   852  }
   853  
   854  // BestPeer retrieves the known peer with the currently highest total difficulty.
   855  func (ps *peerSet) BestPeer() *peer {
   856  	ps.lock.RLock()
   857  	defer ps.lock.RUnlock()
   858  
   859  	var (
   860  		bestPeer *peer
   861  		bestTd   *big.Int
   862  	)
   863  	for _, p := range ps.peers {
   864  		if td := p.Td(); bestPeer == nil || td.Cmp(bestTd) > 0 {
   865  			bestPeer, bestTd = p, td
   866  		}
   867  	}
   868  	return bestPeer
   869  }
   870  
   871  // AllPeers returns all peers in a list
   872  func (ps *peerSet) AllPeers() []*peer {
   873  	ps.lock.RLock()
   874  	defer ps.lock.RUnlock()
   875  
   876  	list := make([]*peer, len(ps.peers))
   877  	i := 0
   878  	for _, peer := range ps.peers {
   879  		list[i] = peer
   880  		i++
   881  	}
   882  	return list
   883  }
   884  
   885  // Close disconnects all peers.
   886  // No new peers can be registered after Close has returned.
   887  func (ps *peerSet) Close() {
   888  	ps.lock.Lock()
   889  	defer ps.lock.Unlock()
   890  
   891  	for _, p := range ps.peers {
   892  		p.Disconnect(p2p.DiscQuitting)
   893  	}
   894  	ps.closed = true
   895  }