p2p

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CMDSize         = 12
	CMDOffset       = 4
	ChecksumSize    = 4
	HeaderSize      = 24
	BlocksCacheSize = 2

	// MaxMessagePayload is the maximum bytes a message can be regardless of other
	// individual limits imposed by messages themselves.
	MaxMessagePayload = 1024 * 1024 * 32 // 32MB
)
View Source
const (
	CmdVersion     = "version"
	CmdVerAck      = "verack"
	CmdGetAddr     = "getaddr"
	CmdAddr        = "addr"
	CmdGetBlocks   = "getblocks"
	CmdInv         = "inv"
	CmdGetData     = "getdata"
	CmdNotFound    = "notfound"
	CmdBlock       = "block"
	CmdTx          = "tx"
	CmdPing        = "ping"
	CmdPong        = "pong"
	CmdMemPool     = "mempool"
	CmdFilterAdd   = "filteradd"
	CmdFilterClear = "filterclear"
	CmdFilterLoad  = "filterload"
	CmdMerkleBlock = "merkleblock"
	CmdReject      = "reject"
	CmdTxFilter    = "txfilter"
	CmdDAddr       = "daddr"
)

Variables

View Source
var (
	ErrInvalidHeader   = fmt.Errorf("invalid message header")
	ErrInvalidPayload  = fmt.Errorf("invalid message payload")
	ErrUnmatchedMagic  = fmt.Errorf("unmatched magic")
	ErrMsgSizeExceeded = fmt.Errorf("message size exceeded")
)

Functions

func WriteMessage

func WriteMessage(w io.Writer, magic uint32, msg Message, getDposBlock func(msg Message) (*types.DposBlock, bool)) error

WriteMessage writes a Message to w including the necessary header information.

Types

type Header struct {
	Magic    uint32
	CMD      [CMDSize]byte
	Length   uint32
	Checksum [ChecksumSize]byte
}

func BuildHeader

func BuildHeader(magic uint32, cmd string, body []byte) *Header

func (*Header) Deserialize

func (header *Header) Deserialize(buf []byte) error

func (*Header) GetCMD

func (header *Header) GetCMD() string

func (*Header) Serialize

func (header *Header) Serialize() ([]byte, error)

func (*Header) Verify

func (header *Header) Verify(buf []byte) error

type MakeEmptyMessage

type MakeEmptyMessage func(command string) (Message, error)

MakeEmptyMessage is a function to make message by the given command.

type Message

type Message interface {
	// Get the message CMD parameter which is the type of this message
	CMD() string
	// Get the max payload size of this message
	MaxLength() uint32
	// A message is a serializable instance
	common.Serializable
}

The message flying in the peer to peer network

func ReadMessage

func ReadMessage(r io.Reader, magic uint32, makeEmptyMessage MakeEmptyMessage) (Message, error)

ReadMessage reads, validates, and parse the Message from r for the provided magic.

type NAFilter

type NAFilter interface {
	// Filter takes a NetAddress and return if this address is ok to use.
	Filter(na *NetAddress) bool
}

NAFilter defines a NetAddress filter interface, it is used to filter inbound peer addresses and cached net addresses when responding to a getaddr message.

type NetAddress

type NetAddress struct {
	// Last time the address was seen.  This is, unfortunately, encoded as a
	// uint32 on the wire and therefore is limited to 2106.  This field is
	// not present in the version message (Version) nor was it
	// added until protocol version >= NetAddressTimeVersion.
	Timestamp time.Time

	// Bitfield which identifies the services supported by the address.
	Services uint64

	// IP address of the peer.
	IP net.IP

	// Port the peer is using. This is encoded in little endian.
	Port uint16
}

NetAddress defines information about a peer on the network including the time it was last seen, the services it supports, its IP address, and port.

func NewNetAddress

func NewNetAddress(addr *net.TCPAddr, services uint64) *NetAddress

NewNetAddress returns a new NetAddress using the provided TCP address and supported services with defaults for the remaining fields.

func NewNetAddressIPPort

func NewNetAddressIPPort(ip net.IP, port uint16, services uint64) *NetAddress

NewNetAddressIPPort returns a new NetAddress using the provided IP, port, and supported services with defaults for the remaining fields.

func NewNetAddressTimestamp

func NewNetAddressTimestamp(
	timestamp time.Time, services uint64, ip net.IP, port uint16) *NetAddress

NewNetAddressTimestamp returns a new NetAddress using the provided timestamp, IP, port, and supported services. The timestamp is rounded to single second precision.

func (*NetAddress) AddService

func (na *NetAddress) AddService(service uint64)

AddService adds service as a supported service by the peer generating the message.

func (*NetAddress) Deserialize

func (na *NetAddress) Deserialize(r io.Reader) error

readNetAddress reads an encoded NetAddress from r.

func (*NetAddress) HasService

func (na *NetAddress) HasService(service uint64) bool

HasService returns whether the specified service is supported by the address.

func (*NetAddress) Serialize

func (na *NetAddress) Serialize(w io.Writer) error

Serialize serializes a NetAddress to w.

func (NetAddress) String

func (na NetAddress) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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