forked from TrueCloudLab/frostfs-node
[#1541] morph/event: Simplify balance contract event parsing
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
7df3520d48
commit
d5d5ce2074
2 changed files with 13 additions and 58 deletions
|
@ -3,7 +3,7 @@ package balance
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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"
|
"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"
|
||||||
|
@ -47,61 +47,17 @@ func (l Lock) TxHash() util.Uint256 { return l.txHash }
|
||||||
|
|
||||||
// ParseLock from notification into lock structure.
|
// ParseLock from notification into lock structure.
|
||||||
func ParseLock(e *state.ContainedNotificationEvent) (event.Event, error) {
|
func ParseLock(e *state.ContainedNotificationEvent) (event.Event, error) {
|
||||||
var (
|
var le balance.LockEvent
|
||||||
ev Lock
|
if err := le.FromStackItem(e.Item); err != nil {
|
||||||
err error
|
return nil, fmt.Errorf("parse balance.LockEvent: %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 != 5 {
|
return Lock{
|
||||||
return nil, event.WrongNumberOfParameters(5, ln)
|
id: le.TxID,
|
||||||
}
|
user: le.From,
|
||||||
|
lock: le.To,
|
||||||
// parse id
|
amount: le.Amount.Int64(),
|
||||||
ev.id, err = client.BytesFromStackItem(params[0])
|
until: le.Until.Int64(),
|
||||||
if err != nil {
|
txHash: e.Container,
|
||||||
return nil, fmt.Errorf("could not get lock id: %w", err)
|
}, nil
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"
|
||||||
|
@ -28,7 +27,7 @@ func TestParseLock(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ParseLock(createNotifyEventFromItems(prms))
|
_, 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) {
|
t.Run("wrong id parameter", func(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue