-
Notifications
You must be signed in to change notification settings - Fork 135
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 support for persistent state in separate virtual memory #2321
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,16 +29,16 @@ fn should_report_max_number_of_entries_for_256gb() { | |
} | ||
|
||
#[test] | ||
fn should_serialize_header_v8() { | ||
fn should_serialize_header_v9() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: shouldn't we still keep the test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New storage is now automatically initialized in Compatibility with existing |
||
let memory = VectorMemory::default(); | ||
let mut storage = Storage::new((1, 2), memory.clone()); | ||
storage.update_salt([5u8; 32]); | ||
storage.flush(); | ||
|
||
assert_eq!(storage.version(), 8); | ||
assert_eq!(storage.version(), 9); | ||
let mut buf = vec![0; HEADER_SIZE]; | ||
memory.read(0, &mut buf); | ||
assert_eq!(buf, hex::decode("49494308000000000100000000000000020000000000000000100505050505050505050505050505050505050505050505050505050505050505").unwrap()); | ||
assert_eq!(buf, hex::decode("49494309000000000100000000000000020000000000000000100505050505050505050505050505050505050505050505050505050505050505").unwrap()); | ||
} | ||
|
||
#[test] | ||
|
@@ -54,6 +54,19 @@ fn should_recover_header_from_memory_v8() { | |
assert_eq!(storage.version(), 8); | ||
} | ||
|
||
#[test] | ||
fn should_recover_header_from_memory_v9() { | ||
let memory = VectorMemory::default(); | ||
memory.grow(1); | ||
memory.write(0, &hex::decode("494943090500000040e2010000000000f1fb090000000000000843434343434343434343434343434343434343434343434343434343434343430002000000000000000000000000000000000000000000000000").unwrap()); | ||
|
||
let storage = Storage::from_memory(memory); | ||
assert_eq!(storage.assigned_anchor_number_range(), (123456, 654321)); | ||
assert_eq!(storage.salt().unwrap(), &[67u8; 32]); | ||
assert_eq!(storage.anchor_count(), 5); | ||
assert_eq!(storage.version(), 9); | ||
} | ||
|
||
#[test] | ||
fn should_read_previous_write() { | ||
let memory = VectorMemory::default(); | ||
|
@@ -104,19 +117,32 @@ fn should_save_and_restore_persistent_state() { | |
} | ||
|
||
#[test] | ||
fn should_not_find_persistent_state_if_it_does_not_exist() { | ||
fn should_not_find_persistent_state_if_it_does_not_exist_v8() { | ||
let memory = VectorMemory::default(); | ||
let mut storage = Storage::new((10_000, 3_784_873), memory); | ||
memory.grow(1); | ||
memory.write(0, &hex::decode("494943080500000040e2010000000000f1fb090000000000000843434343434343434343434343434343434343434343434343434343434343430002000000000000000000000000000000000000000000000000").unwrap()); | ||
let mut storage = Storage::from_memory(memory.clone()); | ||
storage.flush(); | ||
|
||
let result = storage.read_persistent_state(); | ||
assert!(matches!(result, Err(PersistentStateError::NotFound))) | ||
} | ||
|
||
#[test] | ||
fn should_overwrite_persistent_state_with_next_anchor() { | ||
fn should_always_find_persistent_state_v9() { | ||
let memory = VectorMemory::default(); | ||
let mut storage = Storage::new((10_000, 3_784_873), memory.clone()); | ||
let mut storage = Storage::new((10_000, 3_784_873), memory); | ||
storage.flush(); | ||
|
||
assert!(storage.read_persistent_state().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn should_overwrite_persistent_state_with_next_anchor_v8() { | ||
let memory = VectorMemory::default(); | ||
memory.grow(1); | ||
memory.write(0, &hex::decode("494943080500000040e2010000000000f1fb090000000000000843434343434343434343434343434343434343434343434343434343434343430002000000000000000000000000000000000000000000000000").unwrap()); | ||
let mut storage = Storage::from_memory(memory.clone()); | ||
storage.flush(); | ||
|
||
storage.allocate_anchor().unwrap(); | ||
|
@@ -130,6 +156,22 @@ fn should_overwrite_persistent_state_with_next_anchor() { | |
assert!(matches!(result, Err(PersistentStateError::NotFound))); | ||
} | ||
|
||
#[test] | ||
fn should_not_overwrite_persistent_state_with_next_anchor_v9() { | ||
let memory = VectorMemory::default(); | ||
let mut storage = Storage::new((10_000, 3_784_873), memory.clone()); | ||
storage.flush(); | ||
|
||
storage.allocate_anchor().unwrap(); | ||
storage.write_persistent_state(&sample_persistent_state()); | ||
assert!(storage.read_persistent_state().is_ok()); | ||
|
||
let anchor = storage.allocate_anchor().unwrap(); | ||
storage.write(anchor).unwrap(); | ||
|
||
assert!(storage.read_persistent_state().is_ok()); | ||
} | ||
|
||
fn sample_device() -> Device { | ||
Device { | ||
pubkey: ByteBuf::from("hello world, I am a public key"), | ||
|
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.
It would be cleaner to wrap the underlying memory such that:
However, I'll do this in a separate PR.