-
Notifications
You must be signed in to change notification settings - Fork 27
/
boltupgrades.go
666 lines (548 loc) · 16.4 KB
/
boltupgrades.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// Copyright (c) 2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package pool
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
bolt "go.etcd.io/bbolt"
errs "github.com/decred/dcrpool/errors"
)
const (
initialVersion = 0
// TransacionIDVersion is the second version of the database. It adds the
// transactionId field to the payments struct for payment tracking purposes.
transactionIDVersion = 1
// shareIDVersion is the second version of the database. It updates
// the share id key and removes the created on time field.
shareIDVersion = 2
// paymentSourceVersion is the third version of the database. It adds
// payment source tracking to payments.
paymentSourceVersion = 3
// removeTxfeeReserveVersion is the fourth version of the database.
// It removes the tx fee reserve from the database.
removeTxFeeReserveVersion = 4
// shareCreatedOnVersion is the fifth version of the database.
// It restores the created on time field for shares.
shareCreatedOnVersion = 5
// paymentUUIDVersion is the sixth version of the database.
// It adds the UUID field to payments.
paymentUUIDVersion = 6
// hashDataVersion is the seventh version of the database.
// It adds a hash data bucket to the database.
hashDataVersion = 7
// BoltDBVersion is the latest version of the bolt database that is
// understood by the program. Databases with recorded versions higher than
// this will fail to open (meaning any upgrades prevent reverting to older
// software).
BoltDBVersion = paymentUUIDVersion
)
// upgrades maps between old database versions and the upgrade function to
// upgrade the database to the next version.
var upgrades = [...]func(tx *bolt.Tx) error{
transactionIDVersion - 1: transactionIDUpgrade,
shareIDVersion - 1: shareIDUpgrade,
paymentSourceVersion - 1: paymentSourceUpgrade,
removeTxFeeReserveVersion - 1: removeTxFeeReserveUpgrade,
shareCreatedOnVersion - 1: shareCreatedOnUpgrade,
paymentUUIDVersion - 1: paymentUUIDUpgrade,
hashDataVersion - 1: hashDataUpgrade,
}
func fetchDBVersion(tx *bolt.Tx) (uint32, error) {
const funcName = "fetchDBVersion"
pbkt := tx.Bucket(poolBkt)
if poolBkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(poolBkt))
return 0, errs.DBError(errs.StorageNotFound, desc)
}
v := pbkt.Get(versionK)
if v == nil {
desc := fmt.Sprintf("%s: db version not set", funcName)
return 0, errs.DBError(errs.ValueNotFound, desc)
}
return binary.LittleEndian.Uint32(v), nil
}
func setDBVersion(tx *bolt.Tx, newVersion uint32) error {
const funcName = "setDBVersion"
pbkt := tx.Bucket(poolBkt)
if poolBkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(poolBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
vBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(vBytes, newVersion)
err := pbkt.Put(versionK, vBytes)
if err != nil {
desc := fmt.Sprintf("%s: unable to persist version: %v", funcName, err)
return errs.DBError(errs.PersistEntry, desc)
}
return nil
}
func transactionIDUpgrade(tx *bolt.Tx) error {
const oldVersion = 0
const newVersion = 1
const funcName = "transactionIDUpgrade"
dbVersion, err := fetchDBVersion(tx)
if err != nil {
return err
}
if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(poolBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
// Update all entries in the payment and payment archive buckets.
//
// All transaction ids for payments before the upgrade will be set to
// an empty string.
pmtbkt := pbkt.Bucket(paymentBkt)
if pmtbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(paymentBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
pmtCursor := pmtbkt.Cursor()
for k, v := pmtCursor.First(); k != nil; k, v = pmtCursor.Next() {
var payment Payment
err := json.Unmarshal(v, &payment)
if err != nil {
desc := fmt.Sprintf("%s: unable to unmarshal payment: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
pBytes, err := json.Marshal(payment)
if err != nil {
desc := fmt.Sprintf("%s: unable to marshal payment bytes: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
err = pmtbkt.Put(k, pBytes)
if err != nil {
desc := fmt.Sprintf("%s: unable to persist payment: %v",
funcName, err)
return errs.DBError(errs.PersistEntry, desc)
}
}
abkt := pbkt.Bucket(paymentArchiveBkt)
if abkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(paymentArchiveBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
aCursor := abkt.Cursor()
for k, v := aCursor.First(); k != nil; k, v = aCursor.Next() {
var payment Payment
err := json.Unmarshal(v, &payment)
if err != nil {
desc := fmt.Sprintf("%s: unable to unmarshal payment: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
pBytes, err := json.Marshal(payment)
if err != nil {
desc := fmt.Sprintf("%s: unable to marshal payment: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
err = abkt.Put(k, pBytes)
if err != nil {
desc := fmt.Sprintf("%s: unable to persist payment: %v",
funcName, err)
return errs.DBError(errs.PersistEntry, desc)
}
}
return setDBVersion(tx, newVersion)
}
func shareIDUpgrade(tx *bolt.Tx) error {
const oldVersion = 1
const newVersion = 2
const funcName = "shareIDUpgrade"
dbVersion, err := fetchDBVersion(tx)
if err != nil {
return err
}
if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(poolBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
sbkt := pbkt.Bucket(shareBkt)
if sbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(shareBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
toDelete := [][]byte{}
c := sbkt.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var share Share
err := json.Unmarshal(v, &share)
if err != nil {
desc := fmt.Sprintf("%s: unable to unmarshal share: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
createdOn := bigEndianBytesToNano(k)
share.UUID = shareID(share.Account, int64(createdOn))
sBytes, err := json.Marshal(share)
if err != nil {
desc := fmt.Sprintf("%s: unable to marshal share bytes: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
err = sbkt.Put([]byte(share.UUID), sBytes)
if err != nil {
desc := fmt.Sprintf("%s: unable to persist share: %v",
funcName, err)
return errs.DBError(errs.PersistEntry, desc)
}
toDelete = append(toDelete, k)
}
for _, entry := range toDelete {
err := sbkt.Delete(entry)
if err != nil {
desc := fmt.Sprintf("%s: unable to delete share: %v",
funcName, err)
return errs.DBError(errs.DeleteEntry, desc)
}
}
return setDBVersion(tx, newVersion)
}
func paymentSourceUpgrade(tx *bolt.Tx) error {
const oldVersion = 2
const newVersion = 3
const funcName = "paymentSourceUpgrade"
dbVersion, err := fetchDBVersion(tx)
if err != nil {
return err
}
if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(poolBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
// Update all entries in the payment and payment archive buckets.
//
// Payment sources for payments before the upgrade will have an empty
// string for coinbase and block hash fields.
pmtbkt := pbkt.Bucket(paymentBkt)
if pmtbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(paymentBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
zeroSource := &PaymentSource{}
toDelete := [][]byte{}
c := pmtbkt.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var payment Payment
err := json.Unmarshal(v, &payment)
if err != nil {
desc := fmt.Sprintf("%s: unable to unmarshal payment: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
payment.Source = zeroSource
pBytes, err := json.Marshal(payment)
if err != nil {
desc := fmt.Sprintf("%s: unable to marshal payment bytes: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
key := paymentID(payment.Height, payment.CreatedOn, payment.Account)
err = pmtbkt.Put([]byte(key), pBytes)
if err != nil {
desc := fmt.Sprintf("%s: unable to persist payment: %v",
funcName, err)
return errs.DBError(errs.PersistEntry, desc)
}
toDelete = append(toDelete, k)
}
for _, entry := range toDelete {
err := pmtbkt.Delete(entry)
if err != nil {
desc := fmt.Sprintf("%s: unable to delete payment: %v",
funcName, err)
return errs.DBError(errs.DeleteEntry, desc)
}
}
abkt := pbkt.Bucket(paymentArchiveBkt)
if abkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(paymentArchiveBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
toDelete = [][]byte{}
c = abkt.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var payment Payment
err := json.Unmarshal(v, &payment)
if err != nil {
return err
}
payment.Source = zeroSource
pBytes, err := json.Marshal(payment)
if err != nil {
desc := fmt.Sprintf("%s: unable to marshal payment bytes: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
key := paymentID(payment.Height, payment.CreatedOn, payment.Account)
err = abkt.Put([]byte(key), pBytes)
if err != nil {
desc := fmt.Sprintf("%s: unable to persist payment: %v",
funcName, err)
return errs.DBError(errs.PersistEntry, desc)
}
toDelete = append(toDelete, k)
}
for _, entry := range toDelete {
err := abkt.Delete(entry)
if err != nil {
desc := fmt.Sprintf("%s: unable to delete payment: %v",
funcName, err)
return errs.DBError(errs.DeleteEntry, desc)
}
}
return setDBVersion(tx, newVersion)
}
func removeTxFeeReserveUpgrade(tx *bolt.Tx) error {
const oldVersion = 3
const newVersion = 4
const funcName = "removeTxFeeReserveUpgrade"
dbVersion, err := fetchDBVersion(tx)
if err != nil {
return err
}
if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(poolBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
err = pbkt.Delete([]byte("txfeereserve"))
if err != nil {
desc := fmt.Sprintf("%s: unable to remove tx fee reserve entry",
funcName)
return errs.DBError(errs.DeleteEntry, desc)
}
return setDBVersion(tx, newVersion)
}
func shareCreatedOnUpgrade(tx *bolt.Tx) error {
const oldVersion = 4
const newVersion = 5
const funcName = "shareCreatedOnUpgrade"
dbVersion, err := fetchDBVersion(tx)
if err != nil {
return err
}
if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(poolBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
sbkt := pbkt.Bucket(shareBkt)
if sbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(shareBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
c := sbkt.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var share Share
err := json.Unmarshal(v, &share)
if err != nil {
desc := fmt.Sprintf("%s: unable to unmarshal share: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
createdOnB := make([]byte, 8)
_, err = hex.Decode(createdOnB, k[:16])
if err != nil {
desc := fmt.Sprintf("%s: unable to decode share "+
"created-on bytes: %v", funcName, err)
return errs.DBError(errs.Decode, desc)
}
createdOn := bigEndianBytesToNano(createdOnB)
share.CreatedOn = int64(createdOn)
sBytes, err := json.Marshal(share)
if err != nil {
desc := fmt.Sprintf("%s: unable to marshal share bytes: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
err = sbkt.Put([]byte(share.UUID), sBytes)
if err != nil {
desc := fmt.Sprintf("%s: unable to persist share: %v",
funcName, err)
return errs.DBError(errs.PersistEntry, desc)
}
}
return setDBVersion(tx, newVersion)
}
func paymentUUIDUpgrade(tx *bolt.Tx) error {
const oldVersion = 5
const newVersion = 6
const funcName = "paymentUUIDUpgrade"
dbVersion, err := fetchDBVersion(tx)
if err != nil {
return err
}
if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(poolBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
pmtbkt := pbkt.Bucket(paymentBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(paymentBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
c := pmtbkt.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var pmt Payment
err := json.Unmarshal(v, &pmt)
if err != nil {
desc := fmt.Sprintf("%s: unable to unmarshal payment: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
UUID := string(k)
pmt.UUID = UUID
pBytes, err := json.Marshal(pmt)
if err != nil {
desc := fmt.Sprintf("%s: unable to marshal payment bytes: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
err = pmtbkt.Put(k, pBytes)
if err != nil {
desc := fmt.Sprintf("%s: unable to persist payment: %v",
funcName, err)
return errs.DBError(errs.PersistEntry, desc)
}
}
abkt := pbkt.Bucket(paymentArchiveBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(paymentArchiveBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
c = abkt.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var pmt Payment
err := json.Unmarshal(v, &pmt)
if err != nil {
desc := fmt.Sprintf("%s: unable to unmarshal payment: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
pmt.UUID = string(k)
pBytes, err := json.Marshal(pmt)
if err != nil {
desc := fmt.Sprintf("%s: unable to marshal payment bytes: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}
err = abkt.Put(k, pBytes)
if err != nil {
desc := fmt.Sprintf("%s: unable to persist payment: %v",
funcName, err)
return errs.DBError(errs.PersistEntry, desc)
}
}
return setDBVersion(tx, newVersion)
}
func hashDataUpgrade(tx *bolt.Tx) error {
const oldVersion = 6
const newVersion = 7
const funcName = "hashDataUpgrade"
dbVersion, err := fetchDBVersion(tx)
if err != nil {
return err
}
if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
string(poolBkt))
return errs.DBError(errs.StorageNotFound, desc)
}
err = createNestedBucket(pbkt, hashDataBkt)
if err != nil {
return err
}
return setDBVersion(tx, newVersion)
}
// upgradeDB checks whether any upgrades are necessary before the database is
// ready for application usage. If any are, they are performed.
func upgradeDB(db *BoltDB) error {
var version uint32
err := db.DB.View(func(tx *bolt.Tx) error {
var err error
version, err = fetchDBVersion(tx)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
if version == BoltDBVersion {
// No upgrades necessary.
return nil
}
if version > BoltDBVersion {
// Database is too new.
return fmt.Errorf("expected database version <= %d, got %d", BoltDBVersion, version)
}
log.Infof("Upgrading database from version %d to %d", version, BoltDBVersion)
return db.DB.Update(func(tx *bolt.Tx) error {
// Execute all necessary upgrades in order.
for _, upgrade := range upgrades[version:] {
err := upgrade(tx)
if err != nil {
return err
}
}
return nil
})
}