-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
207 lines (165 loc) · 5.11 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
Prepare allows payment operators to upload the prepared payout report to the payments system.
Prepare reads the payment report from the file provided on command line and is expecting valid JSON serialized transactions.
Usage:
prepare [flags] filename...
The flags are:
-v
verbose logging enabled
-e
The environment to which the operator is sending transactions to be put in prepared state.
-ra
The redis addresses comma seperated
-rp
The redis password
-ru
The redis user
*/
package main
import (
"context"
"flag"
"log"
"os"
"strings"
"github.com/brave-intl/bat-go/libs/payments"
paymentscli "github.com/brave-intl/bat-go/tools/payments"
)
func main() {
ctx := context.Background()
// command line flags
key := flag.String(
"k", "test/private.pem",
"the operator's key file location (ed25519 private key) in PEM format")
env := flag.String(
"e", "local",
"the environment to which the tool will interact")
verbose := flag.Bool(
"v", false,
"view verbose logging")
redisAddr := flag.String(
"ra", "127.0.0.1:6380",
"redis address")
redisPass := flag.String(
"rp", "",
"redis password")
redisUser := flag.String(
"ru", "",
"redis username")
pcr2 := flag.String(
"pcr2", "", "the hex PCR2 value for this enclave")
payoutID := flag.String(
"p", "",
"payout id")
cg := flag.String(
"cg", "cli",
"consumer group suffix")
resubmit := flag.Bool(
"resubmit", false,
"resubmit to prepare stream")
batchSize := flag.Int64(
"bsize", 10,
"batch size for worker prepare processing")
flag.Parse()
// get the list of report files for prepare
files := flag.Args()
if *verbose {
// print out the configuration
log.Printf("Environment: %s\n", *env)
log.Printf("Operator Key File Location: %s\n", *key)
}
if *env != "development" && len(*pcr2) != 96 {
log.Fatal("a valid pcr2 is required to prepare in this environment\n")
}
// setup the settlement redis client
ctx, client, err := paymentscli.NewSettlementClient(ctx, *env, map[string]string{
"addr": *redisAddr, "pass": *redisPass, "username": *redisUser, "pcr2": *pcr2, // client specific configurations
})
if err != nil {
log.Fatalf("failed to create settlement client: %v\n", err)
}
if payoutID == nil || strings.TrimSpace(*payoutID) == "" {
log.Fatal("failed payout id cannot be nil or empty\n")
}
firstRun := true
// FIXME
responseStream := payments.PreparePrefix + *payoutID + payments.ResponseSuffix
responseFile := responseStream + ".log"
if _, err := os.Stat(responseFile); err == nil {
firstRun = false
if !*resubmit {
log.Println("not first run, skipping add to prepare stream")
}
}
totalTransactions := 0
f, err := os.Open(files[0])
if err != nil {
log.Fatalf("failed to open report file: %v\n", err)
}
defer f.Close()
report := paymentscli.PreparedReport{}
if err := paymentscli.ReadReport(&report, f); err != nil {
log.Fatalf("failed to read report from stdin: %v\n", err)
}
if err := report.Validate(); err != nil {
log.Fatalf("failed to validate report: %v\n", err)
}
if report[0].PayoutID != *payoutID {
log.Fatalf("payoutID did not match report: %s\n", report[0].PayoutID)
}
totalTransactions += len(report)
if firstRun || *resubmit {
priv, err := paymentscli.GetOperatorPrivateKey(*key)
if err != nil {
log.Fatalf("failed to parse operator key file: %v\n", err)
}
if err := report.Prepare(ctx, priv, client); err != nil {
log.Fatalf("failed to read report from stdin: %v\n", err)
}
wc := &payments.WorkerConfig{
PayoutID: *payoutID,
ConsumerGroup: payments.PreparePrefix + *payoutID + "-cg",
Stream: payments.PreparePrefix + *payoutID,
Count: len(report),
BatchSize: *batchSize,
}
err = client.ConfigureWorker(ctx, payments.PrepareConfigStream, wc)
if err != nil {
log.Fatalf("failed to write to prepare config stream: %v\n", err)
}
if *verbose {
log.Printf("prepare transactions loaded for %+v\n", payoutID)
}
os.Create(responseFile)
}
// FIXME default to public key as consumer group?
err = client.WaitForResponses(ctx, *payoutID, totalTransactions, responseStream, *cg)
if err != nil {
log.Fatalf("failed to wait for prepare responses: %v\n", err)
}
// perform validations
// read in responseFile as attested report
attestedReportFile, err := os.Open(responseFile)
if err != nil {
log.Fatalf("failed to open attested report file: %v\n", err)
}
defer attestedReportFile.Close()
// parse the attested report
attestedReport := paymentscli.AttestedReport{}
if err := paymentscli.ReadReportFromResponses(&attestedReport, attestedReportFile); err != nil {
log.Fatalf("failed to read attested report: %v\n", err)
}
if *verbose {
log.Printf("attested report stats: %d transactions; %s total bat\n",
len(attestedReport), attestedReport.SumBAT())
log.Printf("prepared report stats: %d transactions; %s total bat\n",
len(report), report.SumBAT())
}
// compare performs automated checks to validate reports
if err := paymentscli.Compare(report, attestedReport); err != nil {
log.Fatalf("failed to compare reports: %v\n", err)
}
if *verbose {
log.Println("prepare command complete")
}
}