2021-04-23 14:49:23 +00:00
|
|
|
package intermediate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2021-04-29 05:30:41 +00:00
|
|
|
|
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-23 14:49:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
|
|
|
reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
|
|
|
|
eigentrustcalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator"
|
2021-03-01 19:01:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2021-11-10 07:08:33 +00:00
|
|
|
reputationapi "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
2021-03-01 19:01:45 +00:00
|
|
|
"go.uber.org/zap"
|
2021-04-23 14:49:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RemoteProviderPrm groups the required parameters of the RemoteProvider's constructor.
|
|
|
|
//
|
|
|
|
// 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 {
|
|
|
|
Key *ecdsa.PrivateKey
|
2021-03-01 19:01:45 +00:00
|
|
|
Log *logger.Logger
|
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 {
|
|
|
|
switch {
|
|
|
|
case prm.Key == nil:
|
|
|
|
common.PanicOnPrmValue("NetMapSource", prm.Key)
|
2021-03-01 19:01:45 +00:00
|
|
|
case prm.Log == nil:
|
|
|
|
common.PanicOnPrmValue("Logger", prm.Log)
|
2021-04-23 14:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &RemoteProvider{
|
|
|
|
key: prm.Key,
|
2021-03-01 19:01:45 +00:00
|
|
|
log: prm.Log,
|
2021-04-23 14:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteProvider is an implementation of the clientKeyRemoteProvider interface.
|
|
|
|
type RemoteProvider struct {
|
|
|
|
key *ecdsa.PrivateKey
|
2021-03-01 19:01:45 +00:00
|
|
|
log *logger.Logger
|
2021-04-23 14:49:23 +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{
|
|
|
|
client: c,
|
|
|
|
key: rp.key,
|
2021-03-01 19:01:45 +00:00
|
|
|
log: rp.log,
|
2021-04-23 14:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type TrustWriterProvider struct {
|
2022-01-13 15:01:50 +00:00
|
|
|
client coreclient.Client
|
2021-04-23 14:49:23 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2021-03-01 19:01:45 +00:00
|
|
|
log *logger.Logger
|
2021-04-23 14:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (twp *TrustWriterProvider) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) {
|
2021-05-04 06:16:51 +00:00
|
|
|
eiContext, ok := ctx.(eigentrustcalc.Context)
|
|
|
|
if !ok {
|
2022-02-07 13:34:02 +00:00
|
|
|
// TODO: #1164 think if this can be done without such limitation
|
2021-05-04 06:16:51 +00:00
|
|
|
panic(ErrIncorrectContextPanicMsg)
|
|
|
|
}
|
|
|
|
|
2021-04-23 14:49:23 +00:00
|
|
|
return &RemoteTrustWriter{
|
2021-05-04 06:16:51 +00:00
|
|
|
eiCtx: eiContext,
|
2021-04-23 14:49:23 +00:00
|
|
|
client: twp.client,
|
|
|
|
key: twp.key,
|
2021-03-01 19:01:45 +00:00
|
|
|
log: twp.log,
|
2021-04-23 14:49:23 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoteTrustWriter struct {
|
2021-05-04 06:16:51 +00:00
|
|
|
eiCtx eigentrustcalc.Context
|
2022-01-13 15:01:50 +00:00
|
|
|
client coreclient.Client
|
2021-04-23 14:49:23 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2021-03-01 19:01:45 +00:00
|
|
|
log *logger.Logger
|
2021-04-23 14:49:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Write sends a trust value to a remote node via ReputationService.AnnounceIntermediateResult RPC.
|
2021-05-04 06:16:51 +00:00
|
|
|
func (rtp *RemoteTrustWriter) Write(t reputation.Trust) error {
|
2021-03-01 19:01:45 +00:00
|
|
|
epoch := rtp.eiCtx.Epoch()
|
|
|
|
i := rtp.eiCtx.I()
|
|
|
|
|
|
|
|
rtp.log.Debug("announcing trust",
|
|
|
|
zap.Uint64("epoch", epoch),
|
|
|
|
zap.Uint32("iteration", i),
|
2022-07-04 13:24:51 +00:00
|
|
|
zap.Stringer("trusting_peer", t.TrustingPeer()),
|
|
|
|
zap.Stringer("trusted_peer", t.Peer()),
|
2021-03-01 19:01:45 +00:00
|
|
|
)
|
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
var apiTrust reputationapi.Trust
|
2021-04-23 14:49:23 +00:00
|
|
|
apiTrust.SetValue(t.Value().Float64())
|
2022-07-04 13:24:51 +00:00
|
|
|
apiTrust.SetPeer(t.Peer())
|
2021-04-23 14:49:23 +00:00
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
var apiPeerToPeerTrust reputationapi.PeerToPeerTrust
|
|
|
|
apiPeerToPeerTrust.SetTrustingPeer(t.TrustingPeer())
|
2021-04-23 14:49:23 +00:00
|
|
|
apiPeerToPeerTrust.SetTrust(apiTrust)
|
|
|
|
|
2021-11-02 13:26:39 +00:00
|
|
|
var p internalclient.AnnounceIntermediatePrm
|
|
|
|
|
|
|
|
p.SetContext(rtp.eiCtx)
|
|
|
|
p.SetClient(rtp.client)
|
2021-03-01 19:01:45 +00:00
|
|
|
p.SetEpoch(epoch)
|
|
|
|
p.SetIteration(i)
|
2022-07-04 13:24:51 +00:00
|
|
|
p.SetTrust(apiPeerToPeerTrust)
|
2021-04-23 14:49:23 +00:00
|
|
|
|
2021-11-02 13:26:39 +00:00
|
|
|
_, err := internalclient.AnnounceIntermediate(p)
|
2021-04-23 14:49:23 +00:00
|
|
|
|
2021-11-02 13:26:39 +00:00
|
|
|
return err
|
2021-04-23 14:49:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 13:26:39 +00:00
|
|
|
func (rtp *RemoteTrustWriter) Close() error {
|
|
|
|
return nil
|
2021-04-23 14:49:23 +00:00
|
|
|
}
|