Skip to content

Commit

Permalink
refactor: deprecate 'pricePerGateway' broadcasters property in favor …
Browse files Browse the repository at this point in the history
…of 'gateways'

This commit updates the configuration to replace the `broadcasters` property
specified under the `pricePerGateway` flag with `gateways`. Additionally, it
ensures that a warning is issued when the deprecated property is still used.
  • Loading branch information
rickstaa committed May 16, 2024
1 parent f83dec4 commit 74fa79e
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions cmd/livepeer/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ func StartLivepeer(ctx context.Context, cfg LivepeerConfig) {
glog.Warning("-PricePerBroadcaster flag is deprecated and will be removed in a future release. Please use -PricePerGateway instead")
cfg.PricePerGateway = cfg.PricePerBroadcaster
}
broadcasterPrices := getBroadcasterPrices(*cfg.PricePerGateway)
broadcasterPrices := getGatewayPrices(*cfg.PricePerGateway)
for _, p := range broadcasterPrices {
p := p
pricePerPixel := new(big.Rat).Quo(p.PricePerUnit, p.PixelsPerUnit)
Expand Down Expand Up @@ -1496,44 +1496,57 @@ type GatewayPrice struct {
PixelsPerUnit *big.Rat
}

func getGatewayPrices(gatewayPrices string) []BroadcasterPrice {
func getGatewayPrices(gatewayPrices string) []GatewayPrice {
if gatewayPrices == "" {
return nil
}

// Format of gatewayPrices json
// Format of broadcasterPrices json
// {"gateways":[{"ethaddress":"address1","priceperunit":0.5,"currency":"USD","pixelsperunit":1}, {"ethaddress":"address2","priceperunit":0.3,"currency":"USD","pixelsperunit":3}]}
var pricesSet struct {
Gateways []struct {
EthAddress string `json:"ethaddress"`
PixelsPerUnit json.RawMessage `json:"pixelsperunit"`
PricePerUnit json.RawMessage `json:"priceperunit"`
Currency string `json:"currency"`
} `json:"gateways"`
// TODO: Keep the old name for backwards compatibility, remove in the future
Broadcasters []struct {
EthAddress string `json:"ethaddress"`
// The fields below are specified as a number in the JSON, but we don't want to lose precision so we store the raw characters here and parse as a big.Rat.
// This also allows support for exponential notation for numbers, which is helpful for pricePerUnit which could be a value like 1e12.
EthAddress string `json:"ethaddress"`
PixelsPerUnit json.RawMessage `json:"pixelsperunit"`
PricePerUnit json.RawMessage `json:"priceperunit"`
Currency string `json:"currency"`
} `json:"broadcasters"`
}
pricesFileContent, _ := common.ReadFromFile(broadcasterPrices)
pricesFileContent, _ := common.ReadFromFile(gatewayPrices)

err := json.Unmarshal([]byte(pricesFileContent), &pricesSet)
if err != nil {
glog.Errorf("broadcaster prices could not be parsed: %s", err)
glog.Errorf("gateway prices could not be parsed: %s", err)
return nil
}

prices := make([]BroadcasterPrice, len(pricesSet.Broadcasters))
for i, p := range pricesSet.Broadcasters {
// Check if broadcasters field is used and display a warning
if len(pricesSet.Broadcasters) > 0 {
glog.Warning("The 'broadcaster' property in the 'pricePerGateway' config is deprecated and will be removed in a future release. Please use 'gateways' instead.")
}

// Combine broadcasters and gateways into a single slice
allGateways := append(pricesSet.Broadcasters, pricesSet.Gateways...)

prices := make([]GatewayPrice, len(allGateways))
for i, p := range allGateways {
pixelsPerUnit, ok := new(big.Rat).SetString(string(p.PixelsPerUnit))
if !ok {
glog.Errorf("Pixels per unit could not be parsed for broadcaster %v. must be a valid number, provided %s", p.EthAddress, p.PixelsPerUnit)
glog.Errorf("Pixels per unit could not be parsed for gateway %v. must be a valid number, provided %s", p.EthAddress, p.PixelsPerUnit)
continue
}
pricePerUnit, ok := new(big.Rat).SetString(string(p.PricePerUnit))
if !ok {
glog.Errorf("Price per unit could not be parsed for broadcaster %v. must be a valid number, provided %s", p.EthAddress, p.PricePerUnit)
glog.Errorf("Price per unit could not be parsed for gateway %v. must be a valid number, provided %s", p.EthAddress, p.PricePerUnit)
continue
}
prices[i] = BroadcasterPrice{
prices[i] = GatewayPrice{
EthAddress: p.EthAddress,
Currency: p.Currency,
PricePerUnit: pricePerUnit,
Expand Down

0 comments on commit 74fa79e

Please sign in to comment.