invvect.go raw

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