Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DI - shielded token #3466

Merged
merged 8 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/3466-di-shielded-token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Replaced cross-system dependencies in namada_shielded_token crate with
dependency-injection. ([\#3466](https://github.com/anoma/namada/pull/3466))
3 changes: 3 additions & 0 deletions Cargo.lock

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

10 changes: 6 additions & 4 deletions crates/node/src/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use namada_sdk::state::StorageRead;
use namada_sdk::storage::testing::get_dummy_header;
use namada_sdk::storage::{BlockHeight, Epoch, Key, KeySeg, TxIndex};
use namada_sdk::time::DateTimeUtc;
use namada_sdk::token::{Amount, DenominatedAmount, Transfer};
use namada_sdk::token::{self, Amount, DenominatedAmount, Transfer};
use namada_sdk::tx::data::pos::Bond;
use namada_sdk::tx::data::{BatchedTxResult, Fee, TxResult, VpsResult};
use namada_sdk::tx::event::{new_tx_event, Batch};
Expand Down Expand Up @@ -443,9 +443,11 @@ impl BenchShell {
.is_masp_new_epoch(true, masp_epoch_multiplier)
.unwrap()
{
namada_sdk::token::conversion::update_allowed_conversions(
&mut self.state,
)
token::conversion::update_allowed_conversions::<
_,
parameters::Store<_>,
token::Store<_>,
>(&mut self.state)
.unwrap();
}
}
Expand Down
22 changes: 20 additions & 2 deletions crates/node/src/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use namada_sdk::proof_of_stake::storage::{
find_validator_by_raw_hash, write_last_block_proposer_address,
};
use namada_sdk::state::write_log::StorageModification;
use namada_sdk::state::{ResultExt, StorageWrite, EPOCH_SWITCH_BLOCKS_DELAY};
use namada_sdk::state::{
ResultExt, StorageResult, StorageWrite, EPOCH_SWITCH_BLOCKS_DELAY,
};
use namada_sdk::storage::{BlockResults, Epoch, Header};
use namada_sdk::tx::data::protocol::ProtocolTxType;
use namada_sdk::tx::data::VpStatusFlags;
Expand Down Expand Up @@ -102,7 +104,7 @@ where
new_epoch,
)?;
// - Token
token::finalize_block(&mut self.state, emit_events, is_masp_new_epoch)?;
token_finalize_block(&mut self.state, emit_events, is_masp_new_epoch)?;
// - PoS
// - Must be applied after governance in case it changes PoS params
proof_of_stake::finalize_block(
Expand Down Expand Up @@ -1131,6 +1133,22 @@ fn pos_votes_from_abci(
.collect()
}

/// Dependency-injection indirection for token system
fn token_finalize_block<S>(
storage: &mut S,
events: &mut Vec<Event>,
is_new_masp_epoch: bool,
) -> StorageResult<()>
where
S: StorageWrite + StorageRead + token::WithConversionState,
{
token::finalize_block::<S, parameters::Store<_>>(
storage,
events,
is_new_masp_epoch,
)
}

/// 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)]
Expand Down
8 changes: 5 additions & 3 deletions crates/node/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod tests {
use namada_sdk::storage::{BlockHeight, Key, KeySeg};
use namada_sdk::token::conversion::update_allowed_conversions;
use namada_sdk::{
address, decode, encode, parameters, storage, validation,
address, decode, encode, parameters, storage, token, validation,
};
use proptest::collection::vec;
use proptest::prelude::*;
Expand Down Expand Up @@ -171,8 +171,10 @@ mod tests {
.new_epoch(BlockHeight(100));

// update conversion for a new epoch
update_allowed_conversions(&mut state)
.expect("update conversions failed");
update_allowed_conversions::<_, parameters::Store<_>, token::Store<_>>(
&mut state,
)
.expect("update conversions failed");
state.commit_block().expect("commit failed");

// save the last state and the storage
Expand Down
16 changes: 16 additions & 0 deletions crates/parameters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ where
fn is_native_token_transferable(storage: &S) -> Result<bool> {
storage::is_native_token_transferable(storage)
}

fn epochs_per_year(storage: &S) -> Result<u64> {
read_epochs_per_year(storage)
}
}

impl<S> Write<S> for Store<S>
Expand Down Expand Up @@ -326,6 +330,18 @@ where
Ok(gas_cost_table.get(token).map(|amount| amount.to_owned()))
}

/// Read the number of epochs per year parameter
pub fn read_epochs_per_year<S>(storage: &S) -> namada_storage::Result<u64>
where
S: StorageRead,
{
let key = storage::get_epochs_per_year_key();
let epochs_per_year = storage.read(&key)?;
epochs_per_year
.ok_or(ReadError::ParametersMissing)
.into_storage_result()
}

/// Read all the parameters from storage. Returns the parameters and gas
/// cost.
pub fn read<S>(storage: &S) -> namada_storage::Result<Parameters>
Expand Down
6 changes: 5 additions & 1 deletion crates/sdk/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub type MaspVp<'a, S, CA> = token::vp::MaspVp<
ParamsPreStore<'a, S, CA>,
GovPreStore<'a, S, CA>,
IbcPostStore<'a, S, CA>,
TokenKeys,
TokenPreStore<'a, S, CA>,
>;

/// Native ETH bridge VP
Expand Down Expand Up @@ -117,6 +117,10 @@ pub type PosPreStore<'a, S, CA> = proof_of_stake::Store<
CtxPreStorageRead<'a, 'a, S, VpCache<CA>, Eval<S, CA>>,
>;

/// Token store implementation over the native prior context
pub type TokenPreStore<'a, S, CA> =
token::Store<CtxPreStorageRead<'a, 'a, S, VpCache<CA>, Eval<S, CA>>>;

/// Ibc store implementation over the native posterior context
pub type IbcPostStore<'a, S, CA> =
ibc::Store<CtxPostStorageRead<'a, 'a, S, VpCache<CA>, Eval<S, CA>>>;
Expand Down
3 changes: 1 addition & 2 deletions crates/shielded_token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ namada_account = { path = "../account" }
namada_controller = { path = "../controller" }
namada_core = { path = "../core" }
namada_gas = { path = "../gas" }
namada_parameters = { path = "../parameters" }
namada_state = { path = "../state" }
namada_storage = { path = "../storage" }
namada_systems = { path = "../systems" }
namada_trans_token = { path = "../trans_token" }
namada_tx = { path = "../tx" }
namada_vp = { path = "../vp" }

Expand All @@ -53,6 +51,7 @@ namada_core = { path = "../core", features = ["testing"] }
namada_gas = { path = "../gas" }
namada_parameters = { path = "../parameters", features = ["testing"] }
namada_storage = { path = "../storage", features = ["testing"] }
namada_trans_token = { path = "../trans_token" }

lazy_static.workspace = true
masp_proofs = { workspace = true, features = ["download-params"] }
Expand Down
Loading