← Eric Mastro

Protocol design · Logos Storage (formerly Codex) · 2025

Recoverability analysis

The analysis captures the ability of the sales process to recover in cases of crashes, exceptions, or cancellations. The document outlines two phases, both of which are to be implemented after RepoStore reservations have been removed.

TODO: describe Phase I vs Phase II changes

Phased changes

Phase I

Phase I will include changes to the simplification of the Marketplace sales design that do not include SalesOrders or any additional features. In addition, the Marketplace will use expiries and no explicit deletes.

RepoStore changes

Marketplace callback API changes

Phase II

Marketplace does not use expiries, but uses an explicit delete (locked). Phase II will include the simplification of the Marketplace sales design but no additional features.

RepoStore changes

RepoStore will add support for explicit deletes.

Marketplace callback API changes

onStore considerations

onStore is a long-running operation that may not finish before a request cancellation. If this happens, SaleDownloading.run will be cancelled, which will cancel the onStore call, so onStore will need to handle this exception appropriately.

Phase III

Phase III will include resumption of local state in the Marketplace.

Resumable downloads support

Depends on: Marketplace resumption of local state

for recovery (Download Manager), remembering to handle cancellations appropriately.

will be called and it will resume its operation based on the state.

Phase IV and beyond

Marketplace concurrency support

Depends on: Marketplace support for concurrent workers

stores/deletes (due to locking) by implementing a Marketplace-level dataset ref count.

Phase I: Expiries but no delete

Identify points in the sales state machine with crashes, exceptions, or cancellations in between, especially with RepoStore writes and expiry updates.

Preparing

---
config:
  layout: elk
---
flowchart LR
  GetRequestEnd["Get request end"] -->
  GetSlotState["Fetch slot state"] -- Free, Repair -->
  FindAvailability["Find availability"] -- Found -->
  SlotReserving

  GetSlotState -- Other state --> SaleIgnored
  FindAvailability -- Not found --> SaleIgnored

  GetRequestEnd -- Exception --> SaleErrored
  GetSlotState -- Exception --> SaleErrored
  FindAvailability -- Exception --> SaleErrored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed
SituationOutcome
CRASH at any pointThe slot is forfeited, with no recovery on startup since the slot was not filled.
EXCEPTION at any pointThe slot is forfeited.
CANCELLATION during "get request end", "fetch slot state", or "find availability"The slot is forfeited.

Slot Reserving

---
config:
  layout: elk
---
flowchart LR
  FetchCanReserve["Fetch canReserveSlot"] -- true -->
  ReserveSlot["Reserve slot"] -->
  SaleDownloading
  ReserveSlot -- Cannot reserve exception -->
  SaleIgnored
  ReserveSlot -- Other exception --> SaleErrored

  FetchCanReserve -- false --> SaleIgnored

  FetchCanReserve -- Exception --> SaleErrored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed
SituationOutcome
CRASH at any pointThe slot is forfeited, with no recovery on startup since the slot was not filled
EXCEPTION at any pointThe slot is forfeited.
CANCELLATION during canReserveSlot or reserveSlotThe slot is forfeited.

Downloading

It is important to note that the Marketplace does not update the expiry directly. Updating of the dataset expiry is done indirectly via onStore(expiry). However, consideration of expiration update success is still needed to analyse recoverability.

---
config:
  layout: elk
---
flowchart LR
  FetchSlotState["Fetch slot state"] -- isRepairing -->
  OnStore["onStore(isRepairing)"] -->
  SaleInitialProving

  OnStore -- Exception --> SaleErrored
  FetchSlotState -- Exception --> SaleErrored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed
SituationOutcome
No crash/exception in onStore(expiry)Success
CRASH before onStore(expiry)The slot is forfeited, with no recovery on startup since the slot was not filled.
CRASH in onStore(expiry)The slot is forfeited, with no recovery on startup since the slot was not filled. The dataset will be eligible for cleanup at the request expiry.
CRASH after onStore(expiry) but before the transition to SaleInitialProvingThe slot is forfeited, with no recovery on startup since the slot was not filled. The dataset will be eligible for cleanup at the request expiry.
EXCEPTION before onStore(expiry)Goes to SaleErrored. The slot is forfeited.
EXCEPTION in onStore(expiry)Goes to SaleErrored. The slot is forfeited. The dataset will be eligible for cleanup at the request expiry.
EXCEPTION after onStore(expiry) but before the transition to SaleInitialProvingThe slot is forfeited. The dataset will be eligible for cleanup at the request expiry.
CANCELLATION while fetching slot stateThe slot is forfeited.
CANCELLATION during onStoreThe slot is forfeited. The dataset will be eligible for cleanup at the request expiry.

