From 9563fd18244d37c3837aecd86a2adfd8d6c3df93 Mon Sep 17 00:00:00 2001 From: Elad Gildnur Date: Tue, 22 Oct 2024 16:39:08 +0300 Subject: [PATCH] Numerus changes --- protocol/metrics/metrics_provider_manager.go | 44 ++++++-- protocol/rpcprovider/rpcprovider.go | 22 ++-- .../updaters/provider_freeze_jail_updater.go | 71 ++++++++++++ ... => provider_freeze_jail_updater_mocks.go} | 51 ++++++--- .../provider_freeze_jail_updater_test.go | 86 +++++++++++++++ .../updaters/provider_freeze_updater.go | 95 ---------------- .../updaters/provider_freeze_updater_test.go | 103 ------------------ utils/convert.go | 8 ++ 8 files changed, 241 insertions(+), 239 deletions(-) create mode 100644 protocol/statetracker/updaters/provider_freeze_jail_updater.go rename protocol/statetracker/updaters/{provider_freeze_updater_mocks.go => provider_freeze_jail_updater_mocks.go} (67%) create mode 100644 protocol/statetracker/updaters/provider_freeze_jail_updater_test.go delete mode 100644 protocol/statetracker/updaters/provider_freeze_updater.go delete mode 100644 protocol/statetracker/updaters/provider_freeze_updater_test.go create mode 100644 utils/convert.go diff --git a/protocol/metrics/metrics_provider_manager.go b/protocol/metrics/metrics_provider_manager.go index 83e295ae9b..1d986878d6 100644 --- a/protocol/metrics/metrics_provider_manager.go +++ b/protocol/metrics/metrics_provider_manager.go @@ -42,7 +42,8 @@ type ProviderMetricsManager struct { relaysMonitors map[string]*RelaysMonitor relaysMonitorsLock sync.RWMutex frozenStatusMetric *prometheus.GaugeVec - jailedStatusMetric *prometheus.GaugeVec + jailStatusMetric *prometheus.GaugeVec + jailedCountMetric *prometheus.GaugeVec } func NewProviderMetricsManager(networkAddress string) *ProviderMetricsManager { @@ -113,29 +114,38 @@ func NewProviderMetricsManager(networkAddress string) *ProviderMetricsManager { Name: "lava_provider_fetch_block_success", Help: "The total number of get specific block queries that succeeded by chainfetcher", }, []string{"spec"}) + virtualEpochMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Name: "virtual_epoch", Help: "The current virtual epoch measured", }, []string{"spec"}) + endpointsHealthChecksOkMetric := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "lava_provider_overall_health", Help: "At least one endpoint is healthy", }) endpointsHealthChecksOkMetric.Set(1) + frozenStatusMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Name: "lava_provider_frozen_status", - Help: "Frozen: 1, Healthy: 0", - }, []string{"chainID", "address"}) + Help: "Frozen: 1, Not Frozen: 0", + }, []string{"chainID"}) - jailedStatusMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "lava_provider_jailed_status", + jailStatusMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "lava_provider_jail_status", + Help: "Jailed: 1, Not Jailed: 0", + }, []string{"chainID"}) + + jailedCountMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "lava_provider_jailed_count", Help: "The amount of times the provider was jailed in the last 24 hours", - }, []string{"chainID", "address"}) + }, []string{"chainID"}) protocolVersionMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Name: "lava_provider_protocol_version", Help: "The current running lavap version for the process. major := version / 1000000, minor := (version / 1000) % 1000 patch := version % 1000", }, []string{"version"}) + // Register the metrics with the Prometheus registry. prometheus.MustRegister(totalCUServicedMetric) prometheus.MustRegister(totalCUPaidMetric) @@ -153,7 +163,8 @@ func NewProviderMetricsManager(networkAddress string) *ProviderMetricsManager { prometheus.MustRegister(endpointsHealthChecksOkMetric) prometheus.MustRegister(protocolVersionMetric) prometheus.MustRegister(frozenStatusMetric) - prometheus.MustRegister(jailedStatusMetric) + prometheus.MustRegister(jailStatusMetric) + prometheus.MustRegister(jailedCountMetric) providerMetricsManager := &ProviderMetricsManager{ providerMetrics: map[string]*ProviderMetrics{}, @@ -175,7 +186,8 @@ func NewProviderMetricsManager(networkAddress string) *ProviderMetricsManager { protocolVersionMetric: protocolVersionMetric, relaysMonitors: map[string]*RelaysMonitor{}, frozenStatusMetric: frozenStatusMetric, - jailedStatusMetric: jailedStatusMetric, + jailStatusMetric: jailStatusMetric, + jailedCountMetric: jailedCountMetric, } http.Handle("/metrics", promhttp.Handler()) @@ -366,18 +378,26 @@ func (pme *ProviderMetricsManager) RegisterRelaysMonitor(chainID, apiInterface s pme.relaysMonitors[chainID+apiInterface] = relaysMonitor } -func (pme *ProviderMetricsManager) SetFrozenStatus(frozenStatus float64, chain string, address string) { +func (pme *ProviderMetricsManager) SetFrozenStatus(chain string, frozen bool) { + if pme == nil { + return + } + + pme.frozenStatusMetric.WithLabelValues(chain).Set(utils.Btof(frozen)) +} + +func (pme *ProviderMetricsManager) SetJailStatus(chain string, jailed bool) { if pme == nil { return } - pme.frozenStatusMetric.WithLabelValues(chain, address).Set(frozenStatus) + pme.jailStatusMetric.WithLabelValues(chain).Set(utils.Btof(jailed)) } -func (pme *ProviderMetricsManager) SetJailedStatus(jailedCounter uint64, chain string, address string) { +func (pme *ProviderMetricsManager) SetJailedCount(chain string, jailedCount uint64) { if pme == nil { return } - pme.jailedStatusMetric.WithLabelValues(chain, address).Set(float64(jailedCounter)) + pme.jailedCountMetric.WithLabelValues(chain).Set(float64(jailedCount)) } diff --git a/protocol/rpcprovider/rpcprovider.go b/protocol/rpcprovider/rpcprovider.go index cb35e9f384..e92951a46c 100644 --- a/protocol/rpcprovider/rpcprovider.go +++ b/protocol/rpcprovider/rpcprovider.go @@ -188,8 +188,8 @@ func (rpcp *RPCProvider) Start(options *rpcProviderStartOptions) (err error) { rpcp.rewardServer = rewardserver.NewRewardServer(providerStateTracker, rpcp.providerMetricsManager, rewardDB, options.rewardStoragePath, options.rewardsSnapshotThreshold, options.rewardsSnapshotTimeoutSec, rpcp) rpcp.providerStateTracker.RegisterForEpochUpdates(ctx, rpcp.rewardServer) rpcp.providerStateTracker.RegisterPaymentUpdatableForPayments(ctx, rpcp.rewardServer) - rpcp.createAndRegisterFreezeUpdatersByOptions(options, ctx) } + keyName, err := sigs.GetKeyName(options.clientCtx) if err != nil { utils.LavaFormatFatal("failed getting key name from clientCtx", err) @@ -211,8 +211,13 @@ func (rpcp *RPCProvider) Start(options *rpcProviderStartOptions) (err error) { if err != nil { utils.LavaFormatFatal("failed unmarshaling public address", err, utils.Attribute{Key: "keyName", Value: keyName}, utils.Attribute{Key: "pubkey", Value: pubKey.Address()}) } + utils.LavaFormatInfo("RPCProvider pubkey: " + rpcp.addr.String()) + + rpcp.createAndRegisterFreezeUpdatersByOptions(ctx, options.clientCtx, rpcp.addr.String()) + utils.LavaFormatInfo("RPCProvider setting up endpoints", utils.Attribute{Key: "count", Value: strconv.Itoa(len(options.rpcProviderEndpoints))}) + blockMemorySize, err := rpcp.providerStateTracker.GetEpochSizeMultipliedByRecommendedEpochNumToCollectPayment(ctx) // get the number of blocks to keep in PSM. if err != nil { utils.LavaFormatFatal("Failed fetching GetEpochSizeMultipliedByRecommendedEpochNumToCollectPayment in RPCProvider Start", err) @@ -272,17 +277,10 @@ func (rpcp *RPCProvider) Start(options *rpcProviderStartOptions) (err error) { return nil } -func (rpcp *RPCProvider) createAndRegisterFreezeUpdatersByOptions(options *rpcProviderStartOptions, ctx context.Context) { - providerFreezeUpdaterByChainId := make(map[string]*updaters.ProviderFreezeUpdater) - for _, rpcProviderEndpoint := range options.rpcProviderEndpoints { - _, freezeUpdaterExists := providerFreezeUpdaterByChainId[rpcProviderEndpoint.ChainID] - if !freezeUpdaterExists { - stateQuery := updaters.NewProviderStateQuery(ctx, options.clientCtx) - freezeUpdater := updaters.NewProviderFreezeUpdater(stateQuery.PairingQueryClient, rpcProviderEndpoint.ChainID, options.clientCtx.FromAddress.String(), rpcp.providerMetricsManager) - rpcp.providerStateTracker.RegisterForEpochUpdates(ctx, freezeUpdater) - providerFreezeUpdaterByChainId[rpcProviderEndpoint.ChainID] = freezeUpdater - } - } +func (rpcp *RPCProvider) createAndRegisterFreezeUpdatersByOptions(ctx context.Context, clientCtx client.Context, publicAddress string) { + queryClient := pairingtypes.NewQueryClient(clientCtx) + freezeJailUpdater := updaters.NewProviderFreezeJailUpdater(queryClient, publicAddress, rpcp.providerMetricsManager) + rpcp.providerStateTracker.RegisterForEpochUpdates(ctx, freezeJailUpdater) } func getActiveEndpoints(rpcProviderEndpoints []*lavasession.RPCProviderEndpoint, disabledEndpointsList []*lavasession.RPCProviderEndpoint) []*lavasession.RPCProviderEndpoint { diff --git a/protocol/statetracker/updaters/provider_freeze_jail_updater.go b/protocol/statetracker/updaters/provider_freeze_jail_updater.go new file mode 100644 index 0000000000..69f24ad1ab --- /dev/null +++ b/protocol/statetracker/updaters/provider_freeze_jail_updater.go @@ -0,0 +1,71 @@ +package updaters + +import ( + "context" + "time" + + "github.com/lavanet/lava/v4/utils" + pairingtypes "github.com/lavanet/lava/v4/x/pairing/types" + "google.golang.org/grpc" +) + +const ( + CallbackKeyForFreezeUpdate = "freeze-update" +) + +type ProviderPairingStatusStateQueryInf interface { + Provider(ctx context.Context, in *pairingtypes.QueryProviderRequest, opts ...grpc.CallOption) (*pairingtypes.QueryProviderResponse, error) +} + +type ProviderMetricsManagerInf interface { + SetFrozenStatus(string, bool) + SetJailStatus(string, bool) + SetJailedCount(string, uint64) +} + +type FrozenStatus uint64 + +const ( + AVAILABLE FrozenStatus = iota + FROZEN +) + +type ProviderFreezeJailUpdater struct { + pairingQueryClient ProviderPairingStatusStateQueryInf + metricsManager ProviderMetricsManagerInf + publicAddress string +} + +func NewProviderFreezeJailUpdater( + pairingQueryClient ProviderPairingStatusStateQueryInf, + publicAddress string, + metricsManager ProviderMetricsManagerInf, +) *ProviderFreezeJailUpdater { + return &ProviderFreezeJailUpdater{ + pairingQueryClient: pairingQueryClient, + publicAddress: publicAddress, + metricsManager: metricsManager, + } +} + +func (pfu *ProviderFreezeJailUpdater) UpdateEpoch(epoch uint64) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + response, err := pfu.pairingQueryClient.Provider(ctx, &pairingtypes.QueryProviderRequest{Address: pfu.publicAddress}) + cancel() + + if err != nil { + utils.LavaFormatError("Failed querying pairing client for provider", err) + return + } + + for _, provider := range response.StakeEntries { + if provider.Address != pfu.publicAddress || !provider.IsAddressVaultOrProvider(provider.Address) { + // should never happen, but just in case + continue + } + + pfu.metricsManager.SetJailedCount(provider.Chain, provider.Jails) + pfu.metricsManager.SetJailStatus(provider.Chain, provider.IsJailed(time.Now().UTC().Unix())) + pfu.metricsManager.SetFrozenStatus(provider.Chain, provider.IsFrozen() || provider.StakeAppliedBlock > epoch) + } +} diff --git a/protocol/statetracker/updaters/provider_freeze_updater_mocks.go b/protocol/statetracker/updaters/provider_freeze_jail_updater_mocks.go similarity index 67% rename from protocol/statetracker/updaters/provider_freeze_updater_mocks.go rename to protocol/statetracker/updaters/provider_freeze_jail_updater_mocks.go index c012cb10a5..24b0738393 100644 --- a/protocol/statetracker/updaters/provider_freeze_updater_mocks.go +++ b/protocol/statetracker/updaters/provider_freeze_jail_updater_mocks.go @@ -1,15 +1,20 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: ./protocol/statetracker/updaters/provider_freeze_updater.go - -// Package mock_updaters is a generated GoMock package. +// Source: protocol/statetracker/updaters/provider_freeze_updater.go +// +// Generated by this command: +// +// mockgen -source=protocol/statetracker/updaters/provider_freeze_updater.go -destination protocol/statetracker/updaters/provider_freeze_updater_mocks.go -package updaters +// + +// Package updaters is a generated GoMock package. package updaters import ( context "context" reflect "reflect" - gomock "github.com/golang/mock/gomock" types "github.com/lavanet/lava/v4/x/pairing/types" + gomock "go.uber.org/mock/gomock" grpc "google.golang.org/grpc" ) @@ -39,7 +44,7 @@ func (m *MockProviderPairingStatusStateQueryInf) EXPECT() *MockProviderPairingSt // Provider mocks base method. func (m *MockProviderPairingStatusStateQueryInf) Provider(ctx context.Context, in *types.QueryProviderRequest, opts ...grpc.CallOption) (*types.QueryProviderResponse, error) { m.ctrl.T.Helper() - varargs := []interface{}{ctx, in} + varargs := []any{ctx, in} for _, a := range opts { varargs = append(varargs, a) } @@ -50,9 +55,9 @@ func (m *MockProviderPairingStatusStateQueryInf) Provider(ctx context.Context, i } // Provider indicates an expected call of Provider. -func (mr *MockProviderPairingStatusStateQueryInfMockRecorder) Provider(ctx, in interface{}, opts ...interface{}) *gomock.Call { +func (mr *MockProviderPairingStatusStateQueryInfMockRecorder) Provider(ctx, in any, opts ...any) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{ctx, in}, opts...) + varargs := append([]any{ctx, in}, opts...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Provider", reflect.TypeOf((*MockProviderPairingStatusStateQueryInf)(nil).Provider), varargs...) } @@ -80,25 +85,37 @@ func (m *MockProviderMetricsManagerInf) EXPECT() *MockProviderMetricsManagerInfM } // SetFrozenStatus mocks base method. -func (m *MockProviderMetricsManagerInf) SetFrozenStatus(arg0 float64, arg1, arg2 string) { +func (m *MockProviderMetricsManagerInf) SetFrozenStatus(arg0 string, arg1 bool) { m.ctrl.T.Helper() - m.ctrl.Call(m, "SetFrozenStatus", arg0, arg1, arg2) + m.ctrl.Call(m, "SetFrozenStatus", arg0, arg1) } // SetFrozenStatus indicates an expected call of SetFrozenStatus. -func (mr *MockProviderMetricsManagerInfMockRecorder) SetFrozenStatus(arg0, arg1, arg2 interface{}) *gomock.Call { +func (mr *MockProviderMetricsManagerInfMockRecorder) SetFrozenStatus(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetFrozenStatus", reflect.TypeOf((*MockProviderMetricsManagerInf)(nil).SetFrozenStatus), arg0, arg1) +} + +// SetJailStatus mocks base method. +func (m *MockProviderMetricsManagerInf) SetJailStatus(arg0 string, arg1 bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetJailStatus", arg0, arg1) +} + +// SetJailStatus indicates an expected call of SetJailStatus. +func (mr *MockProviderMetricsManagerInfMockRecorder) SetJailStatus(arg0, arg1 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetFrozenStatus", reflect.TypeOf((*MockProviderMetricsManagerInf)(nil).SetFrozenStatus), arg0, arg1, arg2) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetJailStatus", reflect.TypeOf((*MockProviderMetricsManagerInf)(nil).SetJailStatus), arg0, arg1) } -// SetJailedStatus mocks base method. -func (m *MockProviderMetricsManagerInf) SetJailedStatus(arg0 uint64, arg1, arg2 string) { +// SetJailedCount mocks base method. +func (m *MockProviderMetricsManagerInf) SetJailedCount(arg0 string, arg1 uint64) { m.ctrl.T.Helper() - m.ctrl.Call(m, "SetJailedStatus", arg0, arg1, arg2) + m.ctrl.Call(m, "SetJailedCount", arg0, arg1) } -// SetJailedStatus indicates an expected call of SetJailedStatus. -func (mr *MockProviderMetricsManagerInfMockRecorder) SetJailedStatus(arg0, arg1, arg2 interface{}) *gomock.Call { +// SetJailedCount indicates an expected call of SetJailedCount. +func (mr *MockProviderMetricsManagerInfMockRecorder) SetJailedCount(arg0, arg1 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetJailedStatus", reflect.TypeOf((*MockProviderMetricsManagerInf)(nil).SetJailedStatus), arg0, arg1, arg2) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetJailedCount", reflect.TypeOf((*MockProviderMetricsManagerInf)(nil).SetJailedCount), arg0, arg1) } diff --git a/protocol/statetracker/updaters/provider_freeze_jail_updater_test.go b/protocol/statetracker/updaters/provider_freeze_jail_updater_test.go new file mode 100644 index 0000000000..6d87d0b032 --- /dev/null +++ b/protocol/statetracker/updaters/provider_freeze_jail_updater_test.go @@ -0,0 +1,86 @@ +package updaters + +import ( + "testing" + "time" + + "github.com/lavanet/lava/v4/utils/rand" + epochstoragetypes "github.com/lavanet/lava/v4/x/epochstorage/types" + pairingtypes "github.com/lavanet/lava/v4/x/pairing/types" + gomock "go.uber.org/mock/gomock" +) + +func TestFreezeJailMetricsOnEpochUpdate(t *testing.T) { + rand.InitRandomSeed() + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + specID := "test-spec" + address := "initial1" + epoch := uint64(100) + stakeAppliedBlock := uint64(10) + + stakeEntryList := []epochstoragetypes.StakeEntry{ + { + Address: address, + Chain: specID, + Endpoints: []epochstoragetypes.Endpoint{ + { + IPPORT: "1234567", + Geolocation: 1, + Addons: []string{}, + ApiInterfaces: []string{"banana"}, + Extensions: []string{}, + }, + }, + StakeAppliedBlock: uint64(stakeAppliedBlock), + }, + } + + response := &pairingtypes.QueryProviderResponse{StakeEntries: stakeEntryList} + + stateQuery := NewMockProviderPairingStatusStateQueryInf(ctrl) + metricManager := NewMockProviderMetricsManagerInf(ctrl) + + freezeUpdater := NewProviderFreezeJailUpdater(stateQuery, address, metricManager) + + expectAndRun := func(stakeAppliedBlock, jailedCount uint64, frozen bool, jailed bool) { + stakeEntryList[0].StakeAppliedBlock = stakeAppliedBlock + stakeEntryList[0].Jails = jailedCount + if jailed { + stakeEntryList[0].JailEndTime = time.Now().Add(time.Hour).UTC().Unix() + } + response = &pairingtypes.QueryProviderResponse{StakeEntries: stakeEntryList} + stateQuery. + EXPECT(). + Provider(gomock.Any(), gomock.Any(), gomock.Any()). + Return(response, nil). + AnyTimes() + + metricManager. + EXPECT(). + SetJailStatus(specID, jailed). + Times(1) + + metricManager. + EXPECT(). + SetFrozenStatus(specID, frozen). + Times(1) + + metricManager. + EXPECT(). + SetJailedCount(specID, uint64(jailedCount)). + Times(1) + + freezeUpdater.UpdateEpoch(epoch) + } + + // Normal - no freeze, no jail + expectAndRun(stakeAppliedBlock, 0, false, false) + + // StakeAppliedBlock > epoch - frozen + expectAndRun(epoch+1, 0, true, false) + + // Jail status changed + jail count + expectAndRun(epoch-1, 1, false, true) +} diff --git a/protocol/statetracker/updaters/provider_freeze_updater.go b/protocol/statetracker/updaters/provider_freeze_updater.go deleted file mode 100644 index 61759bb830..0000000000 --- a/protocol/statetracker/updaters/provider_freeze_updater.go +++ /dev/null @@ -1,95 +0,0 @@ -package updaters - -import ( - "context" - "sync" - "time" - - "github.com/lavanet/lava/v4/utils" - "github.com/lavanet/lava/v4/x/pairing/types" - "google.golang.org/grpc" -) - -const ( - CallbackKeyForFreezeUpdate = "freeze-update" -) - -type ProviderPairingStatusStateQueryInf interface { - Provider(ctx context.Context, in *types.QueryProviderRequest, opts ...grpc.CallOption) (*types.QueryProviderResponse, error) -} - -type ProviderMetricsManagerInf interface { - SetFrozenStatus(float64, string, string) - SetJailedStatus(uint64, string, string) -} - -type FrozenStatus uint64 - -const ( - AVAILABLE FrozenStatus = iota - FROZEN -) - -type ProviderFreezeUpdater struct { - lock sync.RWMutex - latestEpoch uint64 - pairingQueryClient ProviderPairingStatusStateQueryInf - metricsManager ProviderMetricsManagerInf - chainId string - publicAddress string -} - -func NewProviderFreezeUpdater( - stateQuery ProviderPairingStatusStateQueryInf, - chainId string, - publicAddress string, - metricsManager ProviderMetricsManagerInf, -) *ProviderFreezeUpdater { - return &ProviderFreezeUpdater{ - pairingQueryClient: stateQuery, - chainId: chainId, - publicAddress: publicAddress, - metricsManager: metricsManager, - latestEpoch: 0, - } -} - -func (pfu *ProviderFreezeUpdater) UpdaterKey() string { - return CallbackKeyForSpecUpdate + pfu.chainId -} - -func (pfu *ProviderFreezeUpdater) UpdateEpoch(epoch uint64) { - pfu.lock.Lock() - defer pfu.lock.Unlock() - - if epoch <= pfu.latestEpoch { - return - } - pfu.latestEpoch = epoch - ctx := context.Background() - - response, err := pfu.pairingQueryClient.Provider(ctx, &types.QueryProviderRequest{ - ChainID: pfu.chainId, - Address: pfu.publicAddress, - }) - if err != nil { - utils.LavaFormatError("Failed querying pairing client for providers", err, utils.LogAttr("chainId", pfu.chainId)) - return - } - for _, provider := range response.StakeEntries { - if provider.Address != pfu.publicAddress || !provider.IsAddressVaultOrProvider(provider.Address) { - continue - } - - pfu.metricsManager.SetJailedStatus(provider.Jails, provider.Chain, provider.Address) - if provider.StakeAppliedBlock > epoch || provider.IsFrozen() || provider.IsJailed(time.Now().UTC().Unix()) { - pfu.setProviderFreezeMetric(FROZEN, provider.Chain, provider.Address) - continue - } - pfu.setProviderFreezeMetric(AVAILABLE, provider.Chain, provider.Address) - } -} - -func (pfu *ProviderFreezeUpdater) setProviderFreezeMetric(isFrozen FrozenStatus, chain string, address string) { - pfu.metricsManager.SetFrozenStatus(float64(isFrozen), chain, address) -} diff --git a/protocol/statetracker/updaters/provider_freeze_updater_test.go b/protocol/statetracker/updaters/provider_freeze_updater_test.go deleted file mode 100644 index ac7ca52f89..0000000000 --- a/protocol/statetracker/updaters/provider_freeze_updater_test.go +++ /dev/null @@ -1,103 +0,0 @@ -package updaters - -import ( - "testing" - - "github.com/golang/mock/gomock" - "github.com/lavanet/lava/v4/utils/rand" - epochstoragetypes "github.com/lavanet/lava/v4/x/epochstorage/types" - pairingtypes "github.com/lavanet/lava/v4/x/pairing/types" -) - -func testFreezeAndJailsMetricsOnEpochUpdate(t *testing.T, freezeStatus FrozenStatus, jailsAmount uint64) { - // setup expectations - rand.InitRandomSeed() - ctrlStateQuery := gomock.NewController(t) - ctrlMetrics := gomock.NewController(t) - - defer ctrlStateQuery.Finish() - defer ctrlMetrics.Finish() - specID := "test-spec" - address := "initial1" - mockEpoch := uint64(1) - stakeAppliedBlock := 0 - if freezeStatus == FROZEN { - stakeAppliedBlock = 1000 - } - - stakeEntryList := []epochstoragetypes.StakeEntry{ - { - Address: address, - Chain: specID, - Endpoints: []epochstoragetypes.Endpoint{ - { - IPPORT: "1234567", - Geolocation: 1, - Addons: []string{}, - ApiInterfaces: []string{"banana"}, - Extensions: []string{}, - }, - }, - StakeAppliedBlock: uint64(stakeAppliedBlock), - Jails: jailsAmount, - }, - } - response := &pairingtypes.QueryProviderResponse{StakeEntries: stakeEntryList} - - // Create a new mock objects - stateQuery := NewMockProviderPairingStatusStateQueryInf(ctrlStateQuery) - stateQuery.EXPECT().Provider(gomock.Any(), gomock.Any(), gomock.Any()).Return(response, nil).AnyTimes() - metricManager := NewMockProviderMetricsManagerInf(ctrlMetrics) - // set expect for correct metric calls - metricManager.EXPECT().SetFrozenStatus(float64(freezeStatus), specID, address).Return().AnyTimes() - metricManager.EXPECT().SetJailedStatus(jailsAmount, specID, address).Return().AnyTimes() - - // create and call freeze updater - pau := NewProviderFreezeUpdater(stateQuery, specID, address, metricManager) - pau.UpdateEpoch(mockEpoch) -} - -func TestFrozenAvailability(t *testing.T) { - testFreezeAndJailsMetricsOnEpochUpdate(t, FROZEN, 0) -} - -func TestFrozenAvailabilityWithJails(t *testing.T) { - testFreezeAndJailsMetricsOnEpochUpdate(t, FROZEN, 2) -} - -func TestNonFrozenAvailability(t *testing.T) { - testFreezeAndJailsMetricsOnEpochUpdate(t, AVAILABLE, 0) -} - -func TestStakeEntryReplyOfDifferentAddress(t *testing.T) { - // setup expectations - rand.InitRandomSeed() - ctrlStateQuery := gomock.NewController(t) - ctrlMetrics := gomock.NewController(t) - - defer ctrlStateQuery.Finish() - defer ctrlMetrics.Finish() - specID := "test-spec" - address := "initial1" - mockEpoch := uint64(1) - - stakeEntryList := []epochstoragetypes.StakeEntry{ - { - Address: address + "-test", - Chain: specID, - }, - } - response := &pairingtypes.QueryProviderResponse{StakeEntries: stakeEntryList} - - // Create a new mock objects - stateQuery := NewMockProviderPairingStatusStateQueryInf(ctrlStateQuery) - stateQuery.EXPECT().Provider(gomock.Any(), gomock.Any(), gomock.Any()).Return(response, nil).AnyTimes() - metricManager := NewMockProviderMetricsManagerInf(ctrlMetrics) - // set expect for correct metric calls - metricManager.EXPECT().SetFrozenStatus(gomock.Any(), gomock.Any(), gomock.Any()).MaxTimes(0) - metricManager.EXPECT().SetJailedStatus(gomock.Any(), gomock.Any(), gomock.Any()).MaxTimes(0) - - // create and call freeze updater - pau := NewProviderFreezeUpdater(stateQuery, specID, address, metricManager) - pau.UpdateEpoch(mockEpoch) -} diff --git a/utils/convert.go b/utils/convert.go new file mode 100644 index 0000000000..0a39cc7093 --- /dev/null +++ b/utils/convert.go @@ -0,0 +1,8 @@ +package utils + +func Btof(b bool) float64 { + if b { + return 1 + } + return 0 +}