-
Notifications
You must be signed in to change notification settings - Fork 27
Make Snapshot
take a strategy trait so that users can write their own strategies
#82
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
Open
apollo-sturdy
wants to merge
6
commits into
CosmWasm:release/1.2
Choose a base branch
from
apollo-sturdy:sturdy/snapshot-strategy-as-trait
base: release/1.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c64cd36
feat: Create SnapshotStrategy trait
apollo-sturdy b958ea4
feat: add height field to should_archive
apollo-sturdy 3f5b9aa
feat: Add S generic to IndexedSnapshotMap
apollo-sturdy 9837319
docs: Add clarifying doc comments to Snapshot
apollo-sturdy 0e271ff
feat: Add IntervalStrategy
apollo-sturdy b1c4527
feat: Make Snapshot pub(crate) again
apollo-sturdy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use cosmwasm_std::{Order, StdResult, Storage}; | ||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
|
||
use crate::{Bound, KeyDeserialize, Map, Prefixer, PrimaryKey}; | ||
|
||
use super::{ChangeSet, SnapshotStrategy}; | ||
|
||
/// A SnapshotStrategy that takes a snapshot only if at least the specified interval has passed. | ||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | ||
pub struct IntervalStrategy { | ||
/// The interval to archive snapshots at. If the time or number of blocks since the last changelog | ||
/// entry is greater than this interval, a new snapshot will be created. | ||
pub interval: u64, | ||
} | ||
|
||
impl IntervalStrategy { | ||
/// Create a new IntervalStrategy with the given interval. | ||
pub const fn new(interval: u64) -> Self { | ||
Self { interval } | ||
} | ||
} | ||
|
||
impl<'a, K, T> SnapshotStrategy<'a, K, T> for IntervalStrategy | ||
where | ||
T: Serialize + DeserializeOwned + Clone, | ||
K: PrimaryKey<'a> + Prefixer<'a> + KeyDeserialize, | ||
{ | ||
fn assert_checkpointed( | ||
&self, | ||
_store: &dyn Storage, | ||
_checkpoints: &Map<'a, u64, u32>, | ||
_height: u64, | ||
) -> StdResult<()> { | ||
Ok(()) | ||
} | ||
|
||
fn should_archive( | ||
&self, | ||
store: &dyn Storage, | ||
_checkpoints: &Map<'a, u64, u32>, | ||
changelog: &Map<'a, (K, u64), ChangeSet<T>>, | ||
key: &K, | ||
height: u64, | ||
) -> StdResult<bool> { | ||
let last_height = height.saturating_sub(self.interval); | ||
|
||
// Check if there is a changelog entry since the last interval | ||
let changelog_entry = changelog | ||
.prefix(key.clone()) | ||
.range_raw( | ||
store, | ||
Some(Bound::inclusive(last_height)), | ||
None, | ||
Order::Ascending, | ||
) | ||
.next(); | ||
|
||
Ok(changelog_entry.is_none()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::snapshot::Snapshot; | ||
|
||
use super::*; | ||
use cosmwasm_std::testing::MockStorage; | ||
|
||
type TestSnapshot = Snapshot<'static, &'static str, u64, IntervalStrategy>; | ||
const INTERVAL_5: TestSnapshot = Snapshot::new( | ||
"interval_5__check", | ||
"interval_5__change", | ||
IntervalStrategy::new(5), | ||
); | ||
|
||
const DUMMY_KEY: &str = "dummy"; | ||
|
||
#[test] | ||
fn should_archive() { | ||
let mut store = MockStorage::new(); | ||
|
||
// Should archive first save since there is no previous changelog entry. | ||
assert_eq!(INTERVAL_5.should_archive(&store, &DUMMY_KEY, 0), Ok(true)); | ||
|
||
// Store changelog entry | ||
INTERVAL_5 | ||
.write_changelog(&mut store, DUMMY_KEY, 0, None) | ||
.unwrap(); | ||
|
||
// Should not archive again | ||
assert_eq!(INTERVAL_5.should_archive(&store, &DUMMY_KEY, 0), Ok(false)); | ||
|
||
// Should archive once interval has passed | ||
assert_eq!(INTERVAL_5.should_archive(&store, &DUMMY_KEY, 6), Ok(true)); | ||
|
||
// Store changelog entry | ||
INTERVAL_5 | ||
.write_changelog(&mut store, DUMMY_KEY, 6, None) | ||
.unwrap(); | ||
|
||
// Should not archive again | ||
assert_eq!(INTERVAL_5.should_archive(&store, &DUMMY_KEY, 6), Ok(false)); | ||
|
||
// Should not archive before interval | ||
assert_eq!( | ||
INTERVAL_5.should_archive(&store, &DUMMY_KEY, 6 + 5), | ||
Ok(false) | ||
); | ||
|
||
// Should archive once interval has passed | ||
assert_eq!( | ||
INTERVAL_5.should_archive(&store, &DUMMY_KEY, 6 + 5 + 1), | ||
Ok(true) | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clippy complains here