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
Since 527c964, proto-generated code is
located in the current code library. Previously, the neofs-api-go
module's code was used for protocol interactions. In essence, it is a
super-layer on top of the `google.golang.org/protobuf` module with only
one feature: stable marshalling (Protocol Buffers with ascending field
order). Since it is now provided by local `proto/*` packages, there is
no longer a need for a separate module.

In addition to trimming the code base and refactoring, a bonus is the
allocation of fewer intermediate Go objects, which will have a
beneficial effect on runtime.

Although most of the changes are refactorings, the following changes
have been applied:
 1. All exported elements that depended on neofs-api-go types are
 permanently removed. They could not be excluded via a temporary
 deprecation mark as this would cause a conflict with importing ''*.pb.go'
 files. Such elements were not recommended for use in advance, so most
 of the updated software will not suffer from breakages.

 2. 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.

 3. `neofs-api-go` a bug with interpreting enums as unsigned numbers,
 while protobuf lib decodes them into `~int32`. In addition, during
 encoding/decoding, all unknown enum values were set to zero, which
 could lead to data loss. This commit fixes both issues. Separating the
 fix from the refactoring would have required adding artificial buggy
 code, it was decided not to generate it.

 4. Test binaries/JSONs/signatures that followed the erroneous
 behavior of p.3 are fixed.

 5. While replacement of `ReadFromV2` methods called `FromProtoMessage`,
 `ProtoMessage` method replacing `WriteToV2`, taking into account the
 proposal from #532 and existing use cases, now dynamically allocates
 the message structure.

