Skip to content

Commit 8e9885c

Browse files
refactor: revise and add tests for exporter/modules/sync_committees_count.go (#1373)
* refactor: updated sync committees count handlers * refactor: move and update sync committees count db functions * test: add ConsensusDBI mocks wth mockery * fix: updated db and regenerated mocks for ConsensusDBI * refactor: add ctx cancellation in exporter loop * test: added sync committees count tests * test: updated sync committees count tests for no records in db * tests: updated Export func tests * refactor: rename ConsensusDBI to ConsensusRepository and move it to /db2/consensus.go * test: update ConsensusRepository mocks with mockery
1 parent ba3822e commit 8e9885c

File tree

5 files changed

+376
-52
lines changed

5 files changed

+376
-52
lines changed

backend/pkg/commons/db2/consensus.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ type ConsensusRepository interface {
2525
GetFirstRelayBlock(tagID string) (types.RelayBlock, error)
2626
GetLastRelayBlock(tagID string) (types.RelayBlock, error)
2727
SaveBlockTagsAndRelays(tagID string, payload types.BidTrace) error
28+
GetSyncCommitteesCountPerValidator() (uint64, error)
29+
GetTotalPeriodSyncCommitteesCountPerValidator() (uint64, error)
30+
GetCountSoFarSyncCommitteesCountPerValidator(period uint64) (float64, error)
31+
SaveSyncCommitteesCount(period uint64, count float64) error
32+
GetEpochValidatorsCount(epoch uint64) (uint64, error)
33+
GetLatestFinalizedEpoch() (uint64, error)
2834
}
2935

3036
type ConsensusDB struct {
@@ -255,3 +261,64 @@ func (c *ConsensusDB) SaveBlockTagsAndRelays(tagID string, payload types.BidTrac
255261

256262
return tx.Commit()
257263
}
264+
265+
func (c *ConsensusDB) GetLatestFinalizedEpoch() (uint64, error) {
266+
var latestFinalized uint64
267+
err := c.WriterDb.Get(&latestFinalized, "SELECT epoch FROM epochs WHERE finalized ORDER BY epoch DESC LIMIT 1")
268+
if err != nil {
269+
if err == sql.ErrNoRows {
270+
return 0, nil
271+
}
272+
log.Error(err, "error retrieving latest exported finalized epoch from the database", 0)
273+
return 0, err
274+
}
275+
276+
return latestFinalized, nil
277+
}
278+
279+
func (c *ConsensusDB) GetSyncCommitteesCountPerValidator() (uint64, error) {
280+
var rowCount uint64
281+
err := c.WriterDb.Get(&rowCount, `SELECT COUNT(*) FROM sync_committees_count_per_validator`)
282+
return rowCount, err
283+
}
284+
285+
func (c *ConsensusDB) GetTotalPeriodSyncCommitteesCountPerValidator() (uint64, error) {
286+
var dbPeriod uint64
287+
err := c.WriterDb.Get(&dbPeriod, `SELECT MAX(period) FROM sync_committees_count_per_validator`)
288+
return dbPeriod, err
289+
}
290+
291+
func (c *ConsensusDB) GetCountSoFarSyncCommitteesCountPerValidator(period uint64) (float64, error) {
292+
var countSoFar float64
293+
err := c.WriterDb.Get(&countSoFar, `SELECT count_so_far FROM sync_committees_count_per_validator WHERE period = $1`, period)
294+
return countSoFar, err
295+
}
296+
297+
func (c *ConsensusDB) SaveSyncCommitteesCount(period uint64, count float64) error {
298+
tx, err := c.WriterDb.Beginx()
299+
if err != nil {
300+
return err
301+
}
302+
defer utils.Rollback(tx)
303+
304+
_, err = tx.Exec(
305+
fmt.Sprintf(`
306+
INSERT INTO sync_committees_count_per_validator (period, count_so_far)
307+
VALUES (%d, %f)
308+
ON CONFLICT (period) DO UPDATE SET
309+
period = excluded.period,
310+
count_so_far = excluded.count_so_far`,
311+
period, count))
312+
313+
if err != nil {
314+
return err
315+
}
316+
317+
return tx.Commit()
318+
}
319+
320+
func (c *ConsensusDB) GetEpochValidatorsCount(epoch uint64) (uint64, error) {
321+
var totalCount uint64
322+
err := c.WriterDb.Get(&totalCount, "SELECT validatorscount FROM epochs WHERE epoch = $1", epoch)
323+
return totalCount, err
324+
}

backend/pkg/commons/db2/mocks/ConsensusRepository.go

Lines changed: 158 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/pkg/exporter/modules/base.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ func StartAll(moduleCtx ModuleContext, modules []ModuleInterface, justV2 bool) {
5252
go genesisExporter.Export()
5353

5454
go syncCommitteesExporter(moduleCtx.ConsClient)
55-
go syncCommitteesCountExporter()
55+
56+
syncCommitteesCountExporter := newSyncCommitteesCountExporter(ctx, consDB)
57+
go syncCommitteesCountExporter.Export()
58+
5659
if utils.Config.SSVExporter.Enabled {
5760
go ssvExporter()
5861
}

0 commit comments

Comments
 (0)