Categorygithub.com/decred/dcrd/wire
modulepackage
1.7.0
Repository: https://github.com/decred/dcrd.git
Documentation: pkg.go.dev

# README

wire

Build Status ISC License Doc

Package wire implements the Decred wire protocol. A comprehensive suite of tests with 100% test coverage is provided to ensure proper functionality.

This package has intentionally been designed so it can be used as a standalone package for any projects needing to interface with Decred peers at the wire protocol level.

Installation and Updating

This package is part of the github.com/decred/dcrd/wire module. Use the standard go tooling for working with modules to incorporate it.

Decred Message Overview

The Decred protocol consists of exchanging messages between peers. Each message is preceded by a header which identifies information about it such as which decred network it is a part of, its type, how big it is, and a checksum to verify validity. All encoding and decoding of message headers is handled by this package.

To accomplish this, there is a generic interface for Decred messages named Message which allows messages of any type to be read, written, or passed around through channels, functions, etc. In addition, concrete implementations of most of the currently supported Decred messages are provided. For these supported messages, all of the details of marshalling and unmarshalling to and from the wire using Decred encoding are handled so the caller doesn't have to concern themselves with the specifics.

Reading Messages Example

In order to unmarshal Decred messages from the wire, use the ReadMessage function. It accepts any io.Reader, but typically this will be a net.Conn to a remote node running a Decred peer. Example syntax is:

	// Use the most recent protocol version supported by the package and the
	// main Decred network.
	pver := wire.ProtocolVersion
	dcrnet := wire.MainNet

	// Reads and validates the next Decred message from conn using the
	// protocol version pver and the Decred network dcrnet.  The returns
	// are a wire.Message, a []byte which contains the unmarshalled
	// raw payload, and a possible error.
	msg, rawPayload, err := wire.ReadMessage(conn, pver, dcrnet)
	if err != nil {
		// Log and handle the error
	}

See the package documentation for details on determining the message type.

Writing Messages Example

In order to marshal Decred messages to the wire, use the WriteMessage function. It accepts any io.Writer, but typically this will be a net.Conn to a remote node running a Decred peer. Example syntax to request addresses from a remote peer is:

	// Use the most recent protocol version supported by the package and the
	// main Decred network.
	pver := wire.ProtocolVersion
	dcrnet := wire.MainNet

	// Create a new getaddr Decred message.
	msg := wire.NewMsgGetAddr()

	// Writes a Decred message msg to conn using the protocol version
	// pver, and the Decred network dcrnet.  The return is a possible
	// error.
	err := wire.WriteMessage(conn, msg, pver, dcrnet)
	if err != nil {
		// Log and handle the error
	}

License

Package wire is licensed under the copyfree ISC License.

# Functions

