Skip to content

Commit

Permalink
add namada_systems crate with systems' abstract interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Jul 2, 2024
1 parent daaae7b commit e3875bf
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 105 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ members = [
"crates/shielded_token",
"crates/state",
"crates/storage",
"crates/systems",
"crates/test_utils",
"crates/tests",
"crates/token",
Expand Down
2 changes: 0 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
40 changes: 0 additions & 40 deletions crates/core/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S> {
/// Storage error
type Err;

/// Read all parameters
fn read(storage: &S) -> Result<Parameters, Self::Err>;

/// Read MASP epoch multiplier
fn masp_epoch_multiplier(storage: &S) -> Result<u64, Self::Err>;

/// Read the the epoch duration parameter from store
fn epoch_duration_parameter(
storage: &S,
) -> Result<EpochDuration, Self::Err>;

/// Get the max signatures per transactio parameter
fn max_signatures_per_transaction(
storage: &S,
) -> Result<Option<u8>, Self::Err>;

/// Helper function to retrieve the `is_native_token_transferable` protocol
/// parameter from storage
fn is_native_token_transferable(storage: &S) -> Result<bool, Self::Err>;
}

/// Abstract parameters storage write interface
pub trait Write<S>: Read<S> {
/// Write all parameters
fn write(storage: &mut S, parameters: &Parameters)
-> Result<(), Self::Err>;
}

/// Protocol parameters
#[derive(
Expand Down
61 changes: 0 additions & 61 deletions crates/core/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S> {
/// Storage error
type Err;
}

/// Abstract token storage write interface
pub trait Write<S>: Read<S> {
/// 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(
Expand Down
17 changes: 17 additions & 0 deletions crates/systems/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
1 change: 1 addition & 0 deletions crates/systems/src/ethereum_bridge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! Ethereum bridge abstract interfaces
File renamed without changes.
1 change: 1 addition & 0 deletions crates/systems/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! IBC abstract interfaces
27 changes: 27 additions & 0 deletions crates/systems/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
43 changes: 43 additions & 0 deletions crates/systems/src/parameters.rs
Original file line number Diff line number Diff line change
@@ -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<S> {
/// Storage error
type Err;

/// Read all parameters
fn read(storage: &S) -> Result<Parameters, Self::Err>;

/// Read MASP epoch multiplier
fn masp_epoch_multiplier(storage: &S) -> Result<u64, Self::Err>;

/// Read the the epoch duration parameter from store
fn epoch_duration_parameter(
storage: &S,
) -> Result<EpochDuration, Self::Err>;

/// Get the max signatures per transactio parameter
fn max_signatures_per_transaction(
storage: &S,
) -> Result<Option<u8>, Self::Err>;

/// Helper function to retrieve the `is_native_token_transferable` protocol
/// parameter from storage
fn is_native_token_transferable(storage: &S) -> Result<bool, Self::Err>;
}

/// Abstract parameters storage write interface
pub trait Write<S>: Read<S> {
/// Write all parameters
fn write(storage: &mut S, parameters: &Parameters)
-> Result<(), Self::Err>;
}
1 change: 1 addition & 0 deletions crates/systems/src/pgf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! PGF abstract interfaces
Original file line number Diff line number Diff line change
@@ -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<S> {
Expand Down
1 change: 1 addition & 0 deletions crates/systems/src/shielded_token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! Shielded token abstract interfaces
65 changes: 65 additions & 0 deletions crates/systems/src/trans_token.rs
Original file line number Diff line number Diff line change
@@ -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<S> {
/// Storage error
type Err;
}

/// Abstract token storage write interface
pub trait Write<S>: Read<S> {
/// 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>;
}

0 comments on commit e3875bf

Please sign in to comment.