aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2019-04-02 14:16:45 +0000
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2019-04-18 15:14:38 +0000
commit35e0a968bde4644a86f6f455b1a50ca25398fa15 (patch)
treebede71958240b9f83161ff7d3b54512ac2d81d27 /src
parentMerge pull request #5415 (diff)
downloadmonero-35e0a968bde4644a86f6f455b1a50ca25398fa15.tar.xz
wallet2: "output lineup" fake out selection
Based on python code by sarang: https://github.com/SarangNoether/skunkworks/blob/outputs/outputs/simulate.py
Diffstat (limited to 'src')
-rw-r--r--src/wallet/wallet2.cpp107
-rw-r--r--src/wallet/wallet2.h25
2 files changed, 72 insertions, 60 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp
index a7da9395c..cfe9e18c6 100644
--- a/src/wallet/wallet2.cpp
+++ b/src/wallet/wallet2.cpp
@@ -29,7 +29,6 @@
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include <numeric>
-#include <random>
#include <tuple>
#include <boost/format.hpp>
#include <boost/optional/optional.hpp>
@@ -129,7 +128,8 @@ using namespace cryptonote;
#define FIRST_REFRESH_GRANULARITY 1024
-#define GAMMA_PICK_HALF_WINDOW 5
+#define GAMMA_SHAPE 19.28
+#define GAMMA_SCALE (1/1.61)
static const std::string MULTISIG_SIGNATURE_MAGIC = "SigMultisigPkV1";
static const std::string MULTISIG_EXTRA_INFO_MAGIC = "MultisigxV1";
@@ -958,6 +958,44 @@ const size_t MAX_SPLIT_ATTEMPTS = 30;
constexpr const std::chrono::seconds wallet2::rpc_timeout;
const char* wallet2::tr(const char* str) { return i18n_translate(str, "tools::wallet2"); }
+gamma_picker::gamma_picker(const std::vector<uint64_t> &rct_offsets, double shape, double scale):
+ rct_offsets(rct_offsets)
+{
+ gamma = std::gamma_distribution<double>(shape, scale);
+ THROW_WALLET_EXCEPTION_IF(rct_offsets.size() <= CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE, error::wallet_internal_error, "Bad offset calculation");
+ const size_t blocks_in_a_year = 86400 * 365 / DIFFICULTY_TARGET_V2;
+ const size_t blocks_to_consider = rct_offsets.size() < blocks_in_a_year;
+ const size_t outputs_to_consider = rct_offsets.back() - (blocks_to_consider < rct_offsets.size() ? rct_offsets[rct_offsets.size() - blocks_to_consider - 1] : 0);
+ begin = rct_offsets.data();
+ end = rct_offsets.data() + rct_offsets.size() - CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE;
+ num_rct_outputs = *(end - 1);
+ THROW_WALLET_EXCEPTION_IF(num_rct_outputs == 0, error::wallet_internal_error, "No rct outputs");
+ average_output_time = DIFFICULTY_TARGET_V2 * rct_offsets.size() / num_rct_outputs; // this assumes constant target over the whole rct range
+};
+
+gamma_picker::gamma_picker(const std::vector<uint64_t> &rct_offsets): gamma_picker(rct_offsets, GAMMA_SHAPE, GAMMA_SCALE) {}
+
+uint64_t gamma_picker::pick()
+{
+ double x = gamma(engine);
+ x = exp(x);
+ uint64_t output_index = x / average_output_time;
+ if (output_index >= num_rct_outputs)
+ return std::numeric_limits<uint64_t>::max(); // bad pick
+ output_index = num_rct_outputs - 1 - output_index;
+
+ const uint64_t *it = std::lower_bound(begin, end, output_index);
+ THROW_WALLET_EXCEPTION_IF(it == end, error::wallet_internal_error, "output_index not found");
+ uint64_t index = std::distance(begin, it);
+
+ const uint64_t first_rct = index == 0 ? 0 : rct_offsets[index - 1];
+ const uint64_t n_rct = rct_offsets[index] - first_rct;
+ if (n_rct == 0)
+ return std::numeric_limits<uint64_t>::max(); // bad pick
+ MDEBUG("Picking 1/" << n_rct << " in block " << index);
+ return first_rct + crypto::rand_idx(n_rct);
+};
+
wallet_keys_unlocker::wallet_keys_unlocker(wallet2 &w, const boost::optional<tools::password_container> &password):
w(w),
locked(password != boost::none)
@@ -7562,61 +7600,9 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>>
COMMAND_RPC_GET_OUTPUTS_BIN::request req = AUTO_VAL_INIT(req);
COMMAND_RPC_GET_OUTPUTS_BIN::response daemon_resp = AUTO_VAL_INIT(daemon_resp);
- struct gamma_engine
- {
- typedef uint64_t result_type;
- static constexpr result_type min() { return 0; }
- static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
- result_type operator()() { return crypto::rand<result_type>(); }
- } engine;
- static const double shape = 19.28/*16.94*/;
- //static const double shape = m_testnet ? 17.02 : 17.28;
- static const double scale = 1/1.61;
- std::gamma_distribution<double> gamma(shape, scale);
- THROW_WALLET_EXCEPTION_IF(rct_offsets.size() <= CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE, error::wallet_internal_error, "Bad offset calculation");
- uint64_t last_usable_block = rct_offsets.size() - 1;
- auto pick_gamma = [&]()
- {
- double x = gamma(engine);
- x = exp(x);
- uint64_t block_offset = x / DIFFICULTY_TARGET_V2; // this assumes constant target over the whole rct range
- if (block_offset > last_usable_block - CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE)
- return std::numeric_limits<uint64_t>::max(); // bad pick
- block_offset = last_usable_block - block_offset;
- THROW_WALLET_EXCEPTION_IF(block_offset > last_usable_block, error::wallet_internal_error, "Bad offset calculation");
- THROW_WALLET_EXCEPTION_IF(block_offset > 0 && rct_offsets[block_offset] < rct_offsets[block_offset - 1],
- error::get_output_distribution, "Decreasing offsets in rct distribution: " +
- std::to_string(block_offset - 1) + ": " + std::to_string(rct_offsets[block_offset - 1]) + ", " +
- std::to_string(block_offset) + ": " + std::to_string(rct_offsets[block_offset]));
- uint64_t first_block_offset = block_offset, last_block_offset = block_offset;
- for (size_t half_window = 0; half_window <= GAMMA_PICK_HALF_WINDOW; ++half_window)
- {
- // end when we have a non empty block
- uint64_t cum0 = first_block_offset > 0 ? rct_offsets[first_block_offset] - rct_offsets[first_block_offset - 1] : rct_offsets[0];
- if (cum0 > 1)
- break;
- uint64_t cum1 = last_block_offset > 0 ? rct_offsets[last_block_offset] - rct_offsets[last_block_offset - 1] : rct_offsets[0];
- if (cum1 > 1)
- break;
- if (first_block_offset == 0 && last_block_offset >= last_usable_block)
- break;
- // expand up to bounds
- if (first_block_offset > 0)
- --first_block_offset;
- else
- return std::numeric_limits<uint64_t>::max(); // bad pick
- if (last_block_offset < last_usable_block - CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE)
- ++last_block_offset;
- else
- return std::numeric_limits<uint64_t>::max(); // bad pick
- }
- const uint64_t first_rct = first_block_offset == 0 ? 0 : rct_offsets[first_block_offset - 1];
- const uint64_t n_rct = rct_offsets[last_block_offset] - first_rct;
- if (n_rct == 0)
- return rct_offsets[block_offset] ? rct_offsets[block_offset] - 1 : 0;
- MDEBUG("Picking 1/" << n_rct << " in " << (last_block_offset - first_block_offset + 1) << " blocks centered around " << block_offset + rct_start_height);
- return first_rct + crypto::rand_idx(n_rct);
- };
+ std::unique_ptr<gamma_picker> gamma;
+ if (has_rct_distribution)
+ gamma.reset(new gamma_picker(rct_offsets));
size_t num_selected_transfers = 0;
for(size_t idx: selected_transfers)
@@ -7814,20 +7800,21 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>>
const char *type = "";
if (amount == 0 && has_rct_distribution)
{
+ THROW_WALLET_EXCEPTION_IF(!gamma, error::wallet_internal_error, "No gamma picker");
// gamma distribution
if (num_found -1 < recent_outputs_count + pre_fork_outputs_count)
{
- do i = pick_gamma(); while (i >= segregation_limit[amount].first);
+ do i = gamma->pick(); while (i >= segregation_limit[amount].first);
type = "pre-fork gamma";
}
else if (num_found -1 < recent_outputs_count + pre_fork_outputs_count + post_fork_outputs_count)
{
- do i = pick_gamma(); while (i < segregation_limit[amount].first || i >= num_outs);
+ do i = gamma->pick(); while (i < segregation_limit[amount].first || i >= num_outs);
type = "post-fork gamma";
}
else
{
- do i = pick_gamma(); while (i >= num_outs);
+ do i = gamma->pick(); while (i >= num_outs);
type = "gamma";
}
}
diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h
index 8561c42ba..d101e87f5 100644
--- a/src/wallet/wallet2.h
+++ b/src/wallet/wallet2.h
@@ -39,6 +39,7 @@
#include <boost/serialization/deque.hpp>
#include <boost/thread/lock_guard.hpp>
#include <atomic>
+#include <random>
#include "include_base_utils.h"
#include "cryptonote_basic/account.h"
@@ -76,6 +77,30 @@ namespace tools
class wallet2;
class Notify;
+ class gamma_picker
+ {
+ public:
+ uint64_t pick();
+ gamma_picker(const std::vector<uint64_t> &rct_offsets);
+ gamma_picker(const std::vector<uint64_t> &rct_offsets, double shape, double scale);
+
+ private:
+ struct gamma_engine
+ {
+ typedef uint64_t result_type;
+ static constexpr result_type min() { return 0; }
+ static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
+ result_type operator()() { return crypto::rand<result_type>(); }
+ } engine;
+
+private:
+ std::gamma_distribution<double> gamma;
+ const std::vector<uint64_t> &rct_offsets;
+ const uint64_t *begin, *end;
+ uint64_t num_rct_outputs;
+ double average_output_time;
+ };
+
class wallet_keys_unlocker
{
public: