From 3a3d083345e8f305060448c9e695d5f81e595b8f Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 2 Aug 2022 14:21:12 +0200 Subject: [PATCH] [contracts] Replace size by slotSize --- codex/contracts/requests.nim | 17 ++++++++++------- codex/node.nim | 3 ++- codex/sales.nim | 2 +- tests/codex/testpurchasing.nim | 7 +++++-- tests/codex/testsales.nim | 6 +++--- tests/contracts/examples.nim | 6 +++--- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/codex/contracts/requests.nim b/codex/contracts/requests.nim index e95722e1..421fe87a 100644 --- a/codex/contracts/requests.nim +++ b/codex/contracts/requests.nim @@ -13,11 +13,11 @@ type expiry*: UInt256 nonce*: array[32, byte] StorageAsk* = object - size*: UInt256 + slots*: uint64 + slotSize*: UInt256 duration*: UInt256 proofProbability*: UInt256 reward*: UInt256 - slots*: uint64 StorageContent* = object cid*: string erasure*: StorageErasure @@ -40,11 +40,11 @@ func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest = func fromTuple(_: type StorageAsk, tupl: tuple): StorageAsk = StorageAsk( - size: tupl[0], - duration: tupl[1], - proofProbability: tupl[2], - reward: tupl[3], - slots: tupl[4] + slots: tupl[0], + slotSize: tupl[1], + duration: tupl[2], + proofProbability: tupl[3], + reward: tupl[4] ) func fromTuple(_: type StorageContent, tupl: tuple): StorageContent = @@ -135,3 +135,6 @@ func price*(ask: StorageAsk): UInt256 = func price*(request: StorageRequest): UInt256 = request.ask.price + +func size*(ask: StorageAsk): UInt256 = + ask.slots.u256 * ask.slotSize diff --git a/codex/node.nim b/codex/node.nim index eab72388..84716471 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -260,7 +260,8 @@ proc requestStorage*(self: CodexNodeRef, let request = StorageRequest( ask: StorageAsk( - size: encoded.size.u256, + slots: nodes + tolerance, + slotSize: (encoded.blockSize * encoded.steps).u256, duration: duration, reward: reward ), diff --git a/codex/sales.nim b/codex/sales.nim index 8363ae36..12b0f051 100644 --- a/codex/sales.nim +++ b/codex/sales.nim @@ -95,7 +95,7 @@ func remove*(sales: Sales, availability: Availability) = func findAvailability(sales: Sales, ask: StorageAsk): ?Availability = for availability in sales.available: - if ask.size <= availability.size and + if ask.slotSize <= availability.size and ask.duration <= availability.duration and ask.pricePerSlot >= availability.minPrice: return some availability diff --git a/tests/codex/testpurchasing.nim b/tests/codex/testpurchasing.nim index d54dc61a..45bc4a05 100644 --- a/tests/codex/testpurchasing.nim +++ b/tests/codex/testpurchasing.nim @@ -20,16 +20,19 @@ suite "Purchasing": purchasing = Purchasing.new(market, clock) request = StorageRequest( ask: StorageAsk( + slots: uint8.example.uint64, + slotSize: uint32.example.u256, duration: uint16.example.u256, - size: uint32.example.u256, + reward: uint8.example.u256 ) ) test "submits a storage request when asked": discard purchasing.purchase(request) let submitted = market.requested[0] + check submitted.ask.slots == request.ask.slots + check submitted.ask.slotSize == request.ask.slotSize check submitted.ask.duration == request.ask.duration - check submitted.ask.size == request.ask.size check submitted.ask.reward == request.ask.reward test "remembers purchases": diff --git a/tests/codex/testsales.nim b/tests/codex/testsales.nim index a7e717bf..589a054e 100644 --- a/tests/codex/testsales.nim +++ b/tests/codex/testsales.nim @@ -14,10 +14,10 @@ suite "Sales": ) var request = StorageRequest( ask: StorageAsk( + slots: 4, + slotSize: 100.u256, duration: 60.u256, - size: 100.u256, reward: 10.u256, - slots: 4 ), content: StorageContent( cid: "some cid" @@ -73,7 +73,7 @@ suite "Sales": test "ignores request when no matching storage is available": sales.add(availability) var tooBig = request - tooBig.ask.size = request.ask.size + 1 + tooBig.ask.slotSize = request.ask.slotSize + 1 discard await market.requestStorage(tooBig) check sales.available == @[availability] diff --git a/tests/contracts/examples.nim b/tests/contracts/examples.nim index ed3c6c46..072e56eb 100644 --- a/tests/contracts/examples.nim +++ b/tests/contracts/examples.nim @@ -13,11 +13,11 @@ proc example*(_: type StorageRequest): StorageRequest = StorageRequest( client: Address.example, ask: StorageAsk( - size: (1 * 1024 * 1024 * 1024).u256, # 1 Gigabyte + slots: 4, + slotSize: (1 * 1024 * 1024 * 1024).u256, # 1 Gigabyte duration: (10 * 60 * 60).u256, # 10 hours proofProbability: 4.u256, # require a proof roughly once every 4 periods - reward: 84.u256, - slots: 4 + reward: 84.u256 ), content: StorageContent( cid: "zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob",