Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions eth/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,25 +410,29 @@ func (s *EventSyncer) SyncClustersToHead(ctx context.Context) error {
return nil
}

var updates int
topics := EventTopics() // Filter by handled event signatures
err = s.client.FetchLogs(ctx, s.ssvContract, fromBlock+1, headBlock, topics,
func(batchEnd uint64, logs []execution.BlockLogs) error {
for _, blockLogs := range logs {
if err := s.applyClusterUpdates(ctx, blockLogs); err != nil {
n, err := s.applyClusterUpdates(ctx, blockLogs)
if err != nil {
return err
}
updates += n
}
return nil
})
if err != nil {
return err
}

logger.Debugw("Clusters synced to head", "from", fromBlock+1, "to", headBlock)
logger.Debugw("Clusters synced to head", "from", fromBlock+1, "to", headBlock, "clusterUpdates", updates)
return nil
}
Comment on lines -427 to 432

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will mention just in case: perhaps we should log "how many updates were applied" even when this SyncClustersToHead errors - to simplify debugging for example? To apply, we'd also need to move the updates += n to execute upon both success/error too.


func (s *EventSyncer) applyClusterUpdates(ctx context.Context, blockLogs execution.BlockLogs) error {
func (s *EventSyncer) applyClusterUpdates(ctx context.Context, blockLogs execution.BlockLogs) (int, error) {
var updated int
for _, log := range blockLogs.Logs {
_, eventData, err := s.parser.parseLog(&log)
if err != nil {
Expand All @@ -453,15 +457,17 @@ func (s *EventSyncer) applyClusterUpdates(ctx context.Context, blockLogs executi

row := &storage.ClusterRow{
ClusterID: clusterID[:],
ValidatorCount: cluster.ValidatorCount,
NetworkFeeIndex: cluster.NetworkFeeIndex,
Index: cluster.Index,
IsActive: cluster.Active,
Balance: cluster.Balance,
}

if err := s.storage.UpdateClusterIfExists(ctx, row); err != nil {
return err
return updated, err
}
updated++
}
return nil
return updated, nil
}
2 changes: 2 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,15 @@ func (s *Storage) SetSyncMode(bulk bool) error {
func (s *Storage) UpdateClusterIfExists(ctx context.Context, cluster *ClusterRow) error {
query := `
UPDATE clusters SET
validator_count = ?,
network_fee_index = ?,
idx = ?,
is_active = ?,
balance = ?
WHERE cluster_id = ?
`
_, err := s.db.ExecContext(ctx, query,
cluster.ValidatorCount,
cluster.NetworkFeeIndex,
cluster.Index,
boolToInt(cluster.IsActive),
Expand Down
8 changes: 4 additions & 4 deletions storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ func TestStorage_UpdateClusterIfExists_Existing(t *testing.T) {
// Now update using UpdateClusterIfExists
updated := &ClusterRow{
ClusterID: clusterID,
ValidatorCount: 5,
NetworkFeeIndex: 500,
Index: 600,
IsActive: false,
Expand All @@ -825,6 +826,9 @@ func TestStorage_UpdateClusterIfExists_Existing(t *testing.T) {
t.Fatal("Expected cluster, got nil")
}

if got.ValidatorCount != 5 {
t.Errorf("ValidatorCount = %d, want 5", got.ValidatorCount)
}
if got.NetworkFeeIndex != 500 {
t.Errorf("NetworkFeeIndex = %d, want 500", got.NetworkFeeIndex)
}
Expand All @@ -837,10 +841,6 @@ func TestStorage_UpdateClusterIfExists_Existing(t *testing.T) {
if got.Balance.Cmp(big.NewInt(9999)) != 0 {
t.Errorf("Balance = %v, want 9999", got.Balance)
}
// ValidatorCount should remain unchanged
if got.ValidatorCount != 1 {
t.Errorf("ValidatorCount = %d, want 1 (unchanged)", got.ValidatorCount)
}
}

func TestStorage_DecodeOperatorIDs_Malformed(t *testing.T) {
Expand Down
Loading