diff --git a/_pkg.dev/wire/payload/minventory.go b/_pkg.dev/wire/payload/minventory.go deleted file mode 100644 index cd994edb3..000000000 --- a/_pkg.dev/wire/payload/minventory.go +++ /dev/null @@ -1,114 +0,0 @@ -package payload - -import ( - "errors" - "io" - - "github.com/CityOfZion/neo-go/pkg/wire/command" - "github.com/CityOfZion/neo-go/pkg/wire/util" -) - -//InvType represents the enum of inventory types -type InvType uint8 - -const ( - // InvTypeTx represents the transaction inventory type - InvTypeTx InvType = 0x01 - // InvTypeBlock represents the block inventory type - InvTypeBlock InvType = 0x02 - // InvTypeConsensus represents the consensus inventory type - InvTypeConsensus InvType = 0xe0 -) - -const maxHashes = 0x10000000 - -var errMaxHash = errors.New("max size For Hashes reached") - -// InvMessage represents an Inventory message on the neo-network -type InvMessage struct { - cmd command.Type - Type InvType - Hashes []util.Uint256 -} - -//NewInvMessage returns an InvMessage object -func NewInvMessage(typ InvType) (*InvMessage, error) { - - inv := &InvMessage{ - command.Inv, - typ, - nil, - } - return inv, nil -} - -func newAbstractInv(typ InvType, cmd command.Type) (*InvMessage, error) { - inv, err := NewInvMessage(typ) - - if err != nil { - return nil, err - } - inv.cmd = cmd - - return inv, nil - -} - -// AddHash adds a hash to the list of hashes -func (inv *InvMessage) AddHash(h util.Uint256) error { - if len(inv.Hashes)+1 > maxHashes { - return errMaxHash - } - inv.Hashes = append(inv.Hashes, h) - return nil -} - -// AddHashes adds multiple hashes to the list of hashes -func (inv *InvMessage) AddHashes(hashes []util.Uint256) error { - var err error - for _, hash := range hashes { - err = inv.AddHash(hash) - if err != nil { - break - } - } - return err -} - -// DecodePayload Implements Messager interface -func (inv *InvMessage) DecodePayload(r io.Reader) error { - br := &util.BinReader{R: r} - - br.Read(&inv.Type) - - listLen := br.VarUint() - inv.Hashes = make([]util.Uint256, listLen) - - for i := 0; i < int(listLen); i++ { - br.Read(&inv.Hashes[i]) - } - return nil -} - -// EncodePayload Implements messager interface -func (inv *InvMessage) EncodePayload(w io.Writer) error { - - bw := &util.BinWriter{W: w} - bw.Write(inv.Type) - - lenhashes := len(inv.Hashes) - bw.VarUint(uint64(lenhashes)) - - for _, hash := range inv.Hashes { - - bw.Write(hash) - - } - - return bw.Err -} - -// Command Implements messager interface -func (inv *InvMessage) Command() command.Type { - return inv.cmd -} diff --git a/_pkg.dev/wire/payload/minventory_test.go b/_pkg.dev/wire/payload/minventory_test.go deleted file mode 100644 index 9e9c82dc9..000000000 --- a/_pkg.dev/wire/payload/minventory_test.go +++ /dev/null @@ -1,78 +0,0 @@ -package payload - -import ( - "bytes" - "encoding/hex" - "testing" - - "github.com/CityOfZion/neo-go/pkg/wire/command" - "github.com/CityOfZion/neo-go/pkg/wire/util" - - "github.com/stretchr/testify/assert" -) - -func TestNewInventory(t *testing.T) { - msgInv, err := NewInvMessage(InvTypeBlock) - - assert.Equal(t, nil, err) - assert.Equal(t, command.Inv, msgInv.Command()) - - hash, _ := util.Uint256DecodeBytes([]byte("hello")) - err = msgInv.AddHash(hash) - assert.Equal(t, nil, err) -} - -// Adjust test time or it will timeout -// func TestMaxHashes(t *testing.T) { -// msgInv, err := NewInvMessage(InvTypeBlock) -// assert.Equal(t, nil, err) - -// hash, _ := util.Uint256DecodeBytes([]byte("hello")) - -// for i := 0; i <= maxHashes+1; i++ { -// err = msgInv.AddHash(hash) -// } -// if err == nil { -// assert.Fail(t, "Max Hashes Exceeded, only allowed %v but have %v", maxHashes, len(msgInv.Hashes)) -// } else if err != MaxHashError { -// assert.Fail(t, "Expected a MaxHashError, however we got %s", err.Error()) -// } -// } -func TestEncodeDecodePayload(t *testing.T) { - msgInv, err := NewInvMessage(InvTypeBlock) - assert.Equal(t, nil, err) - - blockOneHash := "d782db8a38b0eea0d7394e0f007c61c71798867578c77c387c08113903946cc9" - hash, _ := util.Uint256DecodeString(blockOneHash) - - err = msgInv.AddHash(hash) - assert.Equal(t, nil, err) - - buf := new(bytes.Buffer) - err = msgInv.EncodePayload(buf) - assert.Equal(t, nil, err) - - numOfHashes := []byte{1} - expected := append([]byte{uint8(InvTypeBlock)}, numOfHashes...) - expected = append(expected, hash.Bytes()...) - - assert.Equal(t, hex.EncodeToString(expected), hex.EncodeToString(buf.Bytes())) - - var InvDec InvMessage - r := bytes.NewReader(buf.Bytes()) - err = InvDec.DecodePayload(r) - assert.Equal(t, nil, err) - - assert.Equal(t, 1, len(InvDec.Hashes)) - assert.Equal(t, blockOneHash, hex.EncodeToString(InvDec.Hashes[0].Bytes())) - -} -func TestEmptyInv(t *testing.T) { - msgInv, err := NewInvMessage(InvTypeBlock) - assert.Equal(t, nil, err) - - buf := new(bytes.Buffer) - msgInv.EncodePayload(buf) - assert.Equal(t, []byte{byte(InvTypeBlock), 0}, buf.Bytes()) - assert.Equal(t, 0, len(msgInv.Hashes)) -} diff --git a/pkg/network/payload/inventory_test.go b/pkg/network/payload/inventory_test.go index a407f348a..f9fffcc79 100644 --- a/pkg/network/payload/inventory_test.go +++ b/pkg/network/payload/inventory_test.go @@ -25,3 +25,13 @@ func TestInventoryEncodeDecode(t *testing.T) { assert.Nil(t, err) assert.Equal(t, inv, invDecode) } + +func TestEmptyInv(t *testing.T) { + msgInv := NewInventory(TXType, []Uint256{}) + + buf := new(bytes.Buffer) + err := msgInv.EncodeBinary(buf) + assert.Nil(t, err) + assert.Equal(t, []byte{byte(TXType), 0}, buf.Bytes()) + assert.Equal(t, 0, len(msgInv.Hashes)) +}