Skip to content

Commit

Permalink
remove InternalAddress::Minted
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Jun 19, 2023
1 parent ed0860a commit cbf864a
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 75 deletions.
2 changes: 1 addition & 1 deletion core/src/ledger/storage_api/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ledger::storage_api;
use crate::types::address::Address;
use crate::types::token;
pub use crate::types::token::{
balance_key, is_balance_key, is_minted_balance_key, minted_balance_key,
balance_key, is_any_minted_balance_key, is_balance_key, minted_balance_key,
minter_key, Amount, Change,
};

Expand Down
17 changes: 2 additions & 15 deletions core/src/types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ pub const FIXED_LEN_STRING_BYTES: usize = 45;

/// Internal IBC address
pub const IBC: Address = Address::Internal(InternalAddress::Ibc);
/// Internal multitoken mint address
pub const MINTED: Address = Address::Internal(InternalAddress::Minted);
/// Internal ledger parameters address
pub const PARAMETERS: Address = Address::Internal(InternalAddress::Parameters);
/// Internal PoS address
Expand Down Expand Up @@ -85,8 +83,6 @@ mod internal {
"ano::Replay Protection ";
pub const MULTITOKEN: &str =
"ano::Multitoken ";
pub const MINTED: &str =
"ano::Multitoken Mint Address ";
}

