Skip to content

Commit

Permalink
fix: update provider storage
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Oct 28, 2024
1 parent 9fd0129 commit ec5dc35
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions sentinel/event_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (p Proxy) handleBondProviderEvent(result tmCoreTypes.ResultEvent) {
if !p.isMyPubKey(evt.Provider) {
return
}
providerConfig, err := p.ProviderConfigStore.Get(evt.Provider)
providerConfig, err := p.ProviderConfigStore.Get(evt.Provider, service.String())
if err != nil {
p.logger.Info("failed to get provider config, initializing new config", "error", err)
providerConfig = ProviderConfiguration{
Expand Down Expand Up @@ -381,7 +381,7 @@ func (p Proxy) handleModProviderEvent(result tmCoreTypes.ResultEvent) {
return
}

providerConfig, err := p.ProviderConfigStore.Get(evt.Provider)
providerConfig, err := p.ProviderConfigStore.Get(evt.Provider, service.String())
if err != nil {
p.logger.Error(fmt.Sprintf("failed to get provider %s", err))
return
Expand Down
10 changes: 5 additions & 5 deletions sentinel/provider_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ type ProviderConfiguration struct {
}

// GetProviderModOrBondConfig retrieves a ProviderConfiguration by its PubKey
func (ps *ProviderConfigurationStore) Get(pubKey common.PubKey) (ProviderConfiguration, error) {
data, err := ps.db.Get([]byte(pubKey.String()), nil)
func (ps *ProviderConfigurationStore) Get(pubKey common.PubKey, service string) (ProviderConfiguration, error) {
data, err := ps.db.Get([]byte(pubKey.String()+service), nil)
if err != nil {
return ProviderConfiguration{}, err
}
Expand All @@ -80,13 +80,13 @@ func (ps *ProviderConfigurationStore) Set(config ProviderConfiguration) error {
return err
}

err = ps.db.Put([]byte(config.PubKey.String()), data, nil)
err = ps.db.Put([]byte(config.PubKey.String()+config.Service.String()), data, nil)
if err != nil {
return err
}
return nil
}

func (p *ProviderConfigurationStore) Remove(key common.PubKey) error {
return p.db.Delete([]byte(key.String()), nil)
func (p *ProviderConfigurationStore) Remove(pubKey common.PubKey, service string) error {
return p.db.Delete([]byte(pubKey.String()+service), nil)
}
6 changes: 3 additions & 3 deletions sentinel/provider_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestProviderConfigurationStore_SetAndGet(t *testing.T) {
err = store.Set(config)
require.NoError(t, err, "expected no error when setting provider config")

retrievedConfig, err := store.Get(pubKey)
retrievedConfig, err := store.Get(pubKey, service.String())
require.NoError(t, err, "expected no error when getting provider config")
require.Equal(t, config, retrievedConfig, "expected the stored and retrieved configs to be the same")

Expand Down Expand Up @@ -85,10 +85,10 @@ func TestProviderConfigurationStore_Remove(t *testing.T) {
err = store.Set(config)
require.NoError(t, err, "expected no error when setting provider config")

err = store.Remove(pubKey)
err = store.Remove(pubKey, service.String())
require.NoError(t, err, "expected no error when removing provider config")

_, err = store.Get(pubKey)
_, err = store.Get(pubKey, service.String())
require.Error(t, err, "expected error when getting non-existent provider config")

err = store.db.Close()
Expand Down
2 changes: 1 addition & 1 deletion sentinel/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ const (
RoutesClaim = "/claim/{id}"
RoutesOpenClaims = "/open-claims"
RouteManage = "/manage/contract/{id}"
RouteProviderData = "/provider"
RouteProviderData = "/provider/{service}"
)
9 changes: 8 additions & 1 deletion sentinel/sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,15 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {

func (p Proxy) handleProviderData(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Content-Type", "application/json")
vars := mux.Vars(r)
serviceString, ok := vars["service"]
if !ok {
respondWithError(w, "missing service in uri", http.StatusBadRequest)
return
}
service := common.Service(common.ServiceLookup[serviceString])

providerConfigData, err := p.ProviderConfigStore.Get(p.Config.ProviderPubKey)
providerConfigData, err := p.ProviderConfigStore.Get(p.Config.ProviderPubKey, service.String())
if err != nil {
p.logger.Error("failed to get provider details", "error", err, "provider", p.Config.ProviderPubKey)
respondWithError(w, fmt.Sprintf("Invalid Provider: %s", err), http.StatusBadRequest)
Expand Down

0 comments on commit ec5dc35

Please sign in to comment.