github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/eth/handler_eth.go (about)

     1  // Copyright 2020 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 eth
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  
    23  	"github.com/ethereum/go-ethereum/core"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/eth/protocols/eth"
    26  	"github.com/ethereum/go-ethereum/p2p/enode"
    27  )
    28  
    29  // ethHandler implements the eth.Backend interface to handle the various network
    30  // packets that are sent as replies or broadcasts.
    31  type ethHandler handler
    32  
    33  func (h *ethHandler) Chain() *core.BlockChain { return h.chain }
    34  func (h *ethHandler) TxPool() eth.TxPool      { return h.txpool }
    35  
    36  // RunPeer is invoked when a peer joins on the `eth` protocol.
    37  func (h *ethHandler) RunPeer(peer *eth.Peer, hand eth.Handler) error {
    38  	return (*handler)(h).runEthPeer(peer, hand)
    39  }
    40  
    41  // PeerInfo retrieves all known `eth` information about a peer.
    42  func (h *ethHandler) PeerInfo(id enode.ID) interface{} {
    43  	if p := h.peers.peer(id.String()); p != nil {
    44  		return p.info()
    45  	}
    46  	return nil
    47  }
    48  
    49  // AcceptTxs retrieves whether transaction processing is enabled on the node
    50  // or if inbound transactions should simply be dropped.
    51  func (h *ethHandler) AcceptTxs() bool {
    52  	return h.synced.Load()
    53  }
    54  
    55  // Handle is invoked from a peer's message handler when it receives a new remote
    56  // message that the handler couldn't consume and serve itself.
    57  func (h *ethHandler) Handle(peer *eth.Peer, packet eth.Packet) error {
    58  	// Consume any broadcasts and announces, forwarding the rest to the downloader
    59  	switch packet := packet.(type) {
    60  	case *eth.NewPooledTransactionHashesPacket:
    61  		return h.txFetcher.Notify(peer.ID(), packet.Types, packet.Sizes, packet.Hashes)
    62  
    63  	case *eth.TransactionsPacket:
    64  		for _, tx := range *packet {
    65  			if tx.Type() == types.BlobTxType {
    66  				return errors.New("disallowed broadcast blob transaction")
    67  			}
    68  		}
    69  		return h.txFetcher.Enqueue(peer.ID(), *packet, false)
    70  
    71  	case *eth.PooledTransactionsResponse:
    72  		return h.txFetcher.Enqueue(peer.ID(), *packet, true)
    73  
    74  	default:
    75  		return fmt.Errorf("unexpected eth packet type: %T", packet)
    76  	}
    77  }