2021-06-17 15:01:21 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package reputation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DB is an interface for storing reputation data.
|
|
|
|
type DB interface {
|
|
|
|
Update(ctx context.Context, request UpdateRequest, now time.Time) (_ *overlay.ReputationStatus, changed bool, err error)
|
2021-06-23 00:09:39 +01:00
|
|
|
SetNodeStatus(ctx context.Context, id storj.NodeID, status overlay.ReputationStatus) error
|
2021-06-17 15:01:21 +01:00
|
|
|
Get(ctx context.Context, nodeID storj.NodeID) (*Info, error)
|
2021-06-23 00:09:39 +01:00
|
|
|
|
|
|
|
// UnsuspendNodeUnknownAudit unsuspends a storage node for unknown audits.
|
2021-07-13 23:27:50 +01:00
|
|
|
UnsuspendNodeUnknownAudit(ctx context.Context, nodeID storj.NodeID) (err error)
|
2021-06-23 00:09:39 +01:00
|
|
|
// DisqualifyNode disqualifies a storage node.
|
2021-07-13 23:27:50 +01:00
|
|
|
DisqualifyNode(ctx context.Context, nodeID storj.NodeID) (err error)
|
2021-06-23 00:09:39 +01:00
|
|
|
// SuspendNodeUnknownAudit suspends a storage node for unknown audits.
|
2021-07-13 23:27:50 +01:00
|
|
|
SuspendNodeUnknownAudit(ctx context.Context, nodeID storj.NodeID, suspendedAt time.Time) (err error)
|
2021-07-28 23:29:46 +01:00
|
|
|
// UpdateAuditHistory updates a node's audit history
|
2021-08-04 03:01:19 +01:00
|
|
|
UpdateAuditHistory(ctx context.Context, oldHistory []byte, updateReq UpdateRequest, auditTime time.Time) (res *UpdateAuditHistoryResponse, err error)
|
2021-06-17 15:01:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info contains all reputation data to be stored in DB.
|
|
|
|
type Info struct {
|
|
|
|
AuditSuccessCount int64
|
|
|
|
TotalAuditCount int64
|
|
|
|
VettedAt *time.Time
|
|
|
|
Disqualified *time.Time
|
|
|
|
Suspended *time.Time
|
|
|
|
UnknownAuditSuspended *time.Time
|
|
|
|
OfflineSuspended *time.Time
|
|
|
|
UnderReview *time.Time
|
|
|
|
OnlineScore float64
|
|
|
|
AuditHistory AuditHistory
|
|
|
|
AuditReputationAlpha float64
|
|
|
|
AuditReputationBeta float64
|
|
|
|
UnknownAuditReputationAlpha float64
|
|
|
|
UnknownAuditReputationBeta float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service handles storing node reputation data and updating
|
|
|
|
// the overlay cache when a node's status changes.
|
|
|
|
type Service struct {
|
|
|
|
log *zap.Logger
|
2021-07-13 23:27:50 +01:00
|
|
|
overlay overlay.DB
|
2021-06-17 15:01:21 +01:00
|
|
|
db DB
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new reputation service.
|
2021-07-13 23:27:50 +01:00
|
|
|
func NewService(log *zap.Logger, overlay overlay.DB, db DB, config Config) *Service {
|
2021-06-17 15:01:21 +01:00
|
|
|
return &Service{
|
|
|
|
log: log,
|
|
|
|
overlay: overlay,
|
|
|
|
db: db,
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyAudit receives an audit result and applies it to the relevant node in DB.
|
|
|
|
func (service *Service) ApplyAudit(ctx context.Context, nodeID storj.NodeID, result AuditType) (err error) {
|
2021-08-02 18:48:55 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2021-06-17 15:01:21 +01:00
|
|
|
|
|
|
|
statusUpdate, changed, err := service.db.Update(ctx, UpdateRequest{
|
|
|
|
NodeID: nodeID,
|
|
|
|
AuditOutcome: result,
|
|
|
|
|
|
|
|
AuditLambda: service.config.AuditLambda,
|
|
|
|
AuditWeight: service.config.AuditWeight,
|
|
|
|
AuditDQ: service.config.AuditDQ,
|
|
|
|
SuspensionGracePeriod: service.config.SuspensionGracePeriod,
|
|
|
|
SuspensionDQEnabled: service.config.SuspensionDQEnabled,
|
|
|
|
AuditsRequiredForVetting: service.config.AuditCount,
|
|
|
|
AuditHistory: service.config.AuditHistory,
|
|
|
|
}, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if changed {
|
|
|
|
err = service.overlay.UpdateReputation(ctx, nodeID, statusUpdate)
|
2021-06-23 00:09:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-17 15:01:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a node's reputation info from DB.
|
2021-08-02 18:48:55 +01:00
|
|
|
// If a node is not found in the DB, default reputation information is returned.
|
2021-06-17 15:01:21 +01:00
|
|
|
func (service *Service) Get(ctx context.Context, nodeID storj.NodeID) (info *Info, err error) {
|
2021-08-02 18:48:55 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
info, err = service.db.Get(ctx, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
if ErrNodeNotFound.Has(err) {
|
|
|
|
// if there is no audit reputation for the node, that's fine and we
|
|
|
|
// return default reputation values
|
|
|
|
info = &Info{
|
|
|
|
UnknownAuditReputationAlpha: 1,
|
|
|
|
AuditReputationAlpha: 1,
|
|
|
|
OnlineScore: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
2021-06-17 15:01:21 +01:00
|
|
|
}
|
|
|
|
|
2021-07-07 20:20:23 +01:00
|
|
|
// TestSuspendNodeUnknownAudit suspends a storage node for unknown audits.
|
|
|
|
func (service *Service) TestSuspendNodeUnknownAudit(ctx context.Context, nodeID storj.NodeID, suspendedAt time.Time) (err error) {
|
2021-07-13 23:27:50 +01:00
|
|
|
err = service.db.SuspendNodeUnknownAudit(ctx, nodeID, suspendedAt)
|
2021-06-23 00:09:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-15 15:14:13 +01:00
|
|
|
return service.overlay.TestSuspendNodeUnknownAudit(ctx, nodeID, suspendedAt)
|
2021-06-17 15:01:21 +01:00
|
|
|
}
|
2021-06-23 00:09:39 +01:00
|
|
|
|
2021-07-13 23:27:50 +01:00
|
|
|
// TestDisqualifyNode disqualifies a storage node.
|
|
|
|
func (service *Service) TestDisqualifyNode(ctx context.Context, nodeID storj.NodeID) (err error) {
|
|
|
|
err = service.db.DisqualifyNode(ctx, nodeID)
|
2021-06-23 00:09:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-13 23:27:50 +01:00
|
|
|
return service.overlay.DisqualifyNode(ctx, nodeID)
|
2021-06-23 00:09:39 +01:00
|
|
|
}
|
|
|
|
|
2021-07-07 20:20:23 +01:00
|
|
|
// TestUnsuspendNodeUnknownAudit unsuspends a storage node for unknown audits.
|
|
|
|
func (service *Service) TestUnsuspendNodeUnknownAudit(ctx context.Context, nodeID storj.NodeID) (err error) {
|
2021-07-13 23:27:50 +01:00
|
|
|
err = service.db.UnsuspendNodeUnknownAudit(ctx, nodeID)
|
2021-06-23 00:09:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-15 15:14:13 +01:00
|
|
|
return service.overlay.TestUnsuspendNodeUnknownAudit(ctx, nodeID)
|
2021-06-23 00:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes resources.
|
|
|
|
func (service *Service) Close() error { return nil }
|