mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-03-13 11:55:05 +00:00
BIP0144+wire: add a MessageEncoding variant for serialization/deserialization
This commit modifies the existing wire.Message interface to introduce a new MessageEncoding variant which dictates the exact encoding to be used when serializing and deserializing messages. Such an option is now necessary due to the segwit soft-fork package, as btcd will need to be able to optionally encode transactions/blocks without witness data to un-upgraded peers. Two new functions have been introduced: ReadMessageWithEncodingN and WriteMessageWithEncodingN which wrap BtcDecode/BtcEncode with the desired encoding format.
This commit is contained in:
committed by
Dave Collins
parent
1b359e1131
commit
48abfdf87c
@@ -53,13 +53,31 @@ const (
|
||||
CmdFeeFilter = "feefilter"
|
||||
)
|
||||
|
||||
// WireEncoding represents the wire message encoding format to be used.
|
||||
type MessageEncoding uint32
|
||||
|
||||
const (
|
||||
// BaseEncoding encodes all messages in the default format specified
|
||||
// for the Bitcoin wire protocol.
|
||||
BaseEncoding MessageEncoding = 1 << iota
|
||||
|
||||
// WitnessEncoding encodes all messages other than transaction messages
|
||||
// using the default Bitcoin wire protocol specification. For transaction
|
||||
// messages, the new encoding format detailed in BIP0144 will be used.
|
||||
WitnessEncoding
|
||||
)
|
||||
|
||||
// LatestEncoding is the most recently specified encoding for the Bitcoin wire
|
||||
// protocol.
|
||||
var LatestEncoding = WitnessEncoding
|
||||
|
||||
// Message is an interface that describes a bitcoin 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 Message interface {
|
||||
BtcDecode(io.Reader, uint32) error
|
||||
BtcEncode(io.Writer, uint32) error
|
||||
BtcDecode(io.Reader, uint32, MessageEncoding) error
|
||||
BtcEncode(io.Writer, uint32, MessageEncoding) error
|
||||
Command() string
|
||||
MaxPayloadLength(uint32) uint32
|
||||
}
|
||||
@@ -200,6 +218,27 @@ func discardInput(r io.Reader, n uint32) {
|
||||
// information and returns the number of bytes written. This function is the
|
||||
// same as WriteMessage except it also returns the number of bytes written.
|
||||
func WriteMessageN(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) (int, error) {
|
||||
return WriteMessageWithEncodingN(w, msg, pver, btcnet, BaseEncoding)
|
||||
}
|
||||
|
||||
// WriteMessage writes a bitcoin Message to w including the necessary header
|
||||
// information. This function is the same as WriteMessageN except it doesn't
|
||||
// doesn't return the number of bytes written. This function is mainly provided
|
||||
// for backwards compatibility with the original API, but it's also useful for
|
||||
// callers that don't care about byte counts.
|
||||
func WriteMessage(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) error {
|
||||
_, err := WriteMessageN(w, msg, pver, btcnet)
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteMessageWithEncodingN writes a bitcoin Message to w including the
|
||||
// necessary header information and returns the number of bytes written.
|
||||
// This function is the same as WriteMessageN except it also allows the caller
|
||||
// to specify the message encoding format to be used when serializing wire
|
||||
// messages.
|
||||
func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
|
||||
btcnet BitcoinNet, encoding MessageEncoding) (int, error) {
|
||||
|
||||
totalBytes := 0
|
||||
|
||||
// Enforce max command size.
|
||||
@@ -214,7 +253,7 @@ func WriteMessageN(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) (in
|
||||
|
||||
// Encode the message payload.
|
||||
var bw bytes.Buffer
|
||||
err := msg.BtcEncode(&bw, pver)
|
||||
err := msg.BtcEncode(&bw, pver, encoding)
|
||||
if err != nil {
|
||||
return totalBytes, err
|
||||
}
|
||||
@@ -264,22 +303,15 @@ func WriteMessageN(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) (in
|
||||
return totalBytes, err
|
||||
}
|
||||
|
||||
// WriteMessage writes a bitcoin Message to w including the necessary header
|
||||
// information. This function is the same as WriteMessageN except it doesn't
|
||||
// doesn't return the number of bytes written. This function is mainly provided
|
||||
// for backwards compatibility with the original API, but it's also useful for
|
||||
// callers that don't care about byte counts.
|
||||
func WriteMessage(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) error {
|
||||
_, err := WriteMessageN(w, msg, pver, btcnet)
|
||||
return err
|
||||
}
|
||||
// ReadMessageWithEncodingN reads, validates, and parses the next bitcoin Message
|
||||
// from r for the provided protocol version and bitcoin network. It returns the
|
||||
// number of bytes read in addition to the parsed Message and raw bytes which
|
||||
// comprise the message. This function is the same as ReadMessageN except it
|
||||
// allows the caller to specify which message encoding is to to consult when
|
||||
// decoding wire messages.
|
||||
func ReadMessageWithEncodingN(r io.Reader, pver uint32, btcnet BitcoinNet,
|
||||
enc MessageEncoding) (int, Message, []byte, error) {
|
||||
|
||||
// ReadMessageN reads, validates, and parses the next bitcoin Message from r for
|
||||
// the provided protocol version and bitcoin network. It returns the number of
|
||||
// bytes read in addition to the parsed Message and raw bytes which comprise the
|
||||
// message. This function is the same as ReadMessage except it also returns the
|
||||
// number of bytes read.
|
||||
func ReadMessageN(r io.Reader, pver uint32, btcnet BitcoinNet) (int, Message, []byte, error) {
|
||||
totalBytes := 0
|
||||
n, hdr, err := readMessageHeader(r)
|
||||
totalBytes += n
|
||||
@@ -351,7 +383,7 @@ func ReadMessageN(r io.Reader, pver uint32, btcnet BitcoinNet) (int, Message, []
|
||||
// Unmarshal message. NOTE: This must be a *bytes.Buffer since the
|
||||
// MsgVersion BtcDecode function requires it.
|
||||
pr := bytes.NewBuffer(payload)
|
||||
err = msg.BtcDecode(pr, pver)
|
||||
err = msg.BtcDecode(pr, pver, enc)
|
||||
if err != nil {
|
||||
return totalBytes, nil, nil, err
|
||||
}
|
||||
@@ -359,6 +391,15 @@ func ReadMessageN(r io.Reader, pver uint32, btcnet BitcoinNet) (int, Message, []
|
||||
return totalBytes, msg, payload, nil
|
||||
}
|
||||
|
||||
// ReadMessageN reads, validates, and parses the next bitcoin Message from r for
|
||||
// the provided protocol version and bitcoin network. It returns the number of
|
||||
// bytes read in addition to the parsed Message and raw bytes which comprise the
|
||||
// message. This function is the same as ReadMessage except it also returns the
|
||||
// number of bytes read.
|
||||
func ReadMessageN(r io.Reader, pver uint32, btcnet BitcoinNet) (int, Message, []byte, error) {
|
||||
return ReadMessageWithEncodingN(r, pver, btcnet, BaseEncoding)
|
||||
}
|
||||
|
||||
// ReadMessage reads, validates, and parses the next bitcoin Message from r for
|
||||
// the provided protocol version and bitcoin network. It returns the parsed
|
||||
// Message and raw bytes which comprise the message. This function only differs
|
||||
|
||||
Reference in New Issue
Block a user