msgmerkleblock.go raw
1 // Copyright (c) 2014-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package wire
6
7 import (
8 "fmt"
9 "io"
10
11 "github.com/btcsuite/btcd/chainhash/v2"
12 )
13
14 // maxFlagsPerMerkleBlock is the maximum number of flag bytes that could
15 // possibly fit into a merkle block. Since each transaction is represented by
16 // a single bit, this is the max number of transactions per block divided by
17 // 8 bits per byte. Then an extra one to cover partials.
18 const maxFlagsPerMerkleBlock = maxTxPerBlock / 8
19
20 // MsgMerkleBlock implements the Message interface and represents a bitcoin
21 // merkleblock message which is used to reset a Bloom filter.
22 //
23 // This message was not added until protocol version BIP0037Version.
24 type MsgMerkleBlock struct {
25 Header BlockHeader
26 Transactions uint32
27 Hashes []*chainhash.Hash
28 Flags []byte
29 }
30
31 // AddTxHash adds a new transaction hash to the message.
32 func (msg *MsgMerkleBlock) AddTxHash(hash *chainhash.Hash) error {
33 if len(msg.Hashes)+1 > maxTxPerBlock {
34 str := fmt.Sprintf("too many tx hashes for message [max %v]",
35 maxTxPerBlock)
36 return messageError("MsgMerkleBlock.AddTxHash", str)
37 }
38
39 msg.Hashes = append(msg.Hashes, hash)
40 return nil
41 }
42
43 // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
44 // This is part of the Message interface implementation.
45 func (msg *MsgMerkleBlock) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
46 if pver < BIP0037Version {
47 str := fmt.Sprintf("merkleblock message invalid for protocol "+
48 "version %d", pver)
49 return messageError("MsgMerkleBlock.BtcDecode", str)
50 }
51
52 buf := binarySerializer.Borrow()
53 defer binarySerializer.Return(buf)
54
55 err := readBlockHeaderBuf(r, pver, &msg.Header, buf)
56 if err != nil {
57 return err
58 }
59
60 if _, err := io.ReadFull(r, buf[:4]); err != nil {
61 return err
62 }
63 msg.Transactions = littleEndian.Uint32(buf[:4])
64
65 // Read num block locator hashes and limit to max.
66 count, err := ReadVarIntBuf(r, pver, buf)
67 if err != nil {
68 return err
69 }
70 if count > maxTxPerBlock {
71 str := fmt.Sprintf("too many transaction hashes for message "+
72 "[count %v, max %v]", count, maxTxPerBlock)
73 return messageError("MsgMerkleBlock.BtcDecode", str)
74 }
75 if err := validateElementCount(
76 r, count, chainhash.HashSize,
77 ); err != nil {
78 return err
79 }
80
81 // Create a contiguous slice of hashes to deserialize into in order to
82 // reduce the number of allocations.
83 hashes := make([]chainhash.Hash, count)
84 msg.Hashes = make([]*chainhash.Hash, 0, count)
85 for i := uint64(0); i < count; i++ {
86 hash := &hashes[i]
87 _, err := io.ReadFull(r, hash[:])
88 if err != nil {
89 return err
90 }
91 msg.AddTxHash(hash)
92 }
93
94 msg.Flags, err = ReadVarBytesBuf(r, pver, buf, maxFlagsPerMerkleBlock,
95 "merkle block flags size")
96 return err
97 }
98
99 // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
100 // This is part of the Message interface implementation.
101 func (msg *MsgMerkleBlock) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
102 if pver < BIP0037Version {
103 str := fmt.Sprintf("merkleblock message invalid for protocol "+
104 "version %d", pver)
105 return messageError("MsgMerkleBlock.BtcEncode", str)
106 }
107
108 // Read num transaction hashes and limit to max.
109 numHashes := len(msg.Hashes)
110 if numHashes > maxTxPerBlock {
111 str := fmt.Sprintf("too many transaction hashes for message "+
112 "[count %v, max %v]", numHashes, maxTxPerBlock)
113 return messageError("MsgMerkleBlock.BtcDecode", str)
114 }
115 numFlagBytes := len(msg.Flags)
116 if numFlagBytes > maxFlagsPerMerkleBlock {
117 str := fmt.Sprintf("too many flag bytes for message [count %v, "+
118 "max %v]", numFlagBytes, maxFlagsPerMerkleBlock)
119 return messageError("MsgMerkleBlock.BtcDecode", str)
120 }
121
122 buf := binarySerializer.Borrow()
123 defer binarySerializer.Return(buf)
124
125 err := writeBlockHeaderBuf(w, pver, &msg.Header, buf)
126 if err != nil {
127 return err
128 }
129
130 littleEndian.PutUint32(buf[:4], msg.Transactions)
131 if _, err := w.Write(buf[:4]); err != nil {
132 return err
133 }
134
135 err = WriteVarIntBuf(w, pver, uint64(numHashes), buf)
136 if err != nil {
137 return err
138 }
139 for _, hash := range msg.Hashes {
140 _, err := w.Write(hash[:])
141 if err != nil {
142 return err
143 }
144 }
145
146 err = WriteVarBytesBuf(w, pver, msg.Flags, buf)
147 return err
148 }
149
150 // Command returns the protocol command string for the message. This is part
151 // of the Message interface implementation.
152 func (msg *MsgMerkleBlock) Command() string {
153 return CmdMerkleBlock
154 }
155
156 // MaxPayloadLength returns the maximum length the payload can be for the
157 // receiver. This is part of the Message interface implementation.
158 func (msg *MsgMerkleBlock) MaxPayloadLength(pver uint32) uint32 {
159 return MaxBlockPayload
160 }
161
162 // NewMsgMerkleBlock returns a new bitcoin merkleblock message that conforms to
163 // the Message interface. See MsgMerkleBlock for details.
164 func NewMsgMerkleBlock(bh *BlockHeader) *MsgMerkleBlock {
165 return &MsgMerkleBlock{
166 Header: *bh,
167 Transactions: 0,
168 Hashes: make([]*chainhash.Hash, 0),
169 Flags: make([]byte, 0),
170 }
171 }
172