Skip to content

Commit

Permalink
fix: Embed allocated resources into request
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hare committed May 9, 2024
1 parent 49286f8 commit e680fa2
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 129 deletions.
9 changes: 5 additions & 4 deletions bidengine/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,12 @@ loop:
pricech = runner.Do(metricsutils.ObserveRunner(func() runner.Result {
// Calculate price & bid
priceReq := Request{
Owner: group.GroupID.Owner,
GSpec: &group.GroupSpec,
PricePrecision: DefaultPricePrecision,
Owner: group.GroupID.Owner,
GSpec: &group.GroupSpec,
PricePrecision: DefaultPricePrecision,
AllocatedResources: reservation.GetAllocatedResources(),
}
return runner.NewResult(o.cfg.PricingStrategy.CalculatePrice(ctx, priceReq, reservation))
return runner.NewResult(o.cfg.PricingStrategy.CalculatePrice(ctx, priceReq))
}, pricingDuration))
case result := <-pricech:
pricech = nil
Expand Down
13 changes: 5 additions & 8 deletions bidengine/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/akash-network/node/testutil"

clustermocks "github.com/akash-network/provider/cluster/mocks"
ctypes "github.com/akash-network/provider/cluster/types/v1beta3"
clmocks "github.com/akash-network/provider/cluster/types/v1beta3/mocks"
"github.com/akash-network/provider/session"
)
Expand All @@ -57,10 +56,8 @@ type alwaysFailsBidPricingStrategy struct {
failure error
}

var (
_ BidPricingStrategy = (*testBidPricingStrategy)(nil)
_ BidPricingStrategy = (*alwaysFailsBidPricingStrategy)(nil)
)
var _ BidPricingStrategy = (*testBidPricingStrategy)(nil)
var _ BidPricingStrategy = (*alwaysFailsBidPricingStrategy)(nil)

