diff --git a/codex/rest/api.nim b/codex/rest/api.nim index dac4defc..2a2e590f 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -49,6 +49,32 @@ logScope: declareCounter(codex_api_uploads, "codex API uploads") declareCounter(codex_api_downloads, "codex API downloads") +proc getLongestRequestEnd( + node: CodexNodeRef, availabilityId: AvailabilityId +): Future[?!SecondsSince1970] {.async: (raises: []).} = + without contracts =? node.contracts.host: + return failure("Sales unavailable") + + let + reservations = contracts.sales.context.reservations + market = contracts.sales.context.market + + try: + without allReservations =? await reservations.all(Reservation, availabilityId): + return failure("Cannot retrieve the reservations") + + echo "all reservations is done" + let requestEnds = allReservations.mapIt(await market.getRequestEnd(it.requestId)) + + if len(requestEnds) == 0: + return success(0.SecondsSince1970) + + return success(requestEnds.max) + except CancelledError, CatchableError: + error "Error when trying to get longest request end", + error = getCurrentExceptionMsg() + return failure("Cannot retrieve the request dates") + proc validate(pattern: string, value: string): int {.gcsafe, raises: [Defect].} = 0 diff --git a/codex/sales/reservations.nim b/codex/sales/reservations.nim index c34ba474..f913d7c8 100644 --- a/codex/sales/reservations.nim +++ b/codex/sales/reservations.nim @@ -708,14 +708,14 @@ proc findAvailability*( let endTime = getTime().toUnix() + cast[int64](duration) for item in storables.items: if bytes =? (await item) and availability =? Availability.fromJson(bytes): - if availability.enabled and size <= availability.freeSize and - duration <= availability.duration and + let enabled = availability.enabled + if enabled and size <= availability.freeSize and duration <= availability.duration and collateralPerByte <= availability.maxCollateralPerByte and pricePerBytePerSecond >= availability.minPricePerBytePerSecond and (availability.until == 0 or availability.until >= endTime): trace "availability matched", id = availability.id, - enabled = availability.enabled, + enabled = enabled, size, availFreeSize = availability.freeSize, duration, diff --git a/tests/codex/sales/testsales.nim b/tests/codex/sales/testsales.nim index 3aea608c..aa428bbf 100644 --- a/tests/codex/sales/testsales.nim +++ b/tests/codex/sales/testsales.nim @@ -448,7 +448,7 @@ asyncchecksuite "Sales": var storingRequest: StorageRequest sales.onStore = proc( - request: StorageRequest, slot: UInt256, onBatch: BatchProc + request: StorageRequest, slot: uint64, onBatch: BatchProc ): Future[?!void] {.async.} = storingRequest = request return success() diff --git a/tests/integration/testrestapi.nim b/tests/integration/testrestapi.nim index c6b2c494..62d0df2d 100644 --- a/tests/integration/testrestapi.nim +++ b/tests/integration/testrestapi.nim @@ -33,7 +33,7 @@ twonodessuite "REST API": duration = 2.uint64, minPricePerBytePerSecond = minPricePerBytePerSecond, totalCollateral = totalCollateral, - enabled = true.some, + enabled = true, ).get let space = client1.space().tryGet() check: @@ -42,19 +42,6 @@ twonodessuite "REST API": space.quotaUsedBytes == 65592.NBytes space.quotaReservedBytes == 12.NBytes - test "created correctly an availability when not enabled by default", twoNodesConfig: - let totalSize = 12.u256 - let minPricePerBytePerSecond = 1.u256 - let totalCollateral = totalSize * minPricePerBytePerSecond - let availability = client1.postAvailability( - totalSize = totalSize, - duration = 2.u256, - minPricePerBytePerSecond = minPricePerBytePerSecond, - totalCollateral = totalCollateral, - enabled = false.some, - ).get - check availability.enabled == false - test "node lists local files", twoNodesConfig: let content1 = "some file contents" let content2 = "some other contents" diff --git a/tests/integration/testsales.nim b/tests/integration/testsales.nim index 8622996a..6a7a2716 100644 --- a/tests/integration/testsales.nim +++ b/tests/integration/testsales.nim @@ -145,7 +145,21 @@ multinodesuite "Sales": ).get host.patchAvailability(availability.id, enabled = false.some) let updatedAvailability = (host.getAvailabilities().get).findItem(availability).get - check updatedAvailability.enabled == false + check updatedAvailability.enabled == false.some + + test "updating availability - updating until", salesConfig: + var until = cast[SecondsSince1970](getTime().toUnix()) + let availability = host.postAvailability( + totalSize = 140000.u256, + duration = 200.u256, + minPricePerBytePerSecond = 3.u256, + totalCollateral = 300.u256, + until = until.some, + ).get + until += 10.SecondsSince1970 + host.patchAvailability(availability.id, until = until.some) + let updatedAvailability = (host.getAvailabilities().get).findItem(availability).get + check updatedAvailability.until == until test "updating availability - updating totalSize does not allow bellow utilized", salesConfig: @@ -189,3 +203,19 @@ multinodesuite "Sales": (host.getAvailabilities().get).findItem(availability).get check newUpdatedAvailability.totalSize == originalSize + 20000 check newUpdatedAvailability.freeSize - updatedAvailability.freeSize == 20000 + + test "updating availability fails with until negative", salesConfig: + let availability = host.postAvailability( + totalSize = 140000.u256, + duration = 200.u256, + minPricePerBytePerSecond = 3.u256, + totalCollateral = 300.u256, + ).get + + let response = host.patchAvailabilityRaw( + availability.id, until = cast[SecondsSince1970](-1).some + ) + + check: + response.status == "400 Bad Request" + response.body == "Until parameter must be greater or equal 0. Got: -1"