From 68dfadbb2a4b275c8d7842f6af00ec8e88e85aa9 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Sat, 1 Jul 2017 14:43:36 +0800 Subject: [PATCH] YourMoneroRequests::import_recent_wallet_request added --- config/config.json | 1 + html/css_2/moncrypt.webflow.css | 9 +++ html/js/controllers/import_wallet.js | 36 +++++++-- html/modals/imported-account.html | 8 +- main.cpp | 5 +- src/CurrentBlockchainStatus.cpp | 1 + src/CurrentBlockchainStatus.h | 2 + src/YourMoneroRequests.cpp | 117 ++++++++++++++++++++++++--- src/YourMoneroRequests.h | 3 + 9 files changed, 162 insertions(+), 20 deletions(-) diff --git a/config/config.json b/config/config.json index b38532e..20438eb 100755 --- a/config/config.json +++ b/config/config.json @@ -33,6 +33,7 @@ }, "refresh_block_status_every_seconds" : 10, "search_thread_life_in_seconds" : 300, + "max_number_of_blocks_to_import" : 8000, "ssl" : { "enable" : false, diff --git a/html/css_2/moncrypt.webflow.css b/html/css_2/moncrypt.webflow.css index c1276da..6681669 100755 --- a/html/css_2/moncrypt.webflow.css +++ b/html/css_2/moncrypt.webflow.css @@ -1268,6 +1268,15 @@ p { font-weight: 500; text-align: center; } +.success-backing { + padding-top: 15px; + border-radius: 3px; + background-color: #008331; + font-family: Proximanova, sans-serif; + color: white; + font-weight: 500; + text-align: center; +} .support-block { display: inline-block; margin-left: 12px; diff --git a/html/js/controllers/import_wallet.js b/html/js/controllers/import_wallet.js index 378bf9d..955c5d7 100755 --- a/html/js/controllers/import_wallet.js +++ b/html/js/controllers/import_wallet.js @@ -26,13 +26,14 @@ // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -thinwalletCtrls.controller("ImportWalletCtrl", function($scope, $location, $http, AccountService, ModalService, $interval) { +thinwalletCtrls.controller("ImportWalletCtrl", function($scope, $location, $http, AccountService, ModalService, $interval, $timeout) { "use strict"; $scope.payment_address = ''; $scope.payment_id = ''; $scope.import_fee = JSBigInt.ZERO; $scope.status = ''; $scope.command = ''; + $scope.success = ''; $scope.no_blocks_to_import = "1000"; function get_import_request() { @@ -57,15 +58,34 @@ thinwalletCtrls.controller("ImportWalletCtrl", function($scope, $location, $http }); } - var getRequestInterval = $interval(get_import_request, 10 * 1000); - get_import_request(); + // var getRequestInterval = $interval(get_import_request, 10 * 1000); + // get_import_request(); + // + // $scope.$on('$destroy', function() { + // $interval.cancel(getRequestInterval); + // }); - $scope.$on('$destroy', function() { - $interval.cancel(getRequestInterval); - }); + $scope.importLast = function() + { + if ($scope.no_blocks_to_import > 8000) { + ModalService.hide('imported-account'); + return; + } + + $http.post(config.apiUrl + "import_recent_wallet_request", { + address: AccountService.getAddress(), + view_key: AccountService.getViewKey(), + no_blocks_to_import: $scope.no_blocks_to_import + }).success(function(data) { + $scope.status = data.status; - $scope.importLast = function(no_blocks) { - alert($scope.no_blocks_to_import); + if (data.request_fulfilled) { + $scope.success = "Request successful. Import will start shortly. This window will close in few seconds."; + $timeout(function(){ModalService.hide('imported-account')}, 5000); + } + }).error(function(err) { + $scope.error = err.Error || err || "An unexpected server error occurred"; + }); } }); diff --git a/html/modals/imported-account.html b/html/modals/imported-account.html index 6e4f00e..dbd0457 100755 --- a/html/modals/imported-account.html +++ b/html/modals/imported-account.html @@ -25,7 +25,7 @@
- +
@@ -34,6 +34,12 @@
+
+

{{error}}

+
+
+

{{success}}

+
diff --git a/main.cpp b/main.cpp index c4bda2e..d13d65d 100755 --- a/main.cpp +++ b/main.cpp @@ -104,6 +104,8 @@ xmreg::CurrentBlockchainStatus::deamon_url = deamon_url; xmreg::CurrentBlockchainStatus::refresh_block_status_every_seconds = config_json["refresh_block_status_every_seconds"]; +xmreg::CurrentBlockchainStatus::max_number_of_blocks_to_import + = config_json["max_number_of_blocks_to_import"]; xmreg::CurrentBlockchainStatus::search_thread_life_in_seconds = config_json["search_thread_life_in_seconds"]; xmreg::CurrentBlockchainStatus::import_fee @@ -159,9 +161,9 @@ MAKE_RESOURCE(get_unspent_outs); MAKE_RESOURCE(get_random_outs); MAKE_RESOURCE(submit_raw_tx); MAKE_RESOURCE(import_wallet_request); +MAKE_RESOURCE(import_recent_wallet_request); MAKE_RESOURCE(get_version); - // restbed service Service service; @@ -173,6 +175,7 @@ service.publish(get_unspent_outs); service.publish(get_random_outs); service.publish(submit_raw_tx); service.publish(import_wallet_request); +service.publish(import_recent_wallet_request); service.publish(get_version); auto settings = make_shared( ); diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index 22b5013..282e0ec 100755 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -29,6 +29,7 @@ bool CurrentBlockchainStatus::do_not_relay{false}; bool CurrentBlockchainStatus::is_running{false}; std::thread CurrentBlockchainStatus::m_thread; uint64_t CurrentBlockchainStatus::refresh_block_status_every_seconds{20}; +uint64_t CurrentBlockchainStatus::max_number_of_blocks_to_import{8000}; uint64_t CurrentBlockchainStatus::search_thread_life_in_seconds {600}; // 10 minutes vector> CurrentBlockchainStatus::mempool_txs; string CurrentBlockchainStatus::import_payment_address; diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index 30a615f..dee472d 100755 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -49,6 +49,8 @@ struct CurrentBlockchainStatus static uint64_t refresh_block_status_every_seconds; + static uint64_t max_number_of_blocks_to_import; + static uint64_t search_thread_life_in_seconds; static string import_payment_address; diff --git a/src/YourMoneroRequests.cpp b/src/YourMoneroRequests.cpp index 2c6f479..7f52cca 100755 --- a/src/YourMoneroRequests.cpp +++ b/src/YourMoneroRequests.cpp @@ -614,18 +614,27 @@ YourMoneroRequests::get_random_outs(const shared_ptr< Session > session, const B uint64_t count = j_request["count"]; + json j_response { + {"amount_outs", json::array()} + }; + vector amounts; - // populate amounts vector so that we can pass it directly to - // daeamon to get random outputs for these amounts - for (json amount: j_request["amounts"]) + try { - amounts.push_back(boost::lexical_cast(amount.get())); + // populate amounts vector so that we can pass it directly to + // daeamon to get random outputs for these amounts + for (json amount: j_request["amounts"]) + { + amounts.push_back(boost::lexical_cast(amount.get())); + } + } + catch (boost::bad_lexical_cast& e) + { + cerr << "Bed lexical cast" << '\n'; + session_close(session, j_response.dump()); + return; } - - json j_response { - {"amount_outs", json::array()} - }; vector found_outputs; @@ -832,6 +841,96 @@ YourMoneroRequests::import_wallet_request(const shared_ptr< Session > session, c } + +void +YourMoneroRequests::import_recent_wallet_request(const shared_ptr< Session > session, const Bytes & body) +{ + json j_response; + json j_request; + + vector requested_values {"address" , "view_key", "no_blocks_to_import"}; + + if (!parse_request(body, requested_values, j_request, j_response)) + { + session_close(session, j_response.dump()); + return; + } + + string xmr_address = j_request["address"]; + string view_key = j_request["view_key"]; + + uint64_t no_blocks_to_import {1000}; + + try + { + no_blocks_to_import = boost::lexical_cast(j_request["no_blocks_to_import"].get()); + } + catch (boost::bad_lexical_cast& e) + { + cerr << "Cant cast " << j_request["no_blocks_to_import"] << " into number." + << " Using default value of " << no_blocks_to_import << '\n'; + } + + // make sure that we dont import more that the maximum alowed no of blocks + no_blocks_to_import = std::min(no_blocks_to_import, + CurrentBlockchainStatus::max_number_of_blocks_to_import); + + bool request_fulfilled {false}; + + XmrAccount acc; + + if (xmr_accounts->select(xmr_address, acc)) + { + XmrAccount updated_acc = acc; + + // make sure scanned_block_height is larger than no_blocks_to_import so we dont + // end up with overflowing uint64_t. + + if (updated_acc.scanned_block_height > no_blocks_to_import) + { + // repetead calls to import_recent_wallet_request will be moving the scanning backward. + // not sure yet if any protection is needed to make sure that a user does not + // go back too much back by importing his/hers wallet multiple times in a row. + updated_acc.scanned_block_height = updated_acc.scanned_block_height - no_blocks_to_import; + + if (xmr_accounts->update(acc, updated_acc)) + { + // change search blk number in the search thread + if (!CurrentBlockchainStatus::set_new_searched_blk_no(xmr_address, + updated_acc.scanned_block_height)) + { + cerr << "Updating searched_blk_no failed!" << endl; + j_response["status"] = "Updating searched_blk_no failed!"; + } + else + { + // if success, set acc to updated_acc; + request_fulfilled = true; + } + } + + } // if (updated_acc.scanned_block_height > no_blocks_to_import) + } + else + { + cerr << "Updating account with new scanned_block_height failed! " << endl; + j_response["status"] = "Updating account with new scanned_block_height failed!"; + } + + if (request_fulfilled) + { + j_response["request_fulfilled"] = request_fulfilled; + j_response["status"] = "Updating account with for importing recent txs successeful."; + } + + string response_body = j_response.dump(); + + auto response_headers = make_headers({{ "Content-Length", to_string(response_body.size())}}); + + session->close( OK, response_body, response_headers); +} + + void YourMoneroRequests::get_version(const shared_ptr< Session > session, const Bytes & body) { @@ -876,8 +975,6 @@ YourMoneroRequests::generic_options_handler( const shared_ptr< Session > session size_t content_length = request->get_header( "Content-Length", 0); - //cout << "generic_options_handler" << endl; - session->fetch(content_length, [](const shared_ptr< Session > session, const Bytes & body) { session->close( OK, string{}, make_headers()); diff --git a/src/YourMoneroRequests.h b/src/YourMoneroRequests.h index bcd5f03..4953c3d 100755 --- a/src/YourMoneroRequests.h +++ b/src/YourMoneroRequests.h @@ -87,6 +87,9 @@ public: void import_wallet_request(const shared_ptr< Session > session, const Bytes & body); + void + import_recent_wallet_request(const shared_ptr< Session > session, const Bytes & body); + void get_version(const shared_ptr< Session > session, const Bytes & body);