[#1541] morph/event: Simplify container contract event parsing

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-12-05 11:52:30 +03:00 committed by Evgenii Stratonikov
parent d5c46d812a
commit a353d45742
4 changed files with 39 additions and 61 deletions

View file

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

View file

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

View file

@ -3,7 +3,7 @@ package container
import ( import (
"fmt" "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" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"github.com/nspcc-dev/neo-go/pkg/core/state" "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 // ParsePutSuccess decodes notification event thrown by Container contract into
// PutSuccess and returns it as event.Event. // PutSuccess and returns it as event.Event.
func ParsePutSuccess(e *state.ContainedNotificationEvent) (event.Event, error) { func ParsePutSuccess(e *state.ContainedNotificationEvent) (event.Event, error) {
items, err := event.ParseStackArray(e) var pse container.PutSuccessEvent
if err != nil { if err := pse.FromStackItem(e.Item); err != nil {
return nil, fmt.Errorf("parse stack array from raw notification event: %w", err) return nil, fmt.Errorf("parse container.PutSuccessEvent: %w", err)
} }
const expectedItemNumPutSuccess = 2 var cnr cid.ID
cnr.SetSHA256(pse.ContainerID)
if ln := len(items); ln != expectedItemNumPutSuccess { return PutSuccess{
return nil, event.WrongNumberOfParameters(expectedItemNumPutSuccess, ln) ID: cnr,
} }, nil
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
} }

View file

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