Skip to content

Commit

Permalink
Merge branch 'fraccaman/refactor-transfer-code' (#3109)
Browse files Browse the repository at this point in the history
* fraccaman/refactor-transfer-code:
  changelog: add #3109
  fix benches
  fmt, clippy
  fix mint and burn prelude functions
  ci: fix antithesis workflow
  ci: added triggerable antithesis workflow
  ci: downgrade sccache to 0.7.7
  • Loading branch information
brentstone committed Apr 24, 2024
2 parents 534fbf4 + 541c0ad commit 12cbd2e
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 115 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Use token functions from the token crate inside the tx_prelude.
([\#3109](https://github.com/anoma/namada/pull/3109))
20 changes: 15 additions & 5 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down Expand Up @@ -324,7 +326,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down Expand Up @@ -414,7 +418,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down Expand Up @@ -501,7 +507,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down Expand Up @@ -610,7 +618,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ jobs:
key: ${{ runner.os }}-${{ github.job }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Start sccache server
run: sccache --start-server
- name: ${{ matrix.make.name }}
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down Expand Up @@ -138,7 +140,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/triggerable-antithesis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Triggerable Antithesis workflow

on:
workflow_dispatch:
inputs:
namada_docker_tag:
description: 'The docker tag of the namada docker images'
required: true
type: string
scenario_tester_docker_tag:
description: 'The docker tag of the scenario tester image'
required: true
type: string
workload_type:
type: choice
description: The type of workload to use (namada__pr__customer 3h, namada__nightly__customer 8h)
options:
- namada__pr__customer
- namada__nightly__customer

jobs:
antithesis:
runs-on: [ubuntu-latest]

steps:
- uses: antithesishq/antithesis-trigger-action@v0.2
with:
notebook_name: ${{ github.event.inputs.workload_type }}
tenant: heliax
username: ${{ secrets.ANTITHESIS_USER_NAME }}
password: ${{ secrets.ANTITHESIS_PASSWORD }}
github_token: ${{ secrets.GITHUB_TOKEN }}
images: namada-config:${{ github.event.inputs.namada_docker_tag }},namada-genesis:${{ github.event.inputs.namada_docker_tag }},namada:${{ github.event.inputs.namada_docker_tag }},namada:${{ github.event.inputs.namada_docker_tag }}-inst,namada-scenario-tester:${{ github.event.inputs.scenario_tester_docker_tag }}
4 changes: 3 additions & 1 deletion .github/workflows/triggerable_pos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/triggerable_sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ jobs:
version: "25.0"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
uses: mozilla-actions/sccache-action@v0.0.4
with:
version: "v0.7.7"
- name: Setup rust toolchain
uses: oxidecomputer/actions-rs_toolchain@ad3f86084a8a5acf2c09cb691421b31cf8af7a36
with:
Expand Down
9 changes: 8 additions & 1 deletion crates/apps/src/lib/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use borsh_ext::BorshSerializeExt;
use masp_primitives::transaction::Transaction;
use masp_primitives::zip32::ExtendedFullViewingKey;
use masp_proofs::prover::LocalTxProver;
use namada::address::MASP;
use namada::core::address::{self, Address, InternalAddress};
use namada::core::chain::ChainId;
use namada::core::hash::Hash;
Expand Down Expand Up @@ -1079,7 +1080,13 @@ impl BenchShieldedCtx {
source: source.effective_address(),
target: target.effective_address(),
token: address::testing::nam(),
amount: DenominatedAmount::native(amount),
amount: if source.effective_address().eq(&MASP)
&& target.effective_address().eq(&MASP)
{
DenominatedAmount::native(0.into())
} else {
DenominatedAmount::native(amount)
},
key: None,
shielded: shielded_section_hash,
},
Expand Down
13 changes: 6 additions & 7 deletions crates/tx_prelude/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub use namada_ibc::{
IbcActions, IbcCommonContext, IbcStorageContext, NftTransferModule,
ProofSpec, TransferModule,
};
use namada_storage::StorageWrite;
use namada_token::storage_key::minter_key;
use namada_tx_env::TxEnv;

use crate::token::{burn, mint, transfer};
Expand Down Expand Up @@ -73,13 +75,10 @@ impl IbcStorageContext for Ctx {
token: &Address,
amount: Amount,
) -> Result<(), Error> {
mint(
self,
&Address::Internal(InternalAddress::Ibc),
target,
token,
amount,
)
mint(self, target, token, amount)?;

let minter_key = minter_key(token);
self.write(&minter_key, &Address::Internal(InternalAddress::Ibc))
}

fn burn_token(
Expand Down
96 changes: 21 additions & 75 deletions crates/tx_prelude/src/token.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use namada_core::address::Address;
use namada_proof_of_stake::token::storage_key::{
balance_key, minted_balance_key, minter_key,
};
use namada_proof_of_stake::token::storage_key::balance_key;
use namada_storage::{Error as StorageError, ResultExt};
pub use namada_token::*;
use namada_tx_env::TxEnv;

use crate::{Ctx, StorageRead, StorageWrite, TxResult};

#[allow(clippy::too_many_arguments)]
/// A token transfer that can be used in a transaction.
pub fn transfer(
ctx: &mut Ctx,
Expand All @@ -26,78 +23,39 @@ pub fn transfer(
ctx.insert_verifier(token)?;
}

if amount != Amount::default() && src != dest {
let src_key = balance_key(token, src);
let dest_key = balance_key(token, dest);
let src_bal: Option<Amount> = ctx.read(&src_key)?;
let mut src_bal = src_bal.ok_or_else(|| {
StorageError::new_const("the source has no balance")
})?;
src_bal.spend(&amount).into_storage_result()?;
let mut dest_bal: Amount = ctx.read(&dest_key)?.unwrap_or_default();
dest_bal.receive(&amount).into_storage_result()?;
ctx.write(&src_key, src_bal)?;
ctx.write(&dest_key, dest_bal)?;
if amount == Amount::zero() {
return Ok(());
}
Ok(())
}

/// An undenominated token transfer that can be used in a transaction.
pub fn undenominated_transfer(
ctx: &mut Ctx,
src: &Address,
dest: &Address,
token: &Address,
amount: Amount,
) -> TxResult {
// The tx must be authorized by the source address
ctx.insert_verifier(src)?;
if token.is_internal() {
// Established address tokens do not have VPs themselves, their
// validation is handled by the `Multitoken` internal address, but
// internal token addresses have to verify the transfer
ctx.insert_verifier(token)?;
}
let src_key = balance_key(token, src);
let dest_key = balance_key(token, dest);
let src_bal: Option<Amount> = ctx.read(&src_key)?;
let mut src_bal = src_bal
.ok_or_else(|| StorageError::new_const("the source has no balance"))?;

if amount != Amount::default() && src != dest {
let src_key = balance_key(token, src);
let dest_key = balance_key(token, dest);
let src_bal: Option<Amount> = ctx.read(&src_key)?;
let mut src_bal = src_bal.ok_or_else(|| {
StorageError::new_const("the source has no balance")
})?;
src_bal.spend(&amount).into_storage_result()?;
let mut dest_bal: Amount = ctx.read(&dest_key)?.unwrap_or_default();
dest_bal.receive(&amount).into_storage_result()?;
ctx.write(&src_key, src_bal)?;
ctx.write(&dest_key, dest_bal)?;
if !src_bal.can_spend(&amount) {
return Err(StorageError::new_const(
"the source has no enough balance",
));
}

src_bal.spend(&amount).into_storage_result()?;
let mut dest_bal: Amount = ctx.read(&dest_key)?.unwrap_or_default();
dest_bal.receive(&amount).into_storage_result()?;
ctx.write(&src_key, src_bal)?;
ctx.write(&dest_key, dest_bal)?;

Ok(())
}

/// Mint that can be used in a transaction.
pub fn mint(
ctx: &mut Ctx,
minter: &Address,
target: &Address,
token: &Address,
amount: Amount,
) -> TxResult {
let target_key = balance_key(token, target);
let mut target_bal: Amount = ctx.read(&target_key)?.unwrap_or_default();
target_bal.receive(&amount).into_storage_result()?;

let minted_key = minted_balance_key(token);
let mut minted_bal: Amount = ctx.read(&minted_key)?.unwrap_or_default();
minted_bal.receive(&amount).into_storage_result()?;

ctx.write(&target_key, target_bal)?;
ctx.write(&minted_key, minted_bal)?;

let minter_key = minter_key(token);
ctx.write(&minter_key, minter)?;

Ok(())
credit_tokens(ctx, token, target, amount)
}

/// Burn that can be used in a transaction.
Expand All @@ -107,17 +65,5 @@ pub fn burn(
token: &Address,
amount: Amount,
) -> TxResult {
let target_key = balance_key(token, target);
let mut target_bal: Amount = ctx.read(&target_key)?.unwrap_or_default();
target_bal.spend(&amount).into_storage_result()?;

// burn the minted amount
let minted_key = minted_balance_key(token);
let mut minted_bal: Amount = ctx.read(&minted_key)?.unwrap_or_default();
minted_bal.spend(&amount).into_storage_result()?;

ctx.write(&target_key, target_bal)?;
ctx.write(&minted_key, minted_bal)?;

Ok(())
burn_tokens(ctx, token, target, amount)
}
24 changes: 3 additions & 21 deletions wasm/tx_change_bridge_pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ fn apply_tx(ctx: &mut Ctx, signed: Tx) -> TxResult {
amount,
ref payer,
} = transfer.gas_fee;
token::undenominated_transfer(
ctx,
payer,
&BRIDGE_POOL_ADDRESS,
fee_token_addr,
amount,
)?;
token::transfer(ctx, payer, &BRIDGE_POOL_ADDRESS, fee_token_addr, amount)?;
debug_log!("Bridge pool token transfer succeeded");
let TransferToEthereum {
asset,
Expand All @@ -39,23 +33,11 @@ fn apply_tx(ctx: &mut Ctx, signed: Tx) -> TxResult {
// if minting wNam, escrow the correct amount
if asset == native_erc20_address(ctx)? {
let nam_addr = ctx.get_native_token()?;
token::undenominated_transfer(
ctx,
sender,
&address::ETH_BRIDGE,
&nam_addr,
amount,
)?;
token::transfer(ctx, sender, &address::ETH_BRIDGE, &nam_addr, amount)?;
} else {
// Otherwise we escrow ERC20 tokens.
let token = transfer.token_address();
token::undenominated_transfer(
ctx,
sender,
&BRIDGE_POOL_ADDRESS,
&token,
amount,
)?;
token::transfer(ctx, sender, &BRIDGE_POOL_ADDRESS, &token, amount)?;
}
debug_log!("Bridge pool escrow succeeded");
// add transfer into the pool
Expand Down

0 comments on commit 12cbd2e

Please sign in to comment.