MaxTxPerTxTree returns the maximum number of transactions that could possibly fit into a block per each merkle root for the given protocol version.
NewBlockHeader returns a new BlockHeader using the provided previous block hash, merkle root hash, difficulty bits, and nonce used to generate the block with defaults for the remaining fields.
NewInvVect returns a new InvVect using the provided type and hash.
NewMsgAddr returns a new bitcoin addr message that conforms to the Message interface.
NewMsgBlock returns a new Decred block message that conforms to the Message interface.
NewMsgCFHeaders returns a new cfheaders message that conforms to the Message interface.
NewMsgCFilter returns a new cfilter message that conforms to the Message interface.
NewMsgCFiltersV2 returns a new cfiltersv2 message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgCFilterV2 returns a new cfilterv2 message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgCFTypes returns a new cftypes message that conforms to the Message interface.
NewMsgFeeFilter returns a new feefilter message that conforms to the Message interface.
NewMsgGetAddr returns a new Decred getaddr message that conforms to the Message interface.
NewMsgGetBlocks returns a new Decred getblocks message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgGetCFHeaders returns a new getcfheader message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgGetCFilter returns a new getcfilter message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgGetCFilterV2 returns a new Decred getcfilterv2 message that conforms to the Message interface using the passed parameters.
NewMsgGetCFsV2 returns a new Decred getcfiltersv2 message that conforms to the Message interface using the passed parameters.
NewMsgGetCFTypes returns a new getcftypes message that conforms to the Message interface.
NewMsgGetData returns a new Decred getdata message that conforms to the Message interface.
NewMsgGetDataSizeHint returns a new Decred getdata message that conforms to the Message interface.
NewMsgGetHeaders returns a new Decred getheaders message that conforms to the Message interface.
NewMsgGetInitState returns a new Decred getinitialstate message that conforms to the Message interface.
NewMsgGetMiningState returns a new Decred pong message that conforms to the Message interface.
NewMsgHeaders returns a new Decred headers message that conforms to the Message interface.
NewMsgInitState returns a new Decred initstate message that conforms to the Message interface using the defaults for the fields.
NewMsgInitStateFilled returns a new Decred initstate message that conforms to the Message interface and fills the message with the provided data.
NewMsgInv returns a new Decred inv message that conforms to the Message interface.
NewMsgInvSizeHint returns a new Decred inv message that conforms to the Message interface.
NewMsgMemPool returns a new Decred pong message that conforms to the Message interface.
NewMsgMiningState returns a new Decred miningstate message that conforms to the Message interface using the defaults for the fields.
NewMsgMixCiphertexts returns a new mixcphrtxt message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgMixConfirm returns a new mixconfirm message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgMixDCNet returns a new mixdcnet message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgMixFactoredPoly returns a new mixpairreq message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgMixKeyExchange returns a new mixkeyxchg message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgMixPairReq returns a new mixpairreq message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgMixSecrets returns a new mixsecrets message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgMixSlotReserve returns a new mixslotres message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgNotFound returns a new Decred notfound message that conforms to the Message interface.
NewMsgPing returns a new Decred ping message that conforms to the Message interface.
NewMsgPong returns a new Decred pong message that conforms to the Message interface.
NewMsgReject returns a new Decred reject message that conforms to the Message interface.
NewMsgSendHeaders returns a new bitcoin sendheaders message that conforms to the Message interface.
NewMsgTx returns a new Decred tx message that conforms to the Message interface.
NewMsgVerAck returns a new Decred verack message that conforms to the Message interface.
NewMsgVersion returns a new Decred version message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgVersionFromConn is a convenience function that extracts the remote and local address from conn and returns a new Decred version message that conforms to the Message interface.
NewNetAddress returns a new NetAddress using the provided TCP address and supported services with defaults for the remaining fields.
NewNetAddressIPPort returns a new NetAddress using the provided IP, port, and supported services with defaults for the remaining fields.
NewNetAddressTimestamp returns a new NetAddress using the provided timestamp, IP, port, and supported services.
NewOutPoint returns a new Decred transaction outpoint point with the provided hash and index.
NewTxIn returns a new Decred transaction input with the provided previous outpoint point and signature script with a default sequence of MaxTxInSequenceNum.
NewTxOut returns a new Decred transaction output with the provided transaction value and public key script.
RandomUint64 returns a cryptographically random uint64 value.
ReadAsciiVarString reads a variable length string from r and returns it as a Go string.
ReadMessage reads, validates, and parses the next Decred Message from r for the provided protocol version and Decred network.
ReadMessageN reads, validates, and parses the next Decred Message from r for the provided protocol version and Decred network.
ReadOutPoint reads the next sequence of bytes from r as an OutPoint.
ReadVarBytes reads a variable length byte array.
ReadVarInt reads a variable length integer from r and returns it as a uint64.
ReadVarString reads a variable length string from r and returns it as a Go string.
VarIntSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.
WriteMessage writes a Decred Message to w including the necessary header information.
WriteMessageN writes a Decred Message to w including the necessary header information and returns the number of bytes written.
WriteOutPoint encodes op to the Decred protocol encoding for an OutPoint to w.
WriteVarBytes serializes a variable length byte array to w as a varInt containing the number of bytes, followed by the bytes themselves.
WriteVarInt serializes val to w using a variable number of bytes depending on its value.
WriteVarString serializes str to w as a variable length integer containing the length of the string followed by the bytes that represent the string itself.

# Constants

