Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: Allow Os price to increase up to 2x mid-session #2995

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#### Broadcaster

- [#2995](https://github.com/livepeer/go-livepeer/pull/2995) server: Allow Os price to increase up to 2x mid-session (@victorges)

#### Orchestrator

#### Transcoder
Expand Down
15 changes: 10 additions & 5 deletions server/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,10 @@ func TestGenPayment(t *testing.T) {
s.Sender = sender

// Test changing O price
s.InitialPrice = &net.PriceInfo{PricePerUnit: 1, PixelsPerUnit: 5}
s.InitialPrice = &net.PriceInfo{PricePerUnit: 1, PixelsPerUnit: 7}
payment, err = genPayment(context.TODO(), s, 1)
assert.Equal("", payment)
assert.Errorf(err, "Orchestrator price has changed, Orchestrator price: %v, Orchestrator initial price: %v", "1/3", "1/5")
assert.Errorf(err, "Orchestrator price has more than doubled, Orchestrator price: %v, Orchestrator initial price: %v", "1/3", "1/7")

s.InitialPrice = nil

Expand Down Expand Up @@ -694,10 +694,15 @@ func TestValidatePrice(t *testing.T) {
err = validatePrice(s)
assert.Nil(err)

// O Initial Price lower than O Price
s.InitialPrice = &net.PriceInfo{PricePerUnit: 1, PixelsPerUnit: 10}
// O Price higher but up to 2x Initial Price
s.InitialPrice = &net.PriceInfo{PricePerUnit: 1, PixelsPerUnit: 6}
err = validatePrice(s)
assert.ErrorContains(err, "price has changed")
assert.Nil(err)

// O Price higher than 2x Initial Price
s.InitialPrice = &net.PriceInfo{PricePerUnit: 1000, PixelsPerUnit: 6001}
err = validatePrice(s)
assert.ErrorContains(err, "price has more than doubled")

// O.PriceInfo is nil
s.OrchestratorInfo.PriceInfo = nil
Expand Down
18 changes: 15 additions & 3 deletions server/segment_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const segmentHeader = "Livepeer-Segment"

const pixelEstimateMultiplier = 1.02

// Maximum price change allowed in orchestrator pricing before the session is swapped.
var priceIncreaseThreshold = big.NewRat(2, 1)

var errSegEncoding = errors.New("ErrorSegEncoding")
var errSegSig = errors.New("ErrSegSig")
var errFormat = errors.New("unrecognized profile output format")
Expand Down Expand Up @@ -826,9 +829,18 @@ func validatePrice(sess *BroadcastSession) error {
return errors.New("missing orchestrator price")
}

iPrice, err := common.RatPriceInfo(sess.InitialPrice)
if err == nil && iPrice != nil && oPrice.Cmp(iPrice) == 1 {
return fmt.Errorf("Orchestrator price has changed, Orchestrator price: %v, Orchestrator initial price: %v", oPrice, iPrice)
initPrice, err := common.RatPriceInfo(sess.InitialPrice)
if err != nil {
glog.Warningf("Error parsing session initial price (%d / %d): %v",
sess.InitialPrice.PricePerUnit, sess.InitialPrice.PixelsPerUnit, err)
}
if initPrice != nil {
// Prices are dynamic if configured with a custom currency, so we need to allow some change during the session.
// TODO: Make sure prices stay the same during a session so we can make this logic more strict, disallowing any price changes.
maxIncreasedPrice := new(big.Rat).Mul(initPrice, priceIncreaseThreshold)
if oPrice.Cmp(maxIncreasedPrice) > 0 {
return fmt.Errorf("Orchestrator price has more than doubled, Orchestrator price: %v, Orchestrator initial price: %v", oPrice.RatString(), initPrice.RatString())
}
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions server/segment_rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1679,13 +1679,13 @@ func TestSubmitSegment_GenPaymentError_ValidatePriceError(t *testing.T) {
OrchestratorInfo: oinfo,
InitialPrice: &net.PriceInfo{
PricePerUnit: 1,
PixelsPerUnit: 5,
PixelsPerUnit: 7,
},
}

_, err := SubmitSegment(context.TODO(), s, &stream.HLSSegment{}, nil, 0, false, true)

assert.EqualError(t, err, fmt.Sprintf("Orchestrator price has changed, Orchestrator price: %v, Orchestrator initial price: %v", "1/3", "1/5"))
assert.EqualError(t, err, fmt.Sprintf("Orchestrator price has more than doubled, Orchestrator price: %v, Orchestrator initial price: %v", "1/3", "1/7"))
balance.AssertCalled(t, "Credit", existingCredit)
}

Expand Down
Loading