forked from TrueCloudLab/frostfs-node
[#1541] morph/event: Simplify container contract event parsing
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
d5c46d812a
commit
a353d45742
4 changed files with 39 additions and 61 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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,6 +35,7 @@ func TestParsePutSuccess(t *testing.T) {
|
|||
id.Encode(binID)
|
||||
|
||||
t.Run("wrong public key parameter", func(t *testing.T) {
|
||||
t.Run("wrong type", func(t *testing.T) {
|
||||
_, err := ParsePutSuccess(createNotifyEventFromItems([]stackitem.Item{
|
||||
stackitem.NewByteArray(binID),
|
||||
stackitem.NewMap(),
|
||||
|
@ -42,12 +43,23 @@ func TestParsePutSuccess(t *testing.T) {
|
|||
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("correct behavior", func(t *testing.T) {
|
||||
ev, err := ParsePutSuccess(createNotifyEventFromItems([]stackitem.Item{
|
||||
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(pk.PublicKey().Bytes()),
|
||||
}))
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
|
|
Loading…
Reference in a new issue