6c7bf357cd
package in audit This PR implements reputation store and replace overlay in audit service to use such store for storing node's audit stats. In order to keep the changeset smaller, most of the changes in this PR is for copying audit logic in overlay to reputation package. In a following PR, the duplicating code will be removed from overlay. Change-Id: I16c12494a0970f44c422b26cf603c1dc489e5bc1
75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
// Copyright (C) 2021 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package reputation
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"storj.io/common/pb"
|
|
"storj.io/common/storj"
|
|
)
|
|
|
|
// AuditHistoryDB implements the database for audit history.
|
|
//
|
|
// architecture: Database
|
|
type AuditHistoryDB interface {
|
|
// UpdateAuditHistory updates a node's audit history with an online or offline audit.
|
|
UpdateAuditHistory(ctx context.Context, nodeID storj.NodeID, auditTime time.Time, online bool, config AuditHistoryConfig) (*UpdateAuditHistoryResponse, error)
|
|
// GetAuditHistory gets a node's audit history.
|
|
GetAuditHistory(ctx context.Context, nodeID storj.NodeID) (*AuditHistory, error)
|
|
}
|
|
|
|
// AuditHistory represents a node's audit history for the most recent tracking period.
|
|
type AuditHistory struct {
|
|
Score float64
|
|
Windows []*AuditWindow
|
|
}
|
|
|
|
// UpdateAuditHistoryResponse contains information returned by UpdateAuditHistory.
|
|
type UpdateAuditHistoryResponse struct {
|
|
NewScore float64
|
|
TrackingPeriodFull bool
|
|
}
|
|
|
|
// AuditWindow represents the number of online and total audits a node received for a specific time period.
|
|
type AuditWindow struct {
|
|
WindowStart time.Time
|
|
TotalCount int32
|
|
OnlineCount int32
|
|
}
|
|
|
|
// UpdateAuditHistory updates a node's audit history with an online or offline audit.
|
|
func (service *Service) UpdateAuditHistory(ctx context.Context, nodeID storj.NodeID, auditTime time.Time, online bool) (res *UpdateAuditHistoryResponse, err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return service.db.UpdateAuditHistory(ctx, nodeID, auditTime, online, service.config.AuditHistory)
|
|
}
|
|
|
|
// GetAuditHistory gets a node's audit history.
|
|
func (service *Service) GetAuditHistory(ctx context.Context, nodeID storj.NodeID) (auditHistory *AuditHistory, err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return service.db.GetAuditHistory(ctx, nodeID)
|
|
}
|
|
|
|
// AuditHistoryToPB converts an overlay.AuditHistory to a pb.AuditHistory.
|
|
func AuditHistoryToPB(auditHistory *AuditHistory) (historyPB *pb.AuditHistory) {
|
|
if auditHistory == nil {
|
|
return nil
|
|
}
|
|
historyPB = &pb.AuditHistory{
|
|
Score: auditHistory.Score,
|
|
Windows: make([]*pb.AuditWindow, len(auditHistory.Windows)),
|
|
}
|
|
for i, window := range auditHistory.Windows {
|
|
historyPB.Windows[i] = &pb.AuditWindow{
|
|
TotalCount: window.TotalCount,
|
|
OnlineCount: window.OnlineCount,
|
|
WindowStart: window.WindowStart,
|
|
}
|
|
}
|
|
return historyPB
|
|
}
|