diff --git a/config/config.json b/config/config.json index a279bf0..e65d6e9 100755 --- a/config/config.json +++ b/config/config.json @@ -34,7 +34,7 @@ "wallet_import" : { "_comment": "if fee is 0, then importing is free. fee is in base 1e12, e.g., 0.1 xmr is 0.1 x 1e12 = 100000000000", - "fee" : 1000000000, + "fee" : 0, "testnet" : { "address" : "9tzmPMTViHYM3z6NAgQni1Qm1Emzxy5hQFibPgWD3LVTAz91yok5Eni1pH6zKhBHzpTU15GZooPHSGHXFvFuXEdmEG2sWAZ", diff --git a/sql/openmonero.sql b/sql/openmonero.sql index f967f2e..2b33695 100755 --- a/sql/openmonero.sql +++ b/sql/openmonero.sql @@ -40,6 +40,7 @@ CREATE TABLE IF NOT EXISTS `Accounts` ( `scanned_block_height` int(10) UNSIGNED NOT NULL DEFAULT '0', `scanned_block_timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `start_height` int(10) UNSIGNED NOT NULL DEFAULT '0', + `generated_locally` tinyint(1) NOT NULL DEFAULT '0', `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index e2cf94d..3302e96 100755 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -1067,6 +1067,30 @@ CurrentBlockchainStatus::set_new_searched_blk_no( return true; } +// this can be only used for updateding accont details +// of the same account. Cant be used to change the account +// to different addresses for example. +bool +CurrentBlockchainStatus::update_acc( + const string& address, + XmrAccount const& _acc) +{ + std::lock_guard lck (searching_threads_map_mtx); + + if (searching_threads.count(address) == 0) + { + // thread does not exist + OMERROR << address.substr(0,6) + + ": set_new_searched_blk_no failed:" + " thread does not exist"; + return false; + } + + get_search_thread(address).update_acc(_acc); + + return true; +} + TxSearch& CurrentBlockchainStatus::get_search_thread(string const& acc_address) { diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index bfde8cb..a80b09b 100755 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -242,6 +242,10 @@ public: virtual bool set_new_searched_blk_no(const string& address, uint64_t new_value); + + virtual bool + update_acc(const string& address, + XmrAccount const& _acc); virtual bool get_searched_blk_no(const string& address, diff --git a/src/OpenMoneroRequests.cpp b/src/OpenMoneroRequests.cpp index caef6a9..8a085c6 100755 --- a/src/OpenMoneroRequests.cpp +++ b/src/OpenMoneroRequests.cpp @@ -117,10 +117,6 @@ OpenMoneroRequests::login(const shared_ptr session, const Bytes & body) return; } - j_response["generated_locally"] = bool {acc->generated_locally}; - - j_response["start_height"] = acc->start_height; - // set this flag to indicate that we have just created a // new account in mysql. this information is sent to front-end // as it can disply some greeting window to new users upon @@ -128,6 +124,11 @@ OpenMoneroRequests::login(const shared_ptr session, const Bytes & body) new_account_created = true; } // if (!acc) + + + j_response["generated_locally"] = bool {acc->generated_locally}; + + j_response["start_height"] = acc->start_height; // so by now new account has been created or it already exists @@ -997,6 +998,10 @@ OpenMoneroRequests::submit_raw_tx( session->close( OK, response_body, response_headers); } +//@todo current import_wallet_request end point +// still requires some work. The reason is that +// at this moment it is not clear how it is +// handled in mymonero-app-js void OpenMoneroRequests::import_wallet_request( const shared_ptr< Session > session, const Bytes & body) @@ -1052,11 +1057,36 @@ OpenMoneroRequests::import_wallet_request( auto import_fee = current_bc_status->get_bc_setup().import_fee; - // we dont care about any databases or anything, as importing - // wallet is free. - // just reset the scanned block height in mysql and finish. + // if import is free than just upadte mysql and set new + // tx search block if (import_fee == 0) { + + XmrAccount updated_acc = *xmr_account; + + updated_acc.scanned_block_height = 0; + updated_acc.start_height = 0; + + // set scanned_block_height to 0 to begin + // scanning entire blockchain + + + + // @todo we will have race condition here + // as we updated mysql here, but at the same time + // txsearch tread does the same thing + // and just few lines blow we update_acc yet again + + if (!xmr_accounts->update(*xmr_account, updated_acc)) + { + OMERROR << xmr_address.substr(0,6) + + "Updating scanned_block_height failed!\n"; + + session_close(session, j_response, UNPROCESSABLE_ENTITY, + "Updating scanned_block_height failed!"); + return; + } + // change search blk number in the search thread if (!current_bc_status->set_new_searched_blk_no(xmr_address, 0)) { @@ -1064,6 +1094,14 @@ OpenMoneroRequests::import_wallet_request( "Updating searched_blk_no failed!"); return; } + + if (!current_bc_status + ->update_acc(xmr_address, updated_acc)) + { + session_close(session, j_response, UNPROCESSABLE_ENTITY, + "updating acc in search thread failed!"); + return; + } j_response["request_fulfilled"] = true; j_response["status"] = "Import will start shortly"; @@ -1217,9 +1255,15 @@ OpenMoneroRequests::import_wallet_request( XmrAccount updated_acc = *xmr_account; updated_acc.scanned_block_height = 0; + updated_acc.start_height = 0; // set scanned_block_height to 0 to begin // scanning entire blockchain + + // @todo we will have race condition here + // as we updated mysql here, but at the same time + // txsearch tread does the same thing + // and just few lines blow we update_acc yet again if (!xmr_accounts->update(*xmr_account, updated_acc)) { @@ -1230,6 +1274,7 @@ OpenMoneroRequests::import_wallet_request( "Updating scanned_block_height failed!"); return; } + // if success, set acc to updated_acc; request_fulfilled = true; @@ -1241,7 +1286,14 @@ OpenMoneroRequests::import_wallet_request( session_close(session, j_response, UNPROCESSABLE_ENTITY, "updating searched_blk_no failed!"); return; + } + if (!current_bc_status + ->update_acc(xmr_address, updated_acc)) + { + session_close(session, j_response, UNPROCESSABLE_ENTITY, + "updating acc in search thread failed!"); + return; } j_response["request_fulfilled"] @@ -2047,16 +2099,34 @@ OpenMoneroRequests::create_account( current_blockchain_timestamp); - // accounts generated locally (using create account button) - // will have start height equal to current blockchain height. - // existing accounts, i.e., those imported ones or extenal ones - // will have start_height of 0 to indicated that they could - // have been created years ago - uint64_t start_height = generated_locally - ? current_blockchain_height - : 0; + //@todo setting up start_height and scanned_block_height + //needs to be revisited as they are needed for importing + //wallets. The simples way is when import is free and this + //should already work. More problematic is how to set these + //fields when import fee is non-zero. It depends + //how mymonero is doing this. At the momemnt, I'm not sure. + + uint64_t start_height = current_blockchain_height; + uint64_t scanned_block_height = current_blockchain_height; + + if (current_bc_status->get_bc_setup().import_fee == 0) + { + + // accounts generated locally (using create account button) + // will have start height equal to current blockchain height. + // existing accounts, i.e., those imported ones, also called + // extenal ones will have start_height of 0 to + // indicated that they could + // have been created years ago + + start_height = generated_locally + ? current_blockchain_height : 0; + + // if scan block height is zero (for extranl wallets) + // blockchain scanning starts immedietly. + scanned_block_height = start_height; - uint64_t scanned_block_height = start_height; + } // create new account acc = XmrAccount( diff --git a/src/TxSearch.cpp b/src/TxSearch.cpp index 3f521ca..0ff6783 100755 --- a/src/TxSearch.cpp +++ b/src/TxSearch.cpp @@ -71,6 +71,8 @@ uint64_t blocks_lookahead auto current_bc_status_ptr = current_bc_status.get(); +uint64_t account_id = acc->id.data; + searching_is_ongoing = true; MicroCoreAdapter mcore_addapter {current_bc_status_ptr}; @@ -306,7 +308,7 @@ for (auto const& tx_tuple: txs_data) tx_data.hash = tx_hash_str; tx_data.prefix_hash = tx_hash_prefix_str; tx_data.tx_pub_key = tx_pub_key_str; - tx_data.account_id = acc->id.data; + tx_data.account_id = account_id; tx_data.blockchain_tx_id = blockchain_tx_id; tx_data.total_received = total_received; tx_data.total_sent = 0; // at this stage we don't have @@ -360,7 +362,7 @@ for (auto const& tx_tuple: txs_data) XmrOutput out_data; out_data.id = mysqlpp::null; - out_data.account_id = acc->id.data; + out_data.account_id = account_id; out_data.tx_id = tx_mysql_id; out_data.out_pub_key = pod_to_hex(out_info.pub_key); out_data.tx_pub_key = tx_pub_key_str; @@ -601,6 +603,8 @@ for (auto const& tx_tuple: txs_data) // update scanned_block_height every given interval // or when we reached top of the blockchain +std::lock_guard acc_lck(access_acc); + XmrAccount updated_acc = *acc; updated_acc.scanned_block_height = h2; @@ -977,6 +981,13 @@ TxSearch::delete_existing_tx_if_exists(string const& tx_hash) return true; } +void +TxSearch::update_acc(XmrAccount const& _acc) +{ + std::lock_guard acc_lck(access_acc); + *acc = _acc; +} + // default value of static veriables seconds TxSearch::thread_search_life {600}; diff --git a/src/TxSearch.h b/src/TxSearch.h index 5cf6b4e..6a1aece 100755 --- a/src/TxSearch.h +++ b/src/TxSearch.h @@ -62,6 +62,7 @@ private: mutex getting_eptr; mutex getting_known_outputs_keys; + mutex access_acc; seconds last_ping_timestamp; @@ -127,6 +128,9 @@ public: virtual known_outputs_t get_known_outputs_keys(); + virtual void + update_acc(XmrAccount const& _acc); + virtual void set_exception_ptr() {