From bca265647c3cfaa20f60fff017f754d950d9f085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Micha=C5=82ek?= <52135326+cptartur@users.noreply.github.com> Date: Fri, 10 May 2024 11:08:21 +0200 Subject: [PATCH] Add missing require_admin calls, update storage type used for NQG representation (#5) * Add missing require_admin calls * Remove unused import * Use instance storage for layers --- contracts/governance/src/lib.rs | 14 +++++++++----- contracts/governance/src/storage.rs | 12 ++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/contracts/governance/src/lib.rs b/contracts/governance/src/lib.rs index 25108ac..f4ec650 100644 --- a/contracts/governance/src/lib.rs +++ b/contracts/governance/src/lib.rs @@ -10,7 +10,7 @@ use soroban_sdk::{ contract, contractimpl, contracttype, Address, BytesN, Env, Map, String, Vec, I256, }; -use admin::{get_admin, is_set_admin, require_admin}; +use admin::{is_set_admin, require_admin}; use crate::admin::set_admin; use crate::admin::traits::Admin; @@ -89,16 +89,14 @@ impl VotingSystem { /// Change the active round. pub fn set_current_round(env: Env, round: u32) { - let admin = get_admin(&env); - admin.require_auth(); + require_admin(&env); env.storage().instance().set(&DataKey::CurrentRound, &round); } /// Set multiple submissions. pub fn set_submissions(env: Env, new_submissions: Vec) { - let admin = get_admin(&env); - admin.require_auth(); + require_admin(&env); let mut submissions = Vec::new(&env); @@ -124,6 +122,7 @@ impl VotingSystem { votes: Map, ) -> Result<(), VotingSystemError> { require_admin(env); + if !read_submissions(env, Self::get_current_round(env)).contains(submission_id.clone()) { return Err(VotingSystemError::SubmissionDoesNotExist); } @@ -189,6 +188,7 @@ impl VotingSystem { impl Admin for VotingSystem { fn transfer_admin(env: Env, new_admin: Address) { require_admin(&env); + set_admin(&env, &new_admin); } @@ -217,6 +217,8 @@ impl Governance for VotingSystem { } fn remove_layer(env: Env, layer_id: String) -> Result<(), VotingSystemError> { + require_admin(&env); + let mut neural_governance = read_neural_governance(&env).unwrap(); let index = neural_governance .layers @@ -245,6 +247,8 @@ impl Governance for VotingSystem { raw_neurons: Vec<(String, I256)>, layer_aggregator: LayerAggregator, ) -> Result<(), VotingSystemError> { + require_admin(&env); + let layer = read_layer(&env, &layer_id)?; for neuron_id in layer.neurons { diff --git a/contracts/governance/src/storage.rs b/contracts/governance/src/storage.rs index 9d00537..db73924 100644 --- a/contracts/governance/src/storage.rs +++ b/contracts/governance/src/storage.rs @@ -18,19 +18,19 @@ mod key_data; pub(crate) fn read_layer(env: &Env, layer_id: &String) -> ContractResult { let key = get_layer_key(layer_id); env.storage() - .temporary() + .instance() .get(&key) .ok_or(VotingSystemError::LayerMissing) } pub(crate) fn write_layer(env: &Env, layer_id: &String, layer: &Layer) { let key = get_layer_key(layer_id); - env.storage().temporary().set(&key, layer); + env.storage().instance().set(&key, layer); } pub(crate) fn remove_layer(env: &Env, layer_id: &String) { let key = get_layer_key(layer_id); - env.storage().temporary().remove(&key); + env.storage().instance().remove(&key); } pub(crate) fn read_neuron( @@ -40,19 +40,19 @@ pub(crate) fn read_neuron( ) -> ContractResult { let key = get_neuron_key(layer_id, neuron_id); env.storage() - .temporary() + .instance() .get(&key) .ok_or(VotingSystemError::NeuronMissing) } pub(crate) fn write_neuron(env: &Env, layer_id: &String, neuron_id: &String, neuron: &Neuron) { let key = get_neuron_key(layer_id, neuron_id); - env.storage().temporary().set(&key, neuron); + env.storage().instance().set(&key, neuron); } pub(crate) fn remove_neuron(env: &Env, layer_id: &String, neuron_id: &String) { let key = get_neuron_key(layer_id, neuron_id); - env.storage().temporary().remove(&key); + env.storage().instance().remove(&key); } pub(crate) fn read_neuron_result(