Skip to content

Commit

Permalink
Merge pull request #714 from peercoin/develop
Browse files Browse the repository at this point in the history
0.12.6
  • Loading branch information
backpacker69 committed Sep 3, 2023
2 parents b1ab424 + 068fe9f commit 26bd4c3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ define(_CLIENT_VERSION_IS_RELEASE, true)
define(_COPYRIGHT_YEAR, 2023)
define(_PEERCOIN_VERSION_MAJOR, 0)
define(_PEERCOIN_VERSION_MINOR, 12)
define(_PEERCOIN_VERSION_REVISION, 5)
define(_PEERCOIN_VERSION_REVISION, 6)
define(_PEERCOIN_VERSION_BUILD, 0)
define(_PEERCOIN_VERSION_RC, 0)
define(_COPYRIGHT_HOLDERS,[The %s developers])
Expand Down
2 changes: 1 addition & 1 deletion src/node/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ void static ThreadStakeMinter(std::shared_ptr<CWallet> pwallet, NodeContext& m_n
// peercoin: stake minter
void MintStake(std::shared_ptr<CWallet> pwallet, NodeContext& m_node)
{
if (!pwallet->GetKeyPoolSize()) {
if (!WITH_LOCK(pwallet->cs_wallet, return pwallet->GetKeyPoolSize())) {
LogPrintf("Error: Keypool is empty, please make sure the wallet contains keys and call keypoolrefill before restarting the mining thread\n");
return;
}
Expand Down
55 changes: 42 additions & 13 deletions src/wallet/rpc/spend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,22 @@ RPCHelpMan optimizeutxoset()

EnsureWalletIsUnlocked(*pwallet);
CAmount availableCoins = 0;

mapValue_t mapValue;
CCoinControl coin_control;
const std::string address = request.params[0].get_str();
CTxDestination dest = DecodeDestination(address);

if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Peercoin address: ") + address);
}

coin_control.destChange = dest;
CAmount amount = AmountFromValue(request.params[1]);

if (amount < MIN_TXOUT_AMOUNT) {
throw JSONRPCError(RPC_INSUFFICIENT_SEND_AMOUNT, std::string("Output amount too small ") + request.params[1].get_str());
}

std::vector<CRecipient> recipients;
const bool transmit{request.params[2].isNull() ? false : request.params[2].get_bool()};
CAmount nFeeRequired = 0;
Expand All @@ -228,20 +239,26 @@ RPCHelpMan optimizeutxoset()
CAmount fee_calc_out;

if (request.params[3].isNull() == false) {
CTxDestination tmpAddress, fromAddress;
fromAddress = DecodeDestination(request.params[3].get_str());

if (!IsValidDestination(fromAddress)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Peercoin address: ") + request.params[3].get_str());
}

std::vector<COutput> vAvailableCoins;
AvailableCoins(*pwallet, vAvailableCoins, &coin_control);
CTxDestination tmpAddress, fromAddress, toAddress;
fromAddress = DecodeDestination(request.params[3].get_str());
toAddress = DecodeDestination(address);

for (const COutput& out : vAvailableCoins) {
ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, tmpAddress);
if (tmpAddress == fromAddress) {
if (toAddress == tmpAddress && out.tx->tx->vout[out.i].nValue == amount)
if (tmpAddress == dest && out.tx->tx->vout[out.i].nValue == amount)
continue;
coin_control.Select(COutPoint(out.tx->GetHash(), out.i));
availableCoins += out.tx->tx->vout[out.i].nValue;
}
}

coin_control.m_add_inputs = false;
coin_control.fAllowOtherInputs = false;
} else {
Expand All @@ -254,18 +271,30 @@ RPCHelpMan optimizeutxoset()
}

LogPrintf("optimizing outputs %d satoshis\n", availableCoins);


CTxDestination dest = DecodeDestination(address);
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Peercoin address: ") + address);
}

CScript script_pub_key = GetScriptForDestination(dest);
CAmount remaining = availableCoins;

CRecipient recipient = {script_pub_key, amount, false};
CAmount fee = GetMinFee( 2000 + remaining / amount * 40, GetAdjustedTime()); // very approximate
CMutableTransaction txTmp;
const uint32_t nSequence{CTxIn::MAX_SEQUENCE_NONFINAL};
std::vector<COutPoint> vPresetInputs;
coin_control.ListSelected(vPresetInputs);
for (const COutPoint& outpoint : vPresetInputs) {
txTmp.vin.push_back(CTxIn(outpoint, CScript(), nSequence));
}

// Calculate transaction input size
const CWallet& wallet{*pwallet};
TxSize tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txTmp), &wallet, &coin_control);
int nBytes = tx_sizes.vsize;

// calculate size of output
CTxOut txout(amount, script_pub_key);
txTmp.vout.push_back(txout);
tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txTmp), &wallet, &coin_control);
int nBytesPerOut = tx_sizes.vsize - nBytes;

CAmount fee = GetMinFee(nBytes + (unsigned int)(remaining / amount) * nBytesPerOut, GetAdjustedTime());
while (remaining > amount + fee) {
recipients.push_back(recipient);
remaining -= amount;
Expand Down

0 comments on commit 26bd4c3

Please sign in to comment.