use std::cell::Cell;
use std::marker::PhantomData;
use std::sync::{Condvar, Mutex};
struct State {
writers: usize,
changing: bool,
waiting: usize,
}
static STATE: Mutex<State> = Mutex::new(State { writers: 0, changing: false, waiting: 0 });
static TURN: Condvar = Condvar::new();
thread_local! {
static DEPTH: Cell<usize> = const { Cell::new(0) };
static CHANGING_HERE: Cell<bool> = const { Cell::new(false) };
}
fn lock() -> std::sync::MutexGuard<'static, State> {
STATE.lock().unwrap_or_else(|e| e.into_inner())
}
#[cfg(not(target_arch = "wasm32"))]
fn wait(s: std::sync::MutexGuard<'static, State>) -> std::sync::MutexGuard<'static, State> {
TURN.wait(s).unwrap_or_else(|e| e.into_inner())
}
#[cfg(target_arch = "wasm32")]
fn wait(_s: std::sync::MutexGuard<'static, State>) -> std::sync::MutexGuard<'static, State> {
panic!("at-rest gate contended on a single thread: a ticket was held across an await");
}
#[must_use]
pub struct Sealing {
counted: bool,
_not_send: PhantomData<*const ()>,
}
pub fn sealing() -> Sealing {
let counted = !CHANGING_HERE.with(Cell::get) && DEPTH.with(Cell::get) == 0;
if counted {
let mut s = lock();
while s.changing || s.waiting > 0 {
s = wait(s);
}
s.writers += 1;
}
DEPTH.with(|d| d.set(d.get() + 1));
Sealing { counted, _not_send: PhantomData }
}
impl Drop for Sealing {
fn drop(&mut self) {
DEPTH.with(|d| d.set(d.get().saturating_sub(1)));
if self.counted {
let mut s = lock();
s.writers -= 1;
if s.writers == 0 {
TURN.notify_all();
}
}
}
}
pub fn held() -> bool {
DEPTH.with(Cell::get) > 0 || CHANGING_HERE.with(Cell::get)
}
#[must_use]
pub struct KeyChange {
_not_send: PhantomData<*const ()>,
}
pub fn key_change() -> Result<KeyChange, String> {
if DEPTH.with(Cell::get) > 0 || CHANGING_HERE.with(Cell::get) {
return Err("A key change can't start inside an at-rest write".to_string());
}
let mut s = lock();
s.waiting += 1;
while s.changing || s.writers > 0 {
s = wait(s);
}
s.waiting -= 1;
s.changing = true;
drop(s);
CHANGING_HERE.with(|c| c.set(true));
Ok(KeyChange { _not_send: PhantomData })
}
pub fn switching() -> Option<KeyChange> {
key_change().ok()
}
impl Drop for KeyChange {
fn drop(&mut self) {
CHANGING_HERE.with(|c| c.set(false));
lock().changing = false;
TURN.notify_all();
}
}
pub(crate) fn require_held(what: &str) {
if held() {
return;
}
if cfg!(test) {
panic!("{what} sealed outside the at-rest gate");
}
crate::log_warn!("[Encryption] {what} sealed outside the at-rest gate");
}
pub(crate) fn with_sealing_key<R>(f: impl FnOnce(Option<&[u8; 32]>) -> R) -> Result<R, String> {
if crate::db::writes_to_live_account() {
if !crate::state::is_encryption_enabled_fast() {
return Ok(f(None));
}
return crate::state::ENCRYPTION_KEY
.with_key(|key| f(Some(key)))
.ok_or_else(|| "encryption enabled but key vault is empty".to_string());
}
let enabled = crate::db::get_sql_setting("encryption_enabled".to_string())?;
let security = crate::db::get_sql_setting("security_type".to_string())?;
if crate::state::resolve_encryption_enabled(enabled.as_deref(), security.as_deref()) {
Err("The account changed before this write; its key is no longer unlocked".to_string())
} else {
Ok(f(None))
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
#[test]
fn nested_sealing_on_one_thread_does_not_wait() {
let _a = sealing();
let _b = sealing();
assert!(held());
assert!(key_change().is_err(), "a key change inside a write would wait on itself");
}
#[test]
fn a_key_change_waits_for_writers_and_holds_new_ones_off() {
let writing = sealing();
let changed = Arc::new(AtomicBool::new(false));
let flag = changed.clone();
let migration = std::thread::spawn(move || {
let _k = key_change().unwrap();
flag.store(true, Ordering::SeqCst);
std::thread::sleep(Duration::from_millis(50));
flag.store(false, Ordering::SeqCst);
});
std::thread::sleep(Duration::from_millis(50));
assert!(!changed.load(Ordering::SeqCst), "the key change waits for the write in flight");
drop(writing);
while !changed.load(Ordering::SeqCst) {
std::thread::yield_now();
}
let _after = sealing();
assert!(!changed.load(Ordering::SeqCst), "a new write waits until the key change is done");
migration.join().unwrap();
}
}