github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/invvect.go (about)

     1  // Copyright (c) 2013-2015 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package wire
     7  
     8  import (
     9  	"fmt"
    10  	"io"
    11  )
    12  
    13  const (
    14  	// MaxInvPerMsg is the maximum number of inventory vectors that can be in a
    15  	// single bitcoin inv message.
    16  	MaxInvPerMsg = 50000
    17  
    18  	// Maximum payload size for an inventory vector.
    19  	maxInvVectPayload = 4 + HashSize
    20  )
    21  
    22  // InvType represents the allowed types of inventory vectors.  See InvVect.
    23  type InvType uint32
    24  
    25  // These constants define the various supported inventory vector types.
    26  const (
    27  	InvTypeError         InvType = 0
    28  	InvTypeTx            InvType = 1
    29  	InvTypeBlock         InvType = 2
    30  	InvTypeFilteredBlock InvType = 3
    31  )
    32  
    33  // Map of service flags back to their constant names for pretty printing.
    34  var ivStrings = map[InvType]string{
    35  	InvTypeError:         "ERROR",
    36  	InvTypeTx:            "MSG_TX",
    37  	InvTypeBlock:         "MSG_BLOCK",
    38  	InvTypeFilteredBlock: "MSG_FILTERED_BLOCK",
    39  }
    40  
    41  // String returns the InvType in human-readable form.
    42  func (invtype InvType) String() string {
    43  	if s, ok := ivStrings[invtype]; ok {
    44  		return s
    45  	}
    46  
    47  	return fmt.Sprintf("Unknown InvType (%d)", uint32(invtype))
    48  }
    49  
    50  // InvVect defines a bitcoin inventory vector which is used to describe data,
    51  // as specified by the Type field, that a peer wants, has, or does not have to
    52  // another peer.
    53  type InvVect struct {
    54  	Type InvType // Type of data
    55  	Hash ShaHash // Hash of the data
    56  }
    57  
    58  // NewInvVect returns a new InvVect using the provided type and hash.
    59  func NewInvVect(typ InvType, hash *ShaHash) *InvVect {
    60  	return &InvVect{
    61  		Type: typ,
    62  		Hash: *hash,
    63  	}
    64  }
    65  
    66  // readInvVect reads an encoded InvVect from r depending on the protocol
    67  // version.
    68  func readInvVect(r io.Reader, pver uint32, iv *InvVect) error {
    69  	err := readElements(r, &iv.Type, &iv.Hash)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	return nil
    74  }
    75  
    76  // writeInvVect serializes an InvVect to w depending on the protocol version.
    77  func writeInvVect(w io.Writer, pver uint32, iv *InvVect) error {
    78  	err := writeElements(w, iv.Type, &iv.Hash)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	return nil
    83  }