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

@@ -50,6 +50,7 @@ func TestPing(t *testing.T) {
func TestPingBIP0031(t *testing.T) {
// Use the protocol version just prior to BIP0031Version changes.
pver := BIP0031Version
enc := BaseEncoding
nonce, err := RandomUint64()
if err != nil {
@@ -72,14 +73,14 @@ func TestPingBIP0031(t *testing.T) {
// Test encode with old protocol version.
var buf bytes.Buffer
err = msg.BtcEncode(&buf, pver)
err = msg.BtcEncode(&buf, pver, enc)
if err != nil {
t.Errorf("encode of MsgPing failed %v err <%v>", msg, err)
}
// Test decode with old protocol version.
readmsg := NewMsgPing(0)
err = readmsg.BtcDecode(&buf, pver)
err = readmsg.BtcDecode(&buf, pver, enc)
if err != nil {
t.Errorf("decode of MsgPing failed [%v] err <%v>", buf, err)
}
@@ -106,14 +107,14 @@ func TestPingCrossProtocol(t *testing.T) {
// Encode with latest protocol version.
var buf bytes.Buffer
err = msg.BtcEncode(&buf, ProtocolVersion)
err = msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
if err != nil {
t.Errorf("encode of MsgPing failed %v err <%v>", msg, err)
}
// Decode with old protocol version.
readmsg := NewMsgPing(0)
err = readmsg.BtcDecode(&buf, BIP0031Version)
err = readmsg.BtcDecode(&buf, BIP0031Version, BaseEncoding)
if err != nil {
t.Errorf("decode of MsgPing failed [%v] err <%v>", buf, err)
}
@@ -129,10 +130,11 @@ func TestPingCrossProtocol(t *testing.T) {
// versions.
func TestPingWire(t *testing.T) {
tests := []struct {
in MsgPing // Message to encode
out MsgPing // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
in MsgPing // Message to encode
out MsgPing // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
enc MessageEncoding // Message encoding format
}{
// Latest protocol version.
{
@@ -140,6 +142,7 @@ func TestPingWire(t *testing.T) {
MsgPing{Nonce: 123123}, // 0x1e0f3
[]byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
ProtocolVersion,
BaseEncoding,
},
// Protocol version BIP0031Version+1
@@ -148,6 +151,7 @@ func TestPingWire(t *testing.T) {
MsgPing{Nonce: 456456}, // 0x6f708
[]byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
BIP0031Version + 1,
BaseEncoding,
},
// Protocol version BIP0031Version
@@ -156,6 +160,7 @@ func TestPingWire(t *testing.T) {
MsgPing{Nonce: 0}, // No nonce for pver
[]byte{}, // No nonce for pver
BIP0031Version,
BaseEncoding,
},
}
@@ -163,7 +168,7 @@ func TestPingWire(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
@@ -177,7 +182,7 @@ func TestPingWire(t *testing.T) {
// Decode the message from wire format.
var msg MsgPing
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
@@ -196,18 +201,20 @@ func TestPingWireErrors(t *testing.T) {
pver := ProtocolVersion
tests := []struct {
in *MsgPing // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
readErr error // Expected read error
in *MsgPing // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
enc MessageEncoding // Message encoding format
max int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
readErr error // Expected read error
}{
// Latest protocol version with intentional read/write errors.
{
&MsgPing{Nonce: 123123}, // 0x1e0f3
[]byte{0xf3, 0xe0, 0x01, 0x00},
pver,
BaseEncoding,
2,
io.ErrShortWrite,
io.ErrUnexpectedEOF,
@@ -218,7 +225,7 @@ func TestPingWireErrors(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
w := newFixedWriter(test.max)
err := test.in.BtcEncode(w, test.pver)
err := test.in.BtcEncode(w, test.pver, test.enc)
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
@@ -228,7 +235,7 @@ func TestPingWireErrors(t *testing.T) {
// Decode from wire format.
var msg MsgPing
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
err = msg.BtcDecode(r, test.pver, test.enc)
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
i, err, test.readErr)