mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-03-14 20:35:18 +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
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
// TestPongLatest tests the MsgPong API against the latest protocol version.
|
||||
func TestPongLatest(t *testing.T) {
|
||||
enc := BaseEncoding
|
||||
pver := ProtocolVersion
|
||||
|
||||
nonce, err := RandomUint64()
|
||||
@@ -45,14 +46,14 @@ func TestPongLatest(t *testing.T) {
|
||||
|
||||
// Test encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err = msg.BtcEncode(&buf, pver)
|
||||
err = msg.BtcEncode(&buf, pver, enc)
|
||||
if err != nil {
|
||||
t.Errorf("encode of MsgPong failed %v err <%v>", msg, err)
|
||||
}
|
||||
|
||||
// Test decode with latest protocol version.
|
||||
readmsg := NewMsgPong(0)
|
||||
err = readmsg.BtcDecode(&buf, pver)
|
||||
err = readmsg.BtcDecode(&buf, pver, enc)
|
||||
if err != nil {
|
||||
t.Errorf("decode of MsgPong failed [%v] err <%v>", buf, err)
|
||||
}
|
||||
@@ -68,6 +69,7 @@ func TestPongLatest(t *testing.T) {
|
||||
func TestPongBIP0031(t *testing.T) {
|
||||
// Use the protocol version just prior to BIP0031Version changes.
|
||||
pver := BIP0031Version
|
||||
enc := BaseEncoding
|
||||
|
||||
nonce, err := RandomUint64()
|
||||
if err != nil {
|
||||
@@ -87,7 +89,7 @@ func TestPongBIP0031(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 MsgPong succeeded when it shouldn't have %v",
|
||||
msg)
|
||||
@@ -95,7 +97,7 @@ func TestPongBIP0031(t *testing.T) {
|
||||
|
||||
// Test decode with old protocol version.
|
||||
readmsg := NewMsgPong(0)
|
||||
err = readmsg.BtcDecode(&buf, pver)
|
||||
err = readmsg.BtcDecode(&buf, pver, enc)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgPong succeeded when it shouldn't have %v",
|
||||
spew.Sdump(buf))
|
||||
@@ -122,14 +124,14 @@ func TestPongCrossProtocol(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 MsgPong failed %v err <%v>", msg, err)
|
||||
}
|
||||
|
||||
// Decode with old protocol version.
|
||||
readmsg := NewMsgPong(0)
|
||||
err = readmsg.BtcDecode(&buf, BIP0031Version)
|
||||
err = readmsg.BtcDecode(&buf, BIP0031Version, BaseEncoding)
|
||||
if err == nil {
|
||||
t.Errorf("encode of MsgPong succeeded when it shouldn't have %v",
|
||||
msg)
|
||||
@@ -146,10 +148,11 @@ func TestPongCrossProtocol(t *testing.T) {
|
||||
// versions.
|
||||
func TestPongWire(t *testing.T) {
|
||||
tests := []struct {
|
||||
in MsgPong // Message to encode
|
||||
out MsgPong // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in MsgPong // Message to encode
|
||||
out MsgPong // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
enc MessageEncoding // Message encoding format
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
@@ -157,6 +160,7 @@ func TestPongWire(t *testing.T) {
|
||||
MsgPong{Nonce: 123123}, // 0x1e0f3
|
||||
[]byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
ProtocolVersion,
|
||||
BaseEncoding,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version+1
|
||||
@@ -165,6 +169,7 @@ func TestPongWire(t *testing.T) {
|
||||
MsgPong{Nonce: 456456}, // 0x6f708
|
||||
[]byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
BIP0031Version + 1,
|
||||
BaseEncoding,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -172,7 +177,7 @@ func TestPongWire(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
|
||||
@@ -186,7 +191,7 @@ func TestPongWire(t *testing.T) {
|
||||
// Decode the message from wire format.
|
||||
var msg MsgPong
|
||||
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
|
||||
@@ -212,25 +217,26 @@ func TestPongWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *MsgPong // 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 *MsgPong // 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.
|
||||
// Force error in nonce.
|
||||
{basePong, basePongEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
{basePong, basePongEncoded, pver, BaseEncoding, 0, io.ErrShortWrite, io.EOF},
|
||||
// Force error due to unsupported protocol version.
|
||||
{basePong, basePongEncoded, pverNoPong, 4, wireErr, wireErr},
|
||||
{basePong, basePongEncoded, pverNoPong, BaseEncoding, 4, 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)
|
||||
@@ -250,7 +256,7 @@ func TestPongWireErrors(t *testing.T) {
|
||||
// Decode from wire format.
|
||||
var msg MsgPong
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user