Initial proving

---
config:
  layout: elk
---
flowchart LR
  WaitForStableChallenge["Wait for stable challenge"] -->
  GetChallenge["Get challenge"] -- challenge -->
  onProve["onProve(slot, challenge)"] -->
  SaleFilling

  WaitForStableChallenge -- Exception --> SaleErrored
  GetChallenge -- Exception --> SaleErrored
  onProve -- Exception --> SaleErrored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed
SituationOutcome
CRASH at any pointThe slot is forfeited.
EXCEPTION at any pointGoes to SaleErrored. The slot is forfeited.
CANCELLATION during "wait for stable challenge", "get challenge", or onProveThe slot is forfeited.

Filling

---
config:
  layout: elk
---
flowchart LR
  GetCollateral["Get collateral to fill slot"] -- collateral -->
  FillSlot["fillSlot(collateral)"] -->
  SaleFilled

  GetCollateral -- Exception --> SaleErrored
  FillSlot -- Exception --> SaleErrored
  FillSlot -- Slot is not free (filled by other host) --> SaleIgnored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed
SituationOutcomeSolution
fillSlot(collateral) successfulSUCCESS
CRASH before fillSlot(collateral) completesThe slot is forfeited, with no recovery on startup since the slot was not filled. On restart, the dataset will be eligible for cleanup by the maintainer on request expiry.
CRASH before state transitions to SaleFilledOn chain state is restored at startup, starting in the SaleFilled state, which extends the expiry. Since the expiry wasn't extended before the crash, the dataset is at risk of being cleanup by the maintainer. This requires the maintenance module to wait for state restoration to complete.
EXCEPTION during fillSlot(collateral)Goes to SaleErrored. The slot is forfeited. The dataset will be eligible for cleanup by the maintainer on request expiry.For network errors, check the slot state as it may have been filled. Implement retry functionality, with exponential backoff.
EXCEPTION before state transitions to SaleFilledNo recovery options here. Since the slot is already filled, the SP is required to provide proofs and will likely be slashed for missing proofs.This would be an exception raised in the state machine, but not in the SaleFilling state. Because the state machine itself does not raise any exceptions, this is unlikely.
CANCELLATION during "get collateral to fill slot"The slot is forfeited. The dataset will be eligible for cleanup by the maintainer on request expiry.
CANCELLATION during fillSlotIf the slot had not been filled on chain, the slot is forfeited and the dataset will be eligible for cleanup by the maintainer on request expiry. However, if the slot was filled on chain, then there are no recovery options and the SP will likely miss required storage proofs, leading to slashing(s).Possible options include:<br>1. Catch cancellation, check slot state. If slot is filled, raise a Defect, crashing the node and forcing a restart. On startup, slot will enter the SaleFilled state.<br>2. Log an error and increment a metric counter.<br>3. Do not propagate the cancellation. The sale will move to the SaleFilled state. Waiters (eg cancelAndWait may wait indefinitely).

Filled

---
config:
  layout: elk
---
flowchart LR
  GetSlotHost["Get slot host"] -- Slot filled by me -->
  GetRequestEnd["Get request end"] -- requestEnd -->
  UpdateExpiry["Update dataset expiry:<br>onExpiryUpdate(requestEnd)"] -->
  SaleProving

  GetSlotHost -- Exception --> SaleErrored
  GetRequestEnd -- Exception --> SaleErrored
  UpdateExpiry -- Exception --> SaleErrored
  GetSlotHost -- Slot filled by other host --> SaleErrored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed
