From 510e9ff2eca66ef6864822a4631f948ea146d647 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 11 Dec 2020 11:03:27 +0300 Subject: [PATCH] [#243] core/object: Replace test content of tombstone with an API structure Signed-off-by: Leonard Lyubich --- go.mod | 2 +- go.sum | 4 +- pkg/core/object/fmt.go | 31 +++-- pkg/core/object/fmt_test.go | 19 ++- pkg/core/object/tombstone.go | 163 ------------------------- pkg/core/object/tombstone_test.go | 43 ------- pkg/services/object/put/distributed.go | 2 +- 7 files changed, 30 insertions(+), 234 deletions(-) delete mode 100644 pkg/core/object/tombstone.go delete mode 100644 pkg/core/object/tombstone_test.go diff --git a/go.mod b/go.mod index 7d8a985a2..2d3b22865 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/multiformats/go-multihash v0.0.13 // indirect github.com/nspcc-dev/hrw v1.0.9 github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b - github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201208072327-139660c6ff59 + github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201210152623-803c91b3eb2b github.com/nspcc-dev/neofs-crypto v0.3.0 github.com/nspcc-dev/tzhash v1.4.0 github.com/panjf2000/ants/v2 v2.3.0 diff --git a/go.sum b/go.sum index 27c97f4dd..51bd1a9c8 100644 --- a/go.sum +++ b/go.sum @@ -283,8 +283,8 @@ github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1: github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc= github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b h1:gk5bZgpWOehaDVKI5vBDkcjXTpRkKqcvIb1h/vHnBH4= github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b/go.mod h1:9s7LNp2lMgf0caH2t0sam4+WT2SIauXozwP0AdBqnEo= -github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201208072327-139660c6ff59 h1:OfeINXdkMrlfdqsVK5ECYi7vgt9TpO/R3JMQcnQndXI= -github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201208072327-139660c6ff59/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= +github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201210152623-803c91b3eb2b h1:crUr2jA84f0cQabYAN40uTYNTlu3xI9ZoB0BUBooQTA= +github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201210152623-803c91b3eb2b/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw= github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM= diff --git a/pkg/core/object/fmt.go b/pkg/core/object/fmt.go index 042fbef30..54452d95f 100644 --- a/pkg/core/object/fmt.go +++ b/pkg/core/object/fmt.go @@ -108,32 +108,37 @@ func (v *FormatValidator) checkOwnerKey(id *owner.ID, key []byte) error { } // ValidateContent validates payload content according to object type. -func (v *FormatValidator) ValidateContent(o *object.Object) error { +func (v *FormatValidator) ValidateContent(o *Object) error { switch o.Type() { case object.TypeTombstone: if len(o.Payload()) == 0 { return errors.Errorf("(%T) empty payload in tombstone", v) } - content, err := TombstoneContentFromBytes(o.Payload()) - if err != nil { - return errors.Wrapf(err, "(%T) could not parse tombstone content", err) + tombstone := object.NewTombstone() + + if err := tombstone.Unmarshal(o.Payload()); err != nil { + return errors.Wrapf(err, "(%T) could not unmarshal tombstone content", v) } - addrList := content.GetAddressList() + cid := o.ContainerID() + idList := tombstone.Members() + addrList := make([]*object.Address, 0, len(idList)) - for _, addr := range addrList { - if addr.ContainerID() == nil || addr.ObjectID() == nil { - return errors.Errorf("(%T) empty address reference in tombstone", v) + for _, id := range idList { + if id == nil { + return errors.Errorf("(%T) empty member in tombstone", v) } - } - tsAddr := new(object.Address) - tsAddr.SetContainerID(o.ContainerID()) - tsAddr.SetObjectID(o.ID()) + a := object.NewAddress() + a.SetContainerID(cid) + a.SetObjectID(id) + + addrList = append(addrList, a) + } if v.deleteHandler != nil { - v.deleteHandler.DeleteObjects(tsAddr, addrList...) + v.deleteHandler.DeleteObjects(o.Address(), addrList...) } } diff --git a/pkg/core/object/fmt_test.go b/pkg/core/object/fmt_test.go index 63243bdf2..8c2941a30 100644 --- a/pkg/core/object/fmt_test.go +++ b/pkg/core/object/fmt_test.go @@ -108,28 +108,25 @@ func TestFormatValidator_Validate(t *testing.T) { obj := NewRaw() obj.SetType(object.TypeTombstone) - require.Error(t, v.ValidateContent(obj.Object().SDK())) + require.Error(t, v.ValidateContent(obj.Object())) - addr := object.NewAddress() + content := object.NewTombstone() + content.SetMembers([]*object.ID{nil}) - content := NewTombstoneContent() - content.SetAddressList(addr) - - data, err := content.MarshalBinary() + data, err := content.Marshal() require.NoError(t, err) obj.SetPayload(data) - require.Error(t, v.ValidateContent(obj.Object().SDK())) + require.Error(t, v.ValidateContent(obj.Object())) - addr.SetContainerID(testContainerID(t)) - addr.SetObjectID(testObjectID(t)) + content.SetMembers([]*object.ID{testObjectID(t)}) - data, err = content.MarshalBinary() + data, err = content.Marshal() require.NoError(t, err) obj.SetPayload(data) - require.NoError(t, v.ValidateContent(obj.Object().SDK())) + require.NoError(t, v.ValidateContent(obj.Object())) }) } diff --git a/pkg/core/object/tombstone.go b/pkg/core/object/tombstone.go deleted file mode 100644 index 8ad04be9b..000000000 --- a/pkg/core/object/tombstone.go +++ /dev/null @@ -1,163 +0,0 @@ -package object - -import ( - "encoding/binary" - "fmt" - "io" - - "github.com/nspcc-dev/neofs-api-go/pkg/object" - "github.com/nspcc-dev/neofs-api-go/util/proto" - "github.com/nspcc-dev/neofs-api-go/v2/refs" - "github.com/pkg/errors" -) - -// FIXME: replace this code to neofs-api-go - -const addrListField = 1 - -type TombstoneContent TombstoneContentV2 - -type TombstoneContentV2 struct { - addrList []*refs.Address -} - -func NewTombstoneContentFromV2(cV2 *TombstoneContentV2) *TombstoneContent { - return (*TombstoneContent)(cV2) -} - -func NewTombstoneContent() *TombstoneContent { - return NewTombstoneContentFromV2(new(TombstoneContentV2)) -} - -func (c *TombstoneContent) MarshalBinary() ([]byte, error) { - return c.ToV2().StableMarshal(nil) -} - -func (c *TombstoneContent) SetAddressList(v ...*object.Address) { - if c != nil { - addrV2 := make([]*refs.Address, 0, len(v)) - - for i := range v { - addrV2 = append(addrV2, v[i].ToV2()) - } - - (*TombstoneContentV2)(c).SetAddressList(addrV2) - } -} - -func (c *TombstoneContent) GetAddressList() []*object.Address { - if c != nil { - addrV2 := (*TombstoneContentV2)(c).GetAddressList() - addr := make([]*object.Address, 0, len(addrV2)) - - for i := range addrV2 { - addr = append(addr, object.NewAddressFromV2(addrV2[i])) - } - - return addr - } - - return nil -} - -func (c *TombstoneContent) ToV2() *TombstoneContentV2 { - return (*TombstoneContentV2)(c) -} - -func (c *TombstoneContentV2) StableMarshal(buf []byte) ([]byte, error) { - if c == nil { - return make([]byte, 0), nil - } - - if buf == nil { - buf = make([]byte, c.StableSize()) - } - - offset := 0 - - for i := range c.addrList { - n, err := proto.NestedStructureMarshal(addrListField, buf[offset:], c.addrList[i]) - if err != nil { - return nil, err - } - - offset += n - } - - return buf, nil -} - -func (c *TombstoneContentV2) StableSize() (sz int) { - if c == nil { - return - } - - for i := range c.addrList { - sz += proto.NestedStructureSize(addrListField, c.addrList[i]) - } - - return -} - -func (c *TombstoneContentV2) StableUnmarshal(data []byte) error { - c.addrList = c.addrList[:0] - - for len(data) > 0 { - expPrefix, _ := proto.NestedStructurePrefix(addrListField) - - prefix, ln := binary.Uvarint(data) - if ln <= 0 { - return io.ErrUnexpectedEOF - } else if expPrefix != prefix { - return errors.Errorf("wrong prefix %d", prefix) - } - - data = data[ln:] - - sz, ln := binary.Uvarint(data) - if ln <= 0 { - return io.ErrUnexpectedEOF - } - - data = data[ln:] - - addr := new(refs.Address) - if err := addr.Unmarshal(data[:sz]); err != nil { - fmt.Println(err) - return err - } - - c.addrList = append(c.addrList, addr) - - data = data[sz:] - } - - return nil -} - -func (c *TombstoneContentV2) SetAddressList(v []*refs.Address) { - if c != nil { - c.addrList = v - } -} - -func (c *TombstoneContentV2) GetAddressList() []*refs.Address { - if c != nil { - return c.addrList - } - - return nil -} - -func TombstoneContentFromBytes(data []byte) (*TombstoneContent, error) { - if len(data) == 0 { - return nil, nil - } - - cV2 := new(TombstoneContentV2) - if err := cV2.StableUnmarshal(data); err != nil { - return nil, err - } - - return NewTombstoneContentFromV2(cV2), nil -} diff --git a/pkg/core/object/tombstone_test.go b/pkg/core/object/tombstone_test.go deleted file mode 100644 index 7ca935c6a..000000000 --- a/pkg/core/object/tombstone_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package object - -import ( - "crypto/sha256" - "testing" - - "github.com/nspcc-dev/neofs-api-go/pkg/container" - "github.com/nspcc-dev/neofs-api-go/pkg/object" - "github.com/stretchr/testify/require" -) - -func TestTombstoneContent_MarshalBinary(t *testing.T) { - cid1 := container.NewID() - cid1.SetSHA256([sha256.Size]byte{1, 2}) - - id1 := object.NewID() - id1.SetSHA256([sha256.Size]byte{3, 4}) - - addr1 := object.NewAddress() - addr1.SetObjectID(id1) - addr1.SetContainerID(cid1) - - cid2 := container.NewID() - cid2.SetSHA256([sha256.Size]byte{5, 6}) - - id2 := object.NewID() - id2.SetSHA256([sha256.Size]byte{7, 8}) - - addr2 := object.NewAddress() - addr2.SetObjectID(id2) - addr2.SetContainerID(cid2) - - c := NewTombstoneContent() - c.SetAddressList(addr1, addr2) - - data, err := c.MarshalBinary() - require.NoError(t, err) - - c2, err := TombstoneContentFromBytes(data) - require.NoError(t, err) - - require.Equal(t, c, c2) -} diff --git a/pkg/services/object/put/distributed.go b/pkg/services/object/put/distributed.go index 823f03180..81ac32cf4 100644 --- a/pkg/services/object/put/distributed.go +++ b/pkg/services/object/put/distributed.go @@ -65,7 +65,7 @@ func (t *distributedTarget) Close() (*transformer.AccessIdentifiers, error) { t.obj.SetPayload(payload) - if err := t.fmt.ValidateContent(t.obj.Object().SDK()); err != nil { + if err := t.fmt.ValidateContent(t.obj.Object()); err != nil { return nil, errors.Wrapf(err, "(%T) could not validate payload content", t) }