2018-01-28 08:05:35 +01:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
2019-09-16 12:18:13 +03:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-02-04 20:54:51 +01:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2018-01-28 08:05:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// The node can broadcast the object information it owns by this message.
|
2019-02-13 21:01:10 +03:00
|
|
|
// The message can be sent automatically or can be used to answer getblock messages.
|
2018-01-28 08:05:35 +01: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 {
|
|
|
|
case 0x01:
|
|
|
|
return "TX"
|
2019-08-29 20:14:16 +03:00
|
|
|
case 0x02:
|
|
|
|
return "block"
|
2018-01-28 08:05:35 +01:00
|
|
|
case 0xe0:
|
|
|
|
return "consensus"
|
|
|
|
default:
|
|
|
|
return "unknown inventory type"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:56:36 +01:00
|
|
|
// Valid returns true if the inventory (type) is known.
|
|
|
|
func (i InventoryType) Valid() bool {
|
|
|
|
return i == BlockType || i == TXType || i == ConsensusType
|
|
|
|
}
|
|
|
|
|
2018-01-28 08:05:35 +01:00
|
|
|
// List of valid InventoryTypes.
|
|
|
|
const (
|
2018-03-14 10:36:59 +01:00
|
|
|
TXType InventoryType = 0x01 // 1
|
|
|
|
BlockType InventoryType = 0x02 // 2
|
2018-03-09 16:55:25 +01:00
|
|
|
ConsensusType InventoryType = 0xe0 // 224
|
2018-01-28 08:05:35 +01:00
|
|
|
)
|
|
|
|
|
2019-10-22 17:56:03 +03:00
|
|
|
// Inventory payload.
|
2018-01-28 08:05:35 +01:00
|
|
|
type Inventory struct {
|
|
|
|
// Type if the object hash.
|
|
|
|
Type InventoryType
|
2018-03-14 10:36:59 +01:00
|
|
|
|
|
|
|
// A list of hashes.
|
2018-02-04 20:54:51 +01:00
|
|
|
Hashes []util.Uint256
|
2018-01-30 11:56:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInventory return a pointer to an Inventory.
|
2018-02-04 20:54:51 +01:00
|
|
|
func NewInventory(typ InventoryType, hashes []util.Uint256) *Inventory {
|
2018-01-30 11:56:36 +01:00
|
|
|
return &Inventory{
|
|
|
|
Type: typ,
|
|
|
|
Hashes: hashes,
|
|
|
|
}
|
2018-01-28 08:05:35 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 19:31:49 +03:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (p *Inventory) DecodeBinary(br *io.BinReader) {
|
2019-12-12 20:17:50 +03:00
|
|
|
p.Type = InventoryType(br.ReadB())
|
2019-11-14 11:07:23 +03:00
|
|
|
br.ReadArray(&p.Hashes)
|
2018-01-28 08:05:35 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 19:31:49 +03:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (p *Inventory) EncodeBinary(bw *io.BinWriter) {
|
2019-12-12 20:17:50 +03:00
|
|
|
bw.WriteB(byte(p.Type))
|
2019-11-14 11:07:23 +03:00
|
|
|
bw.WriteArray(p.Hashes)
|
2018-01-28 08:05:35 +01:00
|
|
|
}
|