Skip to content

Commit b215185

Browse files
refactor: revise and add tests for exporter/modules/sync_committees.go (#1375)
* refactor: updated sync committees handlers * refactor: move and update sync committees db functions * refactor: update sync committees db functions * test: add SyncCommitteeClient mocks wth mockery * refactor: update sync committees save and move SyncCommittee type def to commons/types * test: add ConsensusDBI mocks wth mockery * refactor: update tiered cache handler * test: added sync committees Export tests * fix: lint * fix: update sync committees calls in misc/main.go * refactor: rename ConsensusDBI to ConsensusRepository and move it to /db2/consensus.go * test: update ConsensusRepository mocks with mockery * refactor: pass deployment type as param to NewStatusReport func
1 parent 8e9885c commit b215185

23 files changed

+421
-118
lines changed

backend/cmd/misc/main.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,19 +2083,23 @@ func UpdateValidatorStatisticsSyncData(day uint64, dryRun bool) error {
20832083
return nil
20842084
}
20852085

2086-
func reExportSyncCommittee(rpcClient rpc.Client, p uint64, dryRun bool) error {
2086+
func reExportSyncCommittee(rpcClient rpc.Client, period uint64, dryRun bool) error {
2087+
ctx := context.Background()
2088+
consDB := db2.NewConsensusRepository(db.ReaderDb, db.WriterDb)
2089+
syncCommitteesExporter := modules.NewSyncCommitteesExporter(ctx, rpcClient, consDB)
2090+
20872091
if dryRun {
20882092
var currentData []struct {
20892093
ValidatorIndex uint64 `db:"validatorindex"`
20902094
CommitteeIndex uint64 `db:"committeeindex"`
20912095
}
20922096

2093-
err := db.WriterDb.Select(&currentData, `SELECT validatorindex, committeeindex FROM sync_committees WHERE period = $1`, p)
2097+
err := db.WriterDb.Select(&currentData, `SELECT validatorindex, committeeindex FROM sync_committees WHERE period = $1`, period)
20942098
if err != nil {
20952099
return errors.Wrap(err, "select old entries")
20962100
}
20972101

2098-
newData, err := modules.GetSyncCommitteAtPeriod(rpcClient, p)
2102+
newData, err := syncCommitteesExporter.GetSyncCommitteAtPeriod(period)
20992103
if err != nil {
21002104
return errors.Wrap(err, "export")
21012105
}
@@ -2121,12 +2125,12 @@ func reExportSyncCommittee(rpcClient rpc.Client, p uint64, dryRun bool) error {
21212125
log.Error(err, "error rolling back transaction", 0)
21222126
}
21232127
}()
2124-
_, err = tx.Exec(`DELETE FROM sync_committees WHERE period = $1`, p)
2128+
_, err = tx.Exec(`DELETE FROM sync_committees WHERE period = $1`, period)
21252129
if err != nil {
21262130
return errors.Wrap(err, "delete old entries")
21272131
}
21282132

2129-
err = modules.ExportSyncCommitteeAtPeriod(rpcClient, p, tx)
2133+
err = syncCommitteesExporter.ExportSyncCommitteeData(period)
21302134
if err != nil {
21312135
return errors.Wrap(err, "export")
21322136
}

backend/pkg/api/services/service_average_network_efficiency.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (s *Services) startEfficiencyDataService(wg *sync.WaitGroup) {
2626
for {
2727
startTime := time.Now()
2828
delay := time.Duration(utils.Config.Chain.ClConfig.SlotsPerEpoch*utils.Config.Chain.ClConfig.SecondsPerSlot) * time.Second
29-
r := services.NewStatusReport(constants.Event_ApiServiceAvgEfficiency, constants.Default, delay)
29+
r := services.NewStatusReport(constants.Event_ApiServiceAvgEfficiency, utils.Config.DeploymentType, constants.Default, delay)
3030
r(constants.Running, nil)
3131
err := s.updateEfficiencyData() // TODO: only update data if something has changed (new head epoch)
3232
if err != nil {

backend/pkg/api/services/service_slot_viz.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (s *Services) startSlotVizDataService(wg *sync.WaitGroup) {
3232
for {
3333
startTime := time.Now()
3434
delay := time.Duration(utils.Config.Chain.ClConfig.SecondsPerSlot) * time.Second
35-
r := services.NewStatusReport(constants.Event_ApiServiceSlotViz, constants.Default, delay)
35+
r := services.NewStatusReport(constants.Event_ApiServiceSlotViz, utils.Config.DeploymentType, constants.Default, delay)
3636
r(constants.Running, nil)
3737
err := s.updateSlotVizData() // TODO: only update data if something has changed (new head slot or new head epoch)
3838
if err != nil {

backend/pkg/api/services/service_validator_mapping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (s *Services) startIndexMappingService(wg *sync.WaitGroup) {
4141
startTime := time.Now()
4242
delay := time.Duration(utils.Config.Chain.ClConfig.SecondsPerSlot) * time.Second
4343
err = nil // clear error
44-
r := services.NewStatusReport(constants.Event_ApiServiceValidatorMapping, constants.Default, delay)
44+
r := services.NewStatusReport(constants.Event_ApiServiceValidatorMapping, utils.Config.DeploymentType, constants.Default, delay)
4545
r(constants.Running, nil)
4646
latestEpoch := cache.LatestEpoch.Get()
4747
if currentValidatorMapping.Load() == nil || latestEpoch != lastEpochUpdate {

backend/pkg/commons/db/db.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/ethereum/go-ethereum/common"
1818
"github.com/gobitfly/beaconchain/pkg/commons/log"
1919
"github.com/gobitfly/beaconchain/pkg/commons/metrics"
20-
2120
"github.com/gobitfly/beaconchain/pkg/commons/types"
2221

2322
"github.com/gobitfly/beaconchain/pkg/commons/utils"

backend/pkg/commons/db2/consensus.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package db2
33
import (
44
"database/sql"
55
"fmt"
6+
"strings"
67

78
"github.com/gobitfly/beaconchain/pkg/commons/log"
89
"github.com/gobitfly/beaconchain/pkg/commons/types"
@@ -31,6 +32,8 @@ type ConsensusRepository interface {
3132
SaveSyncCommitteesCount(period uint64, count float64) error
3233
GetEpochValidatorsCount(epoch uint64) (uint64, error)
3334
GetLatestFinalizedEpoch() (uint64, error)
35+
SaveSyncCommitteeData(data []types.SyncCommittee) error
36+
GetSyncCommitteesPeriods() ([]uint64, error)
3437
}
3538

3639
type ConsensusDB struct {
@@ -322,3 +325,40 @@ func (c *ConsensusDB) GetEpochValidatorsCount(epoch uint64) (uint64, error) {
322325
err := c.WriterDb.Get(&totalCount, "SELECT validatorscount FROM epochs WHERE epoch = $1", epoch)
323326
return totalCount, err
324327
}
328+
329+
func (c *ConsensusDB) GetSyncCommitteesPeriods() ([]uint64, error) {
330+
var periods []uint64
331+
err := c.WriterDb.Select(&periods, `SELECT period FROM sync_committees GROUP BY period`)
332+
return periods, err
333+
}
334+
335+
func (c *ConsensusDB) SaveSyncCommitteeData(data []types.SyncCommittee) error {
336+
tx, err := c.WriterDb.Beginx()
337+
if err != nil {
338+
return err
339+
}
340+
defer utils.Rollback(tx)
341+
342+
nArgs := 3
343+
ids := make([]string, len(data))
344+
queryArgs := make([]interface{}, len(data)*nArgs)
345+
for i, entry := range data {
346+
ids[i] = fmt.Sprintf("($%d,$%d,$%d)", i*nArgs+1, i*nArgs+2, i*nArgs+3)
347+
queryArgs[i*nArgs] = entry.Period
348+
queryArgs[i*nArgs+1] = entry.ValidatorIndex
349+
queryArgs[i*nArgs+2] = entry.CommitteeIndex
350+
}
351+
352+
_, err = tx.Exec(
353+
fmt.Sprintf(`
354+
INSERT INTO sync_committees (period, validatorindex, committeeindex)
355+
VALUES %s ON CONFLICT (period, validatorindex, committeeindex) DO NOTHING`,
356+
strings.Join(ids, ",")),
357+
queryArgs...)
358+
359+
if err != nil {
360+
return err
361+
}
362+
363+
return tx.Commit()
364+
}

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

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

backend/pkg/commons/rpc/mocks/SyncCommitteeClient.go

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

backend/pkg/commons/types/exporter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ type SyncAggregate struct {
105105
SyncAggregateParticipation float64
106106
}
107107

108+
type SyncCommittee struct {
109+
Period uint64 `json:"period"`
110+
ValidatorIndex uint64 `json:"validatorindex"`
111+
CommitteeIndex uint64 `json:"committeeindex"`
112+
}
113+
108114
// Block is a struct to hold block data
109115
type Block struct {
110116
Status uint64

backend/pkg/exporter/modules/base.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ func StartAll(moduleCtx ModuleContext, modules []ModuleInterface, justV2 bool) {
5151
genesisExporter := newGenesisDepositsExporter(ctx, moduleCtx.ConsClient, consDB)
5252
go genesisExporter.Export()
5353

54-
go syncCommitteesExporter(moduleCtx.ConsClient)
54+
syncCommitteesExporter := NewSyncCommitteesExporter(ctx, moduleCtx.ConsClient, consDB)
55+
go syncCommitteesExporter.Export()
5556

5657
syncCommitteesCountExporter := newSyncCommitteesCountExporter(ctx, consDB)
5758
go syncCommitteesCountExporter.Export()
@@ -165,7 +166,7 @@ func notifyAllModules(goPool *errgroup.Group, modules []ModuleInterface, f func(
165166
module := module
166167
goPool.Go(func() error {
167168
start := time.Now()
168-
r := services.NewStatusReport(module.GetMonitoringEventId(), 5*time.Minute, constants.Default)
169+
r := services.NewStatusReport(module.GetMonitoringEventId(), utils.Config.DeploymentType, 5*time.Minute, constants.Default)
169170
r(constants.Running, nil)
170171
err := f(module)
171172
if err != nil {

0 commit comments

Comments
 (0)