use sha2::{Digest, Sha256};
use super::entry::Point;
use super::node::State;
use crate::sqlite::{Pages, Snapshot};
use crate::{Error, ErrorKind, Result, Store};
const VERIFIED_WITHIN: std::time::Duration = std::time::Duration::from_secs(30);
pub fn verify(store: &Store, file: &str) -> Result<u64> {
let what = || format!("verify {file}");
let node = store.node().ok_or_else(|| Error::invalid("a store that is no node").within(what()))?;
node.ask_to_verify(file);
let deadline = std::time::Instant::now() + VERIFIED_WITHIN;
loop {
if let Some(done) = node.verified(file) {
return done.map_err(|error| error.within(what()));
}
if std::time::Instant::now() >= deadline || node.stopping() {
node.forget_verifying(file);
return Err(Error::new(ErrorKind::Unavailable, "no answer came from the node it takes the file from")
.within(what()));
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
}
pub(crate) fn here(state: &State, file: &str) -> Result<(Point, [u8; 32])> {
let pages = Pages::open(&state.dir.join(file))?;
let (point, snapshot) = match state.copy(file) {
Some(copy) => copy.holding(|point| Ok((point, pages.begin()?)))?,
None => state.holding_home(file, |point| Ok((point, pages.begin()?)))?,
};
Ok((point, checksum(&snapshot)?))
}
pub(crate) fn of_copy(state: &State, file: &str) -> Result<[u8; 32]> {
let pages = Pages::open(&state.dir.join(file))?;
checksum(&pages.begin()?)
}
fn checksum(snapshot: &Snapshot<'_>) -> Result<[u8; 32]> {
let mut hash = Sha256::new();
snapshot.each(|number, page| {
if number == 1 && page.len() >= 96 {
let mut first = page.to_vec();
first[24..28].fill(0);
first[92..96].fill(0);
hash.update(&first);
} else {
hash.update(page);
}
})?;
Ok(hash.finalize().into())
}
pub(crate) fn compare(file: &str, sequence: u64, ours: [u8; 32], theirs: &[u8]) -> Result<u64> {
if ours.as_slice() == theirs {
return Ok(sequence);
}
tracing::error!(
target: "tinystore",
file,
entry = sequence,
"a copy's pages differ from the node it takes them from",
);
Err(Error::new(
ErrorKind::Corrupt,
format!("its pages differ from those of the node it takes it from, at entry {sequence}"),
))
}