//! [Keep db flowchart](https://raw.githubusercontent.com/moondance-labs/tanssi/master/docs/keep_db_flowchart.png)
/// Timeout to wait for the database to close before starting it again, used in `wait_for_paritydb_lock`.
/// This is the max timeout, if the db is closed in 1 second then that function will only wait 1 second.
/// `container_chain_para_id` should be passed as separate arguments to the [try_spawn] function.
/// running an embeded orchestrator node, as this will prevent spawning a container chain in a node
/// Messages used to control the `ContainerChainSpawner`. This is needed because one of the fields
// Separate function to allow using `?` to return a result, and also to avoid using `self` in an
// Get a closure that checks if db_path exists.Need this to know when to use full sync instead of warp sync.
// This should be a separate function, but it has so many arguments that I prefer to have it as a closure for now
// Loop will run at most 2 times: 1 time if the db is good and 2 times if the db needs to be removed
// Dropping is not enough because there is some background process that keeps the database open,
// If using full sync, print a warning if the local db is at block 0 and the chain has thousands of blocks
unreachable!("Above loop can run at most 2 times, and in the second iteration it is guaranteed to return")
return Err(format!("Tried to spawn a container chain when another container chain with the same para id was already running: {:?}", container_chain_para_id).into());
log::error!("Essential task failed in container chain {} task manager. Shutting down container chain service", container_chain_para_id);
log::error!("Unexpected shutdown in container chain {} task manager. Shutting down container chain service", container_chain_para_id);
/// Returns the database path for the container chain, can be used with `wait_for_paritydb_lock`
/// to ensure that the container chain has fully stopped. The database path can be `None` if the
/// Returns the database path for the container chain, can be used with `wait_for_paritydb_lock`
/// to ensure that the container chain has fully stopped. The database path can be `None` if the
// But this is not the para id, it's the chain id which we have set to include the para id, but that's not mandatory.
// Ignore orchestrator_para_id because it is handled in a special way, as it does not need to
// Force restart of new assigned container chain: if it was running before it was in "syncing mode",
// Handle edge case of going from (2000, 2001) to (2001, 2000). In that case we must restart both chains,
/// Remember that warp sync doesn't work if a partially synced database already exists, it falls
/// back to full sync instead. The only exception is if the previous instance of the database was
/// interrupted before it finished downloading the state, in that case the node will use warp sync.
/// If it was interrupted during the block history download, the node will use full sync but also
/// Given a container chain client, check if the database is valid. If not, returns `Some` with the
/// * High block diff: when the local db is outdated and it would take a long time to sync using full sync, we remove it to be able to use warp sync.
/// * Genesis hash mismatch, when the chain was deregistered and a different chain with the same para id was registered.
/// `Collator2002-01/data/containers/chains/simple_container_2002/paritydb/full-container-2002`
/// So we use `delete_empty_folders_recursive` to try to remove the parent folders as well, but only
/// Removes all empty folders in `path`, recursively. Then, if `path` is empty, it removes it as well.
pub async fn wait_for_paritydb_lock(db_path: &Path, max_timeout: Duration) -> Result<(), String> {
/// background process is still using the database, so we should wait before trying to open it.
/// <https://github.com/paritytech/parity-db/blob/2b6820e310a08678d4540c044f41a93d87343ac8/src/db.rs#L215>
let db_path = PathBuf::from("/tmp/zombienet/Collator2002-01/data/containers/chains/simple_container_2002/paritydb/full-container-2002");