Skip to content

Commit

Permalink
Merge branch 'origin/grarco/masp-fee-payment' (#3393)
Browse files Browse the repository at this point in the history
* origin/grarco/masp-fee-payment:
  Removes fallback logic when failed fee payment
  Renames misleading gas limit variable
  Removes useless write-log commit in fee payment
  Fixes typo
  Fixes masp amounts conversion
  Fixes broken docs
  Reuses token transfer
  Fixes typo
  Panics in fee payment if balance read fails
  Changelog #3393
  Adds missing gas spending key arg to ibc tx
  Masp fee payment for shielded actions
  Fixes masp tx generation and integration tests
  Updates shielded wasm code to handle fee unshielding
  Removes unused denominate function
  Adds support for masp fee payment in sdk
  Refactors the write log api
  Different gas cost for storage deletes
  Removes write log precommit and leverages the batch log
  Adds integration tests for masp fee payment
  Refactors batch execution in case of masp fee payment
  Skips the execution of the first inner tx when masp fee payment
  Renames fee payment gas limit parameter
  Returns `BatchedTxResult` from masp fee payment
  `check_fees` drop the storage changes in case of failure
  `check_fees` checks masp fee payment
  Reworks masp fee payment to correctly handle errors. Misc refactors
  Passes the correct tx index to masp fee payment check
  Introduces masp fee payment
  • Loading branch information
brentstone committed Jul 4, 2024
2 parents 8975d21 + e1b5857 commit bde8a9a
Show file tree
Hide file tree
Showing 58 changed files with 3,246 additions and 1,059 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/3393-masp-fee-payment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Added support for fee payment directly from the MASP pool.
([\#3393](https://github.com/anoma/namada/pull/3393))
49 changes: 49 additions & 0 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3249,6 +3249,8 @@ pub mod args {
"gas-limit",
DefaultFn(|| GasLimit::from(DEFAULT_GAS_LIMIT)),
);
pub const GAS_SPENDING_KEY: ArgOpt<WalletSpendingKey> =
arg_opt("gas-spending-key");
pub const FEE_TOKEN: ArgDefaultFromCtx<WalletAddrOrNativeToken> =
arg_default_from_ctx("gas-token", DefaultFn(|| "".parse().unwrap()));
pub const FEE_PAYER: Arg<WalletAddress> = arg("fee-payer");
Expand Down Expand Up @@ -4443,9 +4445,16 @@ pub mod args {
});
}

let gas_spending_keys = self
.gas_spending_keys
.iter()
.map(|key| chain_ctx.get_cached(key))
.collect();

Ok(TxShieldedTransfer::<SdkTypes> {
tx,
data,
gas_spending_keys,
tx_code_path: self.tx_code_path.to_path_buf(),
})
}
Expand All @@ -4465,10 +4474,15 @@ pub mod args {
token,
amount,
}];
let mut gas_spending_keys = vec![];
if let Some(key) = GAS_SPENDING_KEY.parse(matches) {
gas_spending_keys.push(key);
}

Self {
tx,
data,
gas_spending_keys,
tx_code_path,
}
}
Expand All @@ -4491,6 +4505,10 @@ pub mod args {
.def()
.help(wrap!("The amount to transfer in decimal.")),
)
.arg(GAS_SPENDING_KEY.def().help(wrap!(
"The optional spending key that will be used in addition \
to the source for gas payment."
)))
}
}

Expand Down Expand Up @@ -4584,10 +4602,16 @@ pub mod args {
amount: transfer_data.amount,
});
}
let gas_spending_keys = self
.gas_spending_keys
.iter()
.map(|key| chain_ctx.get_cached(key))
.collect();

Ok(TxUnshieldingTransfer::<SdkTypes> {
tx,
data,
gas_spending_keys,
source: chain_ctx.get_cached(&self.source),
tx_code_path: self.tx_code_path.to_path_buf(),
})
Expand All @@ -4607,11 +4631,16 @@ pub mod args {
token,
amount,
}];
let mut gas_spending_keys = vec![];
if let Some(key) = GAS_SPENDING_KEY.parse(matches) {
gas_spending_keys.push(key);
}

Self {
tx,
source,
data,
gas_spending_keys,
tx_code_path,
}
}
Expand All @@ -4634,6 +4663,10 @@ pub mod args {
.def()
.help(wrap!("The amount to transfer in decimal.")),
)
.arg(GAS_SPENDING_KEY.def().help(wrap!(
"The optional spending key that will be used in addition \
to the source for gas payment."
)))
}
}

