-
Notifications
You must be signed in to change notification settings - Fork 27
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(marketplace): catch Marketplace_SlotIsFree and continue the cancelled process #1139
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import ../salesagent | ||
import ../statemachine | ||
import ./errored | ||
from ../../contracts/marketplace import Marketplace_SlotIsFree | ||
|
||
logScope: | ||
topics = "marketplace sales failed" | ||
|
@@ -28,12 +29,17 @@ | |
let slot = Slot(request: request, slotIndex: data.slotIndex) | ||
debug "Removing slot from mySlots", | ||
requestId = data.requestId, slotIndex = data.slotIndex | ||
|
||
await market.freeSlot(slot.id) | ||
|
||
let error = newException(SaleFailedError, "Sale failed") | ||
return some State(SaleErrored(error: error)) | ||
except CancelledError as e: | ||
trace "SaleFailed.run was cancelled", error = e.msgDetail | ||
except Marketplace_SlotIsFree as e: | ||
info "The slot cannot be freed because it is already free.", error = e.msg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, re: |
||
let error = newException(SaleFailedError, "Sale failed") | ||
return some State(SaleErrored(error: error)) | ||
except CatchableError as e: | ||
error "Error during SaleFailed.run", error = e.msgDetail | ||
return some State(SaleErrored(error: e)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ | |
subscriptions: Subscriptions | ||
config*: MarketplaceConfig | ||
canReserveSlot*: bool | ||
reserveSlotThrowError*: ?(ref MarketError) | ||
slotThrowError*: ?(ref CatchableError) | ||
clock: ?Clock | ||
|
||
Fulfillment* = object | ||
|
@@ -235,7 +235,7 @@ | |
|
||
method currentCollateral*( | ||
market: MockMarket, slotId: SlotId | ||
): Future[UInt256] {.async.} = | ||
): Future[UInt256] {.async: (raises: [MarketError, AsyncLockError, CancelledError]).} = | ||
for slot in market.filled: | ||
if slotId == slotId(slot.requestId, slot.slotIndex): | ||
return slot.collateral | ||
|
@@ -311,6 +311,9 @@ | |
market.fillSlot(requestId, slotIndex, proof, market.signer, collateral) | ||
|
||
method freeSlot*(market: MockMarket, slotId: SlotId) {.async.} = | ||
if error =? market.slotThrowError: | ||
raise error | ||
|
||
market.freed.add(slotId) | ||
for s in market.filled: | ||
if slotId(s.requestId, s.slotIndex) == slotId: | ||
|
@@ -370,7 +373,7 @@ | |
method reserveSlot*( | ||
market: MockMarket, requestId: RequestId, slotIndex: uint64 | ||
) {.async.} = | ||
if error =? market.reserveSlotThrowError: | ||
if error =? market.slotThrowError: | ||
raise error | ||
|
||
method canReserveSlot*( | ||
|
@@ -381,8 +384,8 @@ | |
func setCanReserveSlot*(market: MockMarket, canReserveSlot: bool) = | ||
market.canReserveSlot = canReserveSlot | ||
|
||
func setReserveSlotThrowError*(market: MockMarket, error: ?(ref MarketError)) = | ||
market.reserveSlotThrowError = error | ||
func setSlotThrowError*(market: MockMarket, error: ?(ref CatchableError)) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Along the lines of the comment in the other PR, i'd make this specific to |
||
market.slotThrowError = error | ||
|
||
method subscribeRequests*( | ||
market: MockMarket, callback: OnRequest | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import pkg/codex/sales/states/cancelled | |
import pkg/codex/sales/salesagent | ||
import pkg/codex/sales/salescontext | ||
import pkg/codex/market | ||
from pkg/codex/contracts/marketplace import Marketplace_SlotIsFree | ||
|
||
import ../../../asynctest | ||
import ../../examples | ||
|
@@ -40,6 +41,11 @@ asyncchecksuite "sales state 'cancelled'": | |
agent.onCleanUp = onCleanUp | ||
state = SaleCancelled.new() | ||
|
||
teardown: | ||
returnBytesWas = bool.none | ||
reprocessSlotWas = bool.none | ||
returnedCollateralValue = UInt256.none | ||
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be in |
||
|
||
test "calls onCleanUp with returnBytes = false, reprocessSlot = true, and returnedCollateral = currentCollateral": | ||
market.fillSlot( | ||
requestId = request.id, | ||
|
@@ -52,3 +58,20 @@ asyncchecksuite "sales state 'cancelled'": | |
check eventually returnBytesWas == some true | ||
check eventually reprocessSlotWas == some false | ||
check eventually returnedCollateralValue == some currentCollateral | ||
|
||
test "calls onCleanUp and returns the collateral when free slot error is raised": | ||
market.fillSlot( | ||
requestId = request.id, | ||
slotIndex = slotIndex, | ||
proof = Groth16Proof.default, | ||
host = Address.example, | ||
collateral = currentCollateral, | ||
) | ||
|
||
let error = newException(Marketplace_SlotIsFree, "") | ||
market.setSlotThrowError(some cast[ref CatchableError](error)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment in other PR regarding use of generics to prevent a conversion/cast. |
||
|
||
discard await state.run(agent) | ||
check eventually returnBytesWas == some true | ||
check eventually reprocessSlotWas == some false | ||
check eventually returnedCollateralValue == some currentCollateral | ||
Comment on lines
+74
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think maybe we can improve the assumptions we're testing here because although we call
The test here covers #2, but would just need to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this was a SP that had filled a slot, and the slot could not be freed, I would think this should be a warning. However, in other cases, this is normal behaviour. So I'd probably reduce this to a
debug
level because in either case, it's not essential that the user sees the log and we don't really need it to display in default Codex logging settings.