Skip to content

Commit

Permalink
Merge pull request #284 from loki-project/dev
Browse files Browse the repository at this point in the history
Merge dev to master
  • Loading branch information
Doy-lee committed Oct 8, 2018
2 parents ce87445 + 65a1ca0 commit 049552a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 15 deletions.
115 changes: 102 additions & 13 deletions src/simplewallet/simplewallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ namespace
return epee::string_tools::trim(buf);
}

bool input_line_and_parse_yes_no_result(char const *prompt)
{
std::string prompt_yes_no = std::string(prompt) + " (Y/Yes/N/No): ";
std::string result = input_line(prompt_yes_no);

if (std::cin.eof())
return false;

return command_line::is_yes(result);
}

boost::optional<tools::password_container> password_prompter(const char *prompt, bool verify)
{
#ifdef HAVE_READLINE
Expand Down Expand Up @@ -4820,6 +4831,7 @@ bool simple_wallet::register_service_node_main(
de.amount = amount_payable_by_operator;
dsts.push_back(de);

bool submitted_to_network = false;
try
{
// figure out what tx will be necessary
Expand Down Expand Up @@ -4923,6 +4935,7 @@ bool simple_wallet::register_service_node_main(
else
{
commit_or_save(ptx_vector, m_do_not_relay);
submitted_to_network = true;
}
}
catch (const std::exception& e)
Expand All @@ -4934,8 +4947,26 @@ bool simple_wallet::register_service_node_main(
LOG_ERROR("unknown error");
fail_msg_writer() << tr("unknown error");
}

if (submitted_to_network && !autostake)
{
success_msg_writer() << tr("Wait for transaction to be included in a block before registration is complete.\n")
<< tr("Use the print_sn command in the daemon to check the status.");
}

return true;
}

static bool prompt_autostaking_non_trusted_contributors_warning()
{
success_msg_writer(false/*color*/)
<< tr("Auto staking to a reserved service node with non-trusted contributors may lock up your loki for the staking duration "
"if they do not restake after service node expiration.")
<< tr("\n\nIf this behaviour is not desirable, please reuse the staking command without the auto command");
bool result = input_line_and_parse_yes_no_result("Accept auto staking towards a reserved service node");
return result;
}

bool simple_wallet::register_service_node(const std::vector<std::string> &args_)
{
if (m_wallet->ask_password() && !get_and_verify_password()) { return true; }
Expand Down Expand Up @@ -5022,7 +5053,6 @@ bool simple_wallet::register_service_node(const std::vector<std::string> &args_)
}

cryptonote::account_public_address address = addresses[0];

if (!m_wallet->contains_address(address))
{
fail_msg_writer() << tr("The first reserved address for this registration does not belong to this wallet.");
Expand All @@ -5038,6 +5068,13 @@ bool simple_wallet::register_service_node(const std::vector<std::string> &args_)

if (autostake)
{
bool is_open_service_node = portions_for_operator != STAKING_PORTIONS;
if (is_open_service_node || portions.size() > 1)
{
if (!prompt_autostaking_non_trusted_contributors_warning())
return true;
}

stop();
m_idle_thread.join();
success_msg_writer(false) << please_wait_to_be_included_in_block_msg;
Expand Down Expand Up @@ -5074,7 +5111,7 @@ bool simple_wallet::register_service_node(const std::vector<std::string> &args_)
//----------------------------------------------------------------------------------------------------
bool simple_wallet::stake_main(
const crypto::public_key& service_node_key,
const cryptonote::account_public_address& address,
const cryptonote::address_parse_info& parse_info,
uint32_t priority,
std::set<uint32_t>& subaddr_indices,
uint64_t amount,
Expand All @@ -5087,6 +5124,13 @@ bool simple_wallet::stake_main(
return true;
}

// sanity check address is not subaddress incase this gets called somewhere else that assumes it can be
if (parse_info.is_subaddress)
{
fail_msg_writer() << tr("Cannot stake from a subaddress");
return false;
}

uint64_t fetched_blocks;
m_wallet->refresh(0, fetched_blocks);

Expand Down Expand Up @@ -5144,23 +5188,19 @@ bool simple_wallet::stake_main(
}

const auto& snode_info = response.service_node_states.front();
bool full = false;

const uint64_t DUST = MAX_NUMBER_OF_CONTRIBUTORS;

if (amount == 0)
amount = snode_info.staking_requirement * amount_fraction;

const bool full = snode_info.contributors.size() >= MAX_NUMBER_OF_CONTRIBUTORS;
uint64_t can_contrib_total = 0;
uint64_t must_contrib_total = 0;

if (snode_info.contributors.size() < MAX_NUMBER_OF_CONTRIBUTORS)
if (!full)
{
can_contrib_total = snode_info.staking_requirement - snode_info.total_reserved;
must_contrib_total = service_nodes::get_min_node_contribution(snode_info.staking_requirement, snode_info.total_reserved);
}
else
full = true;

bool is_preexisting_contributor = false;
for (const auto& contributor : snode_info.contributors)
Expand All @@ -5169,7 +5209,7 @@ bool simple_wallet::stake_main(
if (!cryptonote::get_account_address_from_str(info, m_wallet->nettype(), contributor.address))
info.address = service_nodes::null_address;

if (info.address == address)
if (info.address == parse_info.address)
{
uint64_t max_increase_reserve = snode_info.staking_requirement - snode_info.total_reserved;
uint64_t max_increase_amount_to = contributor.reserved + max_increase_reserve;
Expand All @@ -5184,6 +5224,7 @@ bool simple_wallet::stake_main(
fail_msg_writer() << tr("This service node already has the maximum number of participants, and the specified address is not one of them");
return true;
}

if (can_contrib_total == 0)
{
if (!autostake)
Expand Down Expand Up @@ -5233,11 +5274,11 @@ bool simple_wallet::stake_main(

add_service_node_pubkey_to_tx_extra(extra, service_node_key);

add_service_node_contributor_to_tx_extra(extra, address);
add_service_node_contributor_to_tx_extra(extra, parse_info.address);

vector<cryptonote::tx_destination_entry> dsts;
cryptonote::tx_destination_entry de;
de.addr = address;
de.addr = parse_info.address;
de.is_subaddress = false;
de.amount = amount;
dsts.push_back(de);
Expand Down Expand Up @@ -5441,6 +5482,7 @@ bool simple_wallet::stake(const std::vector<std::string> &args_)
}
}

std::string const &address_str = local_args[1];
cryptonote::address_parse_info info;
if (!cryptonote::get_account_address_from_str_or_url(info, m_wallet->nettype(), local_args[1], oa_prompter))
{
Expand Down Expand Up @@ -5470,6 +5512,53 @@ bool simple_wallet::stake(const std::vector<std::string> &args_)

if (autostake)
{
{
const auto& response = m_wallet->get_service_nodes({ epee::string_tools::pod_to_hex(service_node_key) });
if (response.service_node_states.size() != 1)
{
fail_msg_writer() << tr("Could not find service node in service node list, please make sure it is registered first.");
return false;
}

const auto& snode_info = response.service_node_states.front();
bool preexisting_contributor = false;
for (const auto& contributor : snode_info.contributors)
{
preexisting_contributor = (contributor.address == address_str);
if (preexisting_contributor) break;
}

if (!preexisting_contributor)
{
// NOTE: Disallowed since there isn't a sensible way to recalculate the portions of the staker reliably
fail_msg_writer() << tr("Autostaking is disallowed for contributors who did not reserve a spot in a service node");
return false;
}

// Autostaking in reserved pools warning
if (snode_info.contributors.size() > 1 && !prompt_autostaking_non_trusted_contributors_warning())
{
return true;
}
}

if (amount_fraction == 0) // Fixed amount loki warning
{
success_msg_writer(false/*color*/) << tr("You're autostaking to a service node using a fixed amount of loki: ")
<< print_money(amount)
<< tr(".\nThe staking requirement will be different after the service node expires. Staking a fixed amount "
"may change your percentage of stake towards the service node and consequently your block reward allocation.")
<< tr("\n\nIf this behaviour is not desirable, please reuse the staking command with a percentage sign.");

if (!input_line_and_parse_yes_no_result("Accept staking with a fixed amount of loki"))
{
fail_msg_writer() << tr("Staking transaction with fixed loki specified cancelled.");
return true;
}

success_msg_writer(false/*color*/) << "\n";
}

stop();
m_idle_thread.join();
#ifndef WIN32
Expand All @@ -5486,7 +5575,7 @@ bool simple_wallet::stake(const std::vector<std::string> &args_)
boost::unique_lock<boost::mutex> lock(m_idle_mutex);
if (!m_idle_run.load(std::memory_order_relaxed))
break;
if (!stake_main(service_node_key, info.address, priority, subaddr_indices, amount, amount_fraction, autostake))
if (!stake_main(service_node_key, info, priority, subaddr_indices, amount, amount_fraction, autostake))
break;
if (!m_idle_run.load(std::memory_order_relaxed))
break;
Expand All @@ -5496,7 +5585,7 @@ bool simple_wallet::stake(const std::vector<std::string> &args_)
else
{
LOCK_IDLE_SCOPE();
stake_main(service_node_key, info.address, priority, subaddr_indices, amount, amount_fraction, autostake);
stake_main(service_node_key, info, priority, subaddr_indices, amount, amount_fraction, autostake);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/simplewallet/simplewallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ namespace cryptonote
bool version(const std::vector<std::string>& args);

bool register_service_node_main(const std::vector<std::string>& service_node_key_as_str, uint64_t expiration_timestamp, const cryptonote::account_public_address& address, uint32_t priority, const std::vector<uint64_t>& portions, const std::vector<uint8_t>& extra, std::set<uint32_t>& subaddr_indices, bool autostake);
bool stake_main(const crypto::public_key& service_node_key, const cryptonote::account_public_address& address, uint32_t priority, std::set<uint32_t>& subaddr_indices, uint64_t amount, double amount_fraction, bool autostake);
bool stake_main(const crypto::public_key& service_node_key, const cryptonote::address_parse_info& parse_info, uint32_t priority, std::set<uint32_t>& subaddr_indices, uint64_t amount, double amount_fraction, bool autostake);

uint64_t get_daemon_blockchain_height(std::string& err);
bool try_connect_to_daemon(bool silent = false, uint32_t* version = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion src/version.cpp.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define DEF_LOKI_VERSION_TAG "@VERSIONTAG@"
#define DEF_LOKI_VERSION "1.0.3"
#define DEF_LOKI_VERSION "1.0.4"
#define DEF_LOKI_RELEASE_NAME "Magic Mani"
#define DEF_LOKI_VERSION_FULL DEF_LOKI_VERSION "-" DEF_LOKI_VERSION_TAG

Expand Down

0 comments on commit 049552a

Please sign in to comment.