diff --git a/ecosystem/cache/handlers.go b/ecosystem/cache/handlers.go index 248225c168..853a39c878 100644 --- a/ecosystem/cache/handlers.go +++ b/ecosystem/cache/handlers.go @@ -12,7 +12,7 @@ import ( "time" sdkerrors "cosmossdk.io/errors" - "github.com/dgraph-io/ristretto" + "github.com/dgraph-io/ristretto/v2" "github.com/lavanet/lava/v4/protocol/lavaprotocol" "github.com/lavanet/lava/v4/protocol/parser" "github.com/lavanet/lava/v4/utils" @@ -332,13 +332,13 @@ func (s *RelayerCacheServer) SetRelay(ctx context.Context, relayCacheSet *pairin cache := s.CacheServer.finalizedCache if relayCacheSet.IsNodeError { nodeErrorExpiration := lavaslices.Min([]time.Duration{time.Duration(relayCacheSet.AverageBlockTime), s.CacheServer.ExpirationNodeErrors}) - cache.SetWithTTL(cacheKey, cacheValue, cacheValue.Cost(), nodeErrorExpiration) + cache.SetWithTTL(string(cacheKey), cacheValue, cacheValue.Cost(), nodeErrorExpiration) } else { - cache.SetWithTTL(cacheKey, cacheValue, cacheValue.Cost(), s.CacheServer.ExpirationFinalized) + cache.SetWithTTL(string(cacheKey), cacheValue, cacheValue.Cost(), s.CacheServer.ExpirationFinalized) } } else { cache := s.CacheServer.tempCache - cache.SetWithTTL(cacheKey, cacheValue, cacheValue.Cost(), s.getExpirationForChain(time.Duration(relayCacheSet.AverageBlockTime), relayCacheSet.BlockHash)) + cache.SetWithTTL(string(cacheKey), cacheValue, cacheValue.Cost(), s.getExpirationForChain(time.Duration(relayCacheSet.AverageBlockTime), relayCacheSet.BlockHash)) } // Setting the seen block for shared state. s.setSeenBlockOnSharedStateMode(relayCacheSet.ChainId, relayCacheSet.SharedStateId, latestKnownBlock) @@ -417,7 +417,7 @@ func (s *RelayerCacheServer) getExpirationForChain(averageBlockTimeForChain time return s.CacheServer.ExpirationForChain(averageBlockTimeForChain) } -func getNonExpiredFromCache(c *ristretto.Cache, key interface{}) (value interface{}, found bool) { +func getNonExpiredFromCache(c *ristretto.Cache[string, any], key string) (value interface{}, found bool) { value, found = c.Get(key) if found { return value, true @@ -429,25 +429,25 @@ func (s *RelayerCacheServer) findInAllCaches(finalized bool, cacheKey []byte) (r inner := func(finalized bool, cacheKey []byte) (interface{}, string, bool) { if finalized { cache := s.CacheServer.finalizedCache - value, found := getNonExpiredFromCache(cache, cacheKey) + value, found := getNonExpiredFromCache(cache, string(cacheKey)) if found { return value, "finalized_cache", true } // if a key is finalized still doesn't mean it wasn't set when unfinalized cache = s.CacheServer.tempCache - value, found = getNonExpiredFromCache(cache, cacheKey) + value, found = getNonExpiredFromCache(cache, string(cacheKey)) if found { return value, "temp_cache", true } } else { // if something isn't finalized now it was never finalized, but sometimes when we don't have information we try to get a non finalized entry when in fact its finalized cache := s.CacheServer.tempCache - value, found := getNonExpiredFromCache(cache, cacheKey) + value, found := getNonExpiredFromCache(cache, string(cacheKey)) if found { return value, "temp_cache", true } cache = s.CacheServer.finalizedCache - value, found = getNonExpiredFromCache(cache, cacheKey) + value, found = getNonExpiredFromCache(cache, string(cacheKey)) if found { return value, "finalized_cache", true } diff --git a/ecosystem/cache/server.go b/ecosystem/cache/server.go index 73e9037902..bd8e2e2cc1 100644 --- a/ecosystem/cache/server.go +++ b/ecosystem/cache/server.go @@ -14,7 +14,7 @@ import ( "github.com/lavanet/lava/v4/protocol/chainlib/chainproxy" "github.com/lavanet/lava/v4/utils/lavaslices" - "github.com/dgraph-io/ristretto" + "github.com/dgraph-io/ristretto/v2" "github.com/improbable-eng/grpc-web/go/grpcweb" "github.com/lavanet/lava/v4/utils" pairingtypes "github.com/lavanet/lava/v4/x/pairing/types" @@ -43,9 +43,9 @@ const ( ) type CacheServer struct { - finalizedCache *ristretto.Cache - tempCache *ristretto.Cache // cache for temporary inputs, such as latest blocks - blocksHashesToHeightsCache *ristretto.Cache + finalizedCache *ristretto.Cache[string, any] + tempCache *ristretto.Cache[string, any] // cache for temporary inputs, such as latest blocks + blocksHashesToHeightsCache *ristretto.Cache[string, any] ExpirationFinalized time.Duration ExpirationNonFinalized time.Duration ExpirationNodeErrors time.Duration @@ -71,17 +71,17 @@ func (cs *CacheServer) InitCache( cs.ExpirationBlocksHashesToHeights = time.Duration(float64(expirationBlocksHashesToHeights)) var err error - cs.tempCache, err = ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: cs.CacheMaxCost, BufferItems: 64}) + cs.tempCache, err = ristretto.NewCache(&ristretto.Config[string, any]{NumCounters: CacheNumCounters, MaxCost: cs.CacheMaxCost, BufferItems: 64}) if err != nil { utils.LavaFormatFatal("could not create cache", err) } - cs.finalizedCache, err = ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: cs.CacheMaxCost, BufferItems: 64}) + cs.finalizedCache, err = ristretto.NewCache(&ristretto.Config[string, any]{NumCounters: CacheNumCounters, MaxCost: cs.CacheMaxCost, BufferItems: 64}) if err != nil { utils.LavaFormatFatal("could not create finalized cache", err) } - cs.blocksHashesToHeightsCache, err = ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: cs.CacheMaxCost, BufferItems: 64}) + cs.blocksHashesToHeightsCache, err = ristretto.NewCache(&ristretto.Config[string, any]{NumCounters: CacheNumCounters, MaxCost: cs.CacheMaxCost, BufferItems: 64}) if err != nil { utils.LavaFormatFatal("could not create blocks hashes to heights cache", err) } diff --git a/go.mod b/go.mod index 7015c35da6..9f16e3b357 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/spf13/cast v1.6.0 github.com/spf13/cobra v1.8.0 - github.com/stretchr/testify v1.9.0 + github.com/stretchr/testify v1.10.0 google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect google.golang.org/grpc v1.62.1 google.golang.org/protobuf v1.33.0 @@ -81,6 +81,7 @@ require ( github.com/cosmos/rosetta-sdk-go v0.10.0 // indirect github.com/creachadair/taskgroup v0.4.2 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect + github.com/dgraph-io/ristretto/v2 v2.0.1 // indirect github.com/getsentry/sentry-go v0.23.0 // indirect github.com/go-logr/logr v1.3.0 // indirect github.com/go-logr/stdr v1.2.2 // indirect @@ -143,7 +144,7 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect github.com/cespare/xxhash v1.1.0 // indirect - github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/coinbase/rosetta-sdk-go v0.7.9 // indirect github.com/cosmos/btcutil v1.0.5 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect @@ -154,7 +155,7 @@ require ( github.com/deckarep/golang-set v1.8.0 github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect github.com/dgraph-io/badger/v2 v2.2007.4 // indirect - github.com/dgraph-io/ristretto v0.1.1 + github.com/dgraph-io/ristretto v0.2.0 github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/dvsekhvalnov/jose2go v1.6.0 // indirect @@ -226,7 +227,7 @@ require ( golang.org/x/exp v0.0.0-20230905200255-921286631fa9 golang.org/x/net v0.23.0 golang.org/x/sync v0.6.0 - golang.org/x/sys v0.20.0 // indirect + golang.org/x/sys v0.28.0 // indirect golang.org/x/term v0.18.0 golang.org/x/text v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/go.sum b/go.sum index f4eb9b3ada..4cc939a4c2 100644 --- a/go.sum +++ b/go.sum @@ -338,6 +338,8 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM= @@ -461,6 +463,10 @@ github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de/go.mod h1:KP github.com/dgraph-io/ristretto v0.0.3/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E= github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA= +github.com/dgraph-io/ristretto v0.2.0 h1:XAfl+7cmoUDWW/2Lx8TGZQjjxIQ2Ley9DSf52dru4WE= +github.com/dgraph-io/ristretto v0.2.0/go.mod h1:8uBHCU/PBV4Ag0CJrP47b9Ofby5dqWNh4FicAdoqFNU= +github.com/dgraph-io/ristretto/v2 v2.0.1 h1:7W0LfEP+USCmtrUjJsk+Jv2jbhJmb72N4yRI7GrLdMI= +github.com/dgraph-io/ristretto/v2 v2.0.1/go.mod h1:K7caLeufSdxm+ITp1n/73U+VbFVAHrexfLbz4n14hpo= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-bitstream v0.0.0-20180413035011-3522498ce2c8/go.mod h1:VMaSuZ+SZcx/wljOQKvp5srsbCiKDEb6K2wC4+PiBmQ= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= @@ -1230,6 +1236,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= @@ -1620,6 +1628,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/protocol/lavaprotocol/relay_retries_manager.go b/protocol/lavaprotocol/relay_retries_manager.go index ca32c577d5..a02c66c333 100644 --- a/protocol/lavaprotocol/relay_retries_manager.go +++ b/protocol/lavaprotocol/relay_retries_manager.go @@ -3,7 +3,7 @@ package lavaprotocol import ( "time" - "github.com/dgraph-io/ristretto" + "github.com/dgraph-io/ristretto/v2" "github.com/lavanet/lava/v4/utils" ) @@ -23,11 +23,11 @@ type RelayRetriesManagerInf interface { // On node errors we try to send a relay again. // If this relay failed all retries we ban it from retries to avoid spam and save resources type RelayRetriesManager struct { - cache *ristretto.Cache + cache *ristretto.Cache[string, any] } func NewRelayRetriesManager() *RelayRetriesManager { - cache, err := ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) + cache, err := ristretto.NewCache(&ristretto.Config[string, any]{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) if err != nil { utils.LavaFormatFatal("failed setting up cache for consumer consistency", err) } diff --git a/protocol/monitoring/alerting.go b/protocol/monitoring/alerting.go index d5daa8b07a..02d5c3abaf 100644 --- a/protocol/monitoring/alerting.go +++ b/protocol/monitoring/alerting.go @@ -10,7 +10,7 @@ import ( "github.com/goccy/go-json" - "github.com/dgraph-io/ristretto" + "github.com/dgraph-io/ristretto/v2" "github.com/lavanet/lava/v4/utils" "github.com/lavanet/lava/v4/utils/sigs" ) @@ -72,7 +72,7 @@ type Alerting struct { allowedTimeGapVsReference time.Duration maxProviderLatency time.Duration sameAlertInterval time.Duration - AlertsCache *ristretto.Cache + AlertsCache *ristretto.Cache[string, any] activeAlerts map[AlertEntry]AlertCount // count how many occurrences of an alert healthy map[LavaEntity]struct{} unhealthy map[LavaEntity]struct{} @@ -114,7 +114,7 @@ func NewAlerting(options AlertingOptions) *Alerting { } else { al.sameAlertInterval = defaultSameAlertInterval } - cache, err := ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) + cache, err := ristretto.NewCache(&ristretto.Config[string, any]{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) if err != nil { utils.LavaFormatFatal("failed setting up cache for queries", err) } diff --git a/protocol/provideroptimizer/provider_optimizer.go b/protocol/provideroptimizer/provider_optimizer.go index 6f9198a9e9..c2a2ba5deb 100644 --- a/protocol/provideroptimizer/provider_optimizer.go +++ b/protocol/provideroptimizer/provider_optimizer.go @@ -7,7 +7,7 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/dgraph-io/ristretto" + "github.com/dgraph-io/ristretto/v2" "github.com/lavanet/lava/v4/protocol/common" "github.com/lavanet/lava/v4/protocol/metrics" "github.com/lavanet/lava/v4/utils" @@ -45,8 +45,8 @@ type ConcurrentBlockStore struct { } type cacheInf interface { - Get(key interface{}) (interface{}, bool) - Set(key, value interface{}, cost int64) bool + Get(key string) (interface{}, bool) + Set(key string, value interface{}, cost int64) bool } type consumerOptimizerQoSClientInf interface { @@ -55,7 +55,7 @@ type consumerOptimizerQoSClientInf interface { type ProviderOptimizer struct { strategy Strategy providersStorage cacheInf - providerRelayStats *ristretto.Cache // used to decide on the half time of the decay + providerRelayStats *ristretto.Cache[string, any] // used to decide on the half time of the decay averageBlockTime time.Duration baseWorldLatency time.Duration wantedNumProvidersInConcurrency uint @@ -536,11 +536,11 @@ func (po *ProviderOptimizer) getRelayStatsTimes(providerAddress string) []time.T } func NewProviderOptimizer(strategy Strategy, averageBlockTIme, baseWorldLatency time.Duration, wantedNumProvidersInConcurrency uint, consumerOptimizerQoSClientInf consumerOptimizerQoSClientInf, chainId string) *ProviderOptimizer { - cache, err := ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) + cache, err := ristretto.NewCache(&ristretto.Config[string, any]{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) if err != nil { utils.LavaFormatFatal("failed setting up cache for queries", err) } - relayCache, err := ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) + relayCache, err := ristretto.NewCache(&ristretto.Config[string, any]{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) if err != nil { utils.LavaFormatFatal("failed setting up cache for queries", err) } diff --git a/protocol/rpcconsumer/consumer_consistency.go b/protocol/rpcconsumer/consumer_consistency.go index e2e446a368..c1ef7c606b 100644 --- a/protocol/rpcconsumer/consumer_consistency.go +++ b/protocol/rpcconsumer/consumer_consistency.go @@ -3,7 +3,7 @@ package rpcconsumer import ( "time" - "github.com/dgraph-io/ristretto" + "github.com/dgraph-io/ristretto/v2" common "github.com/lavanet/lava/v4/protocol/common" "github.com/lavanet/lava/v4/utils" ) @@ -16,7 +16,7 @@ const ( ) type ConsumerConsistency struct { - cache *ristretto.Cache + cache *ristretto.Cache[string, any] specId string } @@ -72,7 +72,7 @@ func (cc *ConsumerConsistency) GetSeenBlock(userData common.UserData) (int64, bo } func NewConsumerConsistency(specId string) *ConsumerConsistency { - cache, err := ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) + cache, err := ristretto.NewCache(&ristretto.Config[string, any]{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64, IgnoreInternalCost: true}) if err != nil { utils.LavaFormatFatal("failed setting up cache for consumer consistency", err) } diff --git a/protocol/statetracker/updaters/state_query.go b/protocol/statetracker/updaters/state_query.go index 0877f171c1..a549108659 100644 --- a/protocol/statetracker/updaters/state_query.go +++ b/protocol/statetracker/updaters/state_query.go @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" grpc1 "github.com/cosmos/gogoproto/grpc" - "github.com/dgraph-io/ristretto" + "github.com/dgraph-io/ristretto/v2" reliabilitymanager "github.com/lavanet/lava/v4/protocol/rpcprovider/reliabilitymanager" "github.com/lavanet/lava/v4/utils" conflicttypes "github.com/lavanet/lava/v4/x/conflict/types" @@ -64,7 +64,7 @@ type StateQuery struct { epochStorageQueryClient epochstoragetypes.QueryClient protocolClient protocoltypes.QueryClient downtimeClient downtimev1.QueryClient - ResponsesCache *ristretto.Cache + ResponsesCache *ristretto.Cache[string, any] tendermintRPC client.TendermintRPC } @@ -72,7 +72,7 @@ type StateQuery struct { func NewStateQuery(ctx context.Context, accessInf StateQueryAccessInf) *StateQuery { sq := &StateQuery{} sq.UpdateAccess(accessInf) - cache, err := ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64}) + cache, err := ristretto.NewCache(&ristretto.Config[string, any]{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64}) if err != nil { utils.LavaFormatFatal("failed setting up cache for queries", err) }