aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2016-11-17 16:37:30 +0200
committerRiccardo Spagni <ric@spagni.net>2016-11-17 16:37:30 +0200
commit5df6f0be806587abee8e1cc956697302747124c8 (patch)
treecdb770190efdc81a316962918a6276cf036f3e30
parentMerge pull request #1344 (diff)
parentWallet2: calculate approximate blockchain height on offline creation (diff)
downloadmonero-5df6f0be806587abee8e1cc956697302747124c8.tar.xz
Merge pull request #1318
4fca34d Wallet2: calculate approximate blockchain height on offline creation (Jacob Brydolf)
-rw-r--r--src/wallet/api/wallet.cpp5
-rw-r--r--src/wallet/api/wallet.h1
-rw-r--r--src/wallet/wallet2.cpp28
-rw-r--r--src/wallet/wallet2.h5
-rw-r--r--src/wallet/wallet2_api.h6
5 files changed, 43 insertions, 2 deletions
diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp
index 134ea601c..d21d8b900 100644
--- a/src/wallet/api/wallet.cpp
+++ b/src/wallet/api/wallet.cpp
@@ -469,7 +469,10 @@ uint64_t WalletImpl::blockChainHeight() const
{
return m_wallet->get_blockchain_current_height();
}
-
+uint64_t WalletImpl::approximateBlockChainHeight() const
+{
+ return m_wallet->get_approximate_blockchain_height();
+}
uint64_t WalletImpl::daemonBlockChainHeight() const
{
std::string err;
diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h
index 372d6efd7..02a46da8c 100644
--- a/src/wallet/api/wallet.h
+++ b/src/wallet/api/wallet.h
@@ -76,6 +76,7 @@ public:
uint64_t balance() const;
uint64_t unlockedBalance() const;
uint64_t blockChainHeight() const;
+ uint64_t approximateBlockChainHeight() const;
uint64_t daemonBlockChainHeight() const;
uint64_t daemonBlockChainTargetHeight() const;
bool synchronized() const;
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp
index 093e5c2aa..15a134257 100644
--- a/src/wallet/wallet2.cpp
+++ b/src/wallet/wallet2.cpp
@@ -2009,6 +2009,19 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri
m_account_public_address = m_account.get_keys().m_account_address;
m_watch_only = false;
+ if(m_refresh_from_block_height == 0 && !recover){
+ // Wallets created offline don't know blockchain height.
+ // Set blockchain height calculated from current date/time
+ // -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;
+ uint64_t approx_blockchain_height = get_approximate_blockchain_height();
+ if(approx_blockchain_height > 0) {
+ m_refresh_from_block_height = approx_blockchain_height - blocks_per_month;
+ }
+ }
bool r = store_keys(m_keys_file, password, false);
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file);
@@ -4614,6 +4627,21 @@ uint64_t wallet2::get_daemon_blockchain_target_height(string &err)
return resp_t.result.target_height;
}
+uint64_t wallet2::get_approximate_blockchain_height() const
+{
+ if (m_testnet) return 0;
+ // time of v2 fork
+ const time_t fork_time = 1458748658;
+ // v2 fork block
+ const uint64_t fork_block = 1009827;
+ // avg seconds per block
+ const int seconds_per_block = DIFFICULTY_TARGET_V2;
+ // Calculated blockchain height
+ uint64_t approx_blockchain_height = fork_block + (time(NULL) - fork_time)/seconds_per_block;
+ LOG_PRINT_L2("Calculated blockchain height: " << approx_blockchain_height);
+ return approx_blockchain_height;
+}
+
void wallet2::set_tx_note(const crypto::hash &txid, const std::string &note)
{
m_tx_notes[txid] = note;
diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h
index d0075e2ec..6168873d5 100644
--- a/src/wallet/wallet2.h
+++ b/src/wallet/wallet2.h
@@ -511,7 +511,10 @@ namespace tools
std::string get_daemon_address() const;
uint64_t get_daemon_blockchain_height(std::string& err);
uint64_t get_daemon_blockchain_target_height(std::string& err);
-
+ /*!
+ * \brief Calculates the approximate blockchain height from current date/time.
+ */
+ uint64_t get_approximate_blockchain_height() const;
std::vector<size_t> select_available_outputs_from_histogram(uint64_t count, bool atleast, bool unlocked, 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);
diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h
index 6fe15d1e3..e624ffa69 100644
--- a/src/wallet/wallet2_api.h
+++ b/src/wallet/wallet2_api.h
@@ -282,6 +282,12 @@ struct Wallet
virtual uint64_t blockChainHeight() const = 0;
/**
+ * @brief approximateBlockChainHeight - returns approximate blockchain height calculated from date/time
+ * @return
+ */
+ virtual uint64_t approximateBlockChainHeight() const = 0;
+
+ /**
* @brief daemonBlockChainHeight - returns daemon blockchain height
* @return 0 - in case error communicating with the daemon.
* status() will return Status_Error and errorString() will return verbose error description