func makeMocks(s *orderTestScaffold) {
groupResult := &dtypes.QueryGroupResponse{}
Expand Down Expand Up @@ -320,6 +317,7 @@ func Test_BidOrderPriceTooHigh(t *testing.T) {

// Should have called unreserve once, nothing happened after the bid
scaffold.cluster.AssertCalled(t, "Unreserve", scaffold.orderID, mock.Anything)

}

func Test_BidOrderAndThenClosedUnreserve(t *testing.T) {
Expand Down Expand Up @@ -579,7 +577,6 @@ func Test_ShouldExitWhenAlreadySetAndLost(t *testing.T) {

scaffold.txClient.AssertNotCalled(t, "Broadcast", mock.Anything, expMsgs, mock.Anything)
}

func Test_ShouldCloseBidWhenAlreadySetAndThenTimeout(t *testing.T) {
pricing, err := MakeRandomRangePricing()
require.NoError(t, err)
Expand Down Expand Up @@ -648,7 +645,7 @@ func Test_ShouldRecognizeLeaseCreatedIfBiddingIsSkipped(t *testing.T) {
require.Nil(t, broadcast)
}

func (tbps testBidPricingStrategy) CalculatePrice(_ context.Context, _ Request, _ ctypes.Reservation) (sdk.DecCoin, error) {
func (tbps testBidPricingStrategy) CalculatePrice(_ context.Context, _ Request) (sdk.DecCoin, error) {
return sdk.NewInt64DecCoin(testutil.CoinDenom, int64(tbps)), nil
}

Expand Down Expand Up @@ -677,7 +674,7 @@ func Test_BidOrderUsesBidPricingStrategy(t *testing.T) {
scaffold.cluster.AssertCalled(t, "Unreserve", scaffold.orderID, mock.Anything)
}

func (afbps alwaysFailsBidPricingStrategy) CalculatePrice(_ context.Context, _ Request, _ ctypes.Reservation) (sdk.DecCoin, error) {
func (afbps alwaysFailsBidPricingStrategy) CalculatePrice(_ context.Context, _ Request) (sdk.DecCoin, error) {
return sdk.DecCoin{}, afbps.failure
}

Expand Down
15 changes: 7 additions & 8 deletions bidengine/pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ import (
atypes "github.com/akash-network/akash-api/go/node/types/v1beta3"
"github.com/akash-network/node/sdl"

ctypes "github.com/akash-network/provider/cluster/types/v1beta3"

"github.com/akash-network/provider/cluster/util"
)

type Request struct {
Owner string `json:"owner"`
GSpec *dtypes.GroupSpec
PricePrecision int
Owner string `json:"owner"`
GSpec *dtypes.GroupSpec
AllocatedResources dtypes.ResourceUnits
PricePrecision int
}

const (
DefaultPricePrecision = 6
)

type BidPricingStrategy interface {
CalculatePrice(context.Context, Request, ctypes.Reservation) (sdk.DecCoin, error)
CalculatePrice(ctx context.Context, req Request) (sdk.DecCoin, error)
}

var (
Expand Down Expand Up @@ -130,7 +129,7 @@ func ceilBigRatToBigInt(v *big.Rat) *big.Int {
return result
}

func (fp scalePricing) CalculatePrice(_ context.Context, req Request, _ ctypes.Reservation) (sdk.DecCoin, error) {
func (fp scalePricing) CalculatePrice(_ context.Context, req Request) (sdk.DecCoin, error) {
// Use unlimited precision math here.
// Otherwise, a correctly crafted order could create a cost of '1' given
// a possible configuration
Expand Down Expand Up @@ -250,7 +249,7 @@ func MakeRandomRangePricing() (BidPricingStrategy, error) {
return randomRangePricing(0), nil
}

func (randomRangePricing) CalculatePrice(_ context.Context, req Request, _ ctypes.Reservation) (sdk.DecCoin, error) {
func (randomRangePricing) CalculatePrice(_ context.Context, req Request) (sdk.DecCoin, error) {
min, max := calculatePriceRange(req.GSpec)
if min.IsEqual(max) {
return max, nil
Expand Down
76 changes: 24 additions & 52 deletions bidengine/pricing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
"github.com/stretchr/testify/require"

dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
mtypes "github.com/akash-network/akash-api/go/node/market/v1beta4"
"github.com/akash-network/akash-api/go/node/types/unit"
atypes "github.com/akash-network/akash-api/go/node/types/v1beta3"
"github.com/akash-network/node/sdl"
"github.com/akash-network/node/testutil"

ctypes "github.com/akash-network/provider/cluster/types/v1beta3"
"github.com/akash-network/provider/cluster/util"
)

Expand Down Expand Up @@ -138,7 +136,7 @@ func Test_ScalePricingFailsOnOverflow(t *testing.T) {
GSpec: defaultGroupSpec(),
}

price, err := pricing.CalculatePrice(context.Background(), req, nil)
price, err := pricing.CalculatePrice(context.Background(), req)

require.Equal(t, sdk.DecCoin{}, price)
require.Equal(t, err, ErrBidQuantityInvalid)
Expand All @@ -160,7 +158,7 @@ func Test_ScalePricingOnCpu(t *testing.T) {
GSpec: gspec,
}

price, err := pricing.CalculatePrice(context.Background(), req, nil)
price, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)
require.NotNil(t, pricing)

Expand All @@ -184,7 +182,7 @@ func Test_ScalePricingOnMemory(t *testing.T) {
GSpec: gspec,
}

price, err := pricing.CalculatePrice(context.Background(), req, nil)
price, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)

expectedPrice := testutil.AkashDecCoin(t, int64(memoryScale*memoryQuantity))
Expand All @@ -206,7 +204,7 @@ func Test_ScalePricingOnMemoryLessThanOne(t *testing.T) {
Owner: testutil.AccAddress(t).String(),
GSpec: gspec,
}
price, err := pricing.CalculatePrice(context.Background(), req, nil)
price, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)

expectedPrice, err := sdk.NewDecFromStr("0.0000009536743164")
Expand All @@ -216,7 +214,7 @@ func Test_ScalePricingOnMemoryLessThanOne(t *testing.T) {
// Make a resource exactly 1 less byte less than two megabytes
memoryQuantity = uint64(2*unit.Mi - 1)
gspec.Resources[0].Resources.Memory.Quantity = atypes.NewResourceValue(memoryQuantity)
price, err = pricing.CalculatePrice(context.Background(), req, nil)
price, err = pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)
require.NotNil(t, price)

Expand Down Expand Up @@ -256,7 +254,7 @@ func Test_ScalePricingOnStorage(t *testing.T) {
Owner: testutil.AccAddress(t).String(),
GSpec: gspec,
}
price, err := pricing.CalculatePrice(context.Background(), req, nil)
price, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)

decNearly(t, price.Amount, int64(storageScale*storageQuantity))
Expand All @@ -279,14 +277,14 @@ func Test_ScalePricingByCountOfResources(t *testing.T) {
Owner: testutil.AccAddress(t).String(),
GSpec: gspec,
}
firstPrice, err := pricing.CalculatePrice(context.Background(), req, nil)
firstPrice, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)

require.NoError(t, err)
decNearly(t, firstPrice.Amount, int64(storageScale*storageQuantity))

gspec.Resources[0].Count = 2
secondPrice, err := pricing.CalculatePrice(context.Background(), req, nil)
secondPrice, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)
decNearly(t, secondPrice.Amount, 2*int64(storageScale*storageQuantity))
}
Expand All @@ -312,7 +310,7 @@ func Test_ScalePricingForIPs(t *testing.T) {
Owner: testutil.AccAddress(t).String(),
GSpec: gspec,
}
price, err := pricing.CalculatePrice(context.Background(), req, nil)
price, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)

require.NoError(t, err)
Expand All @@ -323,14 +321,14 @@ func Test_ScalePricingForIPs(t *testing.T) {
SequenceNumber: 1368,
})
require.Equal(t, uint(2), util.GetEndpointQuantityOfResourceGroup(gspec, atypes.Endpoint_LEASED_IP))
price, err = pricing.CalculatePrice(context.Background(), req, nil)
price, err = pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)

