forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persist gossip state (next expected sequence num) to disk (MystenLabs…
…#2445) * FollowerStore for storing the next expected sequence from a gossip peer * Persist next expected sequence number from gossip peers * Fix tests * Add TODO
- Loading branch information
1 parent
eb4c334
commit 2f4366f
Showing
10 changed files
with
174 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,89 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::path::Path; | ||
use sui_types::{ | ||
base_types::AuthorityName, | ||
batch::TxSequenceNumber, | ||
error::{SuiError, SuiResult}, | ||
}; | ||
use typed_store::rocks::DBMap; | ||
use typed_store::{reopen, traits::Map}; | ||
|
||
use crate::default_db_options; | ||
|
||
use tracing::debug; | ||
|
||
/// FollowerStore tracks the next tx sequence numbers that we should expect after the previous | ||
/// batch. | ||
pub struct FollowerStore { | ||
next_sequence: DBMap<AuthorityName, TxSequenceNumber>, | ||
} | ||
|
||
impl FollowerStore { | ||
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, SuiError> { | ||
let (options, _) = default_db_options(None); | ||
|
||
let db = { | ||
let path = &path; | ||
let db_options = Some(options.clone()); | ||
let opt_cfs: &[(&str, &rocksdb::Options)] = &[("next_sequence", &options)]; | ||
typed_store::rocks::open_cf_opts(path, db_options, opt_cfs) | ||
} | ||
.map_err(SuiError::StorageError)?; | ||
|
||
let next_sequence = reopen!(&db, "next_sequence";<AuthorityName, TxSequenceNumber>); | ||
|
||
Ok(Self { next_sequence }) | ||
} | ||
|
||
pub fn get_next_sequence(&self, name: &AuthorityName) -> SuiResult<Option<TxSequenceNumber>> { | ||
self.next_sequence.get(name).map_err(SuiError::StorageError) | ||
} | ||
|
||
pub fn record_next_sequence(&self, name: &AuthorityName, seq: TxSequenceNumber) -> SuiResult { | ||
debug!(peer = ?name, ?seq, "record_next_sequence"); | ||
self.next_sequence | ||
.insert(name, &seq) | ||
.map_err(SuiError::StorageError) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::follower_store::FollowerStore; | ||
use sui_types::crypto::get_key_pair; | ||
|
||
#[test] | ||
fn test_follower_store() { | ||
let working_dir = tempfile::tempdir().unwrap(); | ||
|
||
let follower_store = FollowerStore::open(&working_dir).expect("cannot open db"); | ||
|
||
let (_, key_pair) = get_key_pair(); | ||
let val_name = key_pair.public_key_bytes(); | ||
|
||
let seq = follower_store | ||
.get_next_sequence(val_name) | ||
.expect("read error"); | ||
assert!(seq.is_none()); | ||
|
||
follower_store | ||
.record_next_sequence(val_name, 42) | ||
.expect("write error"); | ||
|
||
let seq = follower_store | ||
.get_next_sequence(val_name) | ||
.expect("read error"); | ||
assert_eq!(seq.unwrap(), 42); | ||
|
||
follower_store | ||
.record_next_sequence(val_name, 43) | ||
.expect("write error"); | ||
|
||
let seq = follower_store | ||
.get_next_sequence(val_name) | ||
.expect("read error"); | ||
assert_eq!(seq.unwrap(), 43); | ||
} | ||
} |
This file contains 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 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