Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

researcher: Check wallet status before beacon renewal #2254

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions src/gridcoin/researcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,19 +645,14 @@ bool SignBeaconPayload(BeaconPayload& payload)
}

//!
//! \brief Send a transaction that contains a beacon contract.
//! \brief Determine whether a wallet can send a beacon transaction.
//!
//! \param cpid CPID to send a beacon for.
//! \param beacon Contains the CPID's beacon public key.
//! \param action Determines whether to add or remove a beacon.
//! \param wallet The wallet that will hold the key and pay for the beacon.
//!
//! \return A variant that contains the new public key if successful or a
//! description of the error that occurred.
//! \return An error that describes why the wallet cannot send a beacon if
//! a transaction will not succeed.
//!
AdvertiseBeaconResult SendBeaconContract(
const Cpid& cpid,
Beacon beacon,
ContractAction action = ContractAction::ADD)
BeaconError CheckBeaconTransactionViable(const CWallet& wallet)
{
if (pwalletMain->IsLocked()) {
LogPrintf("WARNING: %s: Wallet locked.", __func__);
Expand All @@ -675,6 +670,30 @@ AdvertiseBeaconResult SendBeaconContract(
return BeaconError::INSUFFICIENT_FUNDS;
}

return BeaconError::NONE;
}

//!
//! \brief Send a transaction that contains a beacon contract.
//!
//! \param cpid CPID to send a beacon for.
//! \param beacon Contains the CPID's beacon public key.
//! \param action Determines whether to add or remove a beacon.
//!
//! \return A variant that contains the new public key if successful or a
//! description of the error that occurred.
//!
AdvertiseBeaconResult SendBeaconContract(
const Cpid& cpid,
Beacon beacon,
ContractAction action = ContractAction::ADD)
{
const BeaconError error = CheckBeaconTransactionViable(*pwalletMain);

if (error != BeaconError::NONE) {
return error;
}

BeaconPayload payload(cpid, beacon);

if (!SignBeaconPayload(payload)) {
Expand Down Expand Up @@ -706,20 +725,10 @@ AdvertiseBeaconResult SendNewBeacon(const Cpid& cpid)
// transaction. Otherwise, we may create a bogus beacon key that lingers in
// the wallet:
//
if (pwalletMain->IsLocked()) {
LogPrintf("WARNING: %s: Wallet locked.", __func__);
return BeaconError::WALLET_LOCKED;
}
const BeaconError error = CheckBeaconTransactionViable(*pwalletMain);

// Ensure that the wallet contains enough coins to send a transaction for
// the contract.
//
// TODO: refactor wallet so we can determine this dynamically. For now, we
// require 1 GRC:
//
if (pwalletMain->GetBalance() < COIN) {
LogPrintf("WARNING: %s: Insufficient funds.", __func__);
return BeaconError::INSUFFICIENT_FUNDS;
if (error != BeaconError::NONE) {
return error;
}

AdvertiseBeaconResult result = GenerateBeaconKey(cpid);
Expand Down Expand Up @@ -749,6 +758,12 @@ AdvertiseBeaconResult RenewBeacon(const Cpid& cpid, const Beacon& beacon)

LogPrintf("%s: Renewing beacon for %s", __func__, cpid.ToString());

const BeaconError error = CheckBeaconTransactionViable(*pwalletMain);

if (error != BeaconError::NONE) {
return error;
}

// A participant may run the wallet on two computers, but only one computer
// holds the beacon private key. If BOINC also exists on both computers for
// the same CPID, both nodes will attempt to renew the beacon. Only allow a
Expand Down Expand Up @@ -1080,11 +1095,11 @@ void Researcher::RunRenewBeaconJob()
std::string Researcher::Email()
{
std::string email;

// If the investor mode flag is set, it should override the email setting. This is especially important now
// that the read-write settings file is populated, which overrides the settings in the config file.
if (gArgs.GetBoolArg("-investor", false)) return email;

email = gArgs.GetArg("-email", "");
boost::to_lower(email);

Expand Down