Skip to content

Commit 922fa05

Browse files
committed
wallet: merge GetMasternodeVinAndKeys and GetVinAndKeysFromOutput functions.
Plus clean function further.
1 parent 083fcad commit 922fa05

File tree

4 files changed

+69
-83
lines changed

4 files changed

+69
-83
lines changed

src/masternode.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ bool CMasternodeBroadcast::Create(const std::string& strService,
227227
bool fOffline,
228228
int chainHeight)
229229
{
230-
CTxIn txin;
231230
CPubKey pubKeyCollateralAddressNew;
232231
CKey keyCollateralAddressNew;
233232
CPubKey pubKeyMasternodeNew;
@@ -240,15 +239,34 @@ bool CMasternodeBroadcast::Create(const std::string& strService,
240239
return false;
241240
}
242241

242+
std::string strError;
243+
if (strTxHash.empty() || strOutputIndex.empty()) {
244+
strError = "Invalid masternode collateral hash or output index";
245+
return error("%s: %s", __func__, strError);
246+
}
247+
248+
const uint256 collateralHash = uint256S(strTxHash);
249+
int collateralOutputIndex;
250+
try {
251+
collateralOutputIndex = std::stoi(strOutputIndex.c_str());
252+
} catch (const std::exception& e) {
253+
strError = "Invalid masternode output index";
254+
return error("%s: %s on strOutputIndex", __func__, e.what());
255+
}
256+
243257
if (!CMessageSigner::GetKeysFromSecret(strKeyMasternode, keyMasternodeNew, pubKeyMasternodeNew)) {
244258
strErrorRet = strprintf("Invalid masternode key %s", strKeyMasternode);
245259
LogPrint(BCLog::MASTERNODE,"CMasternodeBroadcast::Create -- %s\n", strErrorRet);
246260
return false;
247261
}
248262

249-
std::string strError;
250263
// Use wallet-0 here. Legacy mnb creation can be removed after transition to DMN
251-
if (vpwallets.empty() || !vpwallets[0]->GetMasternodeVinAndKeys(txin, pubKeyCollateralAddressNew, keyCollateralAddressNew, strTxHash, strOutputIndex, strError)) {
264+
COutPoint collateralOut(collateralHash, collateralOutputIndex);
265+
if (vpwallets.empty() || !vpwallets[0]->GetMasternodeVinAndKeys(pubKeyCollateralAddressNew,
266+
keyCollateralAddressNew,
267+
collateralOut,
268+
true, // fValidateCollateral
269+
strError)) {
252270
strErrorRet = strError; // GetMasternodeVinAndKeys logs this error. Only returned for GUI error notification.
253271
LogPrint(BCLog::MASTERNODE,"CMasternodeBroadcast::Create -- %s\n", strprintf("Could not allocate txin %s:%s for masternode %s", strTxHash, strOutputIndex, strService));
254272
return false;
@@ -265,6 +283,7 @@ bool CMasternodeBroadcast::Create(const std::string& strService,
265283
if (!CheckDefaultPort(_service, strErrorRet, "CMasternodeBroadcast::Create"))
266284
return false;
267285

286+
CTxIn txin(collateralOut.hash, collateralOut.n);
268287
return Create(txin, _service, keyCollateralAddressNew, pubKeyCollateralAddressNew, keyMasternodeNew, pubKeyMasternodeNew, strErrorRet, mnbRet);
269288
}
270289

src/wallet/wallet.cpp

Lines changed: 42 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "coincontrol.h"
1616
#include "evo/providertx.h"
1717
#include "guiinterfaceutil.h"
18-
#include "masternode.h"
1918
#include "policy/policy.h"
2019
#include "sapling/key_io_sapling.h"
2120
#include "script/sign.h"
@@ -759,34 +758,6 @@ void CWallet::AddToSpends(const uint256& wtxid)
759758
}
760759
}
761760

762-
bool CWallet::GetVinAndKeysFromOutput(COutput out, CTxIn& txinRet, CPubKey& pubKeyRet, CKey& keyRet, bool fColdStake)
763-
{
764-
// wait for reindex and/or import to finish
765-
if (fImporting || fReindex) return false;
766-
767-
CScript pubScript;
768-
769-
txinRet = CTxIn(out.tx->GetHash(), out.i);
770-
pubScript = out.tx->tx->vout[out.i].scriptPubKey; // the inputs PubKey
771-
772-
CTxDestination address1;
773-
ExtractDestination(pubScript, address1, fColdStake);
774-
775-
const CKeyID* keyID = boost::get<CKeyID>(&address1);
776-
if (!keyID) {
777-
LogPrintf("CWallet::GetVinAndKeysFromOutput -- Address does not refer to a key\n");
778-
return false;
779-
}
780-
781-
if (!GetKey(*keyID, keyRet)) {
782-
LogPrintf("CWallet::GetVinAndKeysFromOutput -- Private key for address is not known\n");
783-
return false;
784-
}
785-
786-
pubKeyRet = keyRet.GetPubKey();
787-
return true;
788-
}
789-
790761
bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
791762
{
792763
if (IsCrypted())
@@ -2407,82 +2378,78 @@ static bool CheckTXAvailability(const CWalletTx* pcoin,
24072378
return CheckTXAvailabilityInternal(pcoin, fOnlySafe, nDepth, safeTx);
24082379
}
24092380

2410-
bool CWallet::GetMasternodeVinAndKeys(CTxIn& txinRet, CPubKey& pubKeyRet, CKey& keyRet, std::string strTxHash, std::string strOutputIndex, std::string& strError)
2381+
bool CWallet::GetMasternodeVinAndKeys(CPubKey& pubKeyRet,
2382+
CKey& keyRet,
2383+
const COutPoint& collateralOut,
2384+
bool fValidateCollateral,
2385+
std::string& strError)
24112386
{
24122387
// wait for reindex and/or import to finish
24132388
if (fImporting || fReindex) return false;
24142389

2415-
if (strTxHash.empty() || strOutputIndex.empty()) {
2416-
strError = "Invalid masternode collateral hash or output index";
2417-
return error("%s: %s", __func__, strError);
2418-
}
2419-
2420-
int nOutputIndex;
2421-
try {
2422-
nOutputIndex = std::stoi(strOutputIndex.c_str());
2423-
} catch (const std::exception& e) {
2424-
strError = "Invalid masternode output index";
2425-
return error("%s: %s on strOutputIndex", __func__, e.what());
2426-
}
2427-
24282390
// Find specific vin
2429-
const uint256 txHash = uint256S(strTxHash);
2430-
const CWalletTx* wtx = GetWalletTx(txHash);
2391+
const CWalletTx* wtx = GetWalletTx(collateralOut.hash);
24312392
if (!wtx) {
24322393
strError = "collateral tx not found in the wallet";
24332394
return error("%s: %s", __func__, strError);
24342395
}
24352396

24362397
// Verify index limits
2437-
if (nOutputIndex < 0 || nOutputIndex >= (int) wtx->tx->vout.size()) {
2398+
if (collateralOut.n < 0 || collateralOut.n >= (uint32_t) wtx->tx->vout.size()) {
24382399
strError = "Invalid masternode output index";
2439-
return error("%s: output index %d not found in %s", __func__, nOutputIndex, strTxHash);
2400+
return error("%s: output index %d not found in %s", __func__, collateralOut.n, collateralOut.hash.GetHex());
24402401
}
24412402

2442-
CTxOut txOut = wtx->tx->vout[nOutputIndex];
2403+
CTxOut txOut = wtx->tx->vout[collateralOut.n];
24432404

24442405
// Masternode collateral value
24452406
const auto& consensus = Params().GetConsensus();
24462407
if (txOut.nValue != consensus.nMNCollateralAmt) {
24472408
strError = strprintf("Invalid collateral tx value, must be %s PIV", FormatMoney(Params().GetConsensus().nMNCollateralAmt));
2448-
return error("%s: tx %s, index %d not a masternode collateral", __func__, strTxHash, nOutputIndex);
2409+
return error("%s: tx %s, index %d not a masternode collateral", __func__, collateralOut.hash.GetHex(), collateralOut.n);
24492410
}
24502411

2451-
int nDepth = 0;
2452-
bool safeTx = false;
2453-
{
2454-
LOCK(cs_wallet);
2455-
// Check availability
2456-
if (!CheckTXAvailability(wtx, true, nDepth, safeTx, m_last_block_processed_height)) {
2457-
strError = "Not available collateral transaction";
2458-
return error("%s: tx %s not available", __func__, strTxHash);
2412+
if (fValidateCollateral) {
2413+
int nDepth = 0;
2414+
{
2415+
LOCK(cs_wallet);
2416+
// Check availability
2417+
bool safeTx = false;
2418+
if (!CheckTXAvailability(wtx, true, nDepth, safeTx, m_last_block_processed_height)) {
2419+
strError = "Not available collateral transaction";
2420+
return error("%s: tx %s not available", __func__, collateralOut.hash.GetHex());
2421+
}
2422+
2423+
// Skip spent coins
2424+
if (IsSpent(collateralOut.hash, collateralOut.n)) {
2425+
strError = "Error: collateral already spent";
2426+
return error("%s: tx %s already spent", __func__, collateralOut.hash.GetHex());
2427+
}
24592428
}
24602429

2461-
// Skip spent coins
2462-
if (IsSpent(txHash, nOutputIndex)) {
2463-
strError = "Error: collateral already spent";
2464-
return error("%s: tx %s already spent", __func__, strTxHash);
2430+
// Depth must be at least MASTERNODE_MIN_CONFIRMATIONS
2431+
if (nDepth < consensus.MasternodeCollateralMinConf()) {
2432+
strError = strprintf("Collateral tx must have at least %d confirmations, has %d",
2433+
consensus.MasternodeCollateralMinConf(), nDepth);
2434+
return error("%s: %s", __func__, strError);
24652435
}
24662436
}
24672437

2468-
// Depth must be at least MASTERNODE_MIN_CONFIRMATIONS
2469-
if (nDepth < consensus.MasternodeCollateralMinConf()) {
2470-
strError = strprintf("Collateral tx must have at least %d confirmations, has %d", consensus.MasternodeCollateralMinConf(), nDepth);
2471-
return error("%s: %s", __func__, strError);
2438+
CTxDestination destCollateral;
2439+
ExtractDestination(txOut.scriptPubKey, destCollateral, false);
2440+
const CKeyID* keyID = boost::get<CKeyID>(&destCollateral);
2441+
if (!keyID) {
2442+
LogPrintf("%s: Address does not refer to a key\n", __func__);
2443+
return false;
24722444
}
24732445

2474-
// utxo need to be mine.
2475-
isminetype mine = IsMine(txOut);
2476-
if (mine != ISMINE_SPENDABLE) {
2477-
strError = "Invalid collateral transaction. Not from this wallet";
2478-
return error("%s: tx %s not mine", __func__, strTxHash);
2446+
if (!GetKey(*keyID, keyRet)) {
2447+
LogPrintf("%s: Private key for address is not known\n", __func__);
2448+
return false;
24792449
}
24802450

2481-
return GetVinAndKeysFromOutput(
2482-
COutput(wtx, nOutputIndex, nDepth, true, true, true),
2483-
txinRet,
2484-
pubKeyRet,
2485-
keyRet);
2451+
pubKeyRet = keyRet.GetPubKey();
2452+
return true;
24862453
}
24872454

24882455
CWallet::OutputAvailabilityResult CWallet::CheckOutputAvailability(

src/wallet/wallet.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -851,10 +851,11 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
851851
std::map<libzcash::SaplingPaymentAddress, std::vector<SaplingNoteEntry>> ListNotes() const;
852852

853853
/// Get 10000 PIV output and keys which can be used for the Masternode
854-
bool GetMasternodeVinAndKeys(CTxIn& txinRet, CPubKey& pubKeyRet,
855-
CKey& keyRet, std::string strTxHash, std::string strOutputIndex, std::string& strError);
856-
/// Extract txin information and keys from output
857-
bool GetVinAndKeysFromOutput(COutput out, CTxIn& txinRet, CPubKey& pubKeyRet, CKey& keyRet, bool fColdStake = false);
854+
bool GetMasternodeVinAndKeys(CPubKey& pubKeyRet,
855+
CKey& keyRet,
856+
const COutPoint& collateralOut,
857+
bool fValidateCollateral,
858+
std::string& strError);
858859

859860
bool IsSpent(const COutPoint& outpoint) const;
860861
bool IsSpent(const uint256& hash, unsigned int n) const;

test/lint/lint-circular-dependencies.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ EXPECTED_CIRCULAR_DEPENDENCIES=(
1919
"evo/specialtx_validation -> validation -> evo/specialtx_validation"
2020
"kernel -> validation -> kernel"
2121
"masternode -> masternodeman -> masternode"
22-
"masternode -> wallet/wallet -> masternode"
2322
"masternode-payments -> masternodeman -> masternode-payments"
2423
"masternode-payments -> validation -> masternode-payments"
2524
"masternode-sync -> masternodeman -> masternode-sync"

0 commit comments

Comments
 (0)