Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snapshot functionality to ink_sandbox #2261

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add snapshot impl to sandbox_client
0xLucca committed Jun 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 7739ce24b2eb848e71a5cf8a500b1bacf62fd723
4 changes: 2 additions & 2 deletions crates/e2e/sandbox/src/lib.rs
Original file line number Diff line number Diff line change
@@ -48,9 +48,9 @@ pub use {
#[derive(Clone, Debug)]
pub struct Snapshot {
/// The storage raw key-value pairs.
storage: RawStorage,
pub storage: RawStorage,
/// The storage root hash.
storage_root: StorageRoot,
pub storage_root: StorageRoot,
}

pub type RawStorage = Vec<(Vec<u8>, (Vec<u8>, i32))>;
32 changes: 32 additions & 0 deletions crates/e2e/src/sandbox_client.rs
Original file line number Diff line number Diff line change
@@ -469,6 +469,7 @@ pub mod preset {
Extension,
RuntimeMetadataPrefixed,
Sandbox,
Snapshot,
};
pub use pallet_contracts_mock_network::*;
use sp_runtime::traits::Dispatchable;
@@ -543,6 +544,37 @@ pub mod preset {
{
Some(account).into()
}

fn take_snapshot(&mut self) -> Snapshot {
EXT_PARAA.with(|v| {
let v = v.borrow();
let mut backend = v.as_backend().clone();
let raw_key_values = backend
.backend_storage_mut()
.drain()
.into_iter()
.filter(|(_, (_, r))| *r > 0)
.collect::<Vec<(Vec<u8>, (Vec<u8>, i32))>>();
let root = backend.root().to_owned();

Snapshot {
storage: raw_key_values,
storage_root: root,
}
})
}

fn restore_snapshot(&mut self, snapshot: ink_sandbox::Snapshot) {
EXT_PARAA.with(|v| {
let mut v = v.borrow_mut();

*v = ink_sandbox::TestExternalities::from_raw_snapshot(
snapshot.storage,
snapshot.storage_root,
Default::default(),
);
})
}
}
}
}