Skip to content

Commit

Permalink
Refactor libvcx
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Dec 28, 2022
1 parent 8cfb396 commit 30c32bc
Show file tree
Hide file tree
Showing 73 changed files with 618 additions and 632 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup-testing-nodejs/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ runs:
- name: "Build libvcx"
shell: bash
run: |
cargo build --manifest-path="libvcx/Cargo.toml"
cargo build --features libvcx_c --manifest-path="libvcx/Cargo.toml"
sudo cp "target/debug/libvcx.so" /usr/lib
- name: "Start indypool, mysql, agency"
if: ${{ inputs.skip-docker-setup != 'true' }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ jobs:
uses: ./.github/actions/setup-testing-rust
- name: "Run libvcx tests: pool_tests"
run: |
RUST_TEST_THREADS=1 cargo test --manifest-path="libvcx/Cargo.toml" -F "pool_tests"
RUST_TEST_THREADS=1 cargo test --features libvcx_c --manifest-path="libvcx/Cargo.toml" -F "pool_tests"
test-node-wrapper:
needs: workflow-setup
Expand Down Expand Up @@ -586,7 +586,7 @@ jobs:
sudo apt-get update -y
sudo apt-get install -y libsodium-dev libssl-dev libzmq3-dev
- name: "Build libvcx.so"
run: cargo build --release
run: cargo build --release --features libvcx_c
- name: "Publish artifact libvcx.so"
uses: actions/upload-artifact@v2
with:
Expand All @@ -610,7 +610,7 @@ jobs:
brew install zeromq
brew install libsodium
- name: "Build libvcx.dylib"
run: cargo build --release
run: cargo build --release --features libvcx_c
- name: "Publish artifact libvcx.dylib"
uses: actions/upload-artifact@v2
with:
Expand Down
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ci/libvcx.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WORKDIR /home/indy
COPY --chown=indy ./ ./

USER indy
RUN cargo build --release --manifest-path=/home/indy/libvcx/Cargo.toml
RUN cargo build --release --features libvcx_c --manifest-path=/home/indy/libvcx/Cargo.toml
USER root
RUN mv /home/indy/target/release/libvcx.so .

Expand Down
5 changes: 2 additions & 3 deletions libvcx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ doctest = false
test_utils = [ "aries-vcx/test_utils" ]
pool_tests = [ "test_utils" ]
general_test = [ "test_utils" ]
# to_restore = []
fatal_warnings = []
ios-wrapper = ["aries-vcx/ios-wrapper"]
libvcx_c = []

[dependencies]
num-traits = "0.2.0"
Expand All @@ -33,10 +33,8 @@ rand = "0.7.3"
serde = "1.0.97"
serde_json = "1.0.40"
serde_derive = "1.0.97"
base64 = "0.10"
futures = { version = "0.3", default-features = false }
tokio = { version = "1.15.0", features = ["rt-multi-thread"] }
uuid = {version = "0.7.1", default-features = false, features = ["v4"]}
aries-vcx = { path = "../aries_vcx" }
thiserror = "1.0.37"

Expand All @@ -45,3 +43,4 @@ android_logger = "0.5"

[dev-dependencies]
tokio = { version = "1.15", features = [ "rt", "macros" ] }
uuid = {version = "0.7.1", default-features = false, features = ["v4"]}
14 changes: 7 additions & 7 deletions libvcx/src/api_lib/api_c/agent.rs → libvcx/src/api_c/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::ptr;
use futures::future::BoxFuture;
use libc::c_char;

use crate::api_lib::api_c::types::CommandHandle;
use crate::api_lib::api_handle::agent;
use crate::api_lib::errors::error;
use crate::api_lib::errors::error::{LibvcxError, LibvcxErrorKind};
use crate::api_lib::utils::cstring::CStringUtils;
use crate::api_lib::utils::current_error::set_current_error_vcx;
use crate::api_lib::utils::runtime::{execute, execute_async};
use crate::api_c::cutils::cstring::CStringUtils;
use crate::api_c::cutils::current_error::set_current_error_vcx;
use crate::api_c::cutils::runtime::{execute, execute_async};
use crate::api_c::types::CommandHandle;
use crate::api_vcx::api_handle::agent;
use crate::errors::error;
use crate::errors::error::{LibvcxError, LibvcxErrorKind};

#[no_mangle]
pub extern "C" fn vcx_public_agent_create(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
extern crate libc;

use libc::c_char;
use std::collections::HashMap;
use std::ffi::CStr;
use std::hash::Hash;
use std::slice;
use std::sync::Mutex;

use self::libc::c_char;

pub const POISON_MSG: &str = "FAILED TO LOCK CALLBACK MAP!";

pub fn build_string(ptr: *const c_char) -> Option<String> {
Expand Down Expand Up @@ -48,9 +45,10 @@ pub fn get_cb<H: Eq + Hash, T>(command_handle: H, map: &Mutex<HashMap<H, T>>) ->
#[cfg(test)]
#[cfg(feature = "general_test")]
mod tests {
use crate::api_c::cutils::callback::{build_string, get_cb};
use std::collections::HashMap;
use std::ffi::CString;

use super::*;
use std::sync::Mutex;

fn cstring(str_val: &String) -> CString {
CString::new(str_val.clone()).unwrap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use std::sync::Mutex;

use libc::c_char;

use crate::api_c::cutils::callback::{build_buf, build_string, get_cb};
use aries_vcx::vdrtools::CommandHandle;

use crate::api_lib::utils::callback::{build_buf, build_string, get_cb};

lazy_static! {
pub static ref CALLBACKS_U32: Mutex<HashMap<CommandHandle, Box<dyn FnMut(u32) + Send>>> = Default::default();
pub static ref CALLBACKS_U32_U32: Mutex<HashMap<CommandHandle, Box<dyn FnMut(u32, u32) + Send>>> =
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::ptr;

use libc::c_char;

use crate::api_lib::errors::error::LibvcxError;
use crate::api_lib::utils::cstring::CStringUtils;
use crate::api_c::cutils::cstring::CStringUtils;
use crate::errors::error::LibvcxError;

thread_local! {
pub static CURRENT_ERROR_C_JSON: RefCell<Option<CString>> = RefCell::new(None);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use std::env;
use std::ffi::CString;
use std::io::Write;
use std::ptr;

use crate::api_lib::errors::error::{LibvcxError, LibvcxErrorKind, LibvcxResult};
use chrono::{
format::{DelayedFormat, StrftimeItems},
Local,
};
use crate::api_c::cutils::cstring::CStringUtils;
use crate::errors::error::{LibvcxError, LibvcxErrorKind, LibvcxResult};
use chrono::format::{DelayedFormat, StrftimeItems};
use chrono::Local;
use env_logger::{fmt::Formatter, Builder as EnvLoggerBuilder};
use libc::{c_char, c_void};
use libc::c_char;
use log::{Level, LevelFilter, Metadata, Record};

use crate::api_lib::utils::cstring::CStringUtils;
use std::ffi::{c_void, CString};
use std::io::Write;
use std::{env, ptr};

pub type CVoid = c_void;

Expand All @@ -37,7 +32,7 @@ static mut ENABLED_CB: Option<EnabledCB> = None;
static mut LOG_CB: Option<LogCB> = None;
static mut FLUSH_CB: Option<FlushCB> = None;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum LoggerState {
Default,
Custom,
Expand Down Expand Up @@ -292,6 +287,8 @@ fn get_level(level: u32) -> Level {
#[cfg(test)]
mod tests {
use super::*;
use crate::api_c::cutils::logger::{CVoid, LibvcxLogger};
use std::ptr;

fn get_custom_context() -> *const CVoid {
ptr::null()
Expand Down Expand Up @@ -338,13 +335,15 @@ mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_custom_logger() {
LibvcxLogger::init(
get_custom_context(),
Some(custom_enabled),
custom_log,
Some(custom_flush),
)
.unwrap();
unsafe {
LibvcxLogger::init(
get_custom_context(),
Some(custom_enabled),
custom_log,
Some(custom_flush),
)
.unwrap();
}
error!("error level message"); // first call of log function
unsafe {
assert_eq!(COUNT, 2) // second-time log function was called inside libindy
Expand Down
16 changes: 6 additions & 10 deletions libvcx/src/api_lib/utils/mod.rs → libvcx/src/api_c/cutils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
pub mod callback;
pub mod callback_u32;
pub mod return_types_u32;
pub mod runtime;
#[macro_use]
pub(super) mod ccallback;
pub(crate) mod ccallback;
#[macro_use]
pub mod cstring;
pub mod runtime;

pub mod callback;
pub mod callback_u32;
pub mod current_error;
pub mod logger;
pub mod return_types_u32;
pub mod timeout;

pub mod current_error;
#[cfg(feature = "test_utils")]
pub mod devsetup;
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use std::time::Duration;

use libc::c_char;

use crate::api_lib::errors::error;
use crate::api_lib::errors::mapping_ffi_tests::map_indy_error;
use crate::errors::error;
use crate::errors::mapping_ffi_tests::map_indy_error;
use aries_vcx::indy::utils::next_command_handle;
use aries_vcx::vdrtools::CommandHandle;

use crate::api_lib::utils::callback::POISON_MSG;
use crate::api_lib::utils::callback_u32 as callback;
use crate::api_lib::utils::timeout::TimeoutUtils;
use crate::api_c::cutils::callback::POISON_MSG;
use crate::api_c::cutils::callback_u32 as callback;
use crate::api_c::cutils::timeout::TimeoutUtils;

fn log_error<T: Display>(e: T) {
warn!("Unable to send through libindy callback in vcx: {}", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
use std::future::Future;
use std::sync::atomic::{AtomicUsize, Ordering};

use crate::api_lib::errors::error::{LibvcxError, LibvcxErrorKind, LibvcxResult};
use crate::errors::error::{LibvcxError, LibvcxErrorKind, LibvcxResult};
use futures::future::BoxFuture;
use tokio::runtime::Runtime;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::ptr;

use aries_vcx::global::settings::CONFIG_INSTITUTION_DID;
use futures::future::BoxFuture;
use libc::c_char;

use aries_vcx::global::settings;

use crate::api_lib::api_c::types::CommandHandle;
use crate::api_lib::api_handle::{credential_def, vcx_settings};
use crate::api_lib::errors::error;
use crate::api_lib::errors::error::{LibvcxError, LibvcxErrorKind};
use crate::api_lib::utils::cstring::CStringUtils;
use crate::api_lib::utils::current_error::set_current_error_vcx;
use crate::api_lib::utils::runtime::{execute, execute_async};
use crate::api_c::cutils::cstring::CStringUtils;
use crate::api_c::cutils::current_error::set_current_error_vcx;
use crate::api_c::cutils::runtime::{execute, execute_async};
use crate::api_c::types::CommandHandle;
use crate::api_vcx::api_global::settings;
use crate::api_vcx::api_handle::credential_def;
use crate::errors::error;
use crate::errors::error::{LibvcxError, LibvcxErrorKind};

#[no_mangle]
pub extern "C" fn vcx_credentialdef_create_v2(
Expand All @@ -34,7 +34,7 @@ pub extern "C" fn vcx_credentialdef_create_v2(
check_useful_c_str!(issuer_did, LibvcxErrorKind::InvalidOption);
issuer_did
} else {
match vcx_settings::get_config_value(settings::CONFIG_INSTITUTION_DID) {
match settings::get_config_value(CONFIG_INSTITUTION_DID) {
Ok(did) => did,
Err(err) => return err.into(),
}
Expand Down Expand Up @@ -451,9 +451,9 @@ mod tests {
use aries_vcx::utils::constants::SCHEMA_ID;
use aries_vcx::utils::devsetup::{SetupLibraryWallet, SetupMocks};

use crate::api_lib::errors::error;
use crate::api_lib::utils::return_types_u32;
use crate::api_lib::utils::timeout::TimeoutUtils;
use crate::api_c::cutils::return_types_u32;
use crate::api_c::cutils::timeout::TimeoutUtils;
use crate::errors::error;

use super::*;

Expand Down
3 changes: 3 additions & 0 deletions libvcx/src/api_c/ledger/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod credential_def;
mod revocation_registry;
mod schema;
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::ptr;

use aries_vcx::global::settings::CONFIG_INSTITUTION_DID;
use futures::future::BoxFuture;
use libc::c_char;

use aries_vcx::global::settings;

use crate::api_lib::api_c::types::CommandHandle;
use crate::api_lib::api_handle::{revocation_registry, revocation_registry::RevocationRegistryConfig, vcx_settings};
use crate::api_lib::errors::error;
use crate::api_lib::errors::error::{LibvcxError, LibvcxErrorKind};
use crate::api_lib::utils::cstring::CStringUtils;
use crate::api_lib::utils::current_error::{set_current_error, set_current_error_vcx};
use crate::api_lib::utils::runtime::{execute, execute_async};
use crate::api_c::cutils::cstring::CStringUtils;
use crate::api_c::cutils::current_error::{set_current_error, set_current_error_vcx};
use crate::api_c::cutils::runtime::{execute, execute_async};
use crate::api_c::types::CommandHandle;
use crate::api_vcx::api_global::settings;
use crate::api_vcx::api_handle::{revocation_registry, revocation_registry::RevocationRegistryConfig};
use crate::errors::error;
use crate::errors::error::{LibvcxError, LibvcxErrorKind};

#[no_mangle]
pub extern "C" fn vcx_revocation_registry_create(
Expand Down Expand Up @@ -122,7 +122,7 @@ pub extern "C" fn vcx_revocation_registry_publish_revocations(

check_useful_c_callback!(cb, LibvcxErrorKind::InvalidOption);

let issuer_did: String = match vcx_settings::get_config_value(settings::CONFIG_INSTITUTION_DID) {
let issuer_did: String = match settings::get_config_value(CONFIG_INSTITUTION_DID) {
Ok(value) => value,
Err(err) => return err.into(),
};
Expand Down
Loading

0 comments on commit 30c32bc

Please sign in to comment.