diff --git a/x/auction/app_test.go b/x/auction/app_test.go deleted file mode 100644 index 20106efb..00000000 --- a/x/auction/app_test.go +++ /dev/null @@ -1,171 +0,0 @@ -package auction - -import ( - "testing" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank" - "github.com/cosmos/cosmos-sdk/x/mock" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto" -) - -// TestApp contans several basic integration tests of creating an auction, placing a bid, and the auction closing. - -func TestApp_ForwardAuction(t *testing.T) { - // Setup - mapp, keeper, addresses, privKeys := setUpMockApp() - seller := addresses[0] - //sellerKey := privKeys[0] - buyer := addresses[1] - buyerKey := privKeys[1] - - // Create a block where an auction is started (lot: 20 t1, initialBid: 0 t2) - header := abci.Header{Height: mapp.LastBlockHeight() + 1} - mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) - ctx := mapp.BaseApp.NewContext(false, header) // make sure first arg is false, otherwise no db writes - keeper.StartForwardAuction(ctx, seller, sdk.NewInt64Coin("token1", 20), sdk.NewInt64Coin("token2", 0)) // lot, initialBid - mapp.EndBlock(abci.RequestEndBlock{}) - mapp.Commit() - - // Check seller's coins have decreased - mock.CheckBalance(t, mapp, seller, sdk.NewCoins(sdk.NewInt64Coin("token1", 80), sdk.NewInt64Coin("token2", 100))) - - // Deliver a block that contains a PlaceBid tx (bid: 10 t2, lot: same as starting) - msgs := []sdk.Msg{NewMsgPlaceBid(0, buyer, sdk.NewInt64Coin("token2", 10), sdk.NewInt64Coin("token1", 20))} // bid, lot - header = abci.Header{Height: mapp.LastBlockHeight() + 1} - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, msgs, []uint64{1}, []uint64{0}, true, true, buyerKey) // account number for the buyer account is 1 - - // Check buyer's coins have decreased - mock.CheckBalance(t, mapp, buyer, sdk.NewCoins(sdk.NewInt64Coin("token1", 100), sdk.NewInt64Coin("token2", 90))) - // Check seller's coins have increased - mock.CheckBalance(t, mapp, seller, sdk.NewCoins(sdk.NewInt64Coin("token1", 80), sdk.NewInt64Coin("token2", 110))) - - // Deliver empty blocks until the auction should be closed (bid placed on block 3) - // TODO is there a way of skipping ahead? This takes a while and prints a lot. - for h := mapp.LastBlockHeight() + 1; h < int64(DefaultMaxBidDuration)+4; h++ { - mapp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: h}}) - mapp.EndBlock(abci.RequestEndBlock{Height: h}) - mapp.Commit() - } - // Check buyer's coins increased - mock.CheckBalance(t, mapp, buyer, sdk.NewCoins(sdk.NewInt64Coin("token1", 120), sdk.NewInt64Coin("token2", 90))) -} - -func TestApp_ReverseAuction(t *testing.T) { - // Setup - mapp, keeper, addresses, privKeys := setUpMockApp() - seller := addresses[0] - sellerKey := privKeys[0] - buyer := addresses[1] - //buyerKey := privKeys[1] - - // Create a block where an auction is started - header := abci.Header{Height: mapp.LastBlockHeight() + 1} - mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) - ctx := mapp.BaseApp.NewContext(false, header) - keeper.StartReverseAuction(ctx, buyer, sdk.NewInt64Coin("token1", 20), sdk.NewInt64Coin("token2", 99)) // buyer, bid, initialLot - mapp.EndBlock(abci.RequestEndBlock{}) - mapp.Commit() - - // Check buyer's coins have decreased - mock.CheckBalance(t, mapp, buyer, sdk.NewCoins(sdk.NewInt64Coin("token1", 100), sdk.NewInt64Coin("token2", 1))) - - // Deliver a block that contains a PlaceBid tx - msgs := []sdk.Msg{NewMsgPlaceBid(0, seller, sdk.NewInt64Coin("token1", 20), sdk.NewInt64Coin("token2", 10))} // bid, lot - header = abci.Header{Height: mapp.LastBlockHeight() + 1} - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, msgs, []uint64{0}, []uint64{0}, true, true, sellerKey) - - // Check seller's coins have decreased - mock.CheckBalance(t, mapp, seller, sdk.NewCoins(sdk.NewInt64Coin("token1", 80), sdk.NewInt64Coin("token2", 100))) - // Check buyer's coins have increased - mock.CheckBalance(t, mapp, buyer, sdk.NewCoins(sdk.NewInt64Coin("token1", 120), sdk.NewInt64Coin("token2", 90))) - - // Deliver empty blocks until the auction should be closed (bid placed on block 3) - for h := mapp.LastBlockHeight() + 1; h < int64(DefaultMaxBidDuration)+4; h++ { - mapp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: h}}) - mapp.EndBlock(abci.RequestEndBlock{Height: h}) - mapp.Commit() - } - - // Check seller's coins increased - mock.CheckBalance(t, mapp, seller, sdk.NewCoins(sdk.NewInt64Coin("token1", 80), sdk.NewInt64Coin("token2", 110))) -} -func TestApp_ForwardReverseAuction(t *testing.T) { - // Setup - mapp, keeper, addresses, privKeys := setUpMockApp() - seller := addresses[0] - //sellerKey := privKeys[0] - buyer := addresses[1] - buyerKey := privKeys[1] - recipient := addresses[2] - - // Create a block where an auction is started - header := abci.Header{Height: mapp.LastBlockHeight() + 1} - mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) - ctx := mapp.BaseApp.NewContext(false, header) - keeper.StartForwardReverseAuction(ctx, seller, sdk.NewInt64Coin("token1", 20), sdk.NewInt64Coin("token2", 50), recipient) // seller, lot, maxBid, otherPerson - mapp.EndBlock(abci.RequestEndBlock{}) - mapp.Commit() - - // Check seller's coins have decreased - mock.CheckBalance(t, mapp, seller, sdk.NewCoins(sdk.NewInt64Coin("token1", 80), sdk.NewInt64Coin("token2", 100))) - - // Deliver a block that contains a PlaceBid tx - msgs := []sdk.Msg{NewMsgPlaceBid(0, buyer, sdk.NewInt64Coin("token2", 50), sdk.NewInt64Coin("token1", 15))} // bid, lot - header = abci.Header{Height: mapp.LastBlockHeight() + 1} - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, msgs, []uint64{1}, []uint64{0}, true, true, buyerKey) - - // Check bidder's coins have decreased - mock.CheckBalance(t, mapp, buyer, sdk.NewCoins(sdk.NewInt64Coin("token1", 100), sdk.NewInt64Coin("token2", 50))) - // Check seller's coins have increased - mock.CheckBalance(t, mapp, seller, sdk.NewCoins(sdk.NewInt64Coin("token1", 80), sdk.NewInt64Coin("token2", 150))) - // Check "recipient" has received coins - mock.CheckBalance(t, mapp, recipient, sdk.NewCoins(sdk.NewInt64Coin("token1", 105), sdk.NewInt64Coin("token2", 100))) - - // Deliver empty blocks until the auction should be closed (bid placed on block 3) - for h := mapp.LastBlockHeight() + 1; h < int64(DefaultMaxBidDuration)+4; h++ { - mapp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: h}}) - mapp.EndBlock(abci.RequestEndBlock{Height: h}) - mapp.Commit() - } - - // Check buyer's coins increased - mock.CheckBalance(t, mapp, buyer, sdk.NewCoins(sdk.NewInt64Coin("token1", 115), sdk.NewInt64Coin("token2", 50))) -} - -func setUpMockApp() (*mock.App, Keeper, []sdk.AccAddress, []crypto.PrivKey) { - // Create uninitialized mock app - mapp := mock.NewApp() - - // Register codecs - RegisterCodec(mapp.Cdc) - - // Create keepers - keyAuction := sdk.NewKVStoreKey("auction") - blacklistedAddrs := make(map[string]bool) - bankKeeper := bank.NewBaseKeeper(mapp.AccountKeeper, mapp.ParamsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs) - auctionKeeper := NewKeeper(mapp.Cdc, bankKeeper, keyAuction, mapp.ParamsKeeper.Subspace(DefaultParamspace)) - - // Register routes - mapp.Router().AddRoute("auction", NewHandler(auctionKeeper)) - - // Add endblocker - mapp.SetEndBlocker( - func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { - EndBlocker(ctx, auctionKeeper) - return abci.ResponseEndBlock{} - }, - ) - // Mount and load the stores - err := mapp.CompleteSetup(keyAuction) - if err != nil { - panic("mock app setup failed") - } - - // Create a bunch (ie 10) of pre-funded accounts to use for tests - genAccs, addrs, _, privKeys := mock.CreateGenAccounts(10, sdk.NewCoins(sdk.NewInt64Coin("token1", 100), sdk.NewInt64Coin("token2", 100))) - mock.SetGenesis(mapp, genAccs) - - return mapp, auctionKeeper, addrs, privKeys -} diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index 1ffc561b..f43f81cc 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -144,8 +144,8 @@ func (k Keeper) CloseAuction(ctx sdk.Context, auctionID types.ID) sdk.Error { return err } - // delete auction from store (and queue) - k.deleteAuction(ctx, auctionID) + // Delete auction from store (and queue) + k.DeleteAuction(ctx, auctionID) return nil } @@ -204,7 +204,7 @@ func (k Keeper) SetAuction(ctx sdk.Context, auction types.Auction) { store.Set(k.getAuctionKey(auction.GetID()), bz) // add to the queue - k.insertIntoQueue(ctx, auction.GetEndTime(), auction.GetID()) + k.InsertIntoQueue(ctx, auction.GetEndTime(), auction.GetID()) } // getAuction gets an auction from the store by auctionID @@ -221,8 +221,8 @@ func (k Keeper) GetAuction(ctx sdk.Context, auctionID types.ID) (types.Auction, return auction, true } -// deleteAuction removes an auction from the store without any validation -func (k Keeper) deleteAuction(ctx sdk.Context, auctionID types.ID) { +// DeleteAuction removes an auction from the store without any validation +func (k Keeper) DeleteAuction(ctx sdk.Context, auctionID types.ID) { // remove from queue auction, found := k.GetAuction(ctx, auctionID) if found { @@ -245,7 +245,7 @@ func (k Keeper) getAuctionKey(auctionID types.ID) []byte { } // Inserts a AuctionID into the queue at endTime -func (k Keeper) insertIntoQueue(ctx sdk.Context, endTime types.EndTime, auctionID types.ID) { +func (k Keeper) InsertIntoQueue(ctx sdk.Context, endTime types.EndTime, auctionID types.ID) { // get the store store := ctx.KVStore(k.storeKey) // marshal thing to be inserted diff --git a/x/auction/keeper/keeper_test.go b/x/auction/keeper/keeper_test.go index 6c77b32e..79aa9807 100644 --- a/x/auction/keeper/keeper_test.go +++ b/x/auction/keeper/keeper_test.go @@ -1,21 +1,126 @@ -package keeper +package keeper_test import ( "testing" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/kava-labs/kava/x/auction/types" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" + + "github.com/kava-labs/kava/app" + "github.com/kava-labs/kava/x/auction/keeper" + "github.com/kava-labs/kava/x/auction/types" ) +func TestKeeper_ForwardAuction(t *testing.T) { + // Setup + _, addrs := app.GeneratePrivKeyAddressPairs(2) + seller := addrs[0] + buyer := addrs[1] + + tApp := app.NewTestApp() + authGenState := tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100))}) + tApp.InitializeFromGenesisStates(authGenState) + + ctx := tApp.NewContext(false, abci.Header{}) + keeper := tApp.GetAuctionKeeper() + + // Create an auction (lot: 20 t1, initialBid: 0 t2) + auctionID, err := keeper.StartForwardAuction(ctx, seller, c("token1", 20), c("token2", 0)) // lot, initialBid + require.NoError(t, err) + // Check seller's coins have decreased + tApp.CheckBalance(t, ctx, seller, cs(c("token1", 80), c("token2", 100))) + + // PlaceBid (bid: 10 t2, lot: same as starting) + require.NoError(t, keeper.PlaceBid(ctx, 0, buyer, c("token2", 10), c("token1", 20))) // bid, lot + // Check buyer's coins have decreased + tApp.CheckBalance(t, ctx, buyer, cs(c("token1", 100), c("token2", 90))) + // Check seller's coins have increased + tApp.CheckBalance(t, ctx, seller, cs(c("token1", 80), c("token2", 110))) + + // Close auction at just after auction expiry + ctx = ctx.WithBlockHeight(int64(types.DefaultMaxBidDuration)) + require.NoError(t, keeper.CloseAuction(ctx, auctionID)) + // Check buyer's coins increased + tApp.CheckBalance(t, ctx, buyer, cs(c("token1", 120), c("token2", 90))) +} + +func TestKeeper_ReverseAuction(t *testing.T) { + // Setup + _, addrs := app.GeneratePrivKeyAddressPairs(2) + seller := addrs[0] + buyer := addrs[1] + + tApp := app.NewTestApp() + authGenState := tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100))}) + tApp.InitializeFromGenesisStates(authGenState) + + ctx := tApp.NewContext(false, abci.Header{}) + keeper := tApp.GetAuctionKeeper() + + // Start auction + auctionID, err := keeper.StartReverseAuction(ctx, buyer, c("token1", 20), c("token2", 99)) // buyer, bid, initialLot + require.NoError(t, err) + // Check buyer's coins have decreased + tApp.CheckBalance(t, ctx, buyer, cs(c("token1", 100), c("token2", 1))) + + // Place a bid + require.NoError(t, keeper.PlaceBid(ctx, 0, seller, c("token1", 20), c("token2", 10))) // bid, lot + // Check seller's coins have decreased + tApp.CheckBalance(t, ctx, seller, cs(c("token1", 80), c("token2", 100))) + // Check buyer's coins have increased + tApp.CheckBalance(t, ctx, buyer, cs(c("token1", 120), c("token2", 90))) + + // Close auction at just after auction expiry + ctx = ctx.WithBlockHeight(int64(types.DefaultMaxBidDuration)) + require.NoError(t, keeper.CloseAuction(ctx, auctionID)) + // Check seller's coins increased + tApp.CheckBalance(t, ctx, seller, cs(c("token1", 80), c("token2", 110))) +} + +func TestKeeper_ForwardReverseAuction(t *testing.T) { + // Setup + _, addrs := app.GeneratePrivKeyAddressPairs(3) + seller := addrs[0] + buyer := addrs[1] + recipient := addrs[2] + + tApp := app.NewTestApp() + authGenState := tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100))}) + tApp.InitializeFromGenesisStates(authGenState) + + ctx := tApp.NewContext(false, abci.Header{}) + keeper := tApp.GetAuctionKeeper() + + // Start auction + auctionID, err := keeper.StartForwardReverseAuction(ctx, seller, c("token1", 20), c("token2", 50), recipient) // seller, lot, maxBid, otherPerson + require.NoError(t, err) + // Check seller's coins have decreased + tApp.CheckBalance(t, ctx, seller, cs(c("token1", 80), c("token2", 100))) + + // Place a bid + require.NoError(t, keeper.PlaceBid(ctx, 0, buyer, c("token2", 50), c("token1", 15))) // bid, lot + // Check bidder's coins have decreased + tApp.CheckBalance(t, ctx, buyer, cs(c("token1", 100), c("token2", 50))) + // Check seller's coins have increased + tApp.CheckBalance(t, ctx, seller, cs(c("token1", 80), c("token2", 150))) + // Check "recipient" has received coins + tApp.CheckBalance(t, ctx, recipient, cs(c("token1", 105), c("token2", 100))) + + // Close auction at just after auction expiry + ctx = ctx.WithBlockHeight(int64(types.DefaultMaxBidDuration)) + require.NoError(t, keeper.CloseAuction(ctx, auctionID)) + // Check buyer's coins increased + tApp.CheckBalance(t, ctx, buyer, cs(c("token1", 115), c("token2", 50))) +} + func TestKeeper_SetGetDeleteAuction(t *testing.T) { // setup keeper, create auction - mapp, keeper, addresses, _ := setUpMockApp() - header := abci.Header{Height: mapp.LastBlockHeight() + 1} - mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) // Without this it panics about "invalid memory address or nil pointer dereference" - ctx := mapp.BaseApp.NewContext(false, header) - auction, _ := types.NewForwardAuction(addresses[0], sdk.NewInt64Coin("usdx", 100), sdk.NewInt64Coin("kava", 0), types.EndTime(1000)) + _, addrs := app.GeneratePrivKeyAddressPairs(1) + tApp := app.NewTestApp() + keeper := tApp.GetAuctionKeeper() + ctx := tApp.NewContext(true, abci.Header{}) + auction, _ := types.NewForwardAuction(addrs[0], c("usdx", 100), c("kava", 0), types.EndTime(1000)) id := types.ID(5) auction.SetID(id) @@ -26,15 +131,13 @@ func TestKeeper_SetGetDeleteAuction(t *testing.T) { // check before and after match require.True(t, found) require.Equal(t, &auction, readAuction) - t.Log(auction) - t.Log(readAuction.GetID()) // check auction is in queue iter := keeper.GetQueueIterator(ctx, 100000) require.Equal(t, 1, len(convertIteratorToSlice(keeper, iter))) iter.Close() // delete auction - keeper.deleteAuction(ctx, id) + keeper.DeleteAuction(ctx, id) // check auction does not exist _, found = keeper.GetAuction(ctx, id) @@ -49,10 +152,10 @@ func TestKeeper_SetGetDeleteAuction(t *testing.T) { // TODO convert to table driven test with more test cases func TestKeeper_ExpiredAuctionQueue(t *testing.T) { // setup keeper - mapp, keeper, _, _ := setUpMockApp() - header := abci.Header{Height: mapp.LastBlockHeight() + 1} - mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) - ctx := mapp.BaseApp.NewContext(false, header) + tApp := app.NewTestApp() + keeper := tApp.GetAuctionKeeper() + ctx := tApp.NewContext(true, abci.Header{}) + // create an example queue type queue []struct { endTime types.EndTime @@ -62,7 +165,7 @@ func TestKeeper_ExpiredAuctionQueue(t *testing.T) { // write and read queue for _, v := range q { - keeper.insertIntoQueue(ctx, v.endTime, v.auctionID) + keeper.InsertIntoQueue(ctx, v.endTime, v.auctionID) } iter := keeper.GetQueueIterator(ctx, 1000) @@ -70,19 +173,22 @@ func TestKeeper_ExpiredAuctionQueue(t *testing.T) { i := 0 for ; iter.Valid(); iter.Next() { var auctionID types.ID - keeper.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &auctionID) + tApp.Codec().MustUnmarshalBinaryLengthPrefixed(iter.Value(), &auctionID) require.Equal(t, q[i].auctionID, auctionID) i++ } } -func convertIteratorToSlice(keeper Keeper, iterator sdk.Iterator) []types.ID { +func convertIteratorToSlice(keeper keeper.Keeper, iterator sdk.Iterator) []types.ID { var queue []types.ID for ; iterator.Valid(); iterator.Next() { var auctionID types.ID - keeper.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &auctionID) + types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &auctionID) queue = append(queue, auctionID) } return queue } + +func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amount) } +func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) } diff --git a/x/auction/keeper/test_common.go b/x/auction/keeper/test_common.go deleted file mode 100644 index b5702438..00000000 --- a/x/auction/keeper/test_common.go +++ /dev/null @@ -1,35 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank" - "github.com/cosmos/cosmos-sdk/x/mock" - "github.com/kava-labs/kava/x/auction/types" - "github.com/tendermint/tendermint/crypto" -) - -func setUpMockApp() (*mock.App, Keeper, []sdk.AccAddress, []crypto.PrivKey) { - // Create uninitialized mock app - mapp := mock.NewApp() - - // Register codecs - types.RegisterCodec(mapp.Cdc) - - // Create keepers - keyAuction := sdk.NewKVStoreKey("auction") - blacklistedAddrs := make(map[string]bool) - bankKeeper := bank.NewBaseKeeper(mapp.AccountKeeper, mapp.ParamsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs) - auctionKeeper := NewKeeper(mapp.Cdc, bankKeeper, keyAuction, mapp.ParamsKeeper.Subspace(types.DefaultParamspace)) - - // Mount and load the stores - err := mapp.CompleteSetup(keyAuction) - if err != nil { - panic("mock app setup failed") - } - - // Create a bunch (ie 10) of pre-funded accounts to use for tests - genAccs, addrs, _, privKeys := mock.CreateGenAccounts(10, sdk.NewCoins(sdk.NewInt64Coin("token1", 100), sdk.NewInt64Coin("token2", 100))) - mock.SetGenesis(mapp, genAccs) - - return mapp, auctionKeeper, addrs, privKeys -}