/// Fixed-length address strings prefix for established addresses.
Expand Down Expand Up @@ -233,7 +229,6 @@ impl Address {
InternalAddress::Multitoken => {
internal::MULTITOKEN.to_string()
}
InternalAddress::Minted => internal::MINTED.to_string(),
};
debug_assert_eq!(string.len(), FIXED_LEN_STRING_BYTES);
string
Expand Down Expand Up @@ -307,9 +302,6 @@ impl Address {
internal::MULTITOKEN => {
Ok(Address::Internal(InternalAddress::Multitoken))
}
internal::MINTED => {
Ok(Address::Internal(InternalAddress::Minted))
}
_ => Err(Error::new(
ErrorKind::InvalidData,
"Invalid internal address",
Expand Down Expand Up @@ -523,8 +515,6 @@ pub enum InternalAddress {
ReplayProtection,
/// Multitoken
Multitoken,
/// Minted multitoken address
Minted,
}

impl Display for InternalAddress {
Expand All @@ -543,7 +533,6 @@ impl Display for InternalAddress {
Self::EthBridge => "EthBridge".to_string(),
Self::ReplayProtection => "ReplayProtection".to_string(),
Self::Multitoken => "Multitoken".to_string(),
Self::Minted => "Minted".to_string(),
}
)
}
Expand Down Expand Up @@ -810,9 +799,8 @@ pub mod testing {
InternalAddress::IbcToken(_) => {}
InternalAddress::EthBridge => {}
InternalAddress::ReplayProtection => {}
InternalAddress::Multitoken => {}
InternalAddress::Minted => {} /* Add new addresses in the
* `prop_oneof` below. */
InternalAddress::Multitoken => {} /* Add new addresses in the
* `prop_oneof` below. */
};
prop_oneof![
Just(InternalAddress::PoS),
Expand All @@ -825,7 +813,6 @@ pub mod testing {
Just(InternalAddress::EthBridge),
Just(InternalAddress::ReplayProtection),
Just(InternalAddress::Multitoken),
Just(InternalAddress::Minted)
]
}

Expand Down
28 changes: 20 additions & 8 deletions core/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ impl TryFrom<IbcAmount> for Amount {
pub const BALANCE_STORAGE_KEY: &str = "balance";
/// Key segment for multitoken minter
pub const MINTER_STORAGE_KEY: &str = "minter";
/// Key segment for minted balance
pub const MINTED_STORAGE_KEY: &str = "minted";
/// Key segment for head shielded transaction pointer key
pub const HEAD_TX_KEY: &str = "head-tx";
/// Key segment prefix for shielded transaction key
Expand Down Expand Up @@ -372,12 +374,12 @@ pub fn minter_key(token_addr: &Address) -> Key {
/// Obtain a storage key for the minted multitoken balance.
pub fn minted_balance_key(token_addr: &Address) -> Key {
balance_prefix(token_addr)
.push(&Address::Internal(InternalAddress::Minted).to_db_key())
.push(&MINTED_STORAGE_KEY.to_owned())
.expect("Cannot obtain a storage key")
}

/// Check if the given storage key is balance key for the given token. If it is,
/// returns the owner.
/// returns the owner. For minted balances, use [`is_any_minted_balance_key()`].
pub fn is_balance_key<'a>(
token_addr: &Address,
key: &'a Key,
Expand Down Expand Up @@ -426,12 +428,22 @@ pub fn is_masp_key(key: &Key) -> bool {
|| key.starts_with(PIN_KEY_PREFIX)))
}

/// Is storage key for total supply of a specific token?
pub fn is_minted_balance_key(token_addr: &Address, key: &Key) -> bool {
if let Some(owner) = is_balance_key(token_addr, key) {
*owner == Address::Internal(InternalAddress::Minted)
} else {
false
/// Check if the given storage key is for total supply of a unspecified token.
/// If it is, returns the token.
pub fn is_any_minted_balance_key(key: &Key) -> Option<&Address> {
match &key.segments[..] {
[
DbKeySeg::AddressSeg(addr),
DbKeySeg::AddressSeg(token),
DbKeySeg::StringSeg(balance),
DbKeySeg::StringSeg(owner),
] if *addr == Address::Internal(InternalAddress::Multitoken)
&& balance == BALANCE_STORAGE_KEY
&& owner == MINTED_STORAGE_KEY =>
{
Some(token)
}
_ => None,
}
}

Expand Down
42 changes: 21 additions & 21 deletions shared/src/ledger/native_vp/multitoken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::proto::Tx;
use crate::types::address::{Address, InternalAddress};
use crate::types::storage::Key;
use crate::types::token::{
is_any_token_balance_key, is_minted_balance_key, minter_key, Amount,
is_any_minted_balance_key, is_any_token_balance_key, minter_key, Amount,
};
use crate::vm::WasmCacheAccess;

Expand Down Expand Up @@ -59,27 +59,27 @@ where
let pre: Amount = self.ctx.read_pre(key)?.unwrap_or_default();
let post: Amount = self.ctx.read_post(key)?.unwrap_or_default();
let diff = post.change() - pre.change();
match changes.get_mut(token) {
Some(change) => *change += diff,
None => _ = changes.insert(token, diff),
}
} else if let Some(token) = is_any_minted_balance_key(key) {
let pre: Amount = self.ctx.read_pre(key)?.unwrap_or_default();
let post: Amount = self.ctx.read_post(key)?.unwrap_or_default();
let diff = post.change() - pre.change();
match mints.get_mut(token) {
Some(mint) => *mint += diff,
None => _ = mints.insert(token, diff),
}

if is_minted_balance_key(token, key) {
match mints.get_mut(token) {
Some(mint) => *mint += diff,
None => _ = mints.insert(token, diff),
}

// Check if the minter VP is called
let minter_key = minter_key(token);
let minter = match self.ctx.read_post(&minter_key)? {
Some(m) => m,
None => return Ok(false),
};
if !verifiers.contains(&minter) {
return Ok(false);
}
} else {
match changes.get_mut(token) {
Some(change) => *change += diff,
None => _ = changes.insert(token, diff),
}
// Check if the minter VP is called
let minter_key = minter_key(token);
let minter = match self.ctx.read_post(&minter_key)? {
Some(m) => m,
None => return Ok(false),
};
if !verifiers.contains(&minter) {
return Ok(false);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions shared/src/ledger/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@ where
replay_protection_vp.ctx.gas_meter.into_inner();
result
}
InternalAddress::IbcToken(_)
| InternalAddress::Minted => {
// These addresses should be a part of a multitoken
// key
InternalAddress::IbcToken(_) => {
// The address should be a part of a multitoken key
gas_meter = ctx.gas_meter.into_inner();
Ok(true)
Ok(verifiers.contains(&Address::Internal(
InternalAddress::Multitoken,
)))
}
};

Expand Down
3 changes: 1 addition & 2 deletions tests/src/e2e/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use namada_apps::config::utils::convert_tm_addr_to_socket_addr;
use namada_apps::config::{Config, TendermintMode};

use super::setup::{
self, sleep, NamadaBgCmd, NamadaCmd, Test, ENV_VAR_DEBUG,
ENV_VAR_USE_PREBUILT_BINARIES,
sleep, NamadaCmd, Test, ENV_VAR_DEBUG, ENV_VAR_USE_PREBUILT_BINARIES,
};
use crate::e2e::setup::{Bin, Who, APPS_PACKAGE};
use crate::run;
Expand Down
44 changes: 21 additions & 23 deletions wasm/wasm_source/src/vp_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,33 @@ fn token_checks(

match owner {
None => {
if key.segments.get(0) == Some(&token.to_db_key()) {
if let Some(t) = token::is_any_minted_balance_key(key) {
if t == token {
// check if total supply is changed, which it should
// never be from a tx
let total_pre: token::Amount =
ctx.read_pre(key)?.unwrap();
let total_post: token::Amount =
ctx.read_post(key)?.unwrap();
if total_pre != total_post {
return reject();
}
}
} else if key.segments.get(0) == Some(&token.to_db_key()) {
// Unknown changes to this address space are disallowed, but
// unknown changes anywhere else are permitted
return reject();
}
}
Some(owner) => {
if token::is_minted_balance_key(token, key) {
// check if total supply is changed, which it should never
// be from a tx
let total_pre: token::Amount = ctx.read_pre(key)?.unwrap();
let total_post: token::Amount =
ctx.read_post(key)?.unwrap();
if total_pre != total_post {
return reject();
}
} else {
// accumulate the change
let pre: token::Amount =
ctx.read_pre(key)?.unwrap_or_default();
let post: token::Amount =
ctx.read_post(key)?.unwrap_or_default();
// make sure that the spender approved the transaction
if post < pre
&& !(verifiers.contains(owner)
|| *owner == address::masp())
{
return reject();
}
let pre: token::Amount = ctx.read_pre(key)?.unwrap_or_default();
let post: token::Amount =
ctx.read_post(key)?.unwrap_or_default();
// make sure that the spender approved the transaction
if post < pre
&& !(verifiers.contains(owner) || *owner == address::masp())
{
return reject();
}
}
}
Expand Down

0 comments on commit cbf864a

Please sign in to comment.