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 29, 2024
1 parent 095f13f commit ea854f4
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 137 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
38 changes: 1 addition & 37 deletions crates/core/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,18 @@
//! IBC-related data types

use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Display;
use std::str::FromStr;

use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use data_encoding::{DecodePartial, HEXLOWER, HEXLOWER_PERMISSIVE};
pub use ibc::*;
use masp_primitives::transaction::components::ValueSum;
use masp_primitives::transaction::TransparentAddress;
use namada_macros::BorshDeserializer;
#[cfg(feature = "migrations")]
use namada_migrations::*;
use serde::{Deserialize, Serialize};

use super::address::HASH_LEN;
use crate::address::Address;
use crate::hash::Hash;
use crate::masp::TAddrData;
use crate::{storage, token};

/// Abstract IBC storage read interface
pub trait Read<S> {
/// Storage error
type Err;

/// Extract MASP transaction from IBC envelope
fn try_extract_masp_tx_from_envelope(
tx_data: &[u8],
) -> Result<Option<masp_primitives::transaction::Transaction>, Self::Err>;

/// Apply relevant IBC packets to the changed balances structure
fn apply_ibc_packet(
storage: &S,
tx_data: &[u8],
acc: ChangedBalances,
keys_changed: &BTreeSet<storage::Key>,
) -> Result<ChangedBalances, Self::Err>;
}

/// Balances changed by a transaction
#[derive(Default, Debug, Clone)]
pub struct ChangedBalances {
/// Map between MASP transparent address and namada types
pub decoder: BTreeMap<TransparentAddress, TAddrData>,
/// Balances before the tx
pub pre: BTreeMap<TransparentAddress, ValueSum<Address, token::Amount>>,
/// Balances after the tx
pub post: BTreeMap<TransparentAddress, ValueSum<Address, token::Amount>>,
}

/// IBC token hash derived from a denomination.
#[derive(
Expand All @@ -69,7 +33,7 @@ pub struct ChangedBalances {
#[repr(transparent)]
pub struct IbcTokenHash(pub [u8; HASH_LEN]);

impl std::fmt::Display for IbcTokenHash {
impl Display for IbcTokenHash {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", HEXLOWER.encode(&self.0))
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
35 changes: 0 additions & 35 deletions crates/core/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +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>;

/// 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.
39 changes: 39 additions & 0 deletions crates/systems/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! IBC abstract interfaces

use std::collections::{BTreeMap, BTreeSet};

use masp_primitives::transaction::components::ValueSum;
use masp_primitives::transaction::TransparentAddress;
use namada_core::address::Address;
use namada_core::masp::TAddrData;
use namada_core::{masp_primitives, storage, token};

/// Abstract IBC storage read interface
pub trait Read<S> {
/// Storage error
type Err;

/// Extract MASP transaction from IBC envelope
fn try_extract_masp_tx_from_envelope(
tx_data: &[u8],
) -> Result<Option<masp_primitives::transaction::Transaction>, Self::Err>;

/// Apply relevant IBC packets to the changed balances structure
fn apply_ibc_packet(
storage: &S,
tx_data: &[u8],
acc: ChangedBalances,
keys_changed: &BTreeSet<storage::Key>,
) -> Result<ChangedBalances, Self::Err>;
}

/// Balances changed by a transaction
#[derive(Default, Debug, Clone)]
pub struct ChangedBalances {
/// Map between MASP transparent address and namada types
pub decoder: BTreeMap<TransparentAddress, TAddrData>,
/// Balances before the tx
pub pre: BTreeMap<TransparentAddress, ValueSum<Address, token::Amount>>,
/// Balances after the tx
pub post: BTreeMap<TransparentAddress, ValueSum<Address, token::Amount>>,
}
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;
38 changes: 38 additions & 0 deletions crates/systems/src/parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! 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>;

/// 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
Loading

0 comments on commit ea854f4

Please sign in to comment.