Skip to content

Commit

Permalink
rusk: Support a revert to a specified commit id
Browse files Browse the repository at this point in the history
  • Loading branch information
goshawk-3 committed Jan 19, 2024
1 parent 5574529 commit 68c8b5e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions rusk/src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub enum Error {
InconsistentState(VerificationOutput),
/// Other
Other(Box<dyn std::error::Error>),
/// Commit not found amongst existing commits
CommitNotFound([u8; 32]),
}

impl std::error::Error for Error {}
Expand Down Expand Up @@ -140,6 +142,9 @@ impl fmt::Display for Error {
"Inconsistent state verification data {verification_output}",
)
}
Error::CommitNotFound(commit_id) => {
write!(f, "Commit not found, id = {}", hex::encode(commit_id),)
}
}
}
}
10 changes: 8 additions & 2 deletions rusk/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,15 @@ impl Rusk {
Ok(())
}

pub fn revert(&self) -> Result<[u8; 32]> {
pub fn revert(&self, state_hash: [u8; 32]) -> Result<[u8; 32]> {
let mut inner = self.inner.lock();
inner.current_commit = inner.base_commit;

let mut commits = &inner.vm.commits();
if !commits.contains(&state_hash) {
return Err(Error::CommitNotFound(state_hash));
}

inner.current_commit = state_hash;
Ok(inner.current_commit)
}

Expand Down
8 changes: 6 additions & 2 deletions rusk/src/lib/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ impl VMExecution for Rusk {
Ok(self.state_root())
}

fn revert(&self) -> anyhow::Result<[u8; 32]> {
fn get_base_state_root(&self) -> anyhow::Result<[u8; 32]> {
Ok(self.base_root())
}

fn revert(&self, state_hash: [u8; 32]) -> anyhow::Result<[u8; 32]> {
let state_hash = self
.revert()
.revert(state_hash)
.map_err(|inner| anyhow::anyhow!("Cannot revert: {inner}"))?;

Ok(state_hash)
Expand Down

0 comments on commit 68c8b5e

Please sign in to comment.