Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding minimum fee calculation & validation #36

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -565,21 +565,23 @@ 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()
.unwrap_or_default();

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)
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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<TxOut>) -> 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
Expand Down
2 changes: 2 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub use a_block_chain::constants::*;

pub const FEE: u64 = 72072;

/*------- BLOCK CONSTANTS --------*/

/// Bit shifting value for reward issuance
Expand Down
9 changes: 7 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
///
Expand Down
Loading