Skip to content

Commit

Permalink
Enforce a limit of 100 registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Nov 21, 2024
1 parent 70ca895 commit e32b298
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
1 change: 1 addition & 0 deletions canister/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type RegisterNeuronPairError = variant {
ErrorCallingGovernanceCanister : record { int32; text };
NotPermittedToVote;
AlreadyRegistered;
RegistrationLimitExceeded : nat32;
GovernanceError : record { int32; text };
};
type Result = variant { Ok : nat64; Err : RegisterNeuronPairError };
Expand Down
3 changes: 2 additions & 1 deletion canister/src/jobs/check_for_new_nns_votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ async fn run() {
log("Checking for new NNS votes");

let futures: Vec<_> = state::mutate(|s| {
s.iter_neuron_pairs()
s.neuron_pairs()
.values()
.map(|p| run_single(p.id(), s.nns_governance_canister_id(), p.nns_neuron_id()))
.collect()
});
Expand Down
3 changes: 2 additions & 1 deletion canister/src/jobs/process_votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ async fn process_vote(vote: VoteToProcess) {
}
VoteToProcess::PendingWtnVote(pair_id, wtn_vote) => {
let Some((canister_id, neuron_id)) = state::read(|s| {
s.neuron_pair(pair_id)
s.neuron_pairs()
.get(&pair_id)
.map(|p| (s.wtn_governance_canister_id(), p.wtn_neuron_id()))
}) else {
return;
Expand Down
1 change: 1 addition & 0 deletions canister/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct RegisterNeuronPairArgs {
enum RegisterNeuronPairError {
AlreadyRegistered,
NotPermittedToVote,
RegistrationLimitExceeded(u32),
GovernanceError(i32, String),
ErrorCallingGovernanceCanister(i32, String),
}
Expand Down
2 changes: 1 addition & 1 deletion canister/src/queries/list_neuron_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ use ic_cdk::query;

#[query]
fn list_neuron_pairs() -> Vec<NeuronPairPublic> {
state::read(|s| s.list_neuron_pairs())
state::read(|s| s.neuron_pairs().values().map(|pair| pair.into()).collect())
}
14 changes: 3 additions & 11 deletions canister/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::logs::log;
use crate::neuron_pair::NeuronPair;
use crate::{InitArgs, NeuronPairPublic, NnsVote, VoteToProcess, WtnVote};
use crate::{InitArgs, NnsVote, VoteToProcess, WtnVote};
use ic_principal::Principal;
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
Expand Down Expand Up @@ -84,10 +84,6 @@ impl State {
self.wtn_protocol_canister_id
}

pub fn list_neuron_pairs(&self) -> Vec<NeuronPairPublic> {
self.neuron_pairs.values().map(|p| p.into()).collect()
}

pub fn register_neuron_pair(
&mut self,
caller: Principal,
Expand Down Expand Up @@ -120,12 +116,8 @@ impl State {
}
}

pub fn neuron_pair(&self, pair_id: u64) -> Option<&NeuronPair> {
self.neuron_pairs.get(&pair_id)
}

pub fn iter_neuron_pairs(&self) -> impl Iterator<Item = &NeuronPair> {
self.neuron_pairs.values()
pub fn neuron_pairs(&self) -> &BTreeMap<u64, NeuronPair> {
&self.neuron_pairs
}

pub fn record_nns_vote(&mut self, pair_id: u64, vote: NnsVote) {
Expand Down
31 changes: 29 additions & 2 deletions canister/src/updates/register_neuron_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ use ic_principal::Principal;
use serde::{Deserialize, Serialize};

const REGISTER_VOTE_PERMISSION: i32 = 4;
const REGISTRATIONS_LIMIT: u32 = 100;

#[update]
async fn register_neuron_pair(
args: RegisterNeuronPairArgs,
) -> Result<u64, RegisterNeuronPairError> {
let caller = ic_cdk::caller();
let wtn_governance_canister = state::read(|s| s.wtn_governance_canister_id());
let PrepareSuccess {
caller,
wtn_governance_canister,
} = match prepare() {
Ok(success) => success,
Err(error) => return Err(error),
};

match call_get_neuron(wtn_governance_canister, args.wtn_neuron_id).await {
Ok(response) => match response.0.result.unwrap() {
Expand Down Expand Up @@ -49,6 +55,27 @@ async fn register_neuron_pair(
}
}

struct PrepareSuccess {
caller: Principal,
wtn_governance_canister: Principal,
}

fn prepare() -> Result<PrepareSuccess, RegisterNeuronPairError> {
state::read(|s| {
if s.neuron_pairs().len() >= REGISTRATIONS_LIMIT as usize {
Err(RegisterNeuronPairError::RegistrationLimitExceeded(
REGISTRATIONS_LIMIT,
))
} else {
Ok(s.wtn_governance_canister_id())
}
})
.map(|wtn_governance_canister| PrepareSuccess {
caller: ic_cdk::caller(),
wtn_governance_canister,
})
}

async fn call_get_neuron(
governance_canister: Principal,
neuron_id: [u8; 32],
Expand Down

0 comments on commit e32b298

Please sign in to comment.