aboutsummaryrefslogtreecommitdiff
path: root/src/cryptonote_core
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2016-11-08 22:33:13 +0200
committerRiccardo Spagni <ric@spagni.net>2016-11-08 22:33:13 +0200
commit0fa6cbef3fa19ea2b187d18941b28a98b4c7a9ab (patch)
tree5d5f81d69f559f124441fc3f0b36d2e839200921 /src/cryptonote_core
parentMerge pull request #1295 (diff)
parentwallet: use the dynamic per kB fee (diff)
downloadmonero-0fa6cbef3fa19ea2b187d18941b28a98b4c7a9ab.tar.xz
Merge pull request #1276
18f66f4 wallet: use the dynamic per kB fee (moneromooo-monero) e6deb8a rpc: add a dynamic fee estimation RPC call (moneromooo-monero) 82dbba1 core: dynamic fee algorithm from ArticMine (moneromooo-monero)
Diffstat (limited to 'src/cryptonote_core')
-rw-r--r--src/cryptonote_core/blockchain.cpp82
-rw-r--r--src/cryptonote_core/blockchain.h41
-rw-r--r--src/cryptonote_core/tx_pool.cpp6
3 files changed, 124 insertions, 5 deletions
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index 9ea023a4c..ffebcd592 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -46,6 +46,7 @@
#include "misc_language.h"
#include "profile_tools.h"
#include "file_io_utils.h"
+#include "common/int-util.h"
#include "common/boost_serialization_helper.h"
#include "warnings.h"
#include "crypto/hash.h"
@@ -2709,6 +2710,87 @@ void Blockchain::check_ring_signature(const crypto::hash &tx_prefix_hash, const
}
//------------------------------------------------------------------
+uint64_t Blockchain::get_dynamic_per_kb_fee(uint64_t block_reward, size_t median_block_size)
+{
+ if (median_block_size < CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2)
+ median_block_size = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2;
+
+ uint64_t unscaled_fee_per_kb = (DYNAMIC_FEE_PER_KB_BASE_FEE * CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2 / median_block_size);
+ uint64_t hi, lo = mul128(unscaled_fee_per_kb, block_reward, &hi);
+ static_assert(DYNAMIC_FEE_PER_KB_BASE_BLOCK_REWARD % 1000000 == 0, "DYNAMIC_FEE_PER_KB_BASE_BLOCK_REWARD must be divisible by 1000000");
+ static_assert(DYNAMIC_FEE_PER_KB_BASE_BLOCK_REWARD / 1000000 <= std::numeric_limits<uint32_t>::max(), "DYNAMIC_FEE_PER_KB_BASE_BLOCK_REWARD is too large");
+ // divide in two steps, since the divisor must be 32 bits, but DYNAMIC_FEE_PER_KB_BASE_BLOCK_REWARD isn't
+ div128_32(hi, lo, DYNAMIC_FEE_PER_KB_BASE_BLOCK_REWARD / 1000000, &hi, &lo);
+ div128_32(hi, lo, 1000000, &hi, &lo);
+ assert(hi == 0);
+
+ return lo;
+}
+
+//------------------------------------------------------------------
+bool Blockchain::check_fee(size_t blob_size, uint64_t fee) const
+{
+ const uint8_t version = get_current_hard_fork_version();
+
+ uint64_t fee_per_kb;
+ if (version < HF_VERSION_DYNAMIC_FEE)
+ {
+ fee_per_kb = FEE_PER_KB;
+ }
+ else
+ {
+ uint64_t median = m_current_block_cumul_sz_limit / 2;
+ uint64_t already_generated_coins = m_db->height() ? m_db->get_block_already_generated_coins(m_db->height() - 1) : 0;
+ uint64_t base_reward;
+ if (!get_block_reward(median, 1, already_generated_coins, base_reward, version))
+ return false;
+ fee_per_kb = get_dynamic_per_kb_fee(base_reward, median);
+ }
+ LOG_PRINT_L2("Using " << print_money(fee) << "/kB fee");
+
+ uint64_t needed_fee = blob_size / 1024;
+ needed_fee += (blob_size % 1024) ? 1 : 0;
+ needed_fee *= fee_per_kb;
+
+ if (fee < needed_fee)
+ {
+ LOG_PRINT_L1("transaction fee is not enough: " << print_money(fee) << ", minimum fee: " << print_money(needed_fee));
+ return false;
+ }
+ return true;
+}
+
+//------------------------------------------------------------------
+uint64_t Blockchain::get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks) const
+{
+ const uint8_t version = get_current_hard_fork_version();
+
+ if (version < HF_VERSION_DYNAMIC_FEE)
+ return FEE_PER_KB;
+
+ if (grace_blocks >= CRYPTONOTE_REWARD_BLOCKS_WINDOW)
+ grace_blocks = CRYPTONOTE_REWARD_BLOCKS_WINDOW - 1;
+
+ std::vector<size_t> sz;
+ get_last_n_blocks_sizes(sz, CRYPTONOTE_REWARD_BLOCKS_WINDOW - grace_blocks);
+ for (size_t i = 0; i < grace_blocks; ++i)
+ sz.push_back(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2);
+
+ uint64_t median = epee::misc_utils::median(sz);
+ if(median <= CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2)
+ median = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2;
+
+ uint64_t already_generated_coins = m_db->height() ? m_db->get_block_already_generated_coins(m_db->height() - 1) : 0;
+ uint64_t base_reward;
+ if (!get_block_reward(median, 1, already_generated_coins, base_reward, version))
+ return false;
+
+ uint64_t fee = get_dynamic_per_kb_fee(base_reward, median);
+ LOG_PRINT_L2("Estimating " << grace_blocks << "-block fee at " << print_money(fee) << "/kB");
+ return fee;
+}
+
+//------------------------------------------------------------------
// This function checks to see if a tx is unlocked. unlock_time is either
// a block index or a unix time.
bool Blockchain::is_tx_spendtime_unlocked(uint64_t unlock_time) const
diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h
index 262c2952b..eb7a050b2 100644
--- a/src/cryptonote_core/blockchain.h
+++ b/src/cryptonote_core/blockchain.h
@@ -513,6 +513,47 @@ namespace cryptonote
bool check_tx_inputs(transaction& tx, uint64_t& pmax_used_block_height, crypto::hash& max_used_block_id, tx_verification_context &tvc, bool kept_by_block = false);
/**
+ * @brief get dynamic per kB fee for a given block size
+ *
+ * The dynamic fee is based on the block size in a past window, and
+ * the current block reward. It is expressed by kB.
+ *
+ * @param block_reward the current block reward
+ * @param median_block_size the median blob's size in the past window
+ *
+ * @return the per kB fee
+ */
+ static uint64_t get_dynamic_per_kb_fee(uint64_t block_reward, size_t median_block_size);
+
+ /**
+ * @brief get dynamic per kB fee estimate for the next few blocks
+ *
+ * The dynamic fee is based on the block size in a past window, and
+ * the current block reward. It is expressed by kB. This function
+ * calculates an estimate for a dynamic fee which will be valid for
+ * the next grace_blocks
+ *
+ * @param grace_blocks number of blocks we want the fee to be valid for
+ *
+ * @return the per kB fee estimate
+ */
+ uint64_t get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks) const;
+
+ /**
+ * @brief validate a transaction's fee
+ *
+ * This function validates the fee is enough for the transaction.
+ * This is based on the size of the transaction blob, and, after a
+ * height threshold, on the average size of transaction in a past window
+ *
+ * @param blob_size the transaction blob's size
+ * @param fee the fee
+ *
+ * @return true if the fee is enough, false otherwise
+ */
+ bool check_fee(size_t blob_size, uint64_t fee) const;
+
+ /**
* @brief check that a transaction's outputs conform to current standards
*
* This function checks, for example at the time of this writing, that
diff --git a/src/cryptonote_core/tx_pool.cpp b/src/cryptonote_core/tx_pool.cpp
index dba05a539..e72a592ca 100644
--- a/src/cryptonote_core/tx_pool.cpp
+++ b/src/cryptonote_core/tx_pool.cpp
@@ -133,12 +133,8 @@ namespace cryptonote
fee = tx.rct_signatures.txnFee;
}
- uint64_t needed_fee = blob_size / 1024;
- needed_fee += (blob_size % 1024) ? 1 : 0;
- needed_fee *= FEE_PER_KB;
- if (!kept_by_block && fee < needed_fee)
+ if (!kept_by_block && !m_blockchain.check_fee(blob_size, fee))
{
- LOG_PRINT_L1("transaction fee is not enough: " << print_money(fee) << ", minimum fee: " << print_money(needed_fee));
tvc.m_verifivation_failed = true;
tvc.m_fee_too_low = true;
return false;