Skip to content

Commit a6d5ab2

Browse files
committed
using local redis cache for development
1 parent 440fb1d commit a6d5ab2

File tree

7 files changed

+60
-27
lines changed

7 files changed

+60
-27
lines changed

backend/pkg/api/data_access/data_access.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type DataAccessService struct {
5757
userWriter *sqlx.DB
5858
bigtable *db.Bigtable
5959
persistentRedisDbClient *redis.Client
60+
localRedisDbClient *redis.Client
6061

6162
services *services.Services
6263
config *types.Config
@@ -79,6 +80,7 @@ func NewDataAccessService(cfg *types.Config) *DataAccessService {
7980
db.ClickHouseReader = das.clickhouseReader
8081
db.BigtableClient = das.bigtable
8182
db.PersistentRedisDbClient = das.persistentRedisDbClient
83+
db.LocalRedisDbClient = das.localRedisDbClient
8284

8385
return das
8486
}
@@ -147,6 +149,21 @@ func createDataAccessService(cfg *types.Config) *DataAccessService {
147149
dataAccessService.persistentRedisDbClient = rdc
148150
}()
149151

152+
// Initialize the persistent redis client
153+
wg.Add(1)
154+
go func() {
155+
defer wg.Done()
156+
rdc := redis.NewClient(&redis.Options{
157+
Addr: cfg.RedisLocalCacheEndpoint,
158+
ReadTimeout: time.Second * 60,
159+
})
160+
161+
if err := rdc.Ping(context.Background()).Err(); err != nil {
162+
log.Fatal(err, "error connecting to local redis cache", 0)
163+
}
164+
dataAccessService.localRedisDbClient = rdc
165+
}()
166+
150167
wg.Wait()
151168

152169
if cfg.TieredCacheProvider != "redis" {
@@ -159,7 +176,7 @@ func createDataAccessService(cfg *types.Config) *DataAccessService {
159176

160177
func (d *DataAccessService) StartDataAccessServices() {
161178
// Create the services
162-
d.services = services.NewServices(d.readerDb, d.writerDb, d.clickhouseReader, d.bigtable, d.persistentRedisDbClient)
179+
d.services = services.NewServices(d.readerDb, d.writerDb, d.clickhouseReader, d.bigtable, d.persistentRedisDbClient, d.localRedisDbClient)
163180

164181
// Initialize repositories
165182
d.registerNotificationInterfaceTypes()

backend/pkg/api/services/service.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ type Services struct {
2121
clickhouseReader *sqlx.DB
2222
bigtable *db.Bigtable
2323
persistentRedisDbClient *redis.Client
24+
localRedisDbClient *redis.Client
2425
}
2526

26-
func NewServices(readerDb, writerDb, clickhouseReader *sqlx.DB, bigtable *db.Bigtable, persistentRedisDbClient *redis.Client) *Services {
27+
func NewServices(readerDb, writerDb, clickhouseReader *sqlx.DB, bigtable *db.Bigtable, persistentRedisDbClient, localRedisDbClient *redis.Client) *Services {
2728
return &Services{
2829
readerDb: readerDb,
2930
writerDb: writerDb,
3031
clickhouseReader: clickhouseReader,
3132
bigtable: bigtable,
3233
persistentRedisDbClient: persistentRedisDbClient,
34+
localRedisDbClient: localRedisDbClient,
3335
}
3436
}
3537

backend/pkg/api/services/service_slot_viz.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
package services
22

33
import (
4-
"bytes"
5-
"context"
6-
"encoding/gob"
74
"fmt"
8-
"strconv"
9-
"strings"
105
"sync"
11-
"sync/atomic"
126
"time"
137

14-
"github.com/gobitfly/beaconchain/pkg/commons/cache"
15-
"github.com/gobitfly/beaconchain/pkg/commons/db"
168
"github.com/gobitfly/beaconchain/pkg/commons/log"
17-
"github.com/gobitfly/beaconchain/pkg/commons/types"
189
"github.com/gobitfly/beaconchain/pkg/commons/utils"
19-
constypes "github.com/gobitfly/beaconchain/pkg/consapi/types"
2010
"github.com/gobitfly/beaconchain/pkg/monitoring/constants"
2111
"github.com/gobitfly/beaconchain/pkg/monitoring/services"
22-
"github.com/juliangruber/go-intersect"
23-
"github.com/lib/pq"
24-
"github.com/pkg/errors"
25-
"golang.org/x/sync/errgroup"
2612
)
2713

28-
var currentDutiesInfo atomic.Pointer[SyncData]
14+
// var currentDutiesInfo atomic.Pointer[SyncData]
2915

3016
func (s *Services) startSlotVizDataService(wg *sync.WaitGroup) {
3117
o := sync.Once{}
@@ -49,7 +35,8 @@ func (s *Services) startSlotVizDataService(wg *sync.WaitGroup) {
4935
}
5036

5137
func (s *Services) updateSlotVizData() error {
52-
var dutiesInfo *SyncData
38+
return nil
39+
/*var dutiesInfo *SyncData
5340
if currentDutiesInfo.Load() == nil {
5441
dutiesInfo = s.initDutiesInfo()
5542
} else {
@@ -287,19 +274,39 @@ func (s *Services) updateSlotVizData() error {
287274
288275
currentDutiesInfo.Store(dutiesInfo)
289276
290-
return nil
277+
return nil*/
291278
}
292279

293280
// GetCurrentDutiesInfo returns the current duties info and a function to release the lock
294281
// Call release lock after you are done with accessing the data, otherwise it will block the slot viz service from updating
295282
func (s *Services) GetCurrentDutiesInfo() (*SyncData, error) {
296-
if currentDutiesInfo.Load() == nil {
283+
dutiesInfo := SyncData{}
284+
// dummy data to avoid downloading duties from redis
285+
dutiesInfo.PropAssignmentsForSlot = make(map[uint64]uint64)
286+
// validators in megatron
287+
dutiesInfo.PropAssignmentsForSlot[3_000_000] = 1
288+
dutiesInfo.PropAssignmentsForSlot[3_000_001] = 7
289+
dutiesInfo.PropAssignmentsForSlot[3_000_002] = 13
290+
dutiesInfo.PropAssignmentsForSlot[3_000_003] = 19
291+
dutiesInfo.PropAssignmentsForSlot[3_000_004] = 25
292+
dutiesInfo.PropAssignmentsForSlot[3_000_005] = 31
293+
// and some others
294+
dutiesInfo.PropAssignmentsForSlot[3_000_006] = 29141
295+
dutiesInfo.PropAssignmentsForSlot[3_000_007] = 500167
296+
dutiesInfo.PropAssignmentsForSlot[3_000_008] = 531705
297+
dutiesInfo.PropAssignmentsForSlot[3_000_009] = 542402
298+
dutiesInfo.PropAssignmentsForSlot[3_000_010] = 845851
299+
dutiesInfo.PropAssignmentsForSlot[3_000_011] = 894749
300+
301+
return &dutiesInfo, nil
302+
303+
/*if currentDutiesInfo.Load() == nil {
297304
return nil, fmt.Errorf("%w: dutiesInfo", ErrWaiting)
298305
}
299-
return currentDutiesInfo.Load(), nil
306+
return currentDutiesInfo.Load(), nil*/
300307
}
301308

302-
func (s *Services) initDutiesInfo() *SyncData {
309+
/*func (s *Services) initDutiesInfo() *SyncData {
303310
dutiesInfo := SyncData{}
304311
dutiesInfo.LatestSlot = uint64(0)
305312
dutiesInfo.LatestProposedSlot = uint64(0)
@@ -433,9 +440,9 @@ func (s *Services) copyAndCleanDutiesInfo() *SyncData {
433440
}
434441
}
435442
return dutiesInfo
436-
}
443+
}*/
437444

438-
func (s *Services) getMaxValidatorDutiesInfoSlot() uint64 {
445+
/*func (s *Services) getMaxValidatorDutiesInfoSlot() uint64 {
439446
headEpoch := cache.LatestEpoch.Get()
440447
slotsPerEpoch := utils.Config.Chain.ClConfig.SlotsPerEpoch
441448
@@ -451,15 +458,15 @@ func (s *Services) getMaxValidatorDutiesInfoSlot() uint64 {
451458
- Attestation data amount is the main culprit for the database call since it returns huge amounts of data
452459
- Other fields used by slotviz do not change as well (sync bits, exec block). If we at some point include changing fields for headEpoch -2
453460
we should consider making this a separate call to avoid loading all attestation data again
454-
*/
461+
455462
if err == nil && p.AssignmentsFetchedForEpoch > 0 && headEpoch > 0 { // if we have fetched epoch assignments before
456463
minEpoch = headEpoch - 1
457464
}
458465
459466
maxValidatorDutiesInfoSlot := minEpoch * slotsPerEpoch
460467
461468
return maxValidatorDutiesInfoSlot
462-
}
469+
}*/
463470

464471
type SyncData struct {
465472
LatestSlot uint64

backend/pkg/api/services/service_validator_mapping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (s *Services) updateValidatorMapping() error {
9191
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
9292
defer cancel()
9393
key := fmt.Sprintf("%d:%s", utils.Config.Chain.ClConfig.DepositChainID, "vm")
94-
compressed, err := s.persistentRedisDbClient.Get(ctx, key).Bytes()
94+
compressed, err := s.localRedisDbClient.Get(ctx, key).Bytes()
9595
if err != nil {
9696
return errors.Wrap(err, "failed to get compressed validator mapping from db")
9797
}

backend/pkg/commons/db/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var ClickHouseReader *sqlx.DB
4848
var ClickHouseWriter *sqlx.DB
4949

5050
var PersistentRedisDbClient *redis.Client
51+
var LocalRedisDbClient *redis.Client
5152

5253
var FarFutureEpoch = uint64(18446744073709551615)
5354
var MaxSqlNumber = uint64(9223372036854775807)

backend/pkg/commons/types/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Config struct {
3333
EtherscanAPIKey string `yaml:"etherscanApiKey" env:"ETHERSCAN_API_KEY"`
3434
EtherscanAPIBaseURL string `yaml:"etherscanApiBaseUrl" env:"ETHERSCAN_API_BASEURL"`
3535
RedisCacheEndpoint string `yaml:"redisCacheEndpoint" env:"REDIS_CACHE_ENDPOINT"`
36+
RedisLocalCacheEndpoint string `yaml:"redisLocalCacheEndpoint" env:"REDIS_LOCAL_CACHE_ENDPOINT"`
3637
RedisSessionStoreEndpoint string `yaml:"redisSessionStoreEndpoint" env:"REDIS_SESSION_STORE_ENDPOINT"`
3738
TieredCacheProvider string `yaml:"tieredCacheProvider" env:"CACHE_PROVIDER"`
3839
ReportServiceStatus bool `yaml:"reportServiceStatus" env:"REPORT_SERVICE_STATUS"`

backend/pkg/commons/utils/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ func ReadConfig(cfg *types.Config, path string) error {
279279
cfg.RedisSessionStoreEndpoint = cfg.RedisCacheEndpoint
280280
}
281281

282+
if cfg.RedisLocalCacheEndpoint == "" && cfg.RedisCacheEndpoint != "" {
283+
log.Warnf("using RedisCacheEndpoint %s as RedisLocalCacheEndpoint as no dedicated RedisLocalCacheEndpoint was provided", cfg.RedisCacheEndpoint)
284+
cfg.RedisLocalCacheEndpoint = cfg.RedisCacheEndpoint
285+
}
286+
282287
confSanityCheck(cfg)
283288

284289
log.InfoWithFields(log.Fields{

0 commit comments

Comments
 (0)