From e26db67d7dd176a51d9735cfbcdc189710a69b59 Mon Sep 17 00:00:00 2001 From: wadeking98 Date: Thu, 25 Jan 2024 13:58:20 -0800 Subject: [PATCH] added ffi bindings for new cache Signed-off-by: wadeking98 --- indy-vdr-proxy/src/main.rs | 6 ++-- libindy_vdr/include/libindy_vdr.h | 4 +++ libindy_vdr/src/common/error.rs | 6 ++++ libindy_vdr/src/ffi/mod.rs | 30 ++++++++++++++++++- libindy_vdr/src/ffi/pool.rs | 9 ++++-- libindy_vdr/src/lib.rs | 2 +- libindy_vdr/src/pool/builder.rs | 8 +++-- libindy_vdr/tests/utils/pool.rs | 2 +- .../indy-vdr-nodejs/src/NodeJSIndyVdr.ts | 10 +++++++ .../src/library/NativeBindings.ts | 2 ++ .../indy-vdr-react-native/cpp/HostObject.cpp | 2 ++ .../cpp/include/libindy_vdr.h | 4 +++ .../indy-vdr-react-native/cpp/indyVdr.cpp | 19 ++++++++++++ .../indy-vdr-react-native/cpp/indyVdr.h | 2 ++ .../src/NativeBindings.ts | 4 +++ .../src/ReactNativeIndyVdr.ts | 8 +++++ .../indy-vdr-shared/src/types/IndyVdr.ts | 4 +++ wrappers/python/indy_vdr/__init__.py | 4 ++- wrappers/python/indy_vdr/bindings.py | 8 +++++ 19 files changed, 123 insertions(+), 11 deletions(-) diff --git a/indy-vdr-proxy/src/main.rs b/indy-vdr-proxy/src/main.rs index 7f428fd5..62ecf3cf 100644 --- a/indy-vdr-proxy/src/main.rs +++ b/indy-vdr-proxy/src/main.rs @@ -263,8 +263,8 @@ async fn create_pool( ) -> VdrResult { 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 { @@ -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)) diff --git a/libindy_vdr/include/libindy_vdr.h b/libindy_vdr/include/libindy_vdr.h index 7f61bb9f..a52e5b20 100644 --- a/libindy_vdr/include/libindy_vdr.h +++ b/libindy_vdr/include/libindy_vdr.h @@ -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); diff --git a/libindy_vdr/src/common/error.rs b/libindy_vdr/src/common/error.rs index 98e3e6cf..b69769a9 100644 --- a/libindy_vdr/src/common/error.rs +++ b/libindy_vdr/src/common/error.rs @@ -130,6 +130,12 @@ impl From for VdrError { } } +impl From for VdrError { + fn from(err: sled::Error) -> VdrError { + VdrError::new(VdrErrorKind::FileSystem, None, Some(Box::new(err))) + } +} + impl From<(VdrErrorKind, M)> for VdrError where M: fmt::Display + Send + Sync + 'static, diff --git a/libindy_vdr/src/ffi/mod.rs b/libindy_vdr/src/ffi/mod.rs index 6ab2cc33..873d7b3c 100644 --- a/libindy_vdr/src/ffi/mod.rs +++ b/libindy_vdr/src/ffi/mod.rs @@ -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; @@ -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! { diff --git a/libindy_vdr/src/ffi/pool.rs b/libindy_vdr/src/ffi/pool.rs index a10bb4fc..5d87a5e1 100644 --- a/libindy_vdr/src/ffi/pool.rs +++ b/libindy_vdr/src/ffi/pool.rs @@ -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, @@ -40,6 +41,9 @@ pub static POOLS: Lazy>> = pub static POOL_CACHE: Lazy>>> = Lazy::new(|| RwLock::new(Some(Arc::new(InMemoryCache::new())))); +pub static LEDGER_TXN_CACHE: Lazy>>> = + Lazy::new(|| RwLock::new(None)); + #[derive(Serialize, Deserialize, Debug, Clone)] struct PoolCreateParams { #[serde(skip_serializing_if = "Option::is_none")] @@ -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 }); @@ -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; diff --git a/libindy_vdr/src/lib.rs b/libindy_vdr/src/lib.rs index 16c5c0c1..785d1a28 100644 --- a/libindy_vdr/src/lib.rs +++ b/libindy_vdr/src/lib.rs @@ -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(); //! diff --git a/libindy_vdr/src/pool/builder.rs b/libindy_vdr/src/pool/builder.rs index c406f2d6..1f43275d 100644 --- a/libindy_vdr/src/pool/builder.rs +++ b/libindy_vdr/src/pool/builder.rs @@ -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>, + ) -> Self { Self { config, transactions, node_weights: None, refreshed: false, - cache: None, + cache, } } diff --git a/libindy_vdr/tests/utils/pool.rs b/libindy_vdr/tests/utils/pool.rs index 283bf874..8948ab68 100644 --- a/libindy_vdr/tests/utils/pool.rs +++ b/libindy_vdr/tests/utils/pool.rs @@ -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(); diff --git a/wrappers/javascript/indy-vdr-nodejs/src/NodeJSIndyVdr.ts b/wrappers/javascript/indy-vdr-nodejs/src/NodeJSIndyVdr.ts index c82e84f4..62c2a50d 100644 --- a/wrappers/javascript/indy-vdr-nodejs/src/NodeJSIndyVdr.ts +++ b/wrappers/javascript/indy-vdr-nodejs/src/NodeJSIndyVdr.ts @@ -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()) } diff --git a/wrappers/javascript/indy-vdr-nodejs/src/library/NativeBindings.ts b/wrappers/javascript/indy-vdr-nodejs/src/library/NativeBindings.ts index 264602c8..ac222595 100644 --- a/wrappers/javascript/indy-vdr-nodejs/src/library/NativeBindings.ts +++ b/wrappers/javascript/indy-vdr-nodejs/src/library/NativeBindings.ts @@ -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 diff --git a/wrappers/javascript/indy-vdr-react-native/cpp/HostObject.cpp b/wrappers/javascript/indy-vdr-react-native/cpp/HostObject.cpp index dca84796..3810d878 100644 --- a/wrappers/javascript/indy-vdr-react-native/cpp/HostObject.cpp +++ b/wrappers/javascript/indy-vdr-react-native/cpp/HostObject.cpp @@ -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)); diff --git a/wrappers/javascript/indy-vdr-react-native/cpp/include/libindy_vdr.h b/wrappers/javascript/indy-vdr-react-native/cpp/include/libindy_vdr.h index 7f61bb9f..a52e5b20 100644 --- a/wrappers/javascript/indy-vdr-react-native/cpp/include/libindy_vdr.h +++ b/wrappers/javascript/indy-vdr-react-native/cpp/include/libindy_vdr.h @@ -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); diff --git a/wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.cpp b/wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.cpp index 06c1ce7d..02ebb322 100644 --- a/wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.cpp +++ b/wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.cpp @@ -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(rt, options, "capacity"); + auto expiry_offset_ms = jsiToValue(rt, options, "expiry_offset_ms"); + auto path = jsiToValue(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(rt, options, "capacity"); + auto expiry_offset_ms = jsiToValue(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(); diff --git a/wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.h b/wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.h index fa48a925..4baa5426 100644 --- a/wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.h +++ b/wrappers/javascript/indy-vdr-react-native/cpp/indyVdr.h @@ -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); diff --git a/wrappers/javascript/indy-vdr-react-native/src/NativeBindings.ts b/wrappers/javascript/indy-vdr-react-native/src/NativeBindings.ts index 7a7f095e..f87fadd9 100644 --- a/wrappers/javascript/indy-vdr-react-native/src/NativeBindings.ts +++ b/wrappers/javascript/indy-vdr-react-native/src/NativeBindings.ts @@ -13,6 +13,10 @@ export interface NativeBindings { setCacheDirectory(options: { path: string }): ReturnObject + setLedgerTxnCache(options: { capacity: number; expiry_offset_ms: number }): ReturnObject + + setLedgerTxnFsCache(options: { capacity: number; expiry_offset_ms: number; path: string }): ReturnObject + setDefaultLogger(options: Record): ReturnObject setProtocolVersion(options: { version: number }): ReturnObject diff --git a/wrappers/javascript/indy-vdr-react-native/src/ReactNativeIndyVdr.ts b/wrappers/javascript/indy-vdr-react-native/src/ReactNativeIndyVdr.ts index e89f3f3f..ec03bc17 100644 --- a/wrappers/javascript/indy-vdr-react-native/src/ReactNativeIndyVdr.ts +++ b/wrappers/javascript/indy-vdr-react-native/src/ReactNativeIndyVdr.ts @@ -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({})) diff --git a/wrappers/javascript/indy-vdr-shared/src/types/IndyVdr.ts b/wrappers/javascript/indy-vdr-shared/src/types/IndyVdr.ts index bee59f8a..1c71e4d5 100644 --- a/wrappers/javascript/indy-vdr-shared/src/types/IndyVdr.ts +++ b/wrappers/javascript/indy-vdr-shared/src/types/IndyVdr.ts @@ -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 diff --git a/wrappers/python/indy_vdr/__init__.py b/wrappers/python/indy_vdr/__init__.py index 8e41f778..a62e2252 100644 --- a/wrappers/python/indy_vdr/__init__.py +++ b/wrappers/python/indy_vdr/__init__.py @@ -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 @@ -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", diff --git a/wrappers/python/indy_vdr/bindings.py b/wrappers/python/indy_vdr/bindings.py index 488a8ea9..c4faa74e 100644 --- a/wrappers/python/indy_vdr/bindings.py +++ b/wrappers/python/indy_vdr/bindings.py @@ -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."""