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

JSplit fee estimation and calculation optimized #974

Merged
merged 2 commits into from
Jan 30, 2021
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
109 changes: 57 additions & 52 deletions src/qt/coincontroldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,62 +523,67 @@ void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog, bool a
// calculation
if (nQuantity > 0)
{
// Bytes
nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
if (fWitness)
{
// there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
// usually, the result will be an overestimate within a couple of satoshis so that the confirmation dialog ends up displaying a slightly smaller fee.
// also, the witness stack size value value is a variable sized integer. usually, the number of stack items will be well under the single byte var int limit.
nBytes += 2; // account for the serialized marker and flag bytes
nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
}

// in the subtract fee from amount case, we can tell if zero change already and subtract the bytes, so that fee calculation afterwards is accurate
if (CoinControlDialog::fSubtractFeeFromAmount)
if (nAmount - nPayAmount == 0)
nBytes -= 34;

// Fee
nPayFee = CWallet::GetMinimumFee(nBytes, nTxConfirmTarget, mempool);
if (nPayFee > 0 && coinControl->nMinimumTotalFee > nPayFee)
nPayFee = coinControl->nMinimumTotalFee;


// Allow free? (require at least hard-coded threshold and default to that if no estimate)
double mempoolEstimatePriority = mempool.estimateSmartPriority(nTxConfirmTarget);
dPriority = dPriorityInputs / (nBytes - nBytesInputs + (nQuantityUncompressed * 29)); // 29 = 180 - 151 (uncompressed public keys are over the limit. max 151 bytes of the input are ignored for priority)
double dPriorityNeeded = std::max(mempoolEstimatePriority, AllowFreeThreshold());
fAllowFree = (dPriority >= dPriorityNeeded);

if (fSendFreeTransactions)
if (fAllowFree && nBytes <= MAX_FREE_TRANSACTION_CREATE_SIZE)
nPayFee = 0;

if (nPayAmount > 0)
{
nChange = nAmount - nPayAmount;
if (!CoinControlDialog::fSubtractFeeFromAmount)
nChange -= nPayFee;

// Never create dust outputs; if we would, just add the dust to the fee.
if (nChange > 0 && nChange < MIN_CHANGE)
if(anonymousMode){
std::tie(nPayFee, nBytes) = model->getWallet()->EstimateJoinSplitFee(nPayAmount,CoinControlDialog::fSubtractFeeFromAmount, coinControl);
if (nPayAmount > 0) {
nChange = nAmount - nPayAmount;
if (!CoinControlDialog::fSubtractFeeFromAmount)
nChange -= nPayFee;
}
} else {
// Bytes
nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
if (fWitness)
{
CTxOut txout(nChange, (CScript)std::vector<unsigned char>(24, 0));
if (txout.IsDust(dustRelayFee))
{
if (CoinControlDialog::fSubtractFeeFromAmount) // dust-change will be raised until no dust
nChange = txout.GetDustThreshold(dustRelayFee);
else
{
nPayFee += nChange;
nChange = 0;
// there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
// usually, the result will be an overestimate within a couple of satoshis so that the confirmation dialog ends up displaying a slightly smaller fee.
// also, the witness stack size value value is a variable sized integer. usually, the number of stack items will be well under the single byte var int limit.
nBytes += 2; // account for the serialized marker and flag bytes
nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
}

// in the subtract fee from amount case, we can tell if zero change already and subtract the bytes, so that fee calculation afterwards is accurate
if (CoinControlDialog::fSubtractFeeFromAmount)
if (nAmount - nPayAmount == 0)
nBytes -= 34;

// Fee
nPayFee = CWallet::GetMinimumFee(nBytes, nTxConfirmTarget, mempool);
if (nPayFee > 0 && coinControl->nMinimumTotalFee > nPayFee)
nPayFee = coinControl->nMinimumTotalFee;

// Allow free? (require at least hard-coded threshold and default to that if no estimate)
double mempoolEstimatePriority = mempool.estimateSmartPriority(nTxConfirmTarget);
dPriority = dPriorityInputs / (nBytes - nBytesInputs + (nQuantityUncompressed *
29)); // 29 = 180 - 151 (uncompressed public keys are over the limit. max 151 bytes of the input are ignored for priority)
double dPriorityNeeded = std::max(mempoolEstimatePriority, AllowFreeThreshold());
fAllowFree = (dPriority >= dPriorityNeeded);

if (fSendFreeTransactions)
if (fAllowFree && nBytes <= MAX_FREE_TRANSACTION_CREATE_SIZE)
nPayFee = 0;

if (nPayAmount > 0) {
nChange = nAmount - nPayAmount;
if (!CoinControlDialog::fSubtractFeeFromAmount)
nChange -= nPayFee;

// Never create dust outputs; if we would, just add the dust to the fee.
if (nChange > 0 && nChange < MIN_CHANGE) {
CTxOut txout(nChange, (CScript) std::vector<unsigned char>(24, 0));
if (txout.IsDust(dustRelayFee)) {
if (CoinControlDialog::fSubtractFeeFromAmount) // dust-change will be raised until no dust
nChange = txout.GetDustThreshold(dustRelayFee);
else {
nPayFee += nChange;
nChange = 0;
}
}
}
}

if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
nBytes -= 34;
if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
nBytes -= 34;
}
}

// after fee
Expand Down
5 changes: 4 additions & 1 deletion src/wallet/lelantusjoinsplitbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ CWalletTx LelantusJoinSplitBuilder::Build(
nCountNextUse = pwalletMain->zwallet->GetCount();
}

for (fee = payTxFee.GetFeePerK();;) {
std::tie(fee, std::ignore) = wallet.EstimateJoinSplitFee(vOut + mint, recipientsToSubtractFee, coinControl);


for (;;) {
// In case of not enough fee, reset mint seed counter
if (pwalletMain->zwallet) {
pwalletMain->zwallet->SetCount(nCountNextUse);
Expand Down
45 changes: 24 additions & 21 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6843,35 +6843,35 @@ CWalletTx CWallet::CreateLelantusJoinSplitTransaction(
return tx;
}

CAmount CWallet::EstimateJoinSplitFee(CAmount required, bool subtractFeeFromAmount, const CCoinControl *coinControl) {
std::pair<CAmount, unsigned int> CWallet::EstimateJoinSplitFee(CAmount required, bool subtractFeeFromAmount, const CCoinControl *coinControl) {
CAmount fee;

unsigned size;
std::vector<CLelantusEntry> spendCoins;
std::vector<CSigmaEntry> sigmaSpendCoins;

std::list<CSigmaEntry> coins = this->GetAvailableCoins(coinControl, false, true);
CAmount availableSigmaBalance(0);
for (auto coin : coins) {
availableSigmaBalance += coin.get_denomination_value();
}

for (fee = payTxFee.GetFeePerK();;) {
CAmount currentRequired = required;

if (!subtractFeeFromAmount)
currentRequired += fee;


spendCoins.clear();
sigmaSpendCoins.clear();
auto &consensusParams = Params().GetConsensus();
CAmount changeToMint = 0;

std::vector<sigma::CoinDenomination> denomChanges;
try {
std::list<CSigmaEntry> coins = this->GetAvailableCoins(coinControl, false, true);
CAmount availableBalance(0);
for (auto coin : coins) {
availableBalance += coin.get_denomination_value();
}
if (availableBalance > 0) {
if (availableSigmaBalance > 0) {
CAmount inputFromSigma;
if (currentRequired > availableBalance)
inputFromSigma = availableBalance;
if (currentRequired > availableSigmaBalance)
inputFromSigma = availableSigmaBalance;
else
inputFromSigma = currentRequired;

Expand All @@ -6880,30 +6880,33 @@ CAmount CWallet::EstimateJoinSplitFee(CAmount required, bool subtractFeeFromAmou
consensusParams.nMaxValueLelantusSpendPerTransaction, coinControl, true);
currentRequired -= inputFromSigma;
}
} catch (std::runtime_error const &) {
}

if (currentRequired > 0) {
if (!this->GetCoinsToJoinSplit(currentRequired, spendCoins, changeToMint,
consensusParams.nMaxLelantusInputPerTransaction,
consensusParams.nMaxValueLelantusSpendPerTransaction, coinControl, true)) {
throw InsufficientFunds();
if (currentRequired > 0) {
if (!this->GetCoinsToJoinSplit(currentRequired, spendCoins, changeToMint,
consensusParams.nMaxLelantusInputPerTransaction,
consensusParams.nMaxValueLelantusSpendPerTransaction, coinControl, true)) {
return std::make_pair(0, 0);
}
}
} catch (std::runtime_error) {
}

// 9560 is constant part, mainly Schnorr and Range proof, 2560 is for each sigma/aux data
// 956 is constant part, mainly Schnorr and Range proof, 2560 is for each sigma/aux data
// 179 other parts of tx, assuming 1 utxo and 1 jmint
unsigned size = 956 + 2560 * (spendCoins.size() + sigmaSpendCoins.size()) + 179;
size = 956 + 2560 * (spendCoins.size() + sigmaSpendCoins.size()) + 179;
CAmount feeNeeded = CWallet::GetMinimumFee(size, nTxConfirmTarget, mempool);

if (fee >= feeNeeded) {
break;
}

fee = feeNeeded;

if(subtractFeeFromAmount)
break;
}

return fee;
return std::make_pair(fee, size);
}

bool CWallet::CommitLelantusTransaction(CWalletTx& wtxNew, std::vector<CLelantusEntry>& spendCoins, std::vector<CHDMint>& mintCoins) {
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface

void JoinSplitLelantus(const std::vector<CRecipient>& recipients, const std::vector<CAmount>& newMints, CWalletTx& result);

CAmount EstimateJoinSplitFee(CAmount required, bool subtractFeeFromAmount, const CCoinControl *coinControl);
std::pair<CAmount, unsigned int> EstimateJoinSplitFee(CAmount required, bool subtractFeeFromAmount, const CCoinControl *coinControl);

bool GetMint(const uint256& hashSerial, CSigmaEntry& zerocoin, bool forEstimation = false) const;

Expand Down