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

chore(marketplace): notify sales when duration, minPricePerBytePerSecond or totalCollateral is updated #1148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions codex/sales.nim
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ proc load*(sales: Sales) {.async.} =
agent.start(SaleUnknown())
sales.agents.add agent

proc onAvailabilityAdded(sales: Sales, availability: Availability) {.async.} =
proc OnAvailabilitySaved(sales: Sales, availability: Availability) {.async.} =
## When availabilities are modified or added, the queue should be unpaused if
## it was paused and any slots in the queue should have their `seen` flag
## cleared.
Expand Down Expand Up @@ -528,10 +528,10 @@ proc startSlotQueue(sales: Sales) =

slotQueue.start()

proc onAvailabilityAdded(availability: Availability) {.async.} =
await sales.onAvailabilityAdded(availability)
proc OnAvailabilitySaved(availability: Availability) {.async.} =
await sales.OnAvailabilitySaved(availability)

reservations.onAvailabilityAdded = onAvailabilityAdded
reservations.OnAvailabilitySaved = OnAvailabilitySaved

proc subscribe(sales: Sales) {.async.} =
await sales.subscribeRequested()
Expand Down
34 changes: 18 additions & 16 deletions codex/sales/reservations.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@
availabilityLock: AsyncLock
# Lock for protecting assertions of availability's sizes when searching for matching availability
repo: RepoStore
onAvailabilityAdded: ?OnAvailabilityAdded
OnAvailabilitySaved: ?OnAvailabilitySaved

GetNext* = proc(): Future[?seq[byte]] {.upraises: [], gcsafe, closure.}
IterDispose* = proc(): Future[?!void] {.gcsafe, closure.}
OnAvailabilityAdded* =
OnAvailabilitySaved* =
proc(availability: Availability): Future[void] {.upraises: [], gcsafe.}
StorableIter* = ref object
finished*: bool
Expand Down Expand Up @@ -189,10 +189,10 @@
logutils.formatIt(LogFormat.json, SomeStorableId):
it.to0xHexLog

