Skip to content

Commit d64f6a4

Browse files
updated sync monitor to check for BatchConfigStarted event for DKG
1 parent b6c646f commit d64f6a4

File tree

3 files changed

+39
-59
lines changed

3 files changed

+39
-59
lines changed

rolling-shutter/keyperimpl/shutterservice/keyper.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ func (kpr *Keyper) Start(ctx context.Context, runner service.Runner) error {
114114
}
115115

116116
kpr.syncMonitor = &SyncMonitor{
117-
DBPool: kpr.dbpool,
118-
CheckInterval: time.Duration(kpr.config.Chain.SyncMonitorCheckInterval) * time.Second,
119-
DKGStartBlockDelta: kpr.config.Shuttermint.DKGStartBlockDelta,
117+
DBPool: kpr.dbpool,
118+
CheckInterval: time.Duration(kpr.config.Chain.SyncMonitorCheckInterval) * time.Second,
120119
}
121120
runner.Go(func() error { return kpr.processInputs(ctx) })
122121
return runner.StartService(kpr.core, kpr.chainSyncClient, kpr.eonKeyPublisher, kpr.syncMonitor)

rolling-shutter/keyperimpl/shutterservice/syncmonitor.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ import (
1616
)
1717

1818
type SyncMonitor struct {
19-
DBPool *pgxpool.Pool
20-
CheckInterval time.Duration
21-
DKGStartBlockDelta uint64
19+
DBPool *pgxpool.Pool
20+
CheckInterval time.Duration
2221
}
2322

