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", "OnCFilter",
wire.NewMsgCFilter(&chainhash.Hash{}, []byte("payload")), wire.NewMsgCFilter(&chainhash.Hash{}, true,
[]byte("payload")),
}, },
{ {
"OnCFHeaders", "OnCFHeaders",

View file

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

View file

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

View file

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

View file

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