From 07a83ec1ec5a10c78f183159cbe833c64d89551e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 3 Jul 2024 17:38:15 +0100 Subject: [PATCH 1/2] governance: replace deps on other systems with DI --- Cargo.lock | 2 -- crates/governance/Cargo.toml | 2 -- crates/governance/src/pgf/inflation.rs | 19 +++++++++---------- crates/governance/src/storage/mod.rs | 8 +++++--- crates/governance/src/storage/proposal.rs | 13 +++++++------ crates/governance/src/vp/mod.rs | 2 +- crates/ibc/src/actions.rs | 11 +++++------ crates/node/src/shell/finalize_block.rs | 18 +++++++++++++----- crates/systems/src/test.rs | 5 +++-- crates/systems/src/trans_token.rs | 6 +++--- crates/tx_prelude/src/token.rs | 2 +- wasm/Cargo.lock | 2 -- wasm/tx_init_proposal/src/lib.rs | 7 ++++--- wasm_for_tests/Cargo.lock | 2 -- 14 files changed, 51 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a26f9aec36..e0e75606fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4762,12 +4762,10 @@ dependencies = [ "namada_gas", "namada_macros", "namada_migrations", - "namada_parameters", "namada_proof_of_stake", "namada_state", "namada_systems", "namada_token", - "namada_trans_token", "namada_tx", "namada_vm", "namada_vp", diff --git a/crates/governance/Cargo.toml b/crates/governance/Cargo.toml index 72cacdd3d0..7a3dfa4570 100644 --- a/crates/governance/Cargo.toml +++ b/crates/governance/Cargo.toml @@ -25,10 +25,8 @@ namada_core = { path = "../core" } namada_events = { path = "../events", default-features = false } namada_macros = { path = "../macros" } namada_migrations = { path= "../migrations", optional = true } -namada_parameters = { path = "../parameters" } namada_state = { path = "../state" } namada_systems = { path = "../systems" } -namada_trans_token = { path = "../trans_token" } namada_tx = { path = "../tx" } namada_vp = { path = "../vp" } diff --git a/crates/governance/src/pgf/inflation.rs b/crates/governance/src/pgf/inflation.rs index 2be31ac030..6cc814bf17 100644 --- a/crates/governance/src/pgf/inflation.rs +++ b/crates/governance/src/pgf/inflation.rs @@ -1,9 +1,8 @@ //! PGF lib code. use namada_core::address::Address; -use namada_parameters::storage as params_storage; use namada_state::{StorageRead, StorageResult, StorageWrite}; -use namada_trans_token::{credit_tokens, get_effective_total_native_supply}; +use namada_systems::{parameters, trans_token}; use crate::pgf::storage::{ get_continuous_pgf_payments, get_parameters, get_stewards, @@ -11,28 +10,28 @@ use crate::pgf::storage::{ use crate::storage::proposal::{PGFIbcTarget, PGFTarget}; /// Apply the PGF inflation. -pub fn apply_inflation( +pub fn apply_inflation( storage: &mut S, transfer_over_ibc: F, ) -> StorageResult<()> where S: StorageWrite + StorageRead, + Params: parameters::Read, + TransToken: trans_token::Read + trans_token::Write, F: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> StorageResult<()>, { let pgf_parameters = get_parameters(storage)?; let staking_token = storage.get_native_token()?; - let epochs_per_year: u64 = storage - .read(¶ms_storage::get_epochs_per_year_key())? - .expect("Epochs per year should exist in storage"); - let total_supply = get_effective_total_native_supply(storage)?; + let epochs_per_year = Params::epochs_per_year(storage)?; + let total_supply = TransToken::get_effective_total_native_supply(storage)?; let pgf_inflation_amount = total_supply .mul_floor(pgf_parameters.pgf_inflation_rate)? .checked_div_u64(epochs_per_year) .unwrap_or_default(); - credit_tokens( + TransToken::credit_tokens( storage, &staking_token, &super::ADDRESS, @@ -52,7 +51,7 @@ where for funding in pgf_fundings { let result = match &funding.detail { - PGFTarget::Internal(target) => namada_trans_token::transfer( + PGFTarget::Internal(target) => TransToken::transfer( storage, &staking_token, &super::ADDRESS, @@ -96,7 +95,7 @@ where let pgf_steward_reward = pgf_steward_inflation.mul_floor(percentage)?; - if credit_tokens( + if TransToken::credit_tokens( storage, &staking_token, &address, diff --git a/crates/governance/src/storage/mod.rs b/crates/governance/src/storage/mod.rs index 797b46d4b3..50ac876dc8 100644 --- a/crates/governance/src/storage/mod.rs +++ b/crates/governance/src/storage/mod.rs @@ -13,10 +13,11 @@ use namada_core::address::Address; use namada_core::borsh::BorshDeserialize; use namada_core::collections::HashSet; use namada_core::storage::Epoch; +use namada_core::token; use namada_state::{ iter_prefix, StorageError, StorageRead, StorageResult, StorageWrite, }; -use namada_trans_token as token; +use namada_systems::trans_token; use crate::parameters::GovernanceParameters; use crate::storage::keys as governance_keys; @@ -28,7 +29,7 @@ use crate::utils::{ProposalResult, Vote}; use crate::ADDRESS as governance_address; /// A proposal creation transaction. -pub fn init_proposal( +pub fn init_proposal( storage: &mut S, data: &InitProposalData, content: Vec, @@ -36,6 +37,7 @@ pub fn init_proposal( ) -> StorageResult where S: StorageRead + StorageWrite, + TransToken: trans_token::Write, { let counter_key = governance_keys::get_counter_key(); let proposal_id = storage.read(&counter_key)?.expect( @@ -98,7 +100,7 @@ where ); storage.write(&committing_proposals_key, ())?; - token::transfer( + TransToken::transfer( storage, &storage.get_native_token()?, &data.author, diff --git a/crates/governance/src/storage/proposal.rs b/crates/governance/src/storage/proposal.rs index 3cdf7c65e7..b7bad04bed 100644 --- a/crates/governance/src/storage/proposal.rs +++ b/crates/governance/src/storage/proposal.rs @@ -7,10 +7,10 @@ use namada_core::address::Address; use namada_core::hash::Hash; use namada_core::ibc::core::host::types::identifiers::{ChannelId, PortId}; use namada_core::storage::Epoch; +use namada_core::token; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; -use namada_trans_token::Amount; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -282,7 +282,7 @@ impl PGFTarget { } /// Returns the funding amount - pub fn amount(&self) -> Amount { + pub fn amount(&self) -> token::Amount { match self { PGFTarget::Internal(t) => t.amount, PGFTarget::Ibc(t) => t.amount, @@ -323,7 +323,7 @@ pub struct PGFInternalTarget { /// The target address pub target: Address, /// The amount of token to fund the target address - pub amount: Amount, + pub amount: token::Amount, } /// The target of a PGF payment @@ -343,7 +343,7 @@ pub struct PGFIbcTarget { /// The target address on the target chain pub target: String, /// The amount of token to fund the target address - pub amount: Amount, + pub amount: token::Amount, /// Port ID to fund pub port_id: PortId, /// Channel ID to fund @@ -368,7 +368,8 @@ impl borsh::BorshDeserialize for PGFIbcTarget { ) -> std::io::Result { use std::io::{Error, ErrorKind}; let target: String = BorshDeserialize::deserialize_reader(reader)?; - let amount: Amount = BorshDeserialize::deserialize_reader(reader)?; + let amount: token::Amount = + BorshDeserialize::deserialize_reader(reader)?; let port_id: String = BorshDeserialize::deserialize_reader(reader)?; let port_id: PortId = port_id.parse().map_err(|err| { Error::new( @@ -401,7 +402,7 @@ impl borsh::BorshSchema for PGFIbcTarget { ) { let fields = borsh::schema::Fields::NamedFields(vec![ ("target".into(), String::declaration()), - ("amount".into(), Amount::declaration()), + ("amount".into(), token::Amount::declaration()), ("port_id".into(), String::declaration()), ("channel_id".into(), String::declaration()), ]); diff --git a/crates/governance/src/vp/mod.rs b/crates/governance/src/vp/mod.rs index 11016f28c3..5510eb83fc 100644 --- a/crates/governance/src/vp/mod.rs +++ b/crates/governance/src/vp/mod.rs @@ -1236,11 +1236,11 @@ mod test { use namada_core::borsh::BorshSerializeExt; use namada_core::key::testing::keypair_1; use namada_core::key::RefTo; + use namada_core::parameters::Parameters; use namada_core::storage::testing::get_dummy_header; use namada_core::time::DateTimeUtc; use namada_core::token; use namada_gas::{TxGasMeter, VpGasMeter}; - use namada_parameters::Parameters; use namada_proof_of_stake::bond_tokens; use namada_proof_of_stake::test_utils::get_dummy_genesis_validator; use namada_state::mockdb::MockDB; diff --git a/crates/ibc/src/actions.rs b/crates/ibc/src/actions.rs index 10390b8397..3b2ffedad2 100644 --- a/crates/ibc/src/actions.rs +++ b/crates/ibc/src/actions.rs @@ -16,8 +16,8 @@ use namada_events::EmitEvents; use namada_governance::storage::proposal::PGFIbcTarget; use namada_parameters::read_epoch_duration_parameter; use namada_state::{ - DBIter, Epochs, ResultExt, State, StorageError, StorageHasher, StorageRead, - StorageResult, StorageWrite, WlState, DB, + Epochs, ResultExt, State, StorageError, StorageRead, StorageResult, + StorageWrite, }; use namada_token as token; @@ -182,15 +182,14 @@ impl IbcCommonContext for IbcProtocolContext<'_, S> where } /// Transfer tokens over IBC -pub fn transfer_over_ibc( - state: &mut WlState, +pub fn transfer_over_ibc( + state: &mut S, token: &Address, source: &Address, target: &PGFIbcTarget, ) -> StorageResult<()> where - D: DB + for<'iter> DBIter<'iter> + 'static, - H: StorageHasher + 'static, + S: State + EmitEvents, { let token = PrefixedCoin { denom: token.to_string().parse().expect("invalid token"), diff --git a/crates/node/src/shell/finalize_block.rs b/crates/node/src/shell/finalize_block.rs index 1e4c05146a..481f448f7b 100644 --- a/crates/node/src/shell/finalize_block.rs +++ b/crates/node/src/shell/finalize_block.rs @@ -298,10 +298,7 @@ where )?; // Pgf inflation - pgf_inflation::apply_inflation( - self.state.restrict_writes_to_write_log(), - ibc::transfer_over_ibc, - )?; + pgf_apply_inflation(self.state.restrict_writes_to_write_log())?; // Take events that may be emitted from PGF for event in self.state.write_log_mut().take_events() { @@ -1149,6 +1146,17 @@ where ) } +/// Dependency-injection indirection for PGF inflation +fn pgf_apply_inflation(storage: &mut S) -> StorageResult<()> +where + S: State + EmitEvents, +{ + pgf_inflation::apply_inflation::<_, parameters::Store<_>, token::Store<_>, _>( + storage, + ibc::transfer_over_ibc, + ) +} + /// We test the failure cases of [`finalize_block`]. The happy flows /// are covered by the e2e tests. #[allow(clippy::arithmetic_side_effects, clippy::cast_possible_truncation)] @@ -1830,7 +1838,7 @@ mod test_finalize_block { r#type: ProposalType::Default, }; - namada_sdk::governance::init_proposal( + namada_sdk::governance::init_proposal::<_, token::Store<_>>( &mut shell.state, &proposal, vec![], diff --git a/crates/systems/src/test.rs b/crates/systems/src/test.rs index 026850c0ac..4d3d0f71e0 100644 --- a/crates/systems/src/test.rs +++ b/crates/systems/src/test.rs @@ -7,10 +7,11 @@ lazy_static! { /// cross-dependencies (with an exception of dev-deps). static ref SYSTEMS: HashSet<&'static str> = HashSet::from_iter([ - "namada_shielded_token", + "namada_governance", "namada_parameters", - "namada_trans_token", + "namada_shielded_token", "namada_token", + "namada_trans_token", ]); } diff --git a/crates/systems/src/trans_token.rs b/crates/systems/src/trans_token.rs index 9496c0e310..7793e6046d 100644 --- a/crates/systems/src/trans_token.rs +++ b/crates/systems/src/trans_token.rs @@ -66,7 +66,7 @@ pub trait Write: Read { token: &Address, src: &Address, dest: &Address, - amount: Amount, + amount: token::Amount, ) -> Result<()>; /// Burn a specified amount of tokens from some address. If the burn amount @@ -77,7 +77,7 @@ pub trait Write: Read { storage: &mut S, token: &Address, source: &Address, - amount: Amount, + amount: token::Amount, ) -> Result<()>; /// Credit tokens to an account, to be used only by protocol. In @@ -86,6 +86,6 @@ pub trait Write: Read { storage: &mut S, token: &Address, dest: &Address, - amount: Amount, + amount: token::Amount, ) -> Result<()>; } diff --git a/crates/tx_prelude/src/token.rs b/crates/tx_prelude/src/token.rs index 5e2f630255..8c6e4f50ad 100644 --- a/crates/tx_prelude/src/token.rs +++ b/crates/tx_prelude/src/token.rs @@ -10,7 +10,7 @@ use namada_token::event::{TokenEvent, TokenOperation}; #[cfg(any(test, feature = "testing"))] pub use namada_token::testing; pub use namada_token::{ - storage_key, utils, Amount, DenominatedAmount, Transfer, + storage_key, utils, Amount, DenominatedAmount, Store, Transfer, }; use namada_tx_env::TxEnv; diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index a04c089eb0..116101ab91 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -3621,10 +3621,8 @@ dependencies = [ "namada_core", "namada_events", "namada_macros", - "namada_parameters", "namada_state", "namada_systems", - "namada_trans_token", "namada_tx", "namada_vp", "proptest", diff --git a/wasm/tx_init_proposal/src/lib.rs b/wasm/tx_init_proposal/src/lib.rs index 60f812b10b..fdf1f6799d 100644 --- a/wasm/tx_init_proposal/src/lib.rs +++ b/wasm/tx_init_proposal/src/lib.rs @@ -55,9 +55,10 @@ fn apply_tx(ctx: &mut Ctx, tx_data: BatchedTx) -> TxResult { log_string("apply_tx called to create a new governance proposal"); - let proposal_id = - governance::init_proposal(ctx, &tx_data, content, code) - .wrap_err("Failed to initialize new governance proposal")?; + let proposal_id = governance::init_proposal::<_, token::Store<_>>( + ctx, &tx_data, content, code, + ) + .wrap_err("Failed to initialize new governance proposal")?; ctx.emit_event(GovernanceEvent::new_proposal(proposal_id, tx_data.r#type)) } diff --git a/wasm_for_tests/Cargo.lock b/wasm_for_tests/Cargo.lock index 0f2d4f5e36..1e00f30b78 100644 --- a/wasm_for_tests/Cargo.lock +++ b/wasm_for_tests/Cargo.lock @@ -3621,10 +3621,8 @@ dependencies = [ "namada_core", "namada_events", "namada_macros", - "namada_parameters", "namada_state", "namada_systems", - "namada_trans_token", "namada_tx", "namada_vp", "proptest", From 1fbb9fd40db1fd780056b596bb764dbfac7f78ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Tue, 9 Jul 2024 21:27:59 +0100 Subject: [PATCH 2/2] changelog: add #3482 --- .changelog/unreleased/improvements/3482-di-gov.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/improvements/3482-di-gov.md diff --git a/.changelog/unreleased/improvements/3482-di-gov.md b/.changelog/unreleased/improvements/3482-di-gov.md new file mode 100644 index 0000000000..7494e953ee --- /dev/null +++ b/.changelog/unreleased/improvements/3482-di-gov.md @@ -0,0 +1,2 @@ +- Replaced cross-system dependencies in namada_governance crate with dependency- + injection. ([\#3482](https://github.com/anoma/namada/pull/3482)) \ No newline at end of file