Skip to content

Commit

Permalink
core/object: make expiration searching routine public
Browse files Browse the repository at this point in the history
Many packages may benefit from it.

Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
carpawell committed Jun 11, 2024
1 parent c707716 commit 658cbde
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
27 changes: 6 additions & 21 deletions pkg/core/object/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"strconv"

"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
Expand Down Expand Up @@ -78,8 +77,6 @@ var errNilID = errors.New("missing identifier")

var errNilCID = errors.New("missing container identifier")

var errNoExpirationEpoch = errors.New("missing expiration epoch attribute")

var errTombstoneExpiration = errors.New("tombstone body and header contain different expiration values")

var errEmptySGMembers = errors.New("storage group with empty members list")
Expand Down Expand Up @@ -170,7 +167,7 @@ func (v *FormatValidator) Validate(obj *object.Object, unprepared bool) error {
return fmt.Errorf("(%T) could not validate signature key: %w", v, err)
}

if err := v.checkExpiration(obj); err != nil {
if err := v.checkExpiration(*obj); err != nil {
return fmt.Errorf("object did not pass expiration check: %w", err)
}

Expand Down Expand Up @@ -288,7 +285,7 @@ func (v *FormatValidator) ValidateContent(o *object.Object) (ContentMeta, error)
}

// check if the tombstone has the same expiration in the body and the header
exp, err := expirationEpochAttribute(o)
exp, err := Expiration(*o)
if err != nil {
return ContentMeta{}, err
}
Expand Down Expand Up @@ -353,7 +350,7 @@ func (v *FormatValidator) ValidateContent(o *object.Object) (ContentMeta, error)
}

// check that LOCK object has correct expiration epoch
lockExp, err := expirationEpochAttribute(o)
lockExp, err := Expiration(*o)

Check warning on line 353 in pkg/core/object/fmt.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/object/fmt.go#L353

Added line #L353 was not covered by tests
if err != nil {
return ContentMeta{}, fmt.Errorf("lock object expiration epoch: %w", err)
}
Expand Down Expand Up @@ -385,10 +382,10 @@ func (v *FormatValidator) ValidateContent(o *object.Object) (ContentMeta, error)

var errExpired = errors.New("object has expired")

func (v *FormatValidator) checkExpiration(obj *object.Object) error {
exp, err := expirationEpochAttribute(obj)
func (v *FormatValidator) checkExpiration(obj object.Object) error {
exp, err := Expiration(obj)
if err != nil {
if errors.Is(err, errNoExpirationEpoch) {
if errors.Is(err, ErrNoExpiration) {
return nil // objects without expiration attribute are valid
}

Expand Down Expand Up @@ -419,18 +416,6 @@ func (v *FormatValidator) checkExpiration(obj *object.Object) error {
return nil
}

func expirationEpochAttribute(obj *object.Object) (uint64, error) {
for _, a := range obj.Attributes() {
if a.Key() != object.AttributeExpirationEpoch {
continue
}

return strconv.ParseUint(a.Value(), 10, 64)
}

return 0, errNoExpirationEpoch
}

var (
errDuplAttr = errors.New("duplication of attributes detected")
errEmptyAttrVal = errors.New("empty attribute value")
Expand Down
18 changes: 18 additions & 0 deletions pkg/core/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package object

import (
"errors"
"strconv"

"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
Expand All @@ -26,3 +27,20 @@ func AddressOf(obj *object.Object) oid.Address {

return addr
}

// ErrNoExpiration means no expiration was set.
var ErrNoExpiration = errors.New("missing expiration epoch attribute")

// Expiration searches for expiration attribute in the object. Returns
// ErrNoExpiration if not found.
func Expiration(obj object.Object) (uint64, error) {
for _, a := range obj.Attributes() {
if a.Key() != object.AttributeExpirationEpoch {
continue

Check warning on line 39 in pkg/core/object/object.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/object/object.go#L39

Added line #L39 was not covered by tests
}

return strconv.ParseUint(a.Value(), 10, 64)
}

return 0, ErrNoExpiration
}

0 comments on commit 658cbde

Please sign in to comment.