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

[Merged by Bors] - check if the slashing protection database is locked before creating keys #1949

Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions account_manager/src/validator/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ pub fn cli_run<T: EthSpec>(
)
})?;

// Create an empty transaction and drops it. Used to test if the database is locked.
slashing_protection.test_transaction().map_err(|e| {
format!(
"Cannot create keys while the validator client is running: {:?}",
e
)
})?;

for i in 0..n {
let voting_password = random_password();
let withdrawal_password = random_password();
Expand Down
8 changes: 8 additions & 0 deletions account_manager/src/validator/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ pub fn cli_run(matches: &ArgMatches, validator_dir: PathBuf) -> Result<(), Strin
)
})?;

// Create an empty transaction and drop it. Used to test if the database is locked.
slashing_protection.test_transaction().map_err(|e| {
format!(
"Cannot import keys while the validator client is running: {:?}",
e
)
})?;

eprintln!("validator-dir path: {:?}", validator_dir);
// Collect the paths for the keystores that should be imported.
let keystore_paths = match (keystore, keystores_dir) {
Expand Down
17 changes: 17 additions & 0 deletions validator_client/slashing_protection/src/slashing_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ impl SlashingDatabase {
#[cfg(windows)]
fn set_db_file_permissions(file: &File) -> Result<(), NotSafe> {}

/// Creates an empty transaction and drops it. Used to test whether the database is locked.
pub fn test_transaction(&self) -> Result<(), NotSafe> {
let mut conn = self.conn_pool.get()?;
Transaction::new(&mut conn, TransactionBehavior::Exclusive)?;
Ok(())
}

/// Register a validator with the slashing protection database.
///
/// This allows the validator to record their signatures in the database, and check
Expand Down Expand Up @@ -803,4 +810,14 @@ mod tests {
let db2 = SlashingDatabase::open(&file).unwrap();
check(&db2);
}

#[test]
fn test_transaction_failure() {
let dir = tempdir().unwrap();
let file = dir.path().join("db.sqlite");
let _db1 = SlashingDatabase::create(&file).unwrap();

let db2 = SlashingDatabase::open(&file).unwrap();
db2.test_transaction().unwrap_err();
}
}