Expand All @@ -4646,6 +4679,11 @@ pub mod args {
) -> Result<TxIbcTransfer<SdkTypes>, Self::Error> {
let tx = self.tx.to_sdk(ctx)?;
let chain_ctx = ctx.borrow_mut_chain_or_exit();
let gas_spending_keys = self
.gas_spending_keys
.iter()
.map(|key| chain_ctx.get_cached(key))
.collect();

Ok(TxIbcTransfer::<SdkTypes> {
tx,
Expand All @@ -4659,6 +4697,7 @@ pub mod args {
timeout_sec_offset: self.timeout_sec_offset,
refund_target: chain_ctx.get_opt(&self.refund_target),
memo: self.memo,
gas_spending_keys,
tx_code_path: self.tx_code_path.to_path_buf(),
})
}
Expand All @@ -4680,6 +4719,10 @@ pub mod args {
std::fs::read_to_string(path)
.expect("Expected a file at given path")
});
let mut gas_spending_keys = vec![];
if let Some(key) = GAS_SPENDING_KEY.parse(matches) {
gas_spending_keys.push(key);
}
let tx_code_path = PathBuf::from(TX_IBC_WASM);
Self {
tx,
Expand All @@ -4693,6 +4736,7 @@ pub mod args {
timeout_sec_offset,
refund_target,
memo,
gas_spending_keys,
tx_code_path,
}
}
Expand Down Expand Up @@ -4729,6 +4773,11 @@ pub mod args {
.arg(IBC_TRANSFER_MEMO_PATH.def().help(wrap!(
"The path for the memo field of ICS20 transfer."
)))
.arg(GAS_SPENDING_KEY.def().help(wrap!(
"The optional spending key that will be used in addition \
to the source for gas payment (if this is a shielded \
action)."
)))
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/apps_lib/src/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,16 +649,16 @@ pub async fn query_protocol_parameters(
.expect("Parameter should be defined.");
display_line!(context.io(), "{:4}Max block gas: {:?}", "", max_block_gas);

let key = param_storage::get_fee_unshielding_gas_limit_key();
let fee_unshielding_gas_limit: u64 =
let key = param_storage::get_masp_fee_payment_gas_limit_key();
let masp_fee_payment_gas_limit: u64 =
query_storage_value(context.client(), &key)
.await
.expect("Parameter should be defined.");
display_line!(
context.io(),
"{:4}Fee unshielding gas limit: {:?}",
"{:4}Masp fee payment gas limit: {:?}",
"",
fee_unshielding_gas_limit
masp_fee_payment_gas_limit
);

let key = param_storage::get_gas_cost_key();
Expand Down
4 changes: 2 additions & 2 deletions crates/apps_lib/src/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ pub struct Parameters {
pub epochs_per_year: u64,
/// How many epochs it takes to transition to the next masp epoch
pub masp_epoch_multiplier: u64,
/// Fee unshielding gas limit
pub fee_unshielding_gas_limit: u64,
/// The gas limit for a masp transaction paying fees
pub masp_fee_payment_gas_limit: u64,
/// Gas scale
pub gas_scale: u64,
/// Map of the cost per gas unit for every token allowed for fee payment
Expand Down
4 changes: 2 additions & 2 deletions crates/apps_lib/src/config/genesis/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl Finalized {
implicit_vp,
epochs_per_year,
masp_epoch_multiplier,
fee_unshielding_gas_limit,
masp_fee_payment_gas_limit,
gas_scale,
max_block_gas,
minimum_gas_price,
Expand Down Expand Up @@ -344,7 +344,7 @@ impl Finalized {
epochs_per_year,
masp_epoch_multiplier,
max_proposal_bytes,
fee_unshielding_gas_limit,
masp_fee_payment_gas_limit,
gas_scale,
max_block_gas,
minimum_gas_price: minimum_gas_price
Expand Down
8 changes: 4 additions & 4 deletions crates/apps_lib/src/config/genesis/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ pub struct ChainParams<T: TemplateValidation> {
pub masp_epoch_multiplier: u64,
/// Max gas for block
pub max_block_gas: u64,
/// Fee unshielding gas limit
pub fee_unshielding_gas_limit: u64,
/// Gas limit of a masp transaction paying fees
pub masp_fee_payment_gas_limit: u64,
/// Gas scale
pub gas_scale: u64,
/// Map of the cost per gas unit for every token allowed for fee payment
Expand All @@ -319,7 +319,7 @@ impl ChainParams<Unvalidated> {
epochs_per_year,
masp_epoch_multiplier,
max_block_gas,
fee_unshielding_gas_limit,
masp_fee_payment_gas_limit,
gas_scale,
minimum_gas_price,
} = self;
Expand Down Expand Up @@ -364,7 +364,7 @@ impl ChainParams<Unvalidated> {
epochs_per_year,
masp_epoch_multiplier,
max_block_gas,
fee_unshielding_gas_limit,
masp_fee_payment_gas_limit,
gas_scale,
minimum_gas_price: min_gas_prices,
})
Expand Down
2 changes: 2 additions & 0 deletions crates/benches/process_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use namada::core::address;
use namada::core::key::RefTo;
use namada::core::storage::BlockHeight;
use namada::core::time::DateTimeUtc;
use namada::state::TxIndex;
use namada::token::{
Amount, DenominatedAmount, TransparentTransfer, TransparentTransferData,
};
Expand Down Expand Up @@ -80,6 +81,7 @@ fn process_tx(c: &mut Criterion) {
shell
.check_proposal_tx(
&wrapper,
&TxIndex::default(),
validation_meta,
temp_state,
datetime,
Expand Down
23 changes: 20 additions & 3 deletions crates/core/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,15 @@ impl<'de> serde::Deserialize<'de> for PaymentAddress {

/// Wrapper for masp_primitive's ExtendedSpendingKey
#[derive(
Clone, Debug, Copy, BorshSerialize, BorshDeserialize, BorshDeserializer,
Clone,
Debug,
Copy,
BorshSerialize,
BorshDeserialize,
BorshDeserializer,
Hash,
Eq,
PartialEq,
)]
pub struct ExtendedSpendingKey(masp_primitives::zip32::ExtendedSpendingKey);

Expand Down Expand Up @@ -435,7 +443,7 @@ impl<'de> serde::Deserialize<'de> for ExtendedSpendingKey {
}

/// Represents a source of funds for a transfer
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum TransferSource {
/// A transfer coming from a transparent address
Address(Address),
Expand Down Expand Up @@ -543,7 +551,16 @@ pub fn addr_taddr(addr: Address) -> TransparentAddress {
}

/// Represents a target for the funds of a transfer
#[derive(Debug, Clone, BorshDeserialize, BorshSerialize, BorshDeserializer)]
#[derive(
Debug,
Clone,
BorshDeserialize,
BorshSerialize,
BorshDeserializer,
Hash,
Eq,
PartialEq,
)]
pub enum TransferTarget {
/// A transfer going to a transparent address
Address(Address),
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub struct Parameters {
/// The multiplier for masp epochs (it requires this amount of epochs to
/// transition to the next masp epoch)
pub masp_epoch_multiplier: u64,
/// Fee unshielding gas limit
pub fee_unshielding_gas_limit: u64,
/// The gas limit for a masp transaction paying fees
pub masp_fee_payment_gas_limit: u64,
/// Gas scale
pub gas_scale: u64,
/// Map of the cost per gas unit for every token allowed for fee payment
Expand Down
23 changes: 7 additions & 16 deletions crates/core/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,16 @@ impl Amount {
Self { raw: Uint(raw) }
}

/// Given a u128 and [`MaspDigitPos`], construct the corresponding
/// Given a i128 and [`MaspDigitPos`], construct the corresponding
/// amount.
pub fn from_masp_denominated_u128(
val: u128,
pub fn from_masp_denominated_i128(
val: i128,
denom: MaspDigitPos,
) -> Option<Self> {
let lo = u64::try_from(val).ok()?;
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
let lo = val as u64;
#[allow(clippy::cast_sign_loss)]
let hi = (val >> 64) as u64;
let lo_pos = denom as usize;
let hi_pos = lo_pos.checked_add(1)?;
Expand Down Expand Up @@ -922,18 +925,6 @@ impl MaspDigitPos {
let amount = amount.into();
amount.raw.0[*self as usize]
}

/// Get the corresponding u64 word from the input uint256.
pub fn denominate_i128(&self, amount: &Change) -> i128 {
let val = i128::from(amount.abs().0[*self as usize]);
if Change::is_negative(amount) {
// Cannot panic as the value is limited to `u64` range
#[allow(clippy::arithmetic_side_effects)]
-val
} else {
val
}
}
}

impl From<Amount> for IbcAmount {
Expand Down
3 changes: 3 additions & 0 deletions crates/gas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub const STORAGE_ACCESS_GAS_PER_BYTE: u64 =
/// The cost of writing data to storage, per byte
pub const STORAGE_WRITE_GAS_PER_BYTE: u64 =
MEMORY_ACCESS_GAS_PER_BYTE + 69_634 + STORAGE_OCCUPATION_GAS_PER_BYTE;
/// The cost of removing data from storage, per byte
pub const STORAGE_DELETE_GAS_PER_BYTE: u64 =
MEMORY_ACCESS_GAS_PER_BYTE + 69_634 + PHYSICAL_STORAGE_LATENCY_PER_BYTE;
/// The cost of verifying a single signature of a transaction
pub const VERIFY_TX_SIG_GAS: u64 = 594_290;
/// The cost for requesting one more page in wasm (64KiB)
Expand Down
1 change: 1 addition & 0 deletions crates/ibc/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ where
let data = MsgTransfer {
message,
transfer: None,
fee_unshield: None,
}
.serialize_to_vec();

Expand Down
Loading

0 comments on commit bde8a9a

Please sign in to comment.