Skip to content

Commit

Permalink
chore: address remaining PR feedback and get tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
jcnelson committed Nov 5, 2024
1 parent 9731059 commit 45adc33
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 147 deletions.
18 changes: 12 additions & 6 deletions stackslib/src/burnchains/burnchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,10 @@ impl Burnchain {
}

pub fn get_burnchaindb_path(&self) -> String {
if self.working_dir.as_str() == ":memory:" {
return ":memory:".to_string();
}

let chainstate_dir = Burnchain::get_chainstate_path_str(&self.working_dir);
let mut db_pathbuf = PathBuf::from(&chainstate_dir);
db_pathbuf.push("burnchain.sqlite");
Expand Down Expand Up @@ -743,12 +747,14 @@ impl Burnchain {
/// Open just the burnchain database
pub fn open_burnchain_db(&self, readwrite: bool) -> Result<BurnchainDB, burnchain_error> {
let burnchain_db_path = self.get_burnchaindb_path();
if let Err(e) = fs::metadata(&burnchain_db_path) {
warn!(
"Failed to stat burnchain DB path '{}': {:?}",
&burnchain_db_path, &e
);
return Err(burnchain_error::DBError(db_error::NoDBError));
if burnchain_db_path != ":memory:" {
if let Err(e) = fs::metadata(&burnchain_db_path) {
warn!(
"Failed to stat burnchain DB path '{}': {:?}",
&burnchain_db_path, &e
);
return Err(burnchain_error::DBError(db_error::NoDBError));
}
}
test_debug!(
"Open burnchain DB at {} (rw? {})",
Expand Down
55 changes: 30 additions & 25 deletions stackslib/src/burnchains/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,33 +1000,38 @@ impl BurnchainDB {
readwrite: bool,
) -> Result<BurnchainDB, BurnchainError> {
let mut create_flag = false;
let open_flags = match fs::metadata(path) {
Err(e) => {
if e.kind() == io::ErrorKind::NotFound {
// need to create
if readwrite {
create_flag = true;
let ppath = Path::new(path);
let pparent_path = ppath
.parent()
.unwrap_or_else(|| panic!("BUG: no parent of '{}'", path));
fs::create_dir_all(&pparent_path)
.map_err(|e| BurnchainError::from(DBError::IOError(e)))?;

OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE
let open_flags = if path == ":memory:" {
create_flag = true;
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE
} else {
match fs::metadata(path) {
Err(e) => {
if e.kind() == io::ErrorKind::NotFound {
// need to create
if readwrite {
create_flag = true;
let ppath = Path::new(path);
let pparent_path = ppath
.parent()
.unwrap_or_else(|| panic!("BUG: no parent of '{}'", path));
fs::create_dir_all(&pparent_path)
.map_err(|e| BurnchainError::from(DBError::IOError(e)))?;

OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE
} else {
return Err(BurnchainError::from(DBError::NoDBError));
}
} else {
return Err(BurnchainError::from(DBError::NoDBError));
return Err(BurnchainError::from(DBError::IOError(e)));
}
} else {
return Err(BurnchainError::from(DBError::IOError(e)));
}
}
Ok(_md) => {
// can just open
if readwrite {
OpenFlags::SQLITE_OPEN_READ_WRITE
} else {
OpenFlags::SQLITE_OPEN_READ_ONLY
Ok(_md) => {
// can just open
if readwrite {
OpenFlags::SQLITE_OPEN_READ_WRITE
} else {
OpenFlags::SQLITE_OPEN_READ_ONLY
}
}
}
};
Expand Down Expand Up @@ -1089,7 +1094,7 @@ impl BurnchainDB {
let conn = sqlite_open(path, open_flags, true)?;
let mut db = BurnchainDB { conn };

if readwrite {
if readwrite || path == ":memory:" {
db.add_indexes()?;
}
Ok(db)
Expand Down
21 changes: 9 additions & 12 deletions stackslib/src/net/download/nakamoto/download_state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,16 +1418,19 @@ impl NakamotoDownloadStateMachine {
chainstate: &StacksChainState,
ibd: bool,
) -> HashMap<ConsensusHash, Vec<NakamotoBlock>> {
debug!("NakamotoDownloadStateMachine in state {}", &self.state);
debug!(
"run_downloads: burnchain_height={}, network.burnchain_tip.block_height={}, state={}",
burnchain_height, network.burnchain_tip.block_height, &self.state;
"has_network_inventories" => network.inv_state_nakamoto.is_some(),
"next_unconfirmed_check" => self.last_unconfirmed_download_check_ms.saturating_add(CHECK_UNCONFIRMED_TENURES_MS) / 1000,
"timestamp_ms" => get_epoch_time_ms(),
);

let Some(invs) = network.inv_state_nakamoto.as_ref() else {
// nothing to do
debug!("No network inventories");
return HashMap::new();
};
debug!(
"run_downloads: burnchain_height={}, network.burnchain_tip.block_height={}, state={}",
burnchain_height, network.burnchain_tip.block_height, &self.state
);

self.update_available_tenures(
&invs.inventories,
&sortdb.pox_constants,
Expand All @@ -1441,12 +1444,6 @@ impl NakamotoDownloadStateMachine {
.saturating_add(CHECK_UNCONFIRMED_TENURES_MS)
> get_epoch_time_ms()
{
debug!(
"Throttle checking for unconfirmed tenures until {}",
self.last_unconfirmed_download_check_ms
.saturating_add(CHECK_UNCONFIRMED_TENURES_MS)
/ 1000
);
false
} else {
let do_fetch = Self::need_unconfirmed_tenures(
Expand Down
Loading

0 comments on commit 45adc33

Please sign in to comment.