BatchedCFiltersV2Version is the protocol version which adds support for the batched getcfsv2 and cfiltersv2 messages.
CFilterV2Version is the protocol version which adds the getcfilterv2 and cfiltverv2 messages.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Deprecated: This command is no longer valid as of protocol version CFilterV2Version.
Deprecated: This command is no longer valid as of protocol version CFilterV2Version.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Deprecated: This command is no longer valid as of protocol version CFilterV2Version.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Deprecated: This command is no longer valid as of protocol version CFilterV2Version.
Deprecated: This command is no longer valid as of protocol version CFilterV2Version.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Deprecated: This command is no longer valid as of protocol version CFilterV2Version.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
Commands used in message headers which describe the type of message.
CommandSize is the fixed size of all commands in the common Decred message header.
DefaultPkScriptVersion is the default pkScript version, referring to extended Decred script.
DefaultUserAgent for wire in the stack.
ErrCmdTooLong is returned when a command exceeds the maximum command size allowed.
ErrFilterTooLarge is returned when a committed filter exceeds the maximum size allowed.
ErrHeaderContainsTxs is returned when a header's transactions count is greater than zero.
ErrInitialStateTypeTooLong is returned when an individual initial state type is longer than allowed by the protocol.
ErrInvalidMsg is returned for an invalid message structure.
ErrMalformedCmd is returned when a malformed command is received.
ErrMalformedStrictString is returned when a string that has strict formatting requirements does not conform to the requirements.
ErrMismatchedWitnessCount returned when a transaction has unequal witness and prefix txin quantities.
ErrMixPairReqScriptClassTooLong is returned when a mixing script class type string is longer than allowed by the protocol.
ErrMsgInvalidForPVer is returned when a message is invalid for the expected protocol version.
ErrNonCanonicalVarInt is returned when a variable length integer is not canonically encoded.
ErrPayloadChecksum is returned when a message with an invalid checksum is received.
ErrPayloadTooLarge is returned when a payload exceeds the maximum payload size allowed.
ErrReadInPrefixFromWitnessOnlyTx is returned when attempting to read a transaction input prefix from a witness only transaction.
ErrTooManyAddrs is returned when an address list exceeds the maximum allowed.
ErrTooManyBlocks is returned when the number of block hashes exceed the maximum allowed.
ErrTooManyCFilters is returned when the number of committed filters exceeds the maximum allowed in a batch.
ErrTooManyFilterHeaders is returned when the number of committed filter headers exceed the maximum allowed.
ErrTooManyFilterTypes is returned when the number of filter types exceeds the maximum allowed.
ErrTooManyHeaders is returned when the number of block headers exceed the maximum allowed.
ErrTooManyInitialStateTypes is returned when the number of initial state types is larger than the maximum allowed by the protocol.
ErrTooManyLocators is returned when the number of block locators exceed the maximum allowed.
ErrTooManyManyMixPairReqs is returned when the number of mix pair request message hashes exceeds the maximum allowed.
ErrTooManyMixPairReqUTXOs is returned when a MixPairReq message contains more UTXOs than allowed by the protocol.
ErrTooManyPrevMixMsgs is returned when too many previous messages of a mix run are referenced by a message.
ErrTooManyProofs is returned when the number of proof hashes exceeds the maximum allowed.
ErrTooManyTSpends is returned when the number of tspend hashes exceeds the maximum allowed.
ErrTooManyTxs is returned when a the number of transactions exceed the maximum allowed.
ErrTooManyVectors is returned when the number of inventory vectors exceed the maximum allowed.
ErrTooManyVotes is returned when the number of vote hashes exceed the maximum allowed.
ErrUnknownCmd is returned when an unknown command is received.
ErrUnknownTxType is returned when a transaction type is unknown.
ErrUserAgentTooLong is returned when the provided user agent exceeds the maximum allowed.
ErrVarBytesTooLong is returned when a variable-length byte slice exceeds the maximum message size allowed.
ErrVarStringTooLong is returned when a variable string exceeds the maximum message size allowed.
ErrWrongNetwork is returned when a message intended for a different network is received.
FeeFilterVersion is the protocol version which added a new feefilter message.
GCSFilterExtended is the extended filter type.
GCSFilterRegular is the regular filter type.
InitialProcotolVersion is the initial protocol version for the network.
InitStateHeadBlocks is the init state type used to request head blocks for mining.
InitStateHeadBlockVotes is the init state type used to request votes for the head blocks for mining.
InitStateTSpends is the init state type used to request tpends for voting.
InitStateVersion is the protocol version which adds the initstate and getinitstate messages.
These constants define the various supported inventory vector types.
These constants define the various supported inventory vector types.
These constants define the various supported inventory vector types.
These constants define the various supported inventory vector types.
These constants define the various supported inventory vector types.
MainNet represents the main Decred network.
MaxAddrPerMsg is the maximum number of addresses that can be in a single bitcoin addr message (MsgAddr).
MaxBlockHeaderPayload is the maximum number of bytes a block header can be.
MaxBlockHeadersPerMsg is the maximum number of block headers that can be in a single Decred headers message.
MaxBlockLocatorsPerMsg is the maximum number of block locator hashes allowed per message.
1.25MB.
Not actually 1MB which would be 1024 * 1024.
MaxBlockSizeVersion is the protocol version which increased the original blocksize.
MaxBlocksPerMsg is the maximum number of blocks allowed per message.
MaxCFHeaderPayload is the maximum byte size of a committed filter header.
MaxCFHeadersPerMsg is the maximum number of committed filter headers that can be in a single cfheaders message.
MaxCFilterDataSize is the maximum byte size of a committed filter.
MaxCFiltersV2PerBatch is the maximum number of committed filters that may be sent in a cfiltersv2 message.
MaxFilterTypesPerMsg is the maximum number of filter types allowed per message.
MaxHeaderProofHashes is the maximum number of header commitment inclusion proof hashes that can be in a single message.
MaxInitStateTypeLen is the maximum length allowable for an individual requested type.
MaxInitStateTypes is the maximum number of individual types stored in a getinitialstate message.
MaxInvPerMsg is the maximum number of inventory vectors that can be in a single Decred inv message.
MaxISBlocksAtHeadPerMsg is the maximum number of block hashes allowed per message.
MaxISTSpendsAtHeadPerMsg is the maximum number of tspends at head per message.
8 * 5.
32MB.
MaxMixFieldValLen is the maximum number of bytes allowed to represent a value in the slot reservation mix bounded by the field prime.
MaxMixMcount is the maximum number of mixed messages that are allowed in a single mix.
MaxMixPairReqScriptClassLen is the maximum length allowable for a MsgMixPairReq script class.
MaxMixPairReqUTXOPubKeyLen is the maximum length allowed for the pubkey of a UTXO ownership proof.
MaxMixPairReqUTXOs is the maximum number of unspent transaction outputs that may be contributed in a single mixpairreq message.
txscript.MaxScriptSize.
MaxMixPairReqUTXOSignatureLen is the maximum length allowed for the signature of a UTXO ownership proof.
MaxMixPeers is the maximum number of peers allowed together in a single mix.
MaxMSBlocksAtHeadPerMsg is the maximum number of block hashes allowed per message.
8 * 5.
MaxPrevOutIndex is the maximum index the index field of a previous outpoint can be.
MaxTxInSequenceNum is the maximum sequence number the sequence field of a transaction input can be.
MaxUserAgentLen is the maximum allowed length for the user agent field in a version message (MsgVersion).
MaxVarIntPayload is the maximum payload size for a variable length integer.
MessageHeaderSize is the number of bytes in a Decred message header.
No description provided by the author
MixVersion is the protocol version which adds peer-to-peer mixing.
NodeBloomVersion is the protocol version which added the SFNodeBloom service flag (unused).
NodeCFVersion is the protocol version which adds the SFNodeCF service flag and the cfheaders, cfilter, cftypes, getcfheaders, getcfilter and getcftypes messages.
NoExpiryValue is the value of expiry that indicates the transaction has no expiry.
NullBlockHeight is the null value for an input witness.
NullBlockIndex is the null transaction index in a block for an input witness.
NullValueIn is a null value for an input witness.
ProtocolVersion is the latest protocol version this package supports.
RegNet represents the regression test network.
RegTest represents the regression test network.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
RemoveRejectVersion is the protocol version which removes support for the reject message.
SendHeadersVersion is the protocol version which added a new sendheaders message.
SequenceLockTimeDisabled is a flag that if set on a transaction input's sequence number, the sequence number will not be interpreted as a relative locktime.
SequenceLockTimeGranularity is the defined time based granularity for seconds-based relative time locks.
SequenceLockTimeIsSeconds is a flag that if set on a transaction input's sequence number, the relative locktime has units of 512 seconds.
SequenceLockTimeMask is a mask that extracts the relative locktime when masked against the transaction input sequence number.
SFNodeBloom is a flag used to indicate a peer supports bloom filtering.
SFNodeCF is a flag used to indicate a peer supports v1 gcs filters (CFs).
SFNodeNetwork is a flag used to indicate a peer is a full node.
SimNet represents the simulation test network.
TestNet3 represents the 3rd test network.
TxSerializeFull indicates a transaction be serialized with the prefix and all witness data.
TxSerializeNoWitness indicates a transaction be serialized with only the prefix.
TxSerializeOnlyWitness indicates a transaction be serialized with only the witness data.
TxTreeRegular is the value for a normal transaction tree for a transaction's location in a block.
TxTreeStake is the value for a stake transaction tree for a transaction's location in a block.
TxTreeUnknown is the value returned for a transaction tree that is unknown.
TxVersion is the initial transaction version.
TxVersionSeqLock is the transaction version that enables sequence locks.
TxVersionTreasury is the transaction version that enables the decentralized treasury features.

