2020-07-30 12:19:06 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2020-10-30 13:30:11 +00:00
|
|
|
"storj.io/storj/satellite/internalpb"
|
2021-07-07 20:20:23 +01:00
|
|
|
"storj.io/storj/satellite/reputation"
|
2020-07-30 12:19:06 +01:00
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
2021-07-07 20:20:23 +01:00
|
|
|
func addAudit(a *internalpb.AuditHistory, auditTime time.Time, online bool, config reputation.AuditHistoryConfig) error {
|
2020-07-30 12:19:06 +01:00
|
|
|
newAuditWindowStartTime := auditTime.Truncate(config.WindowSize)
|
|
|
|
earliestWindow := newAuditWindowStartTime.Add(-config.TrackingPeriod)
|
|
|
|
// windowsModified is used to determine whether we will need to recalculate the score because windows have been added or removed.
|
|
|
|
windowsModified := false
|
|
|
|
|
|
|
|
// delete windows outside of tracking period scope
|
|
|
|
updatedWindows := a.Windows
|
|
|
|
for i, window := range a.Windows {
|
|
|
|
if window.WindowStart.Before(earliestWindow) {
|
|
|
|
updatedWindows = a.Windows[i+1:]
|
|
|
|
windowsModified = true
|
|
|
|
} else {
|
|
|
|
// windows are in order, so if this window is in the tracking period, we are done deleting windows
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.Windows = updatedWindows
|
|
|
|
|
|
|
|
// if there are no windows or the latest window has passed, add another window
|
|
|
|
if len(a.Windows) == 0 || a.Windows[len(a.Windows)-1].WindowStart.Before(newAuditWindowStartTime) {
|
|
|
|
windowsModified = true
|
2020-10-30 13:30:11 +00:00
|
|
|
a.Windows = append(a.Windows, &internalpb.AuditWindow{WindowStart: newAuditWindowStartTime})
|
2020-07-30 12:19:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
latestIndex := len(a.Windows) - 1
|
|
|
|
if a.Windows[latestIndex].WindowStart.After(newAuditWindowStartTime) {
|
|
|
|
return Error.New("cannot add audit to audit history; window already passed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new audit to latest window
|
|
|
|
if online {
|
|
|
|
a.Windows[latestIndex].OnlineCount++
|
|
|
|
}
|
|
|
|
a.Windows[latestIndex].TotalCount++
|
|
|
|
|
2020-09-29 23:08:48 +01:00
|
|
|
// if no windows were added or removed, score does not change
|
|
|
|
if !windowsModified {
|
2020-07-30 12:19:06 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 23:08:48 +01:00
|
|
|
if len(a.Windows) <= 1 {
|
|
|
|
a.Score = 1
|
2020-07-30 12:19:06 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
totalWindowScores := 0.0
|
|
|
|
for i, window := range a.Windows {
|
|
|
|
// do not include last window in score
|
|
|
|
if i+1 == len(a.Windows) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
totalWindowScores += float64(window.OnlineCount) / float64(window.TotalCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// divide by number of windows-1 because last window is not included
|
|
|
|
a.Score = totalWindowScores / float64(len(a.Windows)-1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 23:08:48 +01:00
|
|
|
// UpdateAuditHistory updates a node's audit history with an online or offline audit.
|
2021-07-07 20:20:23 +01:00
|
|
|
func (reputations *reputations) UpdateAuditHistory(ctx context.Context, nodeID storj.NodeID, auditTime time.Time, online bool, config reputation.AuditHistoryConfig) (res *reputation.UpdateAuditHistoryResponse, err error) {
|
2020-12-21 17:26:07 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-07-07 20:20:23 +01:00
|
|
|
err = reputations.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) (err error) {
|
2020-07-30 12:19:06 +01:00
|
|
|
_, err = tx.Tx.ExecContext(ctx, "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-07 20:20:23 +01:00
|
|
|
res, err = reputations.updateAuditHistoryWithTx(ctx, tx, nodeID, auditTime, online, config)
|
2020-07-30 12:19:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-12-21 17:26:07 +00:00
|
|
|
return res, err
|
2020-07-30 12:19:06 +01:00
|
|
|
}
|
|
|
|
|
2021-07-07 20:20:23 +01:00
|
|
|
func (reputations *reputations) updateAuditHistoryWithTx(ctx context.Context, tx *dbx.Tx, nodeID storj.NodeID, auditTime time.Time, online bool, config reputation.AuditHistoryConfig) (res *reputation.UpdateAuditHistoryResponse, err error) {
|
2020-12-21 17:26:07 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-07-07 20:20:23 +01:00
|
|
|
res = &reputation.UpdateAuditHistoryResponse{
|
2020-12-21 17:26:07 +00:00
|
|
|
NewScore: 1,
|
|
|
|
TrackingPeriodFull: false,
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:19:06 +01:00
|
|
|
// get and deserialize node audit history
|
|
|
|
historyBytes := []byte{}
|
|
|
|
newEntry := false
|
|
|
|
dbAuditHistory, err := tx.Get_AuditHistory_By_NodeId(
|
|
|
|
ctx,
|
|
|
|
dbx.AuditHistory_NodeId(nodeID.Bytes()),
|
|
|
|
)
|
|
|
|
if errs.Is(err, sql.ErrNoRows) {
|
|
|
|
// set flag to true so we know to create rather than update later
|
|
|
|
newEntry = true
|
|
|
|
} else if err != nil {
|
2020-12-21 17:26:07 +00:00
|
|
|
return res, Error.Wrap(err)
|
2020-07-30 12:19:06 +01:00
|
|
|
} else {
|
|
|
|
historyBytes = dbAuditHistory.History
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:30:11 +00:00
|
|
|
history := &internalpb.AuditHistory{}
|
2020-09-29 23:08:48 +01:00
|
|
|
err = pb.Unmarshal(historyBytes, history)
|
2020-07-30 12:19:06 +01:00
|
|
|
if err != nil {
|
2020-12-21 17:26:07 +00:00
|
|
|
return res, err
|
2020-07-30 12:19:06 +01:00
|
|
|
}
|
|
|
|
|
2020-09-29 23:08:48 +01:00
|
|
|
err = addAudit(history, auditTime, online, config)
|
2020-07-30 12:19:06 +01:00
|
|
|
if err != nil {
|
2020-12-21 17:26:07 +00:00
|
|
|
return res, err
|
2020-07-30 12:19:06 +01:00
|
|
|
}
|
|
|
|
|
2020-09-29 23:08:48 +01:00
|
|
|
historyBytes, err = pb.Marshal(history)
|
2020-07-30 12:19:06 +01:00
|
|
|
if err != nil {
|
2020-12-21 17:26:07 +00:00
|
|
|
return res, err
|
2020-07-30 12:19:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the entry did not exist at the beginning, create a new one. Otherwise update
|
|
|
|
if newEntry {
|
|
|
|
_, err = tx.Create_AuditHistory(
|
|
|
|
ctx,
|
|
|
|
dbx.AuditHistory_NodeId(nodeID.Bytes()),
|
|
|
|
dbx.AuditHistory_History(historyBytes),
|
|
|
|
)
|
2020-12-21 17:26:07 +00:00
|
|
|
return res, Error.Wrap(err)
|
2020-07-30 12:19:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Update_AuditHistory_By_NodeId(
|
|
|
|
ctx,
|
|
|
|
dbx.AuditHistory_NodeId(nodeID.Bytes()),
|
|
|
|
dbx.AuditHistory_Update_Fields{
|
|
|
|
History: dbx.AuditHistory_History(historyBytes),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-12-21 17:26:07 +00:00
|
|
|
windowsPerTrackingPeriod := int(config.TrackingPeriod.Seconds() / config.WindowSize.Seconds())
|
|
|
|
res.TrackingPeriodFull = len(history.Windows)-1 >= windowsPerTrackingPeriod
|
|
|
|
res.NewScore = history.Score
|
|
|
|
return res, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAuditHistory gets a node's audit history.
|
2021-07-07 20:20:23 +01:00
|
|
|
func (reputations *reputations) GetAuditHistory(ctx context.Context, nodeID storj.NodeID) (auditHistory *reputation.AuditHistory, err error) {
|
2020-12-21 17:26:07 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-07-07 20:20:23 +01:00
|
|
|
dbAuditHistory, err := reputations.db.Get_AuditHistory_By_NodeId(
|
2020-12-21 17:26:07 +00:00
|
|
|
ctx,
|
|
|
|
dbx.AuditHistory_NodeId(nodeID.Bytes()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errs.Is(err, sql.ErrNoRows) {
|
2021-07-07 20:20:23 +01:00
|
|
|
return nil, reputation.ErrNodeNotFound.New("no audit history for node")
|
2020-12-21 17:26:07 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
history, err := auditHistoryFromPB(dbAuditHistory.History)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return history, nil
|
|
|
|
}
|
|
|
|
|
2021-07-07 20:20:23 +01:00
|
|
|
func auditHistoryFromPB(historyBytes []byte) (auditHistory *reputation.AuditHistory, err error) {
|
2020-12-21 17:26:07 +00:00
|
|
|
historyPB := &internalpb.AuditHistory{}
|
|
|
|
err = pb.Unmarshal(historyBytes, historyPB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-07 20:20:23 +01:00
|
|
|
history := &reputation.AuditHistory{
|
2020-12-21 17:26:07 +00:00
|
|
|
Score: historyPB.Score,
|
2021-07-07 20:20:23 +01:00
|
|
|
Windows: make([]*reputation.AuditWindow, len(historyPB.Windows)),
|
2020-12-21 17:26:07 +00:00
|
|
|
}
|
|
|
|
for i, window := range historyPB.Windows {
|
2021-07-07 20:20:23 +01:00
|
|
|
history.Windows[i] = &reputation.AuditWindow{
|
2020-12-21 17:26:07 +00:00
|
|
|
TotalCount: window.TotalCount,
|
|
|
|
OnlineCount: window.OnlineCount,
|
|
|
|
WindowStart: window.WindowStart,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return history, nil
|
2020-07-30 12:19:06 +01:00
|
|
|
}
|