From dabb8000fb190d3f384b844dcdc6abccdc5750d5 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 4 Apr 2017 14:50:27 -0600 Subject: [PATCH] Add Extended flag to cfilter and cfheaders messages --- peer/peer_test.go | 3 ++- server.go | 5 ++++- wire/message_test.go | 6 +++--- wire/msgcfheaders.go | 16 +++++++++++++++- wire/msgcfilter.go | 22 ++++++++++++++++++++-- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/peer/peer_test.go b/peer/peer_test.go index cddf7aa3..acab18bb 100644 --- a/peer/peer_test.go +++ b/peer/peer_test.go @@ -544,7 +544,8 @@ func TestPeerListeners(t *testing.T) { }, { "OnCFilter", - wire.NewMsgCFilter(&chainhash.Hash{}, []byte("payload")), + wire.NewMsgCFilter(&chainhash.Hash{}, true, + []byte("payload")), }, { "OnCFHeaders", diff --git a/server.go b/server.go index 1827f2c8..ca3e6dd2 100644 --- a/server.go +++ b/server.go @@ -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) } diff --git a/wire/message_test.go b/wire/message_test.go index 3a657704..88533f4d 100644 --- a/wire/message_test.go +++ b/wire/message_test.go @@ -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)) diff --git a/wire/msgcfheaders.go b/wire/msgcfheaders.go index 615fb3f5..aa26f014 100644 --- a/wire/msgcfheaders.go +++ b/wire/msgcfheaders.go @@ -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) } diff --git a/wire/msgcfilter.go b/wire/msgcfilter.go index a1d1b0fb..ed5c1fa1 100644 --- a/wire/msgcfilter.go +++ b/wire/msgcfilter.go @@ -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, } }