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

Skip verifiers with zero or negative node weights #107

Merged
merged 1 commit into from
Nov 3, 2022
Merged
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
60 changes: 46 additions & 14 deletions libindy_vdr/src/pool/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ where
let (tx, rx) = unbounded();
let handle = RequestHandle::next();
let setup_ref = setup.as_ref();
let node_order = choose_nodes(&setup_ref.verifiers, setup_ref.node_weights.clone());
debug!("New {}: reqId({})", handle, req_id);
let node_order = choose_nodes(&setup_ref.verifiers, setup_ref.node_weights.as_ref());
debug!(
"New {}: reqId({}), node order: {:?}",
handle, req_id, node_order
);
networker.send(NetworkerEvent::NewRequest(handle, req_id, req_json, tx))?;
Ok(PoolRequestImpl::new(
handle, rx, setup, networker, node_order,
Expand Down Expand Up @@ -149,19 +152,21 @@ where

pub(crate) fn choose_nodes(
verifiers: &Verifiers,
weights: Option<HashMap<String, f32>>,
weights: Option<&HashMap<String, f32>>,
) -> Vec<String> {
let mut weighted = verifiers
.keys()
.map(|name| {
(
weights
.as_ref()
.and_then(|w| w.get(name))
.cloned()
.unwrap_or(1.0),
name.as_str(),
)
.filter_map(|name| {
let weight = weights
.as_ref()
.and_then(|w| w.get(name))
.copied()
.unwrap_or(1.0);
if weight <= 0.0 {
None
} else {
Some((weight, name.as_str()))
}
})
.collect::<Vec<(f32, &str)>>();
let mut rng = rand::thread_rng();
Expand All @@ -176,9 +181,36 @@ pub(crate) fn choose_nodes(
result
}

/*
#[cfg(test)]
mod tests {
use std::collections::HashMap;

use crate::pool::{VerifierInfo, Verifiers};

use super::*;

#[test]
fn test_choose_nodes() {
let test_verif = VerifierInfo {
client_addr: "127.0.0.1".into(),
node_addr: "127.0.0.1".into(),
public_key: "pk".into(),
enc_key: "ek".into(),
bls_key: None,
};
let mut verifiers = Verifiers::new();
verifiers.insert("a".into(), test_verif.clone());
verifiers.insert("b".into(), test_verif.clone());
verifiers.insert("c".into(), test_verif);

let mut weights = HashMap::new();
weights.insert("a".into(), 0.0);
weights.insert("b".into(), 0.000001);
let found = choose_nodes(&verifiers, Some(&weights));
assert_eq!(found, ["c", "b"]);
}

/*
// use crate::services::pool::events::MockUpdateHandler;
use crate::services::pool::networker::MockNetworker;
use crate::services::pool::request_handler::tests::MockRequestHandler;
Expand Down Expand Up @@ -833,5 +865,5 @@ mod tests {
assert_eq!(_get_f(7), 2);
}
}
*/
}
*/