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

fix(ecocredit/marketplace): nil expirations no longer pruned #1115

Merged
merged 8 commits into from
May 31, 2022
9 changes: 7 additions & 2 deletions x/ecocredit/server/marketplace/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ 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.
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 +34,10 @@ func (k Keeper) PruneSellOrders(ctx context.Context) error {
if err = k.unescrowCredits(ctx, sellOrder.Seller, sellOrder.BatchId, 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 @@ -66,3 +66,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 := time.Parse("2006-01-02", "2020-01-01")
assert.NilError(t, err)
expired, err := time.Parse("2006-01-02", "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)
}