mirror of
https://github.com/monero-project/monero.git
synced 2024-11-17 16:27:39 +00:00
blockchain: always select random outs using triangular distribution
It was only used by the older blockchain_storage. We also move the code to the calling blockchain level, to avoid replicating the code in every DB implementation. This also makes the get_random_out method obsolete, and we delete it.
This commit is contained in:
parent
9c3715e46a
commit
275894cdef
6 changed files with 9 additions and 35 deletions
|
@ -1264,19 +1264,6 @@ uint64_t BlockchainBDB::get_tx_block_height(const crypto::hash& h) const
|
||||||
return (uint64_t)result - 1;
|
return (uint64_t)result - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//FIXME: make sure the random method used here is appropriate
|
|
||||||
uint64_t BlockchainBDB::get_random_output(const uint64_t& amount) const
|
|
||||||
{
|
|
||||||
LOG_PRINT_L3("BlockchainBDB::" << __func__);
|
|
||||||
check_open();
|
|
||||||
|
|
||||||
uint64_t num_outputs = get_num_outputs(amount);
|
|
||||||
if (num_outputs == 0)
|
|
||||||
throw1(OUTPUT_DNE("Attempting to get a random output for an amount, but none exist"));
|
|
||||||
|
|
||||||
return crypto::rand<uint64_t>() % num_outputs;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t BlockchainBDB::get_num_outputs(const uint64_t& amount) const
|
uint64_t BlockchainBDB::get_num_outputs(const uint64_t& amount) const
|
||||||
{
|
{
|
||||||
LOG_PRINT_L3("BlockchainBDB::" << __func__);
|
LOG_PRINT_L3("BlockchainBDB::" << __func__);
|
||||||
|
|
|
@ -295,8 +295,6 @@ public:
|
||||||
|
|
||||||
virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
|
virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
|
||||||
|
|
||||||
virtual uint64_t get_random_output(const uint64_t& amount) const;
|
|
||||||
|
|
||||||
virtual uint64_t get_num_outputs(const uint64_t& amount) const;
|
virtual uint64_t get_num_outputs(const uint64_t& amount) const;
|
||||||
|
|
||||||
virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index);
|
virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index);
|
||||||
|
|
|
@ -104,7 +104,6 @@
|
||||||
* height get_tx_block_height(hash)
|
* height get_tx_block_height(hash)
|
||||||
*
|
*
|
||||||
* Outputs:
|
* Outputs:
|
||||||
* index get_random_output(amount)
|
|
||||||
* uint64_t get_num_outputs(amount)
|
* uint64_t get_num_outputs(amount)
|
||||||
* pub_key get_output_key(amount, index)
|
* pub_key get_output_key(amount, index)
|
||||||
* tx_out get_output(tx_hash, index)
|
* tx_out get_output(tx_hash, index)
|
||||||
|
@ -463,9 +462,6 @@ public:
|
||||||
// returns height of block that contains transaction with hash <h>
|
// returns height of block that contains transaction with hash <h>
|
||||||
virtual uint64_t get_tx_block_height(const crypto::hash& h) const = 0;
|
virtual uint64_t get_tx_block_height(const crypto::hash& h) const = 0;
|
||||||
|
|
||||||
// return global output index of a random output of amount <amount>
|
|
||||||
virtual uint64_t get_random_output(const uint64_t& amount) const = 0;
|
|
||||||
|
|
||||||
// returns the total number of outputs of amount <amount>
|
// returns the total number of outputs of amount <amount>
|
||||||
virtual uint64_t get_num_outputs(const uint64_t& amount) const = 0;
|
virtual uint64_t get_num_outputs(const uint64_t& amount) const = 0;
|
||||||
|
|
||||||
|
|
|
@ -1620,19 +1620,6 @@ uint64_t BlockchainLMDB::get_tx_block_height(const crypto::hash& h) const
|
||||||
return *(const uint64_t*)result.mv_data;
|
return *(const uint64_t*)result.mv_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
//FIXME: make sure the random method used here is appropriate
|
|
||||||
uint64_t BlockchainLMDB::get_random_output(const uint64_t& amount) const
|
|
||||||
{
|
|
||||||
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
|
|
||||||
check_open();
|
|
||||||
|
|
||||||
uint64_t num_outputs = get_num_outputs(amount);
|
|
||||||
if (num_outputs == 0)
|
|
||||||
throw1(OUTPUT_DNE("Attempting to get a random output for an amount, but none exist"));
|
|
||||||
|
|
||||||
return crypto::rand<uint64_t>() % num_outputs;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t BlockchainLMDB::get_num_outputs(const uint64_t& amount) const
|
uint64_t BlockchainLMDB::get_num_outputs(const uint64_t& amount) const
|
||||||
{
|
{
|
||||||
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
|
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
|
||||||
|
|
|
@ -157,8 +157,6 @@ public:
|
||||||
|
|
||||||
virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
|
virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
|
||||||
|
|
||||||
virtual uint64_t get_random_output(const uint64_t& amount) const;
|
|
||||||
|
|
||||||
virtual uint64_t get_num_outputs(const uint64_t& amount) const;
|
virtual uint64_t get_num_outputs(const uint64_t& amount) const;
|
||||||
|
|
||||||
virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index);
|
virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index);
|
||||||
|
|
|
@ -1511,7 +1511,15 @@ bool Blockchain::get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUT
|
||||||
// get a random output index from the DB. If we've already seen it,
|
// get a random output index from the DB. If we've already seen it,
|
||||||
// return to the top of the loop and try again, otherwise add it to the
|
// return to the top of the loop and try again, otherwise add it to the
|
||||||
// list of output indices we've seen.
|
// list of output indices we've seen.
|
||||||
uint64_t i = m_db->get_random_output(amount);
|
|
||||||
|
// triangular distribution over [a,b) with a=0, mode c=b=up_index_limit
|
||||||
|
uint64_t r = crypto::rand<uint64_t>() % ((uint64_t)1 << 53);
|
||||||
|
double frac = std::sqrt((double)r / ((uint64_t)1 << 53));
|
||||||
|
uint64_t i = (uint64_t)(frac*num_outs);
|
||||||
|
// just in case rounding up to 1 occurs after sqrt
|
||||||
|
if (i == num_outs)
|
||||||
|
--i;
|
||||||
|
|
||||||
if (seen_indices.count(i))
|
if (seen_indices.count(i))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|
Loading…
Reference in a new issue