Skip to content

Commit

Permalink
rusk: Add rusk.revert_to_base_root, get_finalized_state_root, revert_…
Browse files Browse the repository at this point in the history
…to_finalized
  • Loading branch information
goshawk-3 authored and goshawk-3 committed Jan 23, 2024
1 parent 14e1f13 commit a4acff5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
3 changes: 1 addition & 2 deletions rusk/benches/block_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ pub fn accept_benchmark(c: &mut Criterion) {
)
.expect("Accepting transactions should succeed");

let base_root = rusk.base_root();
rusk.revert(base_root)
rusk.revert_to_base_root()
.expect("Reverting should succeed");
})
},
Expand Down
4 changes: 4 additions & 0 deletions rusk/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ impl Rusk {
Ok(inner.current_commit)
}

pub fn revert_to_base_root(&self) -> Result<[u8; 32]> {
self.revert(self.base_root())
}

/// Perform an action with the underlying data structure.
pub fn with_inner<'a, F, T>(&'a self, closure: F) -> T
where
Expand Down
10 changes: 9 additions & 1 deletion rusk/src/lib/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl VMExecution for Rusk {
Ok(self.state_root())
}

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

Expand All @@ -160,6 +160,14 @@ impl VMExecution for Rusk {

Ok(state_hash)
}

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

Ok(state_hash)
}
}

impl Rusk {
Expand Down
6 changes: 2 additions & 4 deletions rusk/tests/rusk-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ pub fn rusk_state_accepted() -> Result<()> {
"There should be two notes in the state now"
);

let base_root = rusk.base_root();
rusk.revert(base_root)?;
rusk.revert_to_base_root()?;
let leaves = leaves_from_height(&rusk, 0)?;

assert_eq!(
Expand Down Expand Up @@ -135,8 +134,7 @@ pub fn rusk_state_finalized() -> Result<()> {
"There should be two notes in the state now"
);

let base_root = rusk.base_root();
rusk.revert(base_root)?;
rusk.revert_to_base_root()?;
let leaves = leaves_from_height(&rusk, 0)?;

assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions rusk/tests/services/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ pub async fn wallet() -> Result<()> {
assert_ne!(original_root, new_root, "Root should have changed");

// Revert the state
let base_root = rusk.base_root();
rusk.revert(base_root).expect("Reverting should succeed");
rusk.revert_to_base_root()
.expect("Reverting should succeed");
cache.write().unwrap().clear();

// Check the state's root is back to the original one
Expand Down

0 comments on commit a4acff5

Please sign in to comment.