# Task status flow

Understanding the status flow of tasks will help users understand the status of damocles-worker and perform targeted recovery of paused sector tasks.

The status flow of the task is related to the type of the task, that is, the plan option in sealing_thread. We will elaborate them separately .

In the description below, entries prefixed with State:: are states, and entries prefixed with Event:: are events or procedures.

State::A => {
	Event::B => State::C,
	Event::D => State::E,
}

means when the task is in A state

  1. If B event occurs, then go to C state
  2. If D event occurs, then go to E state

In addition…

  1. For each state, transitions to one or more other states may occur, that is, one or more lines may appear in {}

  2. In addition to the specific states listed, there are also some special states, such as:

  • State::Aborted, indicating that the sector did not complete normally

    1. In any logic evaluations, an exception belonging to the abort category will cause the task to change to the Aborted state. The task will be terminated, and the current sealing_thread will move onto the next task

    2. When the user sends the resume command to a paused task with the Aborted state is attached, the flow described above will still be taken into effect

      Therefore, users can use this mechanism to handle exceptions that are difficult to recover, but have not yet been defined as abort category

  • State::Finished indicates that the sector completed normally

# The state flow of sealer tasks

// Empty state, that is, the sector has not been allocated yet
State::Empty => {
	// apply for a new sector
	Event::Allocate(_) => State::Allocated,
},

// new sector is allocated
State::Allocated => {
	// request a deal
	Event::AcquireDeals(_) => State::DealsAcquired,
},

// deal has been applied
State::DealsAcquired => {
	// fill piece data
	Event::AddPiece(_) => State::PieceAdded,
},

// data is filled
State::PieceAdded => {
	// construct TreeD
	Event::BuildTreeD => State::TreeDBuilt,
},

// TreeD has been generated
State::TreeDBuilt => {
	// Request on-chain random seed required by pc1
	Event::AssignTicket(_) => State::TicketAssigned,
},

// The on-chain random seed required by pc1 has been obtained
State::TicketAssigned => {
	// execute pc1
	Event::PC1(_, _) => State::PC1Done,
},

// pc1 is done
State::PC1Done => {
	// execute pc2
	Event::PC2(_) => State::PC2Done,
},

// pc2 is done
State::PC2Done => {
	// Submit PreCommit on-chain information
	Event::SubmitPC => State::PCSubmitted,
},

// PreCommit on-chain information has been submitted
State::PCSubmitted => {
	// Failed to submit, need to resubmit
	Event::ReSubmitPC => State::PC2Done,
	// Successfully submitted message
	Event::CheckPC => State::PCLanded,
},

// PreCommit information has been submitted 
State::PCLanded => {
	// Perform persistence of sector files
	Event::Persist(_) => State::Persisted,
},

// sector files are persisted
State::Persisted => {
	// Pass persistent files check
	Event::SubmitPersistance => State::PersistanceSubmitted,
},

// Persistence files are confirmed
State::PersistanceSubmitted => {
	// Request on-chain random seed required for c1
	Event::AssignSeed(_) => State::SeedAssigned,
},

// The on-chain random seed required by c1 has been obtained
State::SeedAssigned => {
	// execute c1
	Event::C1(_) => State::C1Done,
},

// c1 is done
State::C1Done => {
	// execute c2
	Event::C2(_) => State::C2Done,
},

// C2 is done
State::C2Done => {
	// Submit CommitProof information
	Event::SubmitProof => State::ProofSubmitted,
},

// CommitProof information has been submitted
State::ProofSubmitted => {
	// Failed to submit, need to resubmit
	Event::ReSubmitProof => State::C2Done,
	// Successful on-chain or skip on-chain check
	Event::Finish => State::Finished,
},

# Upgrade (snapup) task status flow

// Empty state, that is, the sector has not been allocated yet
State::Empty => {
	// Allocate sector and deals for upgrade
	Event::AllocatedSnapUpSector(_, _, _) => State::Allocated,
},

// Sector and deal for upgrade are allocated
State::Allocated => {
	// fill piece data
	Event::AddPiece(_) => State::PieceAdded,
},

// data is filled
State::PieceAdded => {
	// construct TreeD
	Event::BuildTreeD => State::TreeDBuilt,
},

// TreeD has been generated
State::TreeDBuilt => {
	// perform Snap encoding
	Event::SnapEncode(_) => State::SnapEncoded,
},

// Snap encoding is done
State::SnapEncoded => {
	// Compute Snap proof
	Event::SnapProve(_) => State::SnapProved,
},

// Snap proof has been generated
State::SnapProved => {
	// Perform persistence of sector files
	Event::Persist(_) => State::Persisted,
},

// sector files are persisted
State::Persisted => {
	// files fails check
	Event::RePersist => State::SnapProved,
	// files passed the check
	Event::Finish => State::Finished,
},

# The state flow of rebuild tasks

// Empty state, that is, the sector has not been allocated yet
State::Empty => {
	// Allocate sector info for rebuild
	Event::AllocatedRebuildSector(_) => State::Allocated,
},

// Sector info for rebuild are allocated
State::Allocated => {
	// fill piece data
	Event::AddPiece(_) => State::PieceAdded,
},

// Data is filled
State::PieceAdded => {
	// Construct TreeD
	Event::BuildTreeD => State::TreeDBuilt,
},

// TreeD has been generated
State::TreeDBuilt => {
	// Execute pc1
	Event::PC1(_, _) => State::PC1Done,
},

// PC1 is done
State::PC1Done => {
	// Execute pc2
	Event::PC2(_) => State::PC2Done,
},

// PC2 is done
State::PC2Done => {
	// Check sealed file (execute c1)
	Event::CheckSealed => State::SealedChecked,
},

// Sealed file is checked (c1 is done)
State::SealedChecked => {
	// Non upgrade sector, skip snapup steps
	Event::SkipSnap => State::SnapDone,
	// Upgrade sector, fill piece data
	Event::AddPiece(_) => State::SnapPieceAdded,
},

// Data is filled (only upgrade sector)
State::SnapPieceAdded => {
	// Construct TreeD (only upgrade sector)
	Event::BuildTreeD => State::SnapTreeDBuilt,
},

// TreeD has been generated (only upgrade sector)
State::SnapTreeDBuilt => {
	// Perform Snap encoding (only upgrade sector)
	Event::SnapEncode(_) => State::SnapEncoded,
},

// Snap encoding is done (only upgrade sector)
State::SnapEncoded => {
	// Compute Snap proof (only upgrade sector)
	Event::SnapProve(_) => State::SnapDone,
},

// snapup is done or not needed
State::SnapDone => {
	// Perform persistence of sector files
	Event::Persist(_) => State::Persisted,
},

// Sector files are persisted
State::Persisted => {
	// Pass persistent files check
	Event::SubmitPersistance => State::Finished,
},

# Example usage in combination with worker management tools

# 1. For a sector sealing task that has been paused due to an error and cannot be resumed, such as the ticket has expired, you can use…

damocles-worker worker resume --state Aborted --index <index>

or

damocles-manager util worker resume <worker instance name or address> <thread index> Aborted

to terminate the current task.

# 2. For a sector sealing task that has been paused with an error, but it is considered that the task can be reattempted from a previous state. For example, if the ticket has expired, you can use…

damocles-worker worker resume --state TreeDBuilt --index <index>

or

damocles-manager util worker resume <worker instance name or address> <thread index> TreeDBuilt

to let it fall back to the previous state and retry.