eth

package
v1.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2024 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusMsg                     = 0x00
	NewBlockHashesMsg             = 0x01
	TransactionsMsg               = 0x02
	GetBlockHeadersMsg            = 0x03
	BlockHeadersMsg               = 0x04
	GetBlockBodiesMsg             = 0x05
	BlockBodiesMsg                = 0x06
	NewBlockMsg                   = 0x07
	NewPooledTransactionHashesMsg = 0x08
	GetPooledTransactionsMsg      = 0x09
	PooledTransactionsMsg         = 0x0a
	GetReceiptsMsg                = 0x0f
	ReceiptsMsg                   = 0x10
)
View Source
const (
	ETH68 = 68
)

Constants to match up protocol versions and messages

View Source
const ProtocolName = "eth"

ProtocolName is the official short name of the `eth` protocol used during devp2p capability negotiation.

Variables

View Source
var ProtocolVersions = []uint{ETH68}

ProtocolVersions are the supported versions of the `eth` protocol (first is primary).

Functions

func Handle

func Handle(backend Backend, peer *Peer) error

Handle is invoked whenever an `eth` connection is made that successfully passes the protocol handshake. This method will keep processing messages until the connection is torn down.

func MakeProtocols

func MakeProtocols(backend Backend, network uint64, dnsdisc enode.Iterator) []p2p.Protocol

MakeProtocols constructs the P2P protocol definitions for `eth`.

func ServiceGetBlockBodiesQuery

func ServiceGetBlockBodiesQuery(chain *core.BlockChain, query GetBlockBodiesRequest) []rlp.RawValue

ServiceGetBlockBodiesQuery assembles the response to a body query. It is exposed to allow external packages to test protocol behavior.

func ServiceGetBlockHeadersQuery

func ServiceGetBlockHeadersQuery(chain *core.BlockChain, query *GetBlockHeadersRequest, peer *Peer) []rlp.RawValue

ServiceGetBlockHeadersQuery assembles the response to a header query. It is exposed to allow external packages to test protocol behavior.

func ServiceGetReceiptsQuery

func ServiceGetReceiptsQuery(chain *core.BlockChain, query GetReceiptsRequest) []rlp.RawValue

ServiceGetReceiptsQuery assembles the response to a receipt query. It is exposed to allow external packages to test protocol behavior.

func StartENRUpdater

func StartENRUpdater(chain *core.BlockChain, ln *enode.LocalNode)

StartENRUpdater starts the `eth` ENR updater loop, which listens for chain head events and updates the requested node record whenever a fork is passed.

Types

type Backend

type Backend interface {
	// Chain retrieves the blockchain object to serve data.
	Chain() *core.BlockChain

	// TxPool retrieves the transaction pool object to serve data.
	TxPool() TxPool

	// AcceptTxs retrieves whether transaction processing is enabled on the node
	// or if inbound transactions should simply be dropped.
	AcceptTxs() bool

	// RunPeer is invoked when a peer joins on the `eth` protocol. The handler
	// should do any peer maintenance work, handshakes and validations. If all
	// is passed, control should be given back to the `handler` to process the
	// inbound messages going forward.
	RunPeer(peer *Peer, handler Handler) error

	// PeerInfo retrieves all known `eth` information about a peer.
	PeerInfo(id enode.ID) interface{}

	// Handle is a callback to be invoked when a data packet is received from
	// the remote peer. Only packets not consumed by the protocol handler will
	// be forwarded to the backend.
	Handle(peer *Peer, packet Packet) error
}

Backend defines the data retrieval methods to serve remote requests and the callback methods to invoke on remote deliveries.

type BlockBodiesPacket

type BlockBodiesPacket struct {
	RequestId uint64
	BlockBodiesResponse
}

BlockBodiesPacket is the network packet for block content distribution with request ID wrapping.

type BlockBodiesRLPPacket

type BlockBodiesRLPPacket struct {
	RequestId uint64
	BlockBodiesRLPResponse
}

BlockBodiesRLPPacket is the BlockBodiesRLPResponse with request ID wrapping.

type BlockBodiesRLPResponse

type BlockBodiesRLPResponse []rlp.RawValue

BlockBodiesRLPResponse is used for replying to block body requests, in cases where we already have them RLP-encoded, and thus can avoid the decode-encode roundtrip.

