-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically spawn neurons then disburse into the treasury (#5097)
- Loading branch information
Showing
22 changed files
with
217 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use crate::RuntimeState; | ||
|
||
pub mod refresh_neurons; | ||
pub mod process_neurons; | ||
|
||
pub(crate) fn start(_state: &RuntimeState) { | ||
refresh_neurons::start_job(); | ||
process_neurons::start_job(); | ||
} |
93 changes: 93 additions & 0 deletions
93
backend/canisters/neuron_controller/impl/src/jobs/process_neurons.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use crate::updates::manage_nns_neuron::manage_nns_neuron_impl; | ||
use crate::{mutate_state, read_state}; | ||
use ic_ledger_types::{AccountIdentifier, DEFAULT_SUBACCOUNT}; | ||
use nns_governance_canister::types::manage_neuron::{Command, Disburse, Spawn}; | ||
use nns_governance_canister::types::ListNeurons; | ||
use std::time::Duration; | ||
use tracing::info; | ||
use types::{Milliseconds, Timestamped}; | ||
use utils::canister_timers::run_now_then_interval; | ||
use utils::consts::SNS_GOVERNANCE_CANISTER_ID; | ||
use utils::time::DAY_IN_MS; | ||
|
||
const REFRESH_NEURONS_INTERVAL: Milliseconds = DAY_IN_MS; | ||
const E8S_PER_ICP: u64 = 100_000_000; | ||
|
||
pub fn start_job() { | ||
run_now_then_interval(Duration::from_millis(REFRESH_NEURONS_INTERVAL), run); | ||
} | ||
|
||
fn run() { | ||
ic_cdk::spawn(run_async()); | ||
} | ||
|
||
async fn run_async() { | ||
let nns_governance_canister_id = read_state(|state| state.data.nns_governance_canister_id); | ||
|
||
if let Ok(response) = nns_governance_canister_c2c_client::list_neurons( | ||
nns_governance_canister_id, | ||
&ListNeurons { | ||
neuron_ids: Vec::new(), | ||
include_neurons_readable_by_caller: true, | ||
}, | ||
) | ||
.await | ||
{ | ||
let now = read_state(|state| state.env.now()); | ||
|
||
let neurons_to_spawn: Vec<_> = response | ||
.full_neurons | ||
.iter() | ||
.filter(|n| n.maturity_e8s_equivalent > 1000 * E8S_PER_ICP) | ||
.filter_map(|n| n.id.as_ref().map(|id| id.id)) | ||
.collect(); | ||
|
||
let neurons_to_disburse: Vec<_> = response | ||
.full_neurons | ||
.iter() | ||
.filter(|n| n.is_dissolved(now) && n.cached_neuron_stake_e8s > 0) | ||
.filter_map(|n| n.id.as_ref().map(|id| id.id)) | ||
.collect(); | ||
|
||
mutate_state(|state| { | ||
state.data.neurons = Timestamped::new(response.full_neurons, now); | ||
}); | ||
|
||
if !neurons_to_spawn.is_empty() { | ||
spawn_neurons(neurons_to_spawn).await; | ||
} | ||
|
||
if !neurons_to_disburse.is_empty() { | ||
disburse_neurons(neurons_to_disburse).await; | ||
} | ||
} | ||
} | ||
|
||
async fn spawn_neurons(neuron_ids: Vec<u64>) { | ||
let cycles_minting_canister_id = read_state(|state| state.data.cycles_minting_canister_id); | ||
|
||
if let Ok(Ok(modulation)) = cycles_minting_canister_c2c_client::neuron_maturity_modulation(cycles_minting_canister_id).await | ||
{ | ||
// Only spawn when the modulation is at least 102.5% | ||
if modulation >= 250 { | ||
for neuron_id in neuron_ids { | ||
info!(neuron_id, "Spawning neuron from maturity"); | ||
manage_nns_neuron_impl(neuron_id, Command::Spawn(Spawn::default())).await; | ||
} | ||
} | ||
} | ||
} | ||
|
||
async fn disburse_neurons(neuron_ids: Vec<u64>) { | ||
for neuron_id in neuron_ids { | ||
info!(neuron_id, "Disbursing neuron"); | ||
manage_nns_neuron_impl( | ||
neuron_id, | ||
Command::Disburse(Disburse { | ||
to_account: Some(AccountIdentifier::new(&SNS_GOVERNANCE_CANISTER_ID, &DEFAULT_SUBACCOUNT)), | ||
amount: None, | ||
}), | ||
) | ||
.await; | ||
} | ||
} |
35 changes: 0 additions & 35 deletions
35
backend/canisters/neuron_controller/impl/src/jobs/refresh_neurons.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
mod manage_nns_neuron; | ||
mod stake_nns_neuron; | ||
mod wallet_receive; | ||
pub mod manage_nns_neuron; | ||
pub mod stake_nns_neuron; | ||
pub mod wallet_receive; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "cycles_minting_canister" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
candid = { workspace = true } | ||
candid_gen = { path = "../../../libraries/candid_gen" } | ||
serde = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod queries; | ||
|
||
pub use queries::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use candid_gen::generate_candid_method_no_args; | ||
|
||
fn main() { | ||
generate_candid_method_no_args!(cycles_minting, neuron_maturity_modulation, query); | ||
|
||
candid::export_service!(); | ||
std::print!("{}", __export_service()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod neuron_maturity_modulation; |
1 change: 1 addition & 0 deletions
1
backend/external_canisters/cmc/api/src/queries/neuron_maturity_modulation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub type Response = Result<i32, String>; |
Oops, something went wrong.