diff options
Diffstat (limited to 'src/wallet')
-rw-r--r-- | src/wallet/wallet2.cpp | 303 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 311 | ||||
-rw-r--r-- | src/wallet/wallet_errors.h | 611 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server.cpp | 132 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server.h | 55 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server_commans_defs.h | 91 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server_error_codes.h | 11 |
7 files changed, 1160 insertions, 354 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 7b898755a..572affb2f 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -25,25 +25,25 @@ using namespace cryptonote; namespace tools { //---------------------------------------------------------------------------------------------------- -bool wallet2::init(const std::string& daemon_address, uint64_t upper_transaction_size_limit) +void wallet2::init(const std::string& daemon_address, uint64_t upper_transaction_size_limit) { m_upper_transaction_size_limit = upper_transaction_size_limit; m_daemon_address = daemon_address; - return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::process_new_transaction(cryptonote::transaction& tx, uint64_t height, fail_details& fd) +void wallet2::process_new_transaction(const cryptonote::transaction& tx, uint64_t height) { + process_unconfirmed(tx); std::vector<size_t> outs; uint64_t tx_money_got_in_outs = 0; crypto::public_key tx_pub_key = null_pkey; bool r = parse_and_validate_tx_extra(tx, tx_pub_key); - fd.reason = fail_details::error_to_parse_tx_extra; - CHECK_AND_ASSERT_MES(r && tx_pub_key != null_pkey, false, "process_new_transaction failed."); + CHECK_AND_THROW_WALLET_EX(!r, error::tx_extra_parse_error, tx); + r = lookup_acc_outs(m_account.get_keys(), tx, tx_pub_key, outs, tx_money_got_in_outs); - fd.reason = fail_details::error_invalid_tx; - CHECK_AND_ASSERT_MES(r, false, "call lookup_acc_outs failed"); - if(outs.size() && tx_money_got_in_outs) + CHECK_AND_THROW_WALLET_EX(!r, error::acc_outs_lookup_error, tx, tx_pub_key, m_account.get_keys()); + + if(!outs.empty() && tx_money_got_in_outs) { //good news - got money! take care about it //usually we have only one transfer for user in transaction @@ -51,25 +51,18 @@ bool wallet2::process_new_transaction(cryptonote::transaction& tx, uint64_t heig cryptonote::COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response res = AUTO_VAL_INIT(res); req.txid = get_transaction_hash(tx); bool r = net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/get_o_indexes.bin", req, res, m_http_client, WALLET_RCP_CONNECTION_TIMEOUT); - if (!r) fd.reason = fail_details::error_not_connected; - else if (CORE_RPC_STATUS_BUSY == res.status) fd.reason = fail_details::error_daemon_is_busy; - else if (CORE_RPC_STATUS_OK != res.status) fd.reason = fail_details::error_internal_error; - else fd.reason = fail_details::error_ok; - if (fail_details::error_ok != fd.reason) - { - // in case of split while lookup_acc_outs, transaction could be lost (especially if it is coinbase tx) - LOG_PRINT_L0("failed to invoke get_o_indexes.bin: " << interpret_rpc_response(r, res.status)); - return false; - } - - fd.reason = fail_details::error_internal_error; - CHECK_AND_ASSERT_MES(res.o_indexes.size() == tx.vout.size(), false, "internal error: transactions outputs size=" << tx.vout.size() - << " not match with COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES response size=" << res.o_indexes.size()); + CHECK_AND_THROW_WALLET_EX(!r, error::no_connection_to_daemon, "get_o_indexes.bin"); + CHECK_AND_THROW_WALLET_EX(res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_o_indexes.bin"); + CHECK_AND_THROW_WALLET_EX(res.status != CORE_RPC_STATUS_OK, error::get_out_indices_error, res.status); + CHECK_AND_THROW_WALLET_EX(res.o_indexes.size() != tx.vout.size(), error::wallet_internal_error, + "transactions outputs size=" + std::to_string(tx.vout.size()) + + " not match with COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES response size=" + std::to_string(res.o_indexes.size())); BOOST_FOREACH(size_t o, outs) { - fd.reason = fail_details::error_invalid_tx; - CHECK_AND_ASSERT_MES(o < tx.vout.size(), false, "wrong out in transaction: internal index=" << o << ", total_outs" << tx.vout.size()); + CHECK_AND_THROW_WALLET_EX(tx.vout.size() <= o, error::wallet_internal_error, "wrong out in transaction: internal index=" + + std::to_string(o) + ", total_outs=" + std::to_string(tx.vout.size())); + m_transfers.push_back(boost::value_initialized<transfer_details>()); transfer_details& td = m_transfers.back(); td.m_block_height = height; @@ -79,12 +72,13 @@ bool wallet2::process_new_transaction(cryptonote::transaction& tx, uint64_t heig td.m_spent = false; cryptonote::keypair in_ephemeral; cryptonote::generate_key_image_helper(m_account.get_keys(), tx_pub_key, o, in_ephemeral, td.m_key_image); - fd.reason = fail_details::error_internal_error; - CHECK_AND_ASSERT_MES(in_ephemeral.pub == boost::get<cryptonote::txout_to_key>(tx.vout[o].target).key, - false, "internal error: at key_image generating ephemeral public key not matched with output_key"); + CHECK_AND_THROW_WALLET_EX(in_ephemeral.pub != boost::get<cryptonote::txout_to_key>(tx.vout[o].target).key, + error::wallet_internal_error, "key_image generated ephemeral public key not matched with output_key"); + m_key_images[td.m_key_image] = m_transfers.size()-1; - LOG_PRINT_COLOR("Received money: " << print_money(td.amount()) << ", with tx: " << get_transaction_hash(tx), - LOG_LEVEL_0, epee::log_space::console_color_green); + LOG_PRINT_L0("Received money: " << print_money(td.amount()) << ", with tx: " << get_transaction_hash(tx)); + if (0 != m_callback) + m_callback->on_money_received(height, td.m_tx, td.m_internal_output_index); } } // check all outputs for spending (compare key images) @@ -95,36 +89,42 @@ bool wallet2::process_new_transaction(cryptonote::transaction& tx, uint64_t heig auto it = m_key_images.find(boost::get<cryptonote::txin_to_key>(in).k_image); if(it != m_key_images.end()) { - LOG_PRINT_COLOR("Spent money: " << print_money(boost::get<cryptonote::txin_to_key>(in).amount) << ", with tx: " << get_transaction_hash(tx), - LOG_LEVEL_0, epee::log_space::console_color_magenta); - m_transfers[it->second].m_spent = true; + LOG_PRINT_L0("Spent money: " << print_money(boost::get<cryptonote::txin_to_key>(in).amount) << ", with tx: " << get_transaction_hash(tx)); + transfer_details& td = m_transfers[it->second]; + td.m_spent = true; + if (0 != m_callback) + m_callback->on_money_spent(height, td.m_tx, td.m_internal_output_index, tx); } } - return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::process_new_blockchain_entry(cryptonote::block& b, cryptonote::block_complete_entry& bche, crypto::hash& bl_id, uint64_t height, fail_details& fd) +void wallet2::process_unconfirmed(const cryptonote::transaction& tx) +{ + auto unconf_it = m_unconfirmed_txs.find(get_transaction_hash(tx)); + if(unconf_it != m_unconfirmed_txs.end()) + m_unconfirmed_txs.erase(unconf_it); +} +//---------------------------------------------------------------------------------------------------- +void wallet2::process_new_blockchain_entry(const cryptonote::block& b, cryptonote::block_complete_entry& bche, crypto::hash& bl_id, uint64_t height) { //handle transactions from new block - fd.reason = fail_details::error_internal_error; - CHECK_AND_ASSERT_MES(height == m_blockchain.size(), false, "internal error: current_index=" << height << ", m_blockchain.size()=" << m_blockchain.size()); + CHECK_AND_THROW_WALLET_EX(height != m_blockchain.size(), error::wallet_internal_error, + "current_index=" + std::to_string(height) + ", m_blockchain.size()=" + std::to_string(m_blockchain.size())); + //optimization: seeking only for blocks that are not older then the wallet creation time plus 1 day. 1 day is for possible user incorrect time setup if(b.timestamp + 60*60*24 > m_account.get_createtime()) { TIME_MEASURE_START(miner_tx_handle_time); - bool r = process_new_transaction(b.miner_tx, height, fd); + process_new_transaction(b.miner_tx, height); TIME_MEASURE_FINISH(miner_tx_handle_time); - CHECK_AND_NO_ASSERT_MES(r, false, "failed to process transaction"); TIME_MEASURE_START(txs_handle_time); BOOST_FOREACH(auto& txblob, bche.txs) { cryptonote::transaction tx; - r = parse_and_validate_tx_from_blob(txblob, tx); - fd.reason = fail_details::error_to_parse_tx; - CHECK_AND_ASSERT_MES(r, false, "failed to parse and validate transaction from blob"); - r = process_new_transaction(tx, height, fd); - CHECK_AND_ASSERT_MES(r, false, "failed to process transaction"); + bool r = parse_and_validate_tx_from_blob(txblob, tx); + CHECK_AND_THROW_WALLET_EX(!r, error::tx_parse_error, txblob); + process_new_transaction(tx, height); } TIME_MEASURE_FINISH(txs_handle_time); LOG_PRINT_L2("Processed block: " << bl_id << ", height " << height << ", " << miner_tx_handle_time + txs_handle_time << "(" << miner_tx_handle_time << "/" << txs_handle_time <<")ms"); @@ -134,16 +134,18 @@ bool wallet2::process_new_blockchain_entry(cryptonote::block& b, cryptonote::blo } m_blockchain.push_back(bl_id); ++m_local_bc_height; - return true; + + if (0 != m_callback) + m_callback->on_new_block(height, b); } //---------------------------------------------------------------------------------------------------- -bool wallet2::get_short_chain_history(std::list<crypto::hash>& ids) +void wallet2::get_short_chain_history(std::list<crypto::hash>& ids) { size_t i = 0; size_t current_multiplier = 1; size_t sz = m_blockchain.size(); if(!sz) - return true; + return; size_t current_back_offset = 1; bool genesis_included = false; while(current_back_offset < sz) @@ -162,77 +164,68 @@ bool wallet2::get_short_chain_history(std::list<crypto::hash>& ids) } if(!genesis_included) ids.push_back(m_blockchain[0]); - - return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::pull_blocks(size_t& blocks_added, fail_details& fd) +void wallet2::pull_blocks(size_t& blocks_added) { blocks_added = 0; cryptonote::COMMAND_RPC_GET_BLOCKS_FAST::request req = AUTO_VAL_INIT(req); cryptonote::COMMAND_RPC_GET_BLOCKS_FAST::response res = AUTO_VAL_INIT(res); get_short_chain_history(req.block_ids); bool r = net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/getblocks.bin", req, res, m_http_client, WALLET_RCP_CONNECTION_TIMEOUT); - if (!r) fd.reason = fail_details::error_not_connected; - else if (CORE_RPC_STATUS_BUSY == res.status) fd.reason = fail_details::error_daemon_is_busy; - else if (CORE_RPC_STATUS_OK != res.status) fd.reason = fail_details::error_internal_error; - else fd.reason = fail_details::error_ok; - if (fail_details::error_ok != fd.reason) - { - LOG_PRINT_L0("failed to get blocks: " << interpret_rpc_response(r, res.status)); - return false; - } - - //find split position, if split happened - - fd.reason = fail_details::error_internal_error; - CHECK_AND_ASSERT_MES(res.start_height < m_blockchain.size(), false, "wrong daemon response: m_start_height=" - << res.start_height << " not less than local blockchain size=" << m_blockchain.size()); + CHECK_AND_THROW_WALLET_EX(!r, error::no_connection_to_daemon, "getblocks.bin"); + CHECK_AND_THROW_WALLET_EX(res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "getblocks.bin"); + CHECK_AND_THROW_WALLET_EX(res.status != CORE_RPC_STATUS_OK, error::get_blocks_error, res.status); + CHECK_AND_THROW_WALLET_EX(m_blockchain.size() <= res.start_height, error::wallet_internal_error, + "wrong daemon response: m_start_height=" + std::to_string(res.start_height) + + " not less than local blockchain size=" + std::to_string(m_blockchain.size())); size_t current_index = res.start_height; BOOST_FOREACH(auto& bl_entry, res.blocks) { cryptonote::block bl; r = cryptonote::parse_and_validate_block_from_blob(bl_entry.block, bl); - fd.reason = fail_details::error_to_parse_block; - CHECK_AND_ASSERT_MES(r, false, "failed to parse/validate block"); + CHECK_AND_THROW_WALLET_EX(!r, error::block_parse_error, bl_entry.block); + crypto::hash bl_id = get_block_hash(bl); if(current_index >= m_blockchain.size()) { - r = process_new_blockchain_entry(bl, bl_entry, bl_id, current_index, fd); - if(!r) return false; + process_new_blockchain_entry(bl, bl_entry, bl_id, current_index); ++blocks_added; - }else + } + else if(bl_id != m_blockchain[current_index]) { - if(bl_id != m_blockchain[current_index]) - { - //split detected here !!! - fd.reason = fail_details::error_internal_error; - CHECK_AND_ASSERT_MES(current_index != res.start_height, false, "wrong daemon response: first block in response " << string_tools::pod_to_hex(bl_id) - << "\nnot match with local block id " << string_tools::pod_to_hex(m_blockchain[current_index])); - detach_blockchain(current_index); - r = process_new_blockchain_entry(bl, bl_entry, bl_id, current_index, fd); - if(!r) return false; - } + //split detected here !!! + CHECK_AND_THROW_WALLET_EX(current_index == res.start_height, error::wallet_internal_error, + "wrong daemon response: split starts from the first block in response " + string_tools::pod_to_hex(bl_id) + + " (height " + std::to_string(res.start_height) + "), local block id at this height: " + + string_tools::pod_to_hex(m_blockchain[current_index])); + + detach_blockchain(current_index); + process_new_blockchain_entry(bl, bl_entry, bl_id, current_index); } + else + { + LOG_PRINT_L2("Block is already in blockchain: " << string_tools::pod_to_hex(bl_id)); + } + ++current_index; } - return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::refresh(fail_details& fd) +void wallet2::refresh() { size_t blocks_fetched = 0; - return refresh(blocks_fetched, fd); + refresh(blocks_fetched); } //---------------------------------------------------------------------------------------------------- -bool wallet2::refresh(size_t & blocks_fetched, fail_details& fd) +void wallet2::refresh(size_t & blocks_fetched) { bool received_money = false; - return refresh(blocks_fetched, received_money, fd); + refresh(blocks_fetched, received_money); } //---------------------------------------------------------------------------------------------------- -bool wallet2::refresh(size_t & blocks_fetched, bool& received_money, fail_details& fd) +void wallet2::refresh(size_t & blocks_fetched, bool& received_money) { received_money = false; blocks_fetched = 0; @@ -242,33 +235,49 @@ bool wallet2::refresh(size_t & blocks_fetched, bool& received_money, fail_detail while(m_run.load(std::memory_order_relaxed)) { - bool res = pull_blocks(added_blocks, fd); - if(!res) + try + { + pull_blocks(added_blocks); + blocks_fetched += added_blocks; + if(!added_blocks) + break; + } + catch (const std::exception&) { + blocks_fetched += added_blocks; if(try_count < 3) { LOG_PRINT_L1("Another try pull_blocks (try_count=" << try_count << ")..."); ++try_count; - continue; - }else + } + else { - LOG_PRINT_L1("pull_blocks failed, try_count=" << try_count); - return false; + LOG_ERROR("pull_blocks failed, try_count=" << try_count); + throw; } } - blocks_fetched+=added_blocks; - - if(!added_blocks) - break; } if(last_tx_hash_id != (m_transfers.size() ? get_transaction_hash(m_transfers.back().m_tx) : null_hash)) received_money = true; LOG_PRINT_L1("Refresh done, blocks received: " << blocks_fetched << ", balance: " << print_money(balance()) << ", unlocked: " << print_money(unlocked_balance())); - return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::detach_blockchain(uint64_t height) +bool wallet2::refresh(size_t & blocks_fetched, bool& received_money, bool& ok) +{ + try + { + refresh(blocks_fetched, received_money); + ok = true; + } + catch (...) + { + ok = false; + } + return ok; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::detach_blockchain(uint64_t height) { LOG_PRINT_L0("Detaching blockchain on height " << height); size_t transfers_detached = 0; @@ -279,7 +288,7 @@ bool wallet2::detach_blockchain(uint64_t height) for(size_t i = i_start; i!= m_transfers.size();i++) { auto it_ki = m_key_images.find(m_transfers[i].m_key_image); - CHECK_AND_ASSERT_MES(it_ki != m_key_images.end(), false, "key image not found"); + CHECK_AND_THROW_WALLET_EX(it_ki == m_key_images.end(), error::wallet_internal_error, "key image not found"); m_key_images.erase(it_ki); ++transfers_detached; } @@ -290,8 +299,6 @@ bool wallet2::detach_blockchain(uint64_t height) m_local_bc_height -= blocks_detached; LOG_PRINT_L0("Detached blockchain on height " << height << ", transfers detached " << transfers_detached << ", blocks detached " << blocks_detached); - - return true; } //---------------------------------------------------------------------------------------------------- bool wallet2::deinit() @@ -343,13 +350,14 @@ namespace } } //---------------------------------------------------------------------------------------------------- -bool wallet2::load_keys(const std::string& keys_file_name, const std::string& password) +void wallet2::load_keys(const std::string& keys_file_name, const std::string& password) { wallet2::keys_file_data keys_file_data; std::string buf; bool r = epee::file_io_utils::load_file_to_string(keys_file_name, buf); - r &= ::serialization::parse_binary(buf, keys_file_data); - CHECK_AND_ASSERT_MES(r, false, "failed to load wallet keys file: " << keys_file_name); + CHECK_AND_THROW_WALLET_EX(!r, error::file_read_error, keys_file_name); + r = ::serialization::parse_binary(buf, keys_file_data); + CHECK_AND_THROW_WALLET_EX(!r, error::wallet_internal_error, "internal error: failed to deserialize \"" + keys_file_name + '\"'); crypto::chacha8_key key; crypto::generate_chacha8_key(password, key); @@ -361,34 +369,28 @@ bool wallet2::load_keys(const std::string& keys_file_name, const std::string& pa r = epee::serialization::load_t_from_binary(m_account, account_data); r = r && verify_keys(keys.m_view_secret_key, keys.m_account_address.m_view_public_key); r = r && verify_keys(keys.m_spend_secret_key, keys.m_account_address.m_spend_public_key); - if (!r) - { - LOG_ERROR("invalid password"); - return false; - } - - return true; + CHECK_AND_THROW_WALLET_EX(!r, error::invalid_password); } //---------------------------------------------------------------------------------------------------- -bool wallet2::generate(const std::string& wallet_, const std::string& password) +void wallet2::generate(const std::string& wallet_, const std::string& password) { clear(); prepare_file_names(wallet_); - boost::system::error_code e; - if(boost::filesystem::exists(m_wallet_file, e) || boost::filesystem::exists(m_keys_file, e)) - { - LOG_PRINT_RED_L0("failed to generate wallet, file already exist or wrong path: " << wallet_); - return false; - } + + boost::system::error_code ignored_ec; + CHECK_AND_THROW_WALLET_EX(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, m_wallet_file); + CHECK_AND_THROW_WALLET_EX(boost::filesystem::exists(m_keys_file, ignored_ec), error::file_exists, m_keys_file); m_account.generate(); m_account_public_address = m_account.get_keys().m_account_address; bool r = store_keys(m_keys_file, password); - CHECK_AND_ASSERT_MES(r, false, "Failed to store wallet key files!"); + CHECK_AND_THROW_WALLET_EX(!r, error::file_save_error, m_keys_file); + r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str()); if(!r) LOG_PRINT_RED_L0("String with address text not saved"); - return store(); + + store(); } //---------------------------------------------------------------------------------------------------- bool wallet2::prepare_file_names(const std::string& file_path) @@ -418,51 +420,46 @@ bool wallet2::check_connection() return m_http_client.connect(u.host, std::to_string(u.port), WALLET_RCP_CONNECTION_TIMEOUT); } //---------------------------------------------------------------------------------------------------- -bool wallet2::load(const std::string& wallet_, const std::string& password) +void wallet2::load(const std::string& wallet_, const std::string& password) { clear(); prepare_file_names(wallet_); - boost::system::error_code e; - if(!boost::filesystem::exists(m_keys_file, e) || e) - { - LOG_PRINT_L0("file not found: " << m_keys_file); - return false; - } - bool r = load_keys(m_keys_file, password); - if (!r) - return false; + boost::system::error_code e; + bool exists = boost::filesystem::exists(m_keys_file, e); + CHECK_AND_THROW_WALLET_EX(e || !exists, error::file_not_found, m_keys_file); + load_keys(m_keys_file, password); LOG_PRINT_L0("Loaded wallet keys file, with public address: " << m_account.get_public_address_str()); + //keys loaded ok! //try to load wallet file. but even if we failed, it is not big problem if(!boost::filesystem::exists(m_wallet_file, e) || e) { LOG_PRINT_L0("file not found: " << m_wallet_file << ", starting with empty blockchain"); m_account_public_address = m_account.get_keys().m_account_address; - return true; + return; } - r = tools::unserialize_obj_from_file(*this, m_wallet_file); - CHECK_AND_ASSERT_MES(r, false, "failed to load wallet from file " << m_wallet_file); - CHECK_AND_ASSERT_MES(m_account_public_address.m_spend_public_key == m_account.get_keys().m_account_address.m_spend_public_key && - m_account_public_address.m_view_public_key == m_account.get_keys().m_account_address.m_view_public_key, - false, "addresses of wallet keys file and wallet data file are mismatched"); - if(!m_blockchain.size()) + bool r = tools::unserialize_obj_from_file(*this, m_wallet_file); + CHECK_AND_THROW_WALLET_EX(!r, error::file_read_error, m_wallet_file); + CHECK_AND_THROW_WALLET_EX( + m_account_public_address.m_spend_public_key != m_account.get_keys().m_account_address.m_spend_public_key || + m_account_public_address.m_view_public_key != m_account.get_keys().m_account_address.m_view_public_key, + error::wallet_files_doesnt_correspond, m_keys_file, m_wallet_file); + + if(m_blockchain.empty()) { cryptonote::block b; cryptonote::generate_genesis_block(b); m_blockchain.push_back(get_block_hash(b)); } m_local_bc_height = m_blockchain.size(); - - return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::store() +void wallet2::store() { bool r = tools::serialize_obj_to_file(*this, m_wallet_file); - CHECK_AND_ASSERT_MES(r, false, "failed to save wallet to file " << m_wallet_file); - return r; + CHECK_AND_THROW_WALLET_EX(!r, error::file_save_error, m_wallet_file); } //---------------------------------------------------------------------------------------------------- uint64_t wallet2::unlocked_balance() @@ -482,13 +479,16 @@ uint64_t wallet2::balance() if(!td.m_spent) amount += td.amount(); + + BOOST_FOREACH(auto& utx, m_unconfirmed_txs) + amount+= utx.second.m_change; + return amount; } //---------------------------------------------------------------------------------------------------- -bool wallet2::get_transfers(wallet2::transfer_container& incoming_transfers) +void wallet2::get_transfers(wallet2::transfer_container& incoming_transfers) const { incoming_transfers = m_transfers; - return true; } //---------------------------------------------------------------------------------------------------- bool wallet2::is_transfer_unlocked(const transfer_details& td) const @@ -581,22 +581,25 @@ uint64_t wallet2::select_transfers(uint64_t needed_money, bool add_dust, uint64_ return found_money; } //---------------------------------------------------------------------------------------------------- -bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, - uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx) +void wallet2::add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t change_amount) { - return transfer(dsts, fake_outputs_count, unlock_time, fee, detail::digit_split_strategy, tx_dust_policy(fee), tx); + unconfirmed_transfer_details& utd = m_unconfirmed_txs[cryptonote::get_transaction_hash(tx)]; + utd.m_change = change_amount; + utd.m_sent_time = time(NULL); + utd.m_tx = tx; } //---------------------------------------------------------------------------------------------------- -bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx, fail_details& tfd) +void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, + uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx) { - return transfer(dsts, fake_outputs_count, unlock_time, fee, detail::digit_split_strategy, tx_dust_policy(fee), tx, tfd); + transfer(dsts, fake_outputs_count, unlock_time, fee, detail::digit_split_strategy, tx_dust_policy(fee), tx); } //---------------------------------------------------------------------------------------------------- -bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, +void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee) { cryptonote::transaction tx; - return transfer(dsts, fake_outputs_count, unlock_time, fee, tx); + transfer(dsts, fake_outputs_count, unlock_time, fee, tx); } //---------------------------------------------------------------------------------------------------- } diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index f26f8022e..74de1b6d9 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -8,6 +8,7 @@ #include <boost/serialization/list.hpp> #include <boost/serialization/vector.hpp> #include <atomic> + #include "include_base_utils.h" #include "cryptonote_core/account.h" #include "cryptonote_core/account_boost_serialization.h" @@ -20,38 +21,40 @@ #include "crypto/chacha8.h" #include "crypto/hash.h" +#include "wallet_errors.h" + #define DEFAULT_TX_SPENDABLE_AGE 10 #define WALLET_RCP_CONNECTION_TIMEOUT 200000 namespace tools { - inline std::string interpret_rpc_response(bool ok, const std::string& status) + class i_wallet2_callback { - std::string err; - if (ok) - { - if (status == CORE_RPC_STATUS_BUSY) - { - err = "daemon is busy. Please try later"; - } - else if (status != CORE_RPC_STATUS_OK) - { - err = status; - } - } - else + public: + virtual void on_new_block(uint64_t height, const cryptonote::block& block) {} + virtual void on_money_received(uint64_t height, const cryptonote::transaction& tx, size_t out_index) {} + virtual void on_money_spent(uint64_t height, const cryptonote::transaction& in_tx, size_t out_index, const cryptonote::transaction& spend_tx) {} + }; + + struct tx_dust_policy + { + uint64_t dust_threshold; + bool add_to_fee; + cryptonote::account_public_address addr_for_dust; + + tx_dust_policy(uint64_t a_dust_threshold = 0, bool an_add_to_fee = true, cryptonote::account_public_address an_addr_for_dust = cryptonote::account_public_address()) + : dust_threshold(a_dust_threshold) + , add_to_fee(an_add_to_fee) + , addr_for_dust(an_addr_for_dust) { - err = "possible lost connection to daemon"; } - return err; - } - + }; class wallet2 { - wallet2(const wallet2&) : m_run(true) {}; + wallet2(const wallet2&) : m_run(true), m_callback(0) {}; public: - wallet2() : m_run(true) {}; + wallet2() : m_run(true), m_callback(0) {}; struct transfer_details { uint64_t m_block_height; @@ -63,22 +66,16 @@ namespace tools uint64_t amount() const { return m_tx.vout[m_internal_output_index].amount; } }; - typedef std::vector<transfer_details> transfer_container; - struct tx_dust_policy + struct unconfirmed_transfer_details { - uint64_t dust_threshold; - bool add_to_fee; - cryptonote::account_public_address addr_for_dust; - - tx_dust_policy(uint64_t a_dust_threshold = 0, bool an_add_to_fee = true, cryptonote::account_public_address an_addr_for_dust = cryptonote::account_public_address()) - : dust_threshold(a_dust_threshold) - , add_to_fee(an_add_to_fee) - , addr_for_dust(an_addr_for_dust) - { - } + cryptonote::transaction m_tx; + uint64_t m_change; + time_t m_sent_time; }; + typedef std::vector<transfer_details> transfer_container; + struct keys_file_data { crypto::chacha8_iv iv; @@ -90,77 +87,34 @@ namespace tools END_SERIALIZE() }; - struct fail_details - { - enum fail_reason - { - error_ok = 0, - error_not_connected, - error_daemon_is_busy, - error_rejected_by_daemon, - error_too_big_transaction, - error_not_enough_money, - error_too_big_mixin, - error_to_parse_block, - error_to_parse_tx, - error_to_parse_tx_extra, - error_invalid_tx, - error_internal_error - }; - fail_reason reason; - uint64_t tx_blob_size; - uint64_t max_expected_tx_blob_size; - - std::string what() const - { - switch (reason) - { - case error_ok: return "OK"; - case error_not_connected: return "not connected"; - case error_daemon_is_busy: return "daemon is busy. Please try later"; - case error_rejected_by_daemon: return "rejected by daemon"; - case error_too_big_transaction: return "transaction size is too big"; - case error_not_enough_money: return "not enough money"; - case error_too_big_mixin: return "not enough outputs for specified mixin_count"; - case error_to_parse_block: return "failed to parse/validate block"; - case error_to_parse_tx: return "failed to parse/validate tx"; - case error_to_parse_tx_extra: return "failed to parse/validate tx extra"; - case error_invalid_tx: return "wrong tx"; - case error_internal_error: return "internal error"; - default: return "unknown error"; - } - } - }; - - bool generate(const std::string& wallet, const std::string& password); - bool load(const std::string& wallet, const std::string& password); - bool store(); + void generate(const std::string& wallet, const std::string& password); + void load(const std::string& wallet, const std::string& password); + void store(); cryptonote::account_base& get_account(){return m_account;} - bool init(const std::string& daemon_address = "http://localhost:8080", uint64_t upper_transaction_size_limit = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE*2 - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE); - - bool refresh(fail_details& fd); - bool refresh(size_t & blocks_fetched, fail_details& fd); - bool refresh(size_t & blocks_fetched, bool& received_money, fail_details& fd); + void init(const std::string& daemon_address = "http://localhost:8080", uint64_t upper_transaction_size_limit = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE*2 - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE); bool deinit(); void stop() { m_run.store(false, std::memory_order_relaxed); } + i_wallet2_callback* callback() const { return m_callback; } + void callback(i_wallet2_callback* callback) { m_callback = callback; } + + void refresh(); + void refresh(size_t & blocks_fetched); + void refresh(size_t & blocks_fetched, bool& received_money); + bool refresh(size_t & blocks_fetched, bool& received_money, bool& ok); + uint64_t balance(); uint64_t unlocked_balance(); template<typename T> - bool enum_incoming_transfers(const T& handler) const; - template<typename T> - bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy); + void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy); template<typename T> - bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx, fail_details& tfd); - template<typename T> - bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx); - bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee); - bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx); - bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx, fail_details& tfd); + void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx); + void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee); + void transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx); bool check_connection(); - bool get_transfers(wallet2::transfer_container& incoming_transfers); + void get_transfers(wallet2::transfer_container& incoming_transfers) const; uint64_t get_blockchain_current_height() const { return m_local_bc_height; } template <class t_archive> inline void serialize(t_archive &a, const unsigned int ver) @@ -171,21 +125,26 @@ namespace tools a & m_transfers; a & m_account_public_address; a & m_key_images; + if(ver < 6) + return; + a & m_unconfirmed_txs; } private: bool store_keys(const std::string& keys_file_name, const std::string& password); - bool load_keys(const std::string& keys_file_name, const std::string& password); - bool process_new_transaction(cryptonote::transaction& tx, uint64_t height, fail_details& fd); - bool process_new_blockchain_entry(cryptonote::block& b, cryptonote::block_complete_entry& bche, crypto::hash& bl_id, uint64_t height, fail_details& fd); - bool detach_blockchain(uint64_t height); - bool get_short_chain_history(std::list<crypto::hash>& ids); + void load_keys(const std::string& keys_file_name, const std::string& password); + void process_new_transaction(const cryptonote::transaction& tx, uint64_t height); + void process_new_blockchain_entry(const cryptonote::block& b, cryptonote::block_complete_entry& bche, crypto::hash& bl_id, uint64_t height); + void detach_blockchain(uint64_t height); + void get_short_chain_history(std::list<crypto::hash>& ids); bool is_tx_spendtime_unlocked(uint64_t unlock_time) const; bool is_transfer_unlocked(const transfer_details& td) const; bool clear(); - bool pull_blocks(size_t& blocks_added, fail_details& fd); + void pull_blocks(size_t& blocks_added); uint64_t select_transfers(uint64_t needed_money, bool add_dust, uint64_t dust, std::list<transfer_container::iterator>& selected_transfers); bool prepare_file_names(const std::string& file_path); + void process_unconfirmed(const cryptonote::transaction& tx); + void add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t change_amount); cryptonote::account_base m_account; std::string m_daemon_address; @@ -194,6 +153,7 @@ namespace tools epee::net_utils::http::http_simple_client m_http_client; std::vector<crypto::hash> m_blockchain; std::atomic<uint64_t> m_local_bc_height; //temporary workaround + std::unordered_map<crypto::hash, unconfirmed_transfer_details> m_unconfirmed_txs; transfer_container m_transfers; std::unordered_map<crypto::key_image, size_t> m_key_images; @@ -201,9 +161,11 @@ namespace tools uint64_t m_upper_transaction_size_limit; //TODO: auto-calc this value or request from daemon, now use some fixed value std::atomic<bool> m_run; + + i_wallet2_callback* m_callback; }; } -BOOST_CLASS_VERSION(tools::wallet2, 5) +BOOST_CLASS_VERSION(tools::wallet2, 6) namespace boost { @@ -219,6 +181,16 @@ namespace boost a & x.m_spent; a & x.m_key_image; } + + template <class Archive> + inline void serialize(Archive &a, tools::wallet2::unconfirmed_transfer_details &x, const boost::serialization::version_type ver) + { + a & x.m_change; + a & x.m_sent_time; + a & x.m_tx; + } + + } } @@ -288,62 +260,31 @@ namespace tools } //---------------------------------------------------------------------------------------------------- template<typename T> - bool wallet2::enum_incoming_transfers(const T& handler) const - { - if(!m_transfers.empty()) - { - BOOST_FOREACH(const transfer_details& td, m_transfers) - { - handler(td.m_tx, td.m_global_output_index, td.amount(), td.m_spent); - } - return true; - } - else - { - return false; - } - } - //---------------------------------------------------------------------------------------------------- - template<typename T> - bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, + void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy) { cryptonote::transaction tx; - return transfer(dsts, fake_outputs_count, unlock_time, fee, destination_split_strategy, dust_policy, tx); + transfer(dsts, fake_outputs_count, unlock_time, fee, destination_split_strategy, dust_policy, tx); } template<typename T> - bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, + void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx) { - fail_details stub = AUTO_VAL_INIT(stub); - return transfer(dsts, fake_outputs_count, unlock_time, fee, destination_split_strategy, dust_policy, tx, stub); - } - - template<typename T> - bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, - uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx, fail_details& tfd) - { using namespace cryptonote; uint64_t needed_money = fee; BOOST_FOREACH(auto& dt, dsts) { - CHECK_AND_ASSERT_MES(dt.amount > 0, false, "Wrong destination amount value: " << dt.amount); + CHECK_AND_THROW_WALLET_EX(0 == dt.amount, error::zero_destination); needed_money += dt.amount; + CHECK_AND_THROW_WALLET_EX(needed_money < dt.amount, error::tx_sum_overflow, dsts, fee); } std::list<transfer_container::iterator> selected_transfers; uint64_t found_money = select_transfers(needed_money, 0 == fake_outputs_count, dust_policy.dust_threshold, selected_transfers); + CHECK_AND_THROW_WALLET_EX(found_money < needed_money, error::not_enough_money, found_money, needed_money - fee, fee); - if(found_money < needed_money) - { - LOG_ERROR("not enough money, available only " << print_money(found_money) << ", transaction amount " << - print_money(needed_money) << " = " << print_money(needed_money - fee) << " + " << print_money(fee) << " (fee)"); - tfd.reason = fail_details::error_not_enough_money; - return false; - } - //typedef COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount outs_for_amount; typedef COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::out_entry out_entry; typedef cryptonote::tx_source_entry::output_entry tx_output_entry; @@ -354,41 +295,30 @@ namespace tools req.outs_count = fake_outputs_count + 1;// add one to make possible (if need) to skip real output key BOOST_FOREACH(transfer_container::iterator it, selected_transfers) { - CHECK_AND_ASSERT_MES(it->m_tx.vout.size() > it->m_internal_output_index, false, "internal error: m_internal_output_index = " - << it->m_internal_output_index << " more than " << it->m_tx.vout.size()); + CHECK_AND_THROW_WALLET_EX(it->m_tx.vout.size() <= it->m_internal_output_index, error::wallet_internal_error, + "m_internal_output_index = " + std::to_string(it->m_internal_output_index) + + " is greater or equal to outputs count = " + std::to_string(it->m_tx.vout.size())); req.amounts.push_back(it->amount()); } bool r = net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/getrandom_outs.bin", req, daemon_resp, m_http_client, 200000); - if (!r) tfd.reason = fail_details::error_not_connected; - else if (CORE_RPC_STATUS_BUSY == daemon_resp.status) tfd.reason = fail_details::error_daemon_is_busy; - else if (CORE_RPC_STATUS_OK != daemon_resp.status) tfd.reason = fail_details::error_internal_error; - else tfd.reason = fail_details::error_ok; - if (fail_details::error_ok != tfd.reason) - { - LOG_PRINT_L0("failed to invoke getrandom_outs.bin: " << interpret_rpc_response(r, daemon_resp.status)); - return false; - } - - tfd.reason = fail_details::error_internal_error; - CHECK_AND_ASSERT_MES(daemon_resp.outs.size() == selected_transfers.size(), false, - "internal error: daemon returned wrong response for getrandom_outs.bin, wrong amounts count = " - << daemon_resp.outs.size() << ", expected " << selected_transfers.size()); - - tfd.reason = fail_details::error_ok; + CHECK_AND_THROW_WALLET_EX(!r, error::no_connection_to_daemon, "getrandom_outs.bin"); + CHECK_AND_THROW_WALLET_EX(daemon_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "getrandom_outs.bin"); + CHECK_AND_THROW_WALLET_EX(daemon_resp.status != CORE_RPC_STATUS_OK, error::get_random_outs_error, daemon_resp.status); + CHECK_AND_THROW_WALLET_EX(daemon_resp.outs.size() != selected_transfers.size(), error::wallet_internal_error, + "daemon returned wrong response for getrandom_outs.bin, wrong amounts count = " + + std::to_string(daemon_resp.outs.size()) + ", expected " + std::to_string(selected_transfers.size())); + + std::vector<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount> scanty_outs; BOOST_FOREACH(COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& amount_outs, daemon_resp.outs) { - if (amount_outs.outs.size() != fake_outputs_count) + if (amount_outs.outs.size() < fake_outputs_count) { - tfd.reason = fail_details::error_too_big_mixin; - LOG_PRINT_L0("not enough outputs to mix output " << print_money(amount_outs.amount) << ", requested " << - fake_outputs_count << ", found " << amount_outs.outs.size()); + scanty_outs.push_back(amount_outs); } } - if (fail_details::error_ok != tfd.reason) - return false; + CHECK_AND_THROW_WALLET_EX(!scanty_outs.empty(), error::not_enough_outs_to_mix, scanty_outs, fake_outputs_count); } - tfd.reason = fail_details::error_ok; //prepare inputs size_t i = 0; @@ -443,60 +373,36 @@ namespace tools uint64_t dust = 0; std::vector<cryptonote::tx_destination_entry> splitted_dsts; destination_split_strategy(dsts, change_dts, dust_policy.dust_threshold, splitted_dsts, dust); - CHECK_AND_ASSERT_MES(dust <= dust_policy.dust_threshold, false, "internal error: invalid dust value"); + CHECK_AND_THROW_WALLET_EX(dust_policy.dust_threshold < dust, error::wallet_internal_error, "invalid dust value: dust = " + + std::to_string(dust) + ", dust_threshold = " + std::to_string(dust_policy.dust_threshold)); if (0 != dust && !dust_policy.add_to_fee) { splitted_dsts.push_back(cryptonote::tx_destination_entry(dust, dust_policy.addr_for_dust)); } - tfd.reason = fail_details::error_internal_error; bool r = cryptonote::construct_tx(m_account.get_keys(), sources, splitted_dsts, tx, unlock_time); - CHECK_AND_ASSERT_MES(r, false, "Transaction construction failed"); + CHECK_AND_THROW_WALLET_EX(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time); + CHECK_AND_THROW_WALLET_EX(m_upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, m_upper_transaction_size_limit); - //check transaction size - if(get_object_blobsize(tx) >= m_upper_transaction_size_limit) + std::string key_images; + bool all_are_txin_to_key = std::all_of(tx.vin.begin(), tx.vin.end(), [&](const txin_v& s_e) -> bool { - LOG_PRINT_L0("Transaction size is too big: " << get_object_blobsize(tx) << ", expected size < " << m_upper_transaction_size_limit); - tfd.reason = fail_details::error_too_big_transaction; - tfd.tx_blob_size = get_object_blobsize(tx); - tfd.max_expected_tx_blob_size = m_upper_transaction_size_limit; - return false; - } + CHECKED_GET_SPECIFIC_VARIANT(s_e, const txin_to_key, in, false); + key_images += boost::to_string(in.k_image) + " "; + return true; + }); + CHECK_AND_THROW_WALLET_EX(!all_are_txin_to_key, error::unexpected_txin_type, tx); COMMAND_RPC_SEND_RAW_TX::request req; req.tx_as_hex = epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(tx)); COMMAND_RPC_SEND_RAW_TX::response daemon_send_resp; r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/sendrawtransaction", req, daemon_send_resp, m_http_client, 200000); - if (!r) - { - tfd.reason = fail_details::error_not_connected; - LOG_PRINT_L0("failed to send transaction: " << interpret_rpc_response(r, daemon_send_resp.status)); - return false; - } - else if (CORE_RPC_STATUS_BUSY == daemon_send_resp.status) - { - tfd.reason = fail_details::error_daemon_is_busy; - LOG_PRINT_L0("failed to send transaction: " << interpret_rpc_response(r, daemon_send_resp.status)); - return false; - } - else if (CORE_RPC_STATUS_OK != daemon_send_resp.status) - { - tfd.reason = fail_details::error_rejected_by_daemon; - LOG_ERROR("daemon failed to accept generated transaction, id: " << get_transaction_hash(tx)); - return false; - } - else - { - tfd.reason = fail_details::error_ok; - } + CHECK_AND_THROW_WALLET_EX(!r, error::no_connection_to_daemon, "sendrawtransaction"); + CHECK_AND_THROW_WALLET_EX(daemon_send_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "sendrawtransaction"); + CHECK_AND_THROW_WALLET_EX(daemon_send_resp.status != CORE_RPC_STATUS_OK, error::tx_rejected, tx, daemon_send_resp.status); + + add_unconfirmed_tx(tx, change_dts.amount); - std::string key_images; - std::for_each(tx.vin.begin(), tx.vin.end(), [&](const txin_v& s_e) -> bool - { - CHECKED_GET_SPECIFIC_VARIANT(s_e, const txin_to_key, in, false); - key_images += boost::to_string(in.k_image) + " "; - return true; - }); LOG_PRINT_L2("transaction " << get_transaction_hash(tx) << " generated ok and sent to daemon, key_images: [" << key_images << "]"); BOOST_FOREACH(transfer_container::iterator it, selected_transfers) @@ -507,8 +413,5 @@ namespace tools << "Balance: " << print_money(balance()) << ENDL << "Unlocked: " << print_money(unlocked_balance()) << ENDL << "Please, wait for confirmation for your balance to be unlocked."); - - tfd.reason = fail_details::error_ok; - return true; } } diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h new file mode 100644 index 000000000..b517d64f9 --- /dev/null +++ b/src/wallet/wallet_errors.h @@ -0,0 +1,611 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once + +#include <stdexcept> +#include <string> +#include <vector> + +#include "cryptonote_core/cryptonote_format_utils.h" +#include "rpc/core_rpc_server_commands_defs.h" +#include "include_base_utils.h" + + +namespace tools +{ + namespace error + { + // std::exception + // std::runtime_error + // wallet_runtime_error * + // wallet_internal_error + // unexpected_txin_type + // std::logic_error + // wallet_logic_error * + // file_exists + // file_not_found + // file_read_error + // file_save_error + // invalid_password + // refresh_error * + // acc_outs_lookup_error + // block_parse_error + // get_blocks_error + // get_out_indexes_error + // tx_extra_parse_error + // tx_parse_error + // transfer_error * + // get_random_outs_general_error + // not_enough_money + // not_enough_outs_to_mix + // tx_not_constructed + // tx_rejected + // tx_sum_overflow + // tx_too_big + // zero_destination + // wallet_rpc_error * + // daemon_busy + // no_connection_to_daemon + // wallet_files_doesnt_correspond + // + // * - class with protected ctor + + //---------------------------------------------------------------------------------------------------- + template<typename Base> + struct wallet_error_base : public Base + { + const std::string& location() const { return m_loc; } + + std::string to_string() const + { + std::ostringstream ss; + ss << m_loc << ':' << typeid(*this).name() << ": " << Base::what(); + return ss.str(); + } + + protected: + wallet_error_base(std::string&& loc, const std::string& message) + : Base(message) + , m_loc(loc) + { + } + + private: + std::string m_loc; + }; + //---------------------------------------------------------------------------------------------------- + const char* const failed_rpc_request_messages[] = { + "failed to get blocks", + "failed to get out indices", + "failed to get random outs" + }; + enum failed_rpc_request_message_indices + { + get_blocks_error_message_index, + get_out_indices_error_message_index, + get_random_outs_error_message_index + }; + + template<typename Base, int msg_index> + struct failed_rpc_request : public Base + { + explicit failed_rpc_request(std::string&& loc, const std::string& status) + : Base(std::move(loc), failed_rpc_request_messages[msg_index]) + , m_status(status) + { + } + + const std::string& status() const { return m_status; } + + std::string to_string() const + { + std::ostringstream ss; + ss << Base::to_string() << ", status = " << status(); + return ss.str(); + } + + private: + std::string m_status; + }; + //---------------------------------------------------------------------------------------------------- + typedef wallet_error_base<std::logic_error> wallet_logic_error; + typedef wallet_error_base<std::runtime_error> wallet_runtime_error; + //---------------------------------------------------------------------------------------------------- + struct wallet_internal_error : public wallet_runtime_error + { + explicit wallet_internal_error(std::string&& loc, const std::string& message) + : wallet_runtime_error(std::move(loc), message) + { + } + }; + //---------------------------------------------------------------------------------------------------- + struct unexpected_txin_type : public wallet_internal_error + { + explicit unexpected_txin_type(std::string&& loc, const cryptonote::transaction& tx) + : wallet_internal_error(std::move(loc), "one of tx inputs has unexpected type") + , m_tx(tx) + { + } + + const cryptonote::transaction& tx() const { return m_tx; } + + std::string to_string() const + { + std::ostringstream ss; + cryptonote::transaction tx = m_tx; + ss << wallet_internal_error::to_string() << ", tx:\n" << cryptonote::obj_to_json_str(tx); + return ss.str(); + } + + private: + cryptonote::transaction m_tx; + }; + //---------------------------------------------------------------------------------------------------- + const char* const file_error_messages[] = { + "file already exists", + "file not found", + "failed to read file", + "failed to save file" + }; + enum file_error_message_indices + { + file_exists_message_index, + file_not_found_message_index, + file_read_error_message_index, + file_save_error_message_index + }; + + template<int msg_index> + struct file_error_base : public wallet_logic_error + { + explicit file_error_base(std::string&& loc, const std::string& file) + : wallet_logic_error(std::move(loc), std::string(file_error_messages[msg_index]) + " \"" + file + '\"') + , m_file(file) + { + } + + const std::string& file() const { return m_file; } + + std::string to_string() const { return wallet_logic_error::to_string(); } + + private: + std::string m_file; + }; + //---------------------------------------------------------------------------------------------------- + typedef file_error_base<file_exists_message_index> file_exists; + typedef file_error_base<file_not_found_message_index> file_not_found; + typedef file_error_base<file_not_found_message_index> file_not_found; + typedef file_error_base<file_read_error_message_index> file_read_error; + typedef file_error_base<file_save_error_message_index> file_save_error; + //---------------------------------------------------------------------------------------------------- + struct invalid_password : public wallet_logic_error + { + explicit invalid_password(std::string&& loc) + : wallet_logic_error(std::move(loc), "invalid password") + { + } + + std::string to_string() const { return wallet_logic_error::to_string(); } + }; + //---------------------------------------------------------------------------------------------------- + struct refresh_error : public wallet_logic_error + { + protected: + refresh_error(std::string&& loc, const std::string& message) + : wallet_logic_error(std::move(loc), message) + { + } + }; + //---------------------------------------------------------------------------------------------------- + struct acc_outs_lookup_error : public refresh_error + { + explicit acc_outs_lookup_error(std::string&& loc, const cryptonote::transaction& tx, + const crypto::public_key& tx_pub_key, const cryptonote::account_keys& acc_keys) + : refresh_error(std::move(loc), "account outs lookup error") + , m_tx(tx) + , m_tx_pub_key(tx_pub_key) + , m_acc_keys(acc_keys) + { + } + + const cryptonote::transaction& tx() const { return m_tx; } + const crypto::public_key& tx_pub_key() const { return m_tx_pub_key; } + const cryptonote::account_keys& acc_keys() const { return m_acc_keys; } + + std::string to_string() const + { + std::ostringstream ss; + cryptonote::transaction tx = m_tx; + ss << refresh_error::to_string() << ", tx: " << cryptonote::obj_to_json_str(tx); + return ss.str(); + } + + private: + const cryptonote::transaction m_tx; + const crypto::public_key m_tx_pub_key; + const cryptonote::account_keys m_acc_keys; + }; + //---------------------------------------------------------------------------------------------------- + struct block_parse_error : public refresh_error + { + explicit block_parse_error(std::string&& loc, const cryptonote::blobdata& block_data) + : refresh_error(std::move(loc), "block parse error") + , m_block_blob(block_data) + { + } + + const cryptonote::blobdata& block_blob() const { return m_block_blob; } + + std::string to_string() const { return refresh_error::to_string(); } + + private: + cryptonote::blobdata m_block_blob; + }; + //---------------------------------------------------------------------------------------------------- + typedef failed_rpc_request<refresh_error, get_blocks_error_message_index> get_blocks_error; + //---------------------------------------------------------------------------------------------------- + typedef failed_rpc_request<refresh_error, get_out_indices_error_message_index> get_out_indices_error; + //---------------------------------------------------------------------------------------------------- + struct tx_extra_parse_error : public refresh_error + { + explicit tx_extra_parse_error(std::string&& loc, const cryptonote::transaction& tx) + : refresh_error(std::move(loc), "transaction extra parse error") + , m_tx(tx) + { + } + + const cryptonote::transaction& tx() const { return m_tx; } + + std::string to_string() const + { + std::ostringstream ss; + cryptonote::transaction tx = m_tx; + ss << refresh_error::to_string() << ", tx: " << cryptonote::obj_to_json_str(tx); + return ss.str(); + } + + private: + const cryptonote::transaction m_tx; + }; + //---------------------------------------------------------------------------------------------------- + struct tx_parse_error : public refresh_error + { + explicit tx_parse_error(std::string&& loc, const cryptonote::blobdata& tx_blob) + : refresh_error(std::move(loc), "transaction parse error") + , m_tx_blob(tx_blob) + { + } + + const cryptonote::blobdata& tx_blob() const { return m_tx_blob; } + + std::string to_string() const { return refresh_error::to_string(); } + + private: + cryptonote::blobdata m_tx_blob; + }; + //---------------------------------------------------------------------------------------------------- + struct transfer_error : public wallet_logic_error + { + protected: + transfer_error(std::string&& loc, const std::string& message) + : wallet_logic_error(std::move(loc), message) + { + } + }; + //---------------------------------------------------------------------------------------------------- + typedef failed_rpc_request<transfer_error, get_random_outs_error_message_index> get_random_outs_error; + //---------------------------------------------------------------------------------------------------- + struct not_enough_money : public transfer_error + { + not_enough_money(std::string&& loc, uint64_t availbable, uint64_t tx_amount, uint64_t fee) + : transfer_error(std::move(loc), "not enough money") + , m_available(availbable) + , m_tx_amount(tx_amount) + , m_fee(fee) + { + } + + uint64_t available() const { return m_available; } + uint64_t tx_amount() const { return m_tx_amount; } + uint64_t fee() const { return m_fee; } + + std::string to_string() const + { + std::ostringstream ss; + ss << transfer_error::to_string() << + ", available = " << cryptonote::print_money(m_available) << + ", tx_amount = " << cryptonote::print_money(m_tx_amount) << + ", fee = " << cryptonote::print_money(m_fee); + return ss.str(); + } + + private: + uint64_t m_available; + uint64_t m_tx_amount; + uint64_t m_fee; + }; + //---------------------------------------------------------------------------------------------------- + struct not_enough_outs_to_mix : public transfer_error + { + typedef std::vector<cryptonote::COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount> scanty_outs_t; + + explicit not_enough_outs_to_mix(std::string&& loc, const scanty_outs_t& scanty_outs, size_t mixin_count) + : transfer_error(std::move(loc), "not enough outputs to mix") + , m_scanty_outs(scanty_outs) + , m_mixin_count(mixin_count) + { + } + + const scanty_outs_t& scanty_outs() const { return m_scanty_outs; } + size_t mixin_count() const { return m_mixin_count; } + + std::string to_string() const + { + std::ostringstream ss; + ss << transfer_error::to_string() << ", mixin_count = " << m_mixin_count << ", scanty_outs:"; + for (const auto& outs_for_amount : m_scanty_outs) + { + ss << '\n' << cryptonote::print_money(outs_for_amount.amount) << " - " << outs_for_amount.outs.size(); + } + return ss.str(); + } + + private: + scanty_outs_t m_scanty_outs; + size_t m_mixin_count; + }; + //---------------------------------------------------------------------------------------------------- + struct tx_not_constructed : public transfer_error + { + typedef std::vector<cryptonote::tx_source_entry> sources_t; + typedef std::vector<cryptonote::tx_destination_entry> destinations_t; + + explicit tx_not_constructed(std::string&& loc, const sources_t& sources, const destinations_t& destinations, uint64_t unlock_time) + : transfer_error(std::move(loc), "transaction was not constructed") + , m_sources(sources) + , m_destinations(destinations) + , m_unlock_time(unlock_time) + { + } + + const sources_t& sources() const { return m_sources; } + const destinations_t& destinations() const { return m_destinations; } + uint64_t unlock_time() const { return m_unlock_time; } + + std::string to_string() const + { + std::ostringstream ss; + ss << transfer_error::to_string(); + ss << "\nSources:"; + for (size_t i = 0; i < m_sources.size(); ++i) + { + const cryptonote::tx_source_entry& src = m_sources[i]; + ss << "\n source " << i << ":"; + ss << "\n amount: " << cryptonote::print_money(src.amount); + // It's not good, if logs will contain such much data + //ss << "\n real_output: " << src.real_output; + //ss << "\n real_output_in_tx_index: " << src.real_output_in_tx_index; + //ss << "\n real_out_tx_key: " << epee::string_tools::pod_to_hex(src.real_out_tx_key); + //ss << "\n outputs:"; + //for (size_t j = 0; j < src.outputs.size(); ++j) + //{ + // const cryptonote::tx_source_entry::output_entry& out = src.outputs[j]; + // ss << "\n " << j << ": " << out.first << ", " << epee::string_tools::pod_to_hex(out.second); + //} + } + + ss << "\nDestinations:"; + for (size_t i = 0; i < m_destinations.size(); ++i) + { + const cryptonote::tx_destination_entry& dst = m_destinations[i]; + ss << "\n " << i << ": " << cryptonote::get_account_address_as_str(dst.addr) << " " << + cryptonote::print_money(dst.amount); + } + + ss << "\nunlock_time: " << m_unlock_time; + + return ss.str(); + } + + private: + sources_t m_sources; + destinations_t m_destinations; + uint64_t m_unlock_time; + }; + //---------------------------------------------------------------------------------------------------- + struct tx_rejected : public transfer_error + { + explicit tx_rejected(std::string&& loc, const cryptonote::transaction& tx, const std::string& status) + : transfer_error(std::move(loc), "transaction was rejected by daemon") + , m_tx(tx) + , m_status(status) + { + } + + const cryptonote::transaction& tx() const { return m_tx; } + const std::string& status() const { return m_status; } + + std::string to_string() const + { + std::ostringstream ss; + ss << transfer_error::to_string() << ", status = " << m_status << ", tx:\n"; + cryptonote::transaction tx = m_tx; + ss << cryptonote::obj_to_json_str(tx); + return ss.str(); + } + + private: + cryptonote::transaction m_tx; + std::string m_status; + }; + //---------------------------------------------------------------------------------------------------- + struct tx_sum_overflow : public transfer_error + { + tx_sum_overflow(std::string&& loc, const std::vector<cryptonote::tx_destination_entry>& destinations, uint64_t fee) + : transfer_error(std::move(loc), "transaction sum + fee exceeds " + cryptonote::print_money(std::numeric_limits<uint64_t>::max())) + , m_destinations(destinations) + , m_fee(fee) + { + } + + const std::vector<cryptonote::tx_destination_entry>& destinations() const { return m_destinations; } + uint64_t fee() const { return m_fee; } + + std::string to_string() const + { + std::ostringstream ss; + ss << transfer_error::to_string() << + ", fee = " << cryptonote::print_money(m_fee) << + ", destinations:"; + for (const auto& dst : m_destinations) + { + ss << '\n' << cryptonote::print_money(dst.amount) << " -> " << cryptonote::get_account_address_as_str(dst.addr); + } + return ss.str(); + } + + private: + std::vector<cryptonote::tx_destination_entry> m_destinations; + uint64_t m_fee; + }; + //---------------------------------------------------------------------------------------------------- + struct tx_too_big : public transfer_error + { + explicit tx_too_big(std::string&& loc, const cryptonote::transaction& tx, uint64_t tx_size_limit) + : transfer_error(std::move(loc), "transaction is too big") + , m_tx(tx) + , m_tx_size_limit(tx_size_limit) + { + } + + const cryptonote::transaction& tx() const { return m_tx; } + uint64_t tx_size_limit() const { return m_tx_size_limit; } + + std::string to_string() const + { + std::ostringstream ss; + cryptonote::transaction tx = m_tx; + ss << transfer_error::to_string() << + ", tx_size_limit = " << m_tx_size_limit << + ", tx size = " << get_object_blobsize(m_tx) << + ", tx:\n" << cryptonote::obj_to_json_str(tx); + return ss.str(); + } + + private: + cryptonote::transaction m_tx; + uint64_t m_tx_size_limit; + }; + //---------------------------------------------------------------------------------------------------- + struct zero_destination : public transfer_error + { + explicit zero_destination(std::string&& loc) + : transfer_error(std::move(loc), "destination amount is zero") + { + } + }; + //---------------------------------------------------------------------------------------------------- + struct wallet_rpc_error : public wallet_logic_error + { + const std::string& request() const { return m_request; } + + std::string to_string() const + { + std::ostringstream ss; + ss << wallet_logic_error::to_string() << ", request = " << m_request; + return ss.str(); + } + + protected: + wallet_rpc_error(std::string&& loc, const std::string& message, const std::string& request) + : wallet_logic_error(std::move(loc), message) + , m_request(request) + { + } + + private: + std::string m_request; + }; + //---------------------------------------------------------------------------------------------------- + struct daemon_busy : public wallet_rpc_error + { + explicit daemon_busy(std::string&& loc, const std::string& request) + : wallet_rpc_error(std::move(loc), "daemon is busy", request) + { + } + }; + //---------------------------------------------------------------------------------------------------- + struct no_connection_to_daemon : public wallet_rpc_error + { + explicit no_connection_to_daemon(std::string&& loc, const std::string& request) + : wallet_rpc_error(std::move(loc), "no connection to daemon", request) + { + } + }; + //---------------------------------------------------------------------------------------------------- + struct wallet_files_doesnt_correspond : public wallet_logic_error + { + explicit wallet_files_doesnt_correspond(std::string&& loc, const std::string& keys_file, const std::string& wallet_file) + : wallet_logic_error(std::move(loc), "file " + wallet_file + " does not correspond to " + keys_file) + { + } + + const std::string& keys_file() const { return m_keys_file; } + const std::string& wallet_file() const { return m_wallet_file; } + + std::string to_string() const { return wallet_logic_error::to_string(); } + + private: + std::string m_keys_file; + std::string m_wallet_file; + }; + //---------------------------------------------------------------------------------------------------- + +#if !defined(_MSC_VER) + + template<typename TException, typename... TArgs> + void throw_wallet_ex(std::string&& loc, const TArgs&... args) + { + TException e(std::move(loc), args...); + LOG_PRINT_L0(e.to_string()); + throw e; + } + +#else + #include <boost/preprocessor/repetition/enum_binary_params.hpp> + #include <boost/preprocessor/repetition/enum_params.hpp> + #include <boost/preprocessor/repetition/repeat_from_to.hpp> + + template<typename TException> + void throw_wallet_ex(std::string&& loc) + { + TException e(std::move(loc)); + LOG_PRINT_L0(e.to_string()); + throw e; + } + +#define GEN_throw_wallet_ex(z, n, data) \ + template<typename TException, BOOST_PP_ENUM_PARAMS(n, typename TArg)> \ + void throw_wallet_ex(std::string&& loc, BOOST_PP_ENUM_BINARY_PARAMS(n, const TArg, &arg)) \ + { \ + TException e(std::move(loc), BOOST_PP_ENUM_PARAMS(n, arg)); \ + LOG_PRINT_L0(e.to_string()); \ + throw e; \ + } + + BOOST_PP_REPEAT_FROM_TO(1, 6, GEN_throw_wallet_ex, ~) +#endif + } +} + +#define STRINGIZE_DETAIL(x) #x +#define STRINGIZE(x) STRINGIZE_DETAIL(x) + +#define CHECK_AND_THROW_WALLET_EX(cond, err_type, ...) \ + if (cond) \ + { \ + LOG_ERROR(#cond << ". THROW EXCEPTION: " << #err_type); \ + tools::error::throw_wallet_ex<err_type>(std::string(__FILE__ ":" STRINGIZE(__LINE__)), ## __VA_ARGS__); \ + } diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp new file mode 100644 index 000000000..a89cc4e72 --- /dev/null +++ b/src/wallet/wallet_rpc_server.cpp @@ -0,0 +1,132 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + + +#include "include_base_utils.h" +using namespace epee; + +#include "wallet_rpc_server.h" +#include "common/command_line.h" +#include "cryptonote_core/cryptonote_format_utils.h" +#include "cryptonote_core/account.h" +#include "misc_language.h" +#include "crypto/hash.h" + +namespace tools +{ + //----------------------------------------------------------------------------------- + const command_line::arg_descriptor<std::string> wallet_rpc_server::arg_rpc_bind_port = {"rpc-bind-port", "Starts wallet as rpc server for wallet operations, sets bind port for server", "", true}; + const command_line::arg_descriptor<std::string> wallet_rpc_server::arg_rpc_bind_ip = {"rpc-bind-ip", "Specify ip to bind rpc server", "127.0.0.1"}; + + void wallet_rpc_server::init_options(boost::program_options::options_description& desc) + { + command_line::add_arg(desc, arg_rpc_bind_ip); + command_line::add_arg(desc, arg_rpc_bind_port); + } + //------------------------------------------------------------------------------------------------------------------------------ + wallet_rpc_server::wallet_rpc_server(wallet2& w):m_wallet(w) + {} + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::run() + { + m_net_server.add_idle_handler([this](){ + m_wallet.refresh(); + return true; + }, 20000); + + //DO NOT START THIS SERVER IN MORE THEN 1 THREADS WITHOUT REFACTORING + return epee::http_server_impl_base<wallet_rpc_server, connection_context>::run(1, true); + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::handle_command_line(const boost::program_options::variables_map& vm) + { + m_bind_ip = command_line::get_arg(vm, arg_rpc_bind_ip); + m_port = command_line::get_arg(vm, arg_rpc_bind_port); + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::init(const boost::program_options::variables_map& vm) + { + m_net_server.set_threads_prefix("RPC"); + bool r = handle_command_line(vm); + CHECK_AND_ASSERT_MES(r, false, "Failed to process command line in core_rpc_server"); + return epee::http_server_impl_base<wallet_rpc_server, connection_context>::init(m_port, m_bind_ip); + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + try + { + res.balance = m_wallet.balance(); + res.unlocked_balance = m_wallet.unlocked_balance(); + } + catch (std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = e.what(); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + + std::vector<cryptonote::tx_destination_entry> dsts; + for (auto it = req.destinations.begin(); it != req.destinations.end(); it++) + { + cryptonote::tx_destination_entry de; + if(!get_account_address_from_str(de.addr, it->address)) + { + er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS; + er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + it->address; + return false; + } + de.amount = it->amount; + dsts.push_back(de); + } + try + { + cryptonote::transaction tx; + m_wallet.transfer(dsts, req.mixin, req.unlock_time, req.fee, tx); + res.tx_hash = boost::lexical_cast<std::string>(cryptonote::get_transaction_hash(tx)); + return true; + } + catch (const tools::error::daemon_busy& e) + { + er.code = WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY; + er.message = e.what(); + return false; + } + catch (const std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR; + er.message = e.what(); + return false; + } + catch (...) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR"; + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_store(const wallet_rpc::COMMAND_RPC_STORE::request& req, wallet_rpc::COMMAND_RPC_STORE::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + try + { + m_wallet.store(); + } + catch (std::exception& e) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = e.what(); + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ +}
\ No newline at end of file diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h new file mode 100644 index 000000000..418f055d9 --- /dev/null +++ b/src/wallet/wallet_rpc_server.h @@ -0,0 +1,55 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once + +#include <boost/program_options/options_description.hpp> +#include <boost/program_options/variables_map.hpp> +#include "net/http_server_impl_base.h" +#include "wallet_rpc_server_commans_defs.h" +#include "wallet2.h" +#include "common/command_line.h" +namespace tools +{ + /************************************************************************/ + /* */ + /************************************************************************/ + class wallet_rpc_server: public epee::http_server_impl_base<wallet_rpc_server> + { + public: + typedef epee::net_utils::connection_context_base connection_context; + + wallet_rpc_server(wallet2& cr); + + const static command_line::arg_descriptor<std::string> arg_rpc_bind_port; + const static command_line::arg_descriptor<std::string> arg_rpc_bind_ip; + + + static void init_options(boost::program_options::options_description& desc); + bool init(const boost::program_options::variables_map& vm); + bool run(); + private: + + CHAIN_HTTP_TO_MAP2(connection_context); //forward http requests to uri map + + BEGIN_URI_MAP2() + BEGIN_JSON_RPC_MAP("/json_rpc") + MAP_JON_RPC_WE("getbalance", on_getbalance, wallet_rpc::COMMAND_RPC_GET_BALANCE) + MAP_JON_RPC_WE("transfer", on_transfer, wallet_rpc::COMMAND_RPC_TRANSFER) + MAP_JON_RPC_WE("store", on_store, wallet_rpc::COMMAND_RPC_STORE) + END_JSON_RPC_MAP() + END_URI_MAP2() + + //json_rpc + bool on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool on_store(const wallet_rpc::COMMAND_RPC_STORE::request& req, wallet_rpc::COMMAND_RPC_STORE::response& res, epee::json_rpc::error& er, connection_context& cntx); + + bool handle_command_line(const boost::program_options::variables_map& vm); + + wallet2& m_wallet; + std::string m_port; + std::string m_bind_ip; + }; +} diff --git a/src/wallet/wallet_rpc_server_commans_defs.h b/src/wallet/wallet_rpc_server_commans_defs.h new file mode 100644 index 000000000..a94ce36b0 --- /dev/null +++ b/src/wallet/wallet_rpc_server_commans_defs.h @@ -0,0 +1,91 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once +#include "cryptonote_protocol/cryptonote_protocol_defs.h" +#include "cryptonote_core/cryptonote_basic.h" +#include "crypto/hash.h" +#include "wallet_rpc_server_error_codes.h" +namespace tools +{ +namespace wallet_rpc +{ +#define WALLET_RPC_STATUS_OK "OK" +#define WALLET_RPC_STATUS_BUSY "BUSY" + + struct COMMAND_RPC_GET_BALANCE + { + struct request + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + + struct response + { + uint64_t balance; + uint64_t unlocked_balance; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(balance) + KV_SERIALIZE(unlocked_balance) + END_KV_SERIALIZE_MAP() + }; + }; + + struct trnsfer_destination + { + uint64_t amount; + std::string address; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(amount) + KV_SERIALIZE(address) + END_KV_SERIALIZE_MAP() + }; + + struct COMMAND_RPC_TRANSFER + { + struct request + { + std::list<trnsfer_destination> destinations; + uint64_t fee; + uint64_t mixin; + uint64_t unlock_time; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(destinations) + KV_SERIALIZE(fee) + KV_SERIALIZE(mixin) + KV_SERIALIZE(unlock_time) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + std::string tx_hash; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(tx_hash) + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_STORE + { + struct request + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + +} +} + diff --git a/src/wallet/wallet_rpc_server_error_codes.h b/src/wallet/wallet_rpc_server_error_codes.h new file mode 100644 index 000000000..415abf406 --- /dev/null +++ b/src/wallet/wallet_rpc_server_error_codes.h @@ -0,0 +1,11 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once + + +#define WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR -1 +#define WALLET_RPC_ERROR_CODE_WRONG_ADDRESS -2 +#define WALLET_RPC_ERROR_CODE_DAEMON_IS_BUSY -3 +#define WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR -4 |