Skip to content

Commit

Permalink
Add read_finalization_info
Browse files Browse the repository at this point in the history
  • Loading branch information
JeongHunP authored and junha1 committed Aug 28, 2023
1 parent 872cd6f commit fa59a75
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
48 changes: 48 additions & 0 deletions repository/src/interpret/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,54 @@ pub async fn read_last_finalization_proof(
}
}

pub async fn read_finalization_info(
raw: &RawRepository,
height: BlockHeight,
) -> Result<FinalizationInfo, Error> {
let finalized_block_header = read_last_finalized_block_header(raw).await?;
let last_block_height = finalized_block_header.height;

if height == last_block_height {
read_last_finalization_info(raw).await
} else {
let initial_commit = raw.get_initial_commit().await?;
let finalized_commit_hash = get_last_finalized_block_commit_hash(raw).await?;

let commits = raw
.query_commit_path(initial_commit, finalized_commit_hash)
.await?;
let commits = stream::iter(
commits
.iter()
.cloned()
.map(|c| async move { raw.read_semantic_commit(c).await.map(|x| (x, c)) }),
)
.buffered(256)
.collect::<Vec<_>>()
.await;
let commits = commits.into_iter().collect::<Result<Vec<_>, _>>()?;
let commits = commits
.into_iter()
.filter(|(commit, _hash)| {
commit.title == format!(">block: {height}")
|| commit.title == format!(">block: {}", height + 1)
})
.collect::<Vec<_>>();

let header: BlockHeader = serde_spb::from_str(&commits[0].0.body)?;
let next_header: BlockHeader = serde_spb::from_str(&commits[1].0.body)?;
let commit_hash = commits[0].1;
let reserved_state = raw.read_reserved_state_at_commit(commit_hash).await?;
let proof = next_header.prev_block_finalization_proof;
Ok(FinalizationInfo {
header,
commit_hash,
reserved_state,
proof,
})
}
}

pub async fn read_last_finalization_info(raw: &RawRepository) -> Result<FinalizationInfo, Error> {
let header = read_last_finalized_block_header(raw).await?;
let commit_hash = get_last_finalized_block_commit_hash(raw).await?;
Expand Down
4 changes: 2 additions & 2 deletions repository/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ impl DistributedRepository {
/// Reads the finalization information at specific height.
pub async fn read_finalization_info(
&self,
_height: BlockHeight,
height: BlockHeight,
) -> Result<FinalizationInfo, Error> {
todo!()
read_finalization_info(&*self.raw.read().await, height).await
}

/// Reads the given commit.
Expand Down

0 comments on commit fa59a75

Please sign in to comment.