Skip to content

Commit

Permalink
Allow consuming specific UTXO in gobject prepare command (#2482)
Browse files Browse the repository at this point in the history
* Implement optional CoinControl for gobject prepare

* Removes duplicate `);` from merge error

Co-Authored-By: PastaPastaPasta <[email protected]>

* Fix equality check allowing more relevant help output

* Pass COutPoint as const reference

Co-Authored-By: PastaPastaPasta <[email protected]>

* Remove unnecessary SetNull call

Co-Authored-By: PastaPastaPasta <[email protected]>

* Specify proposal fee

Co-Authored-By: PastaPastaPasta <[email protected]>
  • Loading branch information
PastaPastaPasta authored and UdjinM6 committed Dec 3, 2018
1 parent 1270b71 commit 818f0f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
22 changes: 19 additions & 3 deletions src/rpc/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ void gobject_prepare_help()
"3. time (numeric, required) time this object was created\n"
"4. data-hex (string, required) data in hex string form\n"
"5. use-IS (boolean, optional, default=false) InstantSend lock the collateral, only requiring one chain confirmation\n"
"6. outputHash (string, optional) the single output to submit the proposal fee from\n"
"7. outputIndex (numeric, optional) The output index.\n"
);
}

UniValue gobject_prepare(const JSONRPCRequest& request)
{
if (request.fHelp || (request.params.size() != 5 && request.params.size() != 6))
if (request.fHelp || (request.params.size() != 5 && request.params.size() != 6 && request.params.size() != 8))
gobject_prepare_help();

if (!EnsureWalletIsAvailable(request.fHelp))
Expand Down Expand Up @@ -199,9 +201,23 @@ UniValue gobject_prepare(const JSONRPCRequest& request)

EnsureWalletIsUnlocked();

// If specified, spend this outpoint as the proposal fee
COutPoint outpoint;
outpoint.SetNull();
if (request.params.size() == 8) {
uint256 collateralHash = ParseHashV(request.params[6], "outputHash");
int32_t collateralIndex = ParseInt32V(request.params[7], "outputIndex");
if (collateralHash.IsNull() || collateralIndex < 0) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid hash or index: %s-%d", collateralHash.ToString(), collateralIndex));
}
outpoint = COutPoint(collateralHash, (uint32_t)collateralIndex);
}

CWalletTx wtx;
if (!pwalletMain->GetBudgetSystemCollateralTX(wtx, govobj.GetHash(), govobj.GetMinCollateralFee(), useIS)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Error making collateral transaction for governance object. Please check your wallet balance and make sure your wallet is unlocked.");
if (!pwalletMain->GetBudgetSystemCollateralTX(wtx, govobj.GetHash(), govobj.GetMinCollateralFee(), useIS, outpoint)) {
std::string err = "Error making collateral transaction for governance object. Please check your wallet balance and make sure your wallet is unlocked.";
if (request.params.size() == 8) err += "Please verify your specified output is valid and is enough for the combined proposal fee and transaction fee.";
throw JSONRPCError(RPC_INTERNAL_ERROR, err);
}

// -- make our change address
Expand Down
9 changes: 6 additions & 3 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3356,7 +3356,7 @@ bool CWallet::CreateCollateralTransaction(CMutableTransaction& txCollateral, std
return true;
}

bool CWallet::GetBudgetSystemCollateralTX(CWalletTx& tx, uint256 hash, CAmount amount, bool fUseInstantSend)
bool CWallet::GetBudgetSystemCollateralTX(CWalletTx& tx, uint256 hash, CAmount amount, bool fUseInstantSend, const COutPoint& outpoint)
{
// make our change address
CReserveKey reservekey(this);
Expand All @@ -3370,8 +3370,11 @@ bool CWallet::GetBudgetSystemCollateralTX(CWalletTx& tx, uint256 hash, CAmount a
std::vector< CRecipient > vecSend;
vecSend.push_back((CRecipient){scriptChange, amount, false});

CCoinControl *coinControl=NULL;
bool success = CreateTransaction(vecSend, tx, reservekey, nFeeRet, nChangePosRet, strFail, coinControl, true, ALL_COINS, fUseInstantSend);
CCoinControl coinControl;
if (!outpoint.IsNull()) {
coinControl.Select(outpoint);
}
bool success = CreateTransaction(vecSend, tx, reservekey, nFeeRet, nChangePosRet, strFail, &coinControl, true, ALL_COINS, fUseInstantSend);
if(!success){
LogPrintf("CWallet::GetBudgetSystemCollateralTX -- Error: %s\n", strFail);
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
CAmount GetNeedsToBeAnonymizedBalance(CAmount nMinBalance = 0) const;
CAmount GetDenominatedBalance(bool unconfirmed=false) const;

bool GetBudgetSystemCollateralTX(CWalletTx& tx, uint256 hash, CAmount amount, bool fUseInstantSend);
bool GetBudgetSystemCollateralTX(CWalletTx& tx, uint256 hash, CAmount amount, bool fUseInstantSend, const COutPoint& outpoint=COutPoint()/*defaults null*/);

/**
* Insert additional inputs into the transaction by
Expand Down

0 comments on commit 818f0f4

Please sign in to comment.