/
stakedb.go
1148 lines (929 loc) · 34.3 KB
/
stakedb.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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015-2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package udb
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"time"
"github.com/HcashOrg/hcashd/blockchain/stake"
"github.com/HcashOrg/hcashd/chaincfg/chainhash"
"github.com/HcashOrg/hcashd/wire"
"github.com/HcashOrg/hcashutil"
"github.com/HcashOrg/hcashwallet/apperrors"
"github.com/HcashOrg/hcashwallet/walletdb"
)
const (
// Size of various types in bytes.
int8Size = 1
int16Size = 2
int32Size = 4
int64Size = 8
hashSize = 32
// Size of a serialized ssgenRecord.
// hash + uint32 + hash + uint16 + uint64
ssgenRecordSize = 32 + 4 + 32 + 2 + 8
// Size of a serialized ssrtxRecord.
// hash + uint32 + hash + uint64
ssrtxRecordSize = 32 + 4 + 32 + 8
// stakePoolUserTicketSize is the size
// of a serialized stake pool user
// ticket.
// hash + uint32 + uint8 + uint32 + hash
stakePoolUserTicketSize = 32 + 4 + 1 + 4 + 32
// stakePoolTicketsPrefixSize is the length of
// stakePoolTicketsPrefix.
stakePoolTicketsPrefixSize = 5
// stakePoolInvalidPrefixSize is the length of
// stakePoolInvalidPrefix.
stakePoolInvalidPrefixSize = 5
// scriptHashLen is the length of a HASH160
// hash.
scriptHashSize = 20
)
var (
// sstxTicket2PKHPrefix is the PkScript byte prefix for an SStx
// P2PKH ticket output. The entire prefix is 0xba76a914, but we
// only use the first 3 bytes.
sstxTicket2PKHPrefix = []byte{0xba, 0x76, 0xa9}
// sstxTicket2SHPrefix is the PkScript byte prefix for an SStx
// P2SH ticket output.
sstxTicket2SHPrefix = []byte{0xba, 0xa9, 0x14}
// stakePoolTicketsPrefix is the byte slice prefix for valid
// tickets in the stake pool for a given user.
stakePoolTicketsPrefix = []byte("tickt")
// stakePoolInvalidPrefix is the byte slice prefix for invalid
// tickets in the stake pool for a given user.
stakePoolInvalidPrefix = []byte("invld")
)
// Key names for various database fields.
// sstxRecords
// key: sstx tx hash
// val: sstxRecord
// ssgenRecords
// key: sstx tx hash
// val: serialized slice of ssgenRecords
//
var (
// Bucket names.
sstxRecordsBucketName = []byte("sstxrecords")
ssgenRecordsBucketName = []byte("ssgenrecords")
ssrtxRecordsBucketName = []byte("ssrtxrecords")
// Db related key names (main bucket).
stakeStoreCreateDateName = []byte("stakestorecreated")
)
// deserializeSStxRecord deserializes the passed serialized tx record information.
func deserializeSStxRecord(serializedSStxRecord []byte, dbVersion uint32) (*sstxRecord, error) {
switch {
case dbVersion < 3:
record := new(sstxRecord)
curPos := 0
// Read MsgTx size (as a uint64).
msgTxLen := int(binary.LittleEndian.Uint64(
serializedSStxRecord[curPos : curPos+int64Size]))
curPos += int64Size
// Pretend to read the pkScrLoc for the 0th output pkScript.
curPos += int32Size
// Read the intended voteBits and extended voteBits length (uint8).
record.voteBitsSet = false
voteBitsLen := int(serializedSStxRecord[curPos])
if voteBitsLen != 0 {
record.voteBitsSet = true
}
curPos += int8Size
// Read the assumed 2 byte VoteBits as well as the extended
// votebits (75 bytes max).
record.voteBits = binary.LittleEndian.Uint16(
serializedSStxRecord[curPos : curPos+int16Size])
curPos += int16Size
if voteBitsLen != 0 {
record.voteBitsExt = make([]byte, voteBitsLen-int16Size)
copy(record.voteBitsExt, serializedSStxRecord[curPos:curPos+voteBitsLen-int16Size])
}
curPos += stake.MaxSingleBytePushLength - int16Size
// Prepare a buffer for the msgTx.
buf := bytes.NewBuffer(serializedSStxRecord[curPos : curPos+msgTxLen])
curPos += msgTxLen
// Deserialize transaction.
msgTx := new(wire.MsgTx)
err := msgTx.Deserialize(buf)
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, err
}
// Create and save the hcashutil.Tx of the read MsgTx and set its index.
tx := hcashutil.NewTx(msgTx)
tx.SetIndex(hcashutil.TxIndexUnknown)
tx.SetTree(wire.TxTreeStake)
record.tx = tx
// Read received unix time (int64).
received := int64(binary.LittleEndian.Uint64(
serializedSStxRecord[curPos : curPos+int64Size]))
record.ts = time.Unix(received, 0)
return record, nil
case dbVersion >= 3:
// Don't need to read the pkscript location, so first four bytes are
// skipped.
serializedSStxRecord = serializedSStxRecord[4:]
var tx wire.MsgTx
err := tx.Deserialize(bytes.NewReader(serializedSStxRecord))
if err != nil {
return nil, err
}
unixTime := int64(binary.LittleEndian.Uint64(serializedSStxRecord[tx.SerializeSize():]))
return &sstxRecord{tx: hcashutil.NewTx(&tx), ts: time.Unix(unixTime, 0)}, nil
default:
panic("unreachable")
}
}
// deserializeSStxTicketHash160 deserializes and returns a 20 byte script
// hash for a ticket's 0th output.
func deserializeSStxTicketHash160(serializedSStxRecord []byte, dbVersion uint32) (hash160 []byte, p2sh bool, err error) {
var pkscriptLocOffset int
var txOffset int
switch {
case dbVersion < 3:
pkscriptLocOffset = 8 // After transaction size
txOffset = 8 + 4 + 1 + stake.MaxSingleBytePushLength
case dbVersion >= 3:
pkscriptLocOffset = 0
txOffset = 4
}
pkscriptLoc := int(binary.LittleEndian.Uint32(serializedSStxRecord[pkscriptLocOffset:])) + txOffset
// Pop off the script prefix, then pop off the 20 bytes
// HASH160 pubkey or script hash.
prefixBytes := serializedSStxRecord[pkscriptLoc : pkscriptLoc+3]
scriptHash := make([]byte, 20)
p2sh = false
switch {
case bytes.Equal(prefixBytes, sstxTicket2PKHPrefix):
scrHashLoc := pkscriptLoc + 4
if scrHashLoc+20 >= len(serializedSStxRecord) {
return nil, false, stakeStoreError(apperrors.ErrDatabase,
"bad serialized sstx record size for pubkey hash", nil)
}
copy(scriptHash, serializedSStxRecord[scrHashLoc:scrHashLoc+20])
case bytes.Equal(prefixBytes, sstxTicket2SHPrefix):
scrHashLoc := pkscriptLoc + 3
if scrHashLoc+20 >= len(serializedSStxRecord) {
return nil, false, stakeStoreError(apperrors.ErrDatabase,
"bad serialized sstx record size for script hash", nil)
}
copy(scriptHash, serializedSStxRecord[scrHashLoc:scrHashLoc+20])
p2sh = true
}
return scriptHash, p2sh, nil
}
// serializeSSTxRecord returns the serialization of the passed txrecord row.
func serializeSStxRecord(record *sstxRecord, dbVersion uint32) ([]byte, error) {
switch {
case dbVersion < 3:
msgTx := record.tx.MsgTx()
msgTxSize := int64(msgTx.SerializeSize())
size := 0
// tx tree is implicit (stake)
// size of msgTx (recast to int64)
size += int64Size
// byte index of the ticket pk script
size += int32Size
// intended votebits length (uint8)
size += int8Size
// intended votebits (75 bytes)
size += stake.MaxSingleBytePushLength
// msgTx size is variable.
size += int(msgTxSize)
// timestamp (int64)
size += int64Size
buf := make([]byte, size)
curPos := 0
// Write msgTx size (as a uint64).
binary.LittleEndian.PutUint64(buf[curPos:curPos+int64Size], uint64(msgTxSize))
curPos += int64Size
// Write the pkScript loc for the ticket output as a uint32.
pkScrLoc := msgTx.PkScriptLocs()
binary.LittleEndian.PutUint32(buf[curPos:curPos+int32Size], uint32(pkScrLoc[0]))
curPos += int32Size
// Write the intended votebits length (uint8). Hardcode the uint16
// size for now.
buf[curPos] = byte(int16Size + len(record.voteBitsExt))
curPos += int8Size
// Write the first two bytes for the intended votebits (75 bytes max),
// then write the extended vote bits.
binary.LittleEndian.PutUint16(buf[curPos:curPos+int16Size], record.voteBits)
curPos += int16Size
copy(buf[curPos:], record.voteBitsExt)
curPos += stake.MaxSingleBytePushLength - 2
// Serialize and write transaction.
var b bytes.Buffer
b.Grow(msgTx.SerializeSize())
err := msgTx.Serialize(&b)
if err != nil {
return buf, err
}
copy(buf[curPos:curPos+int(msgTxSize)], b.Bytes())
curPos += int(msgTxSize)
// Write received unix time (int64).
binary.LittleEndian.PutUint64(buf[curPos:curPos+int64Size], uint64(record.ts.Unix()))
return buf, nil
case dbVersion >= 3:
tx := record.tx.MsgTx()
txSize := tx.SerializeSize()
buf := make([]byte, 4+txSize+8) // pkscript location + tx + unix timestamp
pkScrLoc := tx.PkScriptLocs()
binary.LittleEndian.PutUint32(buf, uint32(pkScrLoc[0]))
err := tx.Serialize(bytes.NewBuffer(buf[4:4]))
if err != nil {
return nil, err
}
binary.LittleEndian.PutUint64(buf[4+txSize:], uint64(record.ts.Unix()))
return buf, nil
default:
panic("unreachable")
}
}
// deserializeSSGenRecord deserializes the passed serialized tx
// record information.
func deserializeSSGenRecord(serializedSSGenRecord []byte) (*ssgenRecord, error) {
// Cursory check to make sure that the size of the
// record makes sense.
if len(serializedSSGenRecord)%ssgenRecordSize != 0 {
str := "serialized SSGen record was wrong size"
return nil, stakeStoreError(apperrors.ErrDatabase, str, nil)
}
record := new(ssgenRecord)
curPos := 0
// Insert the block hash into the record.
copy(record.blockHash[:], serializedSSGenRecord[curPos:curPos+hashSize])
curPos += hashSize
// Insert the block height into the record.
record.blockHeight = binary.LittleEndian.Uint32(
serializedSSGenRecord[curPos : curPos+int32Size])
curPos += int32Size
// Insert the tx hash into the record.
copy(record.txHash[:], serializedSSGenRecord[curPos:curPos+hashSize])
curPos += hashSize
// Insert the votebits into the record.
record.voteBits = binary.LittleEndian.Uint16(
serializedSSGenRecord[curPos : curPos+int16Size])
curPos += int16Size
// Insert the timestamp into the record.
record.ts = time.Unix(
int64(binary.LittleEndian.Uint64(
serializedSSGenRecord[curPos:curPos+int64Size])),
0)
return record, nil
}
// deserializeSSGenRecords deserializes the passed serialized tx
// record information.
func deserializeSSGenRecords(serializedSSGenRecords []byte) ([]*ssgenRecord, error) {
// Cursory check to make sure that the number of records
// makes sense.
if len(serializedSSGenRecords)%ssgenRecordSize != 0 {
err := io.ErrUnexpectedEOF
return nil, err
}
numRecords := len(serializedSSGenRecords) / ssgenRecordSize
records := make([]*ssgenRecord, numRecords)
// Loop through all the ssgen records, deserialize them, and
// store them.
for i := 0; i < numRecords; i++ {
record, err := deserializeSSGenRecord(
serializedSSGenRecords[i*ssgenRecordSize : (i+1)*ssgenRecordSize])
if err != nil {
str := "problem serializing ssgen record"
return nil, stakeStoreError(apperrors.ErrDatabase, str, err)
}
records[i] = record
}
return records, nil
}
// serializeSSGenRecord returns the serialization of a single SSGen
// record.
func serializeSSGenRecord(record *ssgenRecord) []byte {
buf := make([]byte, ssgenRecordSize)
curPos := 0
// Write the block hash.
copy(buf[curPos:curPos+hashSize], record.blockHash[:])
curPos += hashSize
// Write the block height.
binary.LittleEndian.PutUint32(buf[curPos:curPos+int32Size], record.blockHeight)
curPos += int32Size
// Write the tx hash.
copy(buf[curPos:curPos+hashSize], record.txHash[:])
curPos += hashSize
// Write the vote bits.
binary.LittleEndian.PutUint16(buf[curPos:curPos+int16Size], record.voteBits)
curPos += int16Size
// Write the timestamp.
binary.LittleEndian.PutUint64(buf[curPos:curPos+int64Size], uint64(record.ts.Unix()))
return buf
}
// serializeSSGenRecords returns the serialization of the passed
// SSGen records slice.
func serializeSSGenRecords(records []*ssgenRecord) []byte {
numRecords := len(records)
buf := make([]byte, numRecords*ssgenRecordSize)
// Serialize and write each record into the slice sequentially.
for i := 0; i < numRecords; i++ {
recordBytes := serializeSSGenRecord(records[i])
copy(buf[i*ssgenRecordSize:(i+1)*ssgenRecordSize],
recordBytes)
}
return buf
}
// deserializeSSRtxRecord deserializes the passed serialized SSRtx
// record information.
func deserializeSSRtxRecord(serializedSSRtxRecord []byte) (*ssrtxRecord,
error) {
// Cursory check to make sure that the size of the
// record makes sense.
if len(serializedSSRtxRecord)%ssrtxRecordSize != 0 {
str := "serialized SSRtx record was wrong size"
return nil, stakeStoreError(apperrors.ErrDatabase, str, nil)
}
record := new(ssrtxRecord)
curPos := 0
// Insert the block hash into the record.
copy(record.blockHash[:], serializedSSRtxRecord[curPos:curPos+hashSize])
curPos += hashSize
// Insert the block height into the record.
record.blockHeight = binary.LittleEndian.Uint32(
serializedSSRtxRecord[curPos : curPos+int32Size])
curPos += int32Size
// Insert the tx hash into the record.
copy(record.txHash[:], serializedSSRtxRecord[curPos:curPos+hashSize])
curPos += hashSize
// Insert the timestamp into the record.
record.ts = time.Unix(
int64(binary.LittleEndian.Uint64(
serializedSSRtxRecord[curPos:curPos+int64Size])),
0)
return record, nil
}
// deserializeSSRtxRecords deserializes the passed serialized SSRtx
// records information.
func deserializeSSRtxRecords(serializedSSRtxRecords []byte) ([]*ssrtxRecord,
error) {
// Cursory check to make sure that the number of records
// makes sense.
if len(serializedSSRtxRecords)%ssrtxRecordSize != 0 {
err := io.ErrUnexpectedEOF
return nil, err
}
numRecords := len(serializedSSRtxRecords) / ssrtxRecordSize
records := make([]*ssrtxRecord, numRecords)
// Loop through all the ssgen records, deserialize them, and
// store them.
for i := 0; i < numRecords; i++ {
record, err := deserializeSSRtxRecord(
serializedSSRtxRecords[i*ssrtxRecordSize : (i+1)*ssrtxRecordSize])
if err != nil {
str := "problem serializing ssrtx record"
return nil, stakeStoreError(apperrors.ErrDatabase, str, err)
}
records[i] = record
}
return records, nil
}
// serializeSSRtxRecord returns the serialization of the passed
// SSRtx record.
func serializeSSRtxRecord(record *ssrtxRecord) []byte {
buf := make([]byte, ssrtxRecordSize)
curPos := 0
// Write the block hash.
copy(buf[curPos:curPos+hashSize], record.blockHash[:])
curPos += hashSize
// Write the block height.
binary.LittleEndian.PutUint32(buf[curPos:curPos+int32Size], record.blockHeight)
curPos += int32Size
// Write the tx hash.
copy(buf[curPos:curPos+hashSize], record.txHash[:])
curPos += hashSize
// Write the timestamp.
binary.LittleEndian.PutUint64(buf[curPos:curPos+int64Size], uint64(record.ts.Unix()))
return buf
}
// serializeSSRtxRecords returns the serialization of the passed
// SSRtx records.
func serializeSSRtxRecords(records []*ssrtxRecord) []byte {
numRecords := len(records)
buf := make([]byte, numRecords*ssrtxRecordSize)
// Serialize and write each record into the slice sequentially.
for i := 0; i < numRecords; i++ {
recordBytes := serializeSSRtxRecord(records[i])
copy(buf[i*ssrtxRecordSize:(i+1)*ssrtxRecordSize],
recordBytes)
}
return buf
}
// stakeStoreExists returns whether or not the stake store has already
// been created in the given database namespace.
func stakeStoreExists(ns walletdb.ReadBucket) bool {
mainBucket := ns.NestedReadBucket(mainBucketName)
return mainBucket != nil
}
// fetchSStxRecord retrieves a tx record from the sstx records bucket
// with the given hash.
func fetchSStxRecord(ns walletdb.ReadBucket, hash *chainhash.Hash, dbVersion uint32) (*sstxRecord, error) {
bucket := ns.NestedReadBucket(sstxRecordsBucketName)
key := hash[:]
val := bucket.Get(key)
if val == nil {
str := fmt.Sprintf("missing sstx record for hash '%s'", hash.String())
return nil, stakeStoreError(apperrors.ErrSStxNotFound, str, nil)
}
return deserializeSStxRecord(val, dbVersion)
}
// fetchSStxRecordSStxTicketHash160 retrieves a ticket 0th output script or
// pubkeyhash from the sstx records bucket with the given hash.
func fetchSStxRecordSStxTicketHash160(ns walletdb.ReadBucket, hash *chainhash.Hash,
dbVersion uint32) (hash160 []byte, p2sh bool, err error) {
bucket := ns.NestedReadBucket(sstxRecordsBucketName)
key := hash[:]
val := bucket.Get(key)
if val == nil {
str := fmt.Sprintf("missing sstx record for hash '%s'", hash.String())
return nil, false, stakeStoreError(apperrors.ErrSStxNotFound, str, nil)
}
return deserializeSStxTicketHash160(val, dbVersion)
}
// putSStxRecord inserts a given SStx record to the SStxrecords bucket.
func putSStxRecord(ns walletdb.ReadWriteBucket, record *sstxRecord, dbVersion uint32) error {
bucket := ns.NestedReadWriteBucket(sstxRecordsBucketName)
// Write the serialized txrecord keyed by the tx hash.
serializedSStxRecord, err := serializeSStxRecord(record, dbVersion)
if err != nil {
str := fmt.Sprintf("failed to serialize sstxrecord '%s'", record.tx.Hash())
return stakeStoreError(apperrors.ErrDatabase, str, err)
}
err = bucket.Put(record.tx.Hash()[:], serializedSStxRecord)
if err != nil {
str := fmt.Sprintf("failed to store sstxrecord '%s'", record.tx.Hash())
return stakeStoreError(apperrors.ErrDatabase, str, err)
}
return nil
}
// fetchSSGenRecords retrieves SSGen records from the SSGenRecords bucket with
// the given hash.
func fetchSSGenRecords(ns walletdb.ReadBucket, hash *chainhash.Hash) ([]*ssgenRecord, error) {
bucket := ns.NestedReadBucket(ssgenRecordsBucketName)
key := hash[:]
val := bucket.Get(key)
if val == nil {
str := fmt.Sprintf("missing ssgen records for hash '%s'", hash.String())
return nil, stakeStoreError(apperrors.ErrSSGensNotFound, str, nil)
}
return deserializeSSGenRecords(val)
}
// ssgenRecordExistsInRecords checks to see if a record already exists
// in a slice of ssgen records.
func ssgenRecordExistsInRecords(record *ssgenRecord, records []*ssgenRecord) bool {
for _, r := range records {
if r.txHash.IsEqual(&record.txHash) {
return true
}
}
return false
}
// putSSGenRecord updates an SSGen record in the SSGen records bucket.
func putSSGenRecord(ns walletdb.ReadWriteBucket, hash *chainhash.Hash, record *ssgenRecord) error {
// Fetch the current content of the key.
// Possible buggy behaviour: If deserialization fails,
// we won't detect it here. We assume we're throwing
// ErrSSGenNotFound.
oldRecords, _ := fetchSSGenRecords(ns, hash)
// Don't reinsert records we already have.
if ssgenRecordExistsInRecords(record, oldRecords) {
return nil
}
bucket := ns.NestedReadWriteBucket(ssgenRecordsBucketName)
var records []*ssgenRecord
// Either create a slice if currently nothing exists for this
// key in the db, or append the entry to the slice.
if oldRecords == nil {
records = make([]*ssgenRecord, 1)
records[0] = record
} else {
records = append(oldRecords, record)
}
// Write the serialized SSGens keyed by the sstx hash.
serializedSSGenRecords := serializeSSGenRecords(records)
err := bucket.Put(hash[:], serializedSSGenRecords)
if err != nil {
str := fmt.Sprintf("failed to store ssgen records '%s'", hash)
return stakeStoreError(apperrors.ErrDatabase, str, err)
}
return nil
}
// fetchSSRtxRecords retrieves SSRtx records from the SSRtxRecords bucket with
// the given hash.
func fetchSSRtxRecords(ns walletdb.ReadBucket, hash *chainhash.Hash) ([]*ssrtxRecord, error) {
bucket := ns.NestedReadBucket(ssrtxRecordsBucketName)
key := hash[:]
val := bucket.Get(key)
if val == nil {
str := fmt.Sprintf("missing ssrtx records for hash '%s'", hash.String())
return nil, stakeStoreError(apperrors.ErrSSRtxsNotFound, str, nil)
}
return deserializeSSRtxRecords(val)
}
// ssrtxRecordExistsInRecords checks to see if a record already exists
// in a slice of ssrtx records.
func ssrtxRecordExistsInRecords(record *ssrtxRecord, records []*ssrtxRecord) bool {
for _, r := range records {
if r.txHash.IsEqual(&record.txHash) {
return true
}
}
return false
}
// updateSSRtxRecord updates an SSRtx record in the SSRtx records bucket.
func updateSSRtxRecord(ns walletdb.ReadWriteBucket, hash *chainhash.Hash, record *ssrtxRecord) error {
// Fetch the current content of the key.
// Possible buggy behaviour: If deserialization fails,
// we won't detect it here. We assume we're throwing
// ErrSSRtxsNotFound.
oldRecords, _ := fetchSSRtxRecords(ns, hash)
// Don't reinsert records we already have.
if ssrtxRecordExistsInRecords(record, oldRecords) {
return nil
}
bucket := ns.NestedReadWriteBucket(ssrtxRecordsBucketName)
var records []*ssrtxRecord
// Either create a slice if currently nothing exists for this
// key in the db, or append the entry to the slice.
if oldRecords == nil {
records = make([]*ssrtxRecord, 1)
records[0] = record
} else {
records = append(oldRecords, record)
}
// Write the serialized SSRtxs keyed by the sstx hash.
serializedSSRtxsRecords := serializeSSRtxRecords(records)
err := bucket.Put(hash[:], serializedSSRtxsRecords)
if err != nil {
str := fmt.Sprintf("failed to store ssrtx records '%s'", hash)
return stakeStoreError(apperrors.ErrDatabase, str, err)
}
return nil
}
// putSSRtxRecord inserts a given SSRtxs record to the SSRtxs records bucket.
func putSSRtxRecord(ns walletdb.ReadWriteBucket, hash *chainhash.Hash, record *ssrtxRecord) error {
return updateSSRtxRecord(ns, hash, record)
}
// deserializeUserTicket deserializes the passed serialized user
// ticket information.
func deserializeUserTicket(serializedTicket []byte) (*PoolTicket, error) {
// Cursory check to make sure that the size of the
// ticket makes sense.
if len(serializedTicket)%stakePoolUserTicketSize != 0 {
str := "serialized pool ticket record was wrong size"
return nil, stakeStoreError(apperrors.ErrDatabase, str, nil)
}
record := new(PoolTicket)
curPos := 0
// Insert the ticket hash into the record.
copy(record.Ticket[:], serializedTicket[curPos:curPos+hashSize])
curPos += hashSize
// Insert the ticket height into the record.
record.HeightTicket = binary.LittleEndian.Uint32(
serializedTicket[curPos : curPos+int32Size])
curPos += int32Size
// Insert the status into the record.
record.Status = TicketStatus(serializedTicket[curPos])
curPos += int8Size
// Insert the spent by height into the record.
record.HeightSpent = binary.LittleEndian.Uint32(
serializedTicket[curPos : curPos+int32Size])
curPos += int32Size
// Insert the spending hash into the record.
copy(record.SpentBy[:], serializedTicket[curPos:curPos+hashSize])
return record, nil
}
// deserializeUserTickets deserializes the passed serialized pool
// users tickets information.
func deserializeUserTickets(serializedTickets []byte) ([]*PoolTicket, error) {
// Cursory check to make sure that the number of records
// makes sense.
if len(serializedTickets)%stakePoolUserTicketSize != 0 {
err := io.ErrUnexpectedEOF
return nil, err
}
numRecords := len(serializedTickets) / stakePoolUserTicketSize
records := make([]*PoolTicket, numRecords)
// Loop through all the records, deserialize them, and
// store them.
for i := 0; i < numRecords; i++ {
record, err := deserializeUserTicket(
serializedTickets[i*stakePoolUserTicketSize : (i+
1)*stakePoolUserTicketSize])
if err != nil {
str := "problem deserializing stake pool user tickets"
return nil, stakeStoreError(apperrors.ErrDatabase, str, err)
}
records[i] = record
}
return records, nil
}
// serializeUserTicket returns the serialization of a single stake pool
// user ticket.
func serializeUserTicket(record *PoolTicket) []byte {
buf := make([]byte, stakePoolUserTicketSize)
curPos := 0
// Write the ticket hash.
copy(buf[curPos:curPos+hashSize], record.Ticket[:])
curPos += hashSize
// Write the ticket block height.
binary.LittleEndian.PutUint32(buf[curPos:curPos+int32Size], record.HeightTicket)
curPos += int32Size
// Write the ticket status.
buf[curPos] = byte(record.Status)
curPos += int8Size
// Write the spending height.
binary.LittleEndian.PutUint32(buf[curPos:curPos+int32Size], record.HeightSpent)
curPos += int32Size
// Write the spending tx hash.
copy(buf[curPos:curPos+hashSize], record.SpentBy[:])
return buf
}
// serializeUserTickets returns the serialization of the passed stake pool
// user tickets slice.
func serializeUserTickets(records []*PoolTicket) []byte {
numRecords := len(records)
buf := make([]byte, numRecords*stakePoolUserTicketSize)
// Serialize and write each record into the slice sequentially.
for i := 0; i < numRecords; i++ {
recordBytes := serializeUserTicket(records[i])
copy(buf[i*stakePoolUserTicketSize:(i+1)*stakePoolUserTicketSize],
recordBytes)
}
return buf
}
// fetchStakePoolUserTickets retrieves pool user tickets from the meta bucket with
// the given hash.
func fetchStakePoolUserTickets(ns walletdb.ReadBucket, scriptHash [20]byte) ([]*PoolTicket, error) {
bucket := ns.NestedReadBucket(metaBucketName)
key := make([]byte, stakePoolTicketsPrefixSize+scriptHashSize)
copy(key[0:stakePoolTicketsPrefixSize], stakePoolTicketsPrefix)
copy(key[stakePoolTicketsPrefixSize:stakePoolTicketsPrefixSize+scriptHashSize],
scriptHash[:])
val := bucket.Get(key)
if val == nil {
str := fmt.Sprintf("missing pool user ticket records for hash '%x'",
scriptHash)
return nil, stakeStoreError(apperrors.ErrPoolUserTicketsNotFound, str, nil)
}
return deserializeUserTickets(val)
}
// duplicateExistsInUserTickets checks to see if an exact duplicated of a
// record already exists in a slice of user ticket records.
func duplicateExistsInUserTickets(record *PoolTicket, records []*PoolTicket) bool {
for _, r := range records {
if *r == *record {
return true
}
}
return false
}
// recordExistsInUserTickets checks to see if a record already exists
// in a slice of user ticket records. If it does exist, it returns
// the location where it exists in the slice.
func recordExistsInUserTickets(record *PoolTicket, records []*PoolTicket) (bool, int) {
for i, r := range records {
if r.Ticket == record.Ticket {
return true, i
}
}
return false, 0
}
// updateStakePoolUserTickets updates a database entry for a pool user's tickets.
// The function pulls the current entry in the database, checks to see if the
// ticket is already there, updates it accordingly, or adds it to the list of
// tickets.
func updateStakePoolUserTickets(ns walletdb.ReadWriteBucket, scriptHash [20]byte, record *PoolTicket) error {
// Fetch the current content of the key.
// Possible buggy behaviour: If deserialization fails,
// we won't detect it here. We assume we're throwing
// ErrPoolUserTicketsNotFound.
oldRecords, _ := fetchStakePoolUserTickets(ns, scriptHash)
// Don't reinsert duplicate records we already have.
if duplicateExistsInUserTickets(record, oldRecords) {
return nil
}
// Does this modify an old record? If so, modify the record
// itself and push. Otherwise, we need to insert a new
// record.
var records []*PoolTicket
preExists, loc := recordExistsInUserTickets(record, oldRecords)
if preExists {
records = oldRecords
records[loc] = record
} else {
// Either create a slice if currently nothing exists for this
// key in the db, or append the entry to the slice.
if oldRecords == nil {
records = make([]*PoolTicket, 1)
records[0] = record
} else {
records = append(oldRecords, record)
}
}
bucket := ns.NestedReadWriteBucket(metaBucketName)
key := make([]byte, stakePoolTicketsPrefixSize+scriptHashSize)
copy(key[0:stakePoolTicketsPrefixSize], stakePoolTicketsPrefix)
copy(key[stakePoolTicketsPrefixSize:stakePoolTicketsPrefixSize+scriptHashSize],
scriptHash[:])
// Write the serialized ticket data keyed by the script.
serializedRecords := serializeUserTickets(records)
err := bucket.Put(key, serializedRecords)
if err != nil {
str := fmt.Sprintf("failed to store pool user ticket records '%x'",
scriptHash)
return stakeStoreError(apperrors.ErrDatabase, str, err)
}
return nil
}
// deserializeUserInvalTickets deserializes the passed serialized pool
// users invalid tickets information.
func deserializeUserInvalTickets(serializedTickets []byte) ([]*chainhash.Hash, error) {
// Cursory check to make sure that the number of records
// makes sense.
if len(serializedTickets)%chainhash.HashSize != 0 {
err := io.ErrUnexpectedEOF
return nil, err
}
numRecords := len(serializedTickets) / chainhash.HashSize
records := make([]*chainhash.Hash, numRecords)
// Loop through all the ssgen records, deserialize them, and
// store them.
for i := 0; i < numRecords; i++ {
start := i * chainhash.HashSize
end := (i + 1) * chainhash.HashSize
h, err := chainhash.NewHash(serializedTickets[start:end])
if err != nil {
str := "problem deserializing stake pool invalid user tickets"
return nil, stakeStoreError(apperrors.ErrDatabase, str, err)
}
records[i] = h
}
return records, nil
}
// serializeUserInvalTickets returns the serialization of the passed stake pool
// invalid user tickets slice.
func serializeUserInvalTickets(records []*chainhash.Hash) []byte {
numRecords := len(records)
buf := make([]byte, numRecords*chainhash.HashSize)
// Serialize and write each record into the slice sequentially.
for i := 0; i < numRecords; i++ {
start := i * chainhash.HashSize
end := (i + 1) * chainhash.HashSize
copy(buf[start:end], records[i][:])
}
return buf
}
// fetchStakePoolUserInvalTickets retrieves the list of invalid pool user tickets
// from the meta bucket with the given hash.
func fetchStakePoolUserInvalTickets(ns walletdb.ReadBucket, scriptHash [20]byte) ([]*chainhash.Hash, error) {
bucket := ns.NestedReadBucket(metaBucketName)
key := make([]byte, stakePoolInvalidPrefixSize+scriptHashSize)
copy(key[0:stakePoolInvalidPrefixSize], stakePoolInvalidPrefix)
copy(key[stakePoolInvalidPrefixSize:stakePoolInvalidPrefixSize+scriptHashSize],
scriptHash[:])
val := bucket.Get(key)
if val == nil {
str := fmt.Sprintf("missing pool user invalid ticket records "+
"for hash '%x'", scriptHash)
return nil, stakeStoreError(apperrors.ErrPoolUserInvalTcktsNotFound, str, nil)
}
return deserializeUserInvalTickets(val)
}