Skip to content

Commit

Permalink
feat: PRT-change-slices-to-lavaslices (#1329)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranlavanet authored Mar 24, 2024
1 parent 42eaa78 commit 91958bd
Show file tree
Hide file tree
Showing 30 changed files with 132 additions and 132 deletions.
4 changes: 2 additions & 2 deletions ecosystem/cache/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/lavanet/lava/protocol/lavaprotocol"
"github.com/lavanet/lava/protocol/parser"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/slices"
"github.com/lavanet/lava/utils/lavaslices"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
spectypes "github.com/lavanet/lava/x/spec/types"
emptypb "google.golang.org/protobuf/types/known/emptypb"
Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *RelayerCacheServer) GetRelay(ctx context.Context, relayCacheGet *pairin
waitGroup.Wait()
if err == nil { // in case we got a hit validate seen block of the reply.
// validate that the response seen block is larger or equal to our expectations.
if cacheReply.SeenBlock < slices.Min([]int64{relayCacheGet.SeenBlock, relayCacheGet.RequestedBlock}) { // TODO unitest this.
if cacheReply.SeenBlock < lavaslices.Min([]int64{relayCacheGet.SeenBlock, relayCacheGet.RequestedBlock}) { // TODO unitest this.
// Error, our reply seen block is not larger than our expectations, meaning we got an old response
// this can happen only in the case relayCacheGet.SeenBlock < relayCacheGet.RequestedBlock
// by setting the err variable we will get a cache miss, and the relay will continue to the node.
Expand Down
4 changes: 2 additions & 2 deletions ecosystem/cache/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os/signal"
"time"

"github.com/lavanet/lava/utils/slices"
"github.com/lavanet/lava/utils/lavaslices"

"github.com/dgraph-io/ristretto"
"github.com/improbable-eng/grpc-web/go/grpcweb"
Expand Down Expand Up @@ -115,7 +115,7 @@ func (cs *CacheServer) Serve(ctx context.Context,

func (cs *CacheServer) ExpirationForChain(averageBlockTimeForChain time.Duration) time.Duration {
eighthBlock := averageBlockTimeForChain / 8
return slices.Max([]time.Duration{eighthBlock, cs.ExpirationNonFinalized}) // return the maximum TTL between an eighth block and expiration
return lavaslices.Max([]time.Duration{eighthBlock, cs.ExpirationNonFinalized}) // return the maximum TTL between an eighth block and expiration
}

func Server(
Expand Down
6 changes: 3 additions & 3 deletions protocol/chaintracker/chain_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/lavanet/lava/protocol/lavasession"
"github.com/lavanet/lava/protocol/metrics"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/slices"
"github.com/lavanet/lava/utils/lavaslices"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
grpc "google.golang.org/grpc"
Expand Down Expand Up @@ -467,8 +467,8 @@ func (ct *ChainTracker) updatePollingTimeBasedOnBlockGap(pollingTime time.Durati
if blockGapsLen > PollingUpdateLength { // check we have enough samples
// smaller times give more resolution to indentify changes, and also make block arrival predictions more optimistic
// so we take a 0.33 percentile because we want to be on the safe side by have a smaller time than expected
percentileTime := slices.Percentile(ct.blockEventsGap, 0.33)
stability := slices.Stability(ct.blockEventsGap, percentileTime)
percentileTime := lavaslices.Percentile(ct.blockEventsGap, 0.33)
stability := lavaslices.Stability(ct.blockEventsGap, percentileTime)
if debug {
utils.LavaFormatDebug("block gaps", utils.Attribute{Key: "block gaps", Value: ct.blockEventsGap}, utils.Attribute{Key: "specID", Value: ct.endpoint.ChainID})
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/lavaprotocol/finalization_consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/lavanet/lava/protocol/chainlib"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/slices"
"github.com/lavanet/lava/utils/lavaslices"
conflicttypes "github.com/lavanet/lava/x/conflict/types"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
)
Expand Down Expand Up @@ -251,7 +251,7 @@ func (fc *FinalizationConsensus) ExpectedBlockHeight(chainParser chainlib.ChainP
data[i] = latestBlock
i++
}
return slices.Median(data)
return lavaslices.Median(data)
}
medianOfExpectedBlocks := median(mapExpectedBlockHeights)
providersMedianOfLatestBlock := medianOfExpectedBlocks + int64(blockDistanceForFinalizedData)
Expand Down
4 changes: 2 additions & 2 deletions protocol/lavaprotocol/response_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
btcSecp256k1 "github.com/btcsuite/btcd/btcec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/lavaslices"
"github.com/lavanet/lava/utils/sigs"
"github.com/lavanet/lava/utils/slices"
conflicttypes "github.com/lavanet/lava/x/conflict/types"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
spectypes "github.com/lavanet/lava/x/spec/types"
Expand Down Expand Up @@ -87,7 +87,7 @@ func VerifyFinalizationData(reply *pairingtypes.RelayReply, relayRequest *pairin
providerLatestBlock := reply.LatestBlock
seenBlock := relayRequest.RelayData.SeenBlock
requestBlock := relayRequest.RelayData.RequestBlock
if providerLatestBlock < slices.Min([]int64{seenBlock, requestBlock}) {
if providerLatestBlock < lavaslices.Min([]int64{seenBlock, requestBlock}) {
return nil, nil, utils.LavaFormatError("provider response does not meet consistency requirements", ProviderFinzalizationDataError, utils.LogAttr("ProviderAddress", relayRequest.RelaySession.Provider), utils.LogAttr("providerLatestBlock", providerLatestBlock), utils.LogAttr("seenBlock", seenBlock), utils.LogAttr("requestBlock", requestBlock), utils.Attribute{Key: "provider address", Value: providerAddr})
}
return finalizedBlocks, finalizationConflict, errRet
Expand Down
4 changes: 2 additions & 2 deletions protocol/lavasession/provider_session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync/atomic"

"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/slices"
"github.com/lavanet/lava/utils/lavaslices"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
)

Expand Down Expand Up @@ -109,7 +109,7 @@ func getBadgeEpochDataFromProviderSessionWithConsumer(badgeUser string, provider
func registerBadgeEpochDataToProviderSessionWithConsumer(badgeUser string, badgeCuAllocation uint64, providerSessionsWithConsumer *ProviderSessionsWithConsumerProject) *ProviderSessionsEpochData {
providerSessionsWithConsumer.Lock.Lock()
defer providerSessionsWithConsumer.Lock.Unlock()
providerSessionsWithConsumer.badgeEpochData[badgeUser] = &ProviderSessionsEpochData{MaxComputeUnits: slices.Min([]uint64{providerSessionsWithConsumer.epochData.MaxComputeUnits, badgeCuAllocation})}
providerSessionsWithConsumer.badgeEpochData[badgeUser] = &ProviderSessionsEpochData{MaxComputeUnits: lavaslices.Min([]uint64{providerSessionsWithConsumer.epochData.MaxComputeUnits, badgeCuAllocation})}
return providerSessionsWithConsumer.badgeEpochData[badgeUser]
}

Expand Down
8 changes: 4 additions & 4 deletions protocol/monitoring/health_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/lavanet/lava/protocol/common"
"github.com/lavanet/lava/utils/slices"
"github.com/lavanet/lava/utils/lavaslices"
spectypes "github.com/lavanet/lava/x/spec/types"
)

Expand Down Expand Up @@ -88,7 +88,7 @@ func (healthResults *HealthResults) updateLatestBlock(specId string, latestBlock
if !ok {
healthResults.LatestBlocks[specId] = latestBlock
} else {
healthResults.LatestBlocks[specId] = slices.Max([]int64{existing, latestBlock})
healthResults.LatestBlocks[specId] = lavaslices.Max([]int64{existing, latestBlock})
}
}

Expand Down Expand Up @@ -150,9 +150,9 @@ func (healthResults *HealthResults) SetProviderData(providerKey LavaEntity, late
if existing.Block == 0 {
existing.Block = latestData.Block
} else {
latestData.Block = slices.Min([]int64{existing.Block, latestData.Block})
latestData.Block = lavaslices.Min([]int64{existing.Block, latestData.Block})
}
latestData.Latency = slices.Max([]time.Duration{existing.Latency, latestData.Latency})
latestData.Latency = lavaslices.Max([]time.Duration{existing.Latency, latestData.Latency})
}
healthResults.ProviderData[providerKey] = latestData
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/provideroptimizer/provider_optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/dgraph-io/ristretto"
"github.com/lavanet/lava/protocol/common"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/lavaslices"
"github.com/lavanet/lava/utils/rand"
"github.com/lavanet/lava/utils/score"
"github.com/lavanet/lava/utils/slices"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
"gonum.org/v1/gonum/mathext"
)
Expand Down Expand Up @@ -188,7 +188,7 @@ func (po *ProviderOptimizer) calculateSyncLag(latestSync uint64, timeSync time.T
}
// lag on first block
timeLag := sampleTime.Sub(timeSync) // received the latest block at time X, this provider provided the entry at time Y, which is X-Y time after
firstBlockLag := slices.Min([]time.Duration{po.averageBlockTime, timeLag})
firstBlockLag := lavaslices.Min([]time.Duration{po.averageBlockTime, timeLag})
blocksGap := latestSync - providerBlock - 1 // latestSync > providerBlock
blocksGapTime := time.Duration(blocksGap) * po.averageBlockTime // the provider is behind by X blocks, so is expected to catch up in averageBlockTime * X
timeLag = firstBlockLag + blocksGapTime
Expand Down
8 changes: 4 additions & 4 deletions protocol/rpcprovider/rpcprovider_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"github.com/lavanet/lava/protocol/provideroptimizer"
"github.com/lavanet/lava/protocol/upgrade"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/lavaslices"
"github.com/lavanet/lava/utils/protocopy"
"github.com/lavanet/lava/utils/sigs"
"github.com/lavanet/lava/utils/slices"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
spectypes "github.com/lavanet/lava/x/spec/types"
grpc "google.golang.org/grpc"
Expand Down Expand Up @@ -824,7 +824,7 @@ func (rpcps *RPCProviderServer) TryRelay(ctx context.Context, request *pairingty
} // else: we updated the chain message to request the specific latestBlock we fetched earlier, so use the previously fetched latest block and hashes
if proofBlock < modifiedReqBlock && proofBlock < request.RelayData.SeenBlock {
// we requested with a newer block, but don't necessarily have the finaliziation proof, chaintracker might be behind
proofBlock = slices.Min([]int64{modifiedReqBlock, request.RelayData.SeenBlock})
proofBlock = lavaslices.Min([]int64{modifiedReqBlock, request.RelayData.SeenBlock})

proofBlock, requestedHashes, err = rpcps.GetBlockDataForOptimisticFetch(ctx, relayTimeout, proofBlock, blockDistanceToFinalization, blocksInFinalizationData, averageBlockTime)
if err != nil {
Expand Down Expand Up @@ -864,7 +864,7 @@ func (rpcps *RPCProviderServer) GetBlockDataForOptimisticFetch(ctx context.Conte
}
timeSlept := 0 * time.Millisecond
refreshTime := (averageBlockTime / chaintracker.MostFrequentPollingMultiplier) / 2
sleepTime := slices.Min([]time.Duration{10 * refreshTime, timeCanWait, relayBaseTimeout / 2})
sleepTime := lavaslices.Min([]time.Duration{10 * refreshTime, timeCanWait, relayBaseTimeout / 2})
sleepContext, cancel := context.WithTimeout(context.Background(), sleepTime)
fetchedWithoutError := func() bool {
timeSlept += refreshTime
Expand Down Expand Up @@ -976,7 +976,7 @@ func (rpcps *RPCProviderServer) SleepUntilTimeOrConditionReached(ctx context.Con
var sleeping time.Duration
deadline, ok := ctx.Deadline()
if ok {
sleeping = slices.Min([]time.Duration{queryTime, time.Until(deadline) / 4})
sleeping = lavaslices.Min([]time.Duration{queryTime, time.Until(deadline) / 4})
} else {
sleeping = queryTime
}
Expand Down
8 changes: 4 additions & 4 deletions testutil/common/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
testkeeper "github.com/lavanet/lava/testutil/keeper"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/lavaslices"
"github.com/lavanet/lava/utils/sigs"
"github.com/lavanet/lava/utils/slices"
dualstakingante "github.com/lavanet/lava/x/dualstaking/ante"
dualstakingtypes "github.com/lavanet/lava/x/dualstaking/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
Expand Down Expand Up @@ -297,7 +297,7 @@ func NewCoin(tokenDenom string, amount int64) sdk.Coin {
}

func NewCoins(tokenDenom string, amount ...int64) []sdk.Coin {
return slices.Map(amount, func(a int64) sdk.Coin { return NewCoin(tokenDenom, a) })
return lavaslices.Map(amount, func(a int64) sdk.Coin { return NewCoin(tokenDenom, a) })
}

// keeper helpers
Expand Down Expand Up @@ -648,7 +648,7 @@ func (ts *Tester) TxPairingRelayPayment(addr string, rs ...*pairingtypes.RelaySe
func (ts *Tester) TxPairingFreezeProvider(addr, chainID string) (*pairingtypes.MsgFreezeProviderResponse, error) {
msg := &pairingtypes.MsgFreezeProvider{
Creator: addr,
ChainIds: slices.Slice(chainID),
ChainIds: lavaslices.Slice(chainID),
Reason: "test",
}
return ts.Servers.PairingServer.FreezeProvider(ts.GoCtx, msg)
Expand All @@ -658,7 +658,7 @@ func (ts *Tester) TxPairingFreezeProvider(addr, chainID string) (*pairingtypes.M
func (ts *Tester) TxPairingUnfreezeProvider(addr, chainID string) (*pairingtypes.MsgUnfreezeProviderResponse, error) {
msg := &pairingtypes.MsgUnfreezeProvider{
Creator: addr,
ChainIds: slices.Slice(chainID),
ChainIds: lavaslices.Slice(chainID),
}
return ts.Servers.PairingServer.UnfreezeProvider(ts.GoCtx, msg)
}
Expand Down
2 changes: 1 addition & 1 deletion utils/slices/slices.go → utils/lavaslices/slices.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slices
package lavaslices

import (
"golang.org/x/exp/constraints"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slices
package lavaslices

import (
"math"
Expand Down
4 changes: 2 additions & 2 deletions x/conflict/keeper/msg_server_detection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/testutil/common"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/lavaslices"
"github.com/lavanet/lava/utils/sigs"
"github.com/lavanet/lava/utils/slices"
conflicttypes "github.com/lavanet/lava/x/conflict/types"
conflictconstruct "github.com/lavanet/lava/x/conflict/types/construct"
"github.com/lavanet/lava/x/pairing/types"
Expand Down Expand Up @@ -182,5 +182,5 @@ func TestFrozenProviderDetection(t *testing.T) {
require.NotEqual(t, 0, len(votersList))

// the frozen provider should not be part of the voters list
require.False(t, slices.Contains(votersList, frozenProvider))
require.False(t, lavaslices.Contains(votersList, frozenProvider))
}
2 changes: 1 addition & 1 deletion x/dualstaking/keeper/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/lavanet/lava/utils"
lavaslices "github.com/lavanet/lava/utils/slices"
lavaslices "github.com/lavanet/lava/utils/lavaslices"
"github.com/lavanet/lava/x/dualstaking/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
"golang.org/x/exp/slices"
Expand Down
4 changes: 2 additions & 2 deletions x/dualstaking/keeper/delegator_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/slices"
"github.com/lavanet/lava/utils/lavaslices"
"github.com/lavanet/lava/x/dualstaking/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
spectypes "github.com/lavanet/lava/x/spec/types"
Expand Down Expand Up @@ -207,7 +207,7 @@ func (k Keeper) RewardProvidersAndDelegators(ctx sdk.Context, providerAddr sdk.A
}
}

relevantDelegations := slices.Filter(delegations,
relevantDelegations := lavaslices.Filter(delegations,
func(d types.Delegation) bool {
return d.ChainID == chainID && d.IsFirstMonthPassed(ctx.BlockTime().UTC().Unix()) && d.Delegator != d.Provider
})
Expand Down
8 changes: 4 additions & 4 deletions x/dualstaking/types/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/slices"
"github.com/lavanet/lava/utils/lavaslices"
)

func NewDelegation(delegator, provider, chainID string, blockTime time.Time, tokenDenom string) Delegation {
Expand Down Expand Up @@ -51,14 +51,14 @@ func NewDelegator(delegator, provider string) Delegator {
}

func (delegator *Delegator) AddProvider(provider string) {
if !slices.Contains(delegator.Providers, provider) {
if !lavaslices.Contains(delegator.Providers, provider) {
delegator.Providers = append(delegator.Providers, provider)
}
}

func (delegator *Delegator) DelProvider(provider string) {
if slices.Contains(delegator.Providers, provider) {
delegator.Providers, _ = slices.Remove(delegator.Providers, provider)
if lavaslices.Contains(delegator.Providers, provider) {
delegator.Providers, _ = lavaslices.Remove(delegator.Providers, provider)
}
}

Expand Down
Loading

0 comments on commit 91958bd

Please sign in to comment.