From e3875bff5f7fc49e04a236bc3ae73f77fbbd8ed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Tue, 2 Jul 2024 12:28:20 +0100 Subject: [PATCH] add namada_systems crate with systems' abstract interfaces --- Cargo.lock | 8 +++ Cargo.toml | 1 + crates/core/src/lib.rs | 2 - crates/core/src/parameters.rs | 40 ------------ crates/core/src/token.rs | 61 ----------------- crates/systems/Cargo.toml | 17 +++++ crates/systems/src/ethereum_bridge.rs | 1 + crates/{core => systems}/src/governance.rs | 0 crates/systems/src/ibc.rs | 1 + crates/systems/src/lib.rs | 27 ++++++++ crates/systems/src/parameters.rs | 43 ++++++++++++ crates/systems/src/pgf.rs | 1 + .../{core => systems}/src/proof_of_stake.rs | 4 +- crates/systems/src/shielded_token.rs | 1 + crates/systems/src/trans_token.rs | 65 +++++++++++++++++++ 15 files changed, 167 insertions(+), 105 deletions(-) create mode 100644 crates/systems/Cargo.toml create mode 100644 crates/systems/src/ethereum_bridge.rs rename crates/{core => systems}/src/governance.rs (100%) create mode 100644 crates/systems/src/ibc.rs create mode 100644 crates/systems/src/lib.rs create mode 100644 crates/systems/src/parameters.rs create mode 100644 crates/systems/src/pgf.rs rename crates/{core => systems}/src/proof_of_stake.rs (91%) create mode 100644 crates/systems/src/shielded_token.rs create mode 100644 crates/systems/src/trans_token.rs diff --git a/Cargo.lock b/Cargo.lock index e3fa843a1d6..384a0f687d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5344,6 +5344,14 @@ dependencies = [ "tracing", ] +[[package]] +name = "namada_systems" +version = "0.39.0" +dependencies = [ + "namada_core", + "namada_storage", +] + [[package]] name = "namada_test_utils" version = "0.39.0" diff --git a/Cargo.toml b/Cargo.toml index 95ecf72d8b1..394307cfc5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ members = [ "crates/shielded_token", "crates/state", "crates/storage", + "crates/systems", "crates/test_utils", "crates/tests", "crates/token", diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 200b6971cff..058fa07f7bd 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -21,9 +21,7 @@ pub mod arith; pub mod bytes; #[cfg(any(test, feature = "control_flow"))] pub mod control_flow; -pub mod governance; pub mod hints; -pub mod proof_of_stake; // TODO(namada#3248): only re-export v037 `tendermint-rs` pub use {masp_primitives, tendermint, tendermint_proto}; diff --git a/crates/core/src/parameters.rs b/crates/core/src/parameters.rs index 578155860f1..31e794e763d 100644 --- a/crates/core/src/parameters.rs +++ b/crates/core/src/parameters.rs @@ -12,46 +12,6 @@ use super::hash::Hash; use super::time::DurationSecs; use super::token; use crate::borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; -use crate::storage; - -/// Abstract parameters storage keys interface -pub trait Keys { - /// Key for implicit VP - fn implicit_vp_key() -> storage::Key; -} - -/// Abstract parameters storage read interface -pub trait Read { - /// Storage error - type Err; - - /// Read all parameters - fn read(storage: &S) -> Result; - - /// Read MASP epoch multiplier - fn masp_epoch_multiplier(storage: &S) -> Result; - - /// Read the the epoch duration parameter from store - fn epoch_duration_parameter( - storage: &S, - ) -> Result; - - /// Get the max signatures per transactio parameter - fn max_signatures_per_transaction( - storage: &S, - ) -> Result, Self::Err>; - - /// Helper function to retrieve the `is_native_token_transferable` protocol - /// parameter from storage - fn is_native_token_transferable(storage: &S) -> Result; -} - -/// Abstract parameters storage write interface -pub trait Write: Read { - /// Write all parameters - fn write(storage: &mut S, parameters: &Parameters) - -> Result<(), Self::Err>; -} /// Protocol parameters #[derive( diff --git a/crates/core/src/token.rs b/crates/core/src/token.rs index 8b3e8822ae6..69588b9518a 100644 --- a/crates/core/src/token.rs +++ b/crates/core/src/token.rs @@ -14,73 +14,12 @@ use namada_migrations::*; use serde::{Deserialize, Serialize}; use thiserror::Error; -use crate::address::Address; use crate::arith::{self, checked, CheckedAdd, CheckedSub}; use crate::dec::{Dec, POS_DECIMAL_PRECISION}; use crate::storage; use crate::storage::{DbKeySeg, KeySeg}; use crate::uint::{self, Uint, I256}; -/// Abstract token keys interface -pub trait Keys { - /// Key for transparent token balance - fn balance_key(token: &Address, owner: &Address) -> storage::Key; - - /// Returns the owner address if the given storage key is a balance key for - /// the given token. - fn is_balance_key<'a>( - token_addr: &Address, - key: &'a storage::Key, - ) -> Option<&'a Address>; - - /// Check if the given storage key is a balance key for an unspecified - /// token. If it is, return the token and owner address. - fn is_any_token_balance_key(key: &storage::Key) -> Option<[&Address; 2]>; - - /// Obtain a storage key for the multitoken minter. - fn minter_key(token_addr: &Address) -> storage::Key; -} - -/// Abstract token storage read interface -pub trait Read { - /// Storage error - type Err; -} - -/// Abstract token storage write interface -pub trait Write: Read { - /// Transfer `token` from `src` to `dest`. Returns an `Err` if `src` has - /// insufficient balance or if the transfer the `dest` would overflow (This - /// can only happen if the total supply doesn't fit in `token::Amount`). - fn transfer( - storage: &mut S, - token: &Address, - src: &Address, - dest: &Address, - amount: Amount, - ) -> Result<(), Self::Err>; - - /// Burn a specified amount of tokens from some address. If the burn amount - /// is larger than the total balance of the given address, then the - /// remaining balance is burned. The total supply of the token is - /// properly adjusted. - fn burn_tokens( - storage: &mut S, - token: &Address, - source: &Address, - amount: Amount, - ) -> Result<(), Self::Err>; - - /// Credit tokens to an account, to be used only by protocol. In - /// transactions, this would get rejected by the default `vp_token`. - fn credit_tokens( - storage: &mut S, - token: &Address, - dest: &Address, - amount: Amount, - ) -> Result<(), Self::Err>; -} - /// Amount in micro units. For different granularity another representation /// might be more appropriate. #[derive( diff --git a/crates/systems/Cargo.toml b/crates/systems/Cargo.toml new file mode 100644 index 00000000000..de36aa7d86d --- /dev/null +++ b/crates/systems/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "namada_systems" +description = "Namada systems abstract interfaces" +resolver = "2" +authors.workspace = true +edition.workspace = true +documentation.workspace = true +homepage.workspace = true +keywords.workspace = true +license.workspace = true +readme.workspace = true +repository.workspace = true +version.workspace = true + +[dependencies] +namada_core = { path = "../core" } +namada_storage = { path = "../storage" } diff --git a/crates/systems/src/ethereum_bridge.rs b/crates/systems/src/ethereum_bridge.rs new file mode 100644 index 00000000000..250acbde8da --- /dev/null +++ b/crates/systems/src/ethereum_bridge.rs @@ -0,0 +1 @@ +//! Ethereum bridge abstract interfaces diff --git a/crates/core/src/governance.rs b/crates/systems/src/governance.rs similarity index 100% rename from crates/core/src/governance.rs rename to crates/systems/src/governance.rs diff --git a/crates/systems/src/ibc.rs b/crates/systems/src/ibc.rs new file mode 100644 index 00000000000..c70ab740bd3 --- /dev/null +++ b/crates/systems/src/ibc.rs @@ -0,0 +1 @@ +//! IBC abstract interfaces diff --git a/crates/systems/src/lib.rs b/crates/systems/src/lib.rs new file mode 100644 index 00000000000..7874403d6a6 --- /dev/null +++ b/crates/systems/src/lib.rs @@ -0,0 +1,27 @@ +//! Abstract interfaces for interacting with Namada systems. + +#![doc(html_favicon_url = "https://dev.namada.net/master/favicon.png")] +#![doc(html_logo_url = "https://dev.namada.net/master/rustdoc-logo.png")] +#![deny(rustdoc::broken_intra_doc_links)] +#![deny(rustdoc::private_intra_doc_links)] +#![warn( + missing_docs, + rust_2018_idioms, + clippy::cast_sign_loss, + clippy::cast_possible_truncation, + clippy::cast_possible_wrap, + clippy::cast_lossless, + clippy::arithmetic_side_effects, + clippy::dbg_macro, + clippy::print_stdout, + clippy::print_stderr +)] + +pub mod ethereum_bridge; +pub mod governance; +pub mod ibc; +pub mod parameters; +pub mod pgf; +pub mod proof_of_stake; +pub mod shielded_token; +pub mod trans_token; diff --git a/crates/systems/src/parameters.rs b/crates/systems/src/parameters.rs new file mode 100644 index 00000000000..3f6d1136dc7 --- /dev/null +++ b/crates/systems/src/parameters.rs @@ -0,0 +1,43 @@ +//! Parameters abstract interfaces + +pub use namada_core::parameters::*; +use namada_core::storage; + +/// Abstract parameters storage keys interface +pub trait Keys { + /// Key for implicit VP + fn implicit_vp_key() -> storage::Key; +} + +/// Abstract parameters storage read interface +pub trait Read { + /// Storage error + type Err; + + /// Read all parameters + fn read(storage: &S) -> Result; + + /// Read MASP epoch multiplier + fn masp_epoch_multiplier(storage: &S) -> Result; + + /// Read the the epoch duration parameter from store + fn epoch_duration_parameter( + storage: &S, + ) -> Result; + + /// Get the max signatures per transactio parameter + fn max_signatures_per_transaction( + storage: &S, + ) -> Result, Self::Err>; + + /// Helper function to retrieve the `is_native_token_transferable` protocol + /// parameter from storage + fn is_native_token_transferable(storage: &S) -> Result; +} + +/// Abstract parameters storage write interface +pub trait Write: Read { + /// Write all parameters + fn write(storage: &mut S, parameters: &Parameters) + -> Result<(), Self::Err>; +} diff --git a/crates/systems/src/pgf.rs b/crates/systems/src/pgf.rs new file mode 100644 index 00000000000..13cf6f31cf3 --- /dev/null +++ b/crates/systems/src/pgf.rs @@ -0,0 +1 @@ +//! PGF abstract interfaces diff --git a/crates/core/src/proof_of_stake.rs b/crates/systems/src/proof_of_stake.rs similarity index 91% rename from crates/core/src/proof_of_stake.rs rename to crates/systems/src/proof_of_stake.rs index f2a73c4fda1..12e7af1f138 100644 --- a/crates/core/src/proof_of_stake.rs +++ b/crates/systems/src/proof_of_stake.rs @@ -1,7 +1,7 @@ //! Proof-of-Stake abstract interfaces -use crate::address::Address; -use crate::storage; +use namada_core::address::Address; +use namada_core::storage; /// Abstract PoS storage read interface pub trait Read { diff --git a/crates/systems/src/shielded_token.rs b/crates/systems/src/shielded_token.rs new file mode 100644 index 00000000000..27f7e3fa922 --- /dev/null +++ b/crates/systems/src/shielded_token.rs @@ -0,0 +1 @@ +//! Shielded token abstract interfaces diff --git a/crates/systems/src/trans_token.rs b/crates/systems/src/trans_token.rs new file mode 100644 index 00000000000..6eac8a19763 --- /dev/null +++ b/crates/systems/src/trans_token.rs @@ -0,0 +1,65 @@ +//! Transparent token abstract interfaces + +use namada_core::address::Address; +use namada_core::storage; +pub use namada_core::token::*; + +/// Abstract token keys interface +pub trait Keys { + /// Key for transparent token balance + fn balance_key(token: &Address, owner: &Address) -> storage::Key; + + /// Returns the owner address if the given storage key is a balance key for + /// the given token. + fn is_balance_key<'a>( + token_addr: &Address, + key: &'a storage::Key, + ) -> Option<&'a Address>; + + /// Check if the given storage key is a balance key for an unspecified + /// token. If it is, return the token and owner address. + fn is_any_token_balance_key(key: &storage::Key) -> Option<[&Address; 2]>; + + /// Obtain a storage key for the multitoken minter. + fn minter_key(token_addr: &Address) -> storage::Key; +} + +/// Abstract token storage read interface +pub trait Read { + /// Storage error + type Err; +} + +/// Abstract token storage write interface +pub trait Write: Read { + /// Transfer `token` from `src` to `dest`. Returns an `Err` if `src` has + /// insufficient balance or if the transfer the `dest` would overflow (This + /// can only happen if the total supply doesn't fit in `token::Amount`). + fn transfer( + storage: &mut S, + token: &Address, + src: &Address, + dest: &Address, + amount: Amount, + ) -> Result<(), Self::Err>; + + /// Burn a specified amount of tokens from some address. If the burn amount + /// is larger than the total balance of the given address, then the + /// remaining balance is burned. The total supply of the token is + /// properly adjusted. + fn burn_tokens( + storage: &mut S, + token: &Address, + source: &Address, + amount: Amount, + ) -> Result<(), Self::Err>; + + /// Credit tokens to an account, to be used only by protocol. In + /// transactions, this would get rejected by the default `vp_token`. + fn credit_tokens( + storage: &mut S, + token: &Address, + dest: &Address, + amount: Amount, + ) -> Result<(), Self::Err>; +}