@ -23,7 +23,7 @@
@@ -23,7 +23,7 @@
// calculation, but we should be able to refactor after priority is removed).
// NOTE: this requires that all inputs must be in mapWallet (eg the tx should
// be IsAllFromMe).
int64_t CalculateMaximumSignedTxSize ( const CTransaction & tx , const CWallet * pW allet)
static int64_t CalculateMaximumSignedTxSize ( const CTransaction & tx , const CWallet * w allet)
{
CMutableTransaction txNew ( tx ) ;
std : : vector < CInputCoin > vCoins ;
@ -31,11 +31,11 @@ int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *pWal
@@ -31,11 +31,11 @@ int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *pWal
// IsAllFromMe(ISMINE_SPENDABLE), so every input should already be in our
// wallet, with a valid index into the vout array.
for ( auto & input : tx . vin ) {
const auto mi = pW allet- > mapWallet . find ( input . prevout . hash ) ;
assert ( mi ! = pW allet- > mapWallet . end ( ) & & input . prevout . n < mi - > second . tx - > vout . size ( ) ) ;
const auto mi = w allet- > mapWallet . find ( input . prevout . hash ) ;
assert ( mi ! = w allet- > mapWallet . end ( ) & & input . prevout . n < mi - > second . tx - > vout . size ( ) ) ;
vCoins . emplace_back ( CInputCoin ( & ( mi - > second ) , input . prevout . n ) ) ;
}
if ( ! pW allet- > DummySignTx ( txNew , vCoins ) ) {
if ( ! w allet- > DummySignTx ( txNew , vCoins ) ) {
// This should never happen, because IsAllFromMe(ISMINE_SPENDABLE)
// implies that we can sign for every input.
return - 1 ;
@ -43,103 +43,102 @@ int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *pWal
@@ -43,103 +43,102 @@ int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *pWal
return GetVirtualTransactionSize ( txNew ) ;
}
bool CFeeBumper : : preconditionChecks ( const CWallet * pWallet , const CWalletTx & wtx ) {
if ( pWallet - > HasWalletSpend ( wtx . GetHash ( ) ) ) {
vErrors . push_back ( " Transaction has descendants in the wallet " ) ;
currentResult = BumpFeeResult : : INVALID_PARAMETER ;
return false ;
//! Check whether transaction has descendant in wallet or mempool, or has been
//! mined, or conflicts with a mined transaction. Return a feebumper::Result.
static feebumper : : Result PreconditionChecks ( const CWallet * wallet , const CWalletTx & wtx , std : : vector < std : : string > & errors )
{
if ( wallet - > HasWalletSpend ( wtx . GetHash ( ) ) ) {
errors . push_back ( " Transaction has descendants in the wallet " ) ;
return feebumper : : Result : : INVALID_PARAMETER ;
}
{
LOCK ( mempool . cs ) ;
auto it_mp = mempool . mapTx . find ( wtx . GetHash ( ) ) ;
if ( it_mp ! = mempool . mapTx . end ( ) & & it_mp - > GetCountWithDescendants ( ) > 1 ) {
vErrors . push_back ( " Transaction has descendants in the mempool " ) ;
currentResult = BumpFeeResult : : INVALID_PARAMETER ;
return false ;
errors . push_back ( " Transaction has descendants in the mempool " ) ;
return feebumper : : Result : : INVALID_PARAMETER ;
}
}
if ( wtx . GetDepthInMainChain ( ) ! = 0 ) {
vErrors . push_back ( " Transaction has been mined, or is conflicted with a mined transaction " ) ;
currentResult = BumpFeeResult : : WALLET_ERROR ;
return false ;
errors . push_back ( " Transaction has been mined, or is conflicted with a mined transaction " ) ;
return feebumper : : Result : : WALLET_ERROR ;
}
return true ;
return feebumper : : Result : : OK ;
}
CFeeBumper : : CFeeBumper ( const CWallet * pWallet , const uint256 txidIn , const CCoinControl & coin_control , CAmount totalFee )
:
txid ( std : : move ( txidIn ) ) ,
nOldFee ( 0 ) ,
nNewFee ( 0 )
namespace feebumper {
bool TransactionCanBeBumped ( CWallet * wallet , const uint256 & txid )
{
vErrors . clear ( ) ;
bumpedTxid . SetNull ( ) ;
AssertLockHeld ( pWallet - > cs_wallet ) ;
auto it = pWallet - > mapWallet . find ( txid ) ;
if ( it = = pWallet - > mapWallet . end ( ) ) {
vErrors . push_back ( " Invalid or non-wallet transaction id " ) ;
currentResult = BumpFeeResult : : INVALID_ADDRESS_OR_KEY ;
return ;
LOCK2 ( cs_main , wallet - > cs_wallet ) ;
const CWalletTx * wtx = wallet - > GetWalletTx ( txid ) ;
return wtx & & SignalsOptInRBF ( * wtx - > tx ) & & ! wtx - > mapValue . count ( " replaced_by_txid " ) ;
}
Result CreateTransaction ( const CWallet * wallet , const uint256 & txid , const CCoinControl & coin_control , CAmount total_fee , std : : vector < std : : string > & errors ,
CAmount & old_fee , CAmount & new_fee , CMutableTransaction & mtx )
{
LOCK2 ( cs_main , wallet - > cs_wallet ) ;
errors . clear ( ) ;
auto it = wallet - > mapWallet . find ( txid ) ;
if ( it = = wallet - > mapWallet . end ( ) ) {
errors . push_back ( " Invalid or non-wallet transaction id " ) ;
return Result : : INVALID_ADDRESS_OR_KEY ;
}
const CWalletTx & wtx = it - > second ;
if ( ! preconditionChecks ( pWallet , wtx ) ) {
return ;
Result result = PreconditionChecks ( wallet , wtx , errors ) ;
if ( result ! = Result : : OK ) {
return result ;
}
if ( ! SignalsOptInRBF ( * wtx . tx ) ) {
vErrors . push_back ( " Transaction is not BIP 125 replaceable " ) ;
currentResult = BumpFeeResult : : WALLET_ERROR ;
return ;
errors . push_back ( " Transaction is not BIP 125 replaceable " ) ;
return Result : : WALLET_ERROR ;
}
if ( wtx . mapValue . count ( " replaced_by_txid " ) ) {
vErrors . push_back ( strprintf ( " Cannot bump transaction %s which was already bumped by transaction %s " , txid . ToString ( ) , wtx . mapValue . at ( " replaced_by_txid " ) ) ) ;
currentResult = BumpFeeResult : : WALLET_ERROR ;
return ;
errors . push_back ( strprintf ( " Cannot bump transaction %s which was already bumped by transaction %s " , txid . ToString ( ) , wtx . mapValue . at ( " replaced_by_txid " ) ) ) ;
return Result : : WALLET_ERROR ;
}
// check that original tx consists entirely of our inputs
// if not, we can't bump the fee, because the wallet has no way of knowing the value of the other inputs (thus the fee)
if ( ! pWallet - > IsAllFromMe ( * wtx . tx , ISMINE_SPENDABLE ) ) {
vErrors . push_back ( " Transaction contains inputs that don't belong to this wallet " ) ;
currentResult = BumpFeeResult : : WALLET_ERROR ;
return ;
if ( ! wallet - > IsAllFromMe ( * wtx . tx , ISMINE_SPENDABLE ) ) {
errors . push_back ( " Transaction contains inputs that don't belong to this wallet " ) ;
return Result : : WALLET_ERROR ;
}
// figure out which output was change
// if there was no change output or multiple change outputs, fail
int nOutput = - 1 ;
for ( size_t i = 0 ; i < wtx . tx - > vout . size ( ) ; + + i ) {
if ( pW allet- > IsChange ( wtx . tx - > vout [ i ] ) ) {
if ( w allet- > IsChange ( wtx . tx - > vout [ i ] ) ) {
if ( nOutput ! = - 1 ) {
vErrors . push_back ( " Transaction has multiple change outputs " ) ;
currentResult = BumpFeeResult : : WALLET_ERROR ;
return ;
errors . push_back ( " Transaction has multiple change outputs " ) ;
return Result : : WALLET_ERROR ;
}
nOutput = i ;
}
}
if ( nOutput = = - 1 ) {
vErrors . push_back ( " Transaction does not have a change output " ) ;
currentResult = BumpFeeResult : : WALLET_ERROR ;
return ;
errors . push_back ( " Transaction does not have a change output " ) ;
return Result : : WALLET_ERROR ;
}
// Calculate the expected size of the new transaction.
int64_t txSize = GetVirtualTransactionSize ( * ( wtx . tx ) ) ;
const int64_t maxNewTxSize = CalculateMaximumSignedTxSize ( * wtx . tx , pW allet) ;
const int64_t maxNewTxSize = CalculateMaximumSignedTxSize ( * wtx . tx , w allet) ;
if ( maxNewTxSize < 0 ) {
vErrors . push_back ( " Transaction contains inputs that cannot be signed " ) ;
currentResult = BumpFeeResult : : INVALID_ADDRESS_OR_KEY ;
return ;
errors . push_back ( " Transaction contains inputs that cannot be signed " ) ;
return Result : : INVALID_ADDRESS_OR_KEY ;
}
// calculate the old fee and fee-rate
nOldF ee = wtx . GetDebit ( ISMINE_SPENDABLE ) - wtx . tx - > GetValueOut ( ) ;
CFeeRate nOldFeeRate ( nOldF ee, txSize ) ;
old_f ee = wtx . GetDebit ( ISMINE_SPENDABLE ) - wtx . tx - > GetValueOut ( ) ;
CFeeRate nOldFeeRate ( old_f ee, txSize ) ;
CFeeRate nNewFeeRate ;
// The wallet uses a conservative WALLET_INCREMENTAL_RELAY_FEE value to
// future proof against changes to network wide policy for incremental relay
@ -149,26 +148,24 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, const CCoin
@@ -149,26 +148,24 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, const CCoin
walletIncrementalRelayFee = : : incrementalRelayFee ;
}
if ( totalF ee > 0 ) {
if ( total_f ee > 0 ) {
CAmount minTotalFee = nOldFeeRate . GetFee ( maxNewTxSize ) + : : incrementalRelayFee . GetFee ( maxNewTxSize ) ;
if ( totalF ee < minTotalFee ) {
vE rrors. push_back ( strprintf ( " Insufficient totalFee, must be at least %s (oldFee %s + incrementalFee %s) " ,
if ( total_f ee < minTotalFee ) {
e rrors. push_back ( strprintf ( " Insufficient totalFee, must be at least %s (oldFee %s + incrementalFee %s) " ,
FormatMoney ( minTotalFee ) , FormatMoney ( nOldFeeRate . GetFee ( maxNewTxSize ) ) , FormatMoney ( : : incrementalRelayFee . GetFee ( maxNewTxSize ) ) ) ) ;
currentResult = BumpFeeResult : : INVALID_PARAMETER ;
return ;
return Result : : INVALID_PARAMETER ;
}
CAmount requiredFee = GetRequiredFee ( maxNewTxSize ) ;
if ( totalF ee < requiredFee ) {
vE rrors. push_back ( strprintf ( " Insufficient totalFee (cannot be less than required fee %s) " ,
if ( total_f ee < requiredFee ) {
e rrors. push_back ( strprintf ( " Insufficient totalFee (cannot be less than required fee %s) " ,
FormatMoney ( requiredFee ) ) ) ;
currentResult = BumpFeeResult : : INVALID_PARAMETER ;
return ;
return Result : : INVALID_PARAMETER ;
}
nNewFee = totalF ee ;
nNewFeeRate = CFeeRate ( totalF ee , maxNewTxSize ) ;
new_fee = total_f ee ;
nNewFeeRate = CFeeRate ( total_f ee , maxNewTxSize ) ;
} else {
nNewF ee = GetMinimumFee ( maxNewTxSize , coin_control , mempool , : : feeEstimator , nullptr /* FeeCalculation */ ) ;
nNewFeeRate = CFeeRate ( nNewF ee , maxNewTxSize ) ;
new_f ee = GetMinimumFee ( maxNewTxSize , coin_control , mempool , : : feeEstimator , nullptr /* FeeCalculation */ ) ;
nNewFeeRate = CFeeRate ( new_f ee , maxNewTxSize ) ;
// New fee rate must be at least old rate + minimum incremental relay rate
// walletIncrementalRelayFee.GetFeePerK() should be exact, because it's initialized
@ -177,53 +174,50 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, const CCoin
@@ -177,53 +174,50 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, const CCoin
// add 1 satoshi to the result, because it may have been rounded down.
if ( nNewFeeRate . GetFeePerK ( ) < nOldFeeRate . GetFeePerK ( ) + 1 + walletIncrementalRelayFee . GetFeePerK ( ) ) {
nNewFeeRate = CFeeRate ( nOldFeeRate . GetFeePerK ( ) + 1 + walletIncrementalRelayFee . GetFeePerK ( ) ) ;
nNewF ee = nNewFeeRate . GetFee ( maxNewTxSize ) ;
new_f ee = nNewFeeRate . GetFee ( maxNewTxSize ) ;
}
}
// Check that in all cases the new fee doesn't violate maxTxFee
if ( nNewFee > maxTxFee ) {
vErrors . push_back ( strprintf ( " Specified or calculated fee %s is too high (cannot be higher than maxTxFee %s) " ,
FormatMoney ( nNewFee ) , FormatMoney ( maxTxFee ) ) ) ;
currentResult = BumpFeeResult : : WALLET_ERROR ;
return ;
if ( new_fee > maxTxFee ) {
errors . push_back ( strprintf ( " Specified or calculated fee %s is too high (cannot be higher than maxTxFee %s) " ,
FormatMoney ( new_fee ) , FormatMoney ( maxTxFee ) ) ) ;
return Result : : WALLET_ERROR ;
}
// check that fee rate is higher than mempool's minimum fee
// (no point in bumping fee if we know that the new tx won't be accepted to the mempool)
// This may occur if the user set TotalFee or paytxfee too low, if fallbackfee is too low, or, perhaps,
// in a rare situation where the mempool minimum fee increased significantly since the fee estimation just a
// moment earlier. In this case, we report an error to the user, who may use totalF ee to make an adjustment.
// moment earlier. In this case, we report an error to the user, who may use total_f ee to make an adjustment.
CFeeRate minMempoolFeeRate = mempool . GetMinFee ( gArgs . GetArg ( " -maxmempool " , DEFAULT_MAX_MEMPOOL_SIZE ) * 1000000 ) ;
if ( nNewFeeRate . GetFeePerK ( ) < minMempoolFeeRate . GetFeePerK ( ) ) {
vE rrors. push_back ( strprintf (
e rrors. push_back ( strprintf (
" New fee rate (%s) is lower than the minimum fee rate (%s) to get into the mempool -- "
" the totalFee value should be at least %s or the settxfee value should be at least %s to add transaction " ,
FormatMoney ( nNewFeeRate . GetFeePerK ( ) ) ,
FormatMoney ( minMempoolFeeRate . GetFeePerK ( ) ) ,
FormatMoney ( minMempoolFeeRate . GetFee ( maxNewTxSize ) ) ,
FormatMoney ( minMempoolFeeRate . GetFeePerK ( ) ) ) ) ;
currentResult = BumpFeeResult : : WALLET_ERROR ;
return ;
return Result : : WALLET_ERROR ;
}
// Now modify the output to increase the fee.
// If the output is not large enough to pay the fee, fail.
CAmount nDelta = nNewFee - nOldF ee ;
CAmount nDelta = new_fee - old_f ee ;
assert ( nDelta > 0 ) ;
mtx = * wtx . tx ;
CTxOut * poutput = & ( mtx . vout [ nOutput ] ) ;
if ( poutput - > nValue < nDelta ) {
vErrors . push_back ( " Change output is too small to bump the fee " ) ;
currentResult = BumpFeeResult : : WALLET_ERROR ;
return ;
errors . push_back ( " Change output is too small to bump the fee " ) ;
return Result : : WALLET_ERROR ;
}
// If the output would become dust, discard it (converting the dust to fee)
poutput - > nValue - = nDelta ;
if ( poutput - > nValue < = GetDustThreshold ( * poutput , : : dustRelayFee ) ) {
LogPrint ( BCLog : : RPC , " Bumping fee and discarding dust output \n " ) ;
nNewF ee + = poutput - > nValue ;
new_f ee + = poutput - > nValue ;
mtx . vout . erase ( mtx . vout . begin ( ) + nOutput ) ;
}
@ -234,36 +228,36 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, const CCoin
@@ -234,36 +228,36 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, const CCoin
}
}
currentResult = BumpFee Result: : OK ;
return Result : : OK ;
}
bool CFeeBumper : : signTransaction ( CWallet * pWallet )
{
return pW allet - > SignTransaction ( mtx ) ;
bool SignTransaction ( CWallet * wallet , CMutableTransaction & mtx ) {
LOCK2 ( cs_main , wallet - > cs_wallet ) ;
return w allet- > SignTransaction ( mtx ) ;
}
bool CFeeBumper : : commit ( CWallet * pWallet )
Result CommitTransaction ( CWallet * wallet , const uint256 & txid , CMutableTransaction & & mtx , std : : vector < std : : string > & errors , uint256 & bumped_txid )
{
AssertLockHeld ( pW allet- > cs_wallet ) ;
if ( ! vE rrors. empty ( ) | | currentResult ! = BumpFeeResult : : OK ) {
return false ;
LOCK2 ( cs_main , w allet- > cs_wallet ) ;
if ( ! e rrors. empty ( ) ) {
return Result : : MISC_ERROR ;
}
auto it = txid . IsNull ( ) ? pWallet - > mapWallet . end ( ) : pWallet - > mapWallet . find ( txid ) ;
if ( it = = pWallet - > mapWallet . end ( ) ) {
vErrors . push_back ( " Invalid or non-wallet transaction id " ) ;
currentResult = BumpFeeResult : : MISC_ERROR ;
return false ;
auto it = txid . IsNull ( ) ? wallet - > mapWallet . end ( ) : wallet - > mapWallet . find ( txid ) ;
if ( it = = wallet - > mapWallet . end ( ) ) {
errors . push_back ( " Invalid or non-wallet transaction id " ) ;
return Result : : MISC_ERROR ;
}
CWalletTx & oldWtx = it - > second ;
// make sure the transaction still has no descendants and hasn't been mined in the meantime
if ( ! preconditionChecks ( pWallet , oldWtx ) ) {
return false ;
Result result = PreconditionChecks ( wallet , oldWtx , errors ) ;
if ( result ! = Result : : OK ) {
return result ;
}
CWalletTx wtxBumped ( pW allet, MakeTransactionRef ( std : : move ( mtx ) ) ) ;
CWalletTx wtxBumped ( w allet, MakeTransactionRef ( std : : move ( mtx ) ) ) ;
// commit/broadcast the tx
CReserveKey reservekey ( pW allet) ;
CReserveKey reservekey ( w allet) ;
wtxBumped . mapValue = oldWtx . mapValue ;
wtxBumped . mapValue [ " replaces_txid " ] = oldWtx . GetHash ( ) . ToString ( ) ;
wtxBumped . vOrderForm = oldWtx . vOrderForm ;
@ -271,27 +265,29 @@ bool CFeeBumper::commit(CWallet *pWallet)
@@ -271,27 +265,29 @@ bool CFeeBumper::commit(CWallet *pWallet)
wtxBumped . fTimeReceivedIsTxTime = true ;
wtxBumped . fFromMe = true ;
CValidationState state ;
if ( ! pW allet- > CommitTransaction ( wtxBumped , reservekey , g_connman . get ( ) , state ) ) {
if ( ! w allet- > CommitTransaction ( wtxBumped , reservekey , g_connman . get ( ) , state ) ) {
// NOTE: CommitTransaction never returns false, so this should never happen.
vE rrors. push_back ( strprintf ( " The transaction was rejected: %s " , state . GetRejectReason ( ) ) ) ;
return false ;
e rrors. push_back ( strprintf ( " The transaction was rejected: %s " , state . GetRejectReason ( ) ) ) ;
return Result : : WALLET_ERROR ;
}
bumpedT xid = wtxBumped . GetHash ( ) ;
bumped_t xid = wtxBumped . GetHash ( ) ;
if ( state . IsInvalid ( ) ) {
// This can happen if the mempool rejected the transaction. Report
// what happened in the "errors" response.
vE rrors. push_back ( strprintf ( " The transaction was rejected: %s " , FormatStateMessage ( state ) ) ) ;
e rrors. push_back ( strprintf ( " Error: The transaction was rejected: %s" , FormatStateMessage ( state ) ) ) ;
}
// mark the original tx as bumped
if ( ! pW allet- > MarkReplaced ( oldWtx . GetHash ( ) , wtxBumped . GetHash ( ) ) ) {
if ( ! w allet- > MarkReplaced ( oldWtx . GetHash ( ) , wtxBumped . GetHash ( ) ) ) {
// TODO: see if JSON-RPC has a standard way of returning a response
// along with an exception. It would be good to return information about
// wtxBumped to the caller even if marking the original transaction
// replaced does not succeed for some reason.
vE rrors. push_back ( " Created new bumpfee transaction but could not mark the original transaction as replaced " ) ;
e rrors. push_back ( " Created new bumpfee transaction but could not mark the original transaction as replaced " ) ;
}
return true ;
return Result : : OK ;
}
} // namespace feebumper