require.NoError(t, err)
decNearly(t, price.Amount, 2*ipPriceInt)

gspec.Resources[0].Count = 33 // any number greater than 1 works here
price, err = pricing.CalculatePrice(context.Background(), req, nil)
price, err = pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)
decNearly(t, price.Amount, 2*ipPriceInt)
}
Expand Down Expand Up @@ -367,7 +365,7 @@ func Test_ScriptPricingFailsWhenScriptDoesNotExist(t *testing.T) {
Owner: testutil.AccAddress(t).String(),
GSpec: defaultGroupSpec(),
}
_, err = pricing.CalculatePrice(context.Background(), req, nil)
_, err = pricing.CalculatePrice(context.Background(), req)
require.IsType(t, &os.PathError{}, errors.Unwrap(err))
}

Expand All @@ -391,7 +389,7 @@ func Test_ScriptPricingFailsWhenScriptExitsNonZero(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, pricing)

_, err = pricing.CalculatePrice(context.Background(), req, nil)
_, err = pricing.CalculatePrice(context.Background(), req)
require.IsType(t, &exec.ExitError{}, errors.Unwrap(err))
}

Expand All @@ -415,7 +413,7 @@ func Test_ScriptPricingFailsWhenScriptExitsWithoutWritingResultToStdout(t *testi
GSpec: defaultGroupSpec(),
}

_, err = pricing.CalculatePrice(context.Background(), req, nil)
_, err = pricing.CalculatePrice(context.Background(), req)
require.ErrorIs(t, err, io.EOF)
}

Expand All @@ -439,7 +437,7 @@ func Test_ScriptPricingFailsWhenScriptWritesZeroResult(t *testing.T) {
GSpec: defaultGroupSpec(),
}

_, err = pricing.CalculatePrice(context.Background(), req, nil)
_, err = pricing.CalculatePrice(context.Background(), req)
require.Equal(t, ErrBidZero, err)
}

Expand All @@ -463,7 +461,7 @@ func Test_ScriptPricingFailsWhenScriptWritesNegativeResult(t *testing.T) {
GSpec: defaultGroupSpec(),
}

_, err = pricing.CalculatePrice(context.Background(), req, nil)
_, err = pricing.CalculatePrice(context.Background(), req)
require.Equal(t, ErrBidQuantityInvalid, err)
}

