Skip to content

Commit

Permalink
Merge pull request #104 from mysteriumnetwork/remove-v3-pricer
Browse files Browse the repository at this point in the history
Remove v3 pricer
  • Loading branch information
tzununbekov authored Mar 4, 2024
2 parents a56b68e + 1c4eb99 commit 44f4cd6
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 1,340 deletions.
9 changes: 1 addition & 8 deletions cmd/pricer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
_ "github.com/mysteriumnetwork/discovery/docs"
"github.com/mysteriumnetwork/discovery/middleware"
"github.com/mysteriumnetwork/discovery/price"
"github.com/mysteriumnetwork/discovery/price/pricing"
"github.com/mysteriumnetwork/discovery/price/pricingbyservice"
"github.com/mysteriumnetwork/go-rest/apierror"
mlog "github.com/mysteriumnetwork/logger"
Expand Down Expand Up @@ -58,10 +57,9 @@ func main() {
log.Fatal().Err(err).Msg("could not reach redis")
}

v3 := r.Group("/api/v3")
v4 := r.Group("/api/v4")

cfger := pricing.NewConfigProviderDB(rdb)
cfger := pricingbyservice.NewConfigProviderDB(rdb)

_, err = cfger.Get()
if err != nil {
Expand All @@ -74,17 +72,12 @@ func main() {
log.Fatal().Err(err).Msg("Failed to load cfg by service")
}

getter, err := pricing.NewPriceGetter(rdb)
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize price getter")
}
getterByService, err := pricingbyservice.NewPriceGetter(rdb)
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize price getter by service")
}

ac := middleware.NewJWTChecker(cfg.SentinelURL, cfg.UniverseJWTSecret)
price.NewAPI(getter, cfger, ac).RegisterRoutes(v3)
price.NewAPIByService(rdb, getterByService, cfgerByService, ac).RegisterRoutes(v4)

if err := r.Run(); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions e2e/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/dghubble/sling"

"github.com/mysteriumnetwork/discovery/health"
"github.com/mysteriumnetwork/discovery/price/pricing"
"github.com/mysteriumnetwork/discovery/price/pricingbyservice"
v3 "github.com/mysteriumnetwork/discovery/proposal/v3"
)

Expand Down Expand Up @@ -62,21 +62,21 @@ type pricerAPI struct {
basePath string
}

