2018-01-28 07:05:35 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-01-28 07:05:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The node can broadcast the object information it owns by this message.
|
2019-02-13 18:01:10 +00:00
|
|
|
// The message can be sent automatically or can be used to answer getblock messages.
|
2018-01-28 07:05:35 +00:00
|
|
|
|
|
|
|
// InventoryType is the type of an object in the Inventory message.
|
|
|
|
type InventoryType uint8
|
|
|
|
|
|
|
|
// String implements the Stringer interface.
|
|
|
|
func (i InventoryType) String() string {
|
|
|
|
switch i {
|
2020-05-28 21:13:37 +00:00
|
|
|
case TXType:
|
2018-01-28 07:05:35 +00:00
|
|
|
return "TX"
|
2020-05-28 21:13:37 +00:00
|
|
|
case BlockType:
|
2019-08-29 17:14:16 +00:00
|
|
|
return "block"
|
2020-05-28 21:13:37 +00:00
|
|
|
case ConsensusType:
|
2018-01-28 07:05:35 +00:00
|
|
|
return "consensus"
|
2020-11-27 10:55:48 +00:00
|
|
|
case P2PNotaryRequestType:
|
|
|
|
return "p2pNotaryRequest"
|
2018-01-28 07:05:35 +00:00
|
|
|
default:
|
|
|
|
return "unknown inventory type"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 10:56:36 +00:00
|
|
|
// Valid returns true if the inventory (type) is known.
|
2020-11-27 10:55:48 +00:00
|
|
|
func (i InventoryType) Valid(p2pSigExtensionsEnabled bool) bool {
|
|
|
|
return i == BlockType || i == TXType || i == ConsensusType || (p2pSigExtensionsEnabled && i == P2PNotaryRequestType)
|
2018-01-30 10:56:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 07:05:35 +00:00
|
|
|
// List of valid InventoryTypes.
|
|
|
|
const (
|
2020-11-27 10:55:48 +00:00
|
|
|
TXType InventoryType = 0x2b
|
|
|
|
BlockType InventoryType = 0x2c
|
|
|
|
ConsensusType InventoryType = 0x2d
|
|
|
|
P2PNotaryRequestType InventoryType = 0x50
|
2018-01-28 07:05:35 +00:00
|
|
|
)
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Inventory payload.
|
2018-01-28 07:05:35 +00:00
|
|
|
type Inventory struct {
|
|
|
|
// Type if the object hash.
|
|
|
|
Type InventoryType
|
2018-03-14 09:36:59 +00:00
|
|
|
|
|
|
|
// A list of hashes.
|
2018-02-04 19:54:51 +00:00
|
|
|
Hashes []util.Uint256
|
2018-01-30 10:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInventory return a pointer to an Inventory.
|
2018-02-04 19:54:51 +00:00
|
|
|
func NewInventory(typ InventoryType, hashes []util.Uint256) *Inventory {
|
2018-01-30 10:56:36 +00:00
|
|
|
return &Inventory{
|
|
|
|
Type: typ,
|
|
|
|
Hashes: hashes,
|
|
|
|
}
|
2018-01-28 07:05:35 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (p *Inventory) DecodeBinary(br *io.BinReader) {
|
2019-12-12 17:17:50 +00:00
|
|
|
p.Type = InventoryType(br.ReadB())
|
2020-07-08 12:25:58 +00:00
|
|
|
br.ReadArray(&p.Hashes, MaxHashesCount)
|
2018-01-28 07:05:35 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (p *Inventory) EncodeBinary(bw *io.BinWriter) {
|
2019-12-12 17:17:50 +00:00
|
|
|
bw.WriteB(byte(p.Type))
|
2019-11-14 08:07:23 +00:00
|
|
|
bw.WriteArray(p.Hashes)
|
2018-01-28 07:05:35 +00:00
|
|
|
}
|