Skip to content

Commit

Permalink
Now denominate the fee amount in wrapper headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Dec 4, 2023
1 parent 47c887c commit bab08c8
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 124 deletions.
5 changes: 1 addition & 4 deletions apps/src/lib/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,7 @@ impl BenchShieldedCtx {
source: TransferSource,
target: TransferTarget,
) -> Tx {
let denominated_amount = DenominatedAmount {
amount,
denom: 0.into(),
};
let denominated_amount = DenominatedAmount::native(amount);
let async_runtime = tokio::runtime::Runtime::new().unwrap();
let spending_key = self
.wallet
Expand Down
38 changes: 25 additions & 13 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,9 @@ mod test_finalize_block {
use namada::types::key::tm_consensus_key_raw_hash;
use namada::types::storage::Epoch;
use namada::types::time::{DateTimeUtc, DurationSecs};
use namada::types::token::{Amount, NATIVE_MAX_DECIMAL_PLACES};
use namada::types::token::{
Amount, DenominatedAmount, NATIVE_MAX_DECIMAL_PLACES,
};
use namada::types::transaction::governance::{
InitProposalData, VoteProposalData,
};
Expand Down Expand Up @@ -1022,7 +1024,7 @@ mod test_finalize_block {
let mut wrapper_tx =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 1.into(),
amount_per_gas_unit: DenominatedAmount::native(1.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -1063,7 +1065,7 @@ mod test_finalize_block {
let mut outer_tx =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 1.into(),
amount_per_gas_unit: DenominatedAmount::native(1.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -1178,7 +1180,9 @@ mod test_finalize_block {
let mut outer_tx =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: Default::default(),
amount_per_gas_unit: DenominatedAmount::native(
Default::default(),
),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -1232,7 +1236,7 @@ mod test_finalize_block {
// not valid tx bytes
let wrapper = Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 0.into(),
amount_per_gas_unit: DenominatedAmount::native(0.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -2955,7 +2959,7 @@ mod test_finalize_block {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 1.into(),
amount_per_gas_unit: DenominatedAmount::native(1.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand All @@ -2972,7 +2976,7 @@ mod test_finalize_block {
let mut new_wrapper = wrapper.clone();
new_wrapper.update_header(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 1.into(),
amount_per_gas_unit: DenominatedAmount::native(1.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair_2.ref_to(),
Expand Down Expand Up @@ -3084,7 +3088,9 @@ mod test_finalize_block {
let mut unsigned_wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: Amount::zero(),
amount_per_gas_unit: DenominatedAmount::native(
Amount::zero(),
),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -3260,7 +3266,7 @@ mod test_finalize_block {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 0.into(),
amount_per_gas_unit: DenominatedAmount::native(0.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -3339,7 +3345,7 @@ mod test_finalize_block {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 100.into(),
amount_per_gas_unit: DenominatedAmount::native(100.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -3422,7 +3428,7 @@ mod test_finalize_block {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 1.into(),
amount_per_gas_unit: DenominatedAmount::native(1.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
crate::wallet::defaults::albert_keypair().ref_to(),
Expand All @@ -3444,6 +3450,12 @@ mod test_finalize_block {
)));
let fee_amount =
wrapper.header().wrapper().unwrap().get_tx_fee().unwrap();
let fee_amount = fee_amount
.apply_precision(
&wrapper.header().wrapper().unwrap().fee.token,
&shell.wl_storage,
)
.unwrap();

let signer_balance = storage_api::token::read_balance(
&shell.wl_storage,
Expand Down Expand Up @@ -3481,7 +3493,7 @@ mod test_finalize_block {
.unwrap();
assert_eq!(
new_proposer_balance,
proposer_balance.checked_add(fee_amount).unwrap()
proposer_balance.checked_add(fee_amount.amount).unwrap()
);

let new_signer_balance = storage_api::token::read_balance(
Expand All @@ -3492,7 +3504,7 @@ mod test_finalize_block {
.unwrap();
assert_eq!(
new_signer_balance,
signer_balance.checked_sub(fee_amount).unwrap()
signer_balance.checked_sub(fee_amount.amount).unwrap()
)
}

Expand Down
90 changes: 66 additions & 24 deletions apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,15 +1400,35 @@ where
}
};

if wrapper.fee.amount_per_gas_unit < minimum_gas_price {
// The fees do not match the minimum required
return Err(Error::TxApply(protocol::Error::FeeError(format!(
"Fee amount {:?} do not match the minimum required amount \
{:?} for token {}",
wrapper.fee.amount_per_gas_unit,
minimum_gas_price,
wrapper.fee.token
))));
match wrapper
.fee
.amount_per_gas_unit
.apply_precision(&wrapper.fee.token, &self.wl_storage)
{
Ok(amount_per_gas_unit)
if amount_per_gas_unit.amount < minimum_gas_price =>
{
// The fees do not match the minimum required
return Err(Error::TxApply(protocol::Error::FeeError(
format!(
"Fee amount {:?} do not match the minimum required \
amount {:?} for token {}",
wrapper.fee.amount_per_gas_unit,
minimum_gas_price,
wrapper.fee.token
),
)));
}
Ok(_) => {}
Err(err) => {
return Err(Error::TxApply(protocol::Error::FeeError(
format!(
"The precision of the fee amount {:?} is higher than \
the denomination for token {}: {}",
wrapper.fee.amount_per_gas_unit, wrapper.fee.token, err,
),
)));
}
}

if let Some(transaction) = masp_transaction {
Expand Down Expand Up @@ -1580,6 +1600,7 @@ mod test_utils {
use crate::facade::tendermint_proto::v0_37::abci::{
RequestPrepareProposal, RequestProcessProposal,
};
use crate::node::ledger::shell::token::DenominatedAmount;
use crate::node::ledger::shims::abcipp_shim_types;
use crate::node::ledger::shims::abcipp_shim_types::shim::request::{
FinalizeBlock, ProcessedTx,
Expand Down Expand Up @@ -2086,7 +2107,7 @@ mod test_utils {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: Default::default(),
amount_per_gas_unit: DenominatedAmount::native(0.into()),
token: native_token,
},
keypair.ref_to(),
Expand Down Expand Up @@ -2262,6 +2283,7 @@ mod test_utils {
#[cfg(test)]
mod shell_tests {
use namada::core::ledger::replay_protection;
use namada::ledger::storage_api::token::read_denom;
use namada::proto::{
Code, Data, Section, SignableEthMessage, Signature, Signed, Tx,
};
Expand All @@ -2276,6 +2298,7 @@ mod shell_tests {

use super::*;
use crate::node::ledger::shell::test_utils;
use crate::node::ledger::shell::token::DenominatedAmount;
use crate::wallet;

const GAS_LIMIT_MULTIPLIER: u64 = 100_000;
Expand Down Expand Up @@ -2505,8 +2528,10 @@ mod shell_tests {
let mut unsigned_wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: token::Amount::from_uint(100, 0)
.expect("This can't fail"),
amount_per_gas_unit: DenominatedAmount::native(
token::Amount::from_uint(100, 0)
.expect("This can't fail"),
),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -2542,8 +2567,10 @@ mod shell_tests {
let mut invalid_wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: token::Amount::from_uint(100, 0)
.expect("This can't fail"),
amount_per_gas_unit: DenominatedAmount::native(
token::Amount::from_uint(100, 0)
.expect("This can't fail"),
),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand All @@ -2565,7 +2592,8 @@ mod shell_tests {
// we mount a malleability attack to try and remove the fee
let mut new_wrapper =
invalid_wrapper.header().wrapper().expect("Test failed");
new_wrapper.fee.amount_per_gas_unit = Default::default();
new_wrapper.fee.amount_per_gas_unit =
DenominatedAmount::native(0.into());
invalid_wrapper.update_header(TxType::Wrapper(Box::new(new_wrapper)));

let mut result = shell.mempool_validate(
Expand Down Expand Up @@ -2611,8 +2639,10 @@ mod shell_tests {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: token::Amount::from_uint(100, 0)
.expect("This can't fail"),
amount_per_gas_unit: DenominatedAmount::native(
token::Amount::from_uint(100, 0)
.expect("This can't fail"),
),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -2773,7 +2803,7 @@ mod shell_tests {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 100.into(),
amount_per_gas_unit: DenominatedAmount::native(100.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -2806,7 +2836,7 @@ mod shell_tests {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 100.into(),
amount_per_gas_unit: DenominatedAmount::native(100.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down Expand Up @@ -2835,11 +2865,17 @@ mod shell_tests {
#[test]
fn test_fee_non_whitelisted_token() {
let (shell, _recv, _, _) = test_utils::setup();
let apfel_denom = read_denom(&shell.wl_storage, &address::apfel())
.expect("unable to read denomination from storage")
.expect("unable to find denomination of apfels");

let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 100.into(),
amount_per_gas_unit: DenominatedAmount {
amount: 100.into(),
denom: apfel_denom,
},
token: address::apfel(),
},
crate::wallet::defaults::albert_keypair().ref_to(),
Expand Down Expand Up @@ -2874,7 +2910,7 @@ mod shell_tests {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 0.into(),
amount_per_gas_unit: DenominatedAmount::native(0.into()),
token: shell.wl_storage.storage.native_token.clone(),
},
crate::wallet::defaults::albert_keypair().ref_to(),
Expand Down Expand Up @@ -2908,7 +2944,9 @@ mod shell_tests {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 1_000_000_000.into(),
amount_per_gas_unit: DenominatedAmount::native(
1_000_000_000.into(),
),
token: shell.wl_storage.storage.native_token.clone(),
},
crate::wallet::defaults::albert_keypair().ref_to(),
Expand Down Expand Up @@ -2942,7 +2980,9 @@ mod shell_tests {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: token::Amount::max(),
amount_per_gas_unit: DenominatedAmount::native(
token::Amount::max(),
),
token: shell.wl_storage.storage.native_token.clone(),
},
crate::wallet::defaults::albert_keypair().ref_to(),
Expand Down Expand Up @@ -2987,7 +3027,9 @@ mod shell_tests {
let mut wrapper =
Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new(
Fee {
amount_per_gas_unit: 100.into(),
amount_per_gas_unit: DenominatedAmount::native(
100.into(),
),
token: shell.wl_storage.storage.native_token.clone(),
},
keypair.ref_to(),
Expand Down
Loading

0 comments on commit bab08c8

Please sign in to comment.