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

util, rpc, gui: Implement GetMaxInputsForConsolidationTxn() #2119

Merged
Show file tree
Hide file tree
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
5 changes: 0 additions & 5 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,3 @@ inline int GetNewbieSnapshotFixHeight()
{
return fTestNet ? 1480000 : 2197000;
}

inline unsigned int GetMinimumConnectionsRequiredForStaking()
{
return fTestNet ? 1 : 3;
}
1 change: 1 addition & 0 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "gridcoin/staking/reward.h"
#include "gridcoin/staking/status.h"
#include "gridcoin/tally.h"
#include "policy/policy.h"
#include "policy/fees.h"
#include "util.h"
#include "validation.h"
Expand Down
11 changes: 11 additions & 0 deletions src/policy/policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,14 @@ bool AreInputsStandard(const CTransaction& tx, const MapPrevTx& mapInputs)

return true;
}

unsigned int GetMinimumConnectionsRequiredForStaking()
{
return fTestNet ? 1 : 3;
}

unsigned int GetMaxInputsForConsolidationTxn()
{
return (unsigned int) 600;
}

15 changes: 15 additions & 0 deletions src/policy/policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,19 @@ bool IsStandardTx(const CTransaction& tx);
*/
bool AreInputsStandard(const CTransaction& tx, const MapPrevTx& mapInputs);

//!
//! \brief Gets the minimum number of connections required for a wallet to stake.
//!
//! \return unsigned int minimum number of connections to stake
//!
unsigned int GetMinimumConnectionsRequiredForStaking();

//!
//! \brief Gets the maximum number of inputs supported for a UTXO consolidation transaction to ensure
//! the transaction does not exceed the maximum size and fail as a result.
//!
//! \return unsigned int maximum number of consolidation inputs.
//!
unsigned int GetMaxInputsForConsolidationTxn();

#endif // BITCOIN_POLICY_POLICY_H
3 changes: 2 additions & 1 deletion src/qt/coincontroldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "bitcoinunits.h"
#include "addresstablemodel.h"
#include "optionsmodel.h"
#include "policy/policy.h"
#include "policy/fees.h"
#include "validation.h"
#include "wallet/coincontrol.h"
Expand All @@ -29,7 +30,7 @@ CCoinControl* CoinControlDialog::coinControl = new CCoinControl();