type BlockBodiesResponse

type BlockBodiesResponse []*BlockBody

BlockBodiesResponse is the network packet for block content distribution.

func (*BlockBodiesResponse) Kind

func (*BlockBodiesResponse) Kind() byte

func (*BlockBodiesResponse) Name

func (*BlockBodiesResponse) Name() string

func (*BlockBodiesResponse) Unpack

func (p *BlockBodiesResponse) Unpack() ([][]*types.Transaction, [][]*types.Header, [][]*types.Withdrawal)

Unpack retrieves the transactions and uncles from the range packet and returns them in a split flat format that's more consistent with the internal data structures.

type BlockBody

type BlockBody struct {
	Transactions []*types.Transaction // Transactions contained within a block
	Uncles       []*types.Header      // Uncles contained within a block
	Withdrawals  []*types.Withdrawal  `rlp:"optional"` // Withdrawals contained within a block
}

BlockBody represents the data content of a single block.

type BlockHeadersPacket

type BlockHeadersPacket struct {
	RequestId uint64
	BlockHeadersRequest
}

BlockHeadersPacket represents a block header response over with request ID wrapping.

type BlockHeadersRLPPacket

type BlockHeadersRLPPacket struct {
	RequestId uint64
	BlockHeadersRLPResponse
}

BlockHeadersRLPPacket represents a block header response with request ID wrapping.

type BlockHeadersRLPResponse

type BlockHeadersRLPResponse []rlp.RawValue

BlockHeadersRLPResponse represents a block header response, to use when we already have the headers rlp encoded.

type BlockHeadersRequest

type BlockHeadersRequest []*types.Header

BlockHeadersRequest represents a block header response.

func (*BlockHeadersRequest) Kind

func (*BlockHeadersRequest) Kind() byte

func (*BlockHeadersRequest) Name

func (*BlockHeadersRequest) Name() string

type Decoder

type Decoder interface {
	Decode(val interface{}) error
	Time() time.Time
}

type GetBlockBodiesPacket

type GetBlockBodiesPacket struct {
	RequestId uint64
	GetBlockBodiesRequest
}

GetBlockBodiesPacket represents a block body query with request ID wrapping.

type GetBlockBodiesRequest

type GetBlockBodiesRequest []common.Hash

GetBlockBodiesRequest represents a block body query.

func (*GetBlockBodiesRequest) Kind

func (*GetBlockBodiesRequest) Kind() byte

func (*GetBlockBodiesRequest) Name

func (*GetBlockBodiesRequest) Name() string

type GetBlockHeadersPacket

type GetBlockHeadersPacket struct {
	RequestId uint64
	*GetBlockHeadersRequest
}

GetBlockHeadersPacket represents a block header query with request ID wrapping.

type GetBlockHeadersRequest

type GetBlockHeadersRequest struct {
	Origin  HashOrNumber // Block from which to retrieve headers
	Amount  uint64       // Maximum number of headers to retrieve
	Skip    uint64       // Blocks to skip between consecutive headers
	Reverse bool         // Query direction (false = rising towards latest, true = falling towards genesis)
}

GetBlockHeadersRequest represents a block header query.

func (*GetBlockHeadersRequest) Kind

func (*GetBlockHeadersRequest) Kind() byte

func (*GetBlockHeadersRequest) Name

type GetPooledTransactionsPacket

type GetPooledTransactionsPacket struct {
	RequestId uint64
	GetPooledTransactionsRequest
}

GetPooledTransactionsPacket represents a transaction query with request ID wrapping.

type GetPooledTransactionsRequest

type GetPooledTransactionsRequest []common.Hash

GetPooledTransactionsRequest represents a transaction query.

func (*GetPooledTransactionsRequest) Kind

func (*GetPooledTransactionsRequest) Name

type GetReceiptsPacket

type GetReceiptsPacket struct {
	RequestId uint64
	GetReceiptsRequest
}

GetReceiptsPacket represents a block receipts query with request ID wrapping.

type GetReceiptsRequest

type GetReceiptsRequest []common.Hash

GetReceiptsRequest represents a block receipts query.

func (*GetReceiptsRequest) Kind

func (*GetReceiptsRequest) Kind() byte

