mirror of
https://github.com/Cuprate/cuprate.git
synced 2024-12-23 03:59:31 +00:00
move tx_fee to helper
This commit is contained in:
parent
a864f934be
commit
6119972fe8
7 changed files with 50 additions and 72 deletions
|
@ -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"]
|
||||
|
@ -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 }
|
||||
|
|
|
@ -31,6 +31,8 @@ pub mod thread;
|
|||
#[cfg(feature = "time")]
|
||||
pub mod time;
|
||||
|
||||
#[cfg(feature = "tx-utils")]
|
||||
pub mod tx_utils;
|
||||
//---------------------------------------------------------------------------------------------------- Private Usage
|
||||
|
||||
//----------------------------------------------------------------------------------------------------
|
||||
|
|
34
helper/src/tx_utils.rs
Normal file
34
helper/src/tx_utils.rs
Normal file
|
@ -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
|
||||
}
|
|
@ -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};
|
||||
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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"] }
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
@ -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>`.
|
||||
///
|
||||
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue