[sales] rename: negotiation -> sales agent
Reason: with the new design there are no offers and selection, making this less like a negotation, and more like an agent trying to fulfill a storage request
This commit is contained in:
parent
96ca1de768
commit
e06211f827
|
@ -23,7 +23,7 @@ type
|
||||||
size*: UInt256
|
size*: UInt256
|
||||||
duration*: UInt256
|
duration*: UInt256
|
||||||
minPrice*: UInt256
|
minPrice*: UInt256
|
||||||
Negotiation = ref object
|
SalesAgent = ref object
|
||||||
sales: Sales
|
sales: Sales
|
||||||
requestId: array[32, byte]
|
requestId: array[32, byte]
|
||||||
ask: StorageAsk
|
ask: StorageAsk
|
||||||
|
@ -73,54 +73,54 @@ func findAvailability(sales: Sales, ask: StorageAsk): ?Availability =
|
||||||
ask.maxPrice >= availability.minPrice:
|
ask.maxPrice >= availability.minPrice:
|
||||||
return some availability
|
return some availability
|
||||||
|
|
||||||
proc finish(negotiation: Negotiation, success: bool) =
|
proc finish(agent: SalesAgent, success: bool) =
|
||||||
if negotiation.finished:
|
if agent.finished:
|
||||||
return
|
return
|
||||||
|
|
||||||
negotiation.finished = true
|
agent.finished = true
|
||||||
|
|
||||||
if subscription =? negotiation.subscription:
|
if subscription =? agent.subscription:
|
||||||
asyncSpawn subscription.unsubscribe()
|
asyncSpawn subscription.unsubscribe()
|
||||||
|
|
||||||
if running =? negotiation.running:
|
if running =? agent.running:
|
||||||
running.cancel()
|
running.cancel()
|
||||||
|
|
||||||
if waiting =? negotiation.waiting:
|
if waiting =? agent.waiting:
|
||||||
waiting.cancel()
|
waiting.cancel()
|
||||||
|
|
||||||
if success and request =? negotiation.request:
|
if success and request =? agent.request:
|
||||||
if onSale =? negotiation.sales.onSale:
|
if onSale =? agent.sales.onSale:
|
||||||
onSale(negotiation.availability, request)
|
onSale(agent.availability, request)
|
||||||
else:
|
else:
|
||||||
negotiation.sales.add(negotiation.availability)
|
agent.sales.add(agent.availability)
|
||||||
|
|
||||||
proc onFulfill(negotiation: Negotiation, requestId: array[32, byte]) {.async.} =
|
proc onFulfill(agent: SalesAgent, requestId: array[32, byte]) {.async.} =
|
||||||
try:
|
try:
|
||||||
let market = negotiation.sales.market
|
let market = agent.sales.market
|
||||||
let host = await market.getHost(requestId)
|
let host = await market.getHost(requestId)
|
||||||
let me = await market.getSigner()
|
let me = await market.getSigner()
|
||||||
negotiation.finish(success = (host == me.some))
|
agent.finish(success = (host == me.some))
|
||||||
except CatchableError:
|
except CatchableError:
|
||||||
negotiation.finish(success = false)
|
agent.finish(success = false)
|
||||||
|
|
||||||
proc subscribeFulfill(negotiation: Negotiation) {.async.} =
|
proc subscribeFulfill(agent: SalesAgent) {.async.} =
|
||||||
proc onFulfill(requestId: array[32, byte]) {.gcsafe, upraises:[].} =
|
proc onFulfill(requestId: array[32, byte]) {.gcsafe, upraises:[].} =
|
||||||
asyncSpawn negotiation.onFulfill(requestId)
|
asyncSpawn agent.onFulfill(requestId)
|
||||||
let market = negotiation.sales.market
|
let market = agent.sales.market
|
||||||
let subscription = await market.subscribeFulfillment(negotiation.requestId, onFulfill)
|
let subscription = await market.subscribeFulfillment(agent.requestId, onFulfill)
|
||||||
negotiation.subscription = some subscription
|
agent.subscription = some subscription
|
||||||
|
|
||||||
proc waitForExpiry(negotiation: Negotiation) {.async.} =
|
proc waitForExpiry(agent: SalesAgent) {.async.} =
|
||||||
without request =? negotiation.request:
|
without request =? agent.request:
|
||||||
return
|
return
|
||||||
await negotiation.sales.clock.waitUntil(request.expiry.truncate(int64))
|
await agent.sales.clock.waitUntil(request.expiry.truncate(int64))
|
||||||
negotiation.finish(success = false)
|
agent.finish(success = false)
|
||||||
|
|
||||||
proc start(negotiation: Negotiation) {.async.} =
|
proc start(agent: SalesAgent) {.async.} =
|
||||||
try:
|
try:
|
||||||
let sales = negotiation.sales
|
let sales = agent.sales
|
||||||
let market = sales.market
|
let market = sales.market
|
||||||
let availability = negotiation.availability
|
let availability = agent.availability
|
||||||
|
|
||||||
without retrieve =? sales.retrieve:
|
without retrieve =? sales.retrieve:
|
||||||
raiseAssert "retrieve proc not set"
|
raiseAssert "retrieve proc not set"
|
||||||
|
@ -130,14 +130,14 @@ proc start(negotiation: Negotiation) {.async.} =
|
||||||
|
|
||||||
sales.remove(availability)
|
sales.remove(availability)
|
||||||
|
|
||||||
await negotiation.subscribeFulfill()
|
await agent.subscribeFulfill()
|
||||||
|
|
||||||
negotiation.request = await market.getRequest(negotiation.requestId)
|
agent.request = await market.getRequest(agent.requestId)
|
||||||
without request =? negotiation.request:
|
without request =? agent.request:
|
||||||
negotiation.finish(success = false)
|
agent.finish(success = false)
|
||||||
return
|
return
|
||||||
|
|
||||||
negotiation.waiting = some negotiation.waitForExpiry()
|
agent.waiting = some agent.waitForExpiry()
|
||||||
|
|
||||||
await retrieve(request.content.cid)
|
await retrieve(request.content.cid)
|
||||||
let proof = await prove(request.content.cid)
|
let proof = await prove(request.content.cid)
|
||||||
|
@ -145,21 +145,21 @@ proc start(negotiation: Negotiation) {.async.} =
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
raise
|
raise
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
error "Negotiation failed", msg = e.msg
|
error "SalesAgent failed", msg = e.msg
|
||||||
negotiation.finish(success = false)
|
agent.finish(success = false)
|
||||||
|
|
||||||
proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) =
|
proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) =
|
||||||
without availability =? sales.findAvailability(ask):
|
without availability =? sales.findAvailability(ask):
|
||||||
return
|
return
|
||||||
|
|
||||||
let negotiation = Negotiation(
|
let agent = SalesAgent(
|
||||||
sales: sales,
|
sales: sales,
|
||||||
requestId: requestId,
|
requestId: requestId,
|
||||||
ask: ask,
|
ask: ask,
|
||||||
availability: availability
|
availability: availability
|
||||||
)
|
)
|
||||||
|
|
||||||
negotiation.running = some negotiation.start()
|
agent.running = some agent.start()
|
||||||
|
|
||||||
proc start*(sales: Sales) {.async.} =
|
proc start*(sales: Sales) {.async.} =
|
||||||
doAssert sales.subscription.isNone, "Sales already started"
|
doAssert sales.subscription.isNone, "Sales already started"
|
||||||
|
|
Loading…
Reference in New Issue