Skip to content

Commit

Permalink
added ffi bindings for new cache
Browse files Browse the repository at this point in the history
Signed-off-by: wadeking98 <[email protected]>
  • Loading branch information
wadeking98 committed Jan 25, 2024
1 parent 3875fae commit e26db67
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 11 deletions.
6 changes: 3 additions & 3 deletions indy-vdr-proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ async fn create_pool(
) -> VdrResult<LocalPool> {
let pool_states = &state.borrow().pool_states;
let pool_state = pool_states.get(namespace).unwrap();
let pool =
PoolBuilder::new(PoolConfig::default(), pool_state.transactions.clone()).into_local()?;
let pool = PoolBuilder::new(PoolConfig::default(), pool_state.transactions.clone(), None)
.into_local()?;
let refresh_pool = if refresh {
refresh_pool(state.clone(), &pool, 0).await?
} else {
Expand Down Expand Up @@ -317,7 +317,7 @@ async fn refresh_pool(

let (txns, _meta) = perform_refresh(pool).await?;
if let Some(txns) = txns {
let pool = PoolBuilder::new(PoolConfig::default(), txns)
let pool = PoolBuilder::new(PoolConfig::default(), txns, None)
.refreshed(true)
.into_local()?;
Ok(Some(pool))
Expand Down
4 changes: 4 additions & 0 deletions libindy_vdr/include/libindy_vdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ ErrorCode indy_vdr_resolve(PoolHandle pool_handle,

ErrorCode indy_vdr_set_cache_directory(FfiStr path);

ErrorCode indy_vdr_set_ledger_txn_cache(size_t capacity, c_ulong expiry_offset_ms);

ErrorCode indy_vdr_set_ledger_txn_fs_cache(size_t capacity, c_ulong expiry_offset_ms, FfiStr path);

ErrorCode indy_vdr_set_config(FfiStr config);

ErrorCode indy_vdr_set_default_logger(void);
Expand Down
6 changes: 6 additions & 0 deletions libindy_vdr/src/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ impl From<zmq::Error> for VdrError {
}
}

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

impl<M> From<(VdrErrorKind, M)> for VdrError
where
M: fmt::Display + Send + Sync + 'static,
Expand Down
30 changes: 29 additions & 1 deletion libindy_vdr/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ mod resolver;

use crate::common::error::prelude::*;
use crate::config::{PoolConfig, LIB_VERSION};
use crate::pool::cache::storage::{new_fs_ordered_store, OrderedHashMap};
use crate::pool::cache::strategy::CacheStrategyTTL;
use crate::pool::cache::Cache;
use crate::pool::{FilesystemCache, PoolTransactionsCache, ProtocolVersion};
use crate::utils::Validatable;

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

pub type CallbackId = i64;

Expand Down Expand Up @@ -72,6 +75,31 @@ pub extern "C" fn indy_vdr_set_cache_directory(path: FfiStr) -> ErrorCode {
}
}

#[no_mangle]
pub extern "C" fn indy_vdr_set_ledger_txn_cache(capacity: usize, expire_offset: u64) -> ErrorCode {
catch_err! {
debug!("Setting pool ledger transactions cache: capacity={}, expire_offset={}", capacity, expire_offset);
let cache = Cache::new(CacheStrategyTTL::new(capacity, expire_offset.into(), None, None));
*write_lock!(LEDGER_TXN_CACHE)? = Some(cache);
Ok(ErrorCode::Success)
}
}

#[no_mangle]
pub extern "C" fn indy_vdr_set_ledger_txn_fs_cache(
capacity: usize,
expire_offset: u64,
path: FfiStr,
) -> ErrorCode {
catch_err! {
debug!("Setting pool ledger transactions cache: capacity={}, expire_offset={}", capacity, expire_offset);
let store = OrderedHashMap::new(new_fs_ordered_store(path.into())?);
let cache = Cache::new(CacheStrategyTTL::new(capacity, expire_offset.into(), Some(store), None));
*write_lock!(LEDGER_TXN_CACHE)? = Some(cache);
Ok(ErrorCode::Success)
}
}

#[no_mangle]
pub extern "C" fn indy_vdr_set_socks_proxy(socks_proxy: FfiStr) -> ErrorCode {
catch_err! {
Expand Down
9 changes: 7 additions & 2 deletions libindy_vdr/src/ffi/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use once_cell::sync::Lazy;
use crate::common::error::prelude::*;
use crate::common::handle::ResourceHandle;
use crate::config::PoolConfig;
use crate::pool::cache::Cache;
use crate::pool::{
InMemoryCache, PoolBuilder, PoolRunner, PoolTransactions, PoolTransactionsCache, RequestMethod,
RequestResult, RequestResultMeta,
Expand Down Expand Up @@ -40,6 +41,9 @@ pub static POOLS: Lazy<RwLock<BTreeMap<PoolHandle, PoolInstance>>> =
pub static POOL_CACHE: Lazy<RwLock<Option<Arc<dyn PoolTransactionsCache>>>> =
Lazy::new(|| RwLock::new(Some(Arc::new(InMemoryCache::new()))));

pub static LEDGER_TXN_CACHE: Lazy<RwLock<Option<Cache<String, (String, RequestResultMeta)>>>> =
Lazy::new(|| RwLock::new(None));

#[derive(Serialize, Deserialize, Debug, Clone)]
struct PoolCreateParams {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -76,7 +80,8 @@ pub extern "C" fn indy_vdr_pool_create(params: FfiStr, handle_p: *mut PoolHandle
}
}
let config = read_lock!(POOL_CONFIG)?.clone();
let runner = PoolBuilder::new(config, txns.clone()).node_weights(params.node_weights.clone()).refreshed(cached).into_runner()?;
let txn_cache = read_lock!(LEDGER_TXN_CACHE)?.clone();
let runner = PoolBuilder::new(config, txns.clone(), txn_cache).node_weights(params.node_weights.clone()).refreshed(cached).into_runner()?;
let handle = PoolHandle::next();
let mut pools = write_lock!(POOLS)?;
pools.insert(handle, PoolInstance { runner, init_txns: txns, node_weights: params.node_weights });
Expand All @@ -102,7 +107,7 @@ fn handle_pool_refresh(
cache.update(&init_txns, latest_txns)?;
}
if let Some(new_txns) = new_txns {
let runner = PoolBuilder::new(config, new_txns).node_weights(node_weights).refreshed(true).into_runner()?;
let runner = PoolBuilder::new(config, new_txns, None).node_weights(node_weights).refreshed(true).into_runner()?;
let mut pools = write_lock!(POOLS)?;
if let Entry::Occupied(mut entry) = pools.entry(pool_handle) {
entry.get_mut().runner = runner;
Expand Down
2 changes: 1 addition & 1 deletion libindy_vdr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! let txns = PoolTransactions::from_json_file("./genesis.txn").unwrap();
//!
//! // Create a PoolBuilder instance
//! let pool_builder = PoolBuilder::new(PoolConfig::default(), txns);
//! let pool_builder = PoolBuilder::new(PoolConfig::default(), txns, None);
//! // Convert into a thread-local Pool instance
//! let pool = pool_builder.into_local().unwrap();
//!
Expand Down
8 changes: 6 additions & 2 deletions libindy_vdr/src/pool/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ pub struct PoolBuilder {

impl PoolBuilder {
/// Create a new `PoolBuilder` instance.
pub fn new(config: PoolConfig, transactions: PoolTransactions) -> Self {
pub fn new(
config: PoolConfig,
transactions: PoolTransactions,
cache: Option<Cache<String, (String, RequestResultMeta)>>,
) -> Self {
Self {
config,
transactions,
node_weights: None,
refreshed: false,
cache: None,
cache,
}
}

Expand Down
2 changes: 1 addition & 1 deletion libindy_vdr/tests/utils/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl TestPool {
let pool_transactions =
PoolTransactions::from_json_transactions(default_transactions()).unwrap();

let pool = PoolBuilder::new(PoolConfig::default(), pool_transactions)
let pool = PoolBuilder::new(PoolConfig::default(), pool_transactions, None)
.into_shared()
.unwrap();

Expand Down
10 changes: 10 additions & 0 deletions wrappers/javascript/indy-vdr-nodejs/src/NodeJSIndyVdr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ export class NodeJSIndyVdr implements IndyVdr {
this.handleError(this.nativeIndyVdr.indy_vdr_set_cache_directory(path))
}

public setLedgerTxnCache(options: { capacity: number; expiry_offset_ms: number }): void {
const { capacity, expiry_offset_ms } = serializeArguments(options)
this.handleError(this.nativeIndyVdr.indy_vdr_set_ledger_txn_cache(capacity, expiry_offset_ms))
}

public setLedgerTxnFsCache(options: { capacity: number; expiry_offset_ms: number; path: string }): void {
const { capacity, expiry_offset_ms, path } = serializeArguments(options)
this.handleError(this.nativeIndyVdr.indy_vdr_set_ledger_txn_fs_cache(capacity, expiry_offset_ms, path))
}

public setDefaultLogger(): void {
this.handleError(this.nativeIndyVdr.indy_vdr_set_default_logger())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { ByteBuffer } from '../ffi'
export interface NativeMethods {
indy_vdr_set_config: (arg0: string) => number
indy_vdr_set_cache_directory: (arg0: string) => number
indy_vdr_set_ledger_txn_cache: (arg0: number, arg1: number) => number
indy_vdr_set_ledger_txn_fs_cache: (arg0: number, arg1: number, arg2: string) => number
indy_vdr_set_default_logger: () => number
indy_vdr_set_protocol_version: (arg0: number) => number
indy_vdr_set_socks_proxy: (arg0: string) => number
Expand Down
2 changes: 2 additions & 0 deletions wrappers/javascript/indy-vdr-react-native/cpp/HostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ FunctionMap IndyVdrTurboModuleHostObject::functionMapping(jsi::Runtime &rt) {
fMap.insert(std::make_tuple("getCurrentError", &indyVdr::getCurrentError));
fMap.insert(std::make_tuple("setConfig", &indyVdr::setConfig));
fMap.insert(std::make_tuple("setCacheDirectory", &indyVdr::setCacheDirectory));
fMap.insert(std::make_tuple("setLedgerTxnCache", &indyVdr::setLedgerTxnCache));
fMap.insert(std::make_tuple("setLedgerTxnFsCache", &indyVdr::setLedgerTxnFsCache));
fMap.insert(std::make_tuple("setDefaultLogger", &indyVdr::setDefaultLogger));
fMap.insert(
std::make_tuple("setProtocolVersion", &indyVdr::setProtocolVersion));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ ErrorCode indy_vdr_resolve(PoolHandle pool_handle,

ErrorCode indy_vdr_set_cache_directory(FfiStr path);

ErrorCode indy_vdr_set_ledger_txn_cache(size_t capacity, c_ulong expiry_offset_ms);

ErrorCode indy_vdr_set_ledger_txn_fs_cache(size_t capacity, c_ulong expiry_offset_ms, FfiStr path);

ErrorCode indy_vdr_set_config(FfiStr config);

ErrorCode indy_vdr_set_default_logger(void);
Expand Down
19 changes: 19 additions & 0 deletions wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ jsi::Value setCacheDirectory(jsi::Runtime &rt, jsi::Object options) {
return createReturnValue(rt, code, nullptr);
};

jsi::Value setLedgerTxnFsCache(jsi::Runtime &rt, jsi::Object options) {
auto capacity = jsiToValue<size_t>(rt, options, "capacity");
auto expiry_offset_ms = jsiToValue<c_ulong>(rt, options, "expiry_offset_ms");
auto path = jsiToValue<std::string>(rt, options, "path");

ErrorCode code = indy_vdr_set_ledger_txn_cache(capacity, expiry_offset_ms, path.c_str());

return createReturnValue(rt, code, nullptr);
};

jsi::Value setLedgerTxnCache(jsi::Runtime &rt, jsi::Object options) {
auto capacity = jsiToValue<size_t>(rt, options, "capacity");
auto expiry_offset_ms = jsiToValue<c_ulong>(rt, options, "expiry_offset_ms");

ErrorCode code = indy_vdr_set_ledger_txn_cache(capacity, expiry_offset_ms);

return createReturnValue(rt, code, nullptr);
};

jsi::Value setDefaultLogger(jsi::Runtime &rt, jsi::Object options) {
ErrorCode code = indy_vdr_set_default_logger();

Expand Down
2 changes: 2 additions & 0 deletions wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jsi::Value version(jsi::Runtime &rt, jsi::Object options);
jsi::Value getCurrentError(jsi::Runtime &rt, jsi::Object options);
jsi::Value setConfig(jsi::Runtime &rt, jsi::Object options);
jsi::Value setCacheDirectory(jsi::Runtime &rt, jsi::Object options);
jsi::Value setLedgerTxnFsCache(jsi::Runtime &rt, jsi::Object options);
jsi::Value setLedgerTxnCache(jsi::Runtime &rt, jsi::Object options);
jsi::Value setDefaultLogger(jsi::Runtime &rt, jsi::Object options);
jsi::Value setProtocolVersion(jsi::Runtime &rt, jsi::Object options);
jsi::Value setSocksProxy(jsi::Runtime &rt, jsi::Object options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface NativeBindings {

setCacheDirectory(options: { path: string }): ReturnObject<never>

setLedgerTxnCache(options: { capacity: number; expiry_offset_ms: number }): ReturnObject<never>

setLedgerTxnFsCache(options: { capacity: number; expiry_offset_ms: number; path: string }): ReturnObject<never>

setDefaultLogger(options: Record<string, never>): ReturnObject<never>

setProtocolVersion(options: { version: number }): ReturnObject<never>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ export class ReactNativeIndyVdr implements IndyVdr {
const serializedOptions = serializeArguments(options)
this.indyVdr.setCacheDirectory(serializedOptions)
}
public setLedgerTxnCache(options: { capacity: number; expiry_offset_ms: number }): void {
const serializedOptions = serializeArguments(options)
this.indyVdr.setLedgerTxnCache(serializedOptions)
}
public setLedgerTxnFsCache(options: { capacity: number; expiry_offset_ms: number; path: string }): void {
const serializedOptions = serializeArguments(options)
this.indyVdr.setLedgerTxnFsCache(serializedOptions)
}

public setDefaultLogger(): void {
this.handleError(this.indyVdr.setDefaultLogger({}))
Expand Down
4 changes: 4 additions & 0 deletions wrappers/javascript/indy-vdr-shared/src/types/IndyVdr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export interface IndyVdr {

setCacheDirectory(options: { path: string }): void

setLedgerTxnCache(options: { capacity: number; expiry_offset_ms: number }): void

setLedgerTxnFsCache(options: { capacity: number; expiry_offset_ms: number; path: string }): void

setDefaultLogger(): void

setProtocolVersion(options: { version: number }): void
Expand Down
4 changes: 3 additions & 1 deletion wrappers/python/indy_vdr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""indy-vdr Python wrapper library"""

from .bindings import set_cache_directory, set_config, set_protocol_version, version
from .bindings import set_cache_directory, set_ledger_txn_fs_cache, set_ledger_txn_cache, set_config, set_protocol_version, version
from .error import VdrError, VdrErrorCode
from .ledger import LedgerType
from .pool import Pool, open_pool
Expand All @@ -10,6 +10,8 @@
__all__ = [
"open_pool",
"set_cache_directory",
"set_ledger_txn_fs_cache",
"set_ledger_txn_cache",
"set_config",
"set_protocol_version",
"set_socks_proxy",
Expand Down
8 changes: 8 additions & 0 deletions wrappers/python/indy_vdr/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ def set_cache_directory(path: str):
"""Set the library configuration."""
do_call("indy_vdr_set_cache_directory", encode_str(path))

def set_ledger_txn_cache(capacity: int, expiry_offset_ms: int):
"""Set the library configuration."""
do_call("indy_vdr_set_ledger_txn_cache", c_size_t(capacity), c_ulong(expiry_offset_ms))

def set_ledger_txn_fs_cache(capacity: int, expiry_offset_ms: int, path: str):
"""Set the library configuration."""
do_call("indy_vdr_set_ledger_txn_fs_cache", c_size_t(capacity), c_ulong(expiry_offset_ms), encode_str(path))


def set_config(config: dict):
"""Set the library configuration."""
Expand Down

0 comments on commit e26db67

Please sign in to comment.