Skip to content

Commit

Permalink
Merge branch 'master' into feat/manifest/add-metadata-to-manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
2-towns committed Oct 24, 2024
2 parents 9afef44 + 0157ca4 commit a2a76e0
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 24 deletions.
13 changes: 12 additions & 1 deletion codex/contracts/market.nim
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,14 @@ method fillSlot(market: OnChainMarket,
proof: Groth16Proof,
collateral: UInt256) {.async.} =
convertEthersError:
logScope:
requestId
slotIndex

await market.approveFunds(collateral)
trace "calling fillSlot on contract"
discard await market.contract.fillSlot(requestId, slotIndex, proof).confirm(0)
trace "fillSlot transaction completed"

method freeSlot*(market: OnChainMarket, slotId: SlotId) {.async.} =
convertEthersError:
Expand Down Expand Up @@ -253,7 +259,12 @@ method reserveSlot*(
slotIndex: UInt256) {.async.} =

convertEthersError:
discard await market.contract.reserveSlot(requestId, slotIndex).confirm(0)
discard await market.contract.reserveSlot(
requestId,
slotIndex,
# reserveSlot runs out of gas for unknown reason, but 100k gas covers it
TransactionOverrides(gasLimit: some 100000.u256)
).confirm(0)

method canReserveSlot*(
market: OnChainMarket,
Expand Down
25 changes: 18 additions & 7 deletions codex/sales/states/filling.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import ./errorhandling
import ./filled
import ./cancelled
import ./failed
import ./ignored
import ./errored

logScope:
topics = "marketplace sales filling"
Expand All @@ -22,16 +24,25 @@ method onCancelled*(state: SaleFilling, request: StorageRequest): ?State =
method onFailed*(state: SaleFilling, request: StorageRequest): ?State =
return some State(SaleFailed())

method onSlotFilled*(state: SaleFilling, requestId: RequestId,
slotIndex: UInt256): ?State =
return some State(SaleFilled())

method run(state: SaleFilling, machine: Machine): Future[?State] {.async.} =
let data = SalesAgent(machine).data
let market = SalesAgent(machine).context.market
without (collateral =? data.request.?ask.?collateral):
raiseAssert "Request not set"

debug "Filling slot", requestId = data.requestId, slotIndex = data.slotIndex
await market.fillSlot(data.requestId, data.slotIndex, state.proof, collateral)
debug "Waiting for slot filled event...", requestId = $data.requestId, slotIndex = $data.slotIndex
logScope:
requestId = data.requestId
slotIndex = data.slotIndex

debug "Filling slot"
try:
await market.fillSlot(data.requestId, data.slotIndex, state.proof, collateral)
except MarketError as e:
if e.msg.contains "Slot is not free":
debug "Slot is already filled, ignoring slot"
return some State( SaleIgnored(reprocessSlot: false, returnBytes: true) )
else:
return some State( SaleErrored(error: e) )
# other CatchableErrors are handled "automatically" by the ErrorHandlingState

return some State(SaleFilled())
11 changes: 6 additions & 5 deletions codex/sales/states/slotreserving.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ method onCancelled*(state: SaleSlotReserving, request: StorageRequest): ?State =
method onFailed*(state: SaleSlotReserving, request: StorageRequest): ?State =
return some State(SaleFailed())

method onSlotFilled*(state: SaleSlotReserving, requestId: RequestId,
slotIndex: UInt256): ?State =
return some State(SaleFilled())

method run*(state: SaleSlotReserving, machine: Machine): Future[?State] {.async.} =
let agent = SalesAgent(machine)
let data = agent.data
Expand All @@ -48,7 +44,12 @@ method run*(state: SaleSlotReserving, machine: Machine): Future[?State] {.async.
trace "Reserving slot"
await market.reserveSlot(data.requestId, data.slotIndex)
except MarketError as e:
return some State( SaleErrored(error: e) )
if e.msg.contains "Reservation not allowed":
debug "Slot cannot be reserved, ignoring", error = e.msg
return some State( SaleIgnored(reprocessSlot: false, returnBytes: true) )
else:
return some State( SaleErrored(error: e) )
# other CatchableErrors are handled "automatically" by the ErrorHandlingState

trace "Slot successfully reserved"
return some State( SaleDownloading() )
Expand Down
2 changes: 1 addition & 1 deletion codex/utils/asyncstatemachine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ proc scheduler(machine: Machine) {.async.} =
await running.cancelAndWait()
let fromState = if machine.state.isNil: "<none>" else: $machine.state
machine.state = next
debug "enter state", state = machine.state, fromState
debug "enter state", state = fromState & " => " & $machine.state
running = machine.run(machine.state)
running
.track(machine)
Expand Down
4 changes: 0 additions & 4 deletions tests/codex/sales/states/testfilling.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,3 @@ checksuite "sales state 'filling'":
test "switches to failed state when request fails":
let next = state.onFailed(request)
check !next of SaleFailed

test "switches to filled state when slot is filled":
let next = state.onSlotFilled(request.id, slotIndex)
check !next of SaleFilled
4 changes: 0 additions & 4 deletions tests/codex/sales/states/testslotreserving.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ asyncchecksuite "sales state 'SlotReserving'":
let next = state.onFailed(request)
check !next of SaleFailed

test "switches to filled state when slot is filled":
let next = state.onSlotFilled(request.id, slotIndex)
check !next of SaleFilled

test "run switches to downloading when slot successfully reserved":
let next = await state.run(agent)
check !next of SaleDownloading
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/hardhatprocess.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ method onOutputLineCaptured(node: HardhatProcess, line: string) =
return

if error =? logFile.writeFile(line & "\n").errorOption:
error "failed to write to hardhat file", errorCode = error
error "failed to write to hardhat file", errorCode = $error
discard logFile.closeFile()
node.logFile = none IoHandle

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/nodeprocess.nim
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ method stop*(node: NodeProcess) {.base, async.} =
try:
trace "terminating node process..."
if errCode =? node.process.terminate().errorOption:
error "failed to terminate process", errCode
error "failed to terminate process", errCode = $errCode

trace "waiting for node process to exit"
let exitCode = await node.process.waitForExit(3.seconds)
Expand Down

0 comments on commit a2a76e0

Please sign in to comment.