Closes #214. Refs #226. Refs #270. Closes #452. Closes #532. Closes #551
(it's almost impossible to do now). Fixes #606. Refs #652.

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Dec 16, 2024
1 parent c1bde22 commit c0a8824
Show file tree
Hide file tree
Showing 160 changed files with 5,859 additions and 8,661 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
62 changes: 28 additions & 34 deletions accounting/decimal.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
package accounting

import (
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
neofsproto "github.com/nspcc-dev/neofs-sdk-go/internal/proto"
protoaccounting "github.com/nspcc-dev/neofs-sdk-go/proto/accounting"
)

// Decimal represents decimal number for accounting operations.
//
// Decimal is mutually compatible with github.com/nspcc-dev/neofs-api-go/v2/accounting.Decimal
// message. See ReadFromV2 / WriteToV2 methods.
// Decimal is mutually compatible with [protoaccounting.Decimal] message. See
// [Decimal.FromProtoMessage] / [Decimal.FromProtoMessage] methods.
//
// Instances can be created using built-in var declaration.
//
// Note that direct typecast is not safe and may result in loss of compatibility:
//
// _ = Decimal(accounting.Decimal{}) // not recommended
type Decimal accounting.Decimal
type Decimal struct {
val int64
prec uint32
}

// ReadFromV2 reads Decimal from the accounting.Decimal message. Checks if the
// message conforms to NeoFS API V2 protocol.
// FromProtoMessage validates m according to the NeoFS API protocol and restores
// d from it.
//
// See also WriteToV2.
func (d *Decimal) ReadFromV2(m accounting.Decimal) error {
*d = Decimal(m)
// See also [Decimal.ProtoMessage].
func (d *Decimal) FromProtoMessage(m *protoaccounting.Decimal) error {
d.val = m.Value
d.prec = m.Precision
return nil
}

// WriteToV2 writes Decimal to the accounting.Decimal message.
// The message must not be nil.
// ProtoMessage converts d into message to transmit using the NeoFS API
// protocol.
//
// See also ReadFromV2.
func (d Decimal) WriteToV2(m *accounting.Decimal) {
*m = (accounting.Decimal)(d)
// See also [Decimal.FromProtoMessage].
func (d Decimal) ProtoMessage() *protoaccounting.Decimal {
return &protoaccounting.Decimal{
Value: d.val,
Precision: d.prec,
}
}

// Value returns value of the decimal number.
Expand All @@ -39,14 +43,14 @@ func (d Decimal) WriteToV2(m *accounting.Decimal) {
//
// See also SetValue.
func (d Decimal) Value() int64 {
return (*accounting.Decimal)(&d).GetValue()
return d.val
}

// SetValue sets value of the decimal number.
//
// See also Value.
func (d *Decimal) SetValue(v int64) {
(*accounting.Decimal)(d).SetValue(v)
d.val = v
}

// Precision returns precision of the decimal number.
Expand All @@ -55,25 +59,22 @@ func (d *Decimal) SetValue(v int64) {
//
// See also SetPrecision.
func (d Decimal) Precision() uint32 {
return (*accounting.Decimal)(&d).GetPrecision()
return d.prec
}

// SetPrecision sets precision of the decimal number.
//
// See also Precision.
func (d *Decimal) SetPrecision(p uint32) {
(*accounting.Decimal)(d).SetPrecision(p)
d.prec = p
}

// Marshal encodes Decimal into a binary format of the NeoFS API protocol
// (Protocol Buffers with direct field order).
//
// See also Unmarshal.
func (d Decimal) Marshal() []byte {
var m accounting.Decimal
d.WriteToV2(&m)

return m.StableMarshal(nil)
return neofsproto.Marshal(d)
}

// Unmarshal decodes NeoFS API protocol binary format into the Decimal
Expand All @@ -82,12 +83,5 @@ func (d Decimal) Marshal() []byte {
//
// See also Marshal.
func (d *Decimal) Unmarshal(data []byte) error {
var m accounting.Decimal

err := m.Unmarshal(data)
if err != nil {
return err
}

return d.ReadFromV2(m)
return neofsproto.Unmarshal(data, d)
}
23 changes: 11 additions & 12 deletions accounting/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"math/rand/v2"
"testing"

apiaccounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-sdk-go/accounting"
protoaccounting "github.com/nspcc-dev/neofs-sdk-go/proto/accounting"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -48,33 +48,32 @@ func TestDecimal_SetPrecision(t *testing.T) {
testDecimalField(t, accounting.Decimal.Precision, (*accounting.Decimal).SetPrecision)
}

func TestDecimal_ReadFromV2(t *testing.T) {
var m apiaccounting.Decimal
m.SetValue(anyValidValue)
m.SetPrecision(anyValidPrecision)
func TestDecimal_FromProtoMessage(t *testing.T) {
var m protoaccounting.Decimal
m.Value = anyValidValue
m.Precision = anyValidPrecision

var val accounting.Decimal
require.NoError(t, val.ReadFromV2(m))
require.NoError(t, val.FromProtoMessage(&m))
require.EqualValues(t, anyValidValue, val.Value())
require.EqualValues(t, anyValidPrecision, val.Precision())
}

func TestDecimal_WriteToV2(t *testing.T) {
func TestDecimal_ProtoMessage(t *testing.T) {
var val accounting.Decimal
var m apiaccounting.Decimal

// zero
val.WriteToV2(&m)
m := val.ProtoMessage()
require.Zero(t, m.GetValue())
require.Zero(t, m.GetPrecision())

// filled
val.SetValue(anyValidValue)
val.SetPrecision(anyValidPrecision)

val.WriteToV2(&m)
require.EqualValues(t, anyValidValue, val.Value())
require.EqualValues(t, anyValidPrecision, val.Precision())
m = val.ProtoMessage()
require.EqualValues(t, anyValidValue, m.GetValue())
require.EqualValues(t, anyValidPrecision, m.GetPrecision())
}

func TestToken_Marshal(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions accounting/test/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package accountingtest_test
import (
"testing"

apiaccounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-sdk-go/accounting"
accountingtest "github.com/nspcc-dev/neofs-sdk-go/accounting/test"
"github.com/stretchr/testify/require"
Expand All @@ -13,10 +12,9 @@ func TestDecimal(t *testing.T) {
d := accountingtest.Decimal()
require.NotEqual(t, d, accountingtest.Decimal())

var m apiaccounting.Decimal
d.WriteToV2(&m)
m := d.ProtoMessage()
var d2 accounting.Decimal
require.NoError(t, d2.ReadFromV2(m))
require.NoError(t, d2.FromProtoMessage(m))
require.Equal(t, d, d2)

require.NoError(t, new(accounting.Decimal).Unmarshal(d.Marshal()))
Expand Down
Loading

0 comments on commit c0a8824

Please sign in to comment.