Skip to content

Commit

Permalink
Implement check method
Browse files Browse the repository at this point in the history
  • Loading branch information
fakedev9999 committed Dec 10, 2022
1 parent 81b32cb commit 6e68c6e
Showing 1 changed file with 132 additions and 6 deletions.
138 changes: 132 additions & 6 deletions repository/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,139 @@ impl<T: RawRepository> DistributedRepository<T> {
/// Checks the validity of the repository, starting from the given height.
///
/// It checks
/// 1. all the reserved branches and tags
/// 2. the finalization proof in the `fp` branch.
/// 3. the existence of merge commits
/// 4. the canonical history of the `finalized` branch.
/// 1. all the reserved branches and tags,
/// 2. the finalization proof in the `fp` branch,
/// 3. the existence of merge commits,
/// 4. the canonical history of the `finalized` branch,
/// 5. the reserved state in a valid format.
pub async fn check(&self, _starting_height: BlockHeight) -> Result<bool, Error> {
unimplemented!()
pub async fn check(&self, starting_height: BlockHeight) -> Result<bool, Error> {
let last_header = self.get_last_finalized_block_header().await?;
if last_header.height < starting_height {
return Err(anyhow!(
"starting height {} is higher than the last finalized block height {}",
starting_height,
last_header.height
));
}

// Construct a commit list starting from the block commit of the `starting_height`
// to the `last_header_commit`(the most recent commit of the `finalized` branch)
let inital_commit_hash = self.raw.get_initial_commit().await?;
let last_header_commit = self.raw.locate_branch(FINALIZED_BRANCH_NAME.into()).await?;
let all_commits = read_commits(self, inital_commit_hash, last_header_commit).await?;
let starting_block_commit_position = all_commits
.iter()
.position(|(commit, _)| {
if let Commit::Block(block_header) = commit {
block_header.height == starting_height
} else {
false
}
})
.ok_or_else(|| {
anyhow!(
"cannot find the commit of the block at height {}",
starting_height
)
})?;
let commits = all_commits
.into_iter()
.skip(starting_block_commit_position)
.collect::<Vec<_>>();

// TODO: Get the reserved state of the `starting_height` block and verify.
// to check the canonical history of the `finalized` branch
// Note that whether the reserved state in a valid format is also checked
// by the `CommitSequenceVerifier`. (See #166)
// TODO: Check the existence of merge commits
for (commit, commit_hash) in commits {
// Check all the reserved branches
let branches = self.raw.get_branches(commit_hash).await?;
for branch in branches {
match commit {
Commit::Agenda(_) => {
if branch
!= format!(
"a-{:?}",
commit
.to_hash256()
.to_string()
.truncate(TAG_NAME_HASH_DIGITS)
)
{
return Ok(false);
}
}
Commit::AgendaProof(_) => {
if branch
!= format!(
"a-{:?}",
commit
.to_hash256()
.to_string()
.truncate(TAG_NAME_HASH_DIGITS)
)
{
return Ok(false);
}
}
Commit::Block(_) => {
if branch
!= format!(
"b-{:?}",
commit
.to_hash256()
.to_string()
.truncate(TAG_NAME_HASH_DIGITS)
)
{
return Ok(false);
}
}
_ => {
return Ok(false);
}
}
}

// Check all the reserved tags
let tags = self.raw.get_tag(commit_hash).await?;
for tag in tags {
match commit {
Commit::Agenda(_) => {
if tag
!= format!(
"vote-{:?}",
commit
.to_hash256()
.to_string()
.truncate(TAG_NAME_HASH_DIGITS)
)
{
return Ok(false);
}
}
Commit::Block(_) => {
if tag
!= format!(
"veto-{:?}",
commit
.to_hash256()
.to_string()
.truncate(TAG_NAME_HASH_DIGITS)
)
{
return Ok(false);
}
}
_ => {
return Ok(false);
}
}
}
}

Ok(true)
}

/// Synchronizes the `finalized` branch to the given commit.
Expand Down

0 comments on commit 6e68c6e

Please sign in to comment.