simperby_repository/interpret/
genesis.rs

1use super::*;
2
3pub async fn genesis(raw: &mut RawRepository) -> Result<(), Error> {
4    let reserved_state = raw.read_reserved_state().await?;
5    let block_commit = Commit::Block(reserved_state.genesis_info.header.clone());
6    let semantic_commit = to_semantic_commit(&block_commit, reserved_state.clone())?;
7
8    raw.checkout_clean().await?;
9    // TODO: ignore only if the error is 'already exists'. Otherwise, propagate the error.
10    let _ = raw
11        .create_branch(FINALIZED_BRANCH_NAME.into(), raw.get_head().await?)
12        .await;
13    raw.checkout(FINALIZED_BRANCH_NAME.into())
14        .await
15        .map_err(|e| match e {
16            raw::Error::NotFound(_) => {
17                eyre!(IntegrityError::new(format!(
18                    "failed to checkout to the finalized branch: {e}"
19                )))
20            }
21            _ => eyre!(e),
22        })?;
23    let result = raw.create_semantic_commit(semantic_commit, true).await?;
24    // TODO: ignore only if the error is 'already exists'. Otherwise, propagate the error.
25    let _ = raw.create_branch(FP_BRANCH_NAME.into(), result).await;
26    raw.checkout(FP_BRANCH_NAME.into())
27        .await
28        .map_err(|e| match e {
29            raw::Error::NotFound(_) => {
30                eyre!(IntegrityError::new(format!(
31                    "failed to checkout to the fp branch: {e}"
32                )))
33            }
34            _ => eyre!(e),
35        })?;
36    raw.create_semantic_commit(
37        fp_to_semantic_commit(&LastFinalizationProof {
38            height: 0,
39            proof: reserved_state.genesis_info.genesis_proof.clone(),
40        }),
41        true,
42    )
43    .await?;
44    let finalized_commit_hash = raw.locate_branch(FINALIZED_BRANCH_NAME.to_owned()).await?;
45    raw.checkout_detach(finalized_commit_hash).await?;
46    Ok(())
47}