Skip to content

Commit

Permalink
refactor(nns): pass begin: Bound<NeuronId> to schedule_prune_some_fol…
Browse files Browse the repository at this point in the history
…lowing. (#3061)

Instead of storing it in a global variable (technically, thread_local! +
RefCell).
  • Loading branch information
daniel-wong-dfinity-org authored Dec 9, 2024
1 parent 07ccfce commit a2e1862
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions rs/nns/governance/canister/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn set_governance(gov: Governance) {
fn schedule_timers() {
schedule_seeding(Duration::from_nanos(0));
schedule_adjust_neurons_storage(Duration::from_nanos(0), NeuronIdProto { id: 0 });
schedule_prune_following(Duration::from_secs(0));
schedule_prune_following(Duration::from_secs(0), Bound::Unbounded);
schedule_spawn_neurons();
schedule_unstake_maturity_of_dissolved_neurons();
schedule_vote_processing();
Expand Down Expand Up @@ -227,31 +227,17 @@ fn schedule_seeding(delay: Duration) {
});
}

thread_local! {
// The last neuron whose following was pruned (possibly, trivially, i.e. did
// not try to remove anything, because it refreshed recently enough).
static PRUNE_FOLLOWING_CHECKPOINT: RefCell<Bound<NeuronIdProto>> =
const { RefCell::new(Bound::Unbounded) };
}

fn schedule_prune_following(delay: Duration) {
fn schedule_prune_following(delay: Duration, original_begin: Bound<NeuronIdProto>) {
if !is_prune_following_enabled() {
return;
}

ic_cdk_timers::set_timer(delay, || {
let original_checkpoint = PRUNE_FOLLOWING_CHECKPOINT.with(|p| *p.borrow());

ic_cdk_timers::set_timer(delay, move || {
let carry_on =
|| call_context_instruction_counter() < MAX_PRUNE_SOME_FOLLOWING_INSTRUCTIONS;
let new_checkpoint = governance_mut().prune_some_following(original_checkpoint, carry_on);

PRUNE_FOLLOWING_CHECKPOINT.with(|p| {
let mut borrow = p.borrow_mut();
*borrow = new_checkpoint;
});
let new_begin = governance_mut().prune_some_following(original_begin, carry_on);

schedule_prune_following(PRUNE_FOLLOWING_INTERVAL);
schedule_prune_following(PRUNE_FOLLOWING_INTERVAL, new_begin);
});
}

Expand Down

0 comments on commit a2e1862

Please sign in to comment.