Skip to content

Commit

Permalink
*: Replace github.com/nspcc-dev/neofs-api-go/v2 module
Browse files Browse the repository at this point in the history
Continues 8a26264 and finalizes
deprecation of `neofs-api-go/v2` module in favor of `proto/` packages.

Request/response signing and verification functions have been added
to `crypto` package A. They are used by the API client and will also be
used by https://github.com/nspcc-dev/neofs-node.

API client unit tests and AIO integration ones PASS, therefore, there is
no more way to track more regressions for now. If problems arise in
apps when updating the lib, fixes will be added later before the major
release.

Refs #270. Closes #452. Refs #532. Closes #551 (it's almost impossible
to do now).

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Dec 26, 2024
1 parent 8a26264 commit 1efaa3a
Show file tree
Hide file tree
Showing 49 changed files with 2,800 additions and 3,050 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# neofs-sdk-go
Go implementation of NeoFS SDK. It contains high-level version-independent wrappers
for structures from [neofs-api-go](https://github.com/nspcc-dev/neofs-api-go) as well as
for structures from [proto](https://github.com/nspcc-dev/neofs-sdk-go/proto) packages as well as
helper functions for simplifying node/dApp implementations.

## Repository structure
Expand Down
97 changes: 53 additions & 44 deletions client/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package client

import (
"context"
"fmt"
"time"

v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
protoaccounting "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/nspcc-dev/neofs-sdk-go/accounting"
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
protoaccounting "github.com/nspcc-dev/neofs-sdk-go/proto/accounting"
protosession "github.com/nspcc-dev/neofs-sdk-go/proto/session"
"github.com/nspcc-dev/neofs-sdk-go/stat"
"github.com/nspcc-dev/neofs-sdk-go/user"
"github.com/nspcc-dev/neofs-sdk-go/version"
)

// PrmBalanceGet groups parameters of BalanceGet operation.
Expand Down Expand Up @@ -49,60 +52,66 @@ func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.
return accounting.Decimal{}, err
}

// form request body
var accountV2 refs.OwnerID
prm.account.WriteToV2(&accountV2)

var body v2accounting.BalanceRequestBody
body.SetOwnerID(&accountV2)
req := &protoaccounting.BalanceRequest{
Body: &protoaccounting.BalanceRequest_Body{
OwnerId: prm.account.ProtoMessage(),
},
MetaHeader: &protosession.RequestMetaHeader{
Version: version.Current().ProtoMessage(),
Ttl: defaultRequestTTL,
},
}
writeXHeadersToMeta(prm.xHeaders, req.MetaHeader)

// form request
var req v2accounting.BalanceRequest
var res accounting.Decimal

req.SetBody(&body)
buf := c.buffers.Get().(*[]byte)
defer func() { c.buffers.Put(buf) }()

// init call context
req.VerifyHeader, err = neofscrypto.SignRequestWithBuffer[*protoaccounting.BalanceRequest_Body](c.prm.signer, req, *buf)
if err != nil {
err = fmt.Errorf("%w: %w", errSignRequest, err)
return res, err
}

var (
cc contextCall
res accounting.Decimal
)
resp, err := c.accounting.Balance(ctx, req)
if err != nil {
err = rpcErr(err)
return res, err
}

c.initCallContext(&cc)
cc.meta = prm.prmCommonMeta
cc.req = &req
cc.call = func() (responseV2, error) {
resp, err := c.accounting.Balance(ctx, req.ToGRPCMessage().(*protoaccounting.BalanceRequest))
if c.prm.cbRespInfo != nil {
err = c.prm.cbRespInfo(ResponseMetaInfo{
key: resp.GetVerifyHeader().GetBodySignature().GetKey(),
epoch: resp.GetMetaHeader().GetEpoch(),
})
if err != nil {
return nil, rpcErr(err)
}
var respV2 v2accounting.BalanceResponse
if err = respV2.FromGRPCMessage(resp); err != nil {
return nil, err
err = fmt.Errorf("%w: %w", errResponseCallback, err)
return res, err
}
return &respV2, nil
}
cc.result = func(r responseV2) {
resp := r.(*v2accounting.BalanceResponse)

const fieldBalance = "balance"
if err = neofscrypto.VerifyResponseWithBuffer[*protoaccounting.BalanceResponse_Body](resp, *buf); err != nil {
err = fmt.Errorf("%w: %w", errResponseSignatures, err)
return res, err
}

bal := resp.GetBody().GetBalance()
if bal == nil {
cc.err = newErrMissingResponseField(fieldBalance)
return
}
if err = apistatus.ToError(resp.GetMetaHeader().GetStatus()); err != nil {
return res, err
}

cc.err = res.ReadFromV2(*bal)
if cc.err != nil {
cc.err = newErrInvalidResponseField(fieldBalance, cc.err)
}
const fieldBalance = "balance"

bal := resp.GetBody().GetBalance()
if bal == nil {
err = newErrMissingResponseField(fieldBalance)
return res, err
}

// process call
if !cc.processCall() {
err = cc.err
return accounting.Decimal{}, cc.err
err = res.FromProtoMessage(bal)
if err != nil {
err = newErrInvalidResponseField(fieldBalance, err)
return res, err

Check warning on line 114 in client/accounting.go

View check run for this annotation

Codecov / codecov/patch

client/accounting.go#L113-L114

Added lines #L113 - L114 were not covered by tests
}

return res, nil
Expand Down
11 changes: 1 addition & 10 deletions client/accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"testing"
"time"

v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
protoaccounting "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc"
protoaccounting "github.com/nspcc-dev/neofs-sdk-go/proto/accounting"
"github.com/nspcc-dev/neofs-sdk-go/stat"
"github.com/nspcc-dev/neofs-sdk-go/user"
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
Expand All @@ -35,17 +34,9 @@ type testGetBalanceServer struct {
protoaccounting.UnimplementedAccountingServiceServer
testCommonUnaryServerSettings[
*protoaccounting.BalanceRequest_Body,
v2accounting.BalanceRequestBody,
*v2accounting.BalanceRequestBody,
*protoaccounting.BalanceRequest,
v2accounting.BalanceRequest,
*v2accounting.BalanceRequest,
*protoaccounting.BalanceResponse_Body,
v2accounting.BalanceResponseBody,
*v2accounting.BalanceResponseBody,
*protoaccounting.BalanceResponse,
v2accounting.BalanceResponse,
*v2accounting.BalanceResponse,
]
reqAcc *user.ID
}
Expand Down
14 changes: 7 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"time"

"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
protoaccounting "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc"
protocontainer "github.com/nspcc-dev/neofs-api-go/v2/container/grpc"
protonetmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
protoobject "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
protoreputation "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc"
protosession "github.com/nspcc-dev/neofs-api-go/v2/session/grpc"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
"github.com/nspcc-dev/neofs-sdk-go/internal/uriutil"
protoaccounting "github.com/nspcc-dev/neofs-sdk-go/proto/accounting"
protocontainer "github.com/nspcc-dev/neofs-sdk-go/proto/container"
protonetmap "github.com/nspcc-dev/neofs-sdk-go/proto/netmap"
protoobject "github.com/nspcc-dev/neofs-sdk-go/proto/object"
protoreputation "github.com/nspcc-dev/neofs-sdk-go/proto/reputation"
protosession "github.com/nspcc-dev/neofs-sdk-go/proto/session"
"github.com/nspcc-dev/neofs-sdk-go/stat"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -231,7 +231,7 @@ type PrmInit struct {

cbRespInfo func(ResponseMetaInfo) error

netMagic uint64
netMagic uint64 //nolint:unused // https://github.com/nspcc-dev/neofs-sdk-go/issues/671

statisticCallback stat.OperationCallback

Expand Down
Loading

0 comments on commit 1efaa3a

Please sign in to comment.