Skip to content

Commit

Permalink
Vdrtools & Modular Libs as feature flags (#763)
Browse files Browse the repository at this point in the history
* Add feature flags to opt-in vdrtools or modular libs implementation

Signed-off-by: gmulhearn <gmulhearn@proton.me>
Co-authored-by: George Mulhearn <gmulhearn@anonyome.com>
  • Loading branch information
gmulhearn and gmulhearn-anonyome authored Apr 10, 2023
1 parent 2f45045 commit ae0ca7e
Show file tree
Hide file tree
Showing 25 changed files with 92 additions and 90 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ jobs:
uses: actions/checkout@v3
- name: "Setup rust codecov environment"
uses: ./.github/actions/setup-codecov-rust
- name: "Run workspace tests: modular_dependencies pool_tests agency_pool_tests"
- name: "Run workspace tests: modular_libs_tests pool_tests agency_pool_tests"
run: |
RUSTFLAGS='-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests' \
RUSTDOCFLAGS='-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests' \
RUST_TEST_THREADS=1 CARGO_INCREMENTAL=0 TEST_POOL_IP=127.0.0.1 cargo test --package aries-vcx -F 'pool_tests agency_pool_tests modular_dependencies';
RUST_TEST_THREADS=1 CARGO_INCREMENTAL=0 TEST_POOL_IP=127.0.0.1 cargo test --package aries-vcx -F 'pool_tests agency_pool_tests modular_libs_tests';
mkdir -p /tmp/artifacts/coverage
grcov ./target/debug/ -s . -t lcov --llvm --branch --ignore-not-existing -o /tmp/artifacts/coverage/coverage.lcov
Expand Down
2 changes: 1 addition & 1 deletion agents/rust/aries-vcx-agent/src/agent/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Agent {
let config_issuer = wallet_configure_issuer(wallet_handle, &init_config.enterprise_seed)
.await
.unwrap();
init_issuer_config(&config_issuer).unwrap();
init_issuer_config(&config_issuer.institution_did).unwrap();

let pool_config = PoolConfigBuilder::default()
.genesis_path(&init_config.pool_config.genesis_path)
Expand Down
30 changes: 24 additions & 6 deletions aries_vcx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,32 @@ path = "src/lib.rs"
doctest = false

[features]
test_utils = [ "messages/test_utils" ]
# By default, vdrtools is enabled to allow aries_vcx usage with vdrtools-based wallets/anoncreds/ledgers
default = ["vdrtools"]
########################## DEP FLAGS ################################
# Feature flag to include the libvdrtools dependency
vdrtools = ["dep:libvdrtools"]
# Feature flag to include the 'modular library' dependencies (vdrtools alternatives; indy-vdr, indy-credx)
modular_libs = ["dep:indy-vdr", "dep:indy-credx"]
########################## TEST FLAGS ###############################
# Feature flag for base requirments for running tests underneath the `test_utils` flag (most tests).
# Both `vdrtools` and `modular_libs` are transitively enabled, so they can be used for tests (including interop tests).
test_utils = [ "messages/test_utils", "vdrtools", "modular_libs" ]
# Feature flag for categorising tests which require a ledger to be running
pool_tests = [ "test_utils" ]
# Feature flag for categorising tests which require a ledger and messaging agency to be running
agency_pool_tests = [ "test_utils" ]
# Feature flag for categorising general tests (usually unit tests)
general_test = [ "test_utils", "messages/general_test" ]
# Feature flag to enable experimental tests of a mysql wallet
mysql_test = ["test_utils" ]
# Feature flag used to prompt some tests to run using the `modular_libs` dependencies instead of `vdrtools`
modular_libs_tests = ["test_utils", "tokio/rt", "tokio/macros"]
########################## OTHER FLAGS ##############################
# Feature flag to make compiler warnings into failures
fatal_warnings = []
# Feature flag to log some extra warnings if enabled
warnlog_fetched_messages = []
mysql_test = ["test_utils" ]
modular_dependencies = ["test_utils", "tokio/rt", "tokio/macros"]

[dependencies]
bs58 = "0.4.0"
Expand All @@ -35,9 +53,9 @@ serde_derive = "1.0.97"
regex = "1.1.0"
base64 = "0.10"
openssl = { version = "0.10.48" }
libvdrtools = { path = "../libvdrtools" }
libvdrtools = { path = "../libvdrtools", optional = true }
# vdrtools alternatives ----
indy-vdr = { version = "0.3.4", default-features = false, features = ["ffi", "log"] }
indy-vdr = { version = "0.3.4", default-features = false, features = ["ffi", "log"], optional = true }
# PATCH (TO MONITOR IN FUTURE): The following patch changes the `indy-data-types` (within indy-shared-rs) to depend on
# `ursa "0.3.6"` rather than `ursa "=0.3.6"`. Currently, `libvdrtools` depends on `ursa "0.3.7"`, which causes a mismatch of
# `indy-utils` versions, which causes some types within credx to fail. Details about the issue can be found here: https://github.com/hyperledger/indy-shared-rs/issues/20
Expand All @@ -50,7 +68,7 @@ indy-vdr = { version = "0.3.4", default-features = false, features = ["ffi", "lo
# - monitor anoncred-rs (which will replace indy-credx) as the fix will likely go in here,
# - monitor the issue for other fixes from the maintainers: https://github.com/hyperledger/indy-shared-rs/issues/20
# - update libvdrtools to use =0.3.6 ursa
indy-credx = { git = "https://github.com/anonyome/indy-shared-rs.git", rev = "7342bc624d23ece8845d1a701cd2cdc9cd401bb0" }
indy-credx = { git = "https://github.com/anonyome/indy-shared-rs.git", rev = "7342bc624d23ece8845d1a701cd2cdc9cd401bb0", optional = true }
# ----------------------
futures = { version = "0.3", default-features = false }
libloading = "0.5.0"
Expand Down
4 changes: 2 additions & 2 deletions aries_vcx/src/common/primitives/credential_definition.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::core::profile::profile::Profile;
use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};
use crate::indy::utils::LibindyMock;
use crate::plugins::ledger::base_ledger::BaseLedger;
use crate::utils::constants::{CRED_DEF_ID, CRED_DEF_JSON, DEFAULT_SERIALIZE_VERSION};
use crate::utils::mockdata::mock_settings::StatusCodeMock;
use crate::utils::serialization::ObjectWithVersion;

use crate::global::settings::{self, indy_mocks_enabled};
Expand Down Expand Up @@ -110,7 +110,7 @@ async fn _try_get_cred_def_from_ledger(
// TODO - future - may require more customized logic. We set the rc to 309, as the mock for ledger.get_cred_def will return a valid
// mock cred def unless it reads an rc of 309. Returning a valid mock cred def will result in this method returning an error.
if indy_mocks_enabled() {
LibindyMock::set_next_result(309)
StatusCodeMock::set_next_result(309)
}
match ledger.get_cred_def(cred_def_id, Some(issuer_did)).await {
Ok(cred_def) => Ok(Some(cred_def)),
Expand Down
2 changes: 2 additions & 0 deletions aries_vcx/src/core/profile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "modular_libs")]
pub mod modular_libs_profile;
pub mod profile;
#[cfg(feature = "vdrtools")]
pub mod vdrtools_profile;
3 changes: 3 additions & 0 deletions aries_vcx/src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
pub mod error;
mod mapping_agency_client;
#[cfg(feature = "modular_libs")]
mod mapping_credx;
mod mapping_diddoc;
#[cfg(feature = "modular_libs")]
mod mapping_indyvdr;
mod mapping_messages;
mod mapping_others;
#[cfg(feature = "vdrtools")]
mod mapping_vdrtools;
5 changes: 2 additions & 3 deletions aries_vcx/src/global/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::HashMap;
use std::sync::RwLock;

use crate::errors::error::prelude::*;
use crate::indy::wallet::IssuerConfig;

pub static CONFIG_POOL_NAME: &str = "pool_name";
pub static CONFIG_SDK_TO_REMOTE_ROLE: &str = "sdk_to_remote_role";
Expand Down Expand Up @@ -185,7 +184,7 @@ pub mod unit_tests {
}
}

pub fn init_issuer_config(config: &IssuerConfig) -> VcxResult<()> {
set_config_value(CONFIG_INSTITUTION_DID, &config.institution_did)?;
pub fn init_issuer_config(institution_did: &str) -> VcxResult<()> {
set_config_value(CONFIG_INSTITUTION_DID, institution_did)?;
Ok(())
}
4 changes: 2 additions & 2 deletions aries_vcx/src/indy/credentials/issuer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use vdrtools::WalletHandle;
use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};
use crate::global::settings;
use crate::indy::anoncreds;
use crate::indy::utils::LibindyMock;
use crate::utils;
use crate::utils::constants::LIBINDY_CRED_OFFER;
use crate::utils::mockdata::mock_settings::StatusCodeMock;
use crate::utils::parse_and_validate;

pub async fn libindy_issuer_create_credential_offer(
wallet_handle: WalletHandle,
cred_def_id: &str,
) -> VcxResult<String> {
if settings::indy_mocks_enabled() {
let rc = LibindyMock::get_result();
let rc = StatusCodeMock::get_result();
if rc != 0 {
return Err(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidState,
Expand Down
36 changes: 0 additions & 36 deletions aries_vcx/src/indy/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;

use vdrtools::CommandHandle;

use crate::global::settings;

pub mod mocks;

static COMMAND_HANDLE_COUNTER: AtomicUsize = AtomicUsize::new(1);
Expand All @@ -13,38 +9,6 @@ pub fn next_command_handle() -> CommandHandle {
(COMMAND_HANDLE_COUNTER.fetch_add(1, Ordering::SeqCst) + 1) as CommandHandle
}

lazy_static! {
static ref LIBINDY_MOCK: Mutex<LibindyMock> = Mutex::new(LibindyMock::default());
}

#[derive(Default)]
pub struct LibindyMock {
results: Vec<u32>,
}

// todo: get rid of this, we no longer deal with rc return codes from vdrtools
// (this is leftover from times when we talked to vdrtool via FFI)
impl LibindyMock {
pub fn set_next_result(rc: u32) {
if settings::indy_mocks_enabled() {
LIBINDY_MOCK
.lock()
.expect("Unabled to access LIBINDY_MOCK")
.results
.push(rc);
}
}

pub fn get_result() -> u32 {
LIBINDY_MOCK
.lock()
.expect("Unable to access LIBINDY_MOCK")
.results
.pop()
.unwrap_or_default()
}
}

// TODO: move to devsetup, see if we can reuse this / merge with different setup
#[cfg(feature = "test_utils")]
pub mod test_setup {
Expand Down
2 changes: 2 additions & 0 deletions aries_vcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
pub extern crate agency_client;

// TODO: remove reexports
#[cfg(feature = "vdrtools")]
pub extern crate vdrtools;

#[macro_use]
Expand Down Expand Up @@ -42,6 +43,7 @@ pub mod utils;
pub mod handlers;

pub mod global;
#[cfg(feature = "vdrtools")]
pub mod indy;
pub mod protocols;

Expand Down
2 changes: 0 additions & 2 deletions aries_vcx/src/plugins/anoncreds/indy_anoncreds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use async_trait::async_trait;
use vdrtools::{PoolHandle, WalletHandle};

Expand Down
2 changes: 2 additions & 0 deletions aries_vcx/src/plugins/anoncreds/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod base_anoncreds;
#[cfg(feature = "modular_libs")]
pub mod credx_anoncreds;
#[cfg(feature = "vdrtools")]
pub mod indy_anoncreds;
4 changes: 0 additions & 4 deletions aries_vcx/src/plugins/ledger/indy_ledger.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::sync::Arc;

use async_trait::async_trait;
use vdrtools::{PoolHandle, WalletHandle};

use crate::core::profile::vdrtools_profile::VdrtoolsProfile;

use crate::common::primitives::revocation_registry::RevocationRegistryDefinition;
use crate::errors::error::VcxResult;
use crate::indy;
Expand Down
2 changes: 2 additions & 0 deletions aries_vcx/src/plugins/ledger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod base_ledger;
#[cfg(feature = "vdrtools")]
pub mod indy_ledger;
#[cfg(feature = "modular_libs")]
pub mod indy_vdr_ledger;
1 change: 1 addition & 0 deletions aries_vcx/src/plugins/wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod agency_client_wallet;
pub mod base_wallet;
#[cfg(feature = "vdrtools")]
pub mod indy_wallet;
6 changes: 3 additions & 3 deletions aries_vcx/src/utils/devsetup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl SetupWalletPool {

impl SetupProfile {
pub(self) fn should_run_modular() -> bool {
cfg!(feature = "modular_dependencies")
cfg!(feature = "modular_libs_tests")
}

pub async fn init() -> SetupProfile {
Expand Down Expand Up @@ -569,7 +569,7 @@ pub async fn setup_issuer_wallet_and_agency_client() -> (String, WalletHandle, A
create_wallet_with_master_secret(&config_wallet).await.unwrap();
let wallet_handle = open_wallet(&config_wallet).await.unwrap();
let config_issuer = wallet_configure_issuer(wallet_handle, enterprise_seed).await.unwrap();
init_issuer_config(&config_issuer).unwrap();
init_issuer_config(&config_issuer.institution_did).unwrap();
let mut agency_client = AgencyClient::new();

let wallet: Arc<dyn BaseWallet> = Arc::new(IndySdkWallet::new(wallet_handle));
Expand All @@ -596,7 +596,7 @@ pub async fn setup_issuer_wallet() -> (String, WalletHandle) {
create_wallet_with_master_secret(&config_wallet).await.unwrap();
let wallet_handle = open_wallet(&config_wallet).await.unwrap();
let config_issuer = wallet_configure_issuer(wallet_handle, enterprise_seed).await.unwrap();
init_issuer_config(&config_issuer).unwrap();
init_issuer_config(&config_issuer.institution_did).unwrap();
(config_issuer.institution_did, wallet_handle)
}

Expand Down
31 changes: 30 additions & 1 deletion aries_vcx/src/utils/mockdata/mock_settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::sync::RwLock;
use std::{collections::HashMap, sync::Mutex};

use crate::errors::error::{AriesVcxError, VcxResult};

Expand All @@ -10,6 +10,9 @@ static MOCKED_VALIDATE_INDY_PROOF: &str = "mocked_validate_indy_proof";
lazy_static! {
static ref MOCK_SETTINGS: RwLock<HashMap<String, String>> = RwLock::new(HashMap::new());
static ref MOCK_SETTINGS_RESULT_BOOL: RwLock<HashMap<String, VcxResult<bool>>> = RwLock::new(HashMap::new());
// todo: get rid of this, we no longer deal with rc return codes from vdrtools
// (this is leftover from times when we talked to vdrtool via FFI)
static ref STATUS_CODE_MOCK: Mutex<StatusCodeMock> = Mutex::new(StatusCodeMock::default());
}

pub struct MockBuilder; // empty
Expand Down Expand Up @@ -84,3 +87,29 @@ pub fn get_mock_result_for_validate_indy_proof() -> Option<VcxResult<bool>> {
Err(err) => Err(AriesVcxError::from_msg(err.kind(), err.to_string())),
})
}

#[derive(Default)]
pub struct StatusCodeMock {
results: Vec<u32>,
}

// todo: get rid of this, we no longer deal with rc return codes from vdrtools
// (this is leftover from times when we talked to vdrtool via FFI)
impl StatusCodeMock {
pub fn set_next_result(rc: u32) {
STATUS_CODE_MOCK
.lock()
.expect("Unabled to access LIBINDY_MOCK")
.results
.push(rc);
}

pub fn get_result() -> u32 {
STATUS_CODE_MOCK
.lock()
.expect("Unable to access LIBINDY_MOCK")
.results
.pop()
.unwrap_or_default()
}
}
5 changes: 2 additions & 3 deletions aries_vcx/src/utils/mockdata/profile/mock_anoncreds.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use async_trait::async_trait;

use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};
use crate::utils::mockdata::mock_settings::StatusCodeMock;
use crate::{
global::settings,
indy::utils::LibindyMock,
plugins::anoncreds::base_anoncreds::BaseAnonCreds,
utils::{
self,
Expand Down Expand Up @@ -65,8 +65,7 @@ impl BaseAnonCreds for MockAnoncreds {
}

async fn issuer_create_credential_offer(&self, _cred_def_id: &str) -> VcxResult<String> {
let rc = LibindyMock::get_result();
if rc != 0 {
if StatusCodeMock::get_result() != 0 {
return Err(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidState,
"Mocked error result of issuer_create_credential_offer: issuer_create_credential_offer",
Expand Down
5 changes: 2 additions & 3 deletions aries_vcx/src/utils/mockdata/profile/mock_ledger.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use async_trait::async_trait;

use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};
use crate::utils::mockdata::mock_settings::StatusCodeMock;
use crate::{
common::primitives::revocation_registry::RevocationRegistryDefinition,
indy::utils::LibindyMock,
plugins::ledger::base_ledger::BaseLedger,
utils::{
self,
Expand Down Expand Up @@ -71,8 +71,7 @@ impl BaseLedger for MockLedger {
async fn get_cred_def(&self, cred_def_id: &str, submitter_did: Option<&str>) -> VcxResult<String> {
// TODO - FUTURE - below error is required for tests to pass which require a cred def to not exist (libvcx)
// ideally we can migrate away from it
let rc = LibindyMock::get_result();
if rc == 309 {
if StatusCodeMock::get_result() == 309 {
return Err(AriesVcxError::from_msg(
AriesVcxErrorKind::LedgerItemNotFound,
"Mocked error".to_string(),
Expand Down
Loading

0 comments on commit ae0ca7e

Please sign in to comment.