Skip to content

Commit

Permalink
fix(market): save resources offer to the bid
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <troian.ap@gmail.com>
  • Loading branch information
troian committed Nov 17, 2023
1 parent 00b9406 commit f2dd42f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 17 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/akash-network/node
go 1.21

require (
github.com/akash-network/akash-api v0.0.32
github.com/akash-network/akash-api v0.0.33
github.com/blang/semver/v4 v4.0.0
github.com/boz/go-lifecycle v0.1.1
github.com/cosmos/cosmos-sdk v0.45.16
Expand Down Expand Up @@ -37,6 +37,7 @@ require (
)

retract (
v0.28.1
v0.28.0
v0.26.0
v0.22.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBA
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/akash-network/akash-api v0.0.32 h1:YkvFWUyyK6WKsERAcWCxWoIDXFQLLioTZ5iVI6Bvbv4=
github.com/akash-network/akash-api v0.0.32/go.mod h1:pW5NnJNhxynCOUGEcgxa338GSU2qzMkpn3MMYcAY6O4=
github.com/akash-network/akash-api v0.0.33 h1:SvOht1F3BDz3el8XKC7lbRzc6D++kERs5wFRisMWzCY=
github.com/akash-network/akash-api v0.0.33/go.mod h1:pW5NnJNhxynCOUGEcgxa338GSU2qzMkpn3MMYcAY6O4=
github.com/akash-network/cometbft v0.34.27-akash h1:V1dApDOr8Ee7BJzYyQ7Z9VBtrAul4+baMeA6C49dje0=
github.com/akash-network/cometbft v0.34.27-akash/go.mod h1:BcCbhKv7ieM0KEddnYXvQZR+pZykTKReJJYf7YC7qhw=
github.com/akash-network/ledger-go v0.14.3 h1:LCEFkTfgGA2xFMN2CtiKvXKE7dh0QSM77PJHCpSkaAo=
Expand Down
9 changes: 6 additions & 3 deletions x/market/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,9 @@ func TestCloseBidUnknownOrder(t *testing.T) {
orderID := types.MakeOrderID(group.ID(), 1)
provider := testutil.AccAddress(t)
price := sdk.NewDecCoin(testutil.CoinDenom, sdk.NewInt(int64(rand.Uint16())))
roffer := types.ResourceOfferFromRU(group.GroupSpec.Resources)

bid, err := suite.MarketKeeper().CreateBid(suite.Context(), orderID, provider, price)
bid, err := suite.MarketKeeper().CreateBid(suite.Context(), orderID, provider, price, roffer)
require.NoError(t, err)

suite.MarketKeeper().CreateLease(suite.Context(), bid)
Expand Down Expand Up @@ -418,10 +419,12 @@ func (st *testSuite) createLease() (types.LeaseID, types.Bid, types.Order) {

func (st *testSuite) createBid() (types.Bid, types.Order) {
st.t.Helper()
order, _ := st.createOrder(testutil.Resources(st.t))
order, gspec := st.createOrder(testutil.Resources(st.t))
provider := testutil.AccAddress(st.t)
price := sdk.NewDecCoin(testutil.CoinDenom, sdk.NewInt(int64(rand.Uint16())))
bid, err := st.MarketKeeper().CreateBid(st.Context(), order.ID(), provider, price)
roffer := types.ResourceOfferFromRU(gspec.Resources)

bid, err := st.MarketKeeper().CreateBid(st.Context(), order.ID(), provider, price, roffer)
require.NoError(st.t, err)
require.Equal(st.t, order.ID(), bid.ID().OrderID())
require.Equal(st.t, price, bid.Price)
Expand Down
6 changes: 5 additions & 1 deletion x/market/handler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (ms msgServer) CreateBid(goCtx context.Context, msg *types.MsgCreateBid) (*
return nil, types.ErrBidOverOrder
}

if !msg.ResourcesOffer.MatchGSpec(order.Spec) {
return nil, types.ErrCapabilitiesMismatch
}

provider, err := sdk.AccAddressFromBech32(msg.Provider)
if err != nil {
return nil, types.ErrEmptyProvider
Expand All @@ -84,7 +88,7 @@ func (ms msgServer) CreateBid(goCtx context.Context, msg *types.MsgCreateBid) (*
return nil, types.ErrCapabilitiesMismatch
}

bid, err := ms.keepers.Market.CreateBid(ctx, msg.Order, provider, msg.Price)
bid, err := ms.keepers.Market.CreateBid(ctx, msg.Order, provider, msg.Price, msg.ResourcesOffer)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions x/market/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type IKeeper interface {
Codec() codec.BinaryCodec
StoreKey() sdk.StoreKey
CreateOrder(ctx sdk.Context, gid dtypes.GroupID, spec dtypes.GroupSpec) (types.Order, error)
CreateBid(ctx sdk.Context, oid types.OrderID, provider sdk.AccAddress, price sdk.DecCoin) (types.Bid, error)
CreateBid(ctx sdk.Context, oid types.OrderID, provider sdk.AccAddress, price sdk.DecCoin, roffer types.ResourcesOffer) (types.Bid, error)
CreateLease(ctx sdk.Context, bid types.Bid)
OnOrderMatched(ctx sdk.Context, order types.Order)
OnBidMatched(ctx sdk.Context, bid types.Bid)
Expand Down Expand Up @@ -52,7 +52,6 @@ type Keeper struct {

// NewKeeper creates and returns an instance for Market keeper
func NewKeeper(cdc codec.BinaryCodec, skey sdk.StoreKey, pspace paramtypes.Subspace, ekeeper EscrowKeeper) IKeeper {

if !pspace.HasKeyTable() {
pspace = pspace.WithKeyTable(types.ParamKeyTable())
}
Expand Down Expand Up @@ -122,14 +121,15 @@ func (k Keeper) CreateOrder(ctx sdk.Context, gid dtypes.GroupID, spec dtypes.Gro
}

// CreateBid creates a bid for a order with given orderID, price for bid and provider
func (k Keeper) CreateBid(ctx sdk.Context, oid types.OrderID, provider sdk.AccAddress, price sdk.DecCoin) (types.Bid, error) {
func (k Keeper) CreateBid(ctx sdk.Context, oid types.OrderID, provider sdk.AccAddress, price sdk.DecCoin, roffer types.ResourcesOffer) (types.Bid, error) {
store := ctx.KVStore(k.skey)

bid := types.Bid{
BidID: types.MakeBidID(oid, provider),
State: types.BidOpen,
Price: price,
CreatedAt: ctx.BlockHeight(),
BidID: types.MakeBidID(oid, provider),
State: types.BidOpen,
Price: price,
CreatedAt: ctx.BlockHeight(),
ResourcesOffer: roffer,
}

key := keys.BidKey(bid.ID())
Expand Down
6 changes: 4 additions & 2 deletions x/market/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,12 @@ func createLease(t testing.TB, suite *state.TestSuite) types.LeaseID {
func createBid(t testing.TB, suite *state.TestSuite) (types.Bid, types.Order) {
t.Helper()
ctx := suite.Context()
order, _ := createOrder(t, suite.Context(), suite.MarketKeeper())
order, gspec := createOrder(t, suite.Context(), suite.MarketKeeper())
provider := testutil.AccAddress(t)
price := testutil.AkashDecCoinRandom(t)
bid, err := suite.MarketKeeper().CreateBid(ctx, order.ID(), provider, price)
roffer := types.ResourceOfferFromRU(gspec.Resources)

bid, err := suite.MarketKeeper().CreateBid(ctx, order.ID(), provider, price, roffer)
require.NoError(t, err)
assert.Equal(t, order.ID(), bid.ID().OrderID())
assert.Equal(t, price, bid.Price)
Expand Down
4 changes: 3 additions & 1 deletion x/provider/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"

mtypes "github.com/akash-network/akash-api/go/node/market/v1beta4"
types "github.com/akash-network/akash-api/go/node/provider/v1beta3"

akashtypes "github.com/akash-network/akash-api/go/node/types/v1beta3"
Expand Down Expand Up @@ -234,8 +235,9 @@ func TestProviderUpdateAttributes(t *testing.T) {
require.NoError(t, err)

price := testutil.DecCoin(t)
roffer := mtypes.ResourceOfferFromRU(group.GroupSpec.Resources)

bid, err := suite.mkeeper.CreateBid(suite.ctx, order.ID(), addr, price)
bid, err := suite.mkeeper.CreateBid(suite.ctx, order.ID(), addr, price, roffer)
require.NoError(t, err)

suite.mkeeper.CreateLease(suite.ctx, bid)
Expand Down

0 comments on commit f2dd42f

Please sign in to comment.