1 // Copyright (c) 2013-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.
11 "github.com/btcsuite/btcd/chaincfg/chainhash"
15 // MaxInvPerMsg is the maximum number of inventory vectors that can be in a
16 // single bitcoin inv message.
19 // Maximum payload size for an inventory vector.
20 maxInvVectPayload = 4 + chainhash.HashSize
22 // InvWitnessFlag denotes that the inventory vector type is requesting,
23 // or sending a version which includes witness data.
24 InvWitnessFlag = 1 << 30
27 // InvType represents the allowed types of inventory vectors. See InvVect.
30 // These constants define the various supported inventory vector types.
32 InvTypeError InvType = 0
34 InvTypeBlock InvType = 2
35 InvTypeFilteredBlock InvType = 3
36 InvTypeWitnessBlock InvType = InvTypeBlock | InvWitnessFlag
37 InvTypeWitnessTx InvType = InvTypeTx | InvWitnessFlag
38 InvTypeFilteredWitnessBlock InvType = InvTypeFilteredBlock | InvWitnessFlag
41 // Map of service flags back to their constant names for pretty printing.
42 var ivStrings = map[InvType]string{
43 InvTypeError: "ERROR",
45 InvTypeBlock: "MSG_BLOCK",
46 InvTypeFilteredBlock: "MSG_FILTERED_BLOCK",
47 InvTypeWitnessBlock: "MSG_WITNESS_BLOCK",
48 InvTypeWitnessTx: "MSG_WITNESS_TX",
49 InvTypeFilteredWitnessBlock: "MSG_FILTERED_WITNESS_BLOCK",
52 // String returns the InvType in human-readable form.
53 func (invtype InvType) String() string {
54 if s, ok := ivStrings[invtype]; ok {
58 return fmt.Sprintf("Unknown InvType (%d)", uint32(invtype))
61 // InvVect defines a bitcoin inventory vector which is used to describe data,
62 // as specified by the Type field, that a peer wants, has, or does not have to
65 Type InvType // Type of data
66 Hash chainhash.Hash // Hash of the data
69 // NewInvVect returns a new InvVect using the provided type and hash.
70 func NewInvVect(typ InvType, hash *chainhash.Hash) *InvVect {
77 // readInvVect reads an encoded InvVect from r depending on the protocol
79 func readInvVect(r io.Reader, pver uint32, iv *InvVect) error {
80 return readElements(r, &iv.Type, &iv.Hash)
83 // writeInvVect serializes an InvVect to w depending on the protocol version.
84 func writeInvVect(w io.Writer, pver uint32, iv *InvVect) error {
85 return writeElements(w, iv.Type, &iv.Hash)