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

Fixing wrong transaction size estimation #568

Merged
merged 5 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
72 changes: 18 additions & 54 deletions src/qt/sigmacoincontroldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,54 +509,20 @@ void SigmaCoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
dPriorityInputs += (double)out.tx->vout[out.i].nValue * (out.nDepth+1);

// Bytes
CTxDestination address;
int witnessversion = 0;
std::vector<unsigned char> witnessprogram;
if (out.tx->vout[out.i].scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
{
nBytesInputs += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
fWitness = true;
}
else if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
{
CPubKey pubkey;
CKeyID *keyid = boost::get<CKeyID>(&address);
if (keyid && model->getPubKey(*keyid, pubkey))
{
nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
if (!pubkey.IsCompressed())
nQuantityUncompressed++;
}
else
nBytesInputs += 148; // in all error cases, simply assume 148 here
}
else nBytesInputs += 148;
nBytesInputs += 74;
levonpetrosyan93 marked this conversation as resolved.
Show resolved Hide resolved
levonpetrosyan93 marked this conversation as resolved.
Show resolved Hide resolved
}

// calculation
if (nQuantity > 0)
{
// Bytes
nBytes = nBytesInputs + ((SigmaCoinControlDialog::payAmounts.size() > 0 ? SigmaCoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
if (fWitness)
levonpetrosyan93 marked this conversation as resolved.
Show resolved Hide resolved
{
// 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.
}
// Bytes //1323 is script size for sigma spend
nBytes = nBytesInputs + vCoinControl.size() * 1323 + 10;
levonpetrosyan93 marked this conversation as resolved.
Show resolved Hide resolved

// Priority
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)
sPriorityLabel = SigmaCoinControlDialog::getPriorityLabel(dPriority, mempoolEstimatePriority);

// 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 (SigmaCoinControlDialog::fSubtractFeeFromAmount)
if (nAmount - nPayAmount == 0)
nBytes -= 34;

// Fee
nPayFee = CWallet::GetMinimumFee(nBytes, nTxConfirmTarget, mempool);
if (nPayFee > 0 && coinControl->nMinimumTotalFee > nPayFee)
Expand All @@ -576,27 +542,25 @@ void SigmaCoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
nChange = nAmount - nPayAmount;
if (!SigmaCoinControlDialog::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(::minRelayTxFee))
{
if (SigmaCoinControlDialog::fSubtractFeeFromAmount) // dust-change will be raised until no dust
nChange = txout.GetDustThreshold(::minRelayTxFee);
else
{
nPayFee += nChange;
nChange = 0;
}
}
}
//update bytes
CAmount remChange = nChange;

if (nChange == 0 && !SigmaCoinControlDialog::fSubtractFeeFromAmount)
nBytes -= 34;
std::vector<int64_t> allDenoms;
sigma::GetAllDenoms(allDenoms);
for(int64_t denom : allDenoms) {
while (remChange >= denom) {
nBytes += 44;
levonpetrosyan93 marked this conversation as resolved.
Show resolved Hide resolved
remChange -= denom;
}
if(!remChange)
break;
}

//add remaining to fee
nPayFee += remChange;

// after fee
nAfterFee = nAmount - nPayFee;
if (nAfterFee < 0)
Expand Down
10 changes: 10 additions & 0 deletions src/sigma/coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ void GetAllDenoms(std::vector<sigma::CoinDenomination>& denominations_out) {
denominations_out.push_back(CoinDenomination::SIGMA_DENOM_0_05);
}

void GetAllDenoms(std::vector<int64_t>& denominations_out) {
denominations_out.push_back(100 * COIN);
denominations_out.push_back(25 * COIN);
denominations_out.push_back(10 * COIN);
denominations_out.push_back(1 * COIN);
denominations_out.push_back(50 * CENT);
denominations_out.push_back(10 * CENT);
denominations_out.push_back(5 * CENT);
}

//class PublicCoin
PublicCoin::PublicCoin()
: denomination(CoinDenomination::SIGMA_DENOM_1)
Expand Down
1 change: 1 addition & 0 deletions src/sigma/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ bool RealNumberToDenomination(const double& value, CoinDenomination& denom_out);

/// \brief Returns a list of all possible denominations in descending order of value.
void GetAllDenoms(std::vector<sigma::CoinDenomination>& denominations_out);
void GetAllDenoms(std::vector<int64_t>& denominations_out);

class PublicCoin {
public:
Expand Down