mirror of
https://github.com/status-im/nim-codex.git
synced 2025-02-12 02:36:46 +00:00
Clean up and fix tests
This commit is contained in:
parent
ac24f35250
commit
9764d3f133
@ -580,7 +580,8 @@ proc initSalesApi(node: CodexNodeRef, router: var RestRouter) =
|
||||
|
||||
availability.until = until
|
||||
|
||||
availability.enabled = restAv.enabled
|
||||
if enabled =? restAv.enabled:
|
||||
availability.enabled = enabled
|
||||
|
||||
if err =? (await reservations.update(availability)).errorOption:
|
||||
return RestApiResponse.error(Http500, err.msg)
|
||||
|
@ -74,9 +74,7 @@ type
|
||||
# If false, the availability will not be able to receive new slots.
|
||||
# If it is turned on and the availability is already hosting slots,
|
||||
# it will not affect those existing slots.
|
||||
# It should be implicitly set to true when creating
|
||||
# a new availability. Therefore, it is not required when updating an existing availability.
|
||||
enabled* {.serialize.}: ?bool
|
||||
enabled* {.serialize.}: bool
|
||||
# 0 means non-restricted, otherwise contains timestamp until the Availability will be renewed
|
||||
until* {.serialize.}: SecondsSince1970
|
||||
|
||||
@ -138,7 +136,7 @@ proc init*(
|
||||
duration: UInt256,
|
||||
minPricePerBytePerSecond: UInt256,
|
||||
totalCollateral: UInt256,
|
||||
enabled: ?bool,
|
||||
enabled: bool,
|
||||
until: SecondsSince1970,
|
||||
): Availability =
|
||||
var id: array[32, byte]
|
||||
@ -282,9 +280,7 @@ proc updateAvailability(
|
||||
trace "Creating new Availability"
|
||||
let res = await self.updateImpl(obj)
|
||||
# inform subscribers that Availability has been added
|
||||
without var enabled =? obj.enabled:
|
||||
enabled = oldAvailability.enabled |? true
|
||||
if enabled and onAvailabilityAdded =? self.onAvailabilityAdded:
|
||||
if obj.enabled and onAvailabilityAdded =? self.onAvailabilityAdded:
|
||||
await onAvailabilityAdded(obj)
|
||||
return res
|
||||
else:
|
||||
@ -310,9 +306,7 @@ proc updateAvailability(
|
||||
|
||||
let res = await self.updateImpl(obj)
|
||||
|
||||
without var enabled =? obj.enabled:
|
||||
enabled = oldAvailability.enabled |? true
|
||||
if enabled and oldAvailability.freeSize < obj.freeSize: # availability added
|
||||
if obj.enabled and oldAvailability.freeSize < obj.freeSize: # availability added
|
||||
# inform subscribers that Availability has been modified (with increased
|
||||
# size)
|
||||
if onAvailabilityAdded =? self.onAvailabilityAdded:
|
||||
@ -397,7 +391,7 @@ proc createAvailability*(
|
||||
size, duration, minPricePerBytePerSecond, totalCollateral, enabled, until
|
||||
|
||||
let availability = Availability.init(
|
||||
size, size, duration, minPricePerBytePerSecond, totalCollateral, enabled.some, until
|
||||
size, size, duration, minPricePerBytePerSecond, totalCollateral, enabled, until
|
||||
)
|
||||
let bytes = availability.freeSize.truncate(uint)
|
||||
|
||||
@ -652,14 +646,14 @@ proc findAvailability*(
|
||||
let endTime = getTime().toUnix() + cast[int64](duration)
|
||||
for item in storables.items:
|
||||
if bytes =? (await item) and availability =? Availability.fromJson(bytes):
|
||||
let enabled = availability.enabled |? true
|
||||
if enabled and size <= availability.freeSize and duration <= availability.duration and
|
||||
if availability.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 = enabled,
|
||||
enabled = availability.enabled,
|
||||
size,
|
||||
availFreeSize = availability.freeSize,
|
||||
duration,
|
||||
|
@ -65,7 +65,7 @@ proc example*(
|
||||
duration = uint16.example.u256,
|
||||
minPricePerBytePerSecond = uint8.example.u256,
|
||||
totalCollateral = totalSize * collateralPerByte,
|
||||
enabled = some true,
|
||||
enabled = true,
|
||||
until = 0.SecondsSince1970,
|
||||
)
|
||||
|
||||
|
@ -67,10 +67,10 @@ asyncchecksuite "Reservations module":
|
||||
|
||||
test "generates unique ids for storage availability":
|
||||
let availability1 = Availability.init(
|
||||
1.u256, 2.u256, 3.u256, 4.u256, 5.u256, some true, 0.SecondsSince1970
|
||||
1.u256, 2.u256, 3.u256, 4.u256, 5.u256, true, 0.SecondsSince1970
|
||||
)
|
||||
let availability2 = Availability.init(
|
||||
1.u256, 2.u256, 3.u256, 4.u256, 5.u256, some true, 0.SecondsSince1970
|
||||
1.u256, 2.u256, 3.u256, 4.u256, 5.u256, true, 0.SecondsSince1970
|
||||
)
|
||||
check availability1.id != availability2.id
|
||||
|
||||
@ -265,20 +265,20 @@ asyncchecksuite "Reservations module":
|
||||
check isOk await reservations.update(availability)
|
||||
check (repo.quotaReservedBytes - origQuota) == 100.NBytes
|
||||
|
||||
test "enabled is updated correctly":
|
||||
test "create availability set enabled to true by default":
|
||||
let availability = createAvailability()
|
||||
check availability.enabled == true
|
||||
|
||||
check availability.enabled.get == true
|
||||
test "create availability set until to 0 by default":
|
||||
let availability = createAvailability()
|
||||
check availability.until == 0.SecondsSince1970
|
||||
|
||||
check isOk await reservations.update(availability)
|
||||
let key = availability.key.get
|
||||
var updated = !(await reservations.get(key, Availability))
|
||||
check updated.enabled.get == true
|
||||
test "create availability whith correct values":
|
||||
var until = cast[SecondsSince1970](getTime().toUnix())
|
||||
|
||||
availability.enabled = false.some
|
||||
check isOk await reservations.update(availability)
|
||||
updated = !(await reservations.get(key, Availability))
|
||||
check updated.enabled.get == false
|
||||
let availability = createAvailability(enabled = false, until = until)
|
||||
check availability.enabled == false
|
||||
check availability.until == until
|
||||
|
||||
test "reservation can be partially released":
|
||||
let availability = createAvailability()
|
||||
@ -362,7 +362,7 @@ asyncchecksuite "Reservations module":
|
||||
check found.isSome
|
||||
check found.get == availability
|
||||
|
||||
test "availabilities cannot be found when it is not enabled":
|
||||
test "does not find an availability when is it disabled":
|
||||
let availability = createAvailability(enabled = false)
|
||||
|
||||
let found = await reservations.findAvailability(
|
||||
|
@ -425,7 +425,7 @@ asyncchecksuite "Sales":
|
||||
createAvailability(enabled = true)
|
||||
await market.requestStorage(request)
|
||||
|
||||
availability.enabled = some false
|
||||
availability.enabled = false
|
||||
discard await reservations.update(availability)
|
||||
|
||||
check wasIgnored()
|
||||
|
@ -267,7 +267,8 @@ marketplacesuite "Simulate invalid proofs":
|
||||
# duration=totalPeriods.periods.u256,
|
||||
# minPricePerBytePerSecond=minPricePerBytePerSecond,
|
||||
# totalCollateral=slotSize * minPricePerBytePerSecond,
|
||||
# enabled = true.some
|
||||
# enabled = true.some,
|
||||
# until = 0.SecondsSince1970.some,
|
||||
# )
|
||||
|
||||
# let cid = client0.upload(data).get
|
||||
|
@ -21,8 +21,6 @@ multinodesuite "Sales":
|
||||
providers: CodexConfigs.init(nodes = 1).some,
|
||||
)
|
||||
|
||||
let minPricePerBytePerSecond = 1.u256
|
||||
|
||||
var host: CodexClient
|
||||
var client: CodexClient
|
||||
|
||||
@ -54,36 +52,7 @@ multinodesuite "Sales":
|
||||
).get
|
||||
check availability in host.getAvailabilities().get
|
||||
|
||||
test "created correctly an availability when not enabled by default", salesConfig:
|
||||
let totalSize = 12.u256
|
||||
let minPricePerBytePerSecond = 1.u256
|
||||
let totalCollateral = totalSize * minPricePerBytePerSecond
|
||||
let availability = host.postAvailability(
|
||||
totalSize = totalSize,
|
||||
duration = 2.u256,
|
||||
minPricePerBytePerSecond = minPricePerBytePerSecond,
|
||||
totalCollateral = totalCollateral,
|
||||
enabled = false.some,
|
||||
).get
|
||||
check availability.enabled == false.some
|
||||
|
||||
test "create availability fails when until is negative", salesConfig:
|
||||
let totalSize = 12.u256
|
||||
let minPricePerBytePerSecond = 1.u256
|
||||
let totalCollateral = totalSize * minPricePerBytePerSecond
|
||||
let response = host.postAvailabilityRaw(
|
||||
totalSize = totalSize,
|
||||
duration = 2.u256,
|
||||
minPricePerBytePerSecond = minPricePerBytePerSecond,
|
||||
totalCollateral = totalCollateral,
|
||||
until = cast[SecondsSince1970](-1).some,
|
||||
)
|
||||
|
||||
check:
|
||||
response.status == "400 Bad Request"
|
||||
response.body == "Until parameter must be greater or equal 0. Got: -1"
|
||||
|
||||
test "create availability fails when until is negative", salesConfig:
|
||||
test "creating availability fails when until is negative", salesConfig:
|
||||
let totalSize = 12.u256
|
||||
let minPricePerBytePerSecond = 1.u256
|
||||
let totalCollateral = totalSize * minPricePerBytePerSecond
|
||||
@ -116,11 +85,15 @@ multinodesuite "Sales":
|
||||
totalCollateral = 300.u256,
|
||||
).get
|
||||
|
||||
var until = cast[SecondsSince1970](getTime().toUnix())
|
||||
|
||||
host.patchAvailability(
|
||||
availability.id,
|
||||
duration = 100.u256.some,
|
||||
minPricePerBytePerSecond = 2.u256.some,
|
||||
totalCollateral = 200.u256.some,
|
||||
enabled = false.some,
|
||||
until = until.some,
|
||||
)
|
||||
|
||||
let updatedAvailability = (host.getAvailabilities().get).findItem(availability).get
|
||||
@ -129,6 +102,8 @@ multinodesuite "Sales":
|
||||
check updatedAvailability.totalCollateral == 200
|
||||
check updatedAvailability.totalSize == 140000
|
||||
check updatedAvailability.freeSize == 140000
|
||||
check updatedAvailability.enabled == false
|
||||
check updatedAvailability.until == until
|
||||
|
||||
test "updating availability - freeSize is not allowed to be changed", salesConfig:
|
||||
let availability = host.postAvailability(
|
||||
@ -154,32 +129,6 @@ multinodesuite "Sales":
|
||||
check updatedAvailability.totalSize == 100000
|
||||
check updatedAvailability.freeSize == 100000
|
||||
|
||||
test "updating availability - updating enabled", salesConfig:
|
||||
let availability = host.postAvailability(
|
||||
totalSize = 140000.u256,
|
||||
duration = 200.u256,
|
||||
minPricePerBytePerSecond = 3.u256,
|
||||
totalCollateral = 300.u256,
|
||||
enabled = true.some,
|
||||
).get
|
||||
host.patchAvailability(availability.id, enabled = false.some)
|
||||
let updatedAvailability = (host.getAvailabilities().get).findItem(availability).get
|
||||
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:
|
||||
let originalSize = 0xFFFFFF.u256
|
||||
|
Loading…
x
Reference in New Issue
Block a user