Skip to content

Commit

Permalink
Merge pull request graft-project#60 from mbg033/wallet-fix-refresh-fr…
Browse files Browse the repository at this point in the history
…om-block-height

[do not merge yet] Wallet fix refresh from block height
  • Loading branch information
laid37 authored Jan 12, 2018
2 parents b6c31e7 + f95c8b2 commit 9669bdd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 46 deletions.
3 changes: 3 additions & 0 deletions src/simplewallet/simplewallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,9 @@ bool simple_wallet::new_wallet(const boost::program_options::variables_map& vm,
return false;
}

if (m_restore_height)
m_wallet->set_refresh_from_block_height(m_restore_height);

bool was_deprecated_wallet = m_restore_deterministic_wallet && ((old_language == crypto::ElectrumWords::old_language_name) ||
crypto::ElectrumWords::get_is_old_style_seed(m_electrum_seed));

Expand Down
59 changes: 36 additions & 23 deletions src/supernode/graft_wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2302,26 +2302,7 @@ crypto::secret_key GraftWallet::generate(const std::string& wallet_, const std::

// try asking the daemon first
if(m_refresh_from_block_height == 0 && !recover){
std::string err;
uint64_t height = 0;

// we get the max of approximated height and known height
// approximated height is the least of daemon target height
// (the max of what the other daemons are claiming is their
// height) and the theoretical height based on the local
// clock. This will be wrong only if both the local clock
// is bad *and* a peer daemon claims a highest height than
// the real chain.
// known height is the height the local daemon is currently
// synced to, it will be lower than the real chain height if
// the daemon is currently syncing.
height = get_approximate_blockchain_height();
uint64_t target_height = get_daemon_blockchain_target_height(err);
if (err.empty() && target_height < height)
height = target_height;
uint64_t local_height = get_daemon_blockchain_height(err);
if (err.empty() && local_height > height)
height = local_height;
uint64_t height = estimate_blockchain_height();
m_refresh_from_block_height = height >= blocks_per_month ? height - blocks_per_month : 0;
}

Expand All @@ -2339,6 +2320,38 @@ crypto::secret_key GraftWallet::generate(const std::string& wallet_, const std::
return retval;
}

uint64_t GraftWallet::estimate_blockchain_height()
{
// -1 month for fluctuations in block time and machine date/time setup.
// avg seconds per block
const int seconds_per_block = DIFFICULTY_TARGET_V2;
// ~num blocks per month
const uint64_t blocks_per_month = 60*60*24*30/seconds_per_block;

// try asking the daemon first
std::string err;
uint64_t height = 0;

// we get the max of approximated height and known height
// approximated height is the least of daemon target height
// (the max of what the other daemons are claiming is their
// height) and the theoretical height based on the local
// clock. This will be wrong only if both the local clock
// is bad *and* a peer daemon claims a highest height than
// the real chain.
// known height is the height the local daemon is currently
// synced to, it will be lower than the real chain height if
// the daemon is currently syncing.
height = get_approximate_blockchain_height();
uint64_t target_height = get_daemon_blockchain_target_height(err);
if (err.empty() && target_height < height)
height = target_height;
uint64_t local_height = get_daemon_blockchain_height(err);
if (err.empty() && local_height > height)
height = local_height;
return height;
}

/*!
* \brief Generates a wallet or restores one.
* \param wallet_ Name of wallet file
Expand Down Expand Up @@ -5659,10 +5672,10 @@ uint64_t GraftWallet::get_daemon_blockchain_target_height(string &err)

uint64_t GraftWallet::get_approximate_blockchain_height() const
{
// time of v2 fork
const time_t fork_time = m_testnet ? 1448285909 : 1458748658;
// time of begining: testnet: 2018-01-12, mainnet: 2018-01-18;
const time_t fork_time = m_testnet ? 1515715200 : 1516233600;
// v2 fork block
const uint64_t fork_block = m_testnet ? 624634 : 1009827;
const uint64_t fork_block = m_testnet ? 1 : 1;
// avg seconds per block
const int seconds_per_block = DIFFICULTY_TARGET_V2;
// Calculated blockchain height
Expand Down
1 change: 1 addition & 0 deletions src/supernode/graft_wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ namespace tools
* \brief Calculates the approximate blockchain height from current date/time.
*/
uint64_t get_approximate_blockchain_height() const;
uint64_t estimate_blockchain_height();
std::vector<size_t> select_available_outputs_from_histogram(uint64_t count, bool atleast, bool unlocked, bool allow_rct, bool trusted_daemon);
std::vector<size_t> select_available_outputs(const std::function<bool(const transfer_details &td)> &f);
std::vector<size_t> select_available_unmixable_outputs(bool trusted_daemon);
Expand Down
59 changes: 36 additions & 23 deletions src/wallet/wallet2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2233,26 +2233,7 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri

// try asking the daemon first
if(m_refresh_from_block_height == 0 && !recover){
std::string err;
uint64_t height = 0;

// we get the max of approximated height and known height
// approximated height is the least of daemon target height
// (the max of what the other daemons are claiming is their
// height) and the theoretical height based on the local
// clock. This will be wrong only if both the local clock
// is bad *and* a peer daemon claims a highest height than
// the real chain.
// known height is the height the local daemon is currently
// synced to, it will be lower than the real chain height if
// the daemon is currently syncing.
height = get_approximate_blockchain_height();
uint64_t target_height = get_daemon_blockchain_target_height(err);
if (err.empty() && target_height < height)
height = target_height;
uint64_t local_height = get_daemon_blockchain_height(err);
if (err.empty() && local_height > height)
height = local_height;
uint64_t height = estimate_blockchain_height();
m_refresh_from_block_height = height >= blocks_per_month ? height - blocks_per_month : 0;
}

Expand All @@ -2270,6 +2251,38 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri
return retval;
}

