neoneo-go/pkg/network/payload/inventory.go

78 lines
1.7 KiB
Go
Raw Normal View History

2018-01-28 07:05:35 +00:00
package payload
import (
"github.com/CityOfZion/neo-go/pkg/io"
"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.
// 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 "TX"
case 0x02:
return "block"
2018-01-28 07:05:35 +00:00
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 (
TXType InventoryType = 0x01 // 1
BlockType InventoryType = 0x02 // 2
ConsensusType InventoryType = 0xe0 // 224
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
// A list of hashes.
Hashes []util.Uint256
2018-01-30 10:56:36 +00:00
}
// NewInventory return a pointer to an Inventory.
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
}
// DecodeBinary implements Serializable interface.
func (p *Inventory) DecodeBinary(br *io.BinReader) {
br.ReadLE(&p.Type)
2018-01-30 10:56:36 +00:00
listLen := br.ReadVarUint()
p.Hashes = make([]util.Uint256, listLen)
2018-01-30 10:56:36 +00:00
for i := 0; i < int(listLen); i++ {
br.ReadLE(&p.Hashes[i])
2018-01-30 10:56:36 +00:00
}
2018-01-28 07:05:35 +00:00
}
// EncodeBinary implements Serializable interface.
func (p *Inventory) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(p.Type)
2018-01-30 10:56:36 +00:00
listLen := len(p.Hashes)
bw.WriteVarUint(uint64(listLen))
for i := 0; i < listLen; i++ {
bw.WriteLE(p.Hashes[i])
2018-01-30 10:56:36 +00:00
}
2018-01-28 07:05:35 +00:00
}