CoinControlDialog::CoinControlDialog(QWidget *parent) :
QDialog(parent),
m_inputSelectionLimit(600),
m_inputSelectionLimit(GetMaxInputsForConsolidationTxn()),
ui(new Ui::CoinControlDialog),
model(0)
{
Expand Down
79 changes: 41 additions & 38 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "gridcoin/support/block_finder.h"
#include "gridcoin/tx_message.h"
#include "gridcoin/voting/payloads.h"
#include "policy/policy.h"
#include "policy/fees.h"
#include "primitives/transaction.h"
#include "protocol.h"
Expand Down Expand Up @@ -493,50 +494,51 @@ UniValue listunspent(const UniValue& params, bool fHelp)

UniValue consolidateunspent(const UniValue& params, bool fHelp)
{
std::stringstream error_strm;

error_strm << "consolidateunspent <address> [UTXO size] [maximum number of inputs] [sweep all addresses] [sweep change]\n"
"\n"
"<address>: The Gridcoin address target for consolidation.\n"
"\n"
"[UTXO size]: Optional parameter for target consolidation output size.\n"
"\n"
"[maximum number of inputs]: Defaults and clamped to "
<< std::to_string(GetMaxInputsForConsolidationTxn())
<< " maximum to prevent transaction failures.\n"
"\n"
"[sweep all addresses]: Boolean to indicate whether all addresses should be used for inputs to the\n"
" consolidation. If true, the source of the consolidation is all addresses and\n"
" the output will be to the specified address, otherwise inputs will only be\n"
" sourced from the same address.\n"
"\n"
"[sweep change]: Boolean to indicate whether change associated with the address should be\n"
" consolidated. If [sweep all addresses] is true then this is also forced true.\n"
"\n"
"consolidateunspent performs a single transaction to consolidate UTXOs to/on a given address. The optional\n"
"parameter of UTXO size will result in consolidating UTXOs to generate the largest output possible less\n"
"than that size or the total value of the specified maximum number of smallest inputs, whichever is less.\n"
"\n"
"The script is designed to be run repeatedly and will become a no-op if the UTXO's are consolidated such\n"
"that no more meet the specified criteria. This is ideal for automated periodic scripting.\n"
"\n"
"To consolidate the entire wallet to one address do something like:\n"
"\n"
"consolidateunspent <address> <amount equal or larger than balance> 200 true repeatedly until there are\n"
"no more UTXOs to consolidate.\n"
"\n"
"In all cases the address MUST exist in your wallet beforehand. If doing this for the purpose of creating\n"
"a new smaller wallet, create a new address beforehand to serve as the target of the consolidation.\n";

if (fHelp || params.size() < 1 || params.size() > 5)
throw runtime_error(
"consolidateunspent <address> [UTXO size] [maximum number of inputs] [sweep all addresses] [sweep change]\n"
"\n"
"<address>: The Gridcoin address target for consolidation.\n"
"\n"
"[UTXO size]: Optional parameter for target consolidation output size.\n"
"\n"
"[maximum number of inputs]: Defaults to 50, clamped to 200 maximum to prevent transaction failures.\n"
"\n"
"[sweep all addresses]: Boolean to indicate whether all addresses should be used for inputs to the\n"
" consolidation. If true, the source of the consolidation is all addresses and\n"
" the output will be to the specified address, otherwise inputs will only be\n"
" sourced from the same address.\n"
"\n"
"[sweep change]: Boolean to indicate whether change associated with the address should be\n"
" consolidated. If [sweep all addresses] is true then this is also forced true.\n"
"\n"
"consolidateunspent performs a single transaction to consolidate UTXOs to/on a given address. The optional\n"
"parameter of UTXO size will result in consolidating UTXOs to generate the largest output possible less\n"
"than that size or the total value of the specified maximum number of smallest inputs, whichever is less.\n"
"\n"
"The script is designed to be run repeatedly and will become a no-op if the UTXO's are consolidated such\n"
"that no more meet the specified criteria. This is ideal for automated periodic scripting.\n"
"\n"
"To consolidate the entire wallet to one address do something like:\n"
"\n"
"consolidateunspent <address> <amount equal or larger than balance> 200 true repeatedly until there are\n"
"no more UTXOs to consolidate.\n"
"\n"
"In all cases the address MUST exist in your wallet beforehand. If doing this for the purpose of creating\n"
"a new smaller wallet, create a new address beforehand to serve as the target of the consolidation.\n");
throw runtime_error(error_strm.str());

UniValue result(UniValue::VOBJ);

std::string sAddress = params[0].get_str();
CBitcoinAddress OptimizeAddress(sAddress);

int64_t nConsolidateLimit = 0;
// Set default maximum consolidation to 50 inputs if it is not specified. This is based
// on performance tests on the Pi to ensure the transaction returns within a reasonable time.
// The performance tests on the Pi show about 3 UTXOs/second. Intel machines should do
// about 3x that. The GUI will not be responsive during the transaction due to locking.
unsigned int nInputNumberLimit = 50;
unsigned int nInputNumberLimit = GetMaxInputsForConsolidationTxn();

bool sweep_all_addresses = false;

Expand All @@ -551,8 +553,9 @@ UniValue consolidateunspent(const UniValue& params, bool fHelp)

if (params.size() > 4 && !sweep_all_addresses) sweep_change = params[4].get_bool();

// Clamp InputNumberLimit to 200. Above 200 risks an invalid transaction due to the size.
nInputNumberLimit = std::min<unsigned int>(nInputNumberLimit, 200);
// Clamp InputNumberLimit to GetMaxInputsForConsolidationTxn(). Above that number of inputs risks an invalid transaction
// due to the size.
nInputNumberLimit = std::min<unsigned int>(nInputNumberLimit, GetMaxInputsForConsolidationTxn());

if (!OptimizeAddress.IsValid())
{
Expand Down