forked from cruzbit/cruzbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger_disk.go
1085 lines (951 loc) · 27 KB
/
ledger_disk.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 2019 cruzbit developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
package cruzbit
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
"golang.org/x/crypto/ed25519"
)
// LedgerDisk is an on-disk implemenation of the Ledger interface using LevelDB.
type LedgerDisk struct {
db *leveldb.DB
blockStore BlockStorage
prune bool // prune historic transaction and public key transaction indices
}
// NewLedgerDisk returns a new instance of LedgerDisk.
func NewLedgerDisk(dbPath string, readOnly, prune bool, blockStore BlockStorage) (*LedgerDisk, error) {
opts := opt.Options{ReadOnly: readOnly}
db, err := leveldb.OpenFile(dbPath, &opts)
if err != nil {
return nil, err
}
return &LedgerDisk{db: db, blockStore: blockStore, prune: prune}, nil
}
// GetChainTip returns the ID and the height of the block at the current tip of the main chain.
func (l LedgerDisk) GetChainTip() (*BlockID, int64, error) {
return getChainTip(l.db)
}
// Sometimes we call this with *leveldb.DB or *leveldb.Snapshot
func getChainTip(db leveldb.Reader) (*BlockID, int64, error) {
// compute db key
key, err := computeChainTipKey()
if err != nil {
return nil, 0, err
}
// fetch the id
ctBytes, err := db.Get(key, nil)
if err == leveldb.ErrNotFound {
return nil, 0, nil
}
if err != nil {
return nil, 0, err
}
// decode the tip
id, height, err := decodeChainTip(ctBytes)
if err != nil {
return nil, 0, err
}
return id, height, nil
}
// GetBlockIDForHeight returns the ID of the block at the given block chain height.
func (l LedgerDisk) GetBlockIDForHeight(height int64) (*BlockID, error) {
return getBlockIDForHeight(height, l.db)
}
// Sometimes we call this with *leveldb.DB or *leveldb.Snapshot
func getBlockIDForHeight(height int64, db leveldb.Reader) (*BlockID, error) {
// compute db key
key, err := computeBlockHeightIndexKey(height)
if err != nil {
return nil, err
}
// fetch the id
idBytes, err := db.Get(key, nil)
if err == leveldb.ErrNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
// return it
id := new(BlockID)
copy(id[:], idBytes)
return id, nil
}
// SetBranchType sets the branch type for the given block.
func (l LedgerDisk) SetBranchType(id BlockID, branchType BranchType) error {
// compute db key
key, err := computeBranchTypeKey(id)
if err != nil {
return err
}
// write type
wo := opt.WriteOptions{Sync: true}
return l.db.Put(key, []byte{byte(branchType)}, &wo)
}
// GetBranchType returns the branch type for the given block.
func (l LedgerDisk) GetBranchType(id BlockID) (BranchType, error) {
// compute db key
key, err := computeBranchTypeKey(id)
if err != nil {
return UNKNOWN, err
}
// fetch type
branchType, err := l.db.Get(key, nil)
if err == leveldb.ErrNotFound {
return UNKNOWN, nil
}
if err != nil {
return UNKNOWN, err
}
return BranchType(branchType[0]), nil
}
// ConnectBlock connects a block to the tip of the block chain and applies the transactions to the ledger.
func (l LedgerDisk) ConnectBlock(id BlockID, block *Block) ([]TransactionID, error) {
// sanity check
tipID, _, err := l.GetChainTip()
if err != nil {
return nil, err
}
if tipID != nil && *tipID != block.Header.Previous {
return nil, fmt.Errorf("Being asked to connect %s but previous %s does not match tip %s",
id, block.Header.Previous, *tipID)
}
// apply all resulting writes atomically
batch := new(leveldb.Batch)
balanceCache := NewBalanceCache(l, 0)
txIDs := make([]TransactionID, len(block.Transactions))
for i, tx := range block.Transactions {
txID, err := tx.ID()
if err != nil {
return nil, err
}
txIDs[i] = txID
// verify the transaction hasn't been processed already.
// note that we can safely prune indices for transactions older than the previous series
key, err := computeTransactionIndexKey(txID)
if err != nil {
return nil, err
}
ok, err := l.db.Has(key, nil)
if err != nil {
return nil, err
}
if ok {
return nil, fmt.Errorf("Transaction %s already processed", txID)
}
// set the transaction index now
indexBytes, err := encodeTransactionIndex(block.Header.Height, i)
if err != nil {
return nil, err
}
batch.Put(key, indexBytes)
txToApply := tx
if tx.IsCoinbase() {
// don't apply a coinbase to a balance until it's 100 blocks deep.
// during honest reorgs normal transactions usually get into the new most-work branch
// but coinbases vanish. this mitigates the impact on UX when reorgs occur and transactions
// depend on coinbases.
txToApply = nil
if block.Header.Height-COINBASE_MATURITY >= 0 {
// mature the coinbase from 100 blocks ago now
oldID, err := l.GetBlockIDForHeight(block.Header.Height - COINBASE_MATURITY)
if err != nil {
return nil, err
}
if oldID == nil {
return nil, fmt.Errorf("Missing block at height %d\n",
block.Header.Height-COINBASE_MATURITY)
}
// we could store the last 100 coinbases on our own in memory if we end up needing to
oldTx, _, err := l.blockStore.GetTransaction(*oldID, 0)
if err != nil {
return nil, err
}
if oldTx == nil {
return nil, fmt.Errorf("Missing coinbase from block %s\n", *oldID)
}
// apply it to the recipient's balance
txToApply = oldTx
}
}
if txToApply != nil {
// check sender balance and update sender and receiver balances
ok, err := balanceCache.Apply(txToApply)
if err != nil {
return nil, err
}
if !ok {
txID, _ := txToApply.ID()
return nil, fmt.Errorf("Sender has insuffcient balance in transaction %s", txID)
}
}
// associate this transaction with both parties
if !tx.IsCoinbase() {
key, err = computePubKeyTransactionIndexKey(tx.From, &block.Header.Height, &i)
if err != nil {
return nil, err
}
batch.Put(key, []byte{0x1})
}
key, err = computePubKeyTransactionIndexKey(tx.To, &block.Header.Height, &i)
if err != nil {
return nil, err
}
batch.Put(key, []byte{0x1})
}
// update recorded balances
balances := balanceCache.Balances()
for pubKeyBytes, balance := range balances {
key, err := computePubKeyBalanceKey(ed25519.PublicKey(pubKeyBytes[:]))
if err != nil {
return nil, err
}
if balance == 0 {
batch.Delete(key)
} else {
balanceBytes, err := encodeNumber(balance)
if err != nil {
return nil, err
}
batch.Put(key, balanceBytes)
}
}
// index the block by height
key, err := computeBlockHeightIndexKey(block.Header.Height)
if err != nil {
return nil, err
}
batch.Put(key, id[:])
// set this block on the main chain
key, err = computeBranchTypeKey(id)
if err != nil {
return nil, err
}
batch.Put(key, []byte{byte(MAIN)})
// set this block as the new tip
key, err = computeChainTipKey()
if err != nil {
return nil, err
}
ctBytes, err := encodeChainTip(id, block.Header.Height)
if err != nil {
return nil, err
}
batch.Put(key, ctBytes)
// prune historic transaction and public key transaction indices now
if l.prune && block.Header.Height >= 2*BLOCKS_UNTIL_NEW_SERIES {
if err := l.pruneIndices(block.Header.Height-2*BLOCKS_UNTIL_NEW_SERIES, batch); err != nil {
return nil, err
}
}
// perform the writes
wo := opt.WriteOptions{Sync: true}
if err := l.db.Write(batch, &wo); err != nil {
return nil, err
}
return txIDs, nil
}
// DisconnectBlock disconnects a block from the tip of the block chain and undoes the effects of the transactions on the ledger.
func (l LedgerDisk) DisconnectBlock(id BlockID, block *Block) ([]TransactionID, error) {
// sanity check
tipID, _, err := l.GetChainTip()
if err != nil {
return nil, err
}
if tipID == nil {
return nil, fmt.Errorf("Being asked to disconnect %s but no tip is currently set",
id)
}
if *tipID != id {
return nil, fmt.Errorf("Being asked to disconnect %s but it does not match tip %s",
id, *tipID)
}
// apply all resulting writes atomically
batch := new(leveldb.Batch)
balanceCache := NewBalanceCache(l, 0)
txIDs := make([]TransactionID, len(block.Transactions))
// disconnect transactions in reverse order
for i := len(block.Transactions) - 1; i >= 0; i-- {
tx := block.Transactions[i]
txID, err := tx.ID()
if err != nil {
return nil, err
}
// save the id
txIDs[i] = txID
// mark the transaction unprocessed now (delete its index)
key, err := computeTransactionIndexKey(txID)
if err != nil {
return nil, err
}
batch.Delete(key)
txToUndo := tx
if tx.IsCoinbase() {
// coinbase doesn't affect recipient balance for 100 more blocks
txToUndo = nil
if block.Header.Height-COINBASE_MATURITY >= 0 {
// undo the effect of the coinbase from 100 blocks ago now
oldID, err := l.GetBlockIDForHeight(block.Header.Height - COINBASE_MATURITY)
if err != nil {
return nil, err
}
if oldID == nil {
return nil, fmt.Errorf("Missing block at height %d\n",
block.Header.Height-COINBASE_MATURITY)
}
oldTx, _, err := l.blockStore.GetTransaction(*oldID, 0)
if err != nil {
return nil, err
}
if oldTx == nil {
return nil, fmt.Errorf("Missing coinbase from block %s\n", *oldID)
}
// undo its effect on the recipient's balance
txToUndo = oldTx
}
}
if txToUndo != nil {
// credit sender and debit recipient
err = balanceCache.Undo(txToUndo)
if err != nil {
return nil, err
}
}
// unassociate this transaction with both parties
if !tx.IsCoinbase() {
key, err = computePubKeyTransactionIndexKey(tx.From, &block.Header.Height, &i)
if err != nil {
return nil, err
}
batch.Delete(key)
}
key, err = computePubKeyTransactionIndexKey(tx.To, &block.Header.Height, &i)
if err != nil {
return nil, err
}
batch.Delete(key)
}
// update recorded balances
balances := balanceCache.Balances()
for pubKeyBytes, balance := range balances {
key, err := computePubKeyBalanceKey(ed25519.PublicKey(pubKeyBytes[:]))
if err != nil {
return nil, err
}
if balance == 0 {
batch.Delete(key)
} else {
balanceBytes, err := encodeNumber(balance)
if err != nil {
return nil, err
}
batch.Put(key, balanceBytes)
}
}
// remove this block's index by height
key, err := computeBlockHeightIndexKey(block.Header.Height)
if err != nil {
return nil, err
}
batch.Delete(key)
// set this block on a side chain
key, err = computeBranchTypeKey(id)
if err != nil {
return nil, err
}
batch.Put(key, []byte{byte(SIDE)})
// set the previous block as the chain tip
key, err = computeChainTipKey()
if err != nil {
return nil, err
}
ctBytes, err := encodeChainTip(block.Header.Previous, block.Header.Height-1)
if err != nil {
return nil, err
}
batch.Put(key, ctBytes)
// restore historic indices now
if l.prune && block.Header.Height >= 2*BLOCKS_UNTIL_NEW_SERIES {
if err := l.restoreIndices(block.Header.Height-2*BLOCKS_UNTIL_NEW_SERIES, batch); err != nil {
return nil, err
}
}
// perform the writes
wo := opt.WriteOptions{Sync: true}
if err := l.db.Write(batch, &wo); err != nil {
return nil, err
}
return txIDs, nil
}
// Prune transaction and public key transaction indices created by the block at the given height
func (l LedgerDisk) pruneIndices(height int64, batch *leveldb.Batch) error {
// get the ID
id, err := l.GetBlockIDForHeight(height)
if err != nil {
return err
}
if id == nil {
return fmt.Errorf("Missing block ID for height %d\n", height)
}
// fetch the block
block, err := l.blockStore.GetBlock(*id)
if err != nil {
return err
}
if block == nil {
return fmt.Errorf("Missing block %s\n", *id)
}
for i, tx := range block.Transactions {
txID, err := tx.ID()
if err != nil {
return err
}
// prune transaction index
key, err := computeTransactionIndexKey(txID)
if err != nil {
return err
}
batch.Delete(key)
// prune public key transaction indices
if !tx.IsCoinbase() {
key, err = computePubKeyTransactionIndexKey(tx.From, &block.Header.Height, &i)
if err != nil {
return err
}
batch.Delete(key)
}
key, err = computePubKeyTransactionIndexKey(tx.To, &block.Header.Height, &i)
if err != nil {
return err
}
batch.Delete(key)
}
return nil
}
// Restore transaction and public key transaction indices created by the block at the given height
func (l LedgerDisk) restoreIndices(height int64, batch *leveldb.Batch) error {
// get the ID
id, err := l.GetBlockIDForHeight(height)
if err != nil {
return err
}
if id == nil {
return fmt.Errorf("Missing block ID for height %d\n", height)
}
// fetch the block
block, err := l.blockStore.GetBlock(*id)
if err != nil {
return err
}
if block == nil {
return fmt.Errorf("Missing block %s\n", *id)
}
for i, tx := range block.Transactions {
txID, err := tx.ID()
if err != nil {
return err
}
// restore transaction index
key, err := computeTransactionIndexKey(txID)
if err != nil {
return err
}
indexBytes, err := encodeTransactionIndex(block.Header.Height, i)
if err != nil {
return err
}
batch.Put(key, indexBytes)
// restore public key transaction indices
if !tx.IsCoinbase() {
key, err = computePubKeyTransactionIndexKey(tx.From, &block.Header.Height, &i)
if err != nil {
return err
}
batch.Put(key, []byte{0x1})
}
key, err = computePubKeyTransactionIndexKey(tx.To, &block.Header.Height, &i)
if err != nil {
return err
}
batch.Put(key, []byte{0x1})
}
return nil
}
// GetPublicKeyBalance returns the current balance of a given public key.
func (l LedgerDisk) GetPublicKeyBalance(pubKey ed25519.PublicKey) (int64, error) {
// compute db key
key, err := computePubKeyBalanceKey(pubKey)
if err != nil {
return 0, err
}
// fetch balance
balanceBytes, err := l.db.Get(key, nil)
if err == leveldb.ErrNotFound {
return 0, nil
}
if err != nil {
return 0, err
}
// decode and return it
var balance int64
buf := bytes.NewReader(balanceBytes)
binary.Read(buf, binary.BigEndian, &balance)
return balance, nil
}
// GetPublicKeyBalances returns the current balance of the given public keys
// along with block ID and height of the corresponding main chain tip.
func (l LedgerDisk) GetPublicKeyBalances(pubKeys []ed25519.PublicKey) (
map[[ed25519.PublicKeySize]byte]int64, *BlockID, int64, error) {
// get a consistent view across all queries
snapshot, err := l.db.GetSnapshot()
if err != nil {
return nil, nil, 0, err
}
defer snapshot.Release()
// get the chain tip
tipID, tipHeight, err := getChainTip(snapshot)
if err != nil {
return nil, nil, 0, err
}
balances := make(map[[ed25519.PublicKeySize]byte]int64)
for _, pubKey := range pubKeys {
// compute balance db key
key, err := computePubKeyBalanceKey(pubKey)
if err != nil {
return nil, nil, 0, err
}
var pk [ed25519.PublicKeySize]byte
copy(pk[:], pubKey)
// fetch balance
balanceBytes, err := snapshot.Get(key, nil)
if err == leveldb.ErrNotFound {
balances[pk] = 0
continue
}
if err != nil {
return nil, nil, 0, err
}
// decode it
var balance int64
buf := bytes.NewReader(balanceBytes)
binary.Read(buf, binary.BigEndian, &balance)
// save it
balances[pk] = balance
}
return balances, tipID, tipHeight, nil
}
// GetTransactionIndex returns the index of a processed transaction.
func (l LedgerDisk) GetTransactionIndex(id TransactionID) (*BlockID, int, error) {
// compute the db key
key, err := computeTransactionIndexKey(id)
if err != nil {
return nil, 0, err
}
// we want a consistent view during our two queries as height can change
snapshot, err := l.db.GetSnapshot()
if err != nil {
return nil, 0, err
}
defer snapshot.Release()
// fetch and decode the index
indexBytes, err := snapshot.Get(key, nil)
if err == leveldb.ErrNotFound {
return nil, 0, nil
}
if err != nil {
return nil, 0, err
}
height, index, err := decodeTransactionIndex(indexBytes)
if err != nil {
return nil, 0, err
}
// map height to block id
blockID, err := getBlockIDForHeight(height, snapshot)
if err != nil {
return nil, 0, err
}
// return it
return blockID, index, nil
}
// GetPublicKeyTransactionIndicesRange returns transaction indices involving a given public key
// over a range of heights. If startHeight > endHeight this iterates in reverse.
func (l LedgerDisk) GetPublicKeyTransactionIndicesRange(
pubKey ed25519.PublicKey, startHeight, endHeight int64, startIndex, limit int) (
[]BlockID, []int, int64, int, error) {
if endHeight >= startHeight {
// forward
return l.getPublicKeyTransactionIndicesRangeForward(
pubKey, startHeight, endHeight, startIndex, limit)
}
// reverse
return l.getPublicKeyTransactionIndicesRangeReverse(
pubKey, startHeight, endHeight, startIndex, limit)
}
// Iterate through transaction history going forward
func (l LedgerDisk) getPublicKeyTransactionIndicesRangeForward(
pubKey ed25519.PublicKey, startHeight, endHeight int64, startIndex, limit int) (
ids []BlockID, indices []int, lastHeight int64, lastIndex int, err error) {
startKey, err := computePubKeyTransactionIndexKey(pubKey, &startHeight, &startIndex)
if err != nil {
return
}
endHeight += 1 // make it inclusive
endKey, err := computePubKeyTransactionIndexKey(pubKey, &endHeight, nil)
if err != nil {
return
}
heightMap := make(map[int64]*BlockID)
// we want a consistent view of this. heights can change out from under us otherwise
snapshot, err := l.db.GetSnapshot()
if err != nil {
return
}
defer snapshot.Release()
iter := snapshot.NewIterator(&util.Range{Start: startKey, Limit: endKey}, nil)
for iter.Next() {
_, lastHeight, lastIndex, err = decodePubKeyTransactionIndexKey(iter.Key())
if err != nil {
iter.Release()
return nil, nil, 0, 0, err
}
// lookup the block id
id, ok := heightMap[lastHeight]
if !ok {
var err error
id, err = getBlockIDForHeight(lastHeight, snapshot)
if err != nil {
iter.Release()
return nil, nil, 0, 0, err
}
if id == nil {
iter.Release()
return nil, nil, 0, 0, fmt.Errorf(
"No block found at height %d", lastHeight)
}
heightMap[lastHeight] = id
}
ids = append(ids, *id)
indices = append(indices, lastIndex)
if limit != 0 && len(indices) == limit {
break
}
}
iter.Release()
if err := iter.Error(); err != nil {
return nil, nil, 0, 0, err
}
return
}
// Iterate through transaction history in reverse
func (l LedgerDisk) getPublicKeyTransactionIndicesRangeReverse(
pubKey ed25519.PublicKey, startHeight, endHeight int64, startIndex, limit int) (
ids []BlockID, indices []int, lastHeight int64, lastIndex int, err error) {
endKey, err := computePubKeyTransactionIndexKey(pubKey, &endHeight, nil)
if err != nil {
return
}
// make it inclusive
if startIndex == 0 {
startHeight += 1
}
startKey, err := computePubKeyTransactionIndexKey(pubKey, &startHeight, &startIndex)
if err != nil {
return
}
heightMap := make(map[int64]*BlockID)
// we want a consistent view of this. heights can change out from under us otherwise
snapshot, err := l.db.GetSnapshot()
if err != nil {
return
}
defer snapshot.Release()
iter := snapshot.NewIterator(&util.Range{Start: endKey, Limit: startKey}, nil)
for ok := iter.Last(); ok; ok = iter.Prev() {
_, lastHeight, lastIndex, err = decodePubKeyTransactionIndexKey(iter.Key())
if err != nil {
iter.Release()
return nil, nil, 0, 0, err
}
// lookup the block id
id, ok := heightMap[lastHeight]
if !ok {
var err error
id, err = getBlockIDForHeight(lastHeight, snapshot)
if err != nil {
iter.Release()
return nil, nil, 0, 0, err
}
if id == nil {
iter.Release()
return nil, nil, 0, 0, fmt.Errorf(
"No block found at height %d", lastHeight)
}
heightMap[lastHeight] = id
}
ids = append(ids, *id)
indices = append(indices, lastIndex)
if limit != 0 && len(indices) == limit {
break
}
}
iter.Release()
if err := iter.Error(); err != nil {
return nil, nil, 0, 0, err
}
return
}
// Balance returns the total current ledger balance by summing the balance of all public keys.
// It's only used offline for verification purposes.
func (l LedgerDisk) Balance() (int64, error) {
var total int64
// compute the sum of all public key balances
key, err := computePubKeyBalanceKey(nil)
if err != nil {
return 0, err
}
iter := l.db.NewIterator(util.BytesPrefix(key), nil)
for iter.Next() {
var balance int64
buf := bytes.NewReader(iter.Value())
binary.Read(buf, binary.BigEndian, &balance)
total += balance
}
iter.Release()
if err := iter.Error(); err != nil {
return 0, err
}
return total, nil
}
// GetPublicKeyBalanceAt returns the public key balance at the given height.
// It's only used offline for historical and verification purposes.
// This is only accurate when the full block chain is indexed (pruning disabled.)
func (l LedgerDisk) GetPublicKeyBalanceAt(pubKey ed25519.PublicKey, height int64) (int64, error) {
_, currentHeight, err := l.GetChainTip()
if err != nil {
return 0, err
}
startKey, err := computePubKeyTransactionIndexKey(pubKey, nil, nil)
if err != nil {
return 0, err
}
height += 1 // make it inclusive
endKey, err := computePubKeyTransactionIndexKey(pubKey, &height, nil)
if err != nil {
return 0, err
}
var balance int64
iter := l.db.NewIterator(&util.Range{Start: startKey, Limit: endKey}, nil)
for iter.Next() {
_, height, index, err := decodePubKeyTransactionIndexKey(iter.Key())
if err != nil {
iter.Release()
return 0, err
}
if index == 0 && height > currentHeight-COINBASE_MATURITY {
// coinbase isn't mature
continue
}
id, err := l.GetBlockIDForHeight(height)
if err != nil {
iter.Release()
return 0, err
}
if id == nil {
iter.Release()
return 0, fmt.Errorf("No block found at height %d", height)
}
tx, _, err := l.blockStore.GetTransaction(*id, index)
if err != nil {
iter.Release()
return 0, err
}
if tx == nil {
iter.Release()
return 0, fmt.Errorf("No transaction found in block %s at index %d",
*id, index)
}
if bytes.Equal(pubKey, tx.To) {
balance += tx.Amount
} else if bytes.Equal(pubKey, tx.From) {
balance -= tx.Amount
balance -= tx.Fee
if balance < 0 {
iter.Release()
txID, _ := tx.ID()
return 0, fmt.Errorf("Balance went negative at transaction %s", txID)
}
} else {
iter.Release()
txID, _ := tx.ID()
return 0, fmt.Errorf("Transaction %s doesn't involve the public key", txID)
}
}
iter.Release()
if err := iter.Error(); err != nil {
return 0, err
}
return balance, nil
}
// Close is called to close any underlying storage.
func (l LedgerDisk) Close() error {
return l.db.Close()
}
// leveldb schema
// T -> {bid}{height} (main chain tip)
// B{bid} -> main|side|orphan (1 byte)
// h{height} -> {bid}
// t{txid} -> {height}{index} (prunable up to the previous series)
// k{pk}{height}{index} -> 1 (not strictly necessary. probably should make it optional by flag)
// b{pk} -> {balance} (we always need all of this table)
const chainTipPrefix = 'T'
const branchTypePrefix = 'B'
const blockHeightIndexPrefix = 'h'
const transactionIndexPrefix = 't'
const pubKeyTransactionIndexPrefix = 'k'
const pubKeyBalancePrefix = 'b'
func computeBranchTypeKey(id BlockID) ([]byte, error) {
key := new(bytes.Buffer)
if err := key.WriteByte(branchTypePrefix); err != nil {
return nil, err
}
if err := binary.Write(key, binary.BigEndian, id[:]); err != nil {
return nil, err
}
return key.Bytes(), nil
}
func computeBlockHeightIndexKey(height int64) ([]byte, error) {
key := new(bytes.Buffer)
if err := key.WriteByte(blockHeightIndexPrefix); err != nil {
return nil, err
}
if err := binary.Write(key, binary.BigEndian, height); err != nil {
return nil, err
}
return key.Bytes(), nil
}
func computeChainTipKey() ([]byte, error) {
key := new(bytes.Buffer)
if err := key.WriteByte(chainTipPrefix); err != nil {
return nil, err
}
return key.Bytes(), nil
}
func computeTransactionIndexKey(id TransactionID) ([]byte, error) {
key := new(bytes.Buffer)
if err := key.WriteByte(transactionIndexPrefix); err != nil {
return nil, err
}
if err := binary.Write(key, binary.BigEndian, id[:]); err != nil {
return nil, err
}
return key.Bytes(), nil
}
func computePubKeyTransactionIndexKey(
pubKey ed25519.PublicKey, height *int64, index *int) ([]byte, error) {
key := new(bytes.Buffer)
if err := key.WriteByte(pubKeyTransactionIndexPrefix); err != nil {
return nil, err
}
if err := binary.Write(key, binary.BigEndian, pubKey); err != nil {
return nil, err
}
if height == nil {
return key.Bytes(), nil
}
if err := binary.Write(key, binary.BigEndian, *height); err != nil {
return nil, err
}
if index == nil {
return key.Bytes(), nil
}
index32 := int32(*index)
if err := binary.Write(key, binary.BigEndian, index32); err != nil {
return nil, err
}
return key.Bytes(), nil
}
func decodePubKeyTransactionIndexKey(key []byte) (ed25519.PublicKey, int64, int, error) {
buf := bytes.NewBuffer(key)