mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-510] Change coinbase flags to kaspad. * [NOD-510] Removed superfluous spaces after periods in comments. * [NOD-510] Rename btcd -> kaspad in the root folder. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Continue renaming btcd -> kaspad. * [NOD-510] Rename btcjson -> kaspajson. * [NOD-510] Rename file names inside kaspajson. * [NOD-510] Rename kaspajson -> jsonrpc. * [NOD-510] Finish renaming in addrmgr. * [NOD-510] Rename package btcec to ecc. * [NOD-510] Finish renaming stuff in blockdag. * [NOD-510] Rename stuff in cmd. * [NOD-510] Rename stuff in config. * [NOD-510] Rename stuff in connmgr. * [NOD-510] Rename stuff in dagconfig. * [NOD-510] Rename stuff in database. * [NOD-510] Rename stuff in docker. * [NOD-510] Rename stuff in integration. * [NOD-510] Rename jsonrpc to rpcmodel. * [NOD-510] Rename stuff in limits. * [NOD-510] Rename stuff in logger. * [NOD-510] Rename stuff in mempool. * [NOD-510] Rename stuff in mining. * [NOD-510] Rename stuff in netsync. * [NOD-510] Rename stuff in peer. * [NOD-510] Rename stuff in release. * [NOD-510] Rename stuff in rpcclient. * [NOD-510] Rename stuff in server. * [NOD-510] Rename stuff in signal. * [NOD-510] Rename stuff in txscript. * [NOD-510] Rename stuff in util. * [NOD-510] Rename stuff in wire. * [NOD-510] Fix failing tests. * [NOD-510] Fix merge errors. * [NOD-510] Fix go vet errors. * [NOD-510] Remove merged file that's no longer relevant. * [NOD-510] Add a comment above Op0. * [NOD-510] Fix some comments referencing Bitcoin Core. * [NOD-510] Fix some more comments referencing Bitcoin Core. * [NOD-510] Fix bitcoin -> kaspa. * [NOD-510] Fix more bitcoin -> kaspa. * [NOD-510] Fix comments, remove DisconnectBlock in addrindex. * [NOD-510] Rename KSPD to KASD. * [NOD-510] Fix comments and user agent.
137 lines
4.0 KiB
Go
137 lines
4.0 KiB
Go
// Copyright (c) 2013-2016 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package wire
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
// MaxBlockHeadersPerMsg is the maximum number of block headers that can be in
|
|
// a single kaspa headers message.
|
|
const MaxBlockHeadersPerMsg = 2000
|
|
|
|
// MsgHeaders implements the Message interface and represents a kaspa headers
|
|
// message. It is used to deliver block header information in response
|
|
// to a getheaders message (MsgGetHeaders). The maximum number of block headers
|
|
// per message is currently 2000. See MsgGetHeaders for details on requesting
|
|
// the headers.
|
|
type MsgHeaders struct {
|
|
Headers []*BlockHeader
|
|
}
|
|
|
|
// AddBlockHeader adds a new block header to the message.
|
|
func (msg *MsgHeaders) AddBlockHeader(bh *BlockHeader) error {
|
|
if len(msg.Headers)+1 > MaxBlockHeadersPerMsg {
|
|
str := fmt.Sprintf("too many block headers in message [max %d]",
|
|
MaxBlockHeadersPerMsg)
|
|
return messageError("MsgHeaders.AddBlockHeader", str)
|
|
}
|
|
|
|
msg.Headers = append(msg.Headers, bh)
|
|
return nil
|
|
}
|
|
|
|
// KaspaDecode decodes r using the kaspa protocol encoding into the receiver.
|
|
// This is part of the Message interface implementation.
|
|
func (msg *MsgHeaders) KaspaDecode(r io.Reader, pver uint32) error {
|
|
count, err := ReadVarInt(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Limit to max block headers per message.
|
|
if count > MaxBlockHeadersPerMsg {
|
|
str := fmt.Sprintf("too many block headers for message "+
|
|
"[count %d, max %d]", count, MaxBlockHeadersPerMsg)
|
|
return messageError("MsgHeaders.KaspaDecode", str)
|
|
}
|
|
|
|
// Create a contiguous slice of headers to deserialize into in order to
|
|
// reduce the number of allocations.
|
|
headers := make([]BlockHeader, count)
|
|
msg.Headers = make([]*BlockHeader, 0, count)
|
|
for i := uint64(0); i < count; i++ {
|
|
bh := &headers[i]
|
|
err := readBlockHeader(r, pver, bh)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
txCount, err := ReadVarInt(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Ensure the transaction count is zero for headers.
|
|
if txCount > 0 {
|
|
str := fmt.Sprintf("block headers may not contain "+
|
|
"transactions [count %d]", txCount)
|
|
return messageError("MsgHeaders.KaspaDecode", str)
|
|
}
|
|
msg.AddBlockHeader(bh)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// KaspaEncode encodes the receiver to w using the kaspa protocol encoding.
|
|
// This is part of the Message interface implementation.
|
|
func (msg *MsgHeaders) KaspaEncode(w io.Writer, pver uint32) error {
|
|
// Limit to max block headers per message.
|
|
count := len(msg.Headers)
|
|
if count > MaxBlockHeadersPerMsg {
|
|
str := fmt.Sprintf("too many block headers for message "+
|
|
"[count %d, max %d]", count, MaxBlockHeadersPerMsg)
|
|
return messageError("MsgHeaders.KaspaEncode", str)
|
|
}
|
|
|
|
err := WriteVarInt(w, uint64(count))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, bh := range msg.Headers {
|
|
err := writeBlockHeader(w, pver, bh)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// The wire protocol encoding always includes a 0 for the number
|
|
// of transactions on header messages. This is really just an
|
|
// artifact of the way the original implementation serializes
|
|
// block headers, but it is required.
|
|
err = WriteVarInt(w, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Command returns the protocol command string for the message. This is part
|
|
// of the Message interface implementation.
|
|
func (msg *MsgHeaders) Command() string {
|
|
return CmdHeaders
|
|
}
|
|
|
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
// receiver. This is part of the Message interface implementation.
|
|
func (msg *MsgHeaders) MaxPayloadLength(pver uint32) uint32 {
|
|
// Num headers (varInt) + max allowed headers (header length + 1 byte
|
|
// for the number of transactions which is always 0).
|
|
return MaxVarIntPayload + ((MaxBlockHeaderPayload + 1) *
|
|
MaxBlockHeadersPerMsg)
|
|
}
|
|
|
|
// NewMsgHeaders returns a new kaspa headers message that conforms to the
|
|
// Message interface. See MsgHeaders for details.
|
|
func NewMsgHeaders() *MsgHeaders {
|
|
return &MsgHeaders{
|
|
Headers: make([]*BlockHeader, 0, MaxBlockHeadersPerMsg),
|
|
}
|
|
}
|