func (*GetReceiptsRequest) Name

func (*GetReceiptsRequest) Name() string

type Handler

type Handler func(peer *Peer) error

Handler is a callback to invoke from an outside runner after the boilerplate exchanges have passed.

type HashOrNumber

type HashOrNumber struct {
	Hash   common.Hash // Block hash from which to retrieve headers (excludes Number)
	Number uint64      // Block hash from which to retrieve headers (excludes Hash)
}

HashOrNumber is a combined field for specifying an origin block.

func (*HashOrNumber) DecodeRLP

func (hn *HashOrNumber) DecodeRLP(s *rlp.Stream) error

DecodeRLP is a specialized decoder for HashOrNumber to decode the contents into either a block hash or a block number.

func (*HashOrNumber) EncodeRLP

func (hn *HashOrNumber) EncodeRLP(w io.Writer) error

EncodeRLP is a specialized encoder for HashOrNumber to encode only one of the two contained union fields.

type NewBlockHashesPacket

type NewBlockHashesPacket []struct {
	Hash   common.Hash // Hash of one particular block being announced
	Number uint64      // Number of one particular block being announced
}

NewBlockHashesPacket is the network packet for the block announcements.

func (*NewBlockHashesPacket) Kind

func (*NewBlockHashesPacket) Kind() byte

func (*NewBlockHashesPacket) Name

func (*NewBlockHashesPacket) Name() string

func (*NewBlockHashesPacket) Unpack

func (p *NewBlockHashesPacket) Unpack() ([]common.Hash, []uint64)

Unpack retrieves the block hashes and numbers from the announcement packet and returns them in a split flat format that's more consistent with the internal data structures.

type NewBlockPacket

type NewBlockPacket struct {
	Block *types.Block
	TD    *big.Int
}

NewBlockPacket is the network packet for the block propagation message.

func (*NewBlockPacket) Kind

func (*NewBlockPacket) Kind() byte

func (*NewBlockPacket) Name

func (*NewBlockPacket) Name() string

type NewPooledTransactionHashesPacket

type NewPooledTransactionHashesPacket struct {
	Types  []byte
	Sizes  []uint32
	Hashes []common.Hash
}

NewPooledTransactionHashesPacket represents a transaction announcement packet on eth/68 and newer.

func (*NewPooledTransactionHashesPacket) Kind

func (*NewPooledTransactionHashesPacket) Name

type NodeInfo

type NodeInfo struct {
	Network    uint64              `json:"network"`    // Ethereum network ID (1=Mainnet, Goerli=5)
	Difficulty *big.Int            `json:"difficulty"` // Total difficulty of the host's blockchain
	Genesis    common.Hash         `json:"genesis"`    // SHA3 hash of the host's genesis block
	Config     *params.ChainConfig `json:"config"`     // Chain configuration for the fork rules
	Head       common.Hash         `json:"head"`       // Hex hash of the host's best owned block
}

NodeInfo represents a short summary of the `eth` sub-protocol metadata known about the host peer.

type Packet

type Packet interface {
	Name() string // Name returns a string corresponding to the message type.
	Kind() byte   // Kind returns the message type.
}

Packet represents a p2p message in the `eth` protocol.

type Peer

type Peer struct {
	*p2p.Peer // The embedded P2P package peer
	// contains filtered or unexported fields
}

Peer is a collection of relevant information we have about a `eth` peer.

func NewPeer

func NewPeer(version uint, p *p2p.Peer, rw p2p.MsgReadWriter, txpool TxPool) *Peer

NewPeer creates a wrapper for a network connection and negotiated protocol version.

func (*Peer) AsyncSendNewBlock

func (p *Peer) AsyncSendNewBlock(block *types.Block, td *big.Int)

AsyncSendNewBlock queues an entire block for propagation to a remote peer. If the peer's broadcast queue is full, the event is silently dropped.

func (*Peer) AsyncSendNewBlockHash

func (p *Peer) AsyncSendNewBlockHash(block *types.Block)

AsyncSendNewBlockHash queues the availability of a block for propagation to a remote peer. If the peer's broadcast queue is full, the event is silently dropped.

func (*Peer) AsyncSendPooledTransactionHashes

func (p *Peer) AsyncSendPooledTransactionHashes(hashes []common.Hash)

