[marketplace] make availability optional for node restart

This commit is contained in:
Eric Mastro 2022-11-10 15:54:40 +11:00
parent e2ceb431e2
commit 0a4c1c7e22
No known key found for this signature in database
GPG Key ID: 141E3048D95A4E63
2 changed files with 22 additions and 18 deletions

View File

@ -323,7 +323,7 @@ proc start*(node: CodexNodeRef) {.async.} =
# TODO: remove Sales callbacks, pass BlockStore and StorageProofs instead # TODO: remove Sales callbacks, pass BlockStore and StorageProofs instead
contracts.sales.onStore = proc(request: StorageRequest, contracts.sales.onStore = proc(request: StorageRequest,
slot: UInt256, slot: UInt256,
availability: Availability) {.async.} = availability: ?Availability) {.async.} =
## store data in local storage ## store data in local storage
## ##
@ -344,7 +344,7 @@ proc start*(node: CodexNodeRef) {.async.} =
if fetchRes.isErr: if fetchRes.isErr:
raise newException(CodexError, "Unable to retrieve blocks") raise newException(CodexError, "Unable to retrieve blocks")
contracts.sales.onClear = proc(availability: Availability, contracts.sales.onClear = proc(availability: ?Availability,
request: StorageRequest, request: StorageRequest,
slotIndex: UInt256) = slotIndex: UInt256) =
# TODO: remove data from local storage # TODO: remove data from local storage

View File

@ -1,8 +1,8 @@
import std/sets import std/sets
import std/times
import pkg/asynctest import pkg/asynctest
import pkg/chronos import pkg/chronos
import pkg/codex/contracts/requests import pkg/codex/contracts/requests
import pkg/codex/proving
import pkg/codex/sales import pkg/codex/sales
import ./helpers/mockmarket import ./helpers/mockmarket
import ./helpers/mockclock import ./helpers/mockclock
@ -24,7 +24,8 @@ suite "Sales":
), ),
content: StorageContent( content: StorageContent(
cid: "some cid" cid: "some cid"
) ),
expiry: (getTime() + initDuration(hours=1)).toUnix.u256
) )
let proof = exampleProof() let proof = exampleProof()
@ -40,7 +41,7 @@ suite "Sales":
sales = Sales.new(market, clock, proving) sales = Sales.new(market, clock, proving)
sales.onStore = proc(request: StorageRequest, sales.onStore = proc(request: StorageRequest,
slot: UInt256, slot: UInt256,
availability: Availability) {.async.} = availability: ?Availability) {.async.} =
discard discard
sales.onProve = proc(request: StorageRequest, sales.onProve = proc(request: StorageRequest,
slot: UInt256): Future[seq[byte]] {.async.} = slot: UInt256): Future[seq[byte]] {.async.} =
@ -98,10 +99,11 @@ suite "Sales":
var storingAvailability: Availability var storingAvailability: Availability
sales.onStore = proc(request: StorageRequest, sales.onStore = proc(request: StorageRequest,
slot: UInt256, slot: UInt256,
availability: Availability) {.async.} = availability: ?Availability) {.async.} =
storingRequest = request storingRequest = request
storingSlot = slot storingSlot = slot
storingAvailability = availability check availability.isSome
storingAvailability = !availability
sales.add(availability) sales.add(availability)
let requested = await market.requestStorage(request) let requested = await market.requestStorage(request)
check storingRequest == requested check storingRequest == requested
@ -112,7 +114,7 @@ suite "Sales":
let error = newException(IOError, "data retrieval failed") let error = newException(IOError, "data retrieval failed")
sales.onStore = proc(request: StorageRequest, sales.onStore = proc(request: StorageRequest,
slot: UInt256, slot: UInt256,
availability: Availability) {.async.} = availability: ?Availability) {.async.} =
raise error raise error
sales.add(availability) sales.add(availability)
discard await market.requestStorage(request) discard await market.requestStorage(request)
@ -143,10 +145,11 @@ suite "Sales":
var soldAvailability: Availability var soldAvailability: Availability
var soldRequest: StorageRequest var soldRequest: StorageRequest
var soldSlotIndex: UInt256 var soldSlotIndex: UInt256
sales.onSale = proc(availability: Availability, sales.onSale = proc(availability: ?Availability,
request: StorageRequest, request: StorageRequest,
slotIndex: UInt256) = slotIndex: UInt256) =
soldAvailability = availability if a =? availability:
soldAvailability = a
soldRequest = request soldRequest = request
soldSlotIndex = slotIndex soldSlotIndex = slotIndex
sales.add(availability) sales.add(availability)
@ -164,10 +167,11 @@ suite "Sales":
var clearedAvailability: Availability var clearedAvailability: Availability
var clearedRequest: StorageRequest var clearedRequest: StorageRequest
var clearedSlotIndex: UInt256 var clearedSlotIndex: UInt256
sales.onClear = proc(availability: Availability, sales.onClear = proc(availability: ?Availability,
request: StorageRequest, request: StorageRequest,
slotIndex: UInt256) = slotIndex: UInt256) =
clearedAvailability = availability if a =? availability:
clearedAvailability = a
clearedRequest = request clearedRequest = request
clearedSlotIndex = slotIndex clearedSlotIndex = slotIndex
sales.add(availability) sales.add(availability)
@ -180,8 +184,8 @@ suite "Sales":
let otherHost = Address.example let otherHost = Address.example
sales.onStore = proc(request: StorageRequest, sales.onStore = proc(request: StorageRequest,
slot: UInt256, slot: UInt256,
availability: Availability) {.async.} = availability: ?Availability) {.async.} =
await sleepAsync(1.hours) await sleepAsync(chronos.hours(1))
sales.add(availability) sales.add(availability)
discard await market.requestStorage(request) discard await market.requestStorage(request)
for slotIndex in 0..<request.ask.slots: for slotIndex in 0..<request.ask.slots:
@ -191,17 +195,17 @@ suite "Sales":
test "makes storage available again when request expires": test "makes storage available again when request expires":
sales.onStore = proc(request: StorageRequest, sales.onStore = proc(request: StorageRequest,
slot: UInt256, slot: UInt256,
availability: Availability) {.async.} = availability: ?Availability) {.async.} =
await sleepAsync(1.hours) await sleepAsync(chronos.hours(1))
sales.add(availability) sales.add(availability)
discard await market.requestStorage(request) discard await market.requestStorage(request)
clock.set(request.expiry.truncate(int64)) clock.set(request.expiry.truncate(int64))
await sleepAsync(2.seconds) await sleepAsync(chronos.seconds(2))
check sales.available == @[availability] check sales.available == @[availability]
test "adds proving for slot when slot is filled": test "adds proving for slot when slot is filled":
var soldSlotIndex: UInt256 var soldSlotIndex: UInt256
sales.onSale = proc(availability: Availability, sales.onSale = proc(availability: ?Availability,
request: StorageRequest, request: StorageRequest,
slotIndex: UInt256) = slotIndex: UInt256) =
soldSlotIndex = slotIndex soldSlotIndex = slotIndex