mirror of
https://github.com/btcsuite/btcd.git
synced 2026-08-19 13:17:36 +02: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:
parent
1b359e1131
commit
48abfdf87c
51 changed files with 862 additions and 515 deletions
|
|
@ -104,10 +104,11 @@ func TestNotFoundWire(t *testing.T) {
|
|||
}
|
||||
|
||||
tests := []struct {
|
||||
in *MsgNotFound // Message to encode
|
||||
out *MsgNotFound // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *MsgNotFound // Message to encode
|
||||
out *MsgNotFound // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
enc MessageEncoding // Message encoding format
|
||||
}{
|
||||
// Latest protocol version with no inv vectors.
|
||||
{
|
||||
|
|
@ -115,6 +116,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
NoInv,
|
||||
NoInvEncoded,
|
||||
ProtocolVersion,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Latest protocol version with multiple inv vectors.
|
||||
|
|
@ -123,6 +125,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
ProtocolVersion,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version no inv vectors.
|
||||
|
|
@ -131,6 +134,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
NoInv,
|
||||
NoInvEncoded,
|
||||
BIP0035Version,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with multiple inv vectors.
|
||||
|
|
@ -139,6 +143,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
BIP0035Version,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version no inv vectors.
|
||||
|
|
@ -147,6 +152,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
NoInv,
|
||||
NoInvEncoded,
|
||||
BIP0031Version,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with multiple inv vectors.
|
||||
|
|
@ -155,6 +161,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
BIP0031Version,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion no inv vectors.
|
||||
|
|
@ -163,6 +170,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
NoInv,
|
||||
NoInvEncoded,
|
||||
NetAddressTimeVersion,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with multiple inv vectors.
|
||||
|
|
@ -171,6 +179,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
NetAddressTimeVersion,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion no inv vectors.
|
||||
|
|
@ -179,6 +188,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
NoInv,
|
||||
NoInvEncoded,
|
||||
MultipleAddressVersion,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with multiple inv vectors.
|
||||
|
|
@ -187,6 +197,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
MultipleAddressVersion,
|
||||
BaseEncoding,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -194,7 +205,7 @@ func TestNotFoundWire(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
|
||||
|
|
@ -208,7 +219,7 @@ func TestNotFoundWire(t *testing.T) {
|
|||
// Decode the message from wire format.
|
||||
var msg MsgNotFound
|
||||
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
|
||||
|
|
@ -260,26 +271,27 @@ func TestNotFoundWireErrors(t *testing.T) {
|
|||
}
|
||||
|
||||
tests := []struct {
|
||||
in *MsgNotFound // 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 *MsgNotFound // 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
|
||||
}{
|
||||
// Force error in inventory vector count
|
||||
{baseNotFound, baseNotFoundEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
{baseNotFound, baseNotFoundEncoded, pver, BaseEncoding, 0, io.ErrShortWrite, io.EOF},
|
||||
// Force error in inventory list.
|
||||
{baseNotFound, baseNotFoundEncoded, pver, 1, io.ErrShortWrite, io.EOF},
|
||||
{baseNotFound, baseNotFoundEncoded, pver, BaseEncoding, 1, io.ErrShortWrite, io.EOF},
|
||||
// Force error with greater than max inventory vectors.
|
||||
{maxNotFound, maxNotFoundEncoded, pver, 3, wireErr, wireErr},
|
||||
{maxNotFound, maxNotFoundEncoded, pver, BaseEncoding, 3, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
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 reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
|
||||
i, err, test.writeErr)
|
||||
|
|
@ -299,7 +311,7 @@ func TestNotFoundWireErrors(t *testing.T) {
|
|||
// Decode from wire format.
|
||||
var msg MsgNotFound
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
err = msg.BtcDecode(r, test.pver, test.enc)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
|
||||
i, err, test.readErr)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue