Skip to content

Commit

Permalink
Add missing require_admin calls, update storage type used for NQG rep…
Browse files Browse the repository at this point in the history
…resentation (#5)

* Add missing require_admin calls

* Remove unused import

* Use instance storage for layers
  • Loading branch information
cptartur authored May 10, 2024
1 parent cd1b1af commit bca2656
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
14 changes: 9 additions & 5 deletions contracts/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String>) {
let admin = get_admin(&env);
admin.require_auth();
require_admin(&env);

let mut submissions = Vec::new(&env);

Expand All @@ -124,6 +122,7 @@ impl VotingSystem {
votes: Map<String, Vote>,
) -> Result<(), VotingSystemError> {
require_admin(env);

if !read_submissions(env, Self::get_current_round(env)).contains(submission_id.clone()) {
return Err(VotingSystemError::SubmissionDoesNotExist);
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions contracts/governance/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ mod key_data;
pub(crate) fn read_layer(env: &Env, layer_id: &String) -> ContractResult<Layer> {
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(
Expand All @@ -40,19 +40,19 @@ pub(crate) fn read_neuron(
) -> ContractResult<Neuron> {
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(
Expand Down

0 comments on commit bca2656

Please sign in to comment.