Skip to content

Commit

Permalink
move tx_fee to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Boog900 committed Sep 8, 2024
1 parent a864f93 commit 6119972
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 72 deletions.
4 changes: 3 additions & 1 deletion helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/consensus"


[features]
# TODO: I don't think this is a good idea
# All features on by default.
default = ["std", "atomic", "asynch", "cast", "fs", "num", "map", "time", "thread", "constants"]
default = ["std", "atomic", "asynch", "cast", "fs", "num", "map", "time", "thread", "constants", "tx-utils"]
std = []
atomic = ["dep:crossbeam"]
asynch = ["dep:futures", "dep:rayon"]
Expand All @@ -21,6 +22,7 @@ num = []
map = ["cast", "dep:monero-serai"]
time = ["dep:chrono", "std"]
thread = ["std", "dep:target_os_lib"]
tx-utils = ["dep:monero-serai"]

[dependencies]
crossbeam = { workspace = true, optional = true }
Expand Down
2 changes: 2 additions & 0 deletions helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub mod thread;
#[cfg(feature = "time")]
pub mod time;

#[cfg(feature = "tx-utils")]
pub mod tx_utils;
//---------------------------------------------------------------------------------------------------- Private Usage

//----------------------------------------------------------------------------------------------------
34 changes: 34 additions & 0 deletions helper/src/tx_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! 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.checked_sub(output.amount.unwrap_or(0)).unwrap();
}
}
Transaction::V2 { proofs, .. } => {
fee = proofs.as_ref().unwrap().base.fee;
}
};

fee
}
32 changes: 0 additions & 32 deletions storage/blockchain/src/free.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! General free functions (related to the database).

use monero_serai::transaction::{Input, Transaction};
//---------------------------------------------------------------------------------------------------- Import
use cuprate_database::{ConcreteEnv, Env, EnvInner, InitError, RuntimeError, TxRw};

Expand Down Expand Up @@ -62,37 +61,6 @@ pub fn open(config: Config) -> Result<ConcreteEnv, InitError> {
Ok(env)
}

//---------------------------------------------------------------------------------------------------- Tx Fee
/// Calculates the fee of the [`Transaction`].
///
/// # Panics
/// This will panic if the inputs overflow or the transaction outputs too much.
pub(crate) 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.checked_sub(output.amount.unwrap_or(0)).unwrap();
}
}
Transaction::V2 { proofs, .. } => {
fee = proofs.as_ref().unwrap().base.fee;
}
};

fee
}

//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
Expand Down
8 changes: 5 additions & 3 deletions storage/blockchain/src/ops/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ use monero_serai::block::{Block, BlockHeader};
use cuprate_database::{
RuntimeError, StorableVec, {DatabaseRo, DatabaseRw},
};
use cuprate_helper::map::{combine_low_high_bits_to_u128, split_u128_into_low_high_bits};
use cuprate_helper::{
map::{combine_low_high_bits_to_u128, split_u128_into_low_high_bits},
tx_utils::tx_fee,
};
use cuprate_types::{
AltBlockInformation, ChainId, ExtendedBlockHeader, HardFork, VerifiedBlockInformation,
VerifiedTransactionInformation,
};

use crate::free::tx_fee;
use crate::ops::alt_block;
use crate::{
ops::{
alt_block,
blockchain::{chain_height, cumulative_generated_coins},
macros::doc_error,
output::get_rct_num_outputs,
Expand Down
2 changes: 1 addition & 1 deletion test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Boog900", "hinto-janai"]

[dependencies]
cuprate-types = { path = "../types" }
cuprate-helper = { path = "../helper", features = ["map"] }
cuprate-helper = { path = "../helper", features = ["map", "tx-utils"] }
cuprate-wire = { path = "../net/wire" }
cuprate-p2p-core = { path = "../p2p/p2p-core", features = ["borsh"] }

Expand Down
40 changes: 5 additions & 35 deletions test-utils/src/data/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
//---------------------------------------------------------------------------------------------------- Import
use std::sync::LazyLock;

use cuprate_helper::map::combine_low_high_bits_to_u128;
use cuprate_types::{VerifiedBlockInformation, VerifiedTransactionInformation};
use hex_literal::hex;
use monero_serai::transaction::Input;
use monero_serai::{block::Block, transaction::Transaction};

use cuprate_helper::{map::combine_low_high_bits_to_u128, tx_utils::tx_fee};
use cuprate_types::{VerifiedBlockInformation, VerifiedTransactionInformation};

use crate::data::constants::{
BLOCK_43BD1F, BLOCK_5ECB7E, BLOCK_F91043, TX_2180A8, TX_3BC7FF, TX_84D48D, TX_9E3F73,
TX_B6B439, TX_D7FEBD, TX_E2D393, TX_E57440,
Expand Down Expand Up @@ -110,36 +110,6 @@ fn to_tx_verification_data(tx_blob: impl AsRef<[u8]>) -> VerifiedTransactionInfo
}
}

/// Calculates the fee of the [`Transaction`].
///
/// # Panics
/// This will panic if the inputs overflow or the transaction outputs too much.
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.checked_sub(output.amount.unwrap_or(0)).unwrap();
}
}
Transaction::V2 { proofs, .. } => {
fee = proofs.as_ref().unwrap().base.fee;
}
};

fee
}

//---------------------------------------------------------------------------------------------------- Blocks
/// Generate a `static LazyLock<VerifiedBlockInformation>`.
///
Expand Down Expand Up @@ -311,12 +281,12 @@ transaction_verification_data! {
//---------------------------------------------------------------------------------------------------- TESTS
#[cfg(test)]
mod tests {
use super::*;

use pretty_assertions::assert_eq;

use crate::rpc::client::HttpRpcClient;

use super::*;

/// Assert the defined blocks are the same compared to ones received from a local RPC call.
#[ignore] // FIXME: doesn't work in CI, we need a real unrestricted node
#[tokio::test]
Expand Down

0 comments on commit 6119972

Please sign in to comment.