2021-03-31 14:32:47 +00:00
|
|
|
package reputation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-22 10:06:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions"
|
2021-03-31 14:32:47 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/reputation"
|
2022-07-04 13:24:51 +00:00
|
|
|
reputationtest "github.com/nspcc-dev/neofs-sdk-go/reputation/test"
|
2021-03-31 14:32:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParsePut(t *testing.T) {
|
|
|
|
var (
|
2022-07-04 13:24:51 +00:00
|
|
|
peerID = reputationtest.PeerID()
|
2021-04-05 09:29:33 +00:00
|
|
|
|
|
|
|
value reputation.GlobalTrust
|
|
|
|
trust reputation.Trust
|
2022-07-04 13:24:51 +00:00
|
|
|
trustValue float64 = 0.64
|
2021-04-05 09:29:33 +00:00
|
|
|
|
2021-03-31 14:32:47 +00:00
|
|
|
epoch uint64 = 42
|
|
|
|
)
|
|
|
|
|
2021-04-05 09:29:33 +00:00
|
|
|
trust.SetValue(trustValue)
|
2022-07-04 13:24:51 +00:00
|
|
|
trust.SetPeer(peerID)
|
2021-04-05 09:29:33 +00:00
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
value.SetTrust(trust)
|
2021-04-05 09:29:33 +00:00
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
rawValue := value.Marshal()
|
2021-04-05 09:29:33 +00:00
|
|
|
|
2021-03-31 14:32:47 +00:00
|
|
|
t.Run("wrong number of parameters", func(t *testing.T) {
|
|
|
|
prms := []stackitem.Item{
|
|
|
|
stackitem.NewMap(),
|
|
|
|
stackitem.NewMap(),
|
|
|
|
}
|
|
|
|
|
2021-10-22 10:06:08 +00:00
|
|
|
_, err := ParsePut(createNotifyEventFromItems(prms))
|
2021-03-31 14:32:47 +00:00
|
|
|
require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("wrong epoch parameter", func(t *testing.T) {
|
2021-10-22 10:06:08 +00:00
|
|
|
_, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{
|
2021-03-31 14:32:47 +00:00
|
|
|
stackitem.NewMap(),
|
2021-10-22 10:06:08 +00:00
|
|
|
}))
|
2021-03-31 14:32:47 +00:00
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("wrong peerID parameter", func(t *testing.T) {
|
2021-10-22 10:06:08 +00:00
|
|
|
_, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{
|
2021-03-31 14:32:47 +00:00
|
|
|
stackitem.NewBigInteger(new(big.Int).SetUint64(epoch)),
|
|
|
|
stackitem.NewMap(),
|
2021-10-22 10:06:08 +00:00
|
|
|
}))
|
2021-03-31 14:32:47 +00:00
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("wrong value parameter", func(t *testing.T) {
|
2021-10-22 10:06:08 +00:00
|
|
|
_, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{
|
2021-03-31 14:32:47 +00:00
|
|
|
stackitem.NewBigInteger(new(big.Int).SetUint64(epoch)),
|
2022-07-04 13:24:51 +00:00
|
|
|
stackitem.NewByteArray(peerID.PublicKey()),
|
2021-03-31 14:32:47 +00:00
|
|
|
stackitem.NewMap(),
|
2021-10-22 10:06:08 +00:00
|
|
|
}))
|
2021-03-31 14:32:47 +00:00
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("correct behavior", func(t *testing.T) {
|
2021-10-22 10:06:08 +00:00
|
|
|
ev, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{
|
2021-03-31 14:32:47 +00:00
|
|
|
stackitem.NewBigInteger(new(big.Int).SetUint64(epoch)),
|
2022-07-04 13:24:51 +00:00
|
|
|
stackitem.NewByteArray(peerID.PublicKey()),
|
2021-04-05 09:29:33 +00:00
|
|
|
stackitem.NewByteArray(rawValue),
|
2021-10-22 10:06:08 +00:00
|
|
|
}))
|
2021-03-31 14:32:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, Put{
|
|
|
|
epoch: epoch,
|
|
|
|
peerID: peerID,
|
|
|
|
value: value,
|
|
|
|
}, ev)
|
|
|
|
})
|
|
|
|
}
|
2021-10-22 10:06:08 +00:00
|
|
|
|
|
|
|
func createNotifyEventFromItems(items []stackitem.Item) *subscriptions.NotificationEvent {
|
|
|
|
return &subscriptions.NotificationEvent{
|
|
|
|
NotificationEvent: state.NotificationEvent{
|
|
|
|
Item: stackitem.NewArray(items),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|