-
Notifications
You must be signed in to change notification settings - Fork 210
/
meshdb.go
648 lines (564 loc) · 19.2 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
package mesh
import (
"bytes"
"context"
"errors"
"fmt"
"sync"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/database"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/pendingtxs"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/blocks"
"github.com/spacemeshos/go-spacemesh/sql/identities"
"github.com/spacemeshos/go-spacemesh/sql/layers"
"github.com/spacemeshos/go-spacemesh/sql/rewards"
"github.com/spacemeshos/go-spacemesh/sql/transactions"
)
const (
layerSize = 200
)
// DB represents a mesh database instance.
type DB struct {
log.Log
blockCache blockCache
db *sql.Database
coinflipMu sync.RWMutex
coinflips map[types.LayerID]bool // weak coinflip results from Hare
}
// NewPersistentMeshDB creates an instance of a mesh database.
func NewPersistentMeshDB(db *sql.Database, blockCacheSize int, logger log.Log) (*DB, error) {
mdb := &DB{
Log: logger,
blockCache: newBlockCache(blockCacheSize * layerSize),
db: db,
coinflips: make(map[types.LayerID]bool),
}
gLayer := types.GenesisLayer()
for _, b := range gLayer.Ballots() {
mdb.Log.With().Info("adding genesis ballot", b.ID(), b.LayerIndex)
if err := mdb.AddBallot(b); err != nil {
mdb.Log.With().Error("error inserting genesis ballot to db", b.ID(), b.LayerIndex, log.Err(err))
}
}
for _, b := range gLayer.Blocks() {
mdb.Log.With().Info("adding genesis block", b.ID(), b.LayerIndex)
if err := mdb.AddBlock(b); err != nil {
mdb.Log.With().Error("error inserting genesis block to db", b.ID(), b.LayerIndex, log.Err(err))
}
if err := mdb.SaveContextualValidity(b.ID(), b.LayerIndex, true); err != nil {
mdb.Log.With().Error("error inserting genesis block to db", b.ID(), b.LayerIndex, log.Err(err))
}
}
if err := mdb.SaveHareConsensusOutput(context.Background(), gLayer.Index(), types.GenesisBlockID); err != nil {
log.With().Error("error inserting genesis block as hare output to db", gLayer.Index(), log.Err(err))
}
return mdb, nil
}
// PersistentData checks to see if db is empty.
func (m *DB) PersistentData() bool {
lid, err := layers.GetByStatus(m.db, layers.Latest)
if err != nil && errors.Is(err, sql.ErrNotFound) || (lid == types.LayerID{}) {
m.Info("database is empty")
return false
}
return true
}
// NewMemMeshDB is a mock used for testing.
func NewMemMeshDB(logger log.Log) *DB {
mdb := &DB{
Log: logger,
blockCache: newBlockCache(100 * layerSize),
db: sql.InMemory(),
coinflips: make(map[types.LayerID]bool),
}
gLayer := types.GenesisLayer()
for _, b := range gLayer.Ballots() {
mdb.Log.With().Info("adding genesis ballot", b.ID(), b.LayerIndex)
_ = mdb.AddBallot(b)
}
for _, b := range gLayer.Blocks() {
// these are only used for testing so we can safely ignore errors here
mdb.Log.With().Info("adding genesis block", b.ID(), b.LayerIndex)
_ = mdb.AddBlock(b)
_ = mdb.SaveContextualValidity(b.ID(), b.LayerIndex, true)
}
if err := mdb.SaveHareConsensusOutput(context.Background(), gLayer.Index(), types.GenesisBlockID); err != nil {
logger.With().Error("error inserting genesis block as hare output to db", gLayer, log.Err(err))
}
return mdb
}
// Close closes all resources.
func (m *DB) Close() {
if err := m.db.Close(); err != nil {
m.Log.With().Error("error closing database", log.Err(err))
}
}
// todo: for now, these methods are used to export dbs to sync, think about merging the two packages
// Ballots exports the ballot database.
func (m *DB) Ballots() database.Getter {
return newBallotFetcherDB(m)
}
// Blocks exports the block database.
func (m *DB) Blocks() database.Getter {
return newBlockFetcherDB(m)
}
// AddBallot adds a ballot to the database.
func (m *DB) AddBallot(b *types.Ballot) error {
mal, err := identities.IsMalicious(m.db, b.SmesherID().Bytes())
if err != nil {
return err
}
if mal {
b.SetMalicious()
}
// it is important to run add ballot and set identity to malicious atomically
tx, err := m.db.Tx(context.Background())
if err != nil {
return err
}
defer tx.Release()
if err := ballots.Add(tx, b); err != nil && !errors.Is(err, sql.ErrObjectExists) {
return err
}
if !mal {
count, err := ballots.CountByPubkeyLayer(tx, b.LayerIndex, b.SmesherID().Bytes())
if err != nil {
return err
}
if count > 1 {
if err := identities.SetMalicious(tx, b.SmesherID().Bytes()); err != nil {
return err
}
b.SetMalicious()
m.Log.With().Warning("smesher produced more than one ballot in the same layer",
log.Stringer("smesher", b.SmesherID()),
log.Inline(b),
)
}
}
return tx.Commit()
}
// SetMalicious updates smesher as malicious.
func (m *DB) SetMalicious(smesher *signing.PublicKey) error {
return identities.SetMalicious(m.db, smesher.Bytes())
}
// HasBallot returns true if the ballot is stored in a database.
func (m *DB) HasBallot(ballot types.BallotID) bool {
exists, _ := ballots.Has(m.db, ballot)
return exists
}
// GetBallot returns true if the database has Ballot specified by the BallotID and false otherwise.
func (m *DB) GetBallot(id types.BallotID) (*types.Ballot, error) {
return ballots.Get(m.db, id)
}
// LayerBallots retrieves all ballots from a layer by layer ID.
func (m *DB) LayerBallots(lid types.LayerID) ([]*types.Ballot, error) {
return ballots.Layer(m.db, lid)
}
// LayerBallotIDs returns list of the ballot ids in the layer.
func (m *DB) LayerBallotIDs(lid types.LayerID) ([]types.BallotID, error) {
return ballots.IDsInLayer(m.db, lid)
}
// AddBlock adds a block to the database.
func (m *DB) AddBlock(b *types.Block) error {
err := m.writeBlock(b)
if err != nil {
if errors.Is(err, sql.ErrObjectExists) {
return nil
}
return err
}
events.ReportNewBlock(b)
return nil
}
// HasBlock returns true if block exists in the database.
func (m *DB) HasBlock(bid types.BlockID) bool {
exists, _ := blocks.Has(m.db, bid)
return exists
}
// GetBlock returns the block specified by the block ID.
func (m *DB) GetBlock(bid types.BlockID) (*types.Block, error) {
if b := m.blockCache.Get(bid); b != nil {
return b, nil
}
b, err := m.getBlock(bid)
if err != nil {
return nil, err
}
return b, nil
}
// LayerBlockIds retrieves all block IDs from the layer specified by layer ID.
func (m *DB) LayerBlockIds(lid types.LayerID) ([]types.BlockID, error) {
return blocks.IDsInLayer(m.db, lid)
}
// LayerBlocks retrieves all blocks from the layer specified by layer ID.
func (m *DB) LayerBlocks(lid types.LayerID) ([]*types.Block, error) {
ids, err := m.LayerBlockIds(lid)
if err != nil {
return nil, err
}
rst := make([]*types.Block, 0, len(ids))
for _, bid := range ids {
block, err := m.GetBlock(bid)
if err != nil {
return nil, err
}
rst = append(rst, block)
}
return rst, nil
}
func (m *DB) getBlock(id types.BlockID) (*types.Block, error) {
return blocks.Get(m.db, id)
}
// AddZeroBlockLayer tags lyr as a layer without blocks.
func (m *DB) AddZeroBlockLayer(lid types.LayerID) error {
return layers.SetHareOutput(m.db, lid, types.BlockID{})
}
// ContextualValidity retrieves opinion on block from the database.
func (m *DB) ContextualValidity(id types.BlockID) (bool, error) {
return blocks.IsValid(m.db, id)
}
// SaveContextualValidity persists opinion on block to the database.
func (m *DB) SaveContextualValidity(id types.BlockID, lid types.LayerID, valid bool) error {
m.With().Debug("save block contextual validity", id, lid, log.Bool("validity", valid))
if valid {
return blocks.SetValid(m.db, id)
}
return blocks.SetInvalid(m.db, id)
}
// RecordCoinflip saves the weak coinflip result to memory for the given layer.
func (m *DB) RecordCoinflip(ctx context.Context, layerID types.LayerID, coinflip bool) {
m.WithContext(ctx).With().Info("recording coinflip result for layer in mesh",
layerID,
log.Bool("coinflip", coinflip))
m.coinflipMu.Lock()
defer m.coinflipMu.Unlock()
m.coinflips[layerID] = coinflip
}
// GetCoinflip returns the weak coinflip result for the given layer.
func (m *DB) GetCoinflip(_ context.Context, layerID types.LayerID) (bool, bool) {
m.coinflipMu.Lock()
defer m.coinflipMu.Unlock()
coin, exists := m.coinflips[layerID]
return coin, exists
}
// GetHareConsensusOutput gets the input vote vector for a layer (hare results).
func (m *DB) GetHareConsensusOutput(layerID types.LayerID) (types.BlockID, error) {
return layers.GetHareOutput(m.db, layerID)
}
// SaveHareConsensusOutput gets the input vote vector for a layer (hare results).
func (m *DB) SaveHareConsensusOutput(ctx context.Context, id types.LayerID, blockID types.BlockID) error {
m.WithContext(ctx).With().Debug("saving hare output", id, blockID)
return layers.SetHareOutput(m.db, id, blockID)
}
func (m *DB) persistProcessedLayer(layerID types.LayerID) error {
return layers.SetStatus(m.db, layerID, layers.Processed)
}
// GetProcessedLayer loads processed layer from database.
func (m *DB) GetProcessedLayer() (types.LayerID, error) {
return layers.GetByStatus(m.db, layers.Processed)
}
// GetVerifiedLayer loads verified layer from database.
func (m *DB) GetVerifiedLayer() (types.LayerID, error) {
return layers.GetByStatus(m.db, layers.Applied)
}
// GetLatestLayer loads latest layer from database.
func (m *DB) GetLatestLayer() (types.LayerID, error) {
return layers.GetByStatus(m.db, layers.Latest)
}
func (m *DB) writeBlock(b *types.Block) error {
if err := blocks.Add(m.db, b); err != nil {
return fmt.Errorf("could not add block %v to database: %w", b.ID(), err)
}
m.blockCache.put(b)
return nil
}
func (m *DB) recoverLayerHash(layerID types.LayerID) (types.Hash32, error) {
return layers.GetHash(m.db, layerID)
}
func (m *DB) persistLayerHash(layerID types.LayerID, hash types.Hash32) error {
return layers.SetHash(m.db, layerID, hash)
}
func (m *DB) persistAggregatedLayerHash(layerID types.LayerID, hash types.Hash32) error {
return layers.SetAggregatedHash(m.db, layerID, hash)
}
func (m *DB) updateDBTXWithBlockID(b *types.Block, txs ...*types.Transaction) error {
return m.writeTransactions(b.LayerIndex, b.ID(), txs...)
}
// writeTransactions writes all transactions associated with a block atomically.
func (m *DB) writeTransactions(layerID types.LayerID, bid types.BlockID, txs ...*types.Transaction) error {
dbtx, err := m.db.Tx(context.Background())
if err != nil {
return err
}
defer dbtx.Release()
for _, tx := range txs {
if err := transactions.Add(dbtx, layerID, bid, tx); err != nil {
return err
}
m.Debug("wrote tx %v to db", tx.ID().ShortString())
}
return dbtx.Commit()
}
func (m *DB) writeTransactionRewards(l types.LayerID, applied []types.AnyReward) error {
tx, err := m.db.Tx(context.Background())
if err != nil {
return err
}
defer tx.Release()
for i := range applied {
if err := rewards.Add(tx, l, &applied[i]); err != nil {
return err
}
}
return tx.Commit()
}
// GetRewards retrieves account's rewards by the coinbase address.
func (m *DB) GetRewards(coinbase types.Address) ([]types.Reward, error) {
return rewards.FilterByCoinbase(m.db, coinbase)
}
// GetRewardsBySmesherID retrieves rewards by smesherID.
func (m *DB) GetRewardsBySmesherID(smesherID types.NodeID) ([]types.Reward, error) {
return rewards.FilterBySmesher(m.db, smesherID.ToBytes())
}
func (m *DB) markTransactionsDeleted(txs ...*types.Transaction) error {
for _, tx := range txs {
if err := transactions.MarkDeleted(m.db, tx.ID()); err != nil {
return err
}
}
return nil
}
func (m *DB) getAccountPendingTxs(addr types.Address) (*pendingtxs.AccountPendingTxs, error) {
pending := pendingtxs.NewAccountPendingTxs()
txs, err := transactions.FilterPending(m.db, addr)
if err != nil {
return nil, err
}
for _, tx := range txs {
pending.Add(tx.LayerID, &tx.Transaction)
}
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) (uint64, uint64, error) {
pending, err := m.getAccountPendingTxs(addr)
if err != nil {
return 0, 0, err
}
nonce, balance := pending.GetProjection(prevNonce, prevBalance)
return nonce, balance, nil
}
// GetTransactions retrieves a list of txs by their id's.
func (m *DB) GetTransactions(transactions []types.TransactionID) ([]*types.Transaction, map[types.TransactionID]struct{}) {
missing := make(map[types.TransactionID]struct{})
txs := make([]*types.Transaction, 0, len(transactions))
for _, id := range transactions {
if tx, err := m.GetMeshTransaction(id); err != nil {
m.With().Warning("could not fetch tx", id, log.Err(err))
missing[id] = struct{}{}
} else {
txs = append(txs, &tx.Transaction)
}
}
return txs, missing
}
// GetMeshTransactions retrieves list of txs with information in what blocks and layers they are included.
func (m *DB) GetMeshTransactions(transactions []types.TransactionID) ([]*types.MeshTransaction, map[types.TransactionID]struct{}) {
var (
missing = map[types.TransactionID]struct{}{}
txs = make([]*types.MeshTransaction, 0, len(transactions))
)
for _, id := range transactions {
tx, err := m.GetMeshTransaction(id)
if err != nil {
m.With().Warning("could not fetch tx", id, log.Err(err))
missing[id] = struct{}{}
} else {
txs = append(txs, tx)
}
}
return txs, missing
}
// GetMeshTransaction retrieves a tx by its id.
func (m *DB) GetMeshTransaction(id types.TransactionID) (*types.MeshTransaction, error) {
return transactions.Get(m.db, id)
}
// GetTransactionsByDestination retrieves txs by destination in between layers [from, to].
func (m *DB) GetTransactionsByDestination(from, to types.LayerID, address types.Address) ([]*types.MeshTransaction, error) {
return transactions.FilterByDestination(m.db, from, to, address)
}
// GetTransactionsByOrigin retrieves txs by origin in beetween layers [from, to].
func (m *DB) GetTransactionsByOrigin(from, to types.LayerID, address types.Address) ([]*types.MeshTransaction, error) {
return transactions.FilterByOrigin(m.db, from, to, address)
}
// GetTransactionsByAddress retrieves txs for a single address in beetween layers [from, to].
// Guarantees that transaction will appear exactly once, even if origin and recipient is the same, and in insertion order.
func (m *DB) GetTransactionsByAddress(from, to types.LayerID, address types.Address) ([]*types.MeshTransaction, error) {
return transactions.FilterByAddress(m.db, from, to, address)
}
// BlocksByValidity classifies a slice of blocks by validity.
func (m *DB) BlocksByValidity(blocks []*types.Block) ([]*types.Block, []*types.Block) {
var validBlocks, invalidBlocks []*types.Block
for _, b := range blocks {
valid, err := m.ContextualValidity(b.ID())
if err != nil {
m.With().Warning("could not get contextual validity for block in list", b.ID(), log.Err(err))
}
if valid {
validBlocks = append(validBlocks, b)
} else {
invalidBlocks = append(invalidBlocks, b)
}
}
return validBlocks, invalidBlocks
}
// LayerContextuallyValidBlocks returns the set of contextually valid block IDs for the provided layer.
func (m *DB) LayerContextuallyValidBlocks(ctx context.Context, layer types.LayerID) (map[types.BlockID]struct{}, error) {
logger := m.WithContext(ctx)
blockIds, err := m.LayerBlockIds(layer)
if err != nil {
return nil, err
}
validBlks := make(map[types.BlockID]struct{})
cvErrors := make(map[string][]types.BlockID)
cvErrorCount := 0
for _, b := range blockIds {
valid, err := m.ContextualValidity(b)
if err != nil {
logger.With().Warning("could not get contextual validity for block in layer", b, layer, log.Err(err))
cvErrors[err.Error()] = append(cvErrors[err.Error()], b)
cvErrorCount++
}
if !valid {
continue
}
validBlks[b] = struct{}{}
}
logger.With().Debug("count of contextually valid blocks in layer",
layer,
log.Int("count_valid", len(validBlks)),
log.Int("count_error", cvErrorCount),
log.Int("count_total", len(blockIds)))
if cvErrorCount != 0 {
logger.With().Error("errors occurred getting contextual validity of blocks in layer",
layer,
log.Int("count", cvErrorCount),
log.String("errors", fmt.Sprint(cvErrors)))
}
return validBlks, 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.Before(to); i = i.Add(1) {
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
}
// newBlockFetcherDB returns reference to a BlockFetcherDB instance.
func newBlockFetcherDB(mdb *DB) *BlockFetcherDB {
return &BlockFetcherDB{mdb: mdb}
}
// BlockFetcherDB implements API that allows fetcher to get a block from a remote database.
type BlockFetcherDB struct {
mdb *DB
}
// Get types.Block encoded in byte using hash.
func (db *BlockFetcherDB) Get(hash []byte) ([]byte, error) {
id := types.BlockID(types.BytesToHash(hash).ToHash20())
blk, err := db.mdb.GetBlock(id)
if err != nil {
return nil, fmt.Errorf("get block: %w", err)
}
data, err := codec.Encode(blk)
if err != nil {
return data, fmt.Errorf("serialize: %w", err)
}
return data, nil
}
// newBallotFetcherDB returns reference to a BallotFetcherDB instance.
func newBallotFetcherDB(mdb *DB) *BallotFetcherDB {
return &BallotFetcherDB{mdb: mdb}
}
// BallotFetcherDB implements API that allows fetcher to get a ballot from a remote database.
type BallotFetcherDB struct {
mdb *DB
}
// Get types.Block encoded in byte using hash.
func (db *BallotFetcherDB) Get(hash []byte) ([]byte, error) {
id := types.BallotID(types.BytesToHash(hash).ToHash20())
blk, err := db.mdb.GetBallot(id)
if err != nil {
return nil, fmt.Errorf("get ballot: %w", err)
}
data, err := codec.Encode(blk)
if err != nil {
return data, fmt.Errorf("serialize: %w", err)
}
return data, nil
}
type txFetcher struct {
m *Mesh
}
// Get transaction blob, by transaction id.
func (db *txFetcher) Get(hash []byte) ([]byte, error) {
id := types.TransactionID{}
copy(id[:], hash)
if tx, err := db.m.txPool.Get(id); err == nil && tx != nil {
return codec.Encode(tx)
}
return transactions.GetBlob(db.m.db, id)
}
// LayerIDs is a utility function that finds namespaced IDs saved in a database as a key.
func LayerIDs(db database.Database, namespace string, lid types.LayerID, f func(id []byte) error) error {
var (
zero = false
n int
buf = new(bytes.Buffer)
)
buf.WriteString(namespace)
buf.Write(lid.Bytes())
it := db.Find(buf.Bytes())
defer it.Release()
for it.Next() {
if len(it.Key()) == buf.Len() {
zero = true
continue
}
if err := f(it.Key()[buf.Len():]); err != nil {
return err
}
n++
}
if it.Error() != nil {
return fmt.Errorf("iterator: %w", it.Error())
}
if zero || n > 0 {
return nil
}
return database.ErrNotFound
}