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

Add genesis transactions caching #243

Merged
merged 7 commits into from
Dec 12, 2023
Merged
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
2 changes: 1 addition & 1 deletion indy-vdr-proxy/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn http_status_msg<T: std::fmt::Display>(code: StatusCode, msg: T) -> VdrResult<
}

async fn get_pool_genesis<T: Pool>(pool: &T) -> VdrResult<ResponseType> {
let txns = pool.get_json_transactions()?;
let txns = pool.get_transactions().encode_json()?;
Ok(ResponseType::Genesis(txns.join("\n")))
}

Expand Down
26 changes: 9 additions & 17 deletions indy-vdr-proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use tokio_rustls::{
};

use indy_vdr::common::error::prelude::*;
use indy_vdr::config::PoolConfig;
use indy_vdr::pool::{helpers::perform_refresh, LocalPool, PoolBuilder, PoolTransactions};

use crate::utils::{
Expand Down Expand Up @@ -260,10 +261,10 @@ async fn create_pool(
) -> VdrResult<LocalPool> {
let pool_states = &state.borrow().pool_states;
let pool_state = pool_states.get(namespace).unwrap();
let builder = PoolBuilder::default().transactions(pool_state.transactions.clone())?;
let pool = builder.into_local()?;
let pool =
PoolBuilder::new(PoolConfig::default(), pool_state.transactions.clone()).into_local()?;
let refresh_pool = if refresh {
refresh_pool(state.clone(), namespace, &pool, 0).await?
refresh_pool(state.clone(), &pool, 0).await?
} else {
None
};
Expand All @@ -279,7 +280,7 @@ async fn refresh_pools(
let pool_states = &state.borrow().pool_states;
for (namespace, pool_state) in pool_states {
if let Some(pool) = &pool_state.pool {
let upd_pool = match refresh_pool(state.clone(), namespace, pool, delay_mins).await {
let upd_pool = match refresh_pool(state.clone(), pool, delay_mins).await {
Ok(p) => p,
Err(err) => {
eprintln!(
Expand All @@ -304,7 +305,6 @@ async fn refresh_pools(

async fn refresh_pool(
state: Rc<RefCell<AppState>>,
namespace: &str,
pool: &LocalPool,
delay_mins: u32,
) -> VdrResult<Option<LocalPool>> {
Expand All @@ -314,19 +314,11 @@ async fn refresh_pool(
}

let (txns, _meta) = perform_refresh(pool).await?;

let cloned_state = state.clone();
let pool_states = &cloned_state.borrow().pool_states;
let pool_state = pool_states.get(namespace).unwrap();

let pool_txns = &mut pool_state.transactions.to_owned();

if let Some(txns) = txns {
let builder = {
pool_txns.extend_from_json(&txns)?;
PoolBuilder::default().transactions(pool_txns.clone())?
};
Ok(Some(builder.into_local()?))
let pool = PoolBuilder::new(PoolConfig::default(), txns)
.refreshed(true)
.into_local()?;
Ok(Some(pool))
} else {
Ok(None)
}
Expand Down
3 changes: 2 additions & 1 deletion indy-vdr-proxy/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ pub fn init_pool_state_from_folder_structure(

let entries = fs::read_dir(path).map_err(|err| {
err_msg(
VdrErrorKind::FileSystem(err),
VdrErrorKind::FileSystem,
"Could not read local networks folder",
)
.with_source(err)
})?;

for entry in entries {
Expand Down
18 changes: 16 additions & 2 deletions libindy_vdr/src/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub enum VdrErrorKind {
Config,
#[error("Connection error")]
Connection,
#[error("File system error: {0}")]
FileSystem(std::io::Error),
#[error("File system error")]
FileSystem,
#[error("Input error")]
Input,
#[error("Resource error")]
Expand Down Expand Up @@ -69,6 +69,14 @@ impl VdrError {
_ => None,
}
}

pub fn with_source<E>(mut self, source: E) -> Self
where
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
self.source.replace(source.into());
self
}
}

impl fmt::Display for VdrError {
Expand Down Expand Up @@ -110,6 +118,12 @@ impl From<crate::utils::ValidationError> for VdrError {
}
}

impl From<std::io::Error> for VdrError {
fn from(err: std::io::Error) -> VdrError {
VdrError::new(VdrErrorKind::FileSystem, None, Some(Box::new(err)))
}
}

impl From<zmq::Error> for VdrError {
fn from(err: zmq::Error) -> VdrError {
VdrError::new(VdrErrorKind::Connection, None, Some(Box::new(err)))
Expand Down
2 changes: 1 addition & 1 deletion libindy_vdr/src/ffi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl From<&VdrErrorKind> for ErrorCode {
match kind {
VdrErrorKind::Config => ErrorCode::Config,
VdrErrorKind::Connection => ErrorCode::Connection,
VdrErrorKind::FileSystem(_) => ErrorCode::FileSystem,
VdrErrorKind::FileSystem => ErrorCode::FileSystem,
VdrErrorKind::Input => ErrorCode::Input,
VdrErrorKind::Resource => ErrorCode::Resource,
VdrErrorKind::Unavailable => ErrorCode::Unavailable,
Expand Down
40 changes: 25 additions & 15 deletions libindy_vdr/src/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use crate::common::error::prelude::*;
use crate::config::{PoolConfig, LIB_VERSION};
use crate::pool::ProtocolVersion;
use crate::utils::Validatable;

use std::convert::TryFrom;
use std::os::raw::c_char;
use std::sync::RwLock;
use std::sync::Arc;

use ffi_support::{define_string_destructor, rust_string_to_c, FfiStr};
use once_cell::sync::Lazy;

#[macro_use]
mod macros;
Expand All @@ -19,14 +13,18 @@ mod pool;
mod requests;
mod resolver;

use crate::common::error::prelude::*;
use crate::config::{PoolConfig, LIB_VERSION};
use crate::pool::{FilesystemCache, PoolTransactionsCache, ProtocolVersion};
use crate::utils::Validatable;

use self::error::{set_last_error, ErrorCode};
use self::pool::{POOL_CACHE, POOL_CONFIG};

pub type CallbackId = i64;

define_string_destructor!(indy_vdr_string_free);

static POOL_CONFIG: Lazy<RwLock<PoolConfig>> = Lazy::new(|| RwLock::new(PoolConfig::default()));

#[no_mangle]
pub extern "C" fn indy_vdr_set_config(config: FfiStr) -> ErrorCode {
catch_err! {
Expand All @@ -35,8 +33,7 @@ pub extern "C" fn indy_vdr_set_config(config: FfiStr) -> ErrorCode {
serde_json::from_str(config.as_str()).with_input_err("Error deserializing config")?;
config.validate()?;
debug!("Updating pool config: {:?}", config);
let mut gcfg = write_lock!(POOL_CONFIG)?;
*gcfg = config;
*write_lock!(POOL_CONFIG)? = config;
Ok(ErrorCode::Success)
}
}
Expand All @@ -55,8 +52,22 @@ pub extern "C" fn indy_vdr_set_protocol_version(version: i64) -> ErrorCode {
catch_err! {
debug!("Setting pool protocol version: {}", version);
let version = ProtocolVersion::try_from(version)?;
let mut gcfg = write_lock!(POOL_CONFIG)?;
gcfg.protocol_version = version;
write_lock!(POOL_CONFIG)?.protocol_version = version;
Ok(ErrorCode::Success)
}
}

#[no_mangle]
pub extern "C" fn indy_vdr_set_cache_directory(path: FfiStr) -> ErrorCode {
catch_err! {
let cache = if let Some(path) = path.as_opt_str() {
debug!("Initializing filesystem pool transactions cache");
Some(Arc::new(FilesystemCache::new(path)) as Arc<dyn PoolTransactionsCache>)
} else {
debug!("Clearing filesystem pool transactions cache");
None
};
*write_lock!(POOL_CACHE)? = cache;
Ok(ErrorCode::Success)
}
}
Expand All @@ -66,8 +77,7 @@ pub extern "C" fn indy_vdr_set_socks_proxy(socks_proxy: FfiStr) -> ErrorCode {
catch_err! {
let proxy = socks_proxy.into_string();
debug!("Setting pool socks proxy: {}", proxy);
let mut gcfg = write_lock!(POOL_CONFIG)?;
gcfg.socks_proxy = Some(proxy);
write_lock!(POOL_CONFIG)?.socks_proxy.replace(proxy);
Ok(ErrorCode::Success)
}
}
Expand Down
Loading