proc `onAvailabilityAdded=`*(
self: Reservations, onAvailabilityAdded: OnAvailabilityAdded
proc `OnAvailabilitySaved=`*(
self: Reservations, OnAvailabilitySaved: OnAvailabilitySaved
) =
self.onAvailabilityAdded = some onAvailabilityAdded
self.OnAvailabilitySaved = some OnAvailabilitySaved

func key*(id: AvailabilityId): ?!Key =
## sales / reservations / <availabilityId>
Expand Down Expand Up @@ -268,18 +268,18 @@
trace "Creating new Availability"
let res = await self.updateImpl(obj)
# inform subscribers that Availability has been added
if onAvailabilityAdded =? self.onAvailabilityAdded:
# when chronos v4 is implemented, and OnAvailabilityAdded is annotated
if OnAvailabilitySaved =? self.OnAvailabilitySaved:
# when chronos v4 is implemented, and OnAvailabilitySaved is annotated
# with async:(raises:[]), we can remove this try/catch as we know, with
# certainty, that nothing will be raised
try:
await onAvailabilityAdded(obj)
await OnAvailabilitySaved(obj)
except CancelledError as e:
raise e
except CatchableError as e:
# we don't have any insight into types of exceptions that
# `onAvailabilityAdded` can raise because it is caller-defined
warn "Unknown error during 'onAvailabilityAdded' callback", error = e.msg
# `OnAvailabilitySaved` can raise because it is caller-defined
warn "Unknown error during 'OnAvailabilitySaved' callback", error = e.msg

Check warning on line 282 in codex/sales/reservations.nim

View check run for this annotation

Codecov / codecov/patch

codex/sales/reservations.nim#L281-L282

Added lines #L281 - L282 were not covered by tests
return res
else:
return failure(err)
Expand All @@ -300,22 +300,24 @@

let res = await self.updateImpl(obj)

if oldAvailability.freeSize < obj.freeSize: # availability added
if oldAvailability.freeSize < obj.freeSize or oldAvailability.duration < obj.duration or
oldAvailability.minPricePerBytePerSecond < obj.minPricePerBytePerSecond or
oldAvailability.totalCollateral < obj.totalCollateral: # availability updated
# inform subscribers that Availability has been modified (with increased
# size)
if onAvailabilityAdded =? self.onAvailabilityAdded:
# when chronos v4 is implemented, and OnAvailabilityAdded is annotated
if OnAvailabilitySaved =? self.OnAvailabilitySaved:
# when chronos v4 is implemented, and OnAvailabilitySaved is annotated
# with async:(raises:[]), we can remove this try/catch as we know, with
# certainty, that nothing will be raised
try:
await onAvailabilityAdded(obj)
await OnAvailabilitySaved(obj)
except CancelledError as e:
raise e
except CatchableError as e:
# we don't have any insight into types of exceptions that
# `onAvailabilityAdded` can raise because it is caller-defined
warn "Unknown error during 'onAvailabilityAdded' callback", error = e.msg
# `OnAvailabilitySaved` can raise because it is caller-defined
warn "Unknown error during 'OnAvailabilitySaved' callback", error = e.msg
Comment on lines +308 to +319
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good idea to annotate OnAvailableSaved with {.async: (raises: [].} and remove this try/except.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Check warning on line 320 in codex/sales/reservations.nim

View check run for this annotation

Codecov / codecov/patch

codex/sales/reservations.nim#L318-L320

Added lines #L318 - L320 were not covered by tests
return res

proc update*(self: Reservations, obj: Reservation): Future[?!void] {.async.} =
Expand Down
72 changes: 66 additions & 6 deletions tests/codex/sales/testreservations.nim
Original file line number Diff line number Diff line change
Expand Up @@ -283,35 +283,95 @@
check updated.isErr
check updated.error of NotExistsError

test "onAvailabilityAdded called when availability is created":
test "OnAvailabilitySaved called when availability is created":
var added: Availability
reservations.onAvailabilityAdded = proc(a: Availability) {.async.} =
reservations.OnAvailabilitySaved = proc(a: Availability) {.async.} =
added = a

let availability = createAvailability()

check added == availability

test "onAvailabilityAdded called when availability size is increased":
test "OnAvailabilitySaved called when availability size is increased":
var availability = createAvailability()
var added: Availability
reservations.onAvailabilityAdded = proc(a: Availability) {.async.} =
reservations.OnAvailabilitySaved = proc(a: Availability) {.async.} =
added = a
availability.freeSize += 1
discard await reservations.update(availability)

check added == availability

test "onAvailabilityAdded is not called when availability size is decreased":
test "OnAvailabilitySaved is not called when availability size is decreased":
var availability = createAvailability()
var called = false
reservations.onAvailabilityAdded = proc(a: Availability) {.async.} =
reservations.OnAvailabilitySaved = proc(a: Availability) {.async.} =
called = true
availability.freeSize -= 1
discard await reservations.update(availability)

check not called

test "OnAvailabilitySaved called when availability duration is increased":
var availability = createAvailability()
var added: Availability
reservations.OnAvailabilitySaved = proc(a: Availability) {.async.} =
added = a
availability.duration += 1
discard await reservations.update(availability)

check added == availability

test "OnAvailabilitySaved is not called when availability duration is decreased":
var availability = createAvailability()
var called = false
reservations.OnAvailabilitySaved = proc(a: Availability) {.async.} =
called = true
availability.duration -= 1

Check warning on line 330 in tests/codex/sales/testreservations.nim

View check run for this annotation

Codecov / codecov/patch

tests/codex/sales/testreservations.nim#L329-L330

Added lines #L329 - L330 were not covered by tests
discard await reservations.update(availability)

check not called

test "OnAvailabilitySaved called when availability minPricePerBytePerSecond is increased":
var availability = createAvailability()
var added: Availability
reservations.OnAvailabilitySaved = proc(a: Availability) {.async.} =
added = a
availability.minPricePerBytePerSecond += 1.u256
discard await reservations.update(availability)

check added == availability

test "OnAvailabilitySaved is not called when availability minPricePerBytePerSecond is decreased":
var availability = createAvailability()
var called = false
reservations.OnAvailabilitySaved = proc(a: Availability) {.async.} =
called = true

Check warning on line 349 in tests/codex/sales/testreservations.nim

View check run for this annotation

Codecov / codecov/patch

tests/codex/sales/testreservations.nim#L349

Added line #L349 was not covered by tests
availability.minPricePerBytePerSecond -= 1.u256
discard await reservations.update(availability)

check not called

test "OnAvailabilitySaved called when availability totalCollateral is increased":
var availability = createAvailability()
var added: Availability
reservations.OnAvailabilitySaved = proc(a: Availability) {.async.} =
added = a
availability.totalCollateral = availability.totalCollateral + 1.u256
discard await reservations.update(availability)

check added == availability

test "OnAvailabilitySaved is not called when availability totalCollateral is decreased":
var availability = createAvailability()
var called = false
reservations.OnAvailabilitySaved = proc(a: Availability) {.async.} =
called = true

Check warning on line 369 in tests/codex/sales/testreservations.nim

View check run for this annotation

Codecov / codecov/patch

tests/codex/sales/testreservations.nim#L369

Added line #L369 was not covered by tests
availability.totalCollateral = availability.totalCollateral - 1.u256
discard await reservations.update(availability)

check not called

test "availabilities can be found":
let availability = createAvailability()

Expand Down
Loading