Skip to content

Commit

Permalink
Merge branch 'tomas/gov-refactor' (#2506)
Browse files Browse the repository at this point in the history
* tomas/gov-refactor:
  changelog: add #2506
  gov: replace namada_state dep with namada_storage
  • Loading branch information
tzemanovic committed Feb 23, 2024
2 parents 829b925 + 224798f commit 225dba3
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/2506-refactor-gov.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Refactored governance crate dependencies.
([\#2506](https://github.com/anoma/namada/pull/2506))
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/governance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ testing = ["proptest"]
namada_core = { path = "../core" }
namada_macros = {path = "../macros"}
namada_parameters = {path = "../parameters"}
namada_state = {path = "../state"}
namada_storage = {path = "../storage"}
namada_trans_token = {path = "../trans_token"}

borsh.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/governance/src/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use namada_core::borsh::{BorshDeserialize, BorshSerialize};
use namada_core::token;
use namada_state::{StorageRead, StorageResult, StorageWrite};
use namada_storage::{Result, StorageRead, StorageWrite};

use super::storage::keys as goverance_storage;

Expand Down Expand Up @@ -46,7 +46,7 @@ impl Default for GovernanceParameters {

impl GovernanceParameters {
/// Initialize governance parameters into storage
pub fn init_storage<S>(&self, storage: &mut S) -> StorageResult<()>
pub fn init_storage<S>(&self, storage: &mut S) -> Result<()>
where
S: StorageRead + StorageWrite,
{
Expand Down
20 changes: 6 additions & 14 deletions crates/governance/src/pgf/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@
use namada_core::address::Address;
use namada_core::token;
use namada_parameters::storage as params_storage;
use namada_state::{
DBIter, StorageHasher, StorageRead, StorageResult, WlStorage, DB,
};
use namada_storage::{Result, StorageRead, StorageWrite};
use namada_trans_token::credit_tokens;
use namada_trans_token::storage_key::minted_balance_key;

use crate::pgf::storage::{get_parameters, get_payments, get_stewards};
use crate::storage::proposal::{PGFIbcTarget, PGFTarget};

/// Apply the PGF inflation.
pub fn apply_inflation<D, H, F>(
storage: &mut WlStorage<D, H>,
pub fn apply_inflation<S, F>(
storage: &mut S,
transfer_over_ibc: F,
) -> StorageResult<()>
) -> Result<()>
where
D: DB + for<'iter> DBIter<'iter> + Sync + 'static,
H: StorageHasher + Sync + 'static,
F: Fn(
&mut WlStorage<D, H>,
&Address,
&Address,
&PGFIbcTarget,
) -> StorageResult<()>,
S: StorageWrite + StorageRead,
F: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> Result<()>,
{
let pgf_parameters = get_parameters(storage)?;
let staking_token = storage.get_native_token()?;
Expand Down
4 changes: 2 additions & 2 deletions crates/governance/src/pgf/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::BTreeSet;
use namada_core::address::Address;
use namada_core::borsh::{BorshDeserialize, BorshSerialize};
use namada_core::dec::Dec;
use namada_state::{StorageRead, StorageResult, StorageWrite};
use namada_storage::{Result, StorageRead, StorageWrite};
use serde::{Deserialize, Serialize};

use super::storage::keys as pgf_storage;
Expand Down Expand Up @@ -44,7 +44,7 @@ impl Default for PgfParameters {

impl PgfParameters {
/// Initialize governance parameters into storage
pub fn init_storage<S>(&self, storage: &mut S) -> StorageResult<()>
pub fn init_storage<S>(&self, storage: &mut S) -> Result<()>
where
S: StorageRead + StorageWrite,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/governance/src/pgf/storage/keys.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use namada_core::address::Address;
use namada_core::storage::{DbKeySeg, Key, KeySeg};
use namada_macros::StorageKeys;
use namada_state::collections::{lazy_map, LazyCollection, LazyMap};
use namada_storage::collections::{lazy_map, LazyCollection, LazyMap};

use crate::pgf::storage::steward::StewardDetail;
use crate::pgf::ADDRESS;
Expand Down
19 changes: 8 additions & 11 deletions crates/governance/src/pgf/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use std::collections::HashMap;

use namada_core::address::Address;
use namada_core::dec::Dec;
use namada_state::{StorageRead, StorageResult, StorageWrite};
use namada_storage::{Result, StorageRead, StorageWrite};

use crate::pgf::parameters::PgfParameters;
use crate::pgf::storage::keys as pgf_keys;
use crate::pgf::storage::steward::StewardDetail;
use crate::storage::proposal::StoragePgfFunding;

/// Query the current pgf steward set
pub fn get_stewards<S>(storage: &S) -> StorageResult<Vec<StewardDetail>>
pub fn get_stewards<S>(storage: &S) -> Result<Vec<StewardDetail>>
where
S: StorageRead,
{
Expand All @@ -36,26 +36,23 @@ where
pub fn get_steward<S>(
storage: &S,
address: &Address,
) -> StorageResult<Option<StewardDetail>>
) -> Result<Option<StewardDetail>>
where
S: StorageRead,
{
pgf_keys::stewards_handle().get(storage, address)
}

/// Check if an address is a steward
pub fn is_steward<S>(storage: &S, address: &Address) -> StorageResult<bool>
pub fn is_steward<S>(storage: &S, address: &Address) -> Result<bool>
where
S: StorageRead,
{
pgf_keys::stewards_handle().contains(storage, address)
}

/// Remove a steward
pub fn remove_steward<S>(
storage: &mut S,
address: &Address,
) -> StorageResult<()>
pub fn remove_steward<S>(storage: &mut S, address: &Address) -> Result<()>
where
S: StorageRead + StorageWrite,
{
Expand All @@ -65,7 +62,7 @@ where
}

/// Query the current pgf continuous payments
pub fn get_payments<S>(storage: &S) -> StorageResult<Vec<StoragePgfFunding>>
pub fn get_payments<S>(storage: &S) -> Result<Vec<StoragePgfFunding>>
where
S: StorageRead,
{
Expand All @@ -81,7 +78,7 @@ where
}

/// Query the pgf parameters
pub fn get_parameters<S>(storage: &S) -> StorageResult<PgfParameters>
pub fn get_parameters<S>(storage: &S) -> Result<PgfParameters>
where
S: StorageRead,
{
Expand All @@ -108,7 +105,7 @@ pub fn update_commission<S>(
storage: &mut S,
address: Address,
reward_distribution: HashMap<Address, Dec>,
) -> StorageResult<()>
) -> Result<()>
where
S: StorageRead + StorageWrite,
{
Expand Down
39 changes: 14 additions & 25 deletions crates/governance/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use std::collections::BTreeMap;
use namada_core::address::Address;
use namada_core::borsh::BorshDeserialize;
use namada_core::storage::Epoch;
use namada_state::{
iter_prefix, StorageError, StorageRead, StorageResult, StorageWrite,
};
use namada_storage::{iter_prefix, Error, Result, StorageRead, StorageWrite};
use namada_trans_token as token;

use crate::parameters::GovernanceParameters;
Expand All @@ -32,7 +30,7 @@ pub fn init_proposal<S>(
data: InitProposalData,
content: Vec<u8>,
code: Option<Vec<u8>>,
) -> StorageResult<()>
) -> Result<()>
where
S: StorageRead + StorageWrite,
{
Expand All @@ -57,7 +55,7 @@ where
governance_keys::get_proposal_code_key(proposal_id);
let proposal_code = code
.clone()
.ok_or(StorageError::new_const("Missing proposal code"))?;
.ok_or(Error::new_const("Missing proposal code"))?;
storage.write_bytes(&proposal_code_key, proposal_code)?
}
_ => storage.write(&proposal_type_key, data.r#type.clone())?,
Expand All @@ -78,7 +76,7 @@ where
let proposal_code_key =
governance_keys::get_proposal_code_key(proposal_id);
let proposal_code =
code.ok_or(StorageError::new_const("Missing proposal code"))?;
code.ok_or(Error::new_const("Missing proposal code"))?;
storage.write_bytes(&proposal_code_key, proposal_code)?;
}

Expand Down Expand Up @@ -109,10 +107,7 @@ where
}

/// A proposal vote transaction.
pub fn vote_proposal<S>(
storage: &mut S,
data: VoteProposalData,
) -> StorageResult<()>
pub fn vote_proposal<S>(storage: &mut S, data: VoteProposalData) -> Result<()>
where
S: StorageRead + StorageWrite,
{
Expand All @@ -132,7 +127,7 @@ pub fn write_proposal_result<S>(
storage: &mut S,
proposal_id: u64,
proposal_result: ProposalResult,
) -> StorageResult<()>
) -> Result<()>
where
S: StorageRead + StorageWrite,
{
Expand All @@ -145,7 +140,7 @@ where
pub fn get_proposal_by_id<S>(
storage: &S,
id: u64,
) -> StorageResult<Option<StorageProposal>>
) -> Result<Option<StorageProposal>>
where
S: StorageRead,
{
Expand Down Expand Up @@ -178,10 +173,7 @@ where
}

/// Query all the votes for a proposal_id
pub fn get_proposal_votes<S>(
storage: &S,
proposal_id: u64,
) -> StorageResult<Vec<Vote>>
pub fn get_proposal_votes<S>(storage: &S, proposal_id: u64) -> Result<Vec<Vote>>
where
S: StorageRead,
{
Expand Down Expand Up @@ -216,10 +208,7 @@ where
}

/// Check if an accepted proposal is being executed
pub fn is_proposal_accepted<S>(
storage: &S,
tx_data: &[u8],
) -> StorageResult<bool>
pub fn is_proposal_accepted<S>(storage: &S, tx_data: &[u8]) -> Result<bool>
where
S: StorageRead,
{
Expand All @@ -237,7 +226,7 @@ where
pub fn get_proposal_code<S>(
storage: &S,
proposal_id: u64,
) -> StorageResult<Option<Vec<u8>>>
) -> Result<Option<Vec<u8>>>
where
S: StorageRead,
{
Expand All @@ -249,7 +238,7 @@ where
pub fn get_proposal_author<S>(
storage: &S,
proposal_id: u64,
) -> StorageResult<Option<Address>>
) -> Result<Option<Address>>
where
S: StorageRead,
{
Expand All @@ -258,7 +247,7 @@ where
}

/// Get governance parameters
pub fn get_parameters<S>(storage: &S) -> StorageResult<GovernanceParameters>
pub fn get_parameters<S>(storage: &S) -> Result<GovernanceParameters>
where
S: StorageRead,
{
Expand Down Expand Up @@ -295,7 +284,7 @@ where
}

/// Get governance "max_proposal_period" parameter
pub fn get_max_proposal_period<S>(storage: &S) -> StorageResult<u64>
pub fn get_max_proposal_period<S>(storage: &S) -> Result<u64>
where
S: StorageRead,
{
Expand All @@ -309,7 +298,7 @@ where
pub fn get_proposal_result<S>(
storage: &S,
proposal_id: u64,
) -> StorageResult<Option<ProposalResult>>
) -> Result<Option<ProposalResult>>
where
S: StorageRead,
{
Expand Down
2 changes: 1 addition & 1 deletion wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wasm_for_tests/wasm_source/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 225dba3

Please sign in to comment.