[sales] Do not call onSale() when someone else's offer is selected

This commit is contained in:
Mark Spanbroek 2022-03-31 11:20:18 +02:00 committed by markspanbroek
parent 71f25d40c7
commit 3810889fcb
2 changed files with 17 additions and 1 deletions

View File

@ -70,6 +70,7 @@ proc handleRequest(sales: Sales, request: StorageRequest) {.async.} =
proc onSelect(offerId: array[32, byte]) {.gcsafe, upraises:[].} =
if subscription =? subscription:
asyncSpawn subscription.unsubscribe()
if offer.id == offerId:
sales.onSale(offer)
subscription = some await sales.market.subscribeSelection(request.id, onSelect)

View File

@ -88,3 +88,18 @@ suite "Sales":
await market.selectOffer(offer.id)
check selectedOffer == offer
sales.stop()
test "does not call onSale when a different offer is selected":
let availability = Availability.init(size=100, duration=60, minPrice=42.u256)
sales.add(availability)
var onSaleWasCalled: bool
sales.onSale = proc(offer: StorageOffer) =
onSaleWasCalled = true
sales.start()
var request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
request = await market.requestStorage(request)
var otherOffer = StorageOffer(requestId: request.id, price: 1.u256)
otherOffer = await market.offerStorage(otherOffer)
await market.selectOffer(otherOffer.id)
check not onSaleWasCalled
sales.stop()