2021-03-31 15:38:01 +00:00
|
|
|
package reputation
|
|
|
|
|
|
|
|
import (
|
2021-06-11 17:13:24 +00:00
|
|
|
"bytes"
|
2021-03-31 15:38:01 +00:00
|
|
|
"encoding/hex"
|
2021-06-11 17:13:24 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-03-31 15:38:01 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
repClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/reputation"
|
|
|
|
reputationEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/reputation"
|
|
|
|
apireputation "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/reputation"
|
2021-03-31 15:38:01 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-06-11 17:13:24 +00:00
|
|
|
var errWrongManager = errors.New("got manager that is incorrect for peer")
|
|
|
|
|
2021-09-08 15:20:02 +00:00
|
|
|
func (rp *Processor) processPut(e *reputationEvent.Put) {
|
2021-03-31 15:38:01 +00:00
|
|
|
if !rp.alphabetState.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
rp.log.Info(logs.ReputationNonAlphabetModeIgnoreReputationPutNotification)
|
2021-03-31 15:38:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:20:02 +00:00
|
|
|
epoch := e.Epoch()
|
|
|
|
id := e.PeerID()
|
|
|
|
value := e.Value()
|
|
|
|
|
2021-04-05 09:29:33 +00:00
|
|
|
// check if epoch is valid
|
|
|
|
currentEpoch := rp.epochState.EpochCounter()
|
|
|
|
if epoch >= currentEpoch {
|
2023-04-12 14:35:10 +00:00
|
|
|
rp.log.Info(logs.ReputationIgnoreReputationValue,
|
2021-04-05 09:29:33 +00:00
|
|
|
zap.String("reason", "invalid epoch number"),
|
|
|
|
zap.Uint64("trust_epoch", epoch),
|
|
|
|
zap.Uint64("local_epoch", currentEpoch))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check signature
|
2022-07-04 13:24:51 +00:00
|
|
|
if !value.VerifySignature() {
|
2023-04-12 14:35:10 +00:00
|
|
|
rp.log.Info(logs.ReputationIgnoreReputationValue,
|
2021-04-05 09:29:33 +00:00
|
|
|
zap.String("reason", "invalid signature"),
|
2022-07-04 13:24:51 +00:00
|
|
|
)
|
2021-04-05 09:29:33 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-11 17:13:24 +00:00
|
|
|
// check if manager is correct
|
2022-07-04 13:24:51 +00:00
|
|
|
if err := rp.checkManagers(epoch, value.Manager(), id); err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
rp.log.Info(logs.ReputationIgnoreReputationValue,
|
2021-06-11 17:13:24 +00:00
|
|
|
zap.String("reason", "wrong manager"),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-03-31 15:38:01 +00:00
|
|
|
|
2021-09-08 15:20:02 +00:00
|
|
|
rp.approvePutReputation(e)
|
2021-03-31 15:38:01 +00:00
|
|
|
}
|
2021-06-11 17:13:24 +00:00
|
|
|
|
|
|
|
func (rp *Processor) checkManagers(e uint64, mng apireputation.PeerID, peer apireputation.PeerID) error {
|
2022-07-04 13:24:51 +00:00
|
|
|
mm, err := rp.mngBuilder.BuildManagers(e, peer)
|
2021-06-11 17:13:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not build managers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range mm {
|
2022-12-23 17:35:35 +00:00
|
|
|
// FIXME: #1147 do not use `ToV2` method outside frostfs-api-go library
|
2022-07-04 13:24:51 +00:00
|
|
|
if bytes.Equal(mng.PublicKey(), m.PublicKey()) {
|
2021-06-11 17:13:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errWrongManager
|
|
|
|
}
|
2021-09-08 15:20:02 +00:00
|
|
|
|
|
|
|
func (rp *Processor) approvePutReputation(e *reputationEvent.Put) {
|
|
|
|
var (
|
|
|
|
id = e.PeerID()
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if nr := e.NotaryRequest(); nr != nil {
|
|
|
|
// put event was received via Notary service
|
|
|
|
err = rp.reputationWrp.Morph().NotarySignAndInvokeTX(nr.MainTransaction)
|
|
|
|
} else {
|
2022-01-31 12:19:10 +00:00
|
|
|
args := repClient.PutPrm{}
|
2021-09-08 15:20:02 +00:00
|
|
|
args.SetEpoch(e.Epoch())
|
|
|
|
args.SetPeerID(id)
|
|
|
|
args.SetValue(e.Value())
|
|
|
|
|
|
|
|
err = rp.reputationWrp.Put(args)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-12-23 17:35:35 +00:00
|
|
|
// FIXME: #1147 do not use `ToV2` method outside frostfs-api-go library
|
2023-04-12 14:35:10 +00:00
|
|
|
rp.log.Warn(logs.ReputationCantSendApprovalTxForReputationValue,
|
2022-07-04 13:24:51 +00:00
|
|
|
zap.String("peer_id", hex.EncodeToString(id.PublicKey())),
|
2021-09-08 15:20:02 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
}
|