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:
Olaoluwa Osuntokun
2016-10-18 16:21:48 -07:00
committed by Dave Collins
parent 1b359e1131
commit 48abfdf87c
51 changed files with 862 additions and 515 deletions

View File

@@ -41,10 +41,11 @@ func TestVerAckWire(t *testing.T) {
msgVerAckEncoded := []byte{}
tests := []struct {
in *MsgVerAck // Message to encode
out *MsgVerAck // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
in *MsgVerAck // Message to encode
out *MsgVerAck // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
enc MessageEncoding // Message encoding format
}{
// Latest protocol version.
{
@@ -52,6 +53,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAckEncoded,
ProtocolVersion,
BaseEncoding,
},
// Protocol version BIP0035Version.
@@ -60,6 +62,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAckEncoded,
BIP0035Version,
BaseEncoding,
},
// Protocol version BIP0031Version.
@@ -68,6 +71,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAckEncoded,
BIP0031Version,
BaseEncoding,
},
// Protocol version NetAddressTimeVersion.
@@ -76,6 +80,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAckEncoded,
NetAddressTimeVersion,
BaseEncoding,
},
// Protocol version MultipleAddressVersion.
@@ -84,6 +89,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAckEncoded,
MultipleAddressVersion,
BaseEncoding,
},
}
@@ -91,7 +97,7 @@ func TestVerAckWire(t *testing.T) {
for i, test := range tests {
// Encode the message to wire format.
var buf bytes.Buffer
err := test.in.BtcEncode(&buf, test.pver)
err := test.in.BtcEncode(&buf, test.pver, test.enc)
if err != nil {
t.Errorf("BtcEncode #%d error %v", i, err)
continue
@@ -105,7 +111,7 @@ func TestVerAckWire(t *testing.T) {
// Decode the message from wire format.
var msg MsgVerAck
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
err = msg.BtcDecode(rbuf, test.pver, test.enc)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)
continue