2018-01-28 07:05:35 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2018-01-28 17:42:22 +00:00
|
|
|
"io"
|
2018-01-28 07:05:35 +00:00
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
"github.com/CityOfZion/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 {
|
|
|
|
case 0x01:
|
|
|
|
return "block"
|
|
|
|
case 0x02:
|
|
|
|
return "TX"
|
|
|
|
case 0xe0:
|
|
|
|
return "consensus"
|
|
|
|
default:
|
|
|
|
return "unknown inventory type"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 10:56:36 +00: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 07:05:35 +00:00
|
|
|
// List of valid InventoryTypes.
|
|
|
|
const (
|
2018-03-14 09:36:59 +00:00
|
|
|
TXType InventoryType = 0x01 // 1
|
|
|
|
BlockType InventoryType = 0x02 // 2
|
2018-03-09 15:55:25 +00:00
|
|
|
ConsensusType InventoryType = 0xe0 // 224
|
2018-01-28 07:05:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Inventory payload
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-28 17:42:22 +00:00
|
|
|
// DecodeBinary implements the Payload interface.
|
|
|
|
func (p *Inventory) DecodeBinary(r io.Reader) error {
|
2018-03-14 09:36:59 +00:00
|
|
|
if err := binary.Read(r, binary.LittleEndian, &p.Type); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-30 10:56:36 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
listLen := util.ReadVarUint(r)
|
2018-02-04 19:54:51 +00:00
|
|
|
p.Hashes = make([]util.Uint256, listLen)
|
2018-01-30 10:56:36 +00:00
|
|
|
for i := 0; i < int(listLen); i++ {
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &p.Hashes[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 17:42:22 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
return nil
|
2018-01-28 07:05:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 17:42:22 +00:00
|
|
|
// EncodeBinary implements the Payload interface.
|
|
|
|
func (p *Inventory) EncodeBinary(w io.Writer) error {
|
2018-03-14 09:36:59 +00:00
|
|
|
if err := binary.Write(w, binary.LittleEndian, p.Type); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-30 10:56:36 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
listLen := len(p.Hashes)
|
|
|
|
if err := util.WriteVarUint(w, uint64(listLen)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-30 10:56:36 +00:00
|
|
|
for i := 0; i < len(p.Hashes); i++ {
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, p.Hashes[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
return nil
|
2018-01-28 07:05:35 +00:00
|
|
|
}
|