[contracts] Do not crash when contract interaction fails
Log error instead.
This commit is contained in:
parent
d2da225f21
commit
fae05fce86
|
@ -2,6 +2,7 @@ import std/sets
|
|||
import std/times
|
||||
import pkg/upraises
|
||||
import pkg/questionable
|
||||
import pkg/chronicles
|
||||
import ./por/timing/proofs
|
||||
|
||||
export sets
|
||||
|
@ -33,15 +34,18 @@ proc removeEndedContracts(proving: Proving) {.async.} =
|
|||
proving.contracts.excl(ended)
|
||||
|
||||
proc run(proving: Proving) {.async.} =
|
||||
while not proving.stopped:
|
||||
let currentPeriod = await proving.proofs.getCurrentPeriod()
|
||||
await proving.removeEndedContracts()
|
||||
for id in proving.contracts:
|
||||
if (await proving.proofs.isProofRequired(id)) or
|
||||
(await proving.proofs.willProofBeRequired(id)):
|
||||
if callback =? proving.onProofRequired:
|
||||
callback(id)
|
||||
await proving.proofs.waitUntilPeriod(currentPeriod + 1)
|
||||
try:
|
||||
while not proving.stopped:
|
||||
let currentPeriod = await proving.proofs.getCurrentPeriod()
|
||||
await proving.removeEndedContracts()
|
||||
for id in proving.contracts:
|
||||
if (await proving.proofs.isProofRequired(id)) or
|
||||
(await proving.proofs.willProofBeRequired(id)):
|
||||
if callback =? proving.onProofRequired:
|
||||
callback(id)
|
||||
await proving.proofs.waitUntilPeriod(currentPeriod + 1)
|
||||
except CatchableError as e:
|
||||
error "Proving failed", msg = e.msg
|
||||
|
||||
proc start*(proving: Proving) =
|
||||
asyncSpawn proving.run()
|
||||
|
|
|
@ -80,7 +80,6 @@ proc run(purchase: Purchase) {.async.} =
|
|||
|
||||
proc start(purchase: Purchase) =
|
||||
purchase.future = purchase.run()
|
||||
asyncSpawn purchase.future
|
||||
|
||||
proc wait*(purchase: Purchase) {.async.} =
|
||||
await purchase.future
|
||||
|
|
|
@ -4,6 +4,7 @@ import pkg/questionable
|
|||
import pkg/upraises
|
||||
import pkg/stint
|
||||
import pkg/nimcrypto
|
||||
import pkg/chronicles
|
||||
import ./market
|
||||
|
||||
export stint
|
||||
|
@ -111,16 +112,17 @@ proc waitForExpiry(negotiation: Negotiation) {.async.} =
|
|||
negotiation.finish(success = false)
|
||||
|
||||
proc start(negotiation: Negotiation) {.async.} =
|
||||
let sales = negotiation.sales
|
||||
let availability = negotiation.availability
|
||||
sales.remove(availability)
|
||||
await negotiation.sendOffer()
|
||||
await negotiation.subscribeSelect()
|
||||
negotiation.waiting = some negotiation.waitForExpiry()
|
||||
try:
|
||||
let sales = negotiation.sales
|
||||
let availability = negotiation.availability
|
||||
sales.remove(availability)
|
||||
await negotiation.sendOffer()
|
||||
await negotiation.subscribeSelect()
|
||||
negotiation.waiting = some negotiation.waitForExpiry()
|
||||
except CatchableError as e:
|
||||
error "Negotiation failed", msg = e.msg
|
||||
|
||||
proc handleRequest(sales: Sales,
|
||||
requestId: array[32, byte],
|
||||
ask: StorageAsk) {.async.} =
|
||||
proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) =
|
||||
without availability =? sales.findAvailability(ask):
|
||||
return
|
||||
|
||||
|
@ -137,14 +139,24 @@ proc start*(sales: Sales) =
|
|||
doAssert sales.subscription.isNone, "Sales already started"
|
||||
|
||||
proc onRequest(requestId: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} =
|
||||
asyncSpawn sales.handleRequest(requestId, ask)
|
||||
sales.handleRequest(requestId, ask)
|
||||
|
||||
proc subscribe {.async.} =
|
||||
sales.subscription = some await sales.market.subscribeRequests(onRequest)
|
||||
try:
|
||||
sales.subscription = some await sales.market.subscribeRequests(onRequest)
|
||||
except CatchableError as e:
|
||||
error "Unable to start sales", msg = e.msg
|
||||
|
||||
asyncSpawn subscribe()
|
||||
|
||||
proc stop*(sales: Sales) =
|
||||
if subscription =? sales.subscription:
|
||||
asyncSpawn subscription.unsubscribe()
|
||||
sales.subscription = Subscription.none
|
||||
|
||||
proc unsubscribe {.async.} =
|
||||
try:
|
||||
await subscription.unsubscribe()
|
||||
except CatchableError as e:
|
||||
warn "Unsubscribe failed", msg = e.msg
|
||||
|
||||
asyncSpawn unsubscribe()
|
||||
|
|
Loading…
Reference in New Issue