# Variables

ErrInvalidNetAddr describes an error that indicates the caller didn't specify a TCP address as required.

# Structs

BlockHeader defines information about a block and is used in the decred block (MsgBlock) and headers (MsgHeaders) messages.
InvVect defines a Decred inventory vector which is used to describe data, as specified by the Type field, that a peer wants, has, or does not have to another peer.
MessageError describes an issue with a message.
MixPairReqUTXO describes an unspent transaction output to be spent in a mix.
MsgAddr implements the Message interface and represents a bitcoin addr message.
MsgBlock implements the Message interface and represents a decred block message.
MsgCFHeaders implements the Message interface and represents a cfheaders message.
MsgCFilter implements the Message interface and represents a cfilter message.
MsgCFiltersV2 implements the Message interface and represents a cfiltersv2 message.
MsgCFilterV2 implements the Message interface and represents a cfilterv2 message.
MsgCFTypes is the cftypes message.
MsgFeeFilter implements the Message interface and represents a feefilter message.
MsgGetAddr implements the Message interface and represents a decred getaddr message.
MsgGetBlocks implements the Message interface and represents a decred getblocks message.
MsgGetCFHeaders is a message similar to MsgGetHeaders, but for committed filter headers.
MsgGetCFilter implements the Message interface and represents a getcfilter message.
MsgGetCFilterV2 implements the Message interface and represents a decred getcfilterv2 message.
MsgGetCFsV2 implements the Message interface and represents a decred getcfsv2 message.
MsgGetCFTypes is the getcftypes message.
MsgGetData implements the Message interface and represents a decred getdata message.
MsgGetHeaders implements the Message interface and represents a decred getheaders message.
MsgGetInitState implements the Message interface and represents a getinitstate message.
MsgGetMiningState implements the Message interface and represents a getminingstate message.
MsgHeaders implements the Message interface and represents a Decred headers message.
MsgInitState implements the Message interface and represents an initial state message.
MsgInv implements the Message interface and represents a Decred inv message.
MsgMemPool implements the Message interface and represents a Decred mempool message.
MsgMiningState implements the Message interface and represents a mining state message.
MsgMixCiphertexts is used by mixing peers to share SNTRUP4591761 ciphertexts with other peers who have published their public keys.
MsgMixConfirm contains a partially-signed mix transaction, with signatures contributed from the peer identity.
MsgMixDCNet is the DC-net broadcast.
MsgMixFactoredPoly encodes the solution of the factored slot reservation polynomial.
MsgMixKeyExchange implements the Message interface and represents a mixing key exchange message.
MsgMixPairReq implements the Message interface and represents a mixing pair request message.
MsgMixSecrets reveals secrets of a failed mix run.
MsgMixSlotReserve is the slot reservation broadcast.
MsgNotFound defines a Decred notfound message which is sent in response to a getdata message if any of the requested data in not available on the peer.
MsgPing implements the Message interface and represents a Decred ping message.
MsgPong implements the Message interface and represents a Decred pong message which is used primarily to confirm that a connection is still valid in response to a Decred ping message (MsgPing).
MsgReject implements the Message interface and represents a Decred reject message.
MsgSendHeaders implements the Message interface and represents a bitcoin sendheaders message.
MsgTx implements the Message interface and represents a Decred tx message.
MsgVerAck defines a Decred verack message which is used for a peer to acknowledge a version message (MsgVersion) after it has used the information to negotiate parameters.
MsgVersion implements the Message interface and represents a Decred version message.
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.
OutPoint defines a Decred data type that is used to track previous transaction outputs.
TxIn defines a Decred transaction input.
TxLoc holds locator data for the offset and length of where a transaction is located within a MsgBlock data buffer.
TxOut defines a Decred transaction output.

# Interfaces

Message is an interface that describes a Decred message.

# Type aliases

CurrencyNet represents which Decred network a message belongs to.
ErrorCode describes a kind of message error.
FilterType is used to represent a filter type.
InvType represents the allowed types of inventory vectors.
MixVect is vector of padded or unpadded DC-net messages.
RejectCode represents a numeric value by which a remote peer indicates why a message was rejected.
ServiceFlag identifies services supported by a Decred peer.
TxSerializeType represents the serialized type of a transaction.