Expand All @@ -487,7 +485,7 @@ func Test_ScriptPricingWhenScriptWritesFractionalResult(t *testing.T) {
GSpec: defaultGroupSpec(),
}

result, err := pricing.CalculatePrice(context.Background(), req, nil)
result, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)
expectedPrice, err := sdk.NewDecFromStr("1.5")
require.NoError(t, err)
Expand Down Expand Up @@ -515,7 +513,7 @@ func Test_ScriptPricingFailsWhenScriptWritesOverflowResult(t *testing.T) {
GSpec: defaultGroupSpec(),
}

_, err = pricing.CalculatePrice(context.Background(), req, nil)
_, err = pricing.CalculatePrice(context.Background(), req)
require.ErrorIs(t, err, ErrBidQuantityInvalid)
}

Expand All @@ -540,7 +538,7 @@ func Test_ScriptPricingReturnsResultFromScript(t *testing.T) {
GSpec: defaultGroupSpec(),
}

price, err := pricing.CalculatePrice(context.Background(), req, nil)
price, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)
require.Equal(t, "uakt", price.Denom)
require.Equal(t, sdk.NewDec(132), price.Amount)
Expand Down Expand Up @@ -569,7 +567,7 @@ func Test_ScriptPricingDoesNotExhaustSemaphore(t *testing.T) {
GSpec: defaultGroupSpec(),
}

_, err = pricing.CalculatePrice(context.Background(), req, nil)
_, err = pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)
}
}
Expand All @@ -595,7 +593,7 @@ func Test_ScriptPricingStopsByContext(t *testing.T) {
Owner: testutil.AccAddress(t).String(),
GSpec: defaultGroupSpec(),
}
_, err = pricing.CalculatePrice(ctx, req, nil)
_, err = pricing.CalculatePrice(ctx, req)
require.Error(t, err)
require.Equal(t, context.Canceled, err)
}
Expand Down Expand Up @@ -626,7 +624,7 @@ func Test_ScriptPricingStopsByTimeout(t *testing.T) {
GSpec: defaultGroupSpec(),
}

_, err = pricing.CalculatePrice(ctx, req, nil)
_, err = pricing.CalculatePrice(ctx, req)
require.Error(t, err)
require.Equal(t, context.DeadlineExceeded, err)
}
Expand Down Expand Up @@ -654,7 +652,7 @@ func Test_ScriptPricingWritesJsonToStdin(t *testing.T) {
GSpec: gspec,
}

price, err := pricing.CalculatePrice(context.Background(), req, nil)
price, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)
require.Equal(t, "uakt", price.Denom)
require.Equal(t, sdk.NewDec(1), price.Amount)
Expand Down Expand Up @@ -712,40 +710,14 @@ func Test_ScriptPricingFromScript(t *testing.T) {
GSpec: gspec,
}

price, err := pricing.CalculatePrice(context.Background(), req, nil)
price, err := pricing.CalculatePrice(context.Background(), req)
require.NoError(t, err)
amount, err := sdk.NewDecFromStr(expectedPrice)
require.NoError(t, err)

require.Equal(t, sdk.NewDecCoinFromDec("uakt", amount).String(), price.String())
}

var _ ctypes.Reservation = (*testReservation)(nil)

type testReservation struct {
ru dtypes.ResourceUnits
}

func (*testReservation) OrderID() mtypes.OrderID {
return mtypes.OrderID{}
}

func (*testReservation) Resources() dtypes.ResourceGroup {
return nil
}

func (r *testReservation) GetAllocatedResources() dtypes.ResourceUnits {
return r.ru
}

func (r *testReservation) SetAllocatedResources(ru dtypes.ResourceUnits) {
r.ru = ru
}

func (*testReservation) Allocated() bool { return false }
func (*testReservation) ClusterParams() any { return nil }
func (*testReservation) SetClusterParams(any) {}

func TestRationalToIntConversion(t *testing.T) {
x := ceilBigRatToBigInt(big.NewRat(0, 1))
require.Equal(t, big.NewInt(0), x)
Expand Down
Loading

0 comments on commit e680fa2

Please sign in to comment.