mirror of
https://github.com/Cuprate/cuprate.git
synced 2025-03-12 09:29:11 +00:00
Some checks are pending
Audit / audit (push) Waiting to run
CI / fmt (push) Waiting to run
CI / typo (push) Waiting to run
CI / ci (macos-latest, stable, bash) (push) Waiting to run
CI / ci (ubuntu-latest, stable, bash) (push) Waiting to run
CI / ci (windows-latest, stable-x86_64-pc-windows-gnu, msys2 {0}) (push) Waiting to run
Deny / audit (push) Waiting to run
Doc / build (push) Waiting to run
Doc / deploy (push) Blocked by required conditions
* add new tables & types * add function to fully add an alt block * resolve current todo!s * add new requests * WIP: starting re-orgs * add last service request * commit Cargo.lock * add test * more docs + cleanup + alt blocks request * clippy + fmt * document types * move tx_fee to helper * more doc updates * fmt * fix imports * fix fee * Apply suggestions from code review Co-authored-by: hinto-janai <hinto.janai@protonmail.com> * remove default features from `cuprate-helper` * review fixes * fix find_block * add a test and fix some issues in chain history * fix clippy * fmt * Apply suggestions from code review Co-authored-by: hinto-janai <hinto.janai@protonmail.com> * add dev dep * cargo update * move `flush_alt_blocks` * review fixes * more review fixes * fix clippy * remove INVARIANT comments --------- Co-authored-by: hinto-janai <hinto.janai@protonmail.com>
70 lines
1.9 KiB
Rust
70 lines
1.9 KiB
Rust
//! Utils for working with [`Transaction`]
|
|
|
|
use monero_serai::transaction::{Input, Transaction};
|
|
|
|
/// Calculates the fee of the [`Transaction`].
|
|
///
|
|
/// # Panics
|
|
/// This will panic if the inputs overflow or the transaction outputs too much, so should only
|
|
/// be used on known to be valid txs.
|
|
pub fn tx_fee(tx: &Transaction) -> u64 {
|
|
let mut fee = 0_u64;
|
|
|
|
match &tx {
|
|
Transaction::V1 { prefix, .. } => {
|
|
for input in &prefix.inputs {
|
|
match input {
|
|
Input::Gen(_) => return 0,
|
|
Input::ToKey { amount, .. } => {
|
|
fee = fee.checked_add(amount.unwrap_or(0)).unwrap();
|
|
}
|
|
}
|
|
}
|
|
|
|
for output in &prefix.outputs {
|
|
fee = fee.checked_sub(output.amount.unwrap_or(0)).unwrap();
|
|
}
|
|
}
|
|
Transaction::V2 { proofs, .. } => {
|
|
fee = proofs.as_ref().unwrap().base.fee;
|
|
}
|
|
};
|
|
|
|
fee
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use curve25519_dalek::{edwards::CompressedEdwardsY, EdwardsPoint};
|
|
use monero_serai::transaction::{NotPruned, Output, Timelock, TransactionPrefix};
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
|
|
fn tx_fee_panic() {
|
|
let input = Input::ToKey {
|
|
amount: Some(u64::MAX),
|
|
key_offsets: vec![],
|
|
key_image: EdwardsPoint::default(),
|
|
};
|
|
|
|
let output = Output {
|
|
amount: Some(u64::MAX),
|
|
key: CompressedEdwardsY::default(),
|
|
view_tag: None,
|
|
};
|
|
|
|
let tx = Transaction::<NotPruned>::V1 {
|
|
prefix: TransactionPrefix {
|
|
additional_timelock: Timelock::None,
|
|
inputs: vec![input; 2],
|
|
outputs: vec![output],
|
|
extra: vec![],
|
|
},
|
|
signatures: vec![],
|
|
};
|
|
|
|
tx_fee(&tx);
|
|
}
|
|
}
|