Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
bartkrak committed Feb 12, 2025
1 parent f25f79a commit da4a030
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
26 changes: 15 additions & 11 deletions neurons/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ fn main() {
let path = Utf8Path::new("data/trusted_for_user_per_round.json");
let trust_data = load_trust_data(path).unwrap();
let mut trust_graph_neurons: Vec<TrustGraphNeuron> = vec![];
trust_data.iter().for_each(|(round,trusted_for_user)|{
trust_graph_neurons.push(TrustGraphNeuron::from_data(trusted_for_user.clone(), *round));
trust_data.iter().for_each(|(round, trusted_for_user)| {
trust_graph_neurons.push(TrustGraphNeuron::from_data(
trusted_for_user.clone(),
*round,
));
});
let trust_graph_log: TrustHistoryNeuron = TrustHistoryNeuron::new((27,33));// todo make this automatic from loop above
let trust_graph_log: TrustHistoryNeuron = TrustHistoryNeuron::new((27, 33)); // todo make this automatic from loop above

// -- old for comparasion
let path = Utf8Path::new("data/trusted_for_user.json");
Expand All @@ -87,18 +90,19 @@ fn main() {
for trust_neuron in trust_graph_neurons {
neurons.push(Box::new(trust_neuron));
}

calculate_trust_neuron_results(
&users,
neurons
);

calculate_trust_neuron_results(&users, neurons);

calculate_neuron_results(
&users,
vec![Box::new(trust_graph_neuron), Box::new(prior_voting_history_neuron),Box::new(assigned_reputation_neuron), Box::new(trust_graph_log)]
vec![
Box::new(trust_graph_neuron),
Box::new(prior_voting_history_neuron),
Box::new(assigned_reputation_neuron),
Box::new(trust_graph_log),
],
);
do_normalize_votes();

}

fn do_normalize_votes() {
Expand All @@ -117,4 +121,4 @@ fn do_normalize_votes() {
"result/normalized_votes.json",
&to_sorted_map(normalized_votes),
);
}
}
15 changes: 12 additions & 3 deletions neurons/src/neurons/trust_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ impl TrustGraphNeuron {
let reader = BufReader::new(file);

let trusted_for_user: HashMap<String, Vec<String>> = serde_json::from_reader(reader)?;
Ok(Self { trusted_for_user, round:0 })
Ok(Self {
trusted_for_user,
round: 0,
})
}
pub fn from_data(trusted_for_user: HashMap<String, Vec<String>>, round: u32) -> Self {
Self { trusted_for_user, round }
Self {
trusted_for_user,
round,
}
}
}

Expand Down Expand Up @@ -122,7 +128,10 @@ mod tests {
trusted_for_user.insert("D".to_string(), vec!["A".to_string()]);
trusted_for_user.insert("E".to_string(), vec![]);

let trust_graph_neuron = TrustGraphNeuron { trusted_for_user, round: 0 };
let trust_graph_neuron = TrustGraphNeuron {
trusted_for_user,
round: 0,
};
let result = trust_graph_neuron.calculate_result(
&["A", "B", "C", "D", "E"]
.into_iter()
Expand Down
11 changes: 5 additions & 6 deletions neurons/src/neurons/trust_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::BufReader;
#[derive(Clone, Debug)]
pub struct TrustHistoryNeuron {
start: usize,
end: usize
end: usize,
}

impl TrustHistoryNeuron {
Expand Down Expand Up @@ -34,10 +34,10 @@ impl Neuron for TrustHistoryNeuron {
format!("trust_graph_neuron_log")
}

fn calculate_result(&self, users: &[String]) -> HashMap<String, f64> {
fn calculate_result(&self, _users: &[String]) -> HashMap<String, f64> {
let mut users_trust_history: HashMap<String, Vec<f64>> = HashMap::new();

for i in self.start..self.end+1 {
for i in self.start..self.end + 1 {
let path = format!("result/trust_graph_neuron_{}.json", i);
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
Expand All @@ -49,7 +49,7 @@ impl Neuron for TrustHistoryNeuron {
match users_trust_history.get_mut(user) {
Some(trust_vec) => {
trust_vec.push(*trust);
},
}
None => {
let _ = users_trust_history.insert(user.to_string(), vec![*trust]);
}
Expand Down Expand Up @@ -85,6 +85,5 @@ impl Neuron for TrustHistoryNeuron {
});

result

}
}
}
6 changes: 3 additions & 3 deletions offchain/src/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,20 @@ pub fn manual_tally(
contract_client.calculate_voting_powers();

// tally & write result to file
let mut results_map:Map<String, Value> = Map::new();
let mut results_map: Map<String, Value> = Map::new();
for (submission_id, _votes) in normalized_votes {
let submission_id_string = submission_id.to_string();
let result: i128 = match contract_client.tally_submission(&submission_id).to_i128() {
Some(result) => result,
None => panic!("i256 result of [{submission_id_string}] overflow i128"),
};
results_map.insert(submission_id_string,Value::String(result.to_string()));
results_map.insert(submission_id_string, Value::String(result.to_string()));
}
let serialized = serde_json::to_string(&results_map).unwrap();
fs::write(format!("result/voting_result.json"), serialized).unwrap();

// save voting powers to file
let mut powers_map:Map<String, Value> = Map::new();
let mut powers_map: Map<String, Value> = Map::new();
for (public_key, voting_power) in contract_client.get_voting_powers() {
let public_key_string = public_key.to_string();
let power = match voting_power.to_i128() {
Expand Down

0 comments on commit da4a030

Please sign in to comment.