2021-04-29 05:30:41 +00:00
|
|
|
package intermediate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
repClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
|
2021-04-29 05:30:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust"
|
|
|
|
eigentrustcalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2022-07-04 13:24:51 +00:00
|
|
|
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
|
2021-11-10 07:08:33 +00:00
|
|
|
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
|
2021-04-29 05:30:41 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FinalWriterProviderPrm groups the required parameters of the FinalWriterProvider'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 FinalWriterProviderPrm struct {
|
|
|
|
PrivatKey *ecdsa.PrivateKey
|
|
|
|
PubKey []byte
|
2022-01-31 12:19:10 +00:00
|
|
|
Client *repClient.Client
|
2021-04-29 05:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFinalWriterProvider creates a new instance of the FinalWriterProvider.
|
|
|
|
//
|
|
|
|
// Panics if at least one value of the parameters is invalid.
|
|
|
|
//
|
|
|
|
// The created FinalWriterProvider does not require additional
|
|
|
|
// initialization and is completely ready for work.
|
|
|
|
func NewFinalWriterProvider(prm FinalWriterProviderPrm, opts ...FinalWriterOption) *FinalWriterProvider {
|
|
|
|
o := defaultFinalWriterOptionsOpts()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](o)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FinalWriterProvider{
|
|
|
|
prm: prm,
|
|
|
|
opts: o,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// FinalWriterProvider is an implementation of the reputation.eigentrust.calculator
|
2021-04-29 06:33:09 +00:00
|
|
|
// IntermediateWriterProvider interface. It inits FinalWriter.
|
2021-04-29 05:30:41 +00:00
|
|
|
type FinalWriterProvider struct {
|
|
|
|
prm FinalWriterProviderPrm
|
|
|
|
opts *finalWriterOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fwp FinalWriterProvider) InitIntermediateWriter(
|
|
|
|
_ eigentrustcalc.Context) (eigentrustcalc.IntermediateWriter, error) {
|
|
|
|
return &FinalWriter{
|
|
|
|
privatKey: fwp.prm.PrivatKey,
|
|
|
|
pubKey: fwp.prm.PubKey,
|
|
|
|
client: fwp.prm.Client,
|
|
|
|
l: fwp.opts.log,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// FinalWriter is an implementation of the reputation.eigentrust.calculator IntermediateWriter
|
2021-04-29 06:33:09 +00:00
|
|
|
// interface that writes GlobalTrust to contract directly.
|
2021-04-29 05:30:41 +00:00
|
|
|
type FinalWriter struct {
|
|
|
|
privatKey *ecdsa.PrivateKey
|
|
|
|
pubKey []byte
|
2022-01-31 12:19:10 +00:00
|
|
|
client *repClient.Client
|
2021-04-29 05:30:41 +00:00
|
|
|
|
|
|
|
l *logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fw FinalWriter) WriteIntermediateTrust(t eigentrust.IterationTrust) error {
|
2021-03-01 19:01:45 +00:00
|
|
|
fw.l.Debug("start writing global trusts to contract")
|
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
args := repClient.PutPrm{}
|
2021-04-29 05:30:41 +00:00
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
apiTrustedPeerID := t.Peer()
|
2021-04-29 05:30:41 +00:00
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
var apiTrust apireputation.Trust
|
2021-04-29 05:30:41 +00:00
|
|
|
apiTrust.SetValue(t.Value().Float64())
|
2022-07-04 13:24:51 +00:00
|
|
|
apiTrust.SetPeer(t.Peer())
|
2021-04-29 05:30:41 +00:00
|
|
|
|
|
|
|
var managerPublicKey [33]byte
|
|
|
|
copy(managerPublicKey[:], fw.pubKey)
|
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
var apiMangerPeerID apireputation.PeerID
|
|
|
|
apiMangerPeerID.SetPublicKey(managerPublicKey[:])
|
2021-04-29 05:30:41 +00:00
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
var gTrust apireputation.GlobalTrust
|
2021-04-29 05:30:41 +00:00
|
|
|
gTrust.SetTrust(apiTrust)
|
|
|
|
gTrust.SetManager(apiMangerPeerID)
|
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
err := gTrust.Sign(neofsecdsa.Signer(*fw.privatKey))
|
2021-04-29 05:30:41 +00:00
|
|
|
if err != nil {
|
|
|
|
fw.l.Debug(
|
|
|
|
"failed to sign global trust",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
return fmt.Errorf("failed to sign global trust: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.SetEpoch(t.Epoch())
|
2022-07-04 13:24:51 +00:00
|
|
|
args.SetValue(gTrust)
|
|
|
|
args.SetPeerID(apiTrustedPeerID)
|
2021-04-29 05:30:41 +00:00
|
|
|
|
|
|
|
err = fw.client.Put(
|
|
|
|
args,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fw.l.Debug(
|
|
|
|
"failed to write global trust to contract",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
return fmt.Errorf("failed to write global trust to contract: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fw.l.Debug(
|
|
|
|
"sent global trust to contract",
|
|
|
|
zap.Uint64("epoch", t.Epoch()),
|
|
|
|
zap.Float64("value", t.Value().Float64()),
|
2022-07-04 13:24:51 +00:00
|
|
|
zap.Stringer("peer", t.Peer()),
|
2021-04-29 05:30:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type finalWriterOptions struct {
|
|
|
|
log *logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type FinalWriterOption func(*finalWriterOptions)
|
|
|
|
|
|
|
|
func defaultFinalWriterOptionsOpts() *finalWriterOptions {
|
|
|
|
return &finalWriterOptions{
|
2022-09-28 07:41:01 +00:00
|
|
|
log: &logger.Logger{Logger: zap.L()},
|
2021-04-29 05:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func FinalWriterWithLogger(l *logger.Logger) FinalWriterOption {
|
|
|
|
return func(o *finalWriterOptions) {
|
|
|
|
if l != nil {
|
|
|
|
o.log = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|