Skip to content

Commit

Permalink
Merge pull request #55 from getAlby/feat/vss-secondary-store
Browse files Browse the repository at this point in the history
feat: use secondary sqlite store for network graph data when using vss
  • Loading branch information
rolznz authored Nov 26, 2024
2 parents 7cb44ce + 33fda5a commit f3b3743
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,20 @@ impl NodeBuilder {

let vss_seed_bytes: [u8; 32] = vss_xprv.private_key.secret_bytes();

// Alby: use a secondary KV store for non-essential data (not needed by VSS)
let storage_dir_path = config.storage_dir_path.clone();
let secondary_kv_store = Arc::new(
SqliteStore::new(
storage_dir_path.into(),
Some(io::sqlite_store::SQLITE_DB_FILE_NAME.to_string()),
Some(io::sqlite_store::KV_TABLE_NAME.to_string()),
)
.map_err(|_| BuildError::KVStoreSetupFailed)?,
) as Arc<DynStore>;

let vss_store =
VssStore::new(vss_url, store_id, vss_seed_bytes, header_provider).map_err(|e| {
VssStore::new(vss_url, store_id, vss_seed_bytes, header_provider, secondary_kv_store)
.map_err(|e| {
log_error!(logger, "Failed to setup VssStore: {}", e);
BuildError::KVStoreSetupFailed
})?;
Expand Down
45 changes: 42 additions & 3 deletions src/io/vss_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use crate::io::utils::check_namespace_key_validity;
use bitcoin::hashes::{sha256, Hash, HashEngine, Hmac, HmacEngine};
use lightning::io::{self, Error, ErrorKind};
use lightning::util::persist::KVStore;
use lightning::util::persist::{
KVStore, NETWORK_GRAPH_PERSISTENCE_KEY, NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE,
NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE,
};
use prost::Message;
use rand::RngCore;
#[cfg(test)]
Expand Down Expand Up @@ -44,12 +47,14 @@ pub struct VssStore {
runtime: Runtime,
storable_builder: StorableBuilder<RandEntropySource>,
key_obfuscator: KeyObfuscator,
secondary_kv_store: Arc<dyn KVStore + Send + Sync>,
}

impl VssStore {
pub(crate) fn new(
base_url: String, store_id: String, vss_seed: [u8; 32],
header_provider: Arc<dyn VssHeaderProvider>,
secondary_kv_store: Arc<dyn KVStore + Send + Sync>,
) -> io::Result<Self> {
let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
let (data_encryption_key, obfuscation_master_key) =
Expand All @@ -70,7 +75,7 @@ impl VssStore {
}) as _);

let client = VssClient::new_with_headers(base_url, retry_policy, header_provider);
Ok(Self { client, store_id, runtime, storable_builder, key_obfuscator })
Ok(Self { client, store_id, runtime, storable_builder, key_obfuscator, secondary_kv_store })
}

fn build_key(
Expand Down Expand Up @@ -131,6 +136,14 @@ impl KVStore for VssStore {
fn read(
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
) -> io::Result<Vec<u8>> {
// Alby: write network graph to secondary storage
if primary_namespace == NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE
&& secondary_namespace == NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE
&& key == NETWORK_GRAPH_PERSISTENCE_KEY
{
return self.secondary_kv_store.read(primary_namespace, secondary_namespace, key);
}

check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "read")?;
let request = GetObjectRequest {
store_id: self.store_id.clone(),
Expand Down Expand Up @@ -165,6 +178,14 @@ impl KVStore for VssStore {
fn write(
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8],
) -> io::Result<()> {
// Alby: write network graph to secondary storage
if primary_namespace == NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE
&& secondary_namespace == NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE
&& key == NETWORK_GRAPH_PERSISTENCE_KEY
{
return self.secondary_kv_store.write(primary_namespace, secondary_namespace, key, buf);
}

check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "write")?;
let version = -1;
let storable = self.storable_builder.build(buf.to_vec(), version);
Expand Down Expand Up @@ -194,6 +215,18 @@ impl KVStore for VssStore {
fn remove(
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, _lazy: bool,
) -> io::Result<()> {
// Alby: write network graph to secondary storage
if primary_namespace == NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE
&& secondary_namespace == NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE
&& key == NETWORK_GRAPH_PERSISTENCE_KEY
{
return self.secondary_kv_store.remove(
primary_namespace,
secondary_namespace,
key,
_lazy,
);
}
check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "remove")?;
let request = DeleteObjectRequest {
store_id: self.store_id.clone(),
Expand Down Expand Up @@ -229,7 +262,13 @@ impl KVStore for VssStore {
Error::new(ErrorKind::Other, msg)
})?;

Ok(keys)
// Alby: also list keys from secondary storage
let secondary_keys =
self.secondary_kv_store.list(primary_namespace, secondary_namespace)?;

let all_keys: Vec<String> =
keys.iter().cloned().chain(secondary_keys.iter().cloned()).collect();
Ok(all_keys)
}
}

Expand Down

0 comments on commit f3b3743

Please sign in to comment.