Skip to content

Commit

Permalink
Add TestStore implementation of KVStore
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Sep 7, 2023
1 parent 44ec308 commit 4308a8b
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::util::config::UserConfig;
use crate::util::test_channel_signer::{TestChannelSigner, EnforcementState};
use crate::util::logger::{Logger, Level, Record};
use crate::util::ser::{Readable, ReadableArgs, Writer, Writeable};
use crate::util::persist::KVStore;

use bitcoin::EcdsaSighashType;
use bitcoin::blockdata::constants::ChainHash;
Expand Down Expand Up @@ -425,6 +426,97 @@ impl<Signer: sign::WriteableEcdsaChannelSigner> chainmonitor::Persist<Signer> fo
}
}

pub(crate) struct TestStore {
persisted_bytes: Mutex<HashMap<String, HashMap<String, Vec<u8>>>>,
read_only: bool,
}

impl TestStore {
pub fn new(read_only: bool) -> Self {
let persisted_bytes = Mutex::new(HashMap::new());
Self { persisted_bytes, read_only }
}
}

impl KVStore for TestStore {
fn read(&self, namespace: &str, sub_namespace: &str, key: &str) -> io::Result<Vec<u8>> {
let persisted_lock = self.persisted_bytes.lock().unwrap();
let prefixed = if sub_namespace.is_empty() {
namespace.to_string()
} else {
format!("{}/{}", namespace, sub_namespace)
};

if let Some(outer_ref) = persisted_lock.get(&prefixed) {
if let Some(inner_ref) = outer_ref.get(key) {
let bytes = inner_ref.clone();
Ok(bytes)
} else {
Err(io::Error::new(io::ErrorKind::NotFound, "Key not found"))
}
} else {
Err(io::Error::new(io::ErrorKind::NotFound, "Namespace not found"))
}
}

fn write(&self, namespace: &str, sub_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> {
if self.read_only {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"Cannot modify read-only store",
));
}
let mut persisted_lock = self.persisted_bytes.lock().unwrap();

let prefixed = if sub_namespace.is_empty() {
namespace.to_string()
} else {
format!("{}/{}", namespace, sub_namespace)
};
let outer_e = persisted_lock.entry(prefixed).or_insert(HashMap::new());
let mut bytes = Vec::new();
bytes.write_all(buf)?;
outer_e.insert(key.to_string(), bytes);
Ok(())
}

fn remove(&self, namespace: &str, sub_namespace: &str, key: &str, _lazy: bool) -> io::Result<()> {
if self.read_only {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"Cannot modify read-only store",
));
}

let mut persisted_lock = self.persisted_bytes.lock().unwrap();

let prefixed = if sub_namespace.is_empty() {
namespace.to_string()
} else {
format!("{}/{}", namespace, sub_namespace)
};
if let Some(outer_ref) = persisted_lock.get_mut(&prefixed) {
outer_ref.remove(&key.to_string());
}

Ok(())
}

fn list(&self, namespace: &str, sub_namespace: &str) -> io::Result<Vec<String>> {
let mut persisted_lock = self.persisted_bytes.lock().unwrap();

let prefixed = if sub_namespace.is_empty() {
namespace.to_string()
} else {
format!("{}/{}", namespace, sub_namespace)
};
match persisted_lock.entry(prefixed) {
hash_map::Entry::Occupied(e) => Ok(e.get().keys().cloned().collect()),
hash_map::Entry::Vacant(_) => Ok(Vec::new()),
}
}
}

pub struct TestBroadcaster {
pub txn_broadcasted: Mutex<Vec<Transaction>>,
pub blocks: Arc<Mutex<Vec<(Block, u32)>>>,
Expand Down

0 comments on commit 4308a8b

Please sign in to comment.