From 1495f25dea9f0eaa2f7f2d45571dc27cac96c54e Mon Sep 17 00:00:00 2001 From: BHouwens Date: Tue, 19 Mar 2024 10:43:29 +0100 Subject: [PATCH] Adding minimum fee calculation & validation --- src/compute.rs | 57 ++++++++++++++++++++++++++++++------------------ src/constants.rs | 2 ++ src/utils.rs | 9 ++++++-- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/compute.rs b/src/compute.rs index e498b1b..cdb20ac 100644 --- a/src/compute.rs +++ b/src/compute.rs @@ -19,14 +19,14 @@ use crate::raft::RaftCommit; use crate::threaded_call::{ThreadedCallChannel, ThreadedCallSender}; use crate::tracked_utxo::TrackedUtxoSet; use crate::utils::{ - apply_mining_tx, check_druid_participants, create_item_asset_tx_from_sig, create_socket_addr, - format_parition_pow_address, generate_pow_random_num, to_api_keys, to_route_pow_infos, - validate_pow_block, validate_pow_for_address, ApiKeys, LocalEvent, LocalEventChannel, - LocalEventSender, ResponseResult, RoutesPoWInfo, StringError, + apply_mining_tx, calculate_fee, check_druid_participants, create_item_asset_tx_from_sig, + create_socket_addr, format_parition_pow_address, generate_pow_random_num, to_api_keys, + to_route_pow_infos, validate_pow_block, validate_pow_for_address, ApiKeys, LocalEvent, + LocalEventChannel, LocalEventSender, ResponseResult, RoutesPoWInfo, StringError, }; -use a_block_chain::primitives::asset::TokenAmount; +use a_block_chain::primitives::asset::{Asset, TokenAmount}; use a_block_chain::primitives::block::Block; -use a_block_chain::primitives::transaction::{DrsTxHashSpec, Transaction}; +use a_block_chain::primitives::transaction::{DrsTxHashSpec, Transaction, TxOut}; use a_block_chain::utils::druid_utils::druid_expectations_are_met; use a_block_chain::utils::script_utils::{tx_has_valid_create_script, tx_is_valid}; use a_block_chain::utils::transaction_utils::construct_tx_hash; @@ -565,7 +565,7 @@ impl ComputeNode { .node_raft .get_committed_current_block_num() .unwrap_or_default(); - //let sanction_list = &self.sanction_list; + let b_num = self .node_raft .get_committed_current_block_num() @@ -573,13 +573,15 @@ impl ComputeNode { move |tx| { if tx.is_create_tx() { - return tx_has_valid_create_script( - &tx.inputs[0].script_signature, - &tx.outputs[0].value, - ); + return self.tx_has_minimum_fee(&tx.fees) + && tx_has_valid_create_script( + &tx.inputs[0].script_signature, + &tx.outputs[0].value, + ); } !tx.is_coinbase() + && self.tx_has_minimum_fee(&tx.fees) && tx_is_valid(tx, b_num, |v| { utxo_set .get(v) @@ -1161,32 +1163,28 @@ impl ComputeNode { SendSharedConfig { shared_config } => { match peer != self.local_address() && !self.node_raft.get_peers().contains(&peer) { true => None, - false => self.handle_shared_config(peer, shared_config).await + false => self.handle_shared_config(peer, shared_config).await, } } Closing => self.receive_closing(peer), CoordinatedPause { b_num } => { match peer != self.local_address() && !self.node_raft.get_peers().contains(&peer) { true => None, - false => self.handle_coordinated_pause(peer, b_num).await + false => self.handle_coordinated_pause(peer, b_num).await, } } CoordinatedResume => { match peer != self.local_address() && !self.node_raft.get_peers().contains(&peer) { true => None, - false => self.handle_coordinated_resume(peer).await + false => self.handle_coordinated_resume(peer).await, } } - RequestRemoveMiner => { - self.handle_request_remove_miner(peer).await - } - RequestRuntimeData => { - self.handle_receive_request_runtime_data(peer).await - } + RequestRemoveMiner => self.handle_request_remove_miner(peer).await, + RequestRuntimeData => self.handle_receive_request_runtime_data(peer).await, SendRuntimeData { runtime_data } => { match peer != self.local_address() && !self.node_raft.get_peers().contains(&peer) { true => None, - false => self.handle_receive_runtime_data(peer, runtime_data).await + false => self.handle_receive_runtime_data(peer, runtime_data).await, } } SendRaftCmd(msg) => { @@ -1503,6 +1501,23 @@ impl ComputeNode { } } + /// Checks whether a transaction has paid the minimum fee in order to have the + /// transaction processed + /// + /// ### Arguments + /// + /// * `fees` - Vector of TxOuts representing the fees paid + fn tx_has_minimum_fee(&self, fees: &Vec) -> bool { + let fees_paid = fees + .iter() + .fold(TokenAmount(0), |acc, fee| match fee.value { + Asset::Token(v) => acc + v, + _ => acc, + }); + + fees_paid >= calculate_fee() + } + /// Check if a miner is whitelisted /// /// ### Arguments diff --git a/src/constants.rs b/src/constants.rs index f250969..3a124f7 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,5 +1,7 @@ pub use a_block_chain::constants::*; +pub const FEE: u64 = 72072; + /*------- BLOCK CONSTANTS --------*/ /// Bit shifting value for reward issuance diff --git a/src/utils.rs b/src/utils.rs index 9aef688..2a0956a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,8 @@ use crate::comms_handler::Node; use crate::configurations::{UnicornFixedInfo, UtxoSetSpec, WalletTxSpec}; use crate::constants::{ - BLOCK_PREPEND, COINBASE_MATURITY, D_DISPLAY_PLACES_U64, MINING_DIFFICULTY, NETWORK_VERSION, - REWARD_ISSUANCE_VAL, REWARD_SMOOTHING_VAL, + BLOCK_PREPEND, COINBASE_MATURITY, D_DISPLAY_PLACES_U64, FEE, MINING_DIFFICULTY, + NETWORK_VERSION, REWARD_ISSUANCE_VAL, REWARD_SMOOTHING_VAL, }; use crate::interfaces::{ BlockchainItem, BlockchainItemMeta, DruidDroplet, PowInfo, ProofOfWork, StoredSerializingBlock, @@ -434,6 +434,11 @@ pub fn calculate_reward(current_circulation: TokenAmount) -> TokenAmount { ) } +/// Calculates the minimum fee required for a transaction to be processed by the network +pub fn calculate_fee() -> TokenAmount { + TokenAmount(FEE) +} + /// Gets the total amount of tokens for all present coinbase transactions, /// assuming that they have all received the same amount of reward ///