btcd/wire/msgmempool_test.go
Olaoluwa Osuntokun 48abfdf87c 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.
2017-08-13 23:17:40 -05:00

64 lines
1.7 KiB
Go

// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire
import (
"bytes"
"testing"
)
func TestMemPool(t *testing.T) {
pver := ProtocolVersion
enc := BaseEncoding
// Ensure the command is expected value.
wantCmd := "mempool"
msg := NewMsgMemPool()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgMemPool: wrong command - got %v want %v",
cmd, wantCmd)
}
// Ensure max payload is expected value.
wantPayload := uint32(0)
maxPayload := msg.MaxPayloadLength(pver)
if maxPayload != wantPayload {
t.Errorf("MaxPayloadLength: wrong max payload length for "+
"protocol version %d - got %v, want %v", pver,
maxPayload, wantPayload)
}
// Test encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, pver, enc)
if err != nil {
t.Errorf("encode of MsgMemPool failed %v err <%v>", msg, err)
}
// Older protocol versions should fail encode since message didn't
// exist yet.
oldPver := BIP0035Version - 1
err = msg.BtcEncode(&buf, oldPver, enc)
if err == nil {
s := "encode of MsgMemPool passed for old protocol version %v err <%v>"
t.Errorf(s, msg, err)
}
// Test decode with latest protocol version.
readmsg := NewMsgMemPool()
err = readmsg.BtcDecode(&buf, pver, enc)
if err != nil {
t.Errorf("decode of MsgMemPool failed [%v] err <%v>", buf, err)
}
// Older protocol versions should fail decode since message didn't
// exist yet.
err = readmsg.BtcDecode(&buf, oldPver, enc)
if err == nil {
s := "decode of MsgMemPool passed for old protocol version %v err <%v>"
t.Errorf(s, msg, err)
}
}