-
Notifications
You must be signed in to change notification settings - Fork 211
/
meshdb.go
468 lines (403 loc) · 13.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
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/log"
"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"
)
// DB represents a mesh database instance.
type DB struct {
log.Log
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, logger log.Log) (*DB, error) {
mdb := &DB{
Log: logger,
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,
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
}
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) {
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)
}
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) 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())
}
// 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
}
// 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
}
// 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
}