Skip to content

Cleanup NNS resolver a bit #962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/handler/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type contResolver struct {
layer *layer.TestNeoFS
}

func (r *contResolver) Resolve(_ context.Context, name string) (cid.ID, error) {
func (r *contResolver) ResolveCID(_ context.Context, name string) (cid.ID, error) {
return r.layer.ContainerID(name)
}

Expand Down
12 changes: 8 additions & 4 deletions api/layer/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/api/data"
"github.com/nspcc-dev/neofs-s3-gw/api/layer/encryption"
"github.com/nspcc-dev/neofs-s3-gw/api/resolver"
"github.com/nspcc-dev/neofs-s3-gw/api/s3errors"
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
Expand Down Expand Up @@ -47,7 +46,7 @@ type (
// used in case of user wants to do something like anonymous.
// Typical using is a flag --no-sign-request in aws-cli.
anonymous user.ID
resolver resolver.Resolver
resolver Resolver
ncontroller EventListener
cache *Cache
treeService TreeService
Expand All @@ -59,7 +58,7 @@ type (
Caches *CachesConfig
GateKey *keys.PrivateKey
Anonymous user.ID
Resolver resolver.Resolver
Resolver Resolver
TreeService TreeService
}

Expand Down Expand Up @@ -87,6 +86,11 @@ type (
NoErrorOnDeleteMarker bool
}

// Resolver allows to map container ID by container name.
Resolver interface {
ResolveCID(ctx context.Context, containerName string) (cid.ID, error)
}

// RangeParams stores range header request parameters.
RangeParams struct {
Start uint64
Expand Down Expand Up @@ -673,7 +677,7 @@ func (n *layer) CreateBucket(ctx context.Context, p *CreateBucketParams) (*data.
func (n *layer) ResolveBucket(ctx context.Context, name string) (cid.ID, error) {
var cnrID cid.ID
if err := cnrID.DecodeString(name); err != nil {
if cnrID, err = n.resolver.Resolve(ctx, name); err != nil {
if cnrID, err = n.resolver.ResolveCID(ctx, name); err != nil {
return cid.ID{}, err
}

Expand Down
37 changes: 11 additions & 26 deletions api/resolver/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,25 @@ import (

"github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/nspcc-dev/neo-go/pkg/util"
rpcNNS "github.com/nspcc-dev/neofs-contract/rpc/nns"
"github.com/nspcc-dev/neofs-contract/rpc/nns"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
)

const (
nnsContract = int32(1)
)

// Container is a wrapper for the [Resolver]. It allows to update resolvers in runtime, without service restarting.
// Container is a wrapper for the [NNSResolver]. It allows to update resolvers in runtime, without service restarting.
//
// The Container should be used like regular [Resolver].
// The Container should be used like regular [NNSResolver].
type Container struct {
mu sync.RWMutex
resolver Resolver
resolver *NNSResolver
}

// Resolve looks up the container id by its name via NNS contract.
// ResolveCID looks up the container id by its name via NNS contract.
// The method calls inline resolver.
func (r *Container) Resolve(ctx context.Context, name string) (cid.ID, error) {
func (r *Container) ResolveCID(ctx context.Context, name string) (cid.ID, error) {
r.mu.RLock()
defer r.mu.RUnlock()

return r.resolver.Resolve(ctx, name)
return r.resolver.ResolveCID(ctx, name)
}

// UpdateResolvers allows to update resolver in runtime. Resolvers will be created from scratch.
Expand Down Expand Up @@ -63,7 +58,7 @@ func NewContainer(ctx context.Context, endpoint string) (*Container, error) {
// NewResolver returns resolver depending on corresponding endpoint.
//
// If endpoint is empty, error will be returned.
func NewResolver(ctx context.Context, endpoint string) (Resolver, error) {
func NewResolver(ctx context.Context, endpoint string) (*NNSResolver, error) {
if endpoint == "" {
return nil, errors.New("endpoint must be set")
}
Expand All @@ -73,23 +68,13 @@ func NewResolver(ctx context.Context, endpoint string) (Resolver, error) {
return nil, fmt.Errorf("rpcclient: %w", err)
}

nnsHash, err := systemContractHash(cl, nnsContract)
if err != nil {
return nil, fmt.Errorf("nns contract: %w", err)
}

inv := invoker.New(cl, nil)
nnsReader := rpcNNS.NewReader(inv, nnsHash)
return NewNNSResolver(nnsReader), nil
}

func systemContractHash(cl *rpcclient.Client, id int32) (util.Uint160, error) {
c, err := cl.GetContractStateByID(id)
nnsReader, err := nns.NewInferredReader(cl, inv)
if err != nil {
return util.Uint160{}, fmt.Errorf("GetContractStateByID [%d]: %w", id, err)
return nil, fmt.Errorf("nns reader instantiation: %w", err)
}

return c.Hash, nil
return NewNNSResolver(nnsReader), nil
}

func rpcClient(ctx context.Context, endpoint string) (*rpcclient.Client, error) {
Expand Down
9 changes: 2 additions & 7 deletions api/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ var (
ErrNotFound = errors.New("not found")
)

// Resolver allows to map container ID by container name.
type Resolver interface {
Resolve(ctx context.Context, containerName string) (cid.ID, error)
}

// NNSResolver allows to resolve container id by its name.
type NNSResolver struct {
reader *rpcNNS.ContractReader
Expand All @@ -31,8 +26,8 @@ func NewNNSResolver(reader *rpcNNS.ContractReader) *NNSResolver {
return &NNSResolver{reader: reader}
}

// Resolve looks up the container id by its name via NNS contract.
func (r *NNSResolver) Resolve(_ context.Context, name string) (cid.ID, error) {
// ResolveCID looks up the container id by its name via NNS contract.
func (r *NNSResolver) ResolveCID(_ context.Context, name string) (cid.ID, error) {
var result cid.ID

items, err := r.reader.GetRecords(nnsContainerDomain(name), rpcNNS.TXT)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/minio/sio v0.3.0
github.com/nats-io/nats.go v1.31.0
github.com/nspcc-dev/neo-go v0.106.1
github.com/nspcc-dev/neofs-contract v0.19.1
github.com/nspcc-dev/neofs-contract v0.19.2-0.20240610103236-d50c8e0c9396
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.12
github.com/nspcc-dev/tzhash v1.8.0
github.com/panjf2000/ants/v2 v2.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ github.com/nspcc-dev/neo-go v0.106.1 h1:VpWfNBimGRpqNIyjJj6RhuAEMy4GRahDrAClODOm
github.com/nspcc-dev/neo-go v0.106.1/go.mod h1:bZyJexBlrja4ngxiBgo8by5pVHuAbhg9l09/8yVGDyg=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.1-0.20240305074711-35bc78d84dc4 h1:arN0Ypn+jawZpu1BND7TGRn44InAVIqKygndsx0y2no=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.1-0.20240305074711-35bc78d84dc4/go.mod h1:7Tm1NKEoUVVIUlkVwFrPh7GG5+Lmta2m7EGr4oVpBd8=
github.com/nspcc-dev/neofs-contract v0.19.1 h1:U1Uh+MlzfkalO0kRJ2pADZyHrmAOroC6KLFjdWnTNR0=
github.com/nspcc-dev/neofs-contract v0.19.1/go.mod h1:ZOGouuwuHpgvYkx/LCGufGncIzEUhYEO18LL4cWEbyw=
github.com/nspcc-dev/neofs-contract v0.19.2-0.20240610103236-d50c8e0c9396 h1:1Fo1Dgdxqx4qzWU2rKPK2gxOKo4bsVaq93t7S1XKvrM=
github.com/nspcc-dev/neofs-contract v0.19.2-0.20240610103236-d50c8e0c9396/go.mod h1:r0bxoBSOMqE9mdjdjPJXNOmqflxxT7Tcbwry0WxStlA=
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.12 h1:mdxtlSU2I4oVZ/7AXTLKyz8uUPbDWikZw4DM8gvrddA=
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.12/go.mod h1:JdsEM1qgNukrWqgOBDChcYp8oY4XUzidcKaxY4hNJvQ=
github.com/nspcc-dev/rfc6979 v0.2.1 h1:8wWxkamHWFmO790GsewSoKUSJjVnL1fmdRpokU/RgRM=
Expand Down
Loading