Skip to content

Commit

Permalink
feat(cli): Add verbose option to print the node replacement search
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-tomic committed Dec 1, 2022
1 parent 43bf059 commit fd3089d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 13 deletions.
10 changes: 10 additions & 0 deletions rs/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ pub(crate) mod subnet {
/// regardless of the decentralization score
#[clap(long, takes_value = true, multiple_values = true)]
include: Vec<PrincipalId>,

/// More verbose execution. For instance, print logs from the
/// backend.
#[clap(long)]
verbose: bool,
},

/// Extends the size of the subnet
Expand All @@ -122,6 +127,11 @@ pub(crate) mod subnet {
/// Motivation for extending the subnet
#[clap(short, long)]
motivation: Option<String>,

/// More verbose execution. For instance, print logs from the
/// backend.
#[clap(long)]
verbose: bool,
},
}
}
9 changes: 5 additions & 4 deletions rs/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ async fn main() -> Result<(), anyhow::Error> {
motivation,
exclude,
include,
min_nakamoto_coefficients
min_nakamoto_coefficients,
verbose,
} => {
let min_nakamoto_coefficients = parse_min_nakamoto_coefficients(&mut cmd, min_nakamoto_coefficients);

Expand Down Expand Up @@ -92,17 +93,17 @@ async fn main() -> Result<(), anyhow::Error> {
exclude: exclude.clone().into(),
include: include.clone().into(),
min_nakamoto_coefficients,
})
}, *verbose)
.await
}
cli::subnet::Commands::Extend { size, include, exclude, motivation } => {
cli::subnet::Commands::Extend { size, include, exclude, motivation, verbose, } => {
if let Some(motivation) = motivation.clone() {
runner.subnet_extend(ic_management_types::requests::SubnetExtendRequest {
subnet: subnet.id.unwrap(),
size: *size,
exclude: exclude.clone().into(),
include: include.clone().into(),
}, motivation).await
}, motivation, *verbose).await
} else {
cmd.error(
ErrorKind::MissingRequiredArgument,
Expand Down
12 changes: 12 additions & 0 deletions rs/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ impl Runner {
&self,
request: ic_management_types::requests::SubnetExtendRequest,
motivation: String,
verbose: bool,
) -> anyhow::Result<()> {
let subnet = request.subnet;
let change = self.dashboard_backend_client.subnet_extend(request).await?;
if verbose {
if let Some(run_log) = &change.run_log {
println!("{}\n", run_log.join("\n"));
}
}
println!("{}", change);

self.with_confirmation(|r| {
Expand All @@ -63,8 +69,14 @@ impl Runner {
pub async fn membership_replace(
&self,
request: ic_management_types::requests::MembershipReplaceRequest,
verbose: bool,
) -> anyhow::Result<()> {
let change = self.dashboard_backend_client.membership_replace(request).await?;
if verbose {
if let Some(run_log) = &change.run_log {
println!("{}\n", run_log.join("\n"));
}
}
println!("{}", change);

self.with_confirmation(|r| {
Expand Down
2 changes: 2 additions & 0 deletions rs/decentralization/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct SubnetChangeResponse {
pub score_after: nakamoto::NakamotoScore,
pub motivation: Option<String>,
pub comment: Option<String>,
pub run_log: Option<Vec<String>>,
pub feature_diff: HashMap<NodeFeature, FeatureDiff>,
}

Expand Down Expand Up @@ -47,6 +48,7 @@ impl From<&network::SubnetChange> for SubnetChangeResponse {
score_after: nakamoto::NakamotoScore::new_from_nodes(&change.new_nodes),
motivation: None,
comment: change.comment.clone(),
run_log: Some(change.run_log.clone()),
feature_diff: change.new_nodes.iter().fold(
change.old_nodes.iter().fold(
NodeFeature::variants()
Expand Down
6 changes: 5 additions & 1 deletion rs/decentralization/src/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,13 @@ impl Display for NakamotoScore {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"NakamotoScore: min {:0.2} avg log2 {:0.2} crit feat {} crit nodes {} avg linear {:0.2} all coeff {:?}",
"NakamotoScore: min {:0.2} avg log2 {:0.2} crit feat {} crit nodes {} avg linear {:0.2} all coeff {:?} details {:?}",
self.min,
self.avg_log2,
self.coefficients.values().filter(|c| **c < 3.0).count(),
self.control_power_critical_features().unwrap_or(0),
self.avg_linear,
self.coefficients.iter(),
self.value_counts,
)
}
Expand Down Expand Up @@ -452,6 +453,7 @@ mod tests {
nodes: new_test_nodes("feat", num_nodes, num_dfinity_nodes),
min_nakamoto_coefficients: None,
comment: None,
run_log: Vec::new(),
}
}

Expand All @@ -474,6 +476,7 @@ mod tests {
),
min_nakamoto_coefficients: None,
comment: None,
run_log: Vec::new(),
}
}

Expand Down Expand Up @@ -709,6 +712,7 @@ mod tests {
.collect(),
min_nakamoto_coefficients: None,
comment: None,
run_log: Vec::new(),
};

let available_nodes = json_file_read_checked::<Vec<ic_management_types::Node>>(&d.join("available-nodes.json"));
Expand Down
34 changes: 29 additions & 5 deletions rs/decentralization/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub struct DecentralizedSubnet {
pub nodes: Vec<Node>,
pub min_nakamoto_coefficients: Option<MinNakamotoCoefficients>,
pub comment: Option<String>,
pub run_log: Vec<String>,
}

impl DecentralizedSubnet {
Expand All @@ -130,6 +131,11 @@ impl DecentralizedSubnet {
nodes: new_subnet_nodes,
min_nakamoto_coefficients: self.min_nakamoto_coefficients.clone(),
comment: self.comment.clone(),
run_log: {
let mut run_log = self.run_log.clone();
run_log.push(format!("Remove nodes {:?}", removed.iter().map(|n| n.id)));
run_log
},
},
removed,
))
Expand All @@ -138,9 +144,14 @@ impl DecentralizedSubnet {
pub fn add_nodes(&self, nodes: Vec<Node>) -> Self {
Self {
id: self.id,
nodes: self.nodes.clone().into_iter().chain(nodes).collect(),
nodes: self.nodes.clone().into_iter().chain(nodes.clone()).collect(),
min_nakamoto_coefficients: self.min_nakamoto_coefficients.clone(),
comment: self.comment.clone(),
run_log: {
let mut run_log = self.run_log.clone();
run_log.push(format!("Remove nodes {:?}", nodes.iter().map(|n| n.id)));
run_log
},
}
}

Expand Down Expand Up @@ -319,7 +330,11 @@ impl DecentralizedSubnet {
business_rules_log: Vec<String>,
}

for _ in 0..num_nodes_to_add {
for i in 0..num_nodes_to_add {
run_log.push("***********************************************************".to_string());
run_log.push(format!("*** Adding node {}/{}", i + 1, num_nodes_to_add));
run_log.push("***********************************************************".to_string());

let mut sorted_good_nodes: Vec<SortResult> = nodes_available
.iter()
.enumerate()
Expand Down Expand Up @@ -381,10 +396,13 @@ impl DecentralizedSubnet {
})
.collect();

println!("Sorted candidate nodes, with the best candidate at the end:");
println!(" <node-id> <penalty> <Nakamoto score>");
run_log.push("Sorted candidate nodes, with the best candidate at the end:".to_string());
run_log.push(
" <node-id> <penalty> <Nakamoto score>"
.to_string(),
);
for s in &sorted_good_nodes {
println!(" -=> {} {} {}", s.node.id, s.penalty, s.score);
run_log.push(format!(" -=> {} {} {}", s.node.id, s.penalty, s.score));
}
// TODO: if more than one candidate returns the same nakamoto score, pick the
// one that improves the feature diversity
Expand Down Expand Up @@ -420,6 +438,7 @@ impl DecentralizedSubnet {
nodes: nodes_after_extension,
min_nakamoto_coefficients: self.min_nakamoto_coefficients.clone(),
comment,
run_log,
})
}
}
Expand Down Expand Up @@ -449,6 +468,7 @@ impl From<&ic_management_types::Subnet> for DecentralizedSubnet {
nodes: s.nodes.iter().map(Node::from).collect(),
min_nakamoto_coefficients: None,
comment: None,
run_log: Vec::new(),
}
}
}
Expand Down Expand Up @@ -641,6 +661,7 @@ impl SubnetChangeRequest {
new_nodes: extended_subnet.nodes,
min_nakamoto_coefficients: self.min_nakamoto_coefficients.clone(),
comment: extended_subnet.comment,
run_log: extended_subnet.run_log,
};
info!("Subnet {} extend {}", self.subnet.id, subnet_change);
Ok(subnet_change)
Expand Down Expand Up @@ -729,6 +750,7 @@ pub struct SubnetChange {
pub new_nodes: Vec<Node>,
pub min_nakamoto_coefficients: Option<MinNakamotoCoefficients>,
pub comment: Option<String>,
pub run_log: Vec<String>,
}

impl SubnetChange {
Expand All @@ -754,6 +776,7 @@ impl SubnetChange {
nodes: self.old_nodes.clone(),
min_nakamoto_coefficients: self.min_nakamoto_coefficients.clone(),
comment: self.comment.clone(),
run_log: Vec::new(),
}
}

Expand All @@ -763,6 +786,7 @@ impl SubnetChange {
nodes: self.new_nodes.clone(),
min_nakamoto_coefficients: self.min_nakamoto_coefficients.clone(),
comment: self.comment.clone(),
run_log: self.run_log.clone(),
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions rs/ic-management-backend/src/endpoints/query_decentralization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct SubnetRequest {
struct DecentralizedSubnetResponse {
id: PrincipalId,
message: String,
run_log: String,
nakamoto: decentralization::nakamoto::NakamotoScore
}

Expand Down Expand Up @@ -51,7 +52,7 @@ async fn get_decentralization_analysis(
nodes_to_remove: Option<Vec<PrincipalId>>,
min_nakamoto_coefficients: Option<MinNakamotoCoefficients>
) -> Result<HttpResponse, Error> {
let subnets = registry.read().await.subnets();
let subnets = registry.read().await.subnets();
let nodes = registry.read().await.nodes();

let original_subnet = subnet.map(|subnet_id| {
Expand All @@ -62,20 +63,23 @@ async fn get_decentralization_analysis(
nodes: subnet.nodes.iter().map(decentralization::network::Node::from).collect(),
min_nakamoto_coefficients: min_nakamoto_coefficients.clone(),
comment: None,
run_log: Vec::new(),
}
},
None => DecentralizedSubnet {
id: PrincipalId::new_subnet_test_id(0),
nodes: Vec::new(),
min_nakamoto_coefficients: min_nakamoto_coefficients.clone(),
comment: None,
run_log: Vec::new(),
}
}
}).unwrap_or_else(|| DecentralizedSubnet {
id: PrincipalId::new_subnet_test_id(0),
nodes: Vec::new(),
min_nakamoto_coefficients: min_nakamoto_coefficients.clone(),
comment: None,
run_log: Vec::new(),
});

let updated_subnet = match &nodes_to_remove {
Expand All @@ -99,13 +103,15 @@ async fn get_decentralization_analysis(
old_nodes: original_subnet.nodes,
new_nodes: updated_subnet.nodes.clone(),
min_nakamoto_coefficients: updated_subnet.min_nakamoto_coefficients.clone(),
comment: updated_subnet.comment.clone()
comment: updated_subnet.comment.clone(),
run_log: updated_subnet.run_log.clone(),
};

let response = DecentralizedSubnetResponse {
id: subnet.unwrap_or_else(|| PrincipalId::new_subnet_test_id(0)),
message: format!("{}", SubnetChangeResponse::from(&subnet_change)),
nakamoto: updated_subnet.nakamoto_score()
nakamoto: updated_subnet.nakamoto_score(),
run_log: subnet_change.run_log.join("\n"),
};
Ok(HttpResponse::Ok().json(&response))
}
2 changes: 2 additions & 0 deletions rs/ic-management-backend/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ impl SubnetQuerier for RegistryState {
nodes: s.nodes.iter().map(decentralization::network::Node::from).collect(),
min_nakamoto_coefficients: None,
comment: None,
run_log: Vec::new(),
})
.ok_or(NetworkError::SubnetNotFound(*id))
}
Expand Down Expand Up @@ -580,6 +581,7 @@ impl SubnetQuerier for RegistryState {
.collect(),
min_nakamoto_coefficients: None,
comment: None,
run_log: Vec::new(),
})
} else {
Err(NetworkError::IllegalRequest("no subnet found".to_string()))
Expand Down

0 comments on commit fd3089d

Please sign in to comment.