Add Extended flag to cfilter and cfheaders messages

This commit is contained in:
Alex 2017-04-04 14:50:27 -06:00 committed by Olaoluwa Osuntokun
parent 936caad9c2
commit dabb8000fb
5 changed files with 44 additions and 8 deletions

View file

@ -544,7 +544,8 @@ func TestPeerListeners(t *testing.T) {
},
{
"OnCFilter",
wire.NewMsgCFilter(&chainhash.Hash{}, []byte("payload")),
wire.NewMsgCFilter(&chainhash.Hash{}, true,
[]byte("payload")),
},
{
"OnCFHeaders",

View file

@ -756,7 +756,8 @@ func (sp *serverPeer) OnGetCFilter(_ *peer.Peer, msg *wire.MsgGetCFilter) {
err)
}
filterMsg := wire.NewMsgCFilter(&msg.BlockHash, filterBytes)
filterMsg := wire.NewMsgCFilter(&msg.BlockHash, msg.Extended,
filterBytes)
sp.QueueMessage(filterMsg, nil)
}
@ -807,6 +808,7 @@ func (sp *serverPeer) OnGetCFHeaders(_ *peer.Peer, msg *wire.MsgGetCFHeaders) {
headersMsg := wire.NewMsgCFHeaders()
headersMsg.AddCFHeader(&header)
headersMsg.StopHash = msg.HashStop
headersMsg.Extended = msg.Extended
sp.QueueMessage(headersMsg, nil)
return
}
@ -863,6 +865,7 @@ func (sp *serverPeer) OnGetCFHeaders(_ *peer.Peer, msg *wire.MsgGetCFHeaders) {
headersMsg.AddCFHeader(&header)
}
headersMsg.Extended = msg.Extended
headersMsg.StopHash = hashList[len(hashList)-1]
sp.QueueMessage(headersMsg, nil)
}

View file

@ -71,7 +71,7 @@ func TestMessage(t *testing.T) {
msgReject := NewMsgReject("block", RejectDuplicate, "duplicate block")
msgGetCFilter := NewMsgGetCFilter(&chainhash.Hash{}, false)
msgGetCFHeaders := NewMsgGetCFHeaders()
msgCFilter := NewMsgCFilter(&chainhash.Hash{}, []byte("payload"))
msgCFilter := NewMsgCFilter(&chainhash.Hash{}, true, []byte("payload"))
msgCFHeaders := NewMsgCFHeaders()
tests := []struct {
@ -104,8 +104,8 @@ func TestMessage(t *testing.T) {
{msgReject, msgReject, pver, MainNet, 79},
{msgGetCFilter, msgGetCFilter, pver, MainNet, 57},
{msgGetCFHeaders, msgGetCFHeaders, pver, MainNet, 62},
{msgCFilter, msgCFilter, pver, MainNet, 64},
{msgCFHeaders, msgCFHeaders, pver, MainNet, 57},
{msgCFilter, msgCFilter, pver, MainNet, 65},
{msgCFHeaders, msgCFHeaders, pver, MainNet, 58},
}
t.Logf("Running %d tests", len(tests))

View file

@ -28,6 +28,7 @@ const (
// MsgGetCFHeaders for details on requesting the headers.
type MsgCFHeaders struct {
StopHash chainhash.Hash
Extended bool
HeaderHashes []*chainhash.Hash
}
@ -52,6 +53,13 @@ func (msg *MsgCFHeaders) BtcDecode(r io.Reader, pver uint32) error {
return err
}
// Read extended flag
err = readElement(r, &msg.Extended)
if err != nil {
return err
}
// Read number of filter headers
count, err := ReadVarInt(r, pver)
if err != nil {
return err
@ -89,6 +97,12 @@ func (msg *MsgCFHeaders) BtcEncode(w io.Writer, pver uint32) error {
return err
}
// Write extended flag
err = writeElement(w, msg.Extended)
if err != nil {
return err
}
// Limit to max committed headers per message.
count := len(msg.HeaderHashes)
if count > MaxCFHeadersPerMsg {
@ -139,7 +153,7 @@ func (msg *MsgCFHeaders) Command() string {
// receiver. This is part of the Message interface implementation.
func (msg *MsgCFHeaders) MaxPayloadLength(pver uint32) uint32 {
// Hash size + num headers (varInt) + (header size * max headers).
return chainhash.HashSize + MaxVarIntPayload +
return chainhash.HashSize + 1 + MaxVarIntPayload +
(MaxCFHeaderPayload * MaxCFHeadersPerMsg)
}

View file

@ -18,6 +18,7 @@ const (
type MsgCFilter struct {
BlockHash chainhash.Hash
Extended bool
Data []byte
}
@ -25,7 +26,17 @@ type MsgCFilter struct {
// This is part of the Message interface implementation.
func (msg *MsgCFilter) BtcDecode(r io.Reader, pver uint32) error {
var err error
// Read the hash of the filter's block
err = readElement(r, &msg.BlockHash)
if err != nil {
return err
}
// Read extended flag
err = readElement(r, &msg.Extended)
if err != nil {
return err
}
// Read filter data
msg.Data, err = ReadVarBytes(r, pver, MaxCFilterDataSize,
"cfilter data")
return err
@ -46,6 +57,11 @@ func (msg *MsgCFilter) BtcEncode(w io.Writer, pver uint32) error {
return err
}
err = writeElement(w, msg.Extended)
if err != nil {
return err
}
return WriteVarBytes(w, pver, msg.Data)
}
@ -75,14 +91,16 @@ func (msg *MsgCFilter) Command() string {
// receiver. This is part of the Message interface implementation.
func (msg *MsgCFilter) MaxPayloadLength(pver uint32) uint32 {
return uint32(VarIntSerializeSize(MaxCFilterDataSize)) +
MaxCFilterDataSize
MaxCFilterDataSize + chainhash.HashSize + 1
}
// NewMsgCFilter returns a new bitcoin cfilter message that conforms to the
// Message interface. See MsgCFilter for details.
func NewMsgCFilter(blockHash *chainhash.Hash, data []byte) *MsgCFilter {
func NewMsgCFilter(blockHash *chainhash.Hash, extended bool,
data []byte) *MsgCFilter {
return &MsgCFilter{
BlockHash: *blockHash,
Extended: extended,
Data: data,
}
}