Skip to content

Commit

Permalink
fix(ecocredit/marketplace): nil expirations no longer pruned (#1115)
Browse files Browse the repository at this point in the history
* fix: nil expirations no longer pruned

* fix: gigabrain moment

* docs: add godoc explaining hack

* Update x/ecocredit/server/marketplace/prune.go

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>

* test: use ParseDate

Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com>
Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
  • Loading branch information
3 people authored May 31, 2022
1 parent eb54e96 commit 12969b5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
10 changes: 8 additions & 2 deletions x/ecocredit/server/marketplace/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import (
func (k Keeper) PruneSellOrders(ctx context.Context) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)

min, blockTime := timestamppb.New(time.Unix(0, 0)), timestamppb.New(sdkCtx.BlockTime())
// we set the min to 1 ns because nil expirations are encoded as the 0 value timestamp,
// and we DO NOT want those to be deleted/unescrowed.
// https://github.com/cosmos/cosmos-sdk/issues/11980
min, blockTime := timestamppb.New(time.Unix(0, 1)), timestamppb.New(sdkCtx.BlockTime())
fromKey, toKey := marketplaceapi.SellOrderExpirationIndexKey{}.WithExpiration(min), marketplaceapi.SellOrderExpirationIndexKey{}.WithExpiration(blockTime)

it, err := k.stateStore.SellOrderTable().ListRange(ctx, fromKey, toKey)
if err != nil {
return err
}
defer it.Close()
for it.Next() {
sellOrder, err := it.Value()
if err != nil {
Expand All @@ -32,7 +35,10 @@ func (k Keeper) PruneSellOrders(ctx context.Context) error {
if err = k.unescrowCredits(ctx, sellOrder.Seller, sellOrder.BatchKey, sellOrder.Quantity); err != nil {
return err
}

}
it.Close()

return k.stateStore.SellOrderTable().DeleteRange(ctx, fromKey, toKey)
}

Expand Down
38 changes: 38 additions & 0 deletions x/ecocredit/server/marketplace/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,41 @@ func TestSell_Prune(t *testing.T) {
_, err = s.marketStore.SellOrderTable().Get(s.ctx, shouldBeValid)
assert.NilError(t, err)
}

// TestPrune_NilExpiration tests that sell orders with nil expirations are not deleted when PruneOrders is called.
func TestPrune_NilExpiration(t *testing.T) {
t.Parallel()
s := setupBase(t)
s.testSellSetup(batchDenom, ask.Denom, ask.Denom[1:], "C01", start, end, creditType)

blockTime, err := types.ParseDate("block time", "2020-01-01")
assert.NilError(t, err)
expired, err := types.ParseDate("expiration", "2010-01-01")
assert.NilError(t, err)

msg := &marketplace.MsgSell{
Owner: s.addr.String(),
Orders: []*marketplace.MsgSell_Order{
{BatchDenom: batchDenom, Quantity: "5", AskPrice: &ask, Expiration: nil},
{BatchDenom: batchDenom, Quantity: "10", AskPrice: &ask, Expiration: &expired},
},
}
res, err := s.k.Sell(s.ctx, msg)
assert.NilError(t, err)

shouldExistOrder := res.SellOrderIds[0]
shouldNotExistOrder := res.SellOrderIds[1]

s.sdkCtx = s.sdkCtx.WithBlockTime(blockTime)
s.ctx = sdk.WrapSDKContext(s.sdkCtx)

err = s.k.PruneSellOrders(s.ctx)
assert.NilError(t, err)

order, err := s.marketStore.SellOrderTable().Get(s.ctx, shouldExistOrder)
assert.NilError(t, err)
assert.Equal(t, "5", order.Quantity)

order, err = s.marketStore.SellOrderTable().Get(s.ctx, shouldNotExistOrder)
assert.ErrorIs(t, err, ormerrors.NotFound)
}

0 comments on commit 12969b5

Please sign in to comment.