diff --git a/repository/src/interpret/read.rs b/repository/src/interpret/read.rs index 36dbeda2..a0821e8d 100644 --- a/repository/src/interpret/read.rs +++ b/repository/src/interpret/read.rs @@ -81,6 +81,54 @@ pub async fn read_last_finalization_proof( } } +pub async fn read_finalization_info( + raw: &RawRepository, + height: BlockHeight, +) -> Result { + 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::>() + .await; + let commits = commits.into_iter().collect::, _>>()?; + let commits = commits + .into_iter() + .filter(|(commit, _hash)| { + commit.title == format!(">block: {height}") + || commit.title == format!(">block: {}", height + 1) + }) + .collect::>(); + + 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 { let header = read_last_finalized_block_header(raw).await?; let commit_hash = get_last_finalized_block_commit_hash(raw).await?; diff --git a/repository/src/lib.rs b/repository/src/lib.rs index 75b279b5..c3e369be 100644 --- a/repository/src/lib.rs +++ b/repository/src/lib.rs @@ -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 { - todo!() + read_finalization_info(&*self.raw.read().await, height).await } /// Reads the given commit.