diff --git a/server.go b/server.go index 56eae15f7..1e47eca41 100644 --- a/server.go +++ b/server.go @@ -755,6 +755,9 @@ func (sp *serverPeer) OnGetCFilter(_ *peer.Peer, msg *wire.MsgGetCFilter) { peerLog.Infof("Could not obtain CB filter for %v: %v", msg.BlockHash, err) } + + filterMsg := wire.NewMsgCFilter(filterBytes) + sp.QueueMessage(filterMsg, nil) } // enforceNodeBloomFlag disconnects the peer if the server is not configured to diff --git a/wire/message.go b/wire/message.go index 96388b603..168d881d3 100644 --- a/wire/message.go +++ b/wire/message.go @@ -52,6 +52,7 @@ const ( CmdSendHeaders = "sendheaders" CmdFeeFilter = "feefilter" CmdGetCFilter = "getcfilter" + CmdCFilter = "cfilter" ) // MessageEncoding represents the wire message encoding format to be used. @@ -160,6 +161,9 @@ func makeEmptyMessage(command string) (Message, error) { case CmdGetCFilter: msg = &MsgGetCFilter{} + case CmdCFilter: + msg = &MsgCFilter{} + default: return nil, fmt.Errorf("unhandled command [%s]", command) } diff --git a/wire/msgcfilter.go b/wire/msgcfilter.go new file mode 100644 index 000000000..ad12cc909 --- /dev/null +++ b/wire/msgcfilter.go @@ -0,0 +1,61 @@ +// Copyright (c) 2017 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 ( + "fmt" + "io" +) + +const ( + // MaxCFilterDataSize is the maximum byte size of a committed filter. + MaxCFilterDataSize = 65536 +) +type MsgCFilter struct { + Data []byte +} + +// BtcDecode decodes r using the bitcoin protocol encoding into the receiver. +// This is part of the Message interface implementation. +func (msg *MsgCFilter) BtcDecode(r io.Reader, pver uint32) error { + var err error + msg.Data, err = ReadVarBytes(r, pver, MaxCFilterDataSize, + "cfilter data") + return err +} + +// BtcEncode encodes the receiver to w using the bitcoin protocol encoding. +// This is part of the Message interface implementation. +func (msg *MsgCFilter) BtcEncode(w io.Writer, pver uint32) error { + size := len(msg.Data) + if size > MaxCFilterDataSize { + str := fmt.Sprintf("cfilter size too large for message "+ + "[size %v, max %v]", size, MaxCFilterDataSize) + return messageError("MsgCFilter.BtcEncode", str) + } + + return WriteVarBytes(w, pver, msg.Data) +} + +// Command returns the protocol command string for the message. This is part +// of the Message interface implementation. +func (msg *MsgCFilter) Command() string { + return CmdCFilter +} + +// MaxPayloadLength returns the maximum length the payload can be for the +// receiver. This is part of the Message interface implementation. +func (msg *MsgCFilter) MaxPayloadLength(pver uint32) uint32 { + return uint32(VarIntSerializeSize(MaxCFilterDataSize)) + + MaxCFilterDataSize +} + +// NewMsgFilterAdd returns a new bitcoin filteradd message that conforms to the +// Message interface. See MsgCFilter for details. +func NewMsgCFilter(data []byte) *MsgCFilter { + return &MsgCFilter{ + Data: data, + } +} diff --git a/wire/msggetcfilter.go b/wire/msggetcfilter.go index 5e3e86142..b8aa18e77 100644 --- a/wire/msggetcfilter.go +++ b/wire/msggetcfilter.go @@ -1,4 +1,4 @@ -// Copyright (c) 2013-2016 The btcsuite developers +// Copyright (c) 2017 The btcsuite developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file.