forked from TrueCloudLab/frostfs-node
[#1545] morph/event: Simplify frostfs contract event parsing
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
1c12f23b84
commit
d1bc4351c3
8 changed files with 52 additions and 173 deletions
|
@ -3,7 +3,7 @@ package frostfs
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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"
|
"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/core/state"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"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.
|
// ParseCheque from notification into cheque structure.
|
||||||
func ParseCheque(e *state.ContainedNotificationEvent) (event.Event, error) {
|
func ParseCheque(e *state.ContainedNotificationEvent) (event.Event, error) {
|
||||||
var (
|
var ce frostfs.ChequeEvent
|
||||||
ev Cheque
|
if err := ce.FromStackItem(e.Item); err != nil {
|
||||||
err error
|
return nil, fmt.Errorf("parse frostfs.ChequeEvent: %w", err)
|
||||||
)
|
}
|
||||||
|
|
||||||
params, err := event.ParseStackArray(e)
|
lock, err := util.Uint160DecodeBytesBE(ce.LockAccount)
|
||||||
if err != nil {
|
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 Cheque{
|
||||||
return nil, event.WrongNumberOfParameters(4, ln)
|
IDValue: ce.Id,
|
||||||
}
|
AmountValue: ce.Amount.Int64(),
|
||||||
|
UserValue: ce.User,
|
||||||
// parse id
|
LockValue: lock,
|
||||||
ev.IDValue, err = client.BytesFromStackItem(params[0])
|
}, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"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/core/state"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||||
|
@ -27,7 +26,7 @@ func TestParseCheque(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ParseCheque(createNotifyEventFromItems(prms))
|
_, 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) {
|
t.Run("wrong id parameter", func(t *testing.T) {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package frostfs
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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"
|
"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/core/state"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"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 (u Config) Value() []byte { return u.ValueValue }
|
||||||
|
|
||||||
func ParseConfig(e *state.ContainedNotificationEvent) (event.Event, error) {
|
func ParseConfig(e *state.ContainedNotificationEvent) (event.Event, error) {
|
||||||
var (
|
var sce frostfs.SetConfigEvent
|
||||||
ev Config
|
if err := sce.FromStackItem(e.Item); err != nil {
|
||||||
err error
|
return nil, fmt.Errorf("parse frostfs.SetConfigEvent: %w", err)
|
||||||
)
|
|
||||||
|
|
||||||
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 != 3 {
|
return Config{
|
||||||
return nil, event.WrongNumberOfParameters(3, ln)
|
KeyValue: sce.Key,
|
||||||
}
|
ValueValue: sce.Value,
|
||||||
|
IDValue: sce.Id,
|
||||||
// parse id
|
TxHashValue: e.Container,
|
||||||
ev.IDValue, err = client.BytesFromStackItem(params[0])
|
}, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package frostfs
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
@ -21,7 +20,7 @@ func TestParseConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ParseConfig(createNotifyEventFromItems(prms))
|
_, 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) {
|
t.Run("wrong first parameter", func(t *testing.T) {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package frostfs
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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"
|
"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/core/state"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"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.
|
// ParseDeposit notification into deposit structure.
|
||||||
func ParseDeposit(e *state.ContainedNotificationEvent) (event.Event, error) {
|
func ParseDeposit(e *state.ContainedNotificationEvent) (event.Event, error) {
|
||||||
var ev Deposit
|
var de frostfs.DepositEvent
|
||||||
|
if err := de.FromStackItem(e.Item); err != nil {
|
||||||
params, err := event.ParseStackArray(e)
|
return nil, fmt.Errorf("parse frostfs.DepositEvent: %w", err)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ln := len(params); ln != 4 {
|
return Deposit{
|
||||||
return nil, event.WrongNumberOfParameters(4, ln)
|
IDValue: de.TxHash[:],
|
||||||
}
|
AmountValue: de.Amount.Int64(),
|
||||||
|
FromValue: de.From,
|
||||||
// parse from
|
ToValue: de.Receiver,
|
||||||
from, err := client.BytesFromStackItem(params[0])
|
}, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"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/util"
|
||||||
"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"
|
||||||
|
@ -12,7 +11,7 @@ import (
|
||||||
|
|
||||||
func TestParseDeposit(t *testing.T) {
|
func TestParseDeposit(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
id = []byte("Hello World")
|
id = util.Uint256{0, 1, 2, 3}
|
||||||
from = util.Uint160{0x1, 0x2, 0x3}
|
from = util.Uint160{0x1, 0x2, 0x3}
|
||||||
to = util.Uint160{0x3, 0x2, 0x1}
|
to = util.Uint160{0x3, 0x2, 0x1}
|
||||||
|
|
||||||
|
@ -26,7 +25,7 @@ func TestParseDeposit(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ParseDeposit(createNotifyEventFromItems(prms))
|
_, 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) {
|
t.Run("wrong from parameter", func(t *testing.T) {
|
||||||
|
@ -72,12 +71,12 @@ func TestParseDeposit(t *testing.T) {
|
||||||
stackitem.NewByteArray(from.BytesBE()),
|
stackitem.NewByteArray(from.BytesBE()),
|
||||||
stackitem.NewBigInteger(new(big.Int).SetInt64(amount)),
|
stackitem.NewBigInteger(new(big.Int).SetInt64(amount)),
|
||||||
stackitem.NewByteArray(to.BytesBE()),
|
stackitem.NewByteArray(to.BytesBE()),
|
||||||
stackitem.NewByteArray(id),
|
stackitem.NewByteArray(id[:]),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, Deposit{
|
require.Equal(t, Deposit{
|
||||||
IDValue: id,
|
IDValue: id[:],
|
||||||
AmountValue: amount,
|
AmountValue: amount,
|
||||||
FromValue: from,
|
FromValue: from,
|
||||||
ToValue: to,
|
ToValue: to,
|
||||||
|
|
|
@ -3,7 +3,7 @@ package frostfs
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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"
|
"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/core/state"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"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.
|
// ParseWithdraw notification into withdraw structure.
|
||||||
func ParseWithdraw(e *state.ContainedNotificationEvent) (event.Event, error) {
|
func ParseWithdraw(e *state.ContainedNotificationEvent) (event.Event, error) {
|
||||||
var ev Withdraw
|
var we frostfs.WithdrawEvent
|
||||||
|
if err := we.FromStackItem(e.Item); err != nil {
|
||||||
params, err := event.ParseStackArray(e)
|
return nil, fmt.Errorf("parse frostfs.WithdrawEvent: %w", err)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ln := len(params); ln != 3 {
|
return Withdraw{
|
||||||
return nil, event.WrongNumberOfParameters(3, ln)
|
IDValue: we.TxHash[:],
|
||||||
}
|
AmountValue: we.Amount.Int64(),
|
||||||
|
UserValue: we.User,
|
||||||
// parse user
|
}, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"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/util"
|
||||||
"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"
|
||||||
|
@ -12,7 +11,7 @@ import (
|
||||||
|
|
||||||
func TestParseWithdraw(t *testing.T) {
|
func TestParseWithdraw(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
id = []byte("Hello World")
|
id = util.Uint256{1, 2, 3}
|
||||||
user = util.Uint160{0x1, 0x2, 0x3}
|
user = util.Uint160{0x1, 0x2, 0x3}
|
||||||
|
|
||||||
amount int64 = 10
|
amount int64 = 10
|
||||||
|
@ -25,7 +24,7 @@ func TestParseWithdraw(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ParseWithdraw(createNotifyEventFromItems(prms))
|
_, 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) {
|
t.Run("wrong user parameter", func(t *testing.T) {
|
||||||
|
@ -59,12 +58,12 @@ func TestParseWithdraw(t *testing.T) {
|
||||||
ev, err := ParseWithdraw(createNotifyEventFromItems([]stackitem.Item{
|
ev, err := ParseWithdraw(createNotifyEventFromItems([]stackitem.Item{
|
||||||
stackitem.NewByteArray(user.BytesBE()),
|
stackitem.NewByteArray(user.BytesBE()),
|
||||||
stackitem.NewBigInteger(new(big.Int).SetInt64(amount)),
|
stackitem.NewBigInteger(new(big.Int).SetInt64(amount)),
|
||||||
stackitem.NewByteArray(id),
|
stackitem.NewByteArray(id[:]),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, Withdraw{
|
require.Equal(t, Withdraw{
|
||||||
IDValue: id,
|
IDValue: id[:],
|
||||||
AmountValue: amount,
|
AmountValue: amount,
|
||||||
UserValue: user,
|
UserValue: user,
|
||||||
}, ev)
|
}, ev)
|
||||||
|
|
Loading…
Reference in a new issue