From bf11ef5b550e1ff7d24adc4bdec8c96a46c291cb Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 1 Aug 2022 14:25:32 +0200 Subject: [PATCH] [sales] Use correct slot price --- codex/contracts/requests.nim | 8 +++++++- codex/sales.nim | 2 +- tests/codex/testsales.nim | 11 +++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/codex/contracts/requests.nim b/codex/contracts/requests.nim index 8a39ea9b..e95722e1 100644 --- a/codex/contracts/requests.nim +++ b/codex/contracts/requests.nim @@ -127,5 +127,11 @@ func slotId*(requestId: array[32, byte], slot: UInt256): array[32, byte] = func slotId*(request: StorageRequest, slot: UInt256): array[32, byte] = slotId(request.id, slot) +func pricePerSlot*(ask: StorageAsk): UInt256 = + ask.duration * ask.reward + +func price*(ask: StorageAsk): UInt256 = + ask.slots.u256 * ask.pricePerSlot + func price*(request: StorageRequest): UInt256 = - request.ask.slots.u256 * request.ask.duration * request.ask.reward + request.ask.price diff --git a/codex/sales.nim b/codex/sales.nim index 4e299ce2..8363ae36 100644 --- a/codex/sales.nim +++ b/codex/sales.nim @@ -97,7 +97,7 @@ func findAvailability(sales: Sales, ask: StorageAsk): ?Availability = for availability in sales.available: if ask.size <= availability.size and ask.duration <= availability.duration and - ask.reward >= availability.minPrice: + ask.pricePerSlot >= availability.minPrice: return some availability proc finish(agent: SalesAgent, success: bool) = diff --git a/tests/codex/testsales.nim b/tests/codex/testsales.nim index c4bac488..a7e717bf 100644 --- a/tests/codex/testsales.nim +++ b/tests/codex/testsales.nim @@ -10,13 +10,13 @@ suite "Sales": let availability = Availability.init( size=100.u256, duration=60.u256, - minPrice=42.u256 + minPrice=600.u256 ) var request = StorageRequest( ask: StorageAsk( duration: 60.u256, size: 100.u256, - reward:42.u256, + reward: 10.u256, slots: 4 ), content: StorageContent( @@ -77,6 +77,13 @@ suite "Sales": discard await market.requestStorage(tooBig) check sales.available == @[availability] + test "ignores request when reward is too low": + sales.add(availability) + var tooCheap = request + tooCheap.ask.reward = request.ask.reward - 1 + discard await market.requestStorage(tooCheap) + check sales.available == @[availability] + test "retrieves and stores data locally": var storingCid: string var storingAvailability: Availability