mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-03 22:13:12 +00:00
chore(marketplace): notify sales when duration, minPricePerBytePerSecond or totalCollateral is updated (#1148)
* Call onAvailabilityAdded when freeSize, duration or minPricePerBytePerSecond is increased * Rename onAvailabilityAdded to onAvailabilitySaved * Rename OnAvailabilitySaved to OnAvailabilityUpserted * Go back to OnAvailabilitySaved
This commit is contained in:
parent
703921df32
commit
17d3bb55cf
@ -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.
|
||||
@ -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()
|
||||
|
||||
@ -82,11 +82,11 @@ type
|
||||
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
|
||||
@ -189,10 +189,10 @@ logutils.formatIt(LogFormat.textLines, SomeStorableId):
|
||||
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>
|
||||
@ -268,18 +268,18 @@ proc updateAvailability(
|
||||
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
|
||||
return res
|
||||
else:
|
||||
return failure(err)
|
||||
@ -300,21 +300,23 @@ proc updateAvailability(
|
||||
|
||||
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
|
||||
|
||||
return res
|
||||
|
||||
|
||||
@ -283,35 +283,95 @@ asyncchecksuite "Reservations module":
|
||||
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
|
||||
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
|
||||
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
|
||||
availability.totalCollateral = availability.totalCollateral - 1.u256
|
||||
discard await reservations.update(availability)
|
||||
|
||||
check not called
|
||||
|
||||
test "availabilities can be found":
|
||||
let availability = createAvailability()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user