diff --git a/src/masternode/masternode-payments.cpp b/src/masternode/masternode-payments.cpp index d656027..0ab3c8b 100755 --- a/src/masternode/masternode-payments.cpp +++ b/src/masternode/masternode-payments.cpp @@ -135,7 +135,7 @@ bool CMasternodePayments::CanVote(COutPoint outMasternode, int nBlockHeight) return true; } -void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool hasMNPayment) +void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool &hasMNPayment) { CBlockIndex* pindexPrev = ::ChainActive().Tip(); if(!pindexPrev) return; @@ -160,12 +160,12 @@ std::string GetRequiredPaymentsString(int nBlockHeight) } } -void CMasternodePayments::FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool hasPayment) +void CMasternodePayments::FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool &hasMNPayment) { CBlockIndex* pindexPrev = ::ChainActive().Tip(); if(!pindexPrev) return; - //bool hasPayment = true; + bool hasPayment = true; CScript payee; //spork @@ -206,6 +206,7 @@ void CMasternodePayments::FillBlockPayee(CMutableTransaction& txNew, int64_t nFe LogPrint(BCLog::NET, "Masternode payment to %s\n", EncodeDestination(address1)); } + hasMNPayment = hasPayment; } int CMasternodePayments::GetMinMasternodePaymentsProto() { diff --git a/src/masternode/masternode-payments.h b/src/masternode/masternode-payments.h index 060ee13..50af615 100755 --- a/src/masternode/masternode-payments.h +++ b/src/masternode/masternode-payments.h @@ -30,7 +30,7 @@ bool IsReferenceNode(CTxIn& vin); bool IsBlockPayeeValid(const CAmount& nValueCreated, const CTransaction& txNew, int nBlockHeight, const uint32_t& nTime, const uint32_t& nTimePrevBlock); std::string GetRequiredPaymentsString(int nBlockHeight); bool IsBlockValueValid(const CBlock& block, int64_t nExpectedValue); -void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool hasMNPayment); +void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool &hasMNPayment); class CMasternodePayee { @@ -224,7 +224,7 @@ class CMasternodePayments int GetMinMasternodePaymentsProto(); void ProcessMessageMasternodePayments(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv); std::string GetRequiredPaymentsString(int nBlockHeight); - void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool hasMNPayment); + void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool &hasMNPayment); std::string ToString() const; int GetOldestBlock(); int GetNewestBlock(); diff --git a/src/miner.cpp b/src/miner.cpp index 8eacd42..57e6bcd 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -204,7 +204,7 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc // Masternode and general budget payments if (IsSporkActive(SPORK_4_ENABLE_MASTERNODE_PAYMENTS) || Params().NetworkIDString() == CBaseChainParams::TESTNET) { - bool hasMNPayment =true; + bool hasMNPayment = true; FillBlockPayee(coinbaseTx, nFees, hasMNPayment); SNFillBlockPayee(coinbaseTx, nFees, hasMNPayment); } diff --git a/src/systemnode/systemnode-payments.cpp b/src/systemnode/systemnode-payments.cpp index 302fb61..f140c3f 100755 --- a/src/systemnode/systemnode-payments.cpp +++ b/src/systemnode/systemnode-payments.cpp @@ -178,7 +178,7 @@ bool CSystemnodePayments::CanVote(COutPoint outSystemnode, int nBlockHeight) return true; } -void SNFillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool hasMNPayment) +void SNFillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool &hasMNPayment) { CBlockIndex* pindexPrev = ::ChainActive().Tip(); if (!pindexPrev) @@ -204,7 +204,7 @@ std::string SNGetRequiredPaymentsString(int nBlockHeight) } } -void CSystemnodePayments::FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool hasMNPayment) +void CSystemnodePayments::FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool &hasMNPayment) { CBlockIndex* pindexPrev = ::ChainActive().Tip(); if(!pindexPrev) return; diff --git a/src/systemnode/systemnode-payments.h b/src/systemnode/systemnode-payments.h index 7469265..7cb7a74 100755 --- a/src/systemnode/systemnode-payments.h +++ b/src/systemnode/systemnode-payments.h @@ -25,7 +25,7 @@ extern CSystemnodePayments systemnodePayments; #define SNPAYMENTS_SIGNATURES_TOTAL 10 #define SN_PMT_SLOT 2 -void SNFillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool hasMNPayment); +void SNFillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool &hasMNPayment); bool SNIsBlockPayeeValid(const CAmount& nValueCreated, const CTransaction& txNew, int nBlockHeight, const uint32_t& nTime, const uint32_t& nTimePrevBlock); std::string SNGetRequiredPaymentsString(int nBlockHeight); @@ -216,7 +216,7 @@ class CSystemnodePayments bool IsScheduled(CSystemnode& sn, int nNotBlockHeight); bool CanVote(COutPoint outSystemnode, int nBlockHeight); std::string GetRequiredPaymentsString(int nBlockHeight); - void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool hasMNPayment); + void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees, bool &hasMNPayment); std::string ToString() const; SERIALIZE_METHODS(CSystemnodePayments, obj) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 1367b64..1fcde50 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3281,7 +3281,7 @@ bool CWallet::CreateContract(CContract& contract, CTransactionRef& tx, std::stri cctl.m_avoid_address_reuse = false; cctl.m_min_depth = 1; cctl.m_max_depth = 9999999; - AvailableCoins(vecOutputs, false, &cctl, 0, nAmount, MAX_MONEY, 0); + AvailableCoins(vecOutputs, false, &cctl, 0, MAX_MONEY, MAX_MONEY, 0); } for (const COutput& out : vecOutputs) { @@ -3320,7 +3320,7 @@ bool CWallet::CreateContract(CContract& contract, CTransactionRef& tx, std::stri mapValue_t mapValue; - //coin_control.m_add_inputs = false; + coin_control.m_add_inputs = false; std::vector recipients; CRecipient recipient = {Params().GetConsensus().mandatory_coinbase_destination, nAmount, 0, asset, CAsset(), false, false}; @@ -3406,19 +3406,64 @@ bool CWallet::CreateAsset(CAsset& asset, CTransactionRef& tx, std::string& asset CAsset assetNew = CAsset(meta); - std::vector recipients; - - CRecipient recipient = {GetScriptForDestination(DecodeDestination(contract.sIssuingaddress)), outputamt/100, outputamt, Params().GetConsensus().subsidy_asset, assetNew, false, true}; - recipients.push_back(recipient); - // Send CAmount nFeeRequired; int nChangePosRet = -1; bilingual_str error; FeeCalculation fee_calc_out; CCoinControl coin_control; + CTxDestination dest = DecodeDestination(contract.sIssuingaddress); + + std::vector vecOutputs; + { + CCoinControl cctl; + cctl.m_avoid_address_reuse = false; + cctl.m_min_depth = 1; + cctl.m_max_depth = 9999999; + AvailableCoins(vecOutputs, false, &cctl, 0, MAX_MONEY, MAX_MONEY, 0); + } + + for (const COutput& out : vecOutputs) { + CTxDestination address; + const CScript& scriptPubKey = (out.tx->tx->nVersion >= TX_ELE_VERSION ? out.tx->tx->vpout[out.i].scriptPubKey : out.tx->tx->vout[out.i].scriptPubKey) ; + bool fValidAddress = ExtractDestination(scriptPubKey, address); + + if(std::get(dest) != std::get(address)) + continue; + + if (!fValidAddress) + continue; + + // Elements + CAmount amount = (out.tx->tx->nVersion >= TX_ELE_VERSION ? out.tx->tx->vpout[out.i].nValue : out.tx->tx->vout[out.i].nValue) ; + CAsset assetid; + if(out.tx->tx->nVersion >= TX_ELE_VERSION) + assetid = out.tx->tx->vpout[out.i].nAsset; + + if ((amount < 0 || assetid.IsNull())) { + WalletLogPrintf("Bad amount or asset: %s:%d\n", out.tx->tx->GetHash().GetHex(), out.i); + continue; + } + + if (assetid != Params().GetConsensus().subsidy_asset) { + continue; + } + + coin_control.Select(COutPoint(out.tx->GetHash(), out.i)); + } + + if(coin_control.setSelected.size() < 1){ + strFailReason ="No suitable output found"; + return false; + } + mapValue_t mapValue; + coin_control.m_add_inputs = false; + std::vector recipients; + + CRecipient recipient = {GetScriptForDestination(dest), inputamt, outputamt, Params().GetConsensus().subsidy_asset, assetNew, false, true}; + recipients.push_back(recipient); bool fCreated = CreateTransaction(recipients, tx, nFeeRequired, nChangePosRet, error, coin_control, fee_calc_out, !IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)); if (!fCreated) {