func (a *pricerAPI) LatestPrices() (latestPrices pricing.LatestPrices, err error) {
_, err = sling.New().Base(a.basePath).Get("/api/v3/prices").Receive(&latestPrices, nil)
func (a *pricerAPI) LatestPrices() (latestPrices pricingbyservice.LatestPrices, err error) {
_, err = sling.New().Base(a.basePath).Get("/api/v4/prices").Receive(&latestPrices, nil)
return latestPrices, err
}

func (a *pricerAPI) GetPriceConfig(token string) (config pricing.Config, err error) {
resp, err := sling.New().Base(a.basePath).Add("Authorization", "Bearer "+token).Get("/api/v3/prices/config").Receive(&config, nil)
func (a *pricerAPI) GetPriceConfig(token string) (config pricingbyservice.Config, err error) {
resp, err := sling.New().Base(a.basePath).Add("Authorization", "Bearer "+token).Get("/api/v4/prices/config").Receive(&config, nil)
if resp.StatusCode == 401 {
return config, errors.New(fmt.Sprint(resp.StatusCode))
}
return config, err
}

func (a *pricerAPI) UpdatePriceConfig(token string, cfg pricing.Config) (err error) {
resp, err := sling.New().Base(a.basePath).Add("Authorization", "Bearer "+token).BodyJSON(&cfg).Post("/api/v3/prices/config").Receive(nil, nil)
func (a *pricerAPI) UpdatePriceConfig(token string, cfg pricingbyservice.Config) (err error) {
resp, err := sling.New().Base(a.basePath).Add("Authorization", "Bearer "+token).BodyJSON(&cfg).Post("/api/v4/prices/config").Receive(nil, nil)
if resp.StatusCode == 401 || resp.StatusCode == 400 {
return errors.New(fmt.Sprint(resp.StatusCode))
}
Expand Down
87 changes: 70 additions & 17 deletions e2e/price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"

"github.com/golang-jwt/jwt/v4"
"github.com/mysteriumnetwork/discovery/price/pricing"
"github.com/mysteriumnetwork/discovery/price/pricingbyservice"
"github.com/stretchr/testify/assert"
)

Expand All @@ -31,14 +31,39 @@ func TestMain(m *testing.M) {
func Test_LatestPrices(t *testing.T) {
prices, err := PricerAPI.LatestPrices()
assert.NoError(t, err)
assert.NotNil(t, prices.Defaults.Current.Residential.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Residential.PricePerHour)
assert.NotNil(t, prices.Defaults.Current.Other.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Other.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Residential.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Residential.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Other.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Other.PricePerHour)
assert.NotNil(t, prices.Defaults.Current.Residential.DVPN.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Residential.DVPN.PricePerHour)
assert.NotNil(t, prices.Defaults.Current.Residential.Scraping.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Residential.Scraping.PricePerHour)
assert.NotNil(t, prices.Defaults.Current.Residential.Wireguard.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Residential.Wireguard.PricePerHour)
assert.NotNil(t, prices.Defaults.Current.Residential.DataTransfer.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Residential.DataTransfer.PricePerHour)
assert.NotNil(t, prices.Defaults.Current.Other.DVPN.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Other.DVPN.PricePerHour)
assert.NotNil(t, prices.Defaults.Current.Other.Scraping.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Other.Scraping.PricePerHour)
assert.NotNil(t, prices.Defaults.Current.Other.Wireguard.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Other.Wireguard.PricePerHour)
assert.NotNil(t, prices.Defaults.Current.Other.DataTransfer.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Other.DataTransfer.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Residential.DVPN.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Residential.DVPN.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Residential.Scraping.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Residential.Scraping.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Residential.Wireguard.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Residential.Wireguard.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Residential.DataTransfer.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Residential.DataTransfer.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Other.DVPN.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Other.DVPN.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Other.Scraping.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Other.Scraping.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Other.Wireguard.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Other.Wireguard.PricePerHour)
assert.NotNil(t, prices.Defaults.Previous.Other.DataTransfer.PricePerGiB)
assert.NotNil(t, prices.Defaults.Previous.Other.DataTransfer.PricePerHour)

assert.Greater(t, prices.CurrentValidUntil.UnixNano(), time.Unix(0, 0).UnixNano())
assert.Greater(t, prices.PreviousValidUntil.UnixNano(), time.Unix(0, 0).UnixNano())
assert.Greater(t, prices.CurrentServerTime.UnixNano(), time.Unix(0, 0).UnixNano())
Expand All @@ -64,22 +89,22 @@ func Test_GetConfig(t *testing.T) {

func Test_PostConfig(t *testing.T) {
t.Run("rejected with bad token", func(t *testing.T) {
err := PricerAPI.UpdatePriceConfig("tkn", pricing.Config{})
err := PricerAPI.UpdatePriceConfig("tkn", pricingbyservice.Config{})
assert.Error(t, err)
assert.Equal(t, "401", err.Error())
})

t.Run("rejected with invalid config", func(t *testing.T) {
err := PricerAPI.UpdatePriceConfig(validToken, pricing.Config{})
err := PricerAPI.UpdatePriceConfig(validToken, pricingbyservice.Config{})
assert.Error(t, err)
assert.Equal(t, "400", err.Error())
})

t.Run("accepts with valid config", func(t *testing.T) {
toSend := pricing.Config{}
toSend := pricingbyservice.Config{}
err := json.Unmarshal([]byte(expectedPricingConfig), &toSend)
assert.NoError(t, err)
toSend.BasePrices.Other.PricePerGiB = 11
toSend.BasePrices.Other.Wireguard.PricePerGiB = 11

err = PricerAPI.UpdatePriceConfig(validToken, toSend)
assert.NoError(t, err)
Expand Down Expand Up @@ -110,12 +135,40 @@ var expectedPricingConfig = `
{
"base_prices": {
"residential": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
"data_transfer": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
},
"dvpn": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
},
"scraping": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
},
"wireguard": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
}
},
"other": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
"data_transfer": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
},
"dvpn": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
},
"scraping": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
},
"wireguard": {
"price_per_hour_usd": 0.00036,
"price_per_gib_usd": 0.06
}
}
},
"country_modifiers": {
Expand Down
94 changes: 0 additions & 94 deletions price/api.go

This file was deleted.

9 changes: 9 additions & 0 deletions price/api_by_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
"github.com/mysteriumnetwork/go-rest/apierror"
)

const (
errCodeParsingJson = "err_parsing_config"

errCodeNoConfig = "err_no_config"
errCodeUpdateConfig = "err_update_config"

errRedisPingFailed = "failed_redis_ping"
)

type APIByService struct {
pricer *pricingbyservice.PriceGetter
cfger pricingbyservice.ConfigProvider
Expand Down
Loading

0 comments on commit 44f4cd6

Please sign in to comment.