-
Notifications
You must be signed in to change notification settings - Fork 211
/
meshdb.go
871 lines (771 loc) · 26.4 KB
/
meshdb.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
package mesh
import (
"container/list"
"encoding/hex"
"errors"
"fmt"
"math/big"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/database"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/pendingtxs"
)
type layerMutex struct {
m sync.Mutex
layerWorkers uint32
}
// DB represents a mesh database instance
type DB struct {
log.Log
blockCache blockCache
layers database.Database
blocks database.Database
transactions database.Database
contextualValidity database.Database
general database.Database
unappliedTxs database.Database
unappliedTxsMutex sync.Mutex
blockMutex sync.RWMutex
orphanBlocks map[types.LayerID]map[types.BlockID]struct{}
layerMutex map[types.LayerID]*layerMutex
lhMutex sync.Mutex
exit chan struct{}
}
// NewPersistentMeshDB creates an instance of a mesh database
func NewPersistentMeshDB(path string, blockCacheSize int, log log.Log) (*DB, error) {
bdb, err := database.NewLDBDatabase(filepath.Join(path, "blocks"), 0, 0, log)
if err != nil {
return nil, fmt.Errorf("failed to initialize blocks db: %v", err)
}
ldb, err := database.NewLDBDatabase(filepath.Join(path, "layers"), 0, 0, log)
if err != nil {
return nil, fmt.Errorf("failed to initialize layers db: %v", err)
}
vdb, err := database.NewLDBDatabase(filepath.Join(path, "validity"), 0, 0, log)
if err != nil {
return nil, fmt.Errorf("failed to initialize validity db: %v", err)
}
tdb, err := database.NewLDBDatabase(filepath.Join(path, "transactions"), 0, 0, log)
if err != nil {
return nil, fmt.Errorf("failed to initialize transactions db: %v", err)
}
gdb, err := database.NewLDBDatabase(filepath.Join(path, "general"), 0, 0, log)
if err != nil {
return nil, fmt.Errorf("failed to initialize general db: %v", err)
}
utx, err := database.NewLDBDatabase(filepath.Join(path, "unappliedTxs"), 0, 0, log)
if err != nil {
return nil, fmt.Errorf("failed to initialize mesh unappliedTxs db: %v", err)
}
ll := &DB{
Log: log,
blockCache: newBlockCache(blockCacheSize * layerSize),
blocks: bdb,
layers: ldb,
transactions: tdb,
general: gdb,
contextualValidity: vdb,
unappliedTxs: utx,
orphanBlocks: make(map[types.LayerID]map[types.BlockID]struct{}),
layerMutex: make(map[types.LayerID]*layerMutex),
exit: make(chan struct{}),
}
ll.AddBlock(GenesisBlock())
ll.SaveContextualValidity(GenesisBlock().ID(), true)
return ll, nil
}
// PersistentData checks to see if db is empty
func (m *DB) PersistentData() bool {
if _, err := m.general.Get(constLATEST); err == nil {
m.Info("found data to recover on disc")
return true
}
m.Info("did not find data to recover on disc")
return false
}
// NewMemMeshDB is a mock used for testing
func NewMemMeshDB(log log.Log) *DB {
ll := &DB{
Log: log,
blockCache: newBlockCache(100 * layerSize),
blocks: database.NewMemDatabase(),
layers: database.NewMemDatabase(),
general: database.NewMemDatabase(),
contextualValidity: database.NewMemDatabase(),
transactions: database.NewMemDatabase(),
unappliedTxs: database.NewMemDatabase(),
orphanBlocks: make(map[types.LayerID]map[types.BlockID]struct{}),
layerMutex: make(map[types.LayerID]*layerMutex),
exit: make(chan struct{}),
}
ll.AddBlock(GenesisBlock())
ll.SaveContextualValidity(GenesisBlock().ID(), true)
return ll
}
// Close closes all resources
func (m *DB) Close() {
close(m.exit)
m.blocks.Close()
m.layers.Close()
m.transactions.Close()
m.unappliedTxs.Close()
m.general.Close()
m.contextualValidity.Close()
}
// ErrAlreadyExist error returned when adding an existing value to the database
var ErrAlreadyExist = errors.New("block already exists in database")
// AddBlock adds a block to the database
func (m *DB) AddBlock(bl *types.Block) error {
m.blockMutex.Lock()
defer m.blockMutex.Unlock()
if _, err := m.getBlockBytes(bl.ID()); err == nil {
m.With().Warning(ErrAlreadyExist.Error(), bl.ID())
return ErrAlreadyExist
}
if err := m.writeBlock(bl); err != nil {
return err
}
return nil
}
// GetBlock gets a block from the database by id
func (m *DB) GetBlock(id types.BlockID) (*types.Block, error) {
if blkh := m.blockCache.Get(id); blkh != nil {
return blkh, nil
}
b, err := m.getBlockBytes(id)
if err != nil {
return nil, err
}
mbk := &types.Block{}
err = types.BytesToInterface(b, mbk)
mbk.Initialize()
return mbk, err
}
// LayerBlocks retrieves all blocks from a layer by layer index
func (m *DB) LayerBlocks(index types.LayerID) ([]*types.Block, error) {
ids, err := m.LayerBlockIds(index)
if err != nil {
return nil, err
}
blocks := make([]*types.Block, 0, len(ids))
for _, k := range ids {
block, err := m.GetBlock(k)
if err != nil {
return nil, fmt.Errorf("could not retrieve block %s %s", k.String(), err)
}
blocks = append(blocks, block)
}
return blocks, nil
}
// ForBlockInView traverses all blocks in a view and uses blockHandler func on each block
// The block handler func should return two values - a bool indicating whether or not we should stop traversing after the current block (happy flow)
// and an error indicating that an error occurred while handling the block, the traversing will stop in that case as well (error flow)
func (m *DB) ForBlockInView(view map[types.BlockID]struct{}, layer types.LayerID, blockHandler func(block *types.Block) (bool, error)) error {
blocksToVisit := list.New()
for id := range view {
blocksToVisit.PushBack(id)
}
seenBlocks := make(map[types.BlockID]struct{})
for blocksToVisit.Len() > 0 {
block, err := m.GetBlock(blocksToVisit.Remove(blocksToVisit.Front()).(types.BlockID))
if err != nil {
return err
}
// catch blocks that were referenced after more than one layer, and slipped through the stop condition
if block.LayerIndex < layer {
continue
}
// execute handler
stop, err := blockHandler(block)
if err != nil {
return err
}
if stop {
m.Log.With().Debug("ForBlockInView stopped", block.ID())
break
}
// stop condition: referenced blocks must be in lower layers, so we don't traverse them
if block.LayerIndex == layer {
continue
}
// push children to bfs queue
for _, id := range block.ViewEdges {
if _, found := seenBlocks[id]; !found {
seenBlocks[id] = struct{}{}
blocksToVisit.PushBack(id)
}
}
}
return nil
}
// LayerBlockIds retrieves all block ids from a layer by layer index
func (m *DB) LayerBlockIds(index types.LayerID) ([]types.BlockID, error) {
idsBytes, err := m.layers.Get(index.Bytes())
if err != nil {
return nil, err
}
if len(idsBytes) == 0 {
//zero block layer
return []types.BlockID{}, nil
}
blockIds, err := types.BytesToBlockIds(idsBytes)
if err != nil {
return nil, errors.New("could not get all blocks from database")
}
return blockIds, nil
}
// AddZeroBlockLayer tags lyr as a layer without blocks
func (m *DB) AddZeroBlockLayer(index types.LayerID) error {
blockIds := make([]types.BlockID, 0, 1)
w, err := types.BlockIdsToBytes(blockIds)
if err != nil {
return errors.New("could not encode layer blk ids")
}
return m.layers.Put(index.Bytes(), w)
}
func (m *DB) getBlockBytes(id types.BlockID) ([]byte, error) {
return m.blocks.Get(id.Bytes())
}
// ContextualValidity retrieves opinion on block from the database
func (m *DB) ContextualValidity(id types.BlockID) (bool, error) {
b, err := m.contextualValidity.Get(id.Bytes())
if err != nil {
return false, err
}
return b[0] == 1, nil // bytes to bool
}
// SaveContextualValidity persists opinion on block to the database
func (m *DB) SaveContextualValidity(id types.BlockID, valid bool) error {
var v []byte
if valid {
v = constTrue
} else {
v = constFalse
}
m.Debug("save contextual validity %v %v", id, valid)
return m.contextualValidity.Put(id.Bytes(), v)
}
func (m *DB) writeBlock(bl *types.Block) error {
bytes, err := types.InterfaceToBytes(bl)
if err != nil {
return fmt.Errorf("could not encode bl")
}
if err := m.blocks.Put(bl.ID().Bytes(), bytes); err != nil {
return fmt.Errorf("could not add bl %v to database %v", bl.ID(), err)
}
m.updateLayerWithBlock(bl)
m.blockCache.put(bl)
return nil
}
func (m *DB) updateLayerWithBlock(blk *types.Block) error {
lm := m.getLayerMutex(blk.LayerIndex)
defer m.endLayerWorker(blk.LayerIndex)
lm.m.Lock()
defer lm.m.Unlock()
ids, err := m.layers.Get(blk.LayerIndex.Bytes())
var blockIds []types.BlockID
if err != nil {
// layer doesnt exist, need to insert new layer
blockIds = make([]types.BlockID, 0, 1)
} else {
blockIds, err = types.BytesToBlockIds(ids)
if err != nil {
return errors.New("could not get all blocks from database ")
}
}
m.Debug("added block %v to layer %v", blk.ID(), blk.LayerIndex)
blockIds = append(blockIds, blk.ID())
w, err := types.BlockIdsToBytes(blockIds)
if err != nil {
return errors.New("could not encode layer blk ids")
}
m.layers.Put(blk.LayerIndex.Bytes(), w)
return nil
}
// try delete layer Handler (deletes if pending pendingCount is 0)
func (m *DB) endLayerWorker(index types.LayerID) {
m.lhMutex.Lock()
defer m.lhMutex.Unlock()
ll, found := m.layerMutex[index]
if !found {
panic("trying to double close layer mutex")
}
ll.layerWorkers--
if ll.layerWorkers == 0 {
delete(m.layerMutex, index)
}
}
// returns the existing layer Handler (crates one if doesn't exist)
func (m *DB) getLayerMutex(index types.LayerID) *layerMutex {
m.lhMutex.Lock()
defer m.lhMutex.Unlock()
ll, found := m.layerMutex[index]
if !found {
ll = &layerMutex{}
m.layerMutex[index] = ll
}
ll.layerWorkers++
return ll
}
// Schema: "r_<coinbase>_<smesherId>_<layerId> -> reward struct"
func getRewardKey(l types.LayerID, account types.Address, smesherID types.NodeID) []byte {
str := string(getRewardKeyPrefix(account)) + "_" + smesherID.String() + "_" + strconv.FormatUint(l.Uint64(), 10)
return []byte(str)
}
func getRewardKeyPrefix(account types.Address) []byte {
str := "r_" + account.String()
return []byte(str)
}
// This function gets the reward key for a particular smesherID
// format for the index "s_<smesherid>_<accountid>_<layerid> -> r_<accountid>_<smesherid>_<layerid> -> the actual reward"
func getSmesherRewardKey(l types.LayerID, smesherID types.NodeID, account types.Address) []byte {
str := string(getSmesherRewardKeyPrefix(smesherID)) + "_" + account.String() + "_" + strconv.FormatUint(l.Uint64(), 10)
return []byte(str)
}
//use r_ for one and s_ for the other so the namespaces can't collide
func getSmesherRewardKeyPrefix(smesherID types.NodeID) []byte {
str := "s_" + smesherID.String()
return []byte(str)
}
func getTransactionOriginKey(l types.LayerID, t *types.Transaction) []byte {
str := string(getTransactionOriginKeyPrefix(l, t.Origin())) + "_" + t.ID().String()
return []byte(str)
}
func getTransactionDestKey(l types.LayerID, t *types.Transaction) []byte {
str := string(getTransactionDestKeyPrefix(l, t.Recipient)) + "_" + t.ID().String()
return []byte(str)
}
func getTransactionOriginKeyPrefix(l types.LayerID, account types.Address) []byte {
str := "a_o_" + account.String() + "_" + strconv.FormatUint(l.Uint64(), 10)
return []byte(str)
}
func getTransactionDestKeyPrefix(l types.LayerID, account types.Address) []byte {
str := "a_d_" + account.String() + "_" + strconv.FormatUint(l.Uint64(), 10)
return []byte(str)
}
type dbTransaction struct {
*types.Transaction
Origin types.Address
}
func newDbTransaction(tx *types.Transaction) *dbTransaction {
return &dbTransaction{Transaction: tx, Origin: tx.Origin()}
}
func (t dbTransaction) getTransaction() *types.Transaction {
t.Transaction.SetOrigin(t.Origin)
return t.Transaction
}
func (m *DB) writeTransactions(l types.LayerID, txs []*types.Transaction) error {
batch := m.transactions.NewBatch()
for _, t := range txs {
bytes, err := types.InterfaceToBytes(newDbTransaction(t))
if err != nil {
return fmt.Errorf("could not marshall tx %v to bytes: %v", t.ID().ShortString(), err)
}
if err := batch.Put(t.ID().Bytes(), bytes); err != nil {
return fmt.Errorf("could not write tx %v to database: %v", t.ID().ShortString(), err)
}
// write extra index for querying txs by account
if err := batch.Put(getTransactionOriginKey(l, t), t.ID().Bytes()); err != nil {
return fmt.Errorf("could not write tx %v to database: %v", t.ID().ShortString(), err)
}
if err := batch.Put(getTransactionDestKey(l, t), t.ID().Bytes()); err != nil {
return fmt.Errorf("could not write tx %v to database: %v", t.ID().ShortString(), err)
}
m.Debug("wrote tx %v to db", t.ID().ShortString())
}
err := batch.Write()
if err != nil {
return fmt.Errorf("failed to write transactions: %v", err)
}
return nil
}
// WriteTransaction writes a single transaction to the db
func (m *DB) WriteTransaction(l types.LayerID, t *types.Transaction) error {
bytes, err := types.InterfaceToBytes(newDbTransaction(t))
if err != nil {
return fmt.Errorf("could not marshall tx %v to bytes: %v", t.ID().ShortString(), err)
}
if err := m.transactions.Put(t.ID().Bytes(), bytes); err != nil {
return fmt.Errorf("could not write tx %v to database: %v", t.ID().ShortString(), err)
}
// write extra index for querying txs by account
if err := m.transactions.Put(getTransactionOriginKey(l, t), t.ID().Bytes()); err != nil {
return fmt.Errorf("could not write tx %v to database: %v", t.ID().ShortString(), err)
}
if err := m.transactions.Put(getTransactionDestKey(l, t), t.ID().Bytes()); err != nil {
return fmt.Errorf("could not write tx %v to database: %v", t.ID().ShortString(), err)
}
m.Debug("wrote tx %v to db", t.ID().ShortString())
return nil
}
//We're not using the existing reward type because the layer is implicit in the key
type dbReward struct {
TotalReward uint64
LayerRewardEstimate uint64
SmesherID types.NodeID
Coinbase types.Address
// TotalReward - LayerRewardEstimate = FeesEstimate
}
func (m *DB) writeTransactionRewards(l types.LayerID, accountBlockCount map[types.Address]map[string]uint64, totalReward, layerReward *big.Int) error {
batch := m.transactions.NewBatch()
for account, smesherAccountEntry := range accountBlockCount {
for smesherString, cnt := range smesherAccountEntry {
smesherEntry, err := types.StringToNodeID(smesherString)
if err != nil {
return fmt.Errorf("could not convert String to NodeID for %v: %v", smesherString, err)
}
reward := dbReward{TotalReward: cnt * totalReward.Uint64(), LayerRewardEstimate: cnt * layerReward.Uint64(), SmesherID: *smesherEntry, Coinbase: account}
if b, err := types.InterfaceToBytes(&reward); err != nil {
return fmt.Errorf("could not marshal reward for %v: %v", account.Short(), err)
} else if err := batch.Put(getRewardKey(l, account, *smesherEntry), b); err != nil {
return fmt.Errorf("could not write reward to %v to database: %v", account.Short(), err)
} else if err := batch.Put(getSmesherRewardKey(l, *smesherEntry, account), getRewardKey(l, account, *smesherEntry)); err != nil {
return fmt.Errorf("could not write reward key for smesherID %v to database: %v", smesherEntry.ShortString(), err)
}
}
}
return batch.Write()
}
// GetRewards retrieves account's rewards by address
func (m *DB) GetRewards(account types.Address) (rewards []types.Reward, err error) {
it := m.transactions.Find(getRewardKeyPrefix(account))
for it.Next() {
if it.Key() == nil {
break
}
str := string(it.Key())
strs := strings.Split(str, "_")
layer, err := strconv.ParseUint(strs[3], 10, 64)
if err != nil {
return nil, fmt.Errorf("wrong key in db %s: %v", it.Key(), err)
}
var reward dbReward
err = types.BytesToInterface(it.Value(), &reward)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal reward: %v", err)
}
rewards = append(rewards, types.Reward{
Layer: types.LayerID(layer),
TotalReward: reward.TotalReward,
LayerRewardEstimate: reward.LayerRewardEstimate,
SmesherID: reward.SmesherID,
Coinbase: reward.Coinbase,
})
}
return
}
// GetRewardsBySmesherID retrieves rewards by smesherID
func (m *DB) GetRewardsBySmesherID(smesherID types.NodeID) (rewards []types.Reward, err error) {
it := m.transactions.Find(getSmesherRewardKeyPrefix(smesherID))
for it.Next() {
if it.Key() == nil {
break
}
str := string(it.Key())
strs := strings.Split(str, "_")
layer, err := strconv.ParseUint(strs[3], 10, 64)
if err != nil {
return nil, fmt.Errorf("error parsing db key %s: %v", it.Key(), err)
}
//find the key to the actual reward struct, which is in it.Value()
var reward dbReward
rewardBytes, err := m.transactions.Get(it.Value())
if err != nil {
return nil, fmt.Errorf("wrong key in db %s: %v", it.Value(), err)
}
if err = types.BytesToInterface(rewardBytes, &reward); err != nil {
return nil, fmt.Errorf("failed to unmarshal reward: %v", err)
}
rewards = append(rewards, types.Reward{
Layer: types.LayerID(layer),
TotalReward: reward.TotalReward,
LayerRewardEstimate: reward.LayerRewardEstimate,
SmesherID: reward.SmesherID,
Coinbase: reward.Coinbase,
})
}
return
}
func (m *DB) addToUnappliedTxs(txs []*types.Transaction, layer types.LayerID) error {
groupedTxs := groupByOrigin(txs)
for addr, accountTxs := range groupedTxs {
if err := m.addToAccountTxs(addr, accountTxs, layer); err != nil {
return err
}
}
return nil
}
func (m *DB) addToAccountTxs(addr types.Address, accountTxs []*types.Transaction, layer types.LayerID) error {
m.unappliedTxsMutex.Lock()
defer m.unappliedTxsMutex.Unlock()
// TODO: instead of storing a list, use LevelDB's prefixed keys and then iterate all relevant keys
pending, err := m.getAccountPendingTxs(addr)
if err != nil {
return err
}
pending.Add(layer, accountTxs...)
if err := m.storeAccountPendingTxs(addr, pending); err != nil {
return err
}
return nil
}
func (m *DB) removeFromUnappliedTxs(accepted []*types.Transaction) (grouped map[types.Address][]*types.Transaction, accounts map[types.Address]struct{}) {
grouped = groupByOrigin(accepted)
accounts = make(map[types.Address]struct{})
for account := range grouped {
accounts[account] = struct{}{}
}
for account := range accounts {
m.removeFromAccountTxs(account, grouped)
}
return
}
func (m *DB) removeFromAccountTxs(account types.Address, gAccepted map[types.Address][]*types.Transaction) {
m.unappliedTxsMutex.Lock()
defer m.unappliedTxsMutex.Unlock()
// TODO: instead of storing a list, use LevelDB's prefixed keys and then iterate all relevant keys
pending, err := m.getAccountPendingTxs(account)
if err != nil {
m.With().Error("failed to get account pending txs",
log.String("address", account.Short()), log.Err(err))
return
}
pending.RemoveAccepted(gAccepted[account])
if err := m.storeAccountPendingTxs(account, pending); err != nil {
m.With().Error("failed to store account pending txs",
log.String("address", account.Short()), log.Err(err))
}
}
func (m *DB) removeRejectedFromAccountTxs(account types.Address, rejected map[types.Address][]*types.Transaction, layer types.LayerID) {
m.unappliedTxsMutex.Lock()
defer m.unappliedTxsMutex.Unlock()
// TODO: instead of storing a list, use LevelDB's prefixed keys and then iterate all relevant keys
pending, err := m.getAccountPendingTxs(account)
if err != nil {
m.With().Error("failed to get account pending txs",
layer, log.String("address", account.Short()), log.Err(err))
return
}
pending.RemoveRejected(rejected[account], layer)
if err := m.storeAccountPendingTxs(account, pending); err != nil {
m.With().Error("failed to store account pending txs",
layer, log.String("address", account.Short()), log.Err(err))
}
}
func (m *DB) storeAccountPendingTxs(account types.Address, pending *pendingtxs.AccountPendingTxs) error {
if pending.IsEmpty() {
if err := m.unappliedTxs.Delete(account.Bytes()); err != nil {
return fmt.Errorf("failed to delete empty pending txs for account %v: %v", account.Short(), err)
}
return nil
}
if accountTxsBytes, err := types.InterfaceToBytes(&pending); err != nil {
return fmt.Errorf("failed to marshal account pending txs: %v", err)
} else if err := m.unappliedTxs.Put(account.Bytes(), accountTxsBytes); err != nil {
return fmt.Errorf("failed to store mesh txs for address %s", account.Short())
}
return nil
}
func (m *DB) getAccountPendingTxs(addr types.Address) (*pendingtxs.AccountPendingTxs, error) {
accountTxsBytes, err := m.unappliedTxs.Get(addr.Bytes())
if err != nil && err != database.ErrNotFound {
return nil, fmt.Errorf("failed to get mesh txs for account %s", addr.Short())
}
if err == database.ErrNotFound {
return pendingtxs.NewAccountPendingTxs(), nil
}
var pending pendingtxs.AccountPendingTxs
if err := types.BytesToInterface(accountTxsBytes, &pending); err != nil {
return nil, fmt.Errorf("failed to unmarshal account pending txs: %v", err)
}
return &pending, nil
}
func groupByOrigin(txs []*types.Transaction) map[types.Address][]*types.Transaction {
grouped := make(map[types.Address][]*types.Transaction)
for _, tx := range txs {
grouped[tx.Origin()] = append(grouped[tx.Origin()], tx)
}
return grouped
}
// GetProjection returns projection of address
func (m *DB) GetProjection(addr types.Address, prevNonce, prevBalance uint64) (nonce, balance uint64, err error) {
pending, err := m.getAccountPendingTxs(addr)
if err != nil {
return 0, 0, err
}
nonce, balance = pending.GetProjection(prevNonce, prevBalance)
return nonce, balance, nil
}
type txGetter struct {
missingIds map[types.TransactionID]struct{}
txs []*types.Transaction
mesh *DB
}
func (g *txGetter) get(id types.TransactionID) {
t, err := g.mesh.GetTransaction(id)
if err != nil {
g.mesh.With().Warning("could not fetch tx", id, log.Err(err))
g.missingIds[id] = struct{}{}
} else {
g.txs = append(g.txs, t)
}
}
func newGetter(m *DB) *txGetter {
return &txGetter{mesh: m, missingIds: make(map[types.TransactionID]struct{})}
}
// GetTransactions retrieves a list of txs by their id's
func (m *DB) GetTransactions(transactions []types.TransactionID) ([]*types.Transaction, map[types.TransactionID]struct{}) {
getter := newGetter(m)
for _, id := range transactions {
getter.get(id)
}
return getter.txs, getter.missingIds
}
// GetTransaction retrieves a tx by its id
func (m *DB) GetTransaction(id types.TransactionID) (*types.Transaction, error) {
tBytes, err := m.transactions.Get(id[:])
if err != nil {
return nil, fmt.Errorf("could not find transaction in database %v err=%v", hex.EncodeToString(id[:]), err)
}
var dbTx dbTransaction
err = types.BytesToInterface(tBytes, &dbTx)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal transaction: %v", err)
}
return dbTx.getTransaction(), nil
}
// GetTransactionsByDestination retrieves txs by destination and layer
func (m *DB) GetTransactionsByDestination(l types.LayerID, account types.Address) (txs []types.TransactionID) {
it := m.transactions.Find(getTransactionDestKeyPrefix(l, account))
for it.Next() {
if it.Key() == nil {
break
}
var a types.TransactionID
err := types.BytesToInterface(it.Value(), &a)
if err != nil {
// log error
break
}
txs = append(txs, a)
}
return
}
// GetTransactionsByOrigin retrieves txs by origin and layer
func (m *DB) GetTransactionsByOrigin(l types.LayerID, account types.Address) (txs []types.TransactionID) {
it := m.transactions.Find(getTransactionOriginKeyPrefix(l, account))
for it.Next() {
if it.Key() == nil {
break
}
var a types.TransactionID
err := types.BytesToInterface(it.Value(), &a)
if err != nil {
// log error
break
}
txs = append(txs, a)
}
return
}
// BlocksByValidity classifies a slice of blocks by validity
func (m *DB) BlocksByValidity(blocks []*types.Block) (validBlocks, invalidBlocks []*types.Block) {
for _, b := range blocks {
valid, err := m.ContextualValidity(b.ID())
if err != nil {
m.With().Error("could not get contextual validity", b.ID(), log.Err(err))
}
if valid {
validBlocks = append(validBlocks, b)
} else {
invalidBlocks = append(invalidBlocks, b)
}
}
return validBlocks, invalidBlocks
}
// ContextuallyValidBlock - returns the contextually valid blocks for the provided layer
func (m *DB) ContextuallyValidBlock(layer types.LayerID) (map[types.BlockID]struct{}, error) {
if layer == 0 || layer == 1 {
v, err := m.LayerBlockIds(layer)
if err != nil {
m.With().Error("could not get layer block ids", layer, log.Err(err))
return nil, err
}
mp := make(map[types.BlockID]struct{}, len(v))
for _, blk := range v {
mp[blk] = struct{}{}
}
return mp, nil
}
blockIds, err := m.LayerBlockIds(layer)
if err != nil {
return nil, err
}
validBlks := make(map[types.BlockID]struct{})
for _, b := range blockIds {
valid, err := m.ContextualValidity(b)
if err != nil {
m.With().Error("could not get contextual validity", b, layer, log.Err(err))
}
if !valid {
continue
}
validBlks[b] = struct{}{}
}
m.With().Info("count of contextually valid blocks in layer",
layer,
log.Int("count_valid", len(validBlks)),
log.Int("count_total", len(blockIds)))
return validBlks, nil
}
// Persist persists an item v into the database using key as its id
func (m *DB) Persist(key []byte, v interface{}) error {
buf, err := types.InterfaceToBytes(v)
if err != nil {
panic(err)
}
return m.general.Put(key, buf)
}
// Retrieve retrieves item by key into v
func (m *DB) Retrieve(key []byte, v interface{}) (interface{}, error) {
val, err := m.general.Get(key)
if err != nil {
m.Warning("failed retrieving object from db ", err)
return nil, err
}
if val == nil {
return nil, fmt.Errorf("no such value in database db ")
}
if err := types.BytesToInterface(val, v); err != nil {
return nil, fmt.Errorf("failed decoding object from db %v", err)
}
return v, nil
}
func (m *DB) cacheWarmUpFromTo(from types.LayerID, to types.LayerID) error {
m.Info("warming up cache with layers %v to %v", from, to)
for i := from; i < to; i++ {
select {
case <-m.exit:
m.Info("shutdown during cache warm up")
return nil
default:
}
layer, err := m.LayerBlockIds(i)
if err != nil {
return fmt.Errorf("could not get layer %v from database %v", layer, err)
}
for _, b := range layer {
block, blockErr := m.GetBlock(b)
if blockErr != nil {
return fmt.Errorf("could not get bl %v from database %v", b, blockErr)
}
m.blockCache.put(block)
}
}
m.Info("done warming up cache")
return nil
}