Make Blockchain::get_fee_quantization_mask() compile time

This also removes potential thread safety bug in that function.
This commit is contained in:
SChernykh 2020-10-20 14:16:09 +02:00 committed by wowario
parent b974ccde3a
commit 67cc83a66c
No known key found for this signature in database
GPG Key ID: 24DCBE762DE9C111
3 changed files with 31 additions and 14 deletions

26
src/common/powerof.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <stdint.h>
namespace tools
{
template<uint64_t a, uint64_t b>
struct PowerOf
{
enum Data : uint64_t
{
// a^b = a * a^(b-1)
Value = a * PowerOf<a, b - 1>::Value,
};
};
template<uint64_t a>
struct PowerOf<a, 0>
{
enum Data : uint64_t
{
// a^0 = 1
Value = 1,
};
};
}

View File

@ -3710,19 +3710,6 @@ void Blockchain::check_ring_signature(const crypto::hash &tx_prefix_hash, const
result = crypto::check_ring_signature(tx_prefix_hash, key_image, p_output_keys, sig.data()) ? 1 : 0;
}
//------------------------------------------------------------------
uint64_t Blockchain::get_fee_quantization_mask()
{
static uint64_t mask = 0;
if (mask == 0)
{
mask = 1;
for (size_t n = PER_KB_FEE_QUANTIZATION_DECIMALS; n < CRYPTONOTE_DISPLAY_DECIMAL_POINT; ++n)
mask *= 10;
}
return mask;
}
//------------------------------------------------------------------
uint64_t Blockchain::get_dynamic_base_fee(uint64_t block_reward, size_t median_block_weight, uint8_t version)
{

View File

@ -51,6 +51,7 @@
#include "string_tools.h"
#include "rolling_median.h"
#include "cryptonote_basic/cryptonote_basic.h"
#include "common/powerof.h"
#include "common/util.h"
#include "cryptonote_protocol/cryptonote_protocol_defs.h"
#include "rpc/core_rpc_server_commands_defs.h"
@ -591,7 +592,10 @@ namespace cryptonote
*
* @return the fee quantized mask
*/
static uint64_t get_fee_quantization_mask();
static uint64_t get_fee_quantization_mask()
{
return tools::PowerOf<10, CRYPTONOTE_DISPLAY_DECIMAL_POINT - PER_KB_FEE_QUANTIZATION_DECIMALS>::Value;
}
/**
* @brief get dynamic per kB or byte fee for a given block weight