Documentation ¶
Overview ¶
Package domainmessage implements the kaspa domainmessage protocol.
At a high level, this package provides support for marshalling and unmarshalling supported kaspa messages to and from the domainmessage. This package does not deal with the specifics of message handling such as what to do when a message is received. This provides the caller with a high level of flexibility.
Kaspa Message Overview ¶
The kaspa protocol consists of exchanging messages between peers. Each message is preceded by a header which identifies information about it such as which kaspa 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 kaspa 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 kaspa messages are provided. For these supported messages, all of the details of marshalling and unmarshalling to and from the domainmessage using kaspa encoding are handled so the caller doesn't have to concern themselves with the specifics.
Message Interaction ¶
The following provides a quick summary of how the kaspa messages are intended to interact with one another. As stated above, these interactions are not directly handled by this package.
The initial handshake consists of two peers sending each other a version message (MsgVersion) followed by responding with a verack message (MsgVerAck). Both peers use the information in the version message (MsgVersion) to negotiate things such as protocol version and supported services with each other. Once the initial handshake is complete, the following chart indicates message interactions in no particular order.
Peer A Sends Peer B Responds ---------------------------------------------------------------------------- getaddr message (MsgRequestAddresses) addr message (MsgAddresses) getblockinvs message (MsgGetBlockInvs) inv message (MsgInv) inv message (MsgInv) getdata message (MsgGetData) getdata message (MsgGetData) block message (MsgBlock) -or- tx message (MsgTx) -or- notfound message (MsgNotFound) ping message (MsgPing) pong message (MsgPong)
Common Parameters ¶
There are several common parameters that arise when using this package to read and write kaspa messages. The following sections provide a quick overview of these parameters so the next sections can build on them.
Protocol Version ¶
The protocol version should be negotiated with the remote peer at a higher level than this package via the version (MsgVersion) message exchange, however, this package provides the domainmessage.ProtocolVersion constant which indicates the latest protocol version this package supports and is typically the value to use for all outbound connections before a potentially lower protocol version is negotiated.
Kaspa Network ¶
The kaspa network is a magic number which is used to identify the start of a message and which kaspa network the message applies to. This package provides the following constants:
domainmessage.Mainnet domainmessage.Testnet (Test network) domainmessage.Regtest (Regression test network) domainmessage.Simnet (Simulation test network) domainmessage.Devnet (Development network)
Determining Message Type ¶
As discussed in the kaspa message overview section, this package reads and writes kaspa messages using a generic interface named Message. In order to determine the actual concrete type of the message, use a type switch or type assertion. An example of a type switch follows:
// Assumes msg is already a valid concrete message such as one created // via NewMsgVersion or read via ReadMessage. switch msg := msg.(type) { case *domainmessage.MsgVersion: // The message is a pointer to a MsgVersion struct. fmt.Printf("Protocol version: %d", msg.ProtocolVersion) case *domainmessage.MsgBlock: // The message is a pointer to a MsgBlock struct. fmt.Printf("Number of tx in block: %d", msg.Header.TxnCount) }
Reading Messages ¶
In order to unmarshall kaspa messages from the domainmessage, use the ReadMessage function. It accepts any io.Reader, but typically this will be a net.Conn to a remote node running a kaspa peer. Example syntax is:
// Reads and validates the next kaspa message from conn using the // protocol version pver and the kaspa network kaspaNet. The returns // are a domainmessage.Message, a []byte which contains the unmarshalled // raw payload, and a possible error. msg, rawPayload, err := domainmessage.ReadMessage(conn, pver, kaspaNet) if err != nil { // Log and handle the error }
Writing Messages ¶
In order to marshall kaspa messages to the domainmessage, use the WriteMessage function. It accepts any io.Writer, but typically this will be a net.Conn to a remote node running a kaspa peer. Example syntax to request addresses from a remote peer is:
// Create a new getaddr kaspa message. msg := domainmessage.NewMsgRequestAddresses() // Writes a kaspa message msg to conn using the protocol version // pver, and the kaspa network kaspaNet. The return is a possible // error. err := domainmessage.WriteMessage(conn, msg, pver, kaspaNet) if err != nil { // Log and handle the error }
Errors ¶
Errors returned by this package are either the raw errors provided by underlying calls to read/write from streams such as io.EOF, io.ErrUnexpectedEOF, and io.ErrShortWrite, or of type domainmessage.MessageError. This allows the caller to differentiate between general IO errors and malformed messages through type assertions.
Index ¶
- Constants
- Variables
- func ReadElement(r io.Reader, element interface{}) error
- func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32, fieldName string) ([]byte, error)
- func ReadVarInt(r io.Reader) (uint64, error)
- func ReadVarString(r io.Reader, pver uint32) (string, error)
- func ValidateUserAgent(userAgent string) error
- func VarIntSerializeSize(val uint64) int
- func WriteElement(w io.Writer, element interface{}) error
- func WriteTxOut(w io.Writer, pver uint32, version int32, to *TxOut) error
- func WriteVarBytes(w io.Writer, pver uint32, bytes []byte) error
- func WriteVarInt(w io.Writer, val uint64) error
- func WriteVarString(w io.Writer, str string) error
- type BlockHeader
- func (h *BlockHeader) BlockHash() *daghash.Hash
- func (h *BlockHeader) Deserialize(r io.Reader) error
- func (h *BlockHeader) IsGenesis() bool
- func (h *BlockHeader) KaspaDecode(r io.Reader, pver uint32) error
- func (h *BlockHeader) KaspaEncode(w io.Writer, pver uint32) error
- func (h *BlockHeader) NumParentBlocks() byte
- func (h *BlockHeader) Serialize(w io.Writer) error
- func (h *BlockHeader) SerializeSize() int
- type KaspaNet
- type Message
- type MessageCommand
- type MessageError
- type MsgAddresses
- type MsgBlock
- func (msg *MsgBlock) AddTransaction(tx *MsgTx)
- func (msg *MsgBlock) BlockHash() *daghash.Hash
- func (msg *MsgBlock) ClearTransactions()
- func (msg *MsgBlock) Command() MessageCommand
- func (msg *MsgBlock) ConvertToPartial(subnetworkID *subnetworkid.SubnetworkID)
- func (msg *MsgBlock) Deserialize(r io.Reader) error
- func (msg *MsgBlock) DeserializeTxLoc(r *bytes.Buffer) ([]TxLoc, error)
- func (msg *MsgBlock) KaspaDecode(r io.Reader, pver uint32) error
- func (msg *MsgBlock) KaspaEncode(w io.Writer, pver uint32) error
- func (msg *MsgBlock) MaxPayloadLength(pver uint32) uint32
- func (msg *MsgBlock) Serialize(w io.Writer) error
- func (msg *MsgBlock) SerializeSize() int
- type MsgBlockLocator
- type MsgDoneIBDBlocks
- type MsgIBDBlock
- type MsgInvRelayBlock
- type MsgInvTransaction
- type MsgPing
- type MsgPong
- type MsgRequestAddresses
- type MsgRequestBlockLocator
- type MsgRequestIBDBlocks
- type MsgRequestNextIBDBlocks
- type MsgRequestRelayBlocks
- type MsgRequestSelectedTip
- type MsgRequestTransactions
- type MsgSelectedTip
- type MsgTransactionNotFound
- type MsgTx
- func NewNativeMsgTx(version int32, txIn []*TxIn, txOut []*TxOut) *MsgTx
- func NewNativeMsgTxWithLocktime(version int32, txIn []*TxIn, txOut []*TxOut, locktime uint64) *MsgTx
- func NewRegistryMsgTx(version int32, txIn []*TxIn, txOut []*TxOut, gasLimit uint64) *MsgTx
- func NewSubnetworkMsgTx(version int32, txIn []*TxIn, txOut []*TxOut, ...) *MsgTx
- func (msg *MsgTx) AddTxIn(ti *TxIn)
- func (msg *MsgTx) AddTxOut(to *TxOut)
- func (msg *MsgTx) Command() MessageCommand
- func (msg *MsgTx) Copy() *MsgTx
- func (msg *MsgTx) Deserialize(r io.Reader) error
- func (msg *MsgTx) IsCoinBase() bool
- func (msg *MsgTx) IsSubnetworkCompatible(subnetworkID *subnetworkid.SubnetworkID) bool
- func (msg *MsgTx) KaspaDecode(r io.Reader, pver uint32) error
- func (msg *MsgTx) KaspaEncode(w io.Writer, pver uint32) error
- func (msg *MsgTx) MaxPayloadLength(pver uint32) uint32
- func (msg *MsgTx) ScriptPubKeyLocs() []int
- func (msg *MsgTx) Serialize(w io.Writer) error
- func (msg *MsgTx) SerializeSize() int
- func (msg *MsgTx) TxHash() *daghash.Hash
- func (msg *MsgTx) TxID() *daghash.TxID
- type MsgVerAck
- type MsgVersion
- type NetAddress
- type Outpoint
- type ServiceFlag
- type TxIn
- type TxLoc
- type TxOut
Constants ¶
const ( // TxVersion is the current latest supported transaction version. TxVersion = 1 // MaxTxInSequenceNum is the maximum sequence number the sequence field // of a transaction input can be. MaxTxInSequenceNum uint64 = math.MaxUint64 // MaxPrevOutIndex is the maximum index the index field of a previous // outpoint can be. MaxPrevOutIndex uint32 = 0xffffffff // 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. SequenceLockTimeDisabled = 1 << 31 // SequenceLockTimeIsSeconds is a flag that if set on a transaction // input's sequence number, the relative locktime has units of 512 // seconds. SequenceLockTimeIsSeconds = 1 << 22 // SequenceLockTimeMask is a mask that extracts the relative locktime // when masked against the transaction input sequence number. SequenceLockTimeMask = 0x0000ffff // SequenceLockTimeGranularity is the defined time based granularity // for milliseconds-based relative time locks. When converting from milliseconds // to a sequence number, the value is right shifted by this amount, // therefore the granularity of relative time locks in 524288 or 2^19 // seconds. Enforced relative lock times are multiples of 524288 milliseconds. SequenceLockTimeGranularity = 19 // MinTxOutPayload is the minimum payload size for a transaction output. // Value 8 bytes + Varint for ScriptPubKey length 1 byte. MinTxOutPayload = 9 )
const BaseBlockHeaderPayload = 25 + 3*(daghash.HashSize)
BaseBlockHeaderPayload is the base number of bytes a block header can be, not including the list of parent block headers. Version 4 bytes + Timestamp 8 bytes + Bits 4 bytes + Nonce 8 bytes + + NumParentBlocks 1 byte + HashMerkleRoot hash + + AcceptedIDMerkleRoot hash + UTXOCommitment hash. To get total size of block header len(ParentHashes) * daghash.HashSize should be added to this value
const MaxAddressesPerMsg = 1000
MaxAddressesPerMsg is the maximum number of addresses that can be in a single kaspa Addresses message (MsgAddresses).
const MaxBlockHeaderPayload = BaseBlockHeaderPayload + (MaxNumParentBlocks * daghash.HashSize)
MaxBlockHeaderPayload is the maximum number of bytes a block header can be. BaseBlockHeaderPayload + up to MaxNumParentBlocks hashes of parent blocks
const MaxBlockLocatorsPerMsg = 500
MaxBlockLocatorsPerMsg is the maximum number of block locator hashes allowed per message.
const MaxInvPerMsg = 1 << 17
MaxInvPerMsg is the maximum number of inventory vectors that can be in any type of kaspa inv message.
const MaxInvPerRequestTransactionsMsg = MaxInvPerMsg
MaxInvPerRequestTransactionsMsg is the maximum number of hashes that can be in a single CmdInvTransaction message.
const MaxInvPerTxInvMsg = MaxInvPerMsg
MaxInvPerTxInvMsg is the maximum number of hashes that can be in a single CmdInvTransaction message.
const MaxMassPerBlock = 10000000
MaxMassPerBlock is the maximum total transaction mass a block may have.
const MaxMassPerTx = MaxMassPerBlock / 2
MaxMassPerTx is the maximum total mass a transaction may have.
const MaxMessagePayload = (1024 * 1024 * 32) // 32MB
MaxMessagePayload is the maximum bytes a message can be regardless of other individual limits imposed by messages themselves.
const MaxNumParentBlocks = 255
MaxNumParentBlocks is the maximum number of parent blocks a block can reference. Currently set to 255 as the maximum number NumParentBlocks can be due to it being a byte
const MaxTxPerBlock = (MaxMassPerBlock / minTxPayload) + 1
MaxTxPerBlock is the maximum number of transactions that could possibly fit into a block.
const MaxUserAgentLen = 256
MaxUserAgentLen is the maximum allowed length for the user agent field in a version message (MsgVersion).
const MaxVarIntPayload = 9
MaxVarIntPayload is the maximum payload size for a variable length integer.
const MsgRequestRelayBlocksHashes = MaxInvPerMsg
MsgRequestRelayBlocksHashes is the maximum number of hashes that can be in a single RequestRelayBlocks message.
const ( // ProtocolVersion is the latest protocol version this package supports. ProtocolVersion uint32 = 1 )
XXX pedro: we will probably need to bump this.
Variables ¶
var DefaultUserAgent = fmt.Sprintf("/kaspad:%s/", version.Version())
DefaultUserAgent for domainmessage in the stack
var MessageCommandToString = map[MessageCommand]string{ CmdVersion: "Version", CmdVerAck: "VerAck", CmdRequestAddresses: "RequestAddresses", CmdAddresses: "Addresses", CmdRequestIBDBlocks: "RequestBlocks", CmdBlock: "Block", CmdTx: "Tx", CmdPing: "Ping", CmdPong: "Pong", CmdRequestBlockLocator: "RequestBlockLocator", CmdBlockLocator: "BlockLocator", CmdSelectedTip: "SelectedTip", CmdRequestSelectedTip: "RequestSelectedTip", CmdInvRelayBlock: "InvRelayBlock", CmdRequestRelayBlocks: "RequestRelayBlocks", CmdInvTransaction: "InvTransaction", CmdRequestTransactions: "RequestTransactions", CmdIBDBlock: "IBDBlock", CmdRequestNextIBDBlocks: "RequestNextIBDBlocks", CmdDoneIBDBlocks: "DoneIBDBlocks", CmdTransactionNotFound: "TransactionNotFound", }
MessageCommandToString maps all MessageCommands to their string representation
Functions ¶
func ReadElement ¶
ReadElement reads the next sequence of bytes from r using little endian depending on the concrete type of element pointed to.
func ReadVarBytes ¶
ReadVarBytes reads a variable length byte array. A byte array is encoded as a varInt containing the length of the array followed by the bytes themselves. An error is returned if the length is greater than the passed maxAllowed parameter which helps protect against memory exhaustion attacks and forced panics through malformed messages. The fieldName parameter is only used for the error message so it provides more context in the error.
func ReadVarInt ¶
ReadVarInt reads a variable length integer from r and returns it as a uint64.
func ReadVarString ¶
ReadVarString reads a variable length string from r and returns it as a Go string. A variable length string is encoded as a variable length integer containing the length of the string followed by the bytes that represent the string itself. An error is returned if the length is greater than the maximum block payload size since it helps protect against memory exhaustion attacks and forced panics through malformed messages.
func ValidateUserAgent ¶
ValidateUserAgent checks userAgent length against MaxUserAgentLen
func VarIntSerializeSize ¶
VarIntSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.
func WriteElement ¶
WriteElement writes the little endian representation of element to w.
func WriteTxOut ¶
WriteTxOut encodes to into the kaspa protocol encoding for a transaction output (TxOut) to w.
func WriteVarBytes ¶
WriteVarBytes serializes a variable length byte array to w as a varInt containing the number of bytes, followed by the bytes themselves.
func WriteVarInt ¶
WriteVarInt serializes val to w using a variable number of bytes depending on its value.
Types ¶
type BlockHeader ¶
type BlockHeader struct { // Version of the block. This is not the same as the protocol version. Version int32 // Hashes of the parent block headers in the blockDAG. ParentHashes []*daghash.Hash // HashMerkleRoot is the merkle tree reference to hash of all transactions for the block. HashMerkleRoot *daghash.Hash // AcceptedIDMerkleRoot is merkle tree reference to hash all transactions // accepted form the block.Blues AcceptedIDMerkleRoot *daghash.Hash // UTXOCommitment is an ECMH UTXO commitment to the block UTXO. UTXOCommitment *daghash.Hash // Time the block was created. Timestamp mstime.Time // Difficulty target for the block. Bits uint32 // Nonce used to generate the block. Nonce uint64 }
BlockHeader defines information about a block and is used in the kaspa block (MsgBlock) and headers (MsgHeader) messages.
func NewBlockHeader ¶
func NewBlockHeader(version int32, parentHashes []*daghash.Hash, hashMerkleRoot *daghash.Hash, acceptedIDMerkleRoot *daghash.Hash, utxoCommitment *daghash.Hash, bits uint32, nonce uint64) *BlockHeader
NewBlockHeader returns a new BlockHeader using the provided version, previous block hash, hash merkle root, accepted ID merkle root, difficulty bits, and nonce used to generate the block with defaults or calclulated values for the remaining fields.
func (*BlockHeader) BlockHash ¶
func (h *BlockHeader) BlockHash() *daghash.Hash
BlockHash computes the block identifier hash for the given block header.
func (*BlockHeader) Deserialize ¶
func (h *BlockHeader) Deserialize(r io.Reader) error
Deserialize decodes a block header from r into the receiver using a format that is suitable for long-term storage such as a database while respecting the Version field.
func (*BlockHeader) IsGenesis ¶
func (h *BlockHeader) IsGenesis() bool
IsGenesis returns true iff this block is a genesis block
func (*BlockHeader) KaspaDecode ¶
func (h *BlockHeader) KaspaDecode(r io.Reader, pver uint32) error
KaspaDecode decodes r using the kaspa protocol encoding into the receiver. This is part of the Message interface implementation. See Deserialize for decoding block headers stored to disk, such as in a database, as opposed to decoding block headers from the domainmessage.
func (*BlockHeader) KaspaEncode ¶
func (h *BlockHeader) KaspaEncode(w io.Writer, pver uint32) error
KaspaEncode encodes the receiver to w using the kaspa protocol encoding. This is part of the Message interface implementation. See Serialize for encoding block headers to be stored to disk, such as in a database, as opposed to encoding block headers for the domainmessage.
func (*BlockHeader) NumParentBlocks ¶
func (h *BlockHeader) NumParentBlocks() byte
NumParentBlocks return the number of entries in ParentHashes
func (*BlockHeader) Serialize ¶
func (h *BlockHeader) Serialize(w io.Writer) error
Serialize encodes a block header from r into the receiver using a format that is suitable for long-term storage such as a database while respecting the Version field.
func (*BlockHeader) SerializeSize ¶
func (h *BlockHeader) SerializeSize() int
SerializeSize returns the number of bytes it would take to serialize the block header.
type KaspaNet ¶
type KaspaNet uint32
KaspaNet represents which kaspa network a message belongs to.
const ( // Mainnet represents the main kaspa network. Mainnet KaspaNet = 0x3ddcf71d // Testnet represents the test network. Testnet KaspaNet = 0xddb8af8f // Regtest represents the regression test network. Regtest KaspaNet = 0xf396cdd6 // Simnet represents the simulation test network. Simnet KaspaNet = 0x374dcf1c // Devnet represents the development test network. Devnet KaspaNet = 0x732d87e1 )
Constants used to indicate the message kaspa network. They can also be used to seek to the next message when a stream's state is unknown, but this package does not provide that functionality since it's generally a better idea to simply disconnect clients that are misbehaving over TCP.
type Message ¶
type Message interface {
Command() MessageCommand
}
Message is an interface that describes a kaspa message. A type that implements Message has complete control over the representation of its data and may therefore contain additional or fewer fields than those which are used directly in the protocol encoded message.
type MessageCommand ¶
type MessageCommand uint32
MessageCommand is a number in the header of a message that represents its type.
const ( CmdVersion MessageCommand = iota CmdVerAck CmdRequestAddresses CmdAddresses CmdRequestIBDBlocks CmdBlock CmdTx CmdPing CmdPong CmdRequestBlockLocator CmdBlockLocator CmdSelectedTip CmdRequestSelectedTip CmdInvRelayBlock CmdRequestRelayBlocks CmdInvTransaction CmdRequestTransactions CmdIBDBlock CmdRequestNextIBDBlocks CmdDoneIBDBlocks CmdTransactionNotFound )
Commands used in kaspa message headers which describe the type of message.
func (MessageCommand) String ¶
func (cmd MessageCommand) String() string
type MessageError ¶
type MessageError struct { Func string // Function name Description string // Human readable description of the issue }
MessageError describes an issue with a message. An example of some potential issues are messages from the wrong kaspa network, invalid commands, mismatched checksums, and exceeding max payloads.
This provides a mechanism for the caller to type assert the error to differentiate between general io errors such as io.EOF and issues that resulted from malformed messages.
func (*MessageError) Error ¶
func (e *MessageError) Error() string
Error satisfies the error interface and prints human-readable errors.
type MsgAddresses ¶
type MsgAddresses struct { IncludeAllSubnetworks bool SubnetworkID *subnetworkid.SubnetworkID AddrList []*NetAddress }
MsgAddresses implements the Message interface and represents a kaspa Addresses message. It is used to provide a list of known active peers on the network. An active peer is considered one that has transmitted a message within the last 3 hours. Nodes which have not transmitted in that time frame should be forgotten. Each message is limited to a maximum number of addresses, which is currently 1000. As a result, multiple messages must be used to relay the full list.
Use the AddAddress function to build up the list of known addresses when sending an Addresses message to another peer.
func NewMsgAddresses ¶
func NewMsgAddresses(includeAllSubnetworks bool, subnetworkID *subnetworkid.SubnetworkID) *MsgAddresses
NewMsgAddresses returns a new kaspa Addresses message that conforms to the Message interface. See MsgAddresses for details.
func (*MsgAddresses) AddAddress ¶
func (msg *MsgAddresses) AddAddress(na *NetAddress) error
AddAddress adds a known active peer to the message.
func (*MsgAddresses) AddAddresses ¶
func (msg *MsgAddresses) AddAddresses(netAddrs ...*NetAddress) error
AddAddresses adds multiple known active peers to the message.
func (*MsgAddresses) ClearAddresses ¶
func (msg *MsgAddresses) ClearAddresses()
ClearAddresses removes all addresses from the message.
func (*MsgAddresses) Command ¶
func (msg *MsgAddresses) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgBlock ¶
type MsgBlock struct { Header BlockHeader Transactions []*MsgTx }
MsgBlock implements the Message interface and represents a kaspa block message. It is used to deliver block and transaction information in response to a getdata message (MsgGetData) for a given block hash.
func NewMsgBlock ¶
func NewMsgBlock(blockHeader *BlockHeader) *MsgBlock
NewMsgBlock returns a new kaspa block message that conforms to the Message interface. See MsgBlock for details.
func (*MsgBlock) AddTransaction ¶
AddTransaction adds a transaction to the message.
func (*MsgBlock) ClearTransactions ¶
func (msg *MsgBlock) ClearTransactions()
ClearTransactions removes all transactions from the message.
func (*MsgBlock) Command ¶
func (msg *MsgBlock) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
func (*MsgBlock) ConvertToPartial ¶
func (msg *MsgBlock) ConvertToPartial(subnetworkID *subnetworkid.SubnetworkID)
ConvertToPartial clears out all the payloads of the subnetworks that are incompatible with the given subnetwork ID. Note: this operation modifies the block in place.
func (*MsgBlock) Deserialize ¶
Deserialize decodes a block from r into the receiver using a format that is suitable for long-term storage such as a database while respecting the Version field in the block. This function differs from KaspaDecode in that KaspaDecode decodes from the kaspa domainmessage protocol as it was sent across the network. The domainmessage encoding can technically differ depending on the protocol version and doesn't even really need to match the format of a stored block at all. As of the time this comment was written, the encoded block is the same in both instances, but there is a distinct difference and separating the two allows the API to be flexible enough to deal with changes.
func (*MsgBlock) DeserializeTxLoc ¶
DeserializeTxLoc decodes r in the same manner Deserialize does, but it takes a byte buffer instead of a generic reader and returns a slice containing the start and length of each transaction within the raw data that is being deserialized.
func (*MsgBlock) KaspaDecode ¶
KaspaDecode decodes r using the kaspa protocol encoding into the receiver. This is part of the Message interface implementation. See Deserialize for decoding blocks stored to disk, such as in a database, as opposed to decoding blocks from the domainmessage.
func (*MsgBlock) KaspaEncode ¶
KaspaEncode encodes the receiver to w using the kaspa protocol encoding. This is part of the Message interface implementation. See Serialize for encoding blocks to be stored to disk, such as in a database, as opposed to encoding blocks for the domainmessage.
func (*MsgBlock) MaxPayloadLength ¶
MaxPayloadLength returns the maximum length the payload can be for the receiver. This is part of the Message interface implementation.
func (*MsgBlock) Serialize ¶
Serialize encodes the block to w using a format that suitable for long-term storage such as a database while respecting the Version field in the block. This function differs from KaspaEncode in that KaspaEncode encodes the block to the kaspa domainmessage protocol in order to be sent across the network. The domainmessage encoding can technically differ depending on the protocol version and doesn't even really need to match the format of a stored block at all. As of the time this comment was written, the encoded block is the same in both instances, but there is a distinct difference and separating the two allows the API to be flexible enough to deal with changes.
func (*MsgBlock) SerializeSize ¶
SerializeSize returns the number of bytes it would take to serialize the block.
type MsgBlockLocator ¶
MsgBlockLocator implements the Message interface and represents a kaspa locator message. It is used to find the blockLocator of a peer that is syncing with you.
func NewMsgBlockLocator ¶
func NewMsgBlockLocator(locatorHashes []*daghash.Hash) *MsgBlockLocator
NewMsgBlockLocator returns a new kaspa locator message that conforms to the Message interface. See MsgBlockLocator for details.
func (*MsgBlockLocator) Command ¶
func (msg *MsgBlockLocator) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgDoneIBDBlocks ¶
type MsgDoneIBDBlocks struct{}
MsgDoneIBDBlocks implements the Message interface and represents a kaspa DoneIBDBlocks message. It is used to notify the IBD syncing peer that the syncer sent all the requested blocks.
This message has no payload.
func NewMsgDoneIBDBlocks ¶
func NewMsgDoneIBDBlocks() *MsgDoneIBDBlocks
NewMsgDoneIBDBlocks returns a new kaspa DoneIBDBlocks message that conforms to the Message interface.
func (*MsgDoneIBDBlocks) Command ¶
func (msg *MsgDoneIBDBlocks) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgIBDBlock ¶
type MsgIBDBlock struct {
*MsgBlock
}
MsgIBDBlock implements the Message interface and represents a kaspa ibdblock message. It is used to deliver block and transaction information in response to a RequestIBDBlocks message (MsgRequestIBDBlocks).
func NewMsgIBDBlock ¶
func NewMsgIBDBlock(msgBlock *MsgBlock) *MsgIBDBlock
NewMsgIBDBlock returns a new kaspa ibdblock message that conforms to the Message interface. See MsgIBDBlock for details.
func (*MsgIBDBlock) Command ¶
func (msg *MsgIBDBlock) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
func (*MsgIBDBlock) MaxPayloadLength ¶
func (msg *MsgIBDBlock) MaxPayloadLength(pver uint32) uint32
MaxPayloadLength returns the maximum length the payload can be for the receiver. This is part of the Message interface implementation.
type MsgInvRelayBlock ¶
MsgInvRelayBlock implements the Message interface and represents a kaspa block inventory message. It is used to notify the network about new block by sending their hash, and let the receiving node decide if it needs it.
func NewMsgInvBlock ¶
func NewMsgInvBlock(hash *daghash.Hash) *MsgInvRelayBlock
NewMsgInvBlock returns a new kaspa invrelblk message that conforms to the Message interface. See MsgInvRelayBlock for details.
func (*MsgInvRelayBlock) Command ¶
func (msg *MsgInvRelayBlock) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgInvTransaction ¶
MsgInvTransaction implements the Message interface and represents a kaspa TxInv message. It is used to notify the network about new transactions by sending their ID, and let the receiving node decide if it needs it.
func NewMsgInvTransaction ¶
func NewMsgInvTransaction(ids []*daghash.TxID) *MsgInvTransaction
NewMsgInvTransaction returns a new kaspa TxInv message that conforms to the Message interface. See MsgInvTransaction for details.
func (*MsgInvTransaction) Command ¶
func (msg *MsgInvTransaction) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgPing ¶
type MsgPing struct { // Unique value associated with message that is used to identify // specific ping message. Nonce uint64 }
MsgPing implements the Message interface and represents a kaspa ping message.
For versions BIP0031Version and earlier, it is used primarily to confirm that a connection is still valid. A transmission error is typically interpreted as a closed connection and that the peer should be removed. For versions AFTER BIP0031Version it contains an identifier which can be returned in the pong message to determine network timing.
The payload for this message just consists of a nonce used for identifying it later.
func NewMsgPing ¶
NewMsgPing returns a new kaspa ping message that conforms to the Message interface. See MsgPing for details.
func (*MsgPing) Command ¶
func (msg *MsgPing) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgPong ¶
type MsgPong struct { // Unique value associated with message that is used to identify // specific ping message. Nonce uint64 }
MsgPong implements the Message interface and represents a kaspa pong message which is used primarily to confirm that a connection is still valid in response to a kaspa ping message (MsgPing).
This message was not added until protocol versions AFTER BIP0031Version.
func NewMsgPong ¶
NewMsgPong returns a new kaspa pong message that conforms to the Message interface. See MsgPong for details.
func (*MsgPong) Command ¶
func (msg *MsgPong) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgRequestAddresses ¶
type MsgRequestAddresses struct { IncludeAllSubnetworks bool SubnetworkID *subnetworkid.SubnetworkID }
MsgRequestAddresses implements the Message interface and represents a kaspa RequestAddresses message. It is used to request a list of known active peers on the network from a peer to help identify potential nodes. The list is returned via one or more addr messages (MsgAddresses).
This message has no payload.
func NewMsgRequestAddresses ¶
func NewMsgRequestAddresses(includeAllSubnetworks bool, subnetworkID *subnetworkid.SubnetworkID) *MsgRequestAddresses
NewMsgRequestAddresses returns a new kaspa RequestAddresses message that conforms to the Message interface. See MsgRequestAddresses for details.
func (*MsgRequestAddresses) Command ¶
func (msg *MsgRequestAddresses) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgRequestBlockLocator ¶
MsgRequestBlockLocator implements the Message interface and represents a kaspa RequestBlockLocator message. It is used to request a block locator between high and low hash. The locator is returned via a locator message (MsgBlockLocator).
func NewMsgRequestBlockLocator ¶
func NewMsgRequestBlockLocator(highHash, lowHash *daghash.Hash) *MsgRequestBlockLocator
NewMsgRequestBlockLocator returns a new RequestBlockLocator message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
func (*MsgRequestBlockLocator) Command ¶
func (msg *MsgRequestBlockLocator) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgRequestIBDBlocks ¶
MsgRequestIBDBlocks implements the Message interface and represents a kaspa RequestIBDBlocks message. It is used to request a list of blocks starting after the low hash and until the high hash.
func NewMsgRequstIBDBlocks ¶
func NewMsgRequstIBDBlocks(lowHash, highHash *daghash.Hash) *MsgRequestIBDBlocks
NewMsgRequstIBDBlocks returns a new kaspa RequestIBDBlocks message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
func (*MsgRequestIBDBlocks) Command ¶
func (msg *MsgRequestIBDBlocks) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgRequestNextIBDBlocks ¶
type MsgRequestNextIBDBlocks struct{}
MsgRequestNextIBDBlocks implements the Message interface and represents a kaspa RequestNextIBDBlocks message. It is used to notify the IBD syncer peer to send more blocks.
This message has no payload.
func NewMsgRequestNextIBDBlocks ¶
func NewMsgRequestNextIBDBlocks() *MsgRequestNextIBDBlocks
NewMsgRequestNextIBDBlocks returns a new kaspa RequestNextIBDBlocks message that conforms to the Message interface.
func (*MsgRequestNextIBDBlocks) Command ¶
func (msg *MsgRequestNextIBDBlocks) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgRequestRelayBlocks ¶
MsgRequestRelayBlocks implements the Message interface and represents a kaspa RequestRelayBlocks message. It is used to request blocks as part of the block relay protocol.
func NewMsgRequestRelayBlocks ¶
func NewMsgRequestRelayBlocks(hashes []*daghash.Hash) *MsgRequestRelayBlocks
NewMsgRequestRelayBlocks returns a new kaspa RequestRelayBlocks message that conforms to the Message interface. See MsgRequestRelayBlocks for details.
func (*MsgRequestRelayBlocks) Command ¶
func (msg *MsgRequestRelayBlocks) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgRequestSelectedTip ¶
type MsgRequestSelectedTip struct{}
MsgRequestSelectedTip implements the Message interface and represents a kaspa RequestSelectedTip message. It is used to request the selected tip of another peer.
This message has no payload.
func NewMsgRequestSelectedTip ¶
func NewMsgRequestSelectedTip() *MsgRequestSelectedTip
NewMsgRequestSelectedTip returns a new kaspa RequestSelectedTip message that conforms to the Message interface.
func (*MsgRequestSelectedTip) Command ¶
func (msg *MsgRequestSelectedTip) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgRequestTransactions ¶
MsgRequestTransactions implements the Message interface and represents a kaspa RequestTransactions message. It is used to request transactions as part of the transactions relay protocol.
func NewMsgRequestTransactions ¶
func NewMsgRequestTransactions(ids []*daghash.TxID) *MsgRequestTransactions
NewMsgRequestTransactions returns a new kaspa RequestTransactions message that conforms to the Message interface. See MsgRequestTransactions for details.
func (*MsgRequestTransactions) Command ¶
func (msg *MsgRequestTransactions) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgSelectedTip ¶
type MsgSelectedTip struct { // The selected tip hash of the generator of the message. SelectedTipHash *daghash.Hash }
MsgSelectedTip implements the Message interface and represents a kaspa selectedtip message. It is used to answer getseltip messages and tell the asking peer what is the selected tip of this peer.
func NewMsgSelectedTip ¶
func NewMsgSelectedTip(selectedTipHash *daghash.Hash) *MsgSelectedTip
NewMsgSelectedTip returns a new kaspa selectedtip message that conforms to the Message interface.
func (*MsgSelectedTip) Command ¶
func (msg *MsgSelectedTip) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgTransactionNotFound ¶
MsgTransactionNotFound defines a kaspa TransactionNotFound message which is sent in response to a RequestTransactions message if any of the requested data in not available on the peer.
func NewMsgTransactionNotFound ¶
func NewMsgTransactionNotFound(id *daghash.TxID) *MsgTransactionNotFound
NewMsgTransactionNotFound returns a new kaspa transactionsnotfound message that conforms to the Message interface. See MsgTransactionNotFound for details.
func (*MsgTransactionNotFound) Command ¶
func (msg *MsgTransactionNotFound) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgTx ¶
type MsgTx struct { Version int32 TxIn []*TxIn TxOut []*TxOut LockTime uint64 SubnetworkID subnetworkid.SubnetworkID Gas uint64 PayloadHash *daghash.Hash Payload []byte }
MsgTx implements the Message interface and represents a kaspa tx message. It is used to deliver transaction information in response to a getdata message (MsgGetData) for a given transaction.
Use the AddTxIn and AddTxOut functions to build up the list of transaction inputs and outputs.
func NewNativeMsgTx ¶
NewNativeMsgTx returns a new tx message in the native subnetwork
func NewNativeMsgTxWithLocktime ¶
func NewNativeMsgTxWithLocktime(version int32, txIn []*TxIn, txOut []*TxOut, locktime uint64) *MsgTx
NewNativeMsgTxWithLocktime returns a new tx message in the native subnetwork with a locktime.
See newMsgTx for further documntation of the parameters
func NewRegistryMsgTx ¶
NewRegistryMsgTx creates a new MsgTx that registers a new subnetwork
func NewSubnetworkMsgTx ¶
func NewSubnetworkMsgTx(version int32, txIn []*TxIn, txOut []*TxOut, subnetworkID *subnetworkid.SubnetworkID, gas uint64, payload []byte) *MsgTx
NewSubnetworkMsgTx returns a new tx message in the specified subnetwork with specified gas and payload
func (*MsgTx) Command ¶
func (msg *MsgTx) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
func (*MsgTx) Copy ¶
Copy creates a deep copy of a transaction so that the original does not get modified when the copy is manipulated.
func (*MsgTx) Deserialize ¶
Deserialize decodes a transaction from r into the receiver using a format that is suitable for long-term storage such as a database while respecting the Version field in the transaction. This function differs from KaspaDecode in that KaspaDecode decodes from the kaspa domainmessage protocol as it was sent across the network. The domainmessage encoding can technically differ depending on the protocol version and doesn't even really need to match the format of a stored transaction at all. As of the time this comment was written, the encoded transaction is the same in both instances, but there is a distinct difference and separating the two allows the API to be flexible enough to deal with changes.
func (*MsgTx) IsCoinBase ¶
IsCoinBase determines whether or not a transaction is a coinbase transaction. A coinbase transaction is a special transaction created by miners that distributes fees and block subsidy to the previous blocks' miners, and to specify the scriptPubKey that will be used to pay the current miner in future blocks. Each input of the coinbase transaction should set index to maximum value and reference the relevant block id, instead of previous transaction id.
func (*MsgTx) IsSubnetworkCompatible ¶
func (msg *MsgTx) IsSubnetworkCompatible(subnetworkID *subnetworkid.SubnetworkID) bool
IsSubnetworkCompatible return true iff subnetworkID is one or more of the following: 1. The SupportsAll subnetwork (full node) 2. The native subnetwork 3. The transaction's subnetwork
func (*MsgTx) KaspaDecode ¶
KaspaDecode decodes r using the kaspa protocol encoding into the receiver. This is part of the Message interface implementation. See Deserialize for decoding transactions stored to disk, such as in a database, as opposed to decoding transactions from the domainmessage.
func (*MsgTx) KaspaEncode ¶
KaspaEncode encodes the receiver to w using the kaspa protocol encoding. This is part of the Message interface implementation. See Serialize for encoding transactions to be stored to disk, such as in a database, as opposed to encoding transactions for the domainmessage.
func (*MsgTx) MaxPayloadLength ¶
MaxPayloadLength returns the maximum length the payload can be for the receiver. This is part of the Message interface implementation.
func (*MsgTx) ScriptPubKeyLocs ¶
ScriptPubKeyLocs returns a slice containing the start of each public key script within the raw serialized transaction. The caller can easily obtain the length of each script by using len on the script available via the appropriate transaction output entry.
func (*MsgTx) Serialize ¶
Serialize encodes the transaction to w using a format that suitable for long-term storage such as a database while respecting the Version field in the transaction. This function differs from KaspaEncode in that KaspaEncode encodes the transaction to the kaspa domainmessage protocol in order to be sent across the network. The domainmessage encoding can technically differ depending on the protocol version and doesn't even really need to match the format of a stored transaction at all. As of the time this comment was written, the encoded transaction is the same in both instances, but there is a distinct difference and separating the two allows the API to be flexible enough to deal with changes.
func (*MsgTx) SerializeSize ¶
SerializeSize returns the number of bytes it would take to serialize the transaction.
type MsgVerAck ¶
type MsgVerAck struct{}
MsgVerAck defines a kaspa verack message which is used for a peer to acknowledge a version message (MsgVersion) after it has used the information to negotiate parameters. It implements the Message interface.
This message has no payload.
func NewMsgVerAck ¶
func NewMsgVerAck() *MsgVerAck
NewMsgVerAck returns a new kaspa verack message that conforms to the Message interface.
func (*MsgVerAck) Command ¶
func (msg *MsgVerAck) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
type MsgVersion ¶
type MsgVersion struct { // Version of the protocol the node is using. ProtocolVersion uint32 // Bitfield which identifies the enabled services. Services ServiceFlag // Time the message was generated. This is encoded as an int64 on the domainmessage. Timestamp mstime.Time // Address of the local peer. Address *NetAddress // The peer unique ID ID *id.ID // The user agent that generated messsage. This is a encoded as a varString // on the domainmessage. This has a max length of MaxUserAgentLen. UserAgent string // The selected tip hash of the generator of the version message. SelectedTipHash *daghash.Hash // Don't announce transactions to peer. DisableRelayTx bool // The subnetwork of the generator of the version message. Should be nil in full nodes SubnetworkID *subnetworkid.SubnetworkID }
MsgVersion implements the Message interface and represents a kaspa version message. It is used for a peer to advertise itself as soon as an outbound connection is made. The remote peer then uses this information along with its own to negotiate. The remote peer must then respond with a version message of its own containing the negotiated values followed by a verack message (MsgVerAck). This exchange must take place before any further communication is allowed to proceed.
func NewMsgVersion ¶
func NewMsgVersion(addr *NetAddress, id *id.ID, selectedTipHash *daghash.Hash, subnetworkID *subnetworkid.SubnetworkID) *MsgVersion
NewMsgVersion returns a new kaspa version message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
func (*MsgVersion) AddService ¶
func (msg *MsgVersion) AddService(service ServiceFlag)
AddService adds service as a supported service by the peer generating the message.
func (*MsgVersion) AddUserAgent ¶
func (msg *MsgVersion) AddUserAgent(name string, version string, comments ...string)
AddUserAgent adds a user agent to the user agent string for the version message. The version string is not defined to any strict format, although it is recommended to use the form "major.minor.revision" e.g. "2.6.41".
func (*MsgVersion) Command ¶
func (msg *MsgVersion) Command() MessageCommand
Command returns the protocol command string for the message. This is part of the Message interface implementation.
func (*MsgVersion) HasService ¶
func (msg *MsgVersion) HasService(service ServiceFlag) bool
HasService returns whether the specified service is supported by the peer that generated the message.
type NetAddress ¶
type NetAddress struct { // Last time the address was seen. Timestamp mstime.Time // Bitfield which identifies the services supported by the address. Services ServiceFlag // IP address of the peer. IP net.IP // Port the peer is using. This is encoded in big endian on the domainmessage // which differs from most everything else. 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 ServiceFlag) *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 ServiceFlag) *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 mstime.Time, services ServiceFlag, 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 millisecond precision.
func (*NetAddress) AddService ¶
func (na *NetAddress) AddService(service ServiceFlag)
AddService adds service as a supported service by the peer generating the message.
func (*NetAddress) HasService ¶
func (na *NetAddress) HasService(service ServiceFlag) bool
HasService returns whether the specified service is supported by the address.
func (*NetAddress) TCPAddress ¶
func (na *NetAddress) TCPAddress() *net.TCPAddr
TCPAddress converts the NetAddress to *net.TCPAddr
type Outpoint ¶
Outpoint defines a kaspa data type that is used to track previous transaction outputs.
func NewOutpoint ¶
NewOutpoint returns a new kaspa transaction outpoint point with the provided hash and index.
type ServiceFlag ¶
type ServiceFlag uint64
ServiceFlag identifies services supported by a kaspa peer.
const ( // SFNodeNetwork is a flag used to indicate a peer is a full node. SFNodeNetwork ServiceFlag = 1 << iota // SFNodeGetUTXO is a flag used to indicate a peer supports the // getutxos and utxos commands (BIP0064). SFNodeGetUTXO // SFNodeBloom is a flag used to indicate a peer supports bloom // filtering. SFNodeBloom // SFNodeXthin is a flag used to indicate a peer supports xthin blocks. SFNodeXthin // SFNodeBit5 is a flag used to indicate a peer supports a service // defined by bit 5. SFNodeBit5 // SFNodeCF is a flag used to indicate a peer supports committed // filters (CFs). SFNodeCF )
func (ServiceFlag) String ¶
func (f ServiceFlag) String() string
String returns the ServiceFlag in human-readable form.
type TxIn ¶
TxIn defines a kaspa transaction input.
func NewTxIn ¶
NewTxIn returns a new kaspa transaction input with the provided previous outpoint point and signature script with a default sequence of MaxTxInSequenceNum.
func (*TxIn) SerializeSize ¶
SerializeSize returns the number of bytes it would take to serialize the the transaction input.
type TxLoc ¶
TxLoc holds locator data for the offset and length of where a transaction is located within a MsgBlock data buffer.
type TxOut ¶
TxOut defines a kaspa transaction output.
func NewTxOut ¶
NewTxOut returns a new kaspa transaction output with the provided transaction value and public key script.
func (*TxOut) SerializeSize ¶
SerializeSize returns the number of bytes it would take to serialize the the transaction output.
Source Files ¶
- blockheader.go
- common.go
- doc.go
- error.go
- message.go
- msgaddresses.go
- msgblock.go
- msgblocklocator.go
- msgdoneibdblocks.go
- msgibdblock.go
- msginvrelayblock.go
- msginvtransaction.go
- msgping.go
- msgpong.go
- msgrequestaddresses.go
- msgrequestblocklocator.go
- msgrequestibdblocks.go
- msgrequestnextibdblocks.go
- msgrequestrelayblocks.go
- msgrequestselectedtip.go
- msgrequesttransactions.go
- msgselectedtip.go
- msgtransactionnotfound.go
- msgtx.go
- msgverack.go
- msgversion.go
- netaddress.go
- protocol.go