SituationOutcomeSolution
Dataset expiry updated to requestEndSUCCESS
CRASH before onExpiryUpdate(requestEnd) completesOn chain state is restored at startup, starting in the SaleFilled state, which extends the expiry. Since the expiry wasn't completely extended before the crash, the dataset is at risk of being cleanup by the maintainer.This requires the maintenance module to wait for state restoration to complete.
CRASH before transition to SaleProving stateOn chain state is restored at startup, starting in the SaleFilled state, which extends the expiry, then moves to SaleProving. Dataset is not a risk of getting cleaned up since its expiry was updated.
EXCEPTION during "get slot host" or "get request end" (ie a network-level failure)Goes to SaleErrored. No recovery options. SP will not submit proofs when required and will eventually be slashed.For network exceptions, implement retry functionality, with exponential backoff. For other exceptions, go to SaleErrored. Exceptions may include any errors resulting from the JSON RPC call, or errors originating in ethers.
EXCEPTION during onExpiryUpdateGoes to SaleErrored. No recovery options. SP will not submit proofs when required and will eventually be slashed.Possible options include:<br>1. Raise a Defect, crashing the node and forcing a restart. On startup, slot will enter the SaleFilled state and will retry expiry update. May create an infinite crashing loop.<br>2. Log an error and increment a metric counter.<br>3. Continue to the SaleProving state. Dataset will be eligible for cleanup after request expiry, potentially causing slashings.
CANCELLATION during "get slot host", "get request end"Goes to SaleErrored. No recovery options. SP will not submit proofs when required and will be at risk of being slashed.Possible options include:<br>1. Raise a Defect, crashing the node and forcing a restart. On startup, slot will enter the SaleFilled state.<br>2. Log an error and increment a metric counter.<br>2. Do not propagate the cancellation. The sale will move to the SaleProving state. Waiters (eg cancelAndWait may wait indefinitely).

Proving

---
config:
  layout: elk
---
flowchart TB
  GetCurrentPeriod["Get current period"] -->
  GetSlotState["Get slot state"] -- Filled, proof required -->
  GenProof["Generate proof: onProve"] -- proof -->
  SubmitProof["submitProof(proof)"] -->
  WaitUntilNextPeriod["Wait until start of<br>next period"] -->
  GetCurrentPeriod

  GetSlotState -- Filled, proof not required --> WaitUntilNextPeriod
  GetSlotState -- Cancelled --> WaitForCancelled["Do nothing, wait for<br>elapsed cancelled timer"] --> WaitUntilNextPeriod
  GetSlotState -- Repair --> SaleErrored
  GetSlotState -- Failed --> WaitForFailed["Do nothing, wait for<br>RequestFailed contract<br>event"] --> WaitUntilNextPeriod
  GetSlotState -- Finished --> SalePayout
  GetSlotState -- Free, Paid --> SaleErrored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed

  GenProof -- Exception --> WaitUntilNextPeriod
  SubmitProof -- Exception --> WaitUntilNextPeriod
  GetCurrentPeriod -- Exception --> SaleErrored
  GetSlotState -- Exception --> SaleErrored
  GetSlotState -- Filled, exception getting<br>proof requirement<br>or challenge --> SaleErrored
  WaitUntilNextPeriod -- Exception --> SaleErrored
SituationOutcomeSolution
CRASH at any pointOn chain state is restored at startup, starting in SaleFilled state which extends the expiry to request end (no op), then moves back to SaleProving.
EXCEPTION during getSlotState, waitForNextPeriod, getCurrentPeriod, or getChallengeGoes to SaleErrored, all proving is stopped and SP becomes at risk of being slashed.For network exceptions, implement retry functionality, with exponential backoff. For other exceptions, go to SaleErrored. Exceptions may include any errors resulting from the JSON RPC call, or errors originating in ethers.
CANCELLATION during getSlotState, waitForNextPeriod, getCurrentPeriod, or getChallengeGoes to SaleErrored, all proving is stopped and SP becomes at risk of being slashed.Possible options include:<br>1. Raise a Defect, crashing the node and forcing a restart. On startup, slot will enter the SaleFilled state, then move to SaleProving.<br>2. Log an error and increment a metric counter.<br>2. Do not propagate the cancellation. The sale will continue in the SaleProving state. Waiters (eg cancelAndWait may wait indefinitely).

Payout

---
config:
  layout: elk
---
flowchart LR
  freeSlot -->
  SaleFinished

  freeSlot -- Exception --> SaleErrored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed
SituationOutcomeSolution
freeSlot is successfulSUCCESS
CRASH before freeSlot completesOn chain state is restored at startup, starting in SalePayout, where freeSlot will be tried again.
CRASH after freeSlot completesSlot is no longer part of mySlots, so on startup, slot state will not be restored.
EXCEPTION during freeSlotGoes to SaleErrored. If freeSlot succeeded before the exception, no clean up routines will be performed. If freeSlot did not succeed before the exception, no funds will have been paid out and there are no recovery options.For network exceptions, implement retry functionality with exponential backoff.
CANCELLATION during freeSlotGoes to SaleErrored. If freeSlot succeeded before the cancellation, no clean up routines will be performed. If freeSlot did not succeed before the cancellation, no funds will have been paid out and there are no recovery options.