AsyncSendPooledTransactionHashes queues a list of transactions hashes to eventually announce to a remote peer. The number of pending sends are capped (new ones will force old sends to be dropped)

func (*Peer) AsyncSendTransactions

func (p *Peer) AsyncSendTransactions(hashes []common.Hash)

AsyncSendTransactions queues a list of transactions (by hash) to eventually propagate to a remote peer. The number of pending sends are capped (new ones will force old sends to be dropped)

func (*Peer) Close

func (p *Peer) Close()

Close signals the broadcast goroutine to terminate. Only ever call this if you created the peer yourself via NewPeer. Otherwise let whoever created it clean it up!

func (*Peer) Handshake

func (p *Peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash, forkID forkid.ID, forkFilter forkid.Filter) error

Handshake executes the eth protocol handshake, negotiating version number, network IDs, difficulties, head and genesis blocks.

func (*Peer) Head

func (p *Peer) Head() (hash common.Hash, td *big.Int)

Head retrieves the current head hash and total difficulty of the peer.

func (*Peer) ID

func (p *Peer) ID() string

ID retrieves the peer's unique identifier.

func (*Peer) KnownBlock

func (p *Peer) KnownBlock(hash common.Hash) bool

KnownBlock returns whether peer is known to already have a block.

func (*Peer) KnownTransaction

func (p *Peer) KnownTransaction(hash common.Hash) bool

KnownTransaction returns whether peer is known to already have a transaction.

func (*Peer) ReplyBlockBodiesRLP

func (p *Peer) ReplyBlockBodiesRLP(id uint64, bodies []rlp.RawValue) error

ReplyBlockBodiesRLP is the response to GetBlockBodies.

func (*Peer) ReplyBlockHeadersRLP

func (p *Peer) ReplyBlockHeadersRLP(id uint64, headers []rlp.RawValue) error

ReplyBlockHeadersRLP is the response to GetBlockHeaders.

func (*Peer) ReplyPooledTransactionsRLP

func (p *Peer) ReplyPooledTransactionsRLP(id uint64, hashes []common.Hash, txs []rlp.RawValue) error

ReplyPooledTransactionsRLP is the response to RequestTxs.

func (*Peer) ReplyReceiptsRLP

func (p *Peer) ReplyReceiptsRLP(id uint64, receipts []rlp.RawValue) error

ReplyReceiptsRLP is the response to GetReceipts.

func (*Peer) RequestBodies

func (p *Peer) RequestBodies(hashes []common.Hash, sink chan *Response) (*Request, error)

RequestBodies fetches a batch of blocks' bodies corresponding to the hashes specified.

func (*Peer) RequestHeadersByHash

func (p *Peer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool, sink chan *Response) (*Request, error)

RequestHeadersByHash fetches a batch of blocks' headers corresponding to the specified header query, based on the hash of an origin block.

func (*Peer) RequestHeadersByNumber

func (p *Peer) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool, sink chan *Response) (*Request, error)

RequestHeadersByNumber fetches a batch of blocks' headers corresponding to the specified header query, based on the number of an origin block.

func (*Peer) RequestOneHeader

func (p *Peer) RequestOneHeader(hash common.Hash, sink chan *Response) (*Request, error)

RequestOneHeader is a wrapper around the header query functions to fetch a single header. It is used solely by the fetcher.

func (*Peer) RequestReceipts

func (p *Peer) RequestReceipts(hashes []common.Hash, sink chan *Response) (*Request, error)

RequestReceipts fetches a batch of transaction receipts from a remote node.

func (*Peer) RequestTxs

func (p *Peer) RequestTxs(hashes []common.Hash) error

RequestTxs fetches a batch of transactions from a remote node.

func (*Peer) SendNewBlock

func (p *Peer) SendNewBlock(block *types.Block, td *big.Int) error

SendNewBlock propagates an entire block to a remote peer.

func (*Peer) SendNewBlockHashes

func (p *Peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error

SendNewBlockHashes announces the availability of a number of blocks through a hash notification.

func (*Peer) SendTransactions

func (p *Peer) SendTransactions(txs types.Transactions) error

SendTransactions sends transactions to the peer and includes the hashes in its transaction hash set for future reference.

This method is a helper used by the async transaction sender. Don't call it directly as the queueing (memory) and transmission (bandwidth) costs should not be managed directly.

The reasons this is public is to allow packages using this protocol to write tests that directly send messages without having to do the async queueing.

func (*Peer) SetHead

func (p *Peer) SetHead(hash common.Hash, td *big.Int)

SetHead updates the head hash and total difficulty of the peer.

func (*Peer) Version

func (p *Peer) Version() uint

Version retrieves the peer's negotiated `eth` protocol version.

type PooledTransactionsPacket

type PooledTransactionsPacket struct {
	RequestId uint64
	PooledTransactionsResponse
}

PooledTransactionsPacket is the network packet for transaction distribution with request ID wrapping.

type PooledTransactionsRLPPacket

type PooledTransactionsRLPPacket struct {
	RequestId uint64
	PooledTransactionsRLPResponse
}

PooledTransactionsRLPPacket is PooledTransactionsRLPResponse with request ID wrapping.

type PooledTransactionsRLPResponse

type PooledTransactionsRLPResponse []rlp.RawValue

PooledTransactionsRLPResponse is the network packet for transaction distribution, used in the cases we already have them in rlp-encoded form

type PooledTransactionsResponse

type PooledTransactionsResponse []*types.Transaction

PooledTransactionsResponse is the network packet for transaction distribution.

func (*PooledTransactionsResponse) Kind

func (*PooledTransactionsResponse) Name

type ReceiptsPacket

type ReceiptsPacket struct {
	RequestId uint64
	ReceiptsResponse
}

ReceiptsPacket is the network packet for block receipts distribution with request ID wrapping.

type ReceiptsRLPPacket

type ReceiptsRLPPacket struct {
	RequestId uint64
	ReceiptsRLPResponse
}

ReceiptsRLPPacket is ReceiptsRLPResponse with request ID wrapping.

type ReceiptsRLPResponse

type ReceiptsRLPResponse []rlp.RawValue

ReceiptsRLPResponse is used for receipts, when we already have it encoded

type ReceiptsResponse

type ReceiptsResponse [][]*types.Receipt

ReceiptsResponse is the network packet for block receipts distribution.

func (*ReceiptsResponse) Kind

func (*ReceiptsResponse) Kind() byte

func (*ReceiptsResponse) Name

func (*ReceiptsResponse) Name() string

type Request

type Request struct {
	Peer string    // Demultiplexer if cross-peer requests are batched together
	Sent time.Time // Timestamp when the request was sent
	// contains filtered or unexported fields
}

Request is a pending request to allow tracking it and delivering a response back to the requester on their chosen channel.

func (*Request) Close

func (r *Request) Close() error

Close aborts an in-flight request. Although there's no way to notify the remote peer about the cancellation, this method notifies the dispatcher to discard any late responses.

type Response

type Response struct {
	Req  *Request      // Original request to cross-reference with
	Res  interface{}   // Remote response for the request query
	Meta interface{}   // Metadata generated locally on the receiver thread
	Time time.Duration // Time it took for the request to be served
	Done chan error    // Channel to signal message handling to the reader
	// contains filtered or unexported fields
}

Response is a reply packet to a previously created request. It is delivered on the channel assigned by the requester subsystem and contains the original request embedded to allow uniquely matching it caller side.

type StatusPacket

type StatusPacket struct {
	ProtocolVersion uint32
	NetworkID       uint64
	TD              *big.Int
	Head            common.Hash
	Genesis         common.Hash
	ForkID          forkid.ID
}

StatusPacket is the network packet for the status message.

func (*StatusPacket) Kind

func (*StatusPacket) Kind() byte

func (*StatusPacket) Name

func (*StatusPacket) Name() string

type TransactionsPacket

type TransactionsPacket []*types.Transaction

TransactionsPacket is the network packet for broadcasting new transactions.

func (*TransactionsPacket) Kind

func (*TransactionsPacket) Kind() byte

func (*TransactionsPacket) Name

func (*TransactionsPacket) Name() string

type TxPool

type TxPool interface {
	// Get retrieves the transaction from the local txpool with the given hash.
	Get(hash common.Hash) *types.Transaction
}

TxPool defines the methods needed by the protocol handler to serve transactions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL