morph/event: Simplify frostfs contract event parsing
Some checks failed
Vulncheck / Vulncheck (pull_request) Successful in 2m46s
DCO action / DCO (pull_request) Failing after 4m44s
Pre-commit hooks / Pre-commit (pull_request) Successful in 5m4s
Tests and linters / gopls check (pull_request) Successful in 5m26s
Build / Build Components (pull_request) Successful in 6m32s
Tests and linters / Run gofumpt (pull_request) Successful in 6m36s
Tests and linters / Staticcheck (pull_request) Successful in 6m45s
Tests and linters / Lint (pull_request) Successful in 7m14s
Tests and linters / Tests (pull_request) Successful in 7m44s
Tests and linters / Tests with -race (pull_request) Successful in 8m3s

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-12-06 13:59:15 +03:00
parent 1c12f23b84
commit 69cb591c12
Signed by: fyrchik
SSH key fingerprint: SHA256:m/TTwCzjnRkXgnzEx9X92ccxy1CcVeinOgDb3NPWWmg
8 changed files with 52 additions and 173 deletions

View file

@ -3,7 +3,7 @@ package frostfs
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/frostfs"
"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"
@ -34,53 +34,20 @@ func (c Cheque) LockAccount() util.Uint160 { return c.LockValue }
// ParseCheque from notification into cheque structure.
func ParseCheque(e *state.ContainedNotificationEvent) (event.Event, error) {
var (
ev Cheque
err error
)
var ce frostfs.ChequeEvent
if err := ce.FromStackItem(e.Item); err != nil {
return nil, fmt.Errorf("parse frostfs.ChequeEvent: %w", err)
}
params, err := event.ParseStackArray(e)
lock, err := util.Uint160DecodeBytesBE(ce.LockAccount)
if err != nil {
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
return nil, fmt.Errorf("parse frostfs.ChequeEvent: field LockAccount: %w", err)
}
if ln := len(params); ln != 4 {
return nil, event.WrongNumberOfParameters(4, ln)
}
// parse id
ev.IDValue, err = client.BytesFromStackItem(params[0])
if err != nil {
return nil, fmt.Errorf("could not get cheque id: %w", err)
}
// parse user
user, err := client.BytesFromStackItem(params[1])
if err != nil {
return nil, fmt.Errorf("could not get cheque user: %w", err)
}
ev.UserValue, err = util.Uint160DecodeBytesBE(user)
if err != nil {
return nil, fmt.Errorf("could not convert cheque user to uint160: %w", err)
}
// parse amount
ev.AmountValue, err = client.IntFromStackItem(params[2])
if err != nil {
return nil, fmt.Errorf("could not get cheque amount: %w", err)
}
// parse lock account
lock, err := client.BytesFromStackItem(params[3])
if err != nil {
return nil, fmt.Errorf("could not get cheque lock account: %w", err)
}
ev.LockValue, err = util.Uint160DecodeBytesBE(lock)
if err != nil {
return nil, fmt.Errorf("could not convert cheque lock account to uint160: %w", err)
}
return ev, nil
return Cheque{
IDValue: ce.Id,
AmountValue: ce.Amount.Int64(),
UserValue: ce.User,
LockValue: lock,
}, 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"
@ -27,7 +26,7 @@ func TestParseCheque(t *testing.T) {
}
_, err := ParseCheque(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(4, len(prms)).Error())
require.Error(t, err)
})
t.Run("wrong id parameter", func(t *testing.T) {

View file

@ -3,7 +3,7 @@ package frostfs
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/frostfs"
"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"
@ -36,39 +36,15 @@ func (u Config) Key() []byte { return u.KeyValue }
func (u Config) Value() []byte { return u.ValueValue }
func ParseConfig(e *state.ContainedNotificationEvent) (event.Event, error) {
var (
ev Config
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 sce frostfs.SetConfigEvent
if err := sce.FromStackItem(e.Item); err != nil {
return nil, fmt.Errorf("parse frostfs.SetConfigEvent: %w", err)
}
if ln := len(params); ln != 3 {
return nil, event.WrongNumberOfParameters(3, ln)
}
// parse id
ev.IDValue, err = client.BytesFromStackItem(params[0])
if err != nil {
return nil, fmt.Errorf("could not get config update id: %w", err)
}
// parse key
ev.KeyValue, err = client.BytesFromStackItem(params[1])
if err != nil {
return nil, fmt.Errorf("could not get config key: %w", err)
}
// parse value
ev.ValueValue, err = client.BytesFromStackItem(params[2])
if err != nil {
return nil, fmt.Errorf("could not get config value: %w", err)
}
ev.TxHashValue = e.Container
return ev, nil
return Config{
KeyValue: sce.Key,
ValueValue: sce.Value,
IDValue: sce.Id,
TxHashValue: e.Container,
}, nil
}

View file

@ -3,7 +3,6 @@ package frostfs
import (
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
@ -21,7 +20,7 @@ func TestParseConfig(t *testing.T) {
}
_, err := ParseConfig(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error())
require.Error(t, err)
})
t.Run("wrong first parameter", func(t *testing.T) {

View file

@ -3,7 +3,7 @@ package frostfs
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/frostfs"
"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"
@ -34,50 +34,15 @@ func (d Deposit) Amount() int64 { return d.AmountValue }
// ParseDeposit notification into deposit structure.
func ParseDeposit(e *state.ContainedNotificationEvent) (event.Event, error) {
var ev Deposit
params, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
var de frostfs.DepositEvent
if err := de.FromStackItem(e.Item); err != nil {
return nil, fmt.Errorf("parse frostfs.DepositEvent: %w", err)
}
if ln := len(params); ln != 4 {
return nil, event.WrongNumberOfParameters(4, ln)
}
// parse from
from, err := client.BytesFromStackItem(params[0])
if err != nil {
return nil, fmt.Errorf("could not get deposit sender: %w", err)
}
ev.FromValue, err = util.Uint160DecodeBytesBE(from)
if err != nil {
return nil, fmt.Errorf("could not convert deposit sender to uint160: %w", err)
}
// parse amount
ev.AmountValue, err = client.IntFromStackItem(params[1])
if err != nil {
return nil, fmt.Errorf("could not get deposit amount: %w", err)
}
// parse to
to, err := client.BytesFromStackItem(params[2])
if err != nil {
return nil, fmt.Errorf("could not get deposit receiver: %w", err)
}
ev.ToValue, err = util.Uint160DecodeBytesBE(to)
if err != nil {
return nil, fmt.Errorf("could not convert deposit receiver to uint160: %w", err)
}
// parse id
ev.IDValue, err = client.BytesFromStackItem(params[3])
if err != nil {
return nil, fmt.Errorf("could not get deposit id: %w", err)
}
return ev, nil
return Deposit{
IDValue: de.TxHash[:],
AmountValue: de.Amount.Int64(),
FromValue: de.From,
ToValue: de.Receiver,
}, 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/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
@ -12,7 +11,7 @@ import (
func TestParseDeposit(t *testing.T) {
var (
id = []byte("Hello World")
id = util.Uint256{0, 1, 2, 3}
from = util.Uint160{0x1, 0x2, 0x3}
to = util.Uint160{0x3, 0x2, 0x1}
@ -26,7 +25,7 @@ func TestParseDeposit(t *testing.T) {
}
_, err := ParseDeposit(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(4, len(prms)).Error())
require.Error(t, err)
})
t.Run("wrong from parameter", func(t *testing.T) {
@ -72,12 +71,12 @@ func TestParseDeposit(t *testing.T) {
stackitem.NewByteArray(from.BytesBE()),
stackitem.NewBigInteger(new(big.Int).SetInt64(amount)),
stackitem.NewByteArray(to.BytesBE()),
stackitem.NewByteArray(id),
stackitem.NewByteArray(id[:]),
}))
require.NoError(t, err)
require.Equal(t, Deposit{
IDValue: id,
IDValue: id[:],
AmountValue: amount,
FromValue: from,
ToValue: to,

View file

@ -3,7 +3,7 @@ package frostfs
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/frostfs"
"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"
@ -30,39 +30,14 @@ func (w Withdraw) Amount() int64 { return w.AmountValue }
// ParseWithdraw notification into withdraw structure.
func ParseWithdraw(e *state.ContainedNotificationEvent) (event.Event, error) {
var ev Withdraw
params, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
var we frostfs.WithdrawEvent
if err := we.FromStackItem(e.Item); err != nil {
return nil, fmt.Errorf("parse frostfs.WithdrawEvent: %w", err)
}
if ln := len(params); ln != 3 {
return nil, event.WrongNumberOfParameters(3, ln)
}
// parse user
user, err := client.BytesFromStackItem(params[0])
if err != nil {
return nil, fmt.Errorf("could not get withdraw user: %w", err)
}
ev.UserValue, err = util.Uint160DecodeBytesBE(user)
if err != nil {
return nil, fmt.Errorf("could not convert withdraw user to uint160: %w", err)
}
// parse amount
ev.AmountValue, err = client.IntFromStackItem(params[1])
if err != nil {
return nil, fmt.Errorf("could not get withdraw amount: %w", err)
}
// parse id
ev.IDValue, err = client.BytesFromStackItem(params[2])
if err != nil {
return nil, fmt.Errorf("could not get withdraw id: %w", err)
}
return ev, nil
return Withdraw{
IDValue: we.TxHash[:],
AmountValue: we.Amount.Int64(),
UserValue: we.User,
}, 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/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
@ -12,7 +11,7 @@ import (
func TestParseWithdraw(t *testing.T) {
var (
id = []byte("Hello World")
id = util.Uint256{1, 2, 3}
user = util.Uint160{0x1, 0x2, 0x3}
amount int64 = 10
@ -25,7 +24,7 @@ func TestParseWithdraw(t *testing.T) {
}
_, err := ParseWithdraw(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error())
require.Error(t, err)
})
t.Run("wrong user parameter", func(t *testing.T) {
@ -59,12 +58,12 @@ func TestParseWithdraw(t *testing.T) {
ev, err := ParseWithdraw(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewByteArray(user.BytesBE()),
stackitem.NewBigInteger(new(big.Int).SetInt64(amount)),
stackitem.NewByteArray(id),
stackitem.NewByteArray(id[:]),
}))
require.NoError(t, err)
require.Equal(t, Withdraw{
IDValue: id,
IDValue: id[:],
AmountValue: amount,
UserValue: user,
}, ev)