github.com/AlohaMobile/go-ethereum@v1.9.7/les/server_handler.go (about)

     1  // Copyright 2019 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  	"encoding/binary"
    21  	"encoding/json"
    22  	"errors"
    23  	"sync"
    24  	"sync/atomic"
    25  	"time"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/common/mclock"
    29  	"github.com/ethereum/go-ethereum/core"
    30  	"github.com/ethereum/go-ethereum/core/rawdb"
    31  	"github.com/ethereum/go-ethereum/core/state"
    32  	"github.com/ethereum/go-ethereum/core/types"
    33  	"github.com/ethereum/go-ethereum/ethdb"
    34  	"github.com/ethereum/go-ethereum/light"
    35  	"github.com/ethereum/go-ethereum/log"
    36  	"github.com/ethereum/go-ethereum/metrics"
    37  	"github.com/ethereum/go-ethereum/p2p"
    38  	"github.com/ethereum/go-ethereum/rlp"
    39  	"github.com/ethereum/go-ethereum/trie"
    40  )
    41  
    42  const (
    43  	softResponseLimit = 2 * 1024 * 1024 // Target maximum size of returned blocks, headers or node data.
    44  	estHeaderRlpSize  = 500             // Approximate size of an RLP encoded block header
    45  	ethVersion        = 63              // equivalent eth version for the downloader
    46  
    47  	MaxHeaderFetch           = 192 // Amount of block headers to be fetched per retrieval request
    48  	MaxBodyFetch             = 32  // Amount of block bodies to be fetched per retrieval request
    49  	MaxReceiptFetch          = 128 // Amount of transaction receipts to allow fetching per request
    50  	MaxCodeFetch             = 64  // Amount of contract codes to allow fetching per request
    51  	MaxProofsFetch           = 64  // Amount of merkle proofs to be fetched per retrieval request
    52  	MaxHelperTrieProofsFetch = 64  // Amount of helper tries to be fetched per retrieval request
    53  	MaxTxSend                = 64  // Amount of transactions to be send per request
    54  	MaxTxStatus              = 256 // Amount of transactions to queried per request
    55  )
    56  
    57  var (
    58  	errTooManyInvalidRequest = errors.New("too many invalid requests made")
    59  	errFullClientPool        = errors.New("client pool is full")
    60  )
    61  
    62  // serverHandler is responsible for serving light client and process
    63  // all incoming light requests.
    64  type serverHandler struct {
    65  	blockchain *core.BlockChain
    66  	chainDb    ethdb.Database
    67  	txpool     *core.TxPool
    68  	server     *LesServer
    69  
    70  	closeCh chan struct{}  // Channel used to exit all background routines of handler.
    71  	wg      sync.WaitGroup // WaitGroup used to track all background routines of handler.
    72  	synced  func() bool    // Callback function used to determine whether local node is synced.
    73  
    74  	// Testing fields
    75  	addTxsSync bool
    76  }
    77  
    78  func newServerHandler(server *LesServer, blockchain *core.BlockChain, chainDb ethdb.Database, txpool *core.TxPool, synced func() bool) *serverHandler {
    79  	handler := &serverHandler{
    80  		server:     server,
    81  		blockchain: blockchain,
    82  		chainDb:    chainDb,
    83  		txpool:     txpool,
    84  		closeCh:    make(chan struct{}),
    85  		synced:     synced,
    86  	}
    87  	return handler
    88  }
    89  
    90  // start starts the server handler.
    91  func (h *serverHandler) start() {
    92  	h.wg.Add(1)
    93  	go h.broadcastHeaders()
    94  }
    95  
    96  // stop stops the server handler.
    97  func (h *serverHandler) stop() {
    98  	close(h.closeCh)
    99  	h.wg.Wait()
   100  }
   101  
   102  // runPeer is the p2p protocol run function for the given version.
   103  func (h *serverHandler) runPeer(version uint, p *p2p.Peer, rw p2p.MsgReadWriter) error {
   104  	peer := newPeer(int(version), h.server.config.NetworkId, false, p, newMeteredMsgWriter(rw, int(version)))
   105  	h.wg.Add(1)
   106  	defer h.wg.Done()
   107  	return h.handle(peer)
   108  }
   109  
   110  func (h *serverHandler) handle(p *peer) error {
   111  	// Reject light clients if server is not synced.
   112  	if !h.synced() {
   113  		return p2p.DiscRequested
   114  	}
   115  	p.Log().Debug("Light Ethereum peer connected", "name", p.Name())
   116  
   117  	// Execute the LES handshake
   118  	var (
   119  		head   = h.blockchain.CurrentHeader()
   120  		hash   = head.Hash()
   121  		number = head.Number.Uint64()
   122  		td     = h.blockchain.GetTd(hash, number)
   123  	)
   124  	if err := p.Handshake(td, hash, number, h.blockchain.Genesis().Hash(), h.server); err != nil {
   125  		p.Log().Debug("Light Ethereum handshake failed", "err", err)
   126  		return err
   127  	}
   128  	defer p.fcClient.Disconnect()
   129  
   130  	// Disconnect the inbound peer if it's rejected by clientPool
   131  	if !h.server.clientPool.connect(p, 0) {
   132  		p.Log().Debug("Light Ethereum peer registration failed", "err", errFullClientPool)
   133  		return errFullClientPool
   134  	}
   135  	// Register the peer locally
   136  	if err := h.server.peers.Register(p); err != nil {
   137  		h.server.clientPool.disconnect(p)
   138  		p.Log().Error("Light Ethereum peer registration failed", "err", err)
   139  		return err
   140  	}
   141  	clientConnectionGauge.Update(int64(h.server.peers.Len()))
   142  
   143  	var wg sync.WaitGroup // Wait group used to track all in-flight task routines.
   144  
   145  	connectedAt := mclock.Now()
   146  	defer func() {
   147  		wg.Wait() // Ensure all background task routines have exited.
   148  		h.server.peers.Unregister(p.id)
   149  		h.server.clientPool.disconnect(p)
   150  		clientConnectionGauge.Update(int64(h.server.peers.Len()))
   151  		connectionTimer.Update(time.Duration(mclock.Now() - connectedAt))
   152  	}()
   153  
   154  	// Spawn a main loop to handle all incoming messages.
   155  	for {
   156  		select {
   157  		case err := <-p.errCh:
   158  			p.Log().Debug("Failed to send light ethereum response", "err", err)
   159  			return err
   160  		default:
   161  		}
   162  		if err := h.handleMsg(p, &wg); err != nil {
   163  			p.Log().Debug("Light Ethereum message handling failed", "err", err)
   164  			return err
   165  		}
   166  	}
   167  }
   168  
   169  // handleMsg is invoked whenever an inbound message is received from a remote
   170  // peer. The remote connection is torn down upon returning any error.
   171  func (h *serverHandler) handleMsg(p *peer, wg *sync.WaitGroup) error {
   172  	// Read the next message from the remote peer, and ensure it's fully consumed
   173  	msg, err := p.rw.ReadMsg()
   174  	if err != nil {
   175  		return err
   176  	}
   177  	p.Log().Trace("Light Ethereum message arrived", "code", msg.Code, "bytes", msg.Size)
   178  
   179  	// Discard large message which exceeds the limitation.
   180  	if msg.Size > ProtocolMaxMsgSize {
   181  		clientErrorMeter.Mark(1)
   182  		return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize)
   183  	}
   184  	defer msg.Discard()
   185  
   186  	var (
   187  		maxCost uint64
   188  		task    *servingTask
   189  	)
   190  	p.responseCount++
   191  	responseCount := p.responseCount
   192  	// accept returns an indicator whether the request can be served.
   193  	// If so, deduct the max cost from the flow control buffer.
   194  	accept := func(reqID, reqCnt, maxCnt uint64) bool {
   195  		// Short circuit if the peer is already frozen or the request is invalid.
   196  		inSizeCost := h.server.costTracker.realCost(0, msg.Size, 0)
   197  		if p.isFrozen() || reqCnt == 0 || reqCnt > maxCnt {
   198  			p.fcClient.OneTimeCost(inSizeCost)
   199  			return false
   200  		}
   201  		// Prepaid max cost units before request been serving.
   202  		maxCost = p.fcCosts.getMaxCost(msg.Code, reqCnt)
   203  		accepted, bufShort, priority := p.fcClient.AcceptRequest(reqID, responseCount, maxCost)
   204  		if !accepted {
   205  			p.freezeClient()
   206  			p.Log().Error("Request came too early", "remaining", common.PrettyDuration(time.Duration(bufShort*1000000/p.fcParams.MinRecharge)))
   207  			p.fcClient.OneTimeCost(inSizeCost)
   208  			return false
   209  		}
   210  		// Create a multi-stage task, estimate the time it takes for the task to
   211  		// execute, and cache it in the request service queue.
   212  		factor := h.server.costTracker.globalFactor()
   213  		if factor < 0.001 {
   214  			factor = 1
   215  			p.Log().Error("Invalid global cost factor", "factor", factor)
   216  		}
   217  		maxTime := uint64(float64(maxCost) / factor)
   218  		task = h.server.servingQueue.newTask(p, maxTime, priority)
   219  		if task.start() {
   220  			return true
   221  		}
   222  		p.fcClient.RequestProcessed(reqID, responseCount, maxCost, inSizeCost)
   223  		return false
   224  	}
   225  	// sendResponse sends back the response and updates the flow control statistic.
   226  	sendResponse := func(reqID, amount uint64, reply *reply, servingTime uint64) {
   227  		p.responseLock.Lock()
   228  		defer p.responseLock.Unlock()
   229  
   230  		// Short circuit if the client is already frozen.
   231  		if p.isFrozen() {
   232  			realCost := h.server.costTracker.realCost(servingTime, msg.Size, 0)
   233  			p.fcClient.RequestProcessed(reqID, responseCount, maxCost, realCost)
   234  			return
   235  		}
   236  		// Positive correction buffer value with real cost.
   237  		var replySize uint32
   238  		if reply != nil {
   239  			replySize = reply.size()
   240  		}
   241  		var realCost uint64
   242  		if h.server.costTracker.testing {
   243  			realCost = maxCost // Assign a fake cost for testing purpose
   244  		} else {
   245  			realCost = h.server.costTracker.realCost(servingTime, msg.Size, replySize)
   246  		}
   247  		bv := p.fcClient.RequestProcessed(reqID, responseCount, maxCost, realCost)
   248  		if amount != 0 {
   249  			// Feed cost tracker request serving statistic.
   250  			h.server.costTracker.updateStats(msg.Code, amount, servingTime, realCost)
   251  			// Reduce priority "balance" for the specific peer.
   252  			h.server.clientPool.requestCost(p, realCost)
   253  		}
   254  		if reply != nil {
   255  			p.queueSend(func() {
   256  				if err := reply.send(bv); err != nil {
   257  					select {
   258  					case p.errCh <- err:
   259  					default:
   260  					}
   261  				}
   262  			})
   263  		}
   264  	}
   265  	switch msg.Code {
   266  	case GetBlockHeadersMsg:
   267  		p.Log().Trace("Received block header request")
   268  		if metrics.EnabledExpensive {
   269  			miscInHeaderPacketsMeter.Mark(1)
   270  			miscInHeaderTrafficMeter.Mark(int64(msg.Size))
   271  			defer func(start time.Time) { miscServingTimeHeaderTimer.UpdateSince(start) }(time.Now())
   272  		}
   273  		var req struct {
   274  			ReqID uint64
   275  			Query getBlockHeadersData
   276  		}
   277  		if err := msg.Decode(&req); err != nil {
   278  			clientErrorMeter.Mark(1)
   279  			return errResp(ErrDecode, "%v: %v", msg, err)
   280  		}
   281  		query := req.Query
   282  		if accept(req.ReqID, query.Amount, MaxHeaderFetch) {
   283  			wg.Add(1)
   284  			go func() {
   285  				defer wg.Done()
   286  				hashMode := query.Origin.Hash != (common.Hash{})
   287  				first := true
   288  				maxNonCanonical := uint64(100)
   289  
   290  				// Gather headers until the fetch or network limits is reached
   291  				var (
   292  					bytes   common.StorageSize
   293  					headers []*types.Header
   294  					unknown bool
   295  				)
   296  				for !unknown && len(headers) < int(query.Amount) && bytes < softResponseLimit {
   297  					if !first && !task.waitOrStop() {
   298  						sendResponse(req.ReqID, 0, nil, task.servingTime)
   299  						return
   300  					}
   301  					// Retrieve the next header satisfying the query
   302  					var origin *types.Header
   303  					if hashMode {
   304  						if first {
   305  							origin = h.blockchain.GetHeaderByHash(query.Origin.Hash)
   306  							if origin != nil {
   307  								query.Origin.Number = origin.Number.Uint64()
   308  							}
   309  						} else {
   310  							origin = h.blockchain.GetHeader(query.Origin.Hash, query.Origin.Number)
   311  						}
   312  					} else {
   313  						origin = h.blockchain.GetHeaderByNumber(query.Origin.Number)
   314  					}
   315  					if origin == nil {
   316  						atomic.AddUint32(&p.invalidCount, 1)
   317  						break
   318  					}
   319  					headers = append(headers, origin)
   320  					bytes += estHeaderRlpSize
   321  
   322  					// Advance to the next header of the query
   323  					switch {
   324  					case hashMode && query.Reverse:
   325  						// Hash based traversal towards the genesis block
   326  						ancestor := query.Skip + 1
   327  						if ancestor == 0 {
   328  							unknown = true
   329  						} else {
   330  							query.Origin.Hash, query.Origin.Number = h.blockchain.GetAncestor(query.Origin.Hash, query.Origin.Number, ancestor, &maxNonCanonical)
   331  							unknown = query.Origin.Hash == common.Hash{}
   332  						}
   333  					case hashMode && !query.Reverse:
   334  						// Hash based traversal towards the leaf block
   335  						var (
   336  							current = origin.Number.Uint64()
   337  							next    = current + query.Skip + 1
   338  						)
   339  						if next <= current {
   340  							infos, _ := json.MarshalIndent(p.Peer.Info(), "", "  ")
   341  							p.Log().Warn("GetBlockHeaders skip overflow attack", "current", current, "skip", query.Skip, "next", next, "attacker", infos)
   342  							unknown = true
   343  						} else {
   344  							if header := h.blockchain.GetHeaderByNumber(next); header != nil {
   345  								nextHash := header.Hash()
   346  								expOldHash, _ := h.blockchain.GetAncestor(nextHash, next, query.Skip+1, &maxNonCanonical)
   347  								if expOldHash == query.Origin.Hash {
   348  									query.Origin.Hash, query.Origin.Number = nextHash, next
   349  								} else {
   350  									unknown = true
   351  								}
   352  							} else {
   353  								unknown = true
   354  							}
   355  						}
   356  					case query.Reverse:
   357  						// Number based traversal towards the genesis block
   358  						if query.Origin.Number >= query.Skip+1 {
   359  							query.Origin.Number -= query.Skip + 1
   360  						} else {
   361  							unknown = true
   362  						}
   363  
   364  					case !query.Reverse:
   365  						// Number based traversal towards the leaf block
   366  						query.Origin.Number += query.Skip + 1
   367  					}
   368  					first = false
   369  				}
   370  				reply := p.ReplyBlockHeaders(req.ReqID, headers)
   371  				sendResponse(req.ReqID, query.Amount, p.ReplyBlockHeaders(req.ReqID, headers), task.done())
   372  				if metrics.EnabledExpensive {
   373  					miscOutHeaderPacketsMeter.Mark(1)
   374  					miscOutHeaderTrafficMeter.Mark(int64(reply.size()))
   375  				}
   376  			}()
   377  		}
   378  
   379  	case GetBlockBodiesMsg:
   380  		p.Log().Trace("Received block bodies request")
   381  		if metrics.EnabledExpensive {
   382  			miscInBodyPacketsMeter.Mark(1)
   383  			miscInBodyTrafficMeter.Mark(int64(msg.Size))
   384  			defer func(start time.Time) { miscServingTimeBodyTimer.UpdateSince(start) }(time.Now())
   385  		}
   386  		var req struct {
   387  			ReqID  uint64
   388  			Hashes []common.Hash
   389  		}
   390  		if err := msg.Decode(&req); err != nil {
   391  			clientErrorMeter.Mark(1)
   392  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   393  		}
   394  		var (
   395  			bytes  int
   396  			bodies []rlp.RawValue
   397  		)
   398  		reqCnt := len(req.Hashes)
   399  		if accept(req.ReqID, uint64(reqCnt), MaxBodyFetch) {
   400  			wg.Add(1)
   401  			go func() {
   402  				defer wg.Done()
   403  				for i, hash := range req.Hashes {
   404  					if i != 0 && !task.waitOrStop() {
   405  						sendResponse(req.ReqID, 0, nil, task.servingTime)
   406  						return
   407  					}
   408  					if bytes >= softResponseLimit {
   409  						break
   410  					}
   411  					body := h.blockchain.GetBodyRLP(hash)
   412  					if body == nil {
   413  						atomic.AddUint32(&p.invalidCount, 1)
   414  						continue
   415  					}
   416  					bodies = append(bodies, body)
   417  					bytes += len(body)
   418  				}
   419  				reply := p.ReplyBlockBodiesRLP(req.ReqID, bodies)
   420  				sendResponse(req.ReqID, uint64(reqCnt), reply, task.done())
   421  				if metrics.EnabledExpensive {
   422  					miscOutBodyPacketsMeter.Mark(1)
   423  					miscOutBodyTrafficMeter.Mark(int64(reply.size()))
   424  				}
   425  			}()
   426  		}
   427  
   428  	case GetCodeMsg:
   429  		p.Log().Trace("Received code request")
   430  		if metrics.EnabledExpensive {
   431  			miscInCodePacketsMeter.Mark(1)
   432  			miscInCodeTrafficMeter.Mark(int64(msg.Size))
   433  			defer func(start time.Time) { miscServingTimeCodeTimer.UpdateSince(start) }(time.Now())
   434  		}
   435  		var req struct {
   436  			ReqID uint64
   437  			Reqs  []CodeReq
   438  		}
   439  		if err := msg.Decode(&req); err != nil {
   440  			clientErrorMeter.Mark(1)
   441  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   442  		}
   443  		var (
   444  			bytes int
   445  			data  [][]byte
   446  		)
   447  		reqCnt := len(req.Reqs)
   448  		if accept(req.ReqID, uint64(reqCnt), MaxCodeFetch) {
   449  			wg.Add(1)
   450  			go func() {
   451  				defer wg.Done()
   452  				for i, request := range req.Reqs {
   453  					if i != 0 && !task.waitOrStop() {
   454  						sendResponse(req.ReqID, 0, nil, task.servingTime)
   455  						return
   456  					}
   457  					// Look up the root hash belonging to the request
   458  					header := h.blockchain.GetHeaderByHash(request.BHash)
   459  					if header == nil {
   460  						p.Log().Warn("Failed to retrieve associate header for code", "hash", request.BHash)
   461  						atomic.AddUint32(&p.invalidCount, 1)
   462  						continue
   463  					}
   464  					// Refuse to search stale state data in the database since looking for
   465  					// a non-exist key is kind of expensive.
   466  					local := h.blockchain.CurrentHeader().Number.Uint64()
   467  					if !h.server.archiveMode && header.Number.Uint64()+core.TriesInMemory <= local {
   468  						p.Log().Debug("Reject stale code request", "number", header.Number.Uint64(), "head", local)
   469  						atomic.AddUint32(&p.invalidCount, 1)
   470  						continue
   471  					}
   472  					triedb := h.blockchain.StateCache().TrieDB()
   473  
   474  					account, err := h.getAccount(triedb, header.Root, common.BytesToHash(request.AccKey))
   475  					if err != nil {
   476  						p.Log().Warn("Failed to retrieve account for code", "block", header.Number, "hash", header.Hash(), "account", common.BytesToHash(request.AccKey), "err", err)
   477  						atomic.AddUint32(&p.invalidCount, 1)
   478  						continue
   479  					}
   480  					code, err := triedb.Node(common.BytesToHash(account.CodeHash))
   481  					if err != nil {
   482  						p.Log().Warn("Failed to retrieve account code", "block", header.Number, "hash", header.Hash(), "account", common.BytesToHash(request.AccKey), "codehash", common.BytesToHash(account.CodeHash), "err", err)
   483  						continue
   484  					}
   485  					// Accumulate the code and abort if enough data was retrieved
   486  					data = append(data, code)
   487  					if bytes += len(code); bytes >= softResponseLimit {
   488  						break
   489  					}
   490  				}
   491  				reply := p.ReplyCode(req.ReqID, data)
   492  				sendResponse(req.ReqID, uint64(reqCnt), reply, task.done())
   493  				if metrics.EnabledExpensive {
   494  					miscOutCodePacketsMeter.Mark(1)
   495  					miscOutCodeTrafficMeter.Mark(int64(reply.size()))
   496  				}
   497  			}()
   498  		}
   499  
   500  	case GetReceiptsMsg:
   501  		p.Log().Trace("Received receipts request")
   502  		if metrics.EnabledExpensive {
   503  			miscInReceiptPacketsMeter.Mark(1)
   504  			miscInReceiptTrafficMeter.Mark(int64(msg.Size))
   505  			defer func(start time.Time) { miscServingTimeReceiptTimer.UpdateSince(start) }(time.Now())
   506  		}
   507  		var req struct {
   508  			ReqID  uint64
   509  			Hashes []common.Hash
   510  		}
   511  		if err := msg.Decode(&req); err != nil {
   512  			clientErrorMeter.Mark(1)
   513  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   514  		}
   515  		var (
   516  			bytes    int
   517  			receipts []rlp.RawValue
   518  		)
   519  		reqCnt := len(req.Hashes)
   520  		if accept(req.ReqID, uint64(reqCnt), MaxReceiptFetch) {
   521  			wg.Add(1)
   522  			go func() {
   523  				defer wg.Done()
   524  				for i, hash := range req.Hashes {
   525  					if i != 0 && !task.waitOrStop() {
   526  						sendResponse(req.ReqID, 0, nil, task.servingTime)
   527  						return
   528  					}
   529  					if bytes >= softResponseLimit {
   530  						break
   531  					}
   532  					// Retrieve the requested block's receipts, skipping if unknown to us
   533  					results := h.blockchain.GetReceiptsByHash(hash)
   534  					if results == nil {
   535  						if header := h.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
   536  							atomic.AddUint32(&p.invalidCount, 1)
   537  							continue
   538  						}
   539  					}
   540  					// If known, encode and queue for response packet
   541  					if encoded, err := rlp.EncodeToBytes(results); err != nil {
   542  						log.Error("Failed to encode receipt", "err", err)
   543  					} else {
   544  						receipts = append(receipts, encoded)
   545  						bytes += len(encoded)
   546  					}
   547  				}
   548  				reply := p.ReplyReceiptsRLP(req.ReqID, receipts)
   549  				sendResponse(req.ReqID, uint64(reqCnt), reply, task.done())
   550  				if metrics.EnabledExpensive {
   551  					miscOutReceiptPacketsMeter.Mark(1)
   552  					miscOutReceiptTrafficMeter.Mark(int64(reply.size()))
   553  				}
   554  			}()
   555  		}
   556  
   557  	case GetProofsV2Msg:
   558  		p.Log().Trace("Received les/2 proofs request")
   559  		if metrics.EnabledExpensive {
   560  			miscInTrieProofPacketsMeter.Mark(1)
   561  			miscInTrieProofTrafficMeter.Mark(int64(msg.Size))
   562  			defer func(start time.Time) { miscServingTimeTrieProofTimer.UpdateSince(start) }(time.Now())
   563  		}
   564  		var req struct {
   565  			ReqID uint64
   566  			Reqs  []ProofReq
   567  		}
   568  		if err := msg.Decode(&req); err != nil {
   569  			clientErrorMeter.Mark(1)
   570  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   571  		}
   572  		// Gather state data until the fetch or network limits is reached
   573  		var (
   574  			lastBHash common.Hash
   575  			root      common.Hash
   576  		)
   577  		reqCnt := len(req.Reqs)
   578  		if accept(req.ReqID, uint64(reqCnt), MaxProofsFetch) {
   579  			wg.Add(1)
   580  			go func() {
   581  				defer wg.Done()
   582  				nodes := light.NewNodeSet()
   583  
   584  				for i, request := range req.Reqs {
   585  					if i != 0 && !task.waitOrStop() {
   586  						sendResponse(req.ReqID, 0, nil, task.servingTime)
   587  						return
   588  					}
   589  					// Look up the root hash belonging to the request
   590  					var (
   591  						header *types.Header
   592  						trie   state.Trie
   593  					)
   594  					if request.BHash != lastBHash {
   595  						root, lastBHash = common.Hash{}, request.BHash
   596  
   597  						if header = h.blockchain.GetHeaderByHash(request.BHash); header == nil {
   598  							p.Log().Warn("Failed to retrieve header for proof", "hash", request.BHash)
   599  							atomic.AddUint32(&p.invalidCount, 1)
   600  							continue
   601  						}
   602  						// Refuse to search stale state data in the database since looking for
   603  						// a non-exist key is kind of expensive.
   604  						local := h.blockchain.CurrentHeader().Number.Uint64()
   605  						if !h.server.archiveMode && header.Number.Uint64()+core.TriesInMemory <= local {
   606  							p.Log().Debug("Reject stale trie request", "number", header.Number.Uint64(), "head", local)
   607  							atomic.AddUint32(&p.invalidCount, 1)
   608  							continue
   609  						}
   610  						root = header.Root
   611  					}
   612  					// If a header lookup failed (non existent), ignore subsequent requests for the same header
   613  					if root == (common.Hash{}) {
   614  						atomic.AddUint32(&p.invalidCount, 1)
   615  						continue
   616  					}
   617  					// Open the account or storage trie for the request
   618  					statedb := h.blockchain.StateCache()
   619  
   620  					switch len(request.AccKey) {
   621  					case 0:
   622  						// No account key specified, open an account trie
   623  						trie, err = statedb.OpenTrie(root)
   624  						if trie == nil || err != nil {
   625  							p.Log().Warn("Failed to open storage trie for proof", "block", header.Number, "hash", header.Hash(), "root", root, "err", err)
   626  							continue
   627  						}
   628  					default:
   629  						// Account key specified, open a storage trie
   630  						account, err := h.getAccount(statedb.TrieDB(), root, common.BytesToHash(request.AccKey))
   631  						if err != nil {
   632  							p.Log().Warn("Failed to retrieve account for proof", "block", header.Number, "hash", header.Hash(), "account", common.BytesToHash(request.AccKey), "err", err)
   633  							atomic.AddUint32(&p.invalidCount, 1)
   634  							continue
   635  						}
   636  						trie, err = statedb.OpenStorageTrie(common.BytesToHash(request.AccKey), account.Root)
   637  						if trie == nil || err != nil {
   638  							p.Log().Warn("Failed to open storage trie for proof", "block", header.Number, "hash", header.Hash(), "account", common.BytesToHash(request.AccKey), "root", account.Root, "err", err)
   639  							continue
   640  						}
   641  					}
   642  					// Prove the user's request from the account or stroage trie
   643  					if err := trie.Prove(request.Key, request.FromLevel, nodes); err != nil {
   644  						p.Log().Warn("Failed to prove state request", "block", header.Number, "hash", header.Hash(), "err", err)
   645  						continue
   646  					}
   647  					if nodes.DataSize() >= softResponseLimit {
   648  						break
   649  					}
   650  				}
   651  				reply := p.ReplyProofsV2(req.ReqID, nodes.NodeList())
   652  				sendResponse(req.ReqID, uint64(reqCnt), reply, task.done())
   653  				if metrics.EnabledExpensive {
   654  					miscOutTrieProofPacketsMeter.Mark(1)
   655  					miscOutTrieProofTrafficMeter.Mark(int64(reply.size()))
   656  				}
   657  			}()
   658  		}
   659  
   660  	case GetHelperTrieProofsMsg:
   661  		p.Log().Trace("Received helper trie proof request")
   662  		if metrics.EnabledExpensive {
   663  			miscInHelperTriePacketsMeter.Mark(1)
   664  			miscInHelperTrieTrafficMeter.Mark(int64(msg.Size))
   665  			defer func(start time.Time) { miscServingTimeHelperTrieTimer.UpdateSince(start) }(time.Now())
   666  		}
   667  		var req struct {
   668  			ReqID uint64
   669  			Reqs  []HelperTrieReq
   670  		}
   671  		if err := msg.Decode(&req); err != nil {
   672  			clientErrorMeter.Mark(1)
   673  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   674  		}
   675  		// Gather state data until the fetch or network limits is reached
   676  		var (
   677  			auxBytes int
   678  			auxData  [][]byte
   679  		)
   680  		reqCnt := len(req.Reqs)
   681  		if accept(req.ReqID, uint64(reqCnt), MaxHelperTrieProofsFetch) {
   682  			wg.Add(1)
   683  			go func() {
   684  				defer wg.Done()
   685  				var (
   686  					lastIdx  uint64
   687  					lastType uint
   688  					root     common.Hash
   689  					auxTrie  *trie.Trie
   690  				)
   691  				nodes := light.NewNodeSet()
   692  				for i, request := range req.Reqs {
   693  					if i != 0 && !task.waitOrStop() {
   694  						sendResponse(req.ReqID, 0, nil, task.servingTime)
   695  						return
   696  					}
   697  					if auxTrie == nil || request.Type != lastType || request.TrieIdx != lastIdx {
   698  						auxTrie, lastType, lastIdx = nil, request.Type, request.TrieIdx
   699  
   700  						var prefix string
   701  						if root, prefix = h.getHelperTrie(request.Type, request.TrieIdx); root != (common.Hash{}) {
   702  							auxTrie, _ = trie.New(root, trie.NewDatabase(rawdb.NewTable(h.chainDb, prefix)))
   703  						}
   704  					}
   705  					if request.AuxReq == auxRoot {
   706  						var data []byte
   707  						if root != (common.Hash{}) {
   708  							data = root[:]
   709  						}
   710  						auxData = append(auxData, data)
   711  						auxBytes += len(data)
   712  					} else {
   713  						if auxTrie != nil {
   714  							auxTrie.Prove(request.Key, request.FromLevel, nodes)
   715  						}
   716  						if request.AuxReq != 0 {
   717  							data := h.getAuxiliaryHeaders(request)
   718  							auxData = append(auxData, data)
   719  							auxBytes += len(data)
   720  						}
   721  					}
   722  					if nodes.DataSize()+auxBytes >= softResponseLimit {
   723  						break
   724  					}
   725  				}
   726  				reply := p.ReplyHelperTrieProofs(req.ReqID, HelperTrieResps{Proofs: nodes.NodeList(), AuxData: auxData})
   727  				sendResponse(req.ReqID, uint64(reqCnt), reply, task.done())
   728  				if metrics.EnabledExpensive {
   729  					miscOutHelperTriePacketsMeter.Mark(1)
   730  					miscOutHelperTrieTrafficMeter.Mark(int64(reply.size()))
   731  				}
   732  			}()
   733  		}
   734  
   735  	case SendTxV2Msg:
   736  		p.Log().Trace("Received new transactions")
   737  		if metrics.EnabledExpensive {
   738  			miscInTxsPacketsMeter.Mark(1)
   739  			miscInTxsTrafficMeter.Mark(int64(msg.Size))
   740  			defer func(start time.Time) { miscServingTimeTxTimer.UpdateSince(start) }(time.Now())
   741  		}
   742  		var req struct {
   743  			ReqID uint64
   744  			Txs   []*types.Transaction
   745  		}
   746  		if err := msg.Decode(&req); err != nil {
   747  			clientErrorMeter.Mark(1)
   748  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   749  		}
   750  		reqCnt := len(req.Txs)
   751  		if accept(req.ReqID, uint64(reqCnt), MaxTxSend) {
   752  			wg.Add(1)
   753  			go func() {
   754  				defer wg.Done()
   755  				stats := make([]light.TxStatus, len(req.Txs))
   756  				for i, tx := range req.Txs {
   757  					if i != 0 && !task.waitOrStop() {
   758  						return
   759  					}
   760  					hash := tx.Hash()
   761  					stats[i] = h.txStatus(hash)
   762  					if stats[i].Status == core.TxStatusUnknown {
   763  						addFn := h.txpool.AddRemotes
   764  						// Add txs synchronously for testing purpose
   765  						if h.addTxsSync {
   766  							addFn = h.txpool.AddRemotesSync
   767  						}
   768  						if errs := addFn([]*types.Transaction{tx}); errs[0] != nil {
   769  							stats[i].Error = errs[0].Error()
   770  							continue
   771  						}
   772  						stats[i] = h.txStatus(hash)
   773  					}
   774  				}
   775  				reply := p.ReplyTxStatus(req.ReqID, stats)
   776  				sendResponse(req.ReqID, uint64(reqCnt), reply, task.done())
   777  				if metrics.EnabledExpensive {
   778  					miscOutTxsPacketsMeter.Mark(1)
   779  					miscOutTxsTrafficMeter.Mark(int64(reply.size()))
   780  				}
   781  			}()
   782  		}
   783  
   784  	case GetTxStatusMsg:
   785  		p.Log().Trace("Received transaction status query request")
   786  		if metrics.EnabledExpensive {
   787  			miscInTxStatusPacketsMeter.Mark(1)
   788  			miscInTxStatusTrafficMeter.Mark(int64(msg.Size))
   789  			defer func(start time.Time) { miscServingTimeTxStatusTimer.UpdateSince(start) }(time.Now())
   790  		}
   791  		var req struct {
   792  			ReqID  uint64
   793  			Hashes []common.Hash
   794  		}
   795  		if err := msg.Decode(&req); err != nil {
   796  			clientErrorMeter.Mark(1)
   797  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   798  		}
   799  		reqCnt := len(req.Hashes)
   800  		if accept(req.ReqID, uint64(reqCnt), MaxTxStatus) {
   801  			wg.Add(1)
   802  			go func() {
   803  				defer wg.Done()
   804  				stats := make([]light.TxStatus, len(req.Hashes))
   805  				for i, hash := range req.Hashes {
   806  					if i != 0 && !task.waitOrStop() {
   807  						sendResponse(req.ReqID, 0, nil, task.servingTime)
   808  						return
   809  					}
   810  					stats[i] = h.txStatus(hash)
   811  				}
   812  				reply := p.ReplyTxStatus(req.ReqID, stats)
   813  				sendResponse(req.ReqID, uint64(reqCnt), reply, task.done())
   814  				if metrics.EnabledExpensive {
   815  					miscOutTxStatusPacketsMeter.Mark(1)
   816  					miscOutTxStatusTrafficMeter.Mark(int64(reply.size()))
   817  				}
   818  			}()
   819  		}
   820  
   821  	default:
   822  		p.Log().Trace("Received invalid message", "code", msg.Code)
   823  		clientErrorMeter.Mark(1)
   824  		return errResp(ErrInvalidMsgCode, "%v", msg.Code)
   825  	}
   826  	// If the client has made too much invalid request(e.g. request a non-exist data),
   827  	// reject them to prevent SPAM attack.
   828  	if atomic.LoadUint32(&p.invalidCount) > maxRequestErrors {
   829  		clientErrorMeter.Mark(1)
   830  		return errTooManyInvalidRequest
   831  	}
   832  	return nil
   833  }
   834  
   835  // getAccount retrieves an account from the state based on root.
   836  func (h *serverHandler) getAccount(triedb *trie.Database, root, hash common.Hash) (state.Account, error) {
   837  	trie, err := trie.New(root, triedb)
   838  	if err != nil {
   839  		return state.Account{}, err
   840  	}
   841  	blob, err := trie.TryGet(hash[:])
   842  	if err != nil {
   843  		return state.Account{}, err
   844  	}
   845  	var account state.Account
   846  	if err = rlp.DecodeBytes(blob, &account); err != nil {
   847  		return state.Account{}, err
   848  	}
   849  	return account, nil
   850  }
   851  
   852  // getHelperTrie returns the post-processed trie root for the given trie ID and section index
   853  func (h *serverHandler) getHelperTrie(typ uint, index uint64) (common.Hash, string) {
   854  	switch typ {
   855  	case htCanonical:
   856  		sectionHead := rawdb.ReadCanonicalHash(h.chainDb, (index+1)*h.server.iConfig.ChtSize-1)
   857  		return light.GetChtRoot(h.chainDb, index, sectionHead), light.ChtTablePrefix
   858  	case htBloomBits:
   859  		sectionHead := rawdb.ReadCanonicalHash(h.chainDb, (index+1)*h.server.iConfig.BloomTrieSize-1)
   860  		return light.GetBloomTrieRoot(h.chainDb, index, sectionHead), light.BloomTrieTablePrefix
   861  	}
   862  	return common.Hash{}, ""
   863  }
   864  
   865  // getAuxiliaryHeaders returns requested auxiliary headers for the CHT request.
   866  func (h *serverHandler) getAuxiliaryHeaders(req HelperTrieReq) []byte {
   867  	if req.Type == htCanonical && req.AuxReq == auxHeader && len(req.Key) == 8 {
   868  		blockNum := binary.BigEndian.Uint64(req.Key)
   869  		hash := rawdb.ReadCanonicalHash(h.chainDb, blockNum)
   870  		return rawdb.ReadHeaderRLP(h.chainDb, hash, blockNum)
   871  	}
   872  	return nil
   873  }
   874  
   875  // txStatus returns the status of a specified transaction.
   876  func (h *serverHandler) txStatus(hash common.Hash) light.TxStatus {
   877  	var stat light.TxStatus
   878  	// Looking the transaction in txpool first.
   879  	stat.Status = h.txpool.Status([]common.Hash{hash})[0]
   880  
   881  	// If the transaction is unknown to the pool, try looking it up locally.
   882  	if stat.Status == core.TxStatusUnknown {
   883  		lookup := h.blockchain.GetTransactionLookup(hash)
   884  		if lookup != nil {
   885  			stat.Status = core.TxStatusIncluded
   886  			stat.Lookup = lookup
   887  		}
   888  	}
   889  	return stat
   890  }
   891  
   892  // broadcastHeaders broadcasts new block information to all connected light
   893  // clients. According to the agreement between client and server, server should
   894  // only broadcast new announcement if the total difficulty is higher than the
   895  // last one. Besides server will add the signature if client requires.
   896  func (h *serverHandler) broadcastHeaders() {
   897  	defer h.wg.Done()
   898  
   899  	headCh := make(chan core.ChainHeadEvent, 10)
   900  	headSub := h.blockchain.SubscribeChainHeadEvent(headCh)
   901  	defer headSub.Unsubscribe()
   902  
   903  	var (
   904  		lastHead *types.Header
   905  		lastTd   = common.Big0
   906  	)
   907  	for {
   908  		select {
   909  		case ev := <-headCh:
   910  			peers := h.server.peers.AllPeers()
   911  			if len(peers) == 0 {
   912  				continue
   913  			}
   914  			header := ev.Block.Header()
   915  			hash, number := header.Hash(), header.Number.Uint64()
   916  			td := h.blockchain.GetTd(hash, number)
   917  			if td == nil || td.Cmp(lastTd) <= 0 {
   918  				continue
   919  			}
   920  			var reorg uint64
   921  			if lastHead != nil {
   922  				reorg = lastHead.Number.Uint64() - rawdb.FindCommonAncestor(h.chainDb, header, lastHead).Number.Uint64()
   923  			}
   924  			lastHead, lastTd = header, td
   925  
   926  			log.Debug("Announcing block to peers", "number", number, "hash", hash, "td", td, "reorg", reorg)
   927  			var (
   928  				signed         bool
   929  				signedAnnounce announceData
   930  			)
   931  			announce := announceData{Hash: hash, Number: number, Td: td, ReorgDepth: reorg}
   932  			for _, p := range peers {
   933  				p := p
   934  				switch p.announceType {
   935  				case announceTypeSimple:
   936  					p.queueSend(func() { p.SendAnnounce(announce) })
   937  				case announceTypeSigned:
   938  					if !signed {
   939  						signedAnnounce = announce
   940  						signedAnnounce.sign(h.server.privateKey)
   941  						signed = true
   942  					}
   943  					p.queueSend(func() { p.SendAnnounce(signedAnnounce) })
   944  				}
   945  			}
   946  		case <-h.closeCh:
   947  			return
   948  		}
   949  	}
   950  }