-
Notifications
You must be signed in to change notification settings - Fork 75
/
timestamp.go
359 lines (313 loc) · 9.4 KB
/
timestamp.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) 2022 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ticketvote
import (
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/decred/politeia/politeiad/plugins/ticketvote"
"github.com/decred/politeia/util"
"github.com/pkg/errors"
)
const (
// voteTimestampKey is the key for a ticket vote timestamp entry in the
// key-value store cache.
voteTimestampKey = "timestamp-vote-{shorttoken}-{page}-{index}"
// authTimestampKey is the key for a vote auth timestamp entry in the
// key-value store cache.
authTimestampKey = "timestamp-auth-{shorttoken}-{index}"
// detailsTimestampKey is the key for a vote details timestamp entry in the
// key-value store cache.
detailsTimestampKey = "timestamp-details-{shorttoken}"
)
// cacheFinalVoteTimestamps accepts a slice of vote timestamps, it collects the
// final timestamps then stores them in the key-value store.
func (p *ticketVotePlugin) cacheFinalVoteTimestamps(token []byte, ts []ticketvote.Timestamp, page uint32) error {
// Collect final timestamps
fts := make([]ticketvote.Timestamp, 0, len(ts))
for _, t := range ts {
if timestampIsFinal(t) {
fts = append(fts, t)
}
}
// Store final timestamp
err := p.saveVoteTimestamps(token, fts, page)
if err != nil {
return err
}
log.Debugf("Cached final vote timestamps of %v/%v",
len(fts), len(ts))
return nil
}
// saveVoteTimestamps saves a slice of vote timestamps to the key-value cache.
func (p *ticketVotePlugin) saveVoteTimestamps(token []byte, ts []ticketvote.Timestamp, page uint32) error {
// Setup the blob entries
blobs := make(map[string][]byte, len(ts))
keys := make([]string, 0, len(ts))
for i, v := range ts {
k, err := getVoteTimestampKey(token, page, uint32(i))
if err != nil {
return err
}
b, err := json.Marshal(v)
if err != nil {
return err
}
blobs[k] = b
keys = append(keys, k)
}
// Delete exisiting digests
err := p.tstore.CacheDel(keys)
if err != nil {
return err
}
// Save the blob entries
return p.tstore.CachePut(blobs, false)
}
// cachedVoteTimestamps returns cached vote timestamps if they exist. It
// accepts the requested page as the vote timestamps request is paginated and
// both the page number and the vote index are part of the vote's cache key.
func (p *ticketVotePlugin) cachedVoteTimestamps(token []byte, page, pageSize uint32) ([]ticketvote.Timestamp, error) {
// Setup the timestamp keys
keys := make([]string, 0, pageSize)
for i := uint32(0); i < pageSize; i++ {
key, err := getVoteTimestampKey(token, page, i)
if err != nil {
return nil, err
}
keys = append(keys, key)
}
// Get the timestamp blob entries
blobs, err := p.tstore.CacheGet(keys)
if err != nil {
return nil, err
}
// Decode the timestamps
ts := make([]ticketvote.Timestamp, len(blobs))
for k, v := range blobs {
var t ticketvote.Timestamp
err := json.Unmarshal(v, &t)
if err != nil {
return nil, err
}
idx, err := parseVoteTimestampKey(k)
if err != nil {
return nil, err
}
ts[idx] = t
}
log.Debugf("Retrieved %v cached final vote timestamps", len(ts))
return ts, nil
}
// getVoteTimestampVoteKey returns the key for a vote timestamp in the
// key-value store cache.
func getVoteTimestampKey(token []byte, page, index uint32) (string, error) {
t, err := util.ShortTokenEncode(token)
if err != nil {
return "", err
}
pageStr := strconv.FormatUint(uint64(page), 10)
indexStr := strconv.FormatUint(uint64(index), 10)
key := strings.Replace(voteTimestampKey, "{shorttoken}", t, 1)
key = strings.Replace(key, "{page}", pageStr, 1)
key = strings.Replace(key, "{index}", indexStr, 1)
return key, nil
}
// parseVoteTimestampKey parses the item index from a vote timestamp key.
func parseVoteTimestampKey(key string) (uint32, error) {
s := strings.Split(key, "-")
if len(s) != 5 {
return 0, errors.Errorf("invalid vote timestamp key")
}
index, err := strconv.ParseUint(s[4], 10, 64)
if err != nil {
return 0, err
}
return uint32(index), nil
}
// cacheFinalVoteTimestamps accepts a slice of auth timestamps, it collects the
// final timestamps then stores them in the key-value store.
func (p *ticketVotePlugin) cacheFinalAuthTimestamps(token []byte, ts []ticketvote.Timestamp) error {
// Collect final timestamps
fts := make([]ticketvote.Timestamp, 0, len(ts))
for _, t := range ts {
if timestampIsFinal(t) {
fts = append(fts, t)
}
}
// Store final timestamp
err := p.saveAuthTimestamps(token, fts)
if err != nil {
return err
}
log.Debugf("Cached final auth timestamps of %v/%v",
len(fts), len(ts))
return nil
}
// saveAuthTimestamps saves a slice of vote timestamps to the key-value cache.
func (p *ticketVotePlugin) saveAuthTimestamps(token []byte, ts []ticketvote.Timestamp) error {
// Setup the blob entries
blobs := make(map[string][]byte, len(ts))
keys := make([]string, 0, len(ts))
for i, v := range ts {
k, err := getAuthTimestampKey(token, uint32(i))
if err != nil {
return err
}
b, err := json.Marshal(v)
if err != nil {
return err
}
blobs[k] = b
keys = append(keys, k)
}
// Delete exisiting digests
err := p.tstore.CacheDel(keys)
if err != nil {
return err
}
// Save the blob entries
return p.tstore.CachePut(blobs, false)
}
// cachedAuthTimestamps returns cached auth timestamps if they exist.
func (p *ticketVotePlugin) cachedAuthTimestamps(token []byte) ([]ticketvote.Timestamp, error) {
// Setup the timestamp keys
keys := make([]string, 0, 256)
for i := uint32(0); i < 256; i++ {
key, err := getAuthTimestampKey(token, i)
if err != nil {
return nil, err
}
keys = append(keys, key)
}
// Get the timestamp blob entries
blobs, err := p.tstore.CacheGet(keys)
if err != nil {
return nil, err
}
// Decode the timestamps
ts := make([]ticketvote.Timestamp, len(blobs))
for k, v := range blobs {
var t ticketvote.Timestamp
err := json.Unmarshal(v, &t)
if err != nil {
return nil, err
}
idx, err := parseAuthTimestampKey(k)
if err != nil {
return nil, err
}
ts[idx] = t
}
log.Debugf("Retrieved %v cached final auth timestamps", len(ts))
return ts, nil
}
// getAuthTimestampVoteKey returns the key for a auth timestamp in the
// key-value store cache.
func getAuthTimestampKey(token []byte, index uint32) (string, error) {
t, err := util.ShortTokenEncode(token)
if err != nil {
return "", err
}
indexStr := strconv.FormatUint(uint64(index), 10)
key := strings.Replace(authTimestampKey, "{shorttoken}", t, 1)
key = strings.Replace(key, "{index}", indexStr, 1)
return key, nil
}
// parseAuthTimestampKey parses the item index from a auth timestamp key.
func parseAuthTimestampKey(key string) (uint32, error) {
s := strings.Split(key, "-")
if len(s) != 4 {
return 0, errors.Errorf("invalid auth timestamp key")
}
index, err := strconv.ParseUint(s[3], 10, 64)
if err != nil {
return 0, err
}
return uint32(index), nil
}
// cacheFinalDetailsTimestamp accepts a vote details timestamp, if the given
// vote details timestamp is final it stores in the key-value store.
func (p *ticketVotePlugin) cacheFinalDetailsTimestamp(token []byte, t ticketvote.Timestamp) error {
// Check whether given timestamp is final
if timestampIsFinal(t) {
// Store final timestamp in cache
err := p.saveDetailsTimestamp(token, t)
if err != nil {
return err
}
log.Debugf("Cached final vote details timestamp of %v",
hex.EncodeToString(token))
}
return nil
}
// saveDetailsTimestamp saves a slice of vote timestamps to the key-value cache.
func (p *ticketVotePlugin) saveDetailsTimestamp(token []byte, t ticketvote.Timestamp) error {
// Setup the blob entry
blobs := make(map[string][]byte, 1)
k, err := getDetailsTimestampKey(token)
if err != nil {
return err
}
b, err := json.Marshal(t)
if err != nil {
return err
}
blobs[k] = b
// Delete exisiting digests
err = p.tstore.CacheDel([]string{k})
if err != nil {
return err
}
// Save the blob entries
return p.tstore.CachePut(blobs, false)
}
// cachedDetailsTimestamp returns cached vote details timestamp if one exist.
func (p *ticketVotePlugin) cachedDetailsTimestamp(token []byte) (*ticketvote.Timestamp, error) {
// Setup the timestamp key
key, err := getDetailsTimestampKey(token)
if err != nil {
return nil, err
}
// Get the timestamp blob entry
blobs, err := p.tstore.CacheGet([]string{key})
if err != nil {
return nil, err
}
// There should never be more than a one cached vote details
if len(blobs) > 1 {
return nil, fmt.Errorf("invalid vote details count: "+
"got %v, want 1", len(blobs))
}
// Decode the timestamp if one found
if len(blobs) > 0 {
var t ticketvote.Timestamp
err = json.Unmarshal(blobs[key], &t)
if err != nil {
return nil, err
}
log.Debugf("Retrieved cached vote details for %v",
hex.EncodeToString(token))
return &t, nil
}
return nil, nil
}
// getDetailsTimestampVoteKey returns the key for a auth timestamp in the
// key-value store cache.
func getDetailsTimestampKey(token []byte) (string, error) {
t, err := util.ShortTokenEncode(token)
if err != nil {
return "", err
}
key := strings.Replace(detailsTimestampKey, "{shorttoken}", t, 1)
return key, nil
}
// timestampIsFinal returns whether the timestamp is considered to be final and
// will not change in the future. Once the TxID is present then the timestamp
// is considered to be final since it has been included in a DCR transaction.
func timestampIsFinal(t ticketvote.Timestamp) bool {
return t.TxID != ""
}