mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-652] Add selectedTip and getSelectedTip messages * [NOD-652] Remove peerSyncState.isSelectedTipKnown * [NOD-652] Do nothing on OnSelectedTip if the peer selected tip hasn't changed * [NOD-652] Handle selected tip message with block handler * [NOD-652] Add comments * [NOD-652] go mod tidy * [NOD-652] Fix TestVersion * [NOD-652] Use dag.AdjustedTime instead of dag.timeSource.AdjustedTime * [NOD-652] Create shouldQueryPeerSelectedTips and queueMsgGetSelectedTip functions * [NOD-652] Change selectedTip to selectedTipHash where needed * [NOD-652] add minDAGTimeDelay constant * [NOD-652] add comments * [NOD-652] Fix names and comments * [NOD-652] Put msg.reply push in the right place * [NOD-652] Fix comments and names
88 lines
2.2 KiB
Go
88 lines
2.2 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"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
)
|
|
|
|
// TestGetSelectedTip tests the MsgGetSelectedTip API.
|
|
func TestGetSelectedTip(t *testing.T) {
|
|
pver := ProtocolVersion
|
|
|
|
// Ensure the command is expected value.
|
|
wantCmd := "getseltip"
|
|
msg := NewMsgGetSelectedTip()
|
|
if cmd := msg.Command(); cmd != wantCmd {
|
|
t.Errorf("NewMsgGetSelectedTip: 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)
|
|
}
|
|
}
|
|
|
|
// TestGetSelectedTipWire tests the MsgGetSelectedTip wire encode and decode for various
|
|
// protocol versions.
|
|
func TestGetSelectedTipWire(t *testing.T) {
|
|
msgGetSelectedTip := NewMsgGetSelectedTip()
|
|
msgGetSelectedTipEncoded := []byte{}
|
|
|
|
tests := []struct {
|
|
in *MsgGetSelectedTip // Message to encode
|
|
out *MsgGetSelectedTip // Expected decoded message
|
|
buf []byte // Wire encoding
|
|
pver uint32 // Protocol version for wire encoding
|
|
}{
|
|
// Latest protocol version.
|
|
{
|
|
msgGetSelectedTip,
|
|
msgGetSelectedTip,
|
|
msgGetSelectedTipEncoded,
|
|
ProtocolVersion,
|
|
},
|
|
}
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
for i, test := range tests {
|
|
// Encode the message to wire format.
|
|
var buf bytes.Buffer
|
|
err := test.in.KaspaEncode(&buf, test.pver)
|
|
if err != nil {
|
|
t.Errorf("KaspaEncode #%d error %v", i, err)
|
|
continue
|
|
}
|
|
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
t.Errorf("KaspaEncode #%d\n got: %s want: %s", i,
|
|
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
continue
|
|
}
|
|
|
|
// Decode the message from wire format.
|
|
var msg MsgGetSelectedTip
|
|
rbuf := bytes.NewReader(test.buf)
|
|
err = msg.KaspaDecode(rbuf, test.pver)
|
|
if err != nil {
|
|
t.Errorf("KaspaDecode #%d error %v", i, err)
|
|
continue
|
|
}
|
|
if !reflect.DeepEqual(&msg, test.out) {
|
|
t.Errorf("KaspaDecode #%d\n got: %s want: %s", i,
|
|
spew.Sdump(msg), spew.Sdump(test.out))
|
|
continue
|
|
}
|
|
}
|
|
}
|