2423
func (s *SyncMonitor) Start(ctx context.Context, runner service.Runner) error {
@@ -60,7 +59,11 @@ func (s *SyncMonitor) runCheck(
6059
keyperdb *keyperDB.Queries,
6160
lastBlockNumber *int64,
6261
) error {
63-
if s.dkgIsRunning(ctx, keyperdb) {
62+
isRunning, err := s.isDKGRunning(ctx, keyperdb)
63+
if err != nil {
64+
return fmt.Errorf("syncMonitor | error in dkgIsRunning: %w", err)
65+
}
66+
if isRunning {
6467
log.Debug().Msg("dkg is running, skipping sync monitor checks")
6568
return nil
6669
}
@@ -89,30 +92,28 @@ func (s *SyncMonitor) runCheck(
8992
return ErrBlockNotIncreasing
9093
}
9194

92-
func (s *SyncMonitor) dkgIsRunning(ctx context.Context, keyperdb *keyperDB.Queries) bool {
95+
func (s *SyncMonitor) isDKGRunning(ctx context.Context, keyperdb *keyperDB.Queries) (bool, error) {
9396
batchConfig, err := keyperdb.GetLatestBatchConfig(ctx)
94-
if err != nil {
95-
log.Error().
96-
Err(err).
97-
Msg("syncMonitor | error getting latest batchconfig")
98-
return true
97+
if errors.Is(err, pgx.ErrNoRows) {
98+
return false, nil
9999
}
100-
101-
tmSyncData, err := keyperdb.TMGetSyncMeta(ctx)
102100
if err != nil {
103101
log.Error().
104102
Err(err).
105-
Msg("syncMonitor | error getting TMSyncMeta")
106-
return true
103+
Msg("syncMonitor | error getting latest batchconfig")
104+
return false, err
107105
}
108106

109-
// if batchConfig submission height + DKGStartBlockDelta is greater than shuttermint height then dkg has not started yet
110-
if batchConfig.Height+int64(s.DKGStartBlockDelta) < tmSyncData.LastCommittedHeight {
111-
// if we get an error in getting dkg result then dkg is not completed
107+
// if batchconfig.Started is true that means dkg can happen for this keyper config index
108+
if batchConfig.Started {
109+
// if we get no rows in getting dkg result then dkg is not completed for that keyper config index
112110
_, err := keyperdb.GetDKGResult(ctx, int64(batchConfig.KeyperConfigIndex))
113-
if err != nil {
114-
return true
111+
if errors.Is(err, pgx.ErrNoRows) {
112+
return true, nil
113+
} else if err != nil {
114+
log.Error().Err(err).Msg("syncMonitor | error getting dkg result")
115+
return false, err
115116
}
116117
}
117-
return false
118+
return false, nil
118119
}

rolling-shutter/keyperimpl/shutterservice/syncmonitor_test.go

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ func setupTestData(ctx context.Context, t *testing.T, dbpool *pgxpool.Pool, bloc
2020
db := database.New(dbpool)
2121
keyperdb := keyperDB.New(dbpool)
2222

23-
// Set up batch config
23+
// Set up batch config with Started flag
2424
err := keyperdb.InsertBatchConfig(ctx, keyperDB.InsertBatchConfigParams{
2525
KeyperConfigIndex: 1,
2626
Keypers: []string{},
2727
Height: 50,
28+
Started: true,
2829
})
2930
assert.NilError(t, err)
3031

@@ -35,12 +36,6 @@ func setupTestData(ctx context.Context, t *testing.T, dbpool *pgxpool.Pool, bloc
3536
})
3637
assert.NilError(t, err)
3738

38-
// Set up TMSyncMeta
39-
err = keyperdb.TMSetSyncMeta(ctx, keyperDB.TMSetSyncMetaParams{
40-
LastCommittedHeight: 100,
41-
})
42-
assert.NilError(t, err)
43-
4439
// Set up initial block
4540
err = db.SetIdentityRegisteredEventSyncedUntil(ctx, database.SetIdentityRegisteredEventSyncedUntilParams{
4641
BlockHash: []byte{0x01, 0x02, 0x03},
@@ -142,6 +137,7 @@ func TestAPISyncMonitor_ContinuesWhenNoRows(t *testing.T) {
142137
err := keyperdb.InsertBatchConfig(ctx, keyperDB.InsertBatchConfigParams{
143138
KeyperConfigIndex: 1,
144139
Keypers: []string{},
140+
Started: true,
145141
})
146142
assert.NilError(t, err)
147143

@@ -177,7 +173,7 @@ func TestAPISyncMonitor_ContinuesWhenNoRows(t *testing.T) {
177173
}
178174
}
179175

180-
func TestAPISyncMonitor_ContinuesWhenNoDKGResult(t *testing.T) {
176+
func TestAPISyncMonitor_SkipsWhenDKGIsRunning(t *testing.T) {
181177
ctx, cancel := context.WithCancel(context.Background())
182178
defer cancel()
183179

@@ -191,12 +187,7 @@ func TestAPISyncMonitor_ContinuesWhenNoDKGResult(t *testing.T) {
191187
KeyperConfigIndex: 1,
192188
Keypers: []string{},
193189
Height: 50,
194-
})
195-
assert.NilError(t, err)
196-
197-
// Set up TMSyncMeta
198-
err = keyperdb.TMSetSyncMeta(ctx, keyperDB.TMSetSyncMetaParams{
199-
LastCommittedHeight: 100,
190+
Started: true,
200191
})
201192
assert.NilError(t, err)
202193

@@ -241,7 +232,7 @@ func TestAPISyncMonitor_ContinuesWhenNoDKGResult(t *testing.T) {
241232
assert.Equal(t, initialBlockNumber, syncedData.BlockNumber, "block number should remain unchanged")
242233
}
243234

244-
func TestAPISyncMonitor_ContinuesWhenNoBatchConfig(t *testing.T) {
235+
func TestAPISyncMonitor_RunsNormallyWhenNoBatchConfig(t *testing.T) {
245236
ctx, cancel := context.WithCancel(context.Background())
246237
defer cancel()
247238

@@ -279,9 +270,9 @@ func TestAPISyncMonitor_ContinuesWhenNoBatchConfig(t *testing.T) {
279270

280271
select {
281272
case err := <-errCh:
282-
t.Fatalf("expected monitor to continue without error, but got: %v", err)
273+
assert.ErrorContains(t, err, shutterservice.ErrBlockNotIncreasing.Error())
283274
case <-time.After(1 * time.Second):
284-
// Test passes if no error is received
275+
t.Fatalf("expected monitor to throw error, but no error returned")
285276
}
286277

287278
// Verify the block number hasn't changed
@@ -290,7 +281,7 @@ func TestAPISyncMonitor_ContinuesWhenNoBatchConfig(t *testing.T) {
290281
assert.Equal(t, initialBlockNumber, syncedData.BlockNumber, "block number should remain unchanged")
291282
}
292283

293-
func TestAPISyncMonitor_ContinuesWhenNoTMSyncMeta(t *testing.T) {
284+
func TestAPISyncMonitor_RunsNormallyWhenBatchConfigNotStarted(t *testing.T) {
294285
ctx, cancel := context.WithCancel(context.Background())
295286
defer cancel()
296287

@@ -299,22 +290,12 @@ func TestAPISyncMonitor_ContinuesWhenNoTMSyncMeta(t *testing.T) {
299290
db := database.New(dbpool)
300291
keyperdb := keyperDB.New(dbpool)
301292

302-
batchConfigHeight := int64(50)
303-
// Set TMSyncData height to be less than batchConfigHeight + DKGStartBlockDelta
304-
// This simulates a scenario where DKG hasn't started yet
305-
tmSyncHeight := int64(60)
306-
307-
// Set up batch config
293+
// Set up batch config with Started = false
308294
err := keyperdb.InsertBatchConfig(ctx, keyperDB.InsertBatchConfigParams{
309295
KeyperConfigIndex: 1,
310296
Keypers: []string{},
311-
Height: batchConfigHeight,
312-
})
313-
assert.NilError(t, err)
314-
315-
// Set up TMSyncMeta with lower height
316-
err = keyperdb.TMSetSyncMeta(ctx, keyperDB.TMSetSyncMetaParams{
317-
LastCommittedHeight: tmSyncHeight,
297+
Height: 50,
298+
Started: false,
318299
})
319300
assert.NilError(t, err)
320301

@@ -327,9 +308,8 @@ func TestAPISyncMonitor_ContinuesWhenNoTMSyncMeta(t *testing.T) {
327308
assert.NilError(t, err)
328309

329310
monitor := &shutterservice.SyncMonitor{
330-
DBPool: dbpool,
331-
CheckInterval: 5 * time.Second,
332-
DKGStartBlockDelta: 5,
311+
DBPool: dbpool,
312+
CheckInterval: 5 * time.Second,
333313
}
334314

335315
monitorCtx, cancelMonitor := context.WithCancel(ctx)
@@ -349,9 +329,9 @@ func TestAPISyncMonitor_ContinuesWhenNoTMSyncMeta(t *testing.T) {
349329

350330
select {
351331
case err := <-errCh:
352-
t.Fatalf("expected monitor to continue without error, but got: %v", err)
332+
assert.ErrorContains(t, err, shutterservice.ErrBlockNotIncreasing.Error())
353333
case <-time.After(1 * time.Second):
354-
// Test passes if no error is received
334+
t.Fatalf("expected monitor to throw error, but no error returned")
355335
}
356336

357337
// Verify the block number hasn't changed

0 commit comments

Comments
 (0)