2021-03-24 08:29:02 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/reputation"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/rpc/client"
|
|
|
|
v2reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation"
|
|
|
|
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reputation contains methods for working with Reputation system values.
|
|
|
|
type Reputation interface {
|
2021-05-07 09:34:24 +00:00
|
|
|
// AnnounceLocalTrust announces local trust values of local peer.
|
|
|
|
AnnounceLocalTrust(context.Context, AnnounceLocalTrustPrm, ...CallOption) (*AnnounceLocalTrustRes, error)
|
2021-04-01 15:32:13 +00:00
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
// AnnounceIntermediateTrust announces the intermediate result of the iterative algorithm for calculating
|
2021-04-01 15:32:13 +00:00
|
|
|
// the global reputation of the node.
|
2021-05-07 09:34:24 +00:00
|
|
|
AnnounceIntermediateTrust(context.Context, AnnounceIntermediateTrustPrm, ...CallOption) (*AnnounceIntermediateTrustRes, error)
|
2021-03-24 08:29:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
// AnnounceLocalTrustPrm groups parameters of AnnounceLocalTrust operation.
|
|
|
|
type AnnounceLocalTrustPrm struct {
|
2021-03-24 08:29:02 +00:00
|
|
|
epoch uint64
|
|
|
|
|
|
|
|
trusts []*reputation.Trust
|
|
|
|
}
|
|
|
|
|
|
|
|
// Epoch returns epoch in which the trust was assessed.
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x AnnounceLocalTrustPrm) Epoch() uint64 {
|
2021-03-24 08:29:02 +00:00
|
|
|
return x.epoch
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEpoch sets epoch in which the trust was assessed.
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x *AnnounceLocalTrustPrm) SetEpoch(epoch uint64) {
|
2021-03-24 08:29:02 +00:00
|
|
|
x.epoch = epoch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trusts returns list of local trust values.
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x AnnounceLocalTrustPrm) Trusts() []*reputation.Trust {
|
2021-03-24 08:29:02 +00:00
|
|
|
return x.trusts
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTrusts sets list of local trust values.
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x *AnnounceLocalTrustPrm) SetTrusts(trusts []*reputation.Trust) {
|
2021-03-24 08:29:02 +00:00
|
|
|
x.trusts = trusts
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
// AnnounceLocalTrustRes groups results of AnnounceLocalTrust operation.
|
|
|
|
type AnnounceLocalTrustRes struct{}
|
2021-03-24 08:29:02 +00:00
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
func (c *clientImpl) AnnounceLocalTrust(ctx context.Context, prm AnnounceLocalTrustPrm, opts ...CallOption) (*AnnounceLocalTrustRes, error) {
|
2021-03-24 08:29:02 +00:00
|
|
|
// apply all available options
|
|
|
|
callOptions := c.defaultCallOptions()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](callOptions)
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
reqBody := new(v2reputation.AnnounceLocalTrustRequestBody)
|
2021-03-24 08:29:02 +00:00
|
|
|
reqBody.SetEpoch(prm.Epoch())
|
|
|
|
reqBody.SetTrusts(reputation.TrustsToV2(prm.Trusts()))
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
req := new(v2reputation.AnnounceLocalTrustRequest)
|
2021-03-24 08:29:02 +00:00
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(callOptions.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
resp, err := rpcapi.AnnounceLocalTrust(c.Raw(), req, client.WithContext(ctx))
|
2021-03-24 08:29:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
return new(AnnounceLocalTrustRes), nil
|
2021-03-24 08:29:02 +00:00
|
|
|
}
|
2021-04-01 15:32:13 +00:00
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
// AnnounceIntermediateTrustPrm groups parameters of AnnounceIntermediateTrust operation.
|
|
|
|
type AnnounceIntermediateTrustPrm struct {
|
2021-04-22 08:09:36 +00:00
|
|
|
epoch uint64
|
|
|
|
|
2021-04-01 15:32:13 +00:00
|
|
|
iter uint32
|
|
|
|
|
2021-04-09 08:41:58 +00:00
|
|
|
trust *reputation.PeerToPeerTrust
|
2021-04-01 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x *AnnounceIntermediateTrustPrm) Epoch() uint64 {
|
2021-04-22 08:09:36 +00:00
|
|
|
return x.epoch
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x *AnnounceIntermediateTrustPrm) SetEpoch(epoch uint64) {
|
2021-04-22 08:09:36 +00:00
|
|
|
x.epoch = epoch
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:32:13 +00:00
|
|
|
// Iteration returns sequence number of the iteration.
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x AnnounceIntermediateTrustPrm) Iteration() uint32 {
|
2021-04-01 15:32:13 +00:00
|
|
|
return x.iter
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIteration sets sequence number of the iteration.
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x *AnnounceIntermediateTrustPrm) SetIteration(iter uint32) {
|
2021-04-01 15:32:13 +00:00
|
|
|
x.iter = iter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trust returns current global trust value computed at the specified iteration.
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x AnnounceIntermediateTrustPrm) Trust() *reputation.PeerToPeerTrust {
|
2021-04-01 15:32:13 +00:00
|
|
|
return x.trust
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTrust sets current global trust value computed at the specified iteration.
|
2021-05-07 09:34:24 +00:00
|
|
|
func (x *AnnounceIntermediateTrustPrm) SetTrust(trust *reputation.PeerToPeerTrust) {
|
2021-04-01 15:32:13 +00:00
|
|
|
x.trust = trust
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
// AnnounceIntermediateTrustRes groups results of AnnounceIntermediateTrust operation.
|
|
|
|
type AnnounceIntermediateTrustRes struct{}
|
2021-04-01 15:32:13 +00:00
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
func (c *clientImpl) AnnounceIntermediateTrust(ctx context.Context, prm AnnounceIntermediateTrustPrm, opts ...CallOption) (*AnnounceIntermediateTrustRes, error) {
|
2021-04-01 15:32:13 +00:00
|
|
|
// apply all available options
|
|
|
|
callOptions := c.defaultCallOptions()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](callOptions)
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
reqBody := new(v2reputation.AnnounceIntermediateResultRequestBody)
|
2021-04-22 08:09:36 +00:00
|
|
|
reqBody.SetEpoch(prm.Epoch())
|
2021-04-01 15:32:13 +00:00
|
|
|
reqBody.SetIteration(prm.Iteration())
|
|
|
|
reqBody.SetTrust(prm.Trust().ToV2())
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
req := new(v2reputation.AnnounceIntermediateResultRequest)
|
2021-04-01 15:32:13 +00:00
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(callOptions.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
resp, err := rpcapi.AnnounceIntermediateResult(c.Raw(), req, client.WithContext(ctx))
|
2021-04-01 15:32:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
return new(AnnounceIntermediateTrustRes), nil
|
2021-04-01 15:32:13 +00:00
|
|
|
}
|