Finished

---
config:
  layout: elk
---
flowchart LR
  onClear -->
  onCleanUp

  onClear -- Exception --> SaleErrored
  onCleanUp -- Exception --> SaleErrored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed
SituationOutcome
onClear and onCleanUp is successfulSUCCESS
CRASH before onCleanUp completesOn restart, slot will have been removed from mySlot, so nothing should happen. Anything missed in onCleanUp will not be recoverable.
EXCEPTION during onCleanUpGoes to SaleErrored. Since onCleanUp cleans up SalesAgents, there could be a memory leak.
CANCELLATION during onCleanUpSince onCleanUp cleans up SalesAgents, there could be a memory leak.

Phase II: No expiries but deletes

Depends on: SalesOrder "baseline" design implementation.

Identify points of SalesOrder updates and RepoStore writes, with crashes, exceptions, or cancellations in between.

Downloading

---
config:
  layout: elk
---
flowchart LR
  FetchSlotState["Fetch slot state"] -- isRepairing -->
  CreateSalesOrder["Create SalesOrder"] -->
  OnStore["onStore(isRepairing)"] -->
  SaleInitialProving

  OnStore -- Exception --> SaleErrored
  FetchSlotState -- Exception --> SaleErrored
  CreateSalesOrder -- Exception --> SaleErrored

  OnCancelled["Cancelled timer elapsed"] --> SaleCancelled
  OnFailed["RequestFailed contract event"] --> SaleFailed
SituationOutcome
No crash/exception in onStore(expiry)Success
CRASH before create SalesOrderThe slot is forfeited, with no recovery on startup since the slot was not filled.
CRASH before onStore(expiry)The slot is forfeited, with no recovery on startup since the slot was not filled. On restart, the SalesOrder will be archived.
CRASH in onStore(expiry)The slot is forfeited, with no recovery on startup since the slot was not filled. On restart, the dataset will be cleaned up (corrective cleanup) and the SalesOrder archived.
CRASH after onStore(expiry) but before the transition to SaleInitialProvingThe slot is forfeited, with no recovery on startup since the slot was not filled. On restart, the dataset will be cleaned up (corrective cleanup) and the SalesOrder archived.
EXCEPTION before create SalesOrderGoes to SaleErrored. The slot is forfeited.
EXCEPTION before onStore(expiry)Goes to SaleErrored. The slot is forfeited. The SalesOrder will be archived.
EXCEPTION in onStore(expiry)Goes to SaleErrored. The slot is forfeited. The dataset will be cleaned up (active cleanup) and the SalesOrder archived.
EXCEPTION after onStore(expiry) but before the transition to SaleInitialProvingThe slot is forfeited. The dataset will be cleaned up (active cleanup) and the SalesOrder archived.
CANCELLATION while fetching slot stateThe slot is forfeited.
CANCELLATION during onStoreThe slot is forfeited. The dataset will be cleaned up (active cleanup) and the SalesOrder archived.

SalesOrders are created in the downloading state before any data is written to disk.

  1. [DOWNLOADING] SalesOrder is created -> data downloaded in onStore: SUCCESS
  2. [DOWNLOADING] CRASH -> SalesOrder is created -> data downloaded in onStore:

SUCCESS

and no blocks downloaded

  1. [DOWNLOADING] SalesOrder is created -> CRASH -> data downloaded in onStore

exist) and the slot will be forfeited.

any datasets

  1. [DOWNLOADING] SalesOrder is created -> CRASH while data downloading in

onStore

Deleting

Deletes can occur at any final state of the state machine: failed, finished, errored, cancelled. SalesOrders are archived only after the dataset is deleted. [FAILED] Dataset deleted -> SalesOrder archived: SUCCESS [FAILED] Dataset deleted -> CRASH -> SalesOrder archived

corrective cleanup is run, deleting the dataset (which is already deleted) and finally archiving the SalesOrder. [FAILED] Dataset deleted -> SalesOrder archived -> CRASH: SUCCESS

If the node goes down and corrupts the (eg power failure) in mid-write of the RepoStore a

Written while leading the marketplace team at Codex. Storage provider nodes run unattended on hardware nobody controls, so every state in the sales machine had to have a defined outcome under power loss, process crash, asynchronous exception and cancellation. Remediation was phased across four releases rather than attempted at once, and the findings ultimately drove the redesigned sales and purchasing architecture.

← Back   How I work →   Gas analysis →   Slot reservations →