libwallet_api: fast-refresh for new wallet

This commit is contained in:
Ilya Kitaev 2016-09-30 02:11:28 +03:00
parent 4789347b27
commit 1f73f80c94
3 changed files with 56 additions and 12 deletions

View File

@ -175,6 +175,7 @@ WalletImpl::WalletImpl(bool testnet)
m_wallet->callback(m_wallet2Callback); m_wallet->callback(m_wallet2Callback);
m_refreshThreadDone = false; m_refreshThreadDone = false;
m_refreshEnabled = false; m_refreshEnabled = false;
m_newWallet = true;
m_refreshIntervalMillis = DEFAULT_REFRESH_INTERVAL_MILLIS; m_refreshIntervalMillis = DEFAULT_REFRESH_INTERVAL_MILLIS;
@ -195,6 +196,7 @@ WalletImpl::~WalletImpl()
bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language)
{ {
m_newWallet = true;
clearStatus(); clearStatus();
bool keys_file_exists; bool keys_file_exists;
@ -232,6 +234,7 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co
bool WalletImpl::open(const std::string &path, const std::string &password) bool WalletImpl::open(const std::string &path, const std::string &password)
{ {
m_newWallet = false;
clearStatus(); clearStatus();
try { try {
// TODO: handle "deprecated" // TODO: handle "deprecated"
@ -385,11 +388,7 @@ string WalletImpl::keysFilename() const
bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit) bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit)
{ {
clearStatus(); clearStatus();
doInit(daemon_address, upper_transaction_size_limit);
m_wallet->init(daemon_address, upper_transaction_size_limit);
if (Utils::isAddressLocal(daemon_address)) {
this->setTrustedDaemon(true);
}
bool result = this->refresh(); bool result = this->refresh();
// enabling background refresh thread // enabling background refresh thread
startRefresh(); startRefresh();
@ -400,10 +399,7 @@ bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transact
void WalletImpl::initAsync(const string &daemon_address, uint64_t upper_transaction_size_limit) void WalletImpl::initAsync(const string &daemon_address, uint64_t upper_transaction_size_limit)
{ {
clearStatus(); clearStatus();
m_wallet->init(daemon_address, upper_transaction_size_limit); doInit(daemon_address, upper_transaction_size_limit);
if (Utils::isAddressLocal(daemon_address)) {
this->setTrustedDaemon(true);
}
startRefresh(); startRefresh();
} }
@ -748,4 +744,24 @@ void WalletImpl::pauseRefresh()
} }
bool WalletImpl::isNewWallet() const
{
return m_newWallet;
}
void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit)
{
m_wallet->init(daemon_address, upper_transaction_size_limit);
// in case new wallet, this will force fast-refresh (pulling hashes instead of blocks)
if (isNewWallet()) {
m_wallet->set_refresh_from_block_height(daemonBlockChainHeight());
}
if (Utils::isAddressLocal(daemon_address)) {
this->setTrustedDaemon(true);
}
}
} // namespace } // namespace

View File

@ -101,6 +101,8 @@ private:
void startRefresh(); void startRefresh();
void stopRefresh(); void stopRefresh();
void pauseRefresh(); void pauseRefresh();
bool isNewWallet() const;
void doInit(const std::string &daemon_address, uint64_t upper_transaction_size_limit);
private: private:
friend class PendingTransactionImpl; friend class PendingTransactionImpl;
@ -126,6 +128,7 @@ private:
boost::mutex m_refreshMutex2; boost::mutex m_refreshMutex2;
boost::condition_variable m_refreshCV; boost::condition_variable m_refreshCV;
boost::thread m_refreshThread; boost::thread m_refreshThread;
bool m_newWallet;
}; };

View File

@ -830,8 +830,12 @@ struct MyWalletListener : public Bitmonero::WalletListener
virtual void newBlock(uint64_t height) virtual void newBlock(uint64_t height)
{ {
std::cout << "wallet: " << wallet->address() // std::cout << "wallet: " << wallet->address()
<<", new block received, blockHeight: " << height << std::endl; // <<", new block received, blockHeight: " << height << std::endl;
static int bc_height = wallet->daemonBlockChainHeight();
std::cout << height
<< " / " << bc_height/* 0*/
<< std::endl;
newblock_triggered = true; newblock_triggered = true;
cv_newblock.notify_one(); cv_newblock.notify_one();
} }
@ -1006,7 +1010,27 @@ TEST_F(WalletTest2, WalletCallbackNewBlock)
} }
TEST_F(WalletManagerMainnetTest, CreateOpenAndRefreshWalletMainNet) TEST_F(WalletManagerMainnetTest, CreateOpenAndRefreshWalletMainNetSync)
{
Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME_MAINNET, "", WALLET_LANG);
MyWalletListener * wallet_listener = new MyWalletListener(wallet);
std::chrono::seconds wait_for = std::chrono::seconds(30);
std::unique_lock<std::mutex> lock (wallet_listener->mutex);
// wallet->initAsync(MAINNET_DAEMON_ADDRESS, 0);
wallet->init(MAINNET_DAEMON_ADDRESS, 0);
std::cerr << "TEST: waiting on refresh lock...\n";
//wallet_listener->cv_refresh.wait_for(lock, wait_for);
std::cerr << "TEST: refresh lock acquired...\n";
ASSERT_TRUE(wallet_listener->refresh_triggered);
ASSERT_TRUE(wallet->connected());
ASSERT_TRUE(wallet->blockChainHeight() == wallet->daemonBlockChainHeight());
std::cerr << "TEST: closing wallet...\n";
wmgr->closeWallet(wallet);
}
TEST_F(WalletManagerMainnetTest, CreateOpenAndRefreshWalletMainNetAsync)
{ {
Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME_MAINNET, "", WALLET_LANG); Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME_MAINNET, "", WALLET_LANG);
@ -1014,6 +1038,7 @@ TEST_F(WalletManagerMainnetTest, CreateOpenAndRefreshWalletMainNet)
std::chrono::seconds wait_for = std::chrono::seconds(30); std::chrono::seconds wait_for = std::chrono::seconds(30);
std::unique_lock<std::mutex> lock (wallet_listener->mutex); std::unique_lock<std::mutex> lock (wallet_listener->mutex);
wallet->initAsync(MAINNET_DAEMON_ADDRESS, 0); wallet->initAsync(MAINNET_DAEMON_ADDRESS, 0);
// wallet->init(MAINNET_DAEMON_ADDRESS, 0);
std::cerr << "TEST: waiting on refresh lock...\n"; std::cerr << "TEST: waiting on refresh lock...\n";
wallet_listener->cv_refresh.wait_for(lock, wait_for); wallet_listener->cv_refresh.wait_for(lock, wait_for);
std::cerr << "TEST: refresh lock acquired...\n"; std::cerr << "TEST: refresh lock acquired...\n";