github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/network/payload/inventory.go (about)

     1  package payload
     2  
     3  import (
     4  	"github.com/nspcc-dev/neo-go/pkg/io"
     5  	"github.com/nspcc-dev/neo-go/pkg/util"
     6  )
     7  
     8  // A node can broadcast the object information it owns by this message.
     9  // The message can be sent automatically or can be used to answer getblock messages.
    10  
    11  // InventoryType is the type of an object in the Inventory message.
    12  type InventoryType uint8
    13  
    14  // String implements the Stringer interface.
    15  func (i InventoryType) String() string {
    16  	switch i {
    17  	case TXType:
    18  		return "TX"
    19  	case BlockType:
    20  		return "block"
    21  	case ExtensibleType:
    22  		return "extensible"
    23  	case P2PNotaryRequestType:
    24  		return "p2pNotaryRequest"
    25  	default:
    26  		return "unknown inventory type"
    27  	}
    28  }
    29  
    30  // Valid returns true if the inventory (type) is known.
    31  func (i InventoryType) Valid(p2pSigExtensionsEnabled bool) bool {
    32  	return i == BlockType || i == TXType || i == ExtensibleType || (p2pSigExtensionsEnabled && i == P2PNotaryRequestType)
    33  }
    34  
    35  // List of valid InventoryTypes.
    36  const (
    37  	TXType               InventoryType = 0x2b
    38  	BlockType            InventoryType = 0x2c
    39  	ExtensibleType       InventoryType = 0x2e
    40  	P2PNotaryRequestType InventoryType = 0x50
    41  )
    42  
    43  // Inventory payload.
    44  type Inventory struct {
    45  	// Type of the object hash.
    46  	Type InventoryType
    47  
    48  	// A list of hashes.
    49  	Hashes []util.Uint256
    50  }
    51  
    52  // NewInventory returns a pointer to an Inventory.
    53  func NewInventory(typ InventoryType, hashes []util.Uint256) *Inventory {
    54  	return &Inventory{
    55  		Type:   typ,
    56  		Hashes: hashes,
    57  	}
    58  }
    59  
    60  // DecodeBinary implements the Serializable interface.
    61  func (p *Inventory) DecodeBinary(br *io.BinReader) {
    62  	p.Type = InventoryType(br.ReadB())
    63  	br.ReadArray(&p.Hashes, MaxHashesCount)
    64  }
    65  
    66  // EncodeBinary implements the Serializable interface.
    67  func (p *Inventory) EncodeBinary(bw *io.BinWriter) {
    68  	bw.WriteB(byte(p.Type))
    69  	bw.WriteArray(p.Hashes)
    70  }