network

package
v0.0.0-...-942cd0b Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

package network implements the Bitcoin peer-to-peer network protocol. reference: https://en.bitcoin.it/wiki/Protocol_documentation

Index

Constants

View Source
const (
	TxDataType = iota + 1
	BlockDataType
	FilteredBlockDataType
	CompactBlockDataType
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BloomFilter

type BloomFilter struct {
	Size         uint32 // Size of the bit field in bytes.
	BitField     []byte // The buckets (sets).
	NumHashFuncs uint32 // The number of hash functions.
	Tweak        uint32 // To be able to change the Bloom filter if it hits too many items.
}

Bloom filter as used in the Bitcoin protocol. https://en.wikipedia.org/wiki/Bloom_filter

func (*BloomFilter) Add

func (b *BloomFilter) Add(item []byte)

Add an item (bytes) to the filter. It basically encodes it into the BitField.

type Envelope

type Envelope struct {
	Command string // up to 12 bytes
	Payload []byte
	Testnet bool
}

func (*Envelope) Marshal

func (e *Envelope) Marshal() []byte

func (*Envelope) Unmarshal

func (e *Envelope) Unmarshal(r io.Reader) (*Envelope, error)

type FilterloadMsg

type FilterloadMsg struct {
	BloomFilter
	Flags uint8 // A set of flags that control how matched items are added to the filter.
}

Upon receiving a filterload command, the remote peer will immediately restrict the broadcast transactions it announces (in inv packets) to transactions matching the filter.

type GetDataMsg

type GetDataMsg struct {
	Inventory []InventoryVector
}

Packet getdata is used to retrieve the content of a specific object, and is usually sent after receiving an inv packet, after filtering known elements.

func (*GetDataMsg) Add

func (gd *GetDataMsg) Add(v InventoryVector)

Add a new object to the Inventory.

type GetHeadersMsg

type GetHeadersMsg struct {
	Version    int32    // The protocol version.
	NumHashes  int32    // VarInt. Number of block locator hash entries; can be >1 upon chain split.
	StartBlock [32]byte // Block locator object.
	EndBlock   [32]byte // Hash of last desired block; set to zero for as many blocks as possible.
}

Return a headers packet containing the headers of blocks starting right after the last known hash in the block locator object, up to EndBlock or 2000 blocks, whichever comes first.

type HeadersMsg

type HeadersMsg struct {
	Headers []blockchain.Block
}

The headers packet returns block headers in response to a getheaders packet.

type InventoryVector

type InventoryVector struct {
	Type uint32   // Identifies the object type linked to this inventory.
	Hash [32]byte // Hash of the object.
}

Inventory vectors are used for notifying other nodes about objects they have or data which is being requested.

type MerkleblockMsg

type MerkleblockMsg struct {
	blockchain.Block
	TotalTxs uint32     // Number of transactions in the block (including unmatched ones).
	Hashes   [][32]byte // Hashes in depth-first order.
	Flags    []byte     // Flag bits, packed per 8 in a byte, little-endian. Padded with 0.
}

After a filter has been set, nodes don't merely stop announcing non-matching transactions, they can also serve filtered blocks.

func (*MerkleblockMsg) IsValid

func (mb *MerkleblockMsg) IsValid() (bool, error)

IsValid verifies whether the merkle tree information validates to the merkle root.

type NetAddr

type NetAddr struct {
	Time     uint32 // The Time (version >= 31402). Not present in version message.
	Services uint64 // Same service(s) listed in version.
	IP       net.IP // IPv6 address or IPv4 address.
	Port     uint16 // Port number.
}

When a network address is needed somewhere, this structure is used.

type Node

type Node struct {
	Conn    net.Conn
	Testnet bool
	Logging bool
}

Node is a utility struct used to connect to a single node.

func (*Node) Close

func (n *Node) Close() error

func (*Node) Handshake

func (n *Node) Handshake() error

Handshake performs Version Handshake ref: https://en.bitcoin.it/wiki/Version_Handshake

Local peer (L) connects to a remote peer (R): L -> R: Send version message with the local peer's version L <- R: Send version message back L <- R: Send verack message R: Sets version to the minimum of the 2 versions L -> R: Send verack message after receiving version message from R L: Sets version to the minimum of the 2 versions

func (*Node) Read

func (n *Node) Read() (*Envelope, error)

Read reads a message in a envelope from the connection.

func (*Node) WaitFor

func (n *Node) WaitFor(cmds ...string) (message, error)

func (*Node) Write

func (n *Node) Write(m message) error

Write writes the message to the connection.

type PingMsg

type PingMsg struct {
	Nonce uint64 // Random nonce.
}

The ping message is sent primarily to confirm that the TCP/IP connection is still valid. An error in transmission is presumed to be a closed connection and the address is removed as a current peer.

type PongMsg

type PongMsg struct {
	Nonce uint64 // Nonce from Ping.
}

The pong message is sent in response to a ping message. A pong response is generated using a nonce included in the ping.

type VerackMsg

type VerackMsg struct{}

The verack message is sent in reply to version.

type VersionMsg

type VersionMsg struct {
	Version   int32   // Identifies protocol version being used by the node.
	Services  uint64  // Bitfield of features to be enabled for this connection.
	Timestamp int64   // Standard UNIX timestamp in seconds.
	AddrRecv  NetAddr // The network address of the node receiving this message.
	AddrSndr  NetAddr // The network address of the node sending this message. (can be ignored)
	Nonce     uint64  // Randomly generated every time. Used to detect connections to self.
	UserAgent string  // User Agent identifies the software being run.
	Height    int32   // The last block received by the sending node.
	Relay     bool    // Whether the remote peer should announce relayed tx, see BIP 0037.
}

When a node creates an outgoing connection, it will immediately advertise its version. The remote node will respond with its version. No further communication is possible until both peers have exchanged their version.

Jump to

Keyboard shortcuts

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