morph/event: Reuse auto-generated parsers #1541

Merged
fyrchik merged 4 commits from fyrchik/frostfs-node:fix-event-parsing into master 2024-12-06 10:26:40 +00:00
10 changed files with 59 additions and 138 deletions

2
go.mod
View file

@ -4,7 +4,7 @@ go 1.22
require (
code.gitea.io/sdk/gitea v0.17.1
git.frostfs.info/TrueCloudLab/frostfs-contract v0.21.0-rc.4
git.frostfs.info/TrueCloudLab/frostfs-contract v0.21.1-0.20241205083807-762d7f9f9f08
git.frostfs.info/TrueCloudLab/frostfs-crypto v0.6.0
git.frostfs.info/TrueCloudLab/frostfs-locode-db v0.4.1-0.20240710074952-65761deb5c0d
git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20241112082307-f17779933e88

BIN
go.sum

Binary file not shown.

View file

@ -3,7 +3,7 @@ package balance
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/balance"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -47,61 +47,17 @@ func (l Lock) TxHash() util.Uint256 { return l.txHash }
// ParseLock from notification into lock structure.
func ParseLock(e *state.ContainedNotificationEvent) (event.Event, error) {
var (
ev Lock
err error
)
params, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
var le balance.LockEvent
if err := le.FromStackItem(e.Item); err != nil {
return nil, fmt.Errorf("parse balance.LockEvent: %w", err)
}
if ln := len(params); ln != 5 {
return nil, event.WrongNumberOfParameters(5, ln)
}
// parse id
ev.id, err = client.BytesFromStackItem(params[0])
if err != nil {
return nil, fmt.Errorf("could not get lock id: %w", err)
}
// parse user
user, err := client.BytesFromStackItem(params[1])
if err != nil {
return nil, fmt.Errorf("could not get lock user value: %w", err)
}
ev.user, err = util.Uint160DecodeBytesBE(user)
if err != nil {
return nil, fmt.Errorf("could not convert lock user value to uint160: %w", err)
}
// parse lock account
lock, err := client.BytesFromStackItem(params[2])
if err != nil {
return nil, fmt.Errorf("could not get lock account value: %w", err)
}
ev.lock, err = util.Uint160DecodeBytesBE(lock)
if err != nil {
return nil, fmt.Errorf("could not convert lock account value to uint160: %w", err)
}
// parse amount
ev.amount, err = client.IntFromStackItem(params[3])
if err != nil {
return nil, fmt.Errorf("could not get lock amount: %w", err)
}
// parse until deadline
ev.until, err = client.IntFromStackItem(params[4])
if err != nil {
return nil, fmt.Errorf("could not get lock deadline: %w", err)
}
ev.txHash = e.Container
return ev, nil
return Lock{
id: le.TxID,
user: le.From,
lock: le.To,
amount: le.Amount.Int64(),
until: le.Until.Int64(),
txHash: e.Container,
}, nil
}

View file

@ -4,7 +4,6 @@ import (
"math/big"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
@ -28,7 +27,7 @@ func TestParseLock(t *testing.T) {
}
_, err := ParseLock(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(5, len(prms)).Error())
require.Error(t, err)
})
t.Run("wrong id parameter", func(t *testing.T) {

View file

@ -3,7 +3,7 @@ package container
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/container"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"github.com/nspcc-dev/neo-go/pkg/core/state"
@ -58,28 +58,14 @@ func (DeleteSuccess) MorphEvent() {}
// ParseDeleteSuccess decodes notification event thrown by Container contract into
// DeleteSuccess and returns it as event.Event.
func ParseDeleteSuccess(e *state.ContainedNotificationEvent) (event.Event, error) {
items, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("parse stack array from raw notification event: %w", err)
var dse container.DeleteSuccessEvent
if err := dse.FromStackItem(e.Item); err != nil {
return nil, fmt.Errorf("parse container.DeleteSuccessEvent: %w", err)
}
const expectedItemNumDeleteSuccess = 1
if ln := len(items); ln != expectedItemNumDeleteSuccess {
return nil, event.WrongNumberOfParameters(expectedItemNumDeleteSuccess, ln)
}
binID, err := client.BytesFromStackItem(items[0])
if err != nil {
return nil, fmt.Errorf("parse container ID item: %w", err)
}
var res DeleteSuccess
err = res.ID.Decode(binID)
if err != nil {
return nil, fmt.Errorf("decode container ID: %w", err)
}
return res, nil
var cnr cid.ID
cnr.SetSHA256(dse.ContainerID)
return DeleteSuccess{
ID: cnr,
}, nil
}

View file

@ -4,7 +4,6 @@ import (
"crypto/sha256"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
@ -18,7 +17,7 @@ func TestParseDeleteSuccess(t *testing.T) {
}
_, err := ParseDeleteSuccess(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error())
require.Error(t, err)
})
t.Run("wrong container parameter", func(t *testing.T) {

View file

@ -3,7 +3,7 @@ package container
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/container"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"github.com/nspcc-dev/neo-go/pkg/core/state"
@ -78,33 +78,14 @@ func (PutSuccess) MorphEvent() {}
// ParsePutSuccess decodes notification event thrown by Container contract into
// PutSuccess and returns it as event.Event.
func ParsePutSuccess(e *state.ContainedNotificationEvent) (event.Event, error) {
items, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("parse stack array from raw notification event: %w", err)
var pse container.PutSuccessEvent
if err := pse.FromStackItem(e.Item); err != nil {
return nil, fmt.Errorf("parse container.PutSuccessEvent: %w", err)
}
const expectedItemNumPutSuccess = 2
if ln := len(items); ln != expectedItemNumPutSuccess {
return nil, event.WrongNumberOfParameters(expectedItemNumPutSuccess, ln)
}
binID, err := client.BytesFromStackItem(items[0])
if err != nil {
return nil, fmt.Errorf("parse container ID item: %w", err)
}
_, err = client.BytesFromStackItem(items[1])
if err != nil {
return nil, fmt.Errorf("parse public key item: %w", err)
}
var res PutSuccess
err = res.ID.Decode(binID)
if err != nil {
return nil, fmt.Errorf("decode container ID: %w", err)
}
return res, nil
var cnr cid.ID
cnr.SetSHA256(pse.ContainerID)
return PutSuccess{
ID: cnr,
}, nil
}

View file

@ -4,8 +4,8 @@ import (
"crypto/sha256"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
@ -17,7 +17,7 @@ func TestParsePutSuccess(t *testing.T) {
}
_, err := ParsePutSuccess(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(2, len(prms)).Error())
require.Error(t, err)
})
t.Run("wrong container ID parameter", func(t *testing.T) {
@ -35,18 +35,30 @@ func TestParsePutSuccess(t *testing.T) {
id.Encode(binID)
t.Run("wrong public key parameter", func(t *testing.T) {
_, err := ParsePutSuccess(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewByteArray(binID),
stackitem.NewMap(),
}))
t.Run("wrong type", func(t *testing.T) {
_, err := ParsePutSuccess(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewByteArray(binID),
stackitem.NewMap(),
}))
require.Error(t, err)
require.Error(t, err)
})
t.Run("garbage data", func(t *testing.T) {
_, err := ParsePutSuccess(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewByteArray(binID),
stackitem.NewByteArray([]byte("key")),
}))
require.Error(t, err)
})
})
t.Run("correct behavior", func(t *testing.T) {
pk, err := keys.NewPrivateKey()
require.NoError(t, err)
ev, err := ParsePutSuccess(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewByteArray(binID),
stackitem.NewByteArray([]byte("key")),
stackitem.NewByteArray(pk.PublicKey().Bytes()),
}))
require.NoError(t, err)

View file

@ -1,9 +1,7 @@
package netmap
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/netmap"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -37,22 +35,13 @@ func (s NewEpoch) TxHash() util.Uint256 {
//
// Result is type of NewEpoch.
func ParseNewEpoch(e *state.ContainedNotificationEvent) (event.Event, error) {
params, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
}
if ln := len(params); ln != 1 {
return nil, event.WrongNumberOfParameters(1, ln)
}
prmEpochNum, err := client.IntFromStackItem(params[0])
if err != nil {
return nil, fmt.Errorf("could not get integer epoch number: %w", err)
var nee netmap.NewEpochEvent
if err := nee.FromStackItem(e.Item); err != nil {
return nil, err
}
return NewEpoch{
Num: uint64(prmEpochNum),
Num: uint64(nee.Epoch.Uint64()),
Hash: e.Container,
}, nil
}

View file

@ -4,7 +4,6 @@ import (
"math/big"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
@ -17,7 +16,7 @@ func TestParseNewEpoch(t *testing.T) {
}
_, err := ParseNewEpoch(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error())
require.Error(t, err)
})
t.Run("wrong first parameter type", func(t *testing.T) {