uint64_t wallet2::estimate_blockchain_height()
{
// -1 month for fluctuations in block time and machine date/time setup.
// avg seconds per block
const int seconds_per_block = DIFFICULTY_TARGET_V2;
// ~num blocks per month
const uint64_t blocks_per_month = 60*60*24*30/seconds_per_block;

// try asking the daemon first
std::string err;
uint64_t height = 0;

// we get the max of approximated height and known height
// approximated height is the least of daemon target height
// (the max of what the other daemons are claiming is their
// height) and the theoretical height based on the local
// clock. This will be wrong only if both the local clock
// is bad *and* a peer daemon claims a highest height than
// the real chain.
// known height is the height the local daemon is currently
// synced to, it will be lower than the real chain height if
// the daemon is currently syncing.
height = get_approximate_blockchain_height();
uint64_t target_height = get_daemon_blockchain_target_height(err);
if (err.empty() && target_height < height)
height = target_height;
uint64_t local_height = get_daemon_blockchain_height(err);
if (err.empty() && local_height > height)
height = local_height;
return height;
}

/*!
* \brief Creates a watch only wallet from a public address and a view secret key.
* \param wallet_ Name of wallet file
Expand Down Expand Up @@ -5105,10 +5118,10 @@ uint64_t wallet2::get_daemon_blockchain_target_height(string &err)

uint64_t wallet2::get_approximate_blockchain_height() const
{
// time of v2 fork
const time_t fork_time = m_testnet ? 1448285909 : 1458748658;
// time of begining: testnet: 2018-01-12, mainnet: 2018-01-18;
const time_t fork_time = m_testnet ? 1515715200 : 1516233600;
// v2 fork block
const uint64_t fork_block = m_testnet ? 624634 : 1009827;
const uint64_t fork_block = m_testnet ? 1 : 1;
// avg seconds per block
const int seconds_per_block = DIFFICULTY_TARGET_V2;
// Calculated blockchain height
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet2.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ namespace tools
* \brief Calculates the approximate blockchain height from current date/time.
*/
uint64_t get_approximate_blockchain_height() const;
uint64_t estimate_blockchain_height();
std::vector<size_t> select_available_outputs_from_histogram(uint64_t count, bool atleast, bool unlocked, bool allow_rct, bool trusted_daemon);
std::vector<size_t> select_available_outputs(const std::function<bool(const transfer_details &td)> &f);
std::vector<size_t> select_available_unmixable_outputs(bool trusted_daemon);
Expand Down

0 comments on commit 9669bdd

Please sign in to comment.