Documentation
¶
Overview ¶
Package p2p implements the Ethereum p2p network protocols.
Index ¶
- Constants
- Variables
- func Decrypt(prv *ecdsa.PrivateKey, ct []byte) ([]byte, error)
- func ExpectMsg(r MsgReader, code uint64, content interface{}) error
- func MsgPipe() (*MsgPipeRW, *MsgPipeRW)
- func Send(w MsgWriter, msgcode uint64, data interface{}) error
- func SendItems(w MsgWriter, msgcode uint64, elems ...interface{}) error
- func ToECDSAPub(pub []byte) *ecdsa.PublicKey
- type Cap
- type DiscReason
- type Msg
- type MsgPipeRW
- type MsgReadWriter
- type MsgReader
- type MsgWriter
- type Net
- type Peer
- type Protocol
- type Server
Constants ¶
const ( DefaultNodeName = "znn-node" DefaultListenHost = "0.0.0.0" DefaultListenPort = 35995 DefaultHTTPPort = 35997 DefaultWSPort = 35998 DefaultMinPeers = 8 DefaultMaxPeers = 60 DefaultMaxPendingPeers = 10 DefaultMinConnectedPeers = 16 DefaultNetDirName = "network" DefaultNetPrivateKeyFile = "network-private-key" )
Variables ¶
var (
DefaultSeeders = []string{}/* 116 elements not displayed */
)
var ErrPipeClosed = errors.New("p2p: read or write on closed message pipe")
ErrPipeClosed is returned from pipe operations after the pipe has been closed.
Functions ¶
func ExpectMsg ¶
ExpectMsg reads a message from r and verifies that its code and encoded RLP content match the provided values. If content is nil, the payload is discarded and not verified.
func MsgPipe ¶
MsgPipe creates a message pipe. Reads on one end are matched with writes on the other. The pipe is full-duplex, both ends implement MsgReadWriter.
func Send ¶
Send writes an RLP-encoded message with the given code. data should encode as an RLP list.
func SendItems ¶
SendItems writes an RLP with the given code and data elements. For a call such as:
SendItems(w, code, e1, e2, e3)
the message payload will be an RLP list containing the items:
[e1, e2, e3]
func ToECDSAPub ¶
Types ¶
type DiscReason ¶
type DiscReason uint
const ( DiscRequested DiscReason = iota DiscNetworkError DiscProtocolError DiscUselessPeer DiscTooManyPeers DiscAlreadyConnected DiscIncompatibleVersion DiscInvalidIdentity DiscQuitting DiscUnexpectedIdentity DiscSelf DiscReadTimeout DiscSubprotocolError )
func (DiscReason) Error ¶
func (d DiscReason) Error() string
func (DiscReason) String ¶
func (d DiscReason) String() string
type Msg ¶
type Msg struct { Code uint64 Size uint32 // size of the paylod Payload io.Reader ReceivedAt time.Time }
Msg defines the structure of a p2p message.
Note that a Msg can only be sent once since the Payload reader is consumed during sending. It is not possible to create a Msg and send it any number of times. If you want to reuse an encoded structure, encode the payload into a byte array and create a separate Msg with a bytes.Reader as Payload for each send.
func (Msg) Decode ¶
Decode parses the RLP content of a message into the given value, which must be a pointer.
For the decoding rules, please see package rlp.
type MsgPipeRW ¶
type MsgPipeRW struct {
// contains filtered or unexported fields
}
MsgPipeRW is an endpoint of a MsgReadWriter pipe.
func (*MsgPipeRW) Close ¶
Close unblocks any pending ReadMsg and WriteMsg calls on both ends of the pipe. They will return ErrPipeClosed. Close also interrupts any reads from a message payload.
type MsgReadWriter ¶
MsgReadWriter provides reading and writing of encoded messages. Implementations should ensure that ReadMsg and WriteMsg can be called simultaneously from multiple goroutines.
type Net ¶
type Net struct { // Absolute path to PrivateKeyFile. // (defaults to .znn/ DefaultNetPrivateKeyFile on linux). // If Config.DataDir changes, this reflects changes accordingly. PrivateKeyFile string // MaxPeers is the maximum number of peers that can be // connected. It must be greater than zero. MaxPeers int // MinConnectedPeers is the minimum number of peers that can be connected MinConnectedPeers int // MaxPendingPeers is the maximum number of peers that can be pending in the // handshake phase, counted separately for inbound and outbound connections. // Zero defaults to preset values. MaxPendingPeers int // Name sets the node name of this server. Name string Seeders []string // NodeDatabase is the path to the database containing the previously seen // live nodes in the network. NodeDatabase string // If ListenAddr is set to a non-nil address, the server // will listen for incoming connections. // // If the port is zero, the operating system will pick a port. The // ListenAddr field will be updated with the actual address when // the server is started. ListenAddr string ListenPort int // contains filtered or unexported fields }
func (*Net) PrivateKey ¶
func (c *Net) PrivateKey() *ecdsa.PrivateKey
PrivateKey retrieves the currently configured private key of the node, checking first any manually set key, falling back to the one found in the configured data folder. If no key can be found, a new one is generated.
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
Peer represents a connected remote node.
func (*Peer) Disconnect ¶
func (p *Peer) Disconnect(reason DiscReason)
Disconnect terminates the peer connection with the given reason. It returns immediately and does not wait until the connection is closed.
func (*Peer) RemoteAddr ¶
RemoteAddr returns the remote address of the network connection.
type Protocol ¶
type Protocol struct { // Name should contain the official protocol name, // often a three-letter word. Name string // Version should contain the version number of the protocol. Version uint // Length should contain the number of message codes used // by the protocol. Length uint64 // Run is called in a new groutine when the protocol has been // negotiated with a peer. It should read and write messages from // rw. The Payload for each message must be fully consumed. // // The peer connection is closed when Start returns. It should return // any protocol-level error (such as an I/O error) that is // encountered. Run func(peer *Peer, rw MsgReadWriter) error }
Protocol represents a P2P subprotocol implementation.
type Server ¶
type Server struct { // This field must be set to a valid secp256k1 private key. PrivateKey *ecdsa.PrivateKey // MaxPeers is the maximum number of peers that can be // connected. It must be greater than zero. MaxPeers int // MinConnectedPeers is the minimum number of peers that can be connected. MinConnectedPeers int // MaxPendingPeers is the maximum number of peers that can be pending in the // handshake phase, counted separately for inbound and outbound connections. // Zero defaults to preset values. MaxPendingPeers int // Discovery specifies whether the peer discovery mechanism should be started // or not. Disabling is usually useful for protocol debugging (manual topology). Discovery bool // Name sets the node name of this server. // Use common.MakeName to create a name that follows existing conventions. Name string // Bootstrap nodes are used to establish connectivity // with the rest of the network. BootstrapNodes []*discover.Node // Static nodes are used as pre-configured connections which are always // maintained and re-connected on disconnects. StaticNodes []*discover.Node // Trusted nodes are used as pre-configured connections which are always // allowed to connect, even above the peer limit. TrustedNodes []*discover.Node // NodeDatabase is the path to the database containing the previously seen // live nodes in the network. NodeDatabase string // Protocols should contain the protocols supported // by the server. Matching protocols are launched for // each peer. Protocols []Protocol // If ListenAddr is set to a non-nil address, the server // will listen for incoming connections. // // If the port is zero, the operating system will pick a port. The // ListenAddr field will be updated with the actual address when // the server is started. ListenAddr string // If set to a non-nil value, the given NAT port mapper // is used to make the listening port available to the // Internet. NAT nat.Interface // If Dialer is set to a non-nil value, the given Dialer // is used to dial outbound peer connections. Dialer *net.Dialer // If NoDial is true, the server will not dial any peers. NoDial bool // contains filtered or unexported fields }
Server manages all peer connections.
The fields of Server are used as configuration parameters. You should set them before starting the Server. Fields may not be modified while the server is running.
func (*Server) AddPeer ¶
AddPeer connects to the given node and maintains the connection until the server is shut down. If the connection fails for any reason, the server will attempt to reconnect the peer.