2021-04-18 08:51:49 +00:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
2021-04-23 14:49:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/common"
|
2021-11-02 13:26:39 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/internal/client"
|
2022-01-13 15:01:50 +00:00
|
|
|
coreclient "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2021-04-18 08:51:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
|
|
|
reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
|
2021-11-10 07:08:33 +00:00
|
|
|
reputationapi "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
2021-04-18 08:51:49 +00:00
|
|
|
)
|
|
|
|
|
2021-04-23 14:49:23 +00:00
|
|
|
// RemoteProviderPrm groups the required parameters of the RemoteProvider's constructor.
|
2021-04-18 08:51:49 +00:00
|
|
|
//
|
|
|
|
// All values must comply with the requirements imposed on them.
|
|
|
|
// Passing incorrect parameter values will result in constructor
|
|
|
|
// failure (error or panic depending on the implementation).
|
|
|
|
type RemoteProviderPrm struct {
|
2021-04-23 14:49:23 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 14:49:23 +00:00
|
|
|
// NewRemoteProvider creates a new instance of the RemoteProvider.
|
|
|
|
//
|
|
|
|
// Panics if at least one value of the parameters is invalid.
|
|
|
|
//
|
|
|
|
// The created RemoteProvider does not require additional
|
|
|
|
// initialization and is completely ready for work.
|
|
|
|
func NewRemoteProvider(prm RemoteProviderPrm) *RemoteProvider {
|
2021-04-18 08:51:49 +00:00
|
|
|
switch {
|
|
|
|
case prm.Key == nil:
|
2021-04-23 14:49:23 +00:00
|
|
|
common.PanicOnPrmValue("NetMapSource", prm.Key)
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 14:49:23 +00:00
|
|
|
return &RemoteProvider{
|
|
|
|
key: prm.Key,
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 14:49:23 +00:00
|
|
|
// RemoteProvider is an implementation of the clientKeyRemoteProvider interface.
|
|
|
|
type RemoteProvider struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
}
|
2021-04-18 08:51:49 +00:00
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
func (rp RemoteProvider) WithClient(c coreclient.Client) reputationcommon.WriterProvider {
|
2021-04-23 14:49:23 +00:00
|
|
|
return &TrustWriterProvider{
|
2021-04-18 08:51:49 +00:00
|
|
|
client: c,
|
2021-04-23 14:49:23 +00:00
|
|
|
key: rp.key,
|
|
|
|
}
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 14:49:23 +00:00
|
|
|
type TrustWriterProvider struct {
|
2022-01-13 15:01:50 +00:00
|
|
|
client coreclient.Client
|
2021-04-18 08:51:49 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2021-04-23 14:49:23 +00:00
|
|
|
func (twp *TrustWriterProvider) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) {
|
2021-04-18 08:51:49 +00:00
|
|
|
return &RemoteTrustWriter{
|
|
|
|
ctx: ctx,
|
2021-04-23 14:49:23 +00:00
|
|
|
client: twp.client,
|
|
|
|
key: twp.key,
|
2021-04-18 08:51:49 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoteTrustWriter struct {
|
|
|
|
ctx reputationcommon.Context
|
2022-01-13 15:01:50 +00:00
|
|
|
client coreclient.Client
|
2021-04-18 08:51:49 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2022-01-21 16:53:06 +00:00
|
|
|
buf []reputationapi.Trust
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 06:16:51 +00:00
|
|
|
func (rtp *RemoteTrustWriter) Write(t reputation.Trust) error {
|
2021-04-18 08:51:49 +00:00
|
|
|
apiTrust := reputationapi.NewTrust()
|
|
|
|
|
|
|
|
apiPeer := reputationapi.NewPeerID()
|
|
|
|
apiPeer.SetPublicKey(t.Peer())
|
|
|
|
|
|
|
|
apiTrust.SetValue(t.Value().Float64())
|
|
|
|
apiTrust.SetPeer(apiPeer)
|
|
|
|
|
2022-01-21 16:53:06 +00:00
|
|
|
rtp.buf = append(rtp.buf, *apiTrust)
|
2021-04-18 08:51:49 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rtp *RemoteTrustWriter) Close() error {
|
2021-11-02 13:26:39 +00:00
|
|
|
var prm internalclient.AnnounceLocalPrm
|
2021-04-18 08:51:49 +00:00
|
|
|
|
2021-11-02 13:26:39 +00:00
|
|
|
prm.SetContext(rtp.ctx)
|
|
|
|
prm.SetClient(rtp.client)
|
|
|
|
prm.SetPrivateKey(rtp.key)
|
2021-04-18 08:51:49 +00:00
|
|
|
prm.SetEpoch(rtp.ctx.Epoch())
|
|
|
|
prm.SetTrusts(rtp.buf)
|
|
|
|
|
2021-11-02 13:26:39 +00:00
|
|
|
_, err := internalclient.AnnounceLocal(prm)
|
2021-04-18 08:51:49 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|