Skip to content

Commit

Permalink
refactor(nns): Make cast_vote_and_cascade_follow async (#2670)
Browse files Browse the repository at this point in the history
This gets us closer into the position where we can scale the voting
calculations across multiple messages.

[Prev](#2600) |
[Next](#2697)
  • Loading branch information
max-dfinity authored Nov 19, 2024
1 parent de1a078 commit 1d73be3
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 92 deletions.
2 changes: 2 additions & 0 deletions rs/nns/governance/benches/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ fn make_and_process_proposal(gov: &mut Governance) {
..Default::default()
},
)
.now_or_never()
.unwrap()
.unwrap();
gov.run_periodic_tasks().now_or_never();
}
Expand Down
13 changes: 8 additions & 5 deletions rs/nns/governance/src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5387,7 +5387,7 @@ impl Governance {
.collect()
}

pub fn make_proposal(
pub async fn make_proposal(
&mut self,
proposer_id: &NeuronId,
caller: &PrincipalId,
Expand Down Expand Up @@ -5592,7 +5592,8 @@ impl Governance {
);
self.heap_data.proposals.insert(proposal_num, proposal_data);

self.cast_vote_and_cascade_follow(proposal_id, *proposer_id, Vote::Yes, topic);
self.cast_vote_and_cascade_follow(proposal_id, *proposer_id, Vote::Yes, topic)
.await;

self.process_proposal(proposal_num);

Expand Down Expand Up @@ -5744,7 +5745,7 @@ impl Governance {
}
}

fn register_vote(
async fn register_vote(
&mut self,
neuron_id: &NeuronId,
caller: &PrincipalId,
Expand Down Expand Up @@ -5816,7 +5817,8 @@ impl Governance {
*neuron_id,
vote,
topic,
);
)
.await;

self.process_proposal(proposal_id.id);

Expand Down Expand Up @@ -6328,7 +6330,7 @@ impl Governance {
.follow(&id, caller, f)
.map(|_| ManageNeuronResponse::follow_response()),
Some(Command::MakeProposal(p)) => {
self.make_proposal(&id, caller, p).map(|proposal_id| {
self.make_proposal(&id, caller, p).await.map(|proposal_id| {
ManageNeuronResponse::make_proposal_response(
proposal_id,
"The proposal has been created successfully.".to_string(),
Expand All @@ -6337,6 +6339,7 @@ impl Governance {
}
Some(Command::RegisterVote(v)) => self
.register_vote(&id, caller, v)
.await
.map(|_| ManageNeuronResponse::register_vote_response()),
Some(Command::ClaimOrRefresh(_)) => {
panic!("This should have already returned")
Expand Down
11 changes: 5 additions & 6 deletions rs/nns/governance/src/governance/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
test_utils::{MockEnvironment, StubCMC, StubIcpLedger},
};
use canbench_rs::{bench, bench_fn, BenchResult};
use futures::FutureExt;
use ic_base_types::PrincipalId;
use ic_nns_common::{
pb::v1::{NeuronId as NeuronIdProto, ProposalId},
Expand Down Expand Up @@ -325,12 +326,10 @@ fn cast_vote_cascade_helper(strategy: SetUpStrategy, topic: Topic) -> BenchResul

let proposal_id = ProposalId { id: 1 };
bench_fn(|| {
governance.cast_vote_and_cascade_follow(proposal_id, neuron_id.into(), Vote::Yes, topic);
// let yes_votes = ballots
// .iter()
// .filter(|(id, ballot)| ballot.vote == Vote::Yes as i32)
// .count();
// panic!("Number of cascaded votes: {}, {}", ballots.len(), yes_votes)
governance
.cast_vote_and_cascade_follow(proposal_id, neuron_id.into(), Vote::Yes, topic)
.now_or_never()
.unwrap();
})
}

Expand Down
31 changes: 19 additions & 12 deletions rs/nns/governance/src/governance/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,7 @@ mod cast_vote_and_cascade_follow {
pb::v1::{neuron::Followees, Ballot, ProposalData, Topic, Vote},
test_utils::{MockEnvironment, StubCMC, StubIcpLedger},
};
use futures::FutureExt;
use ic_base_types::PrincipalId;
use ic_nns_common::pb::v1::{NeuronId, ProposalId};
use icp_ledger::Subaccount;
Expand Down Expand Up @@ -1251,12 +1252,15 @@ mod cast_vote_and_cascade_follow {
Box::new(StubCMC {}),
);

governance.cast_vote_and_cascade_follow(
ProposalId { id: 1 },
NeuronId { id: 1 },
Vote::Yes,
topic,
);
governance
.cast_vote_and_cascade_follow(
ProposalId { id: 1 },
NeuronId { id: 1 },
Vote::Yes,
topic,
)
.now_or_never()
.unwrap();

let deciding_voting_power = |neuron_id| {
governance
Expand Down Expand Up @@ -1338,12 +1342,15 @@ mod cast_vote_and_cascade_follow {
Box::new(StubCMC {}),
);

governance.cast_vote_and_cascade_follow(
ProposalId { id: 1 },
NeuronId { id: 1 },
Vote::Yes,
topic,
);
governance
.cast_vote_and_cascade_follow(
ProposalId { id: 1 },
NeuronId { id: 1 },
Vote::Yes,
topic,
)
.now_or_never()
.unwrap();

let deciding_voting_power = |neuron_id| {
governance
Expand Down
4 changes: 2 additions & 2 deletions rs/nns/governance/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ thread_local! {
static VOTING_STATE_MACHINES: RefCell<VotingStateMachines> = RefCell::new(VotingStateMachines::new());
}
impl Governance {
pub fn cast_vote_and_cascade_follow(
pub async fn cast_vote_and_cascade_follow(
&mut self,
proposal_id: ProposalId,
voting_neuron_id: NeuronId,
Expand Down Expand Up @@ -166,7 +166,7 @@ impl ProposalVotingStateMachine {
self.add_followers_to_check(neuron_store, neuron_id, self.topic);
}

// Optimization, will not cause tests to fail if removed
// Memory optimization, will not cause tests to fail if removed
retain_neurons_with_castable_ballots(&mut self.followers_to_check, ballots);

while let Some(follower) = self.followers_to_check.pop_first() {
Expand Down
5 changes: 3 additions & 2 deletions rs/nns/governance/tests/degraded_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async fn test_cannot_submit_motion_in_degraded_mode() {
})),
..Default::default()
},
),
).await,
Err(e) if e.error_type == ErrorType::ResourceExhausted as i32);
}

Expand All @@ -199,7 +199,8 @@ async fn test_can_submit_nns_canister_upgrade_in_degraded_mode() {
})),
..Default::default()
},
),
)
.await,
Ok(_)
);
}
Expand Down
2 changes: 2 additions & 0 deletions rs/nns/governance/tests/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,8 @@ impl ProposalNeuronBehavior {
..Default::default()
},
)
.now_or_never()
.unwrap()
.unwrap();
// Vote
for (voter, vote) in &self.votes {
Expand Down
4 changes: 4 additions & 0 deletions rs/nns/governance/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ impl ProposalNeuronBehavior {
..Default::default()
},
)
.now_or_never()
.unwrap()
.unwrap();
// Vote
for (voter, vote) in &self.votes {
Expand Down Expand Up @@ -816,6 +818,8 @@ impl NNS {
..Default::default()
},
)
.now_or_never()
.unwrap()
.unwrap()
}

Expand Down
Loading

0 comments on commit 1d73be3

Please sign in to comment.