-
Notifications
You must be signed in to change notification settings - Fork 402
/
gracefulexit.go
170 lines (149 loc) · 4.55 KB
/
gracefulexit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"encoding/csv"
"encoding/hex"
"fmt"
"io"
"os"
"strconv"
"text/tabwriter"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/identity"
"storj.io/common/pb"
"storj.io/common/signing"
"storj.io/common/storj"
"storj.io/storj/satellite/gracefulexit"
"storj.io/storj/satellite/satellitedb"
)
// generateGracefulExitCSV creates a report with graceful exit data for exiting or exited nodes in a given period.
func generateGracefulExitCSV(ctx context.Context, completed bool, start time.Time, end time.Time, output io.Writer) error {
db, err := satellitedb.New(zap.L().Named("db"), gracefulExitCfg.Database, satellitedb.Options{})
if err != nil {
return errs.New("error connecting to master database on satellite: %+v", err)
}
defer func() {
err = errs.Combine(err, db.Close())
}()
var nodeIDs storj.NodeIDList
if completed {
nodeIDs, err = db.OverlayCache().GetGracefulExitCompletedByTimeFrame(ctx, start, end)
if err != nil {
return err
}
} else {
nodeIDs, err = db.OverlayCache().GetGracefulExitIncompleteByTimeFrame(ctx, start, end)
if err != nil {
return err
}
}
w := csv.NewWriter(output)
headers := []string{
"nodeID",
"walletAddress",
"nodeCreationDate",
"initiatedGracefulExit",
"completedGracefulExit",
"transferredGB",
}
if err := w.Write(headers); err != nil {
return err
}
for _, id := range nodeIDs {
node, err := db.OverlayCache().Get(ctx, id)
if err != nil {
return err
}
exitProgress, err := db.GracefulExit().GetProgress(ctx, id)
if gracefulexit.ErrNodeNotFound.Has(err) {
exitProgress = &gracefulexit.Progress{}
} else if err != nil {
return err
}
exitStatus := node.ExitStatus
exitFinished := ""
if exitStatus.ExitFinishedAt != nil {
exitFinished = exitStatus.ExitFinishedAt.Format("2006-01-02")
}
nextRow := []string{
node.Id.String(),
node.Operator.Wallet,
node.CreatedAt.Format("2006-01-02"),
exitStatus.ExitInitiatedAt.Format("2006-01-02"),
exitFinished,
strconv.FormatInt(exitProgress.BytesTransferred, 10),
}
if err := w.Write(nextRow); err != nil {
return err
}
}
if err := w.Error(); err != nil {
return err
}
w.Flush()
if output != os.Stdout {
if completed {
fmt.Println("Generated report for nodes that have completed graceful exit.")
} else {
fmt.Println("Generated report for nodes that are in the process of graceful exiting.")
}
}
return err
}
func verifyGracefulExitReceipt(ctx context.Context, identity *identity.FullIdentity, nodeID storj.NodeID, receipt string) error {
signee := signing.SigneeFromPeerIdentity(identity.PeerIdentity())
bytes, err := hex.DecodeString(receipt)
if err != nil {
return errs.Wrap(err)
}
// try to unmarshal as an ExitCompleted first
completed := &pb.ExitCompleted{}
err = pb.Unmarshal(bytes, completed)
if err != nil {
// if it is not a ExitCompleted, try ExitFailed
failed := &pb.ExitFailed{}
err = pb.Unmarshal(bytes, failed)
if err != nil {
return errs.Wrap(err)
}
err = checkIDs(identity.PeerIdentity().ID, nodeID, failed.SatelliteId, failed.NodeId)
if err != nil {
return err
}
err = signing.VerifyExitFailed(ctx, signee, failed)
if err != nil {
return errs.Wrap(err)
}
return writeVerificationMessage(false, failed.SatelliteId, failed.NodeId, failed.Failed)
}
err = checkIDs(identity.PeerIdentity().ID, nodeID, completed.SatelliteId, completed.NodeId)
if err != nil {
return err
}
err = signing.VerifyExitCompleted(ctx, signee, completed)
if err != nil {
return errs.Wrap(err)
}
return writeVerificationMessage(true, completed.SatelliteId, completed.NodeId, completed.Completed)
}
func checkIDs(satelliteID storj.NodeID, providedSNID storj.NodeID, receiptSatelliteID storj.NodeID, receiptSNID storj.NodeID) error {
if satelliteID != receiptSatelliteID {
return errs.New("satellite ID (%v) does not match receipt satellite ID (%v).", satelliteID, receiptSatelliteID)
}
if providedSNID != receiptSNID {
return errs.New("provided storage node ID (%v) does not match receipt node ID (%v).", providedSNID, receiptSNID)
}
return nil
}
func writeVerificationMessage(succeeded bool, satelliteID storj.NodeID, snID storj.NodeID, timestamp time.Time) error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(w, "Succeeded:\t%v\n", succeeded)
fmt.Fprintf(w, "Satellite ID:\t%v\n", satelliteID)
fmt.Fprintf(w, "Storage Node ID:\t%v\n", snID)
fmt.Fprintf(w, "Timestamp:\t%v\n", timestamp)
return errs.Wrap(w.Flush())
}