2017-03-03 16:15:47 +01:00
// Copyright (c) 2017 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2017-11-10 01:57:53 +01:00
# include <consensus/validation.h>
# include <wallet/coincontrol.h>
# include <wallet/feebumper.h>
# include <wallet/fees.h>
# include <wallet/wallet.h>
# include <policy/fees.h>
# include <policy/policy.h>
# include <policy/rbf.h>
# include <validation.h> //for mempool access
# include <txmempool.h>
# include <utilmoneystr.h>
# include <util.h>
# include <net.h>
2017-03-03 16:15:47 +01:00
2017-06-15 16:34:17 +02:00
//! Check whether transaction has descendant in wallet or mempool, or has been
//! mined, or conflicts with a mined transaction. Return a feebumper::Result.
2018-04-25 23:16:16 +02:00
static feebumper : : Result PreconditionChecks ( const CWallet * wallet , const CWalletTx & wtx , std : : vector < std : : string > & errors ) EXCLUSIVE_LOCKS_REQUIRED ( wallet - > cs_wallet )
2017-06-15 16:34:17 +02:00
{
2017-09-20 22:04:05 +02:00
if ( wallet - > HasWalletSpend ( wtx . GetHash ( ) ) ) {
errors . push_back ( " Transaction has descendants in the wallet " ) ;
2017-06-15 16:34:17 +02:00
return feebumper : : Result : : INVALID_PARAMETER ;
2017-05-11 09:34:39 +02:00
}
{
LOCK ( mempool . cs ) ;
auto it_mp = mempool . mapTx . find ( wtx . GetHash ( ) ) ;
if ( it_mp ! = mempool . mapTx . end ( ) & & it_mp - > GetCountWithDescendants ( ) > 1 ) {
2017-09-20 22:04:05 +02:00
errors . push_back ( " Transaction has descendants in the mempool " ) ;
2017-06-15 16:34:17 +02:00
return feebumper : : Result : : INVALID_PARAMETER ;
2017-05-11 09:34:39 +02:00
}
}
if ( wtx . GetDepthInMainChain ( ) ! = 0 ) {
2017-09-20 22:04:05 +02:00
errors . push_back ( " Transaction has been mined, or is conflicted with a mined transaction " ) ;
2017-06-15 16:34:17 +02:00
return feebumper : : Result : : WALLET_ERROR ;
2017-05-11 09:34:39 +02:00
}
2018-01-23 19:57:10 +01:00
if ( ! SignalsOptInRBF ( * wtx . tx ) ) {
errors . push_back ( " Transaction is not BIP 125 replaceable " ) ;
return feebumper : : Result : : WALLET_ERROR ;
}
if ( wtx . mapValue . count ( " replaced_by_txid " ) ) {
errors . push_back ( strprintf ( " Cannot bump transaction %s which was already bumped by transaction %s " , wtx . GetHash ( ) . ToString ( ) , wtx . mapValue . at ( " replaced_by_txid " ) ) ) ;
return feebumper : : 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 ( ! wallet - > IsAllFromMe ( * wtx . tx , ISMINE_SPENDABLE ) ) {
errors . push_back ( " Transaction contains inputs that don't belong to this wallet " ) ;
return feebumper : : Result : : WALLET_ERROR ;
}
2017-06-15 16:34:17 +02:00
return feebumper : : Result : : OK ;
2017-05-11 09:34:39 +02:00
}
2017-06-15 16:34:17 +02:00
namespace feebumper {
2018-01-30 00:25:35 +01:00
bool TransactionCanBeBumped ( const CWallet * wallet , const uint256 & txid )
2017-03-03 16:15:47 +01:00
{
2017-06-15 16:34:17 +02:00
LOCK2 ( cs_main , wallet - > cs_wallet ) ;
const CWalletTx * wtx = wallet - > GetWalletTx ( txid ) ;
2018-01-30 00:25:35 +01:00
if ( wtx = = nullptr ) return false ;
std : : vector < std : : string > errors_dummy ;
feebumper : : Result res = PreconditionChecks ( wallet , * wtx , errors_dummy ) ;
return res = = feebumper : : Result : : OK ;
2017-06-15 16:34:17 +02:00
}
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 ) ;
2017-09-20 22:04:05 +02:00
errors . clear ( ) ;
auto it = wallet - > mapWallet . find ( txid ) ;
if ( it = = wallet - > mapWallet . end ( ) ) {
errors . push_back ( " Invalid or non-wallet transaction id " ) ;
2017-06-15 16:34:17 +02:00
return Result : : INVALID_ADDRESS_OR_KEY ;
2017-03-03 16:15:47 +01:00
}
const CWalletTx & wtx = it - > second ;
2017-06-15 16:34:17 +02:00
Result result = PreconditionChecks ( wallet , wtx , errors ) ;
if ( result ! = Result : : OK ) {
return result ;
2017-03-03 16:15:47 +01:00
}
// 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 ) {
2017-09-20 22:04:05 +02:00
if ( wallet - > IsChange ( wtx . tx - > vout [ i ] ) ) {
2017-03-03 16:15:47 +01:00
if ( nOutput ! = - 1 ) {
2017-09-20 22:04:05 +02:00
errors . push_back ( " Transaction has multiple change outputs " ) ;
2017-06-15 16:34:17 +02:00
return Result : : WALLET_ERROR ;
2017-03-03 16:15:47 +01:00
}
nOutput = i ;
}
}
if ( nOutput = = - 1 ) {
2017-09-20 22:04:05 +02:00
errors . push_back ( " Transaction does not have a change output " ) ;
2017-06-15 16:34:17 +02:00
return Result : : WALLET_ERROR ;
2017-03-03 16:15:47 +01:00
}
// Calculate the expected size of the new transaction.
int64_t txSize = GetVirtualTransactionSize ( * ( wtx . tx ) ) ;
2017-09-20 22:04:05 +02:00
const int64_t maxNewTxSize = CalculateMaximumSignedTxSize ( * wtx . tx , wallet ) ;
2017-03-03 16:15:47 +01:00
if ( maxNewTxSize < 0 ) {
2017-09-20 22:04:05 +02:00
errors . push_back ( " Transaction contains inputs that cannot be signed " ) ;
2017-06-15 16:34:17 +02:00
return Result : : INVALID_ADDRESS_OR_KEY ;
2017-03-03 16:15:47 +01:00
}
// calculate the old fee and fee-rate
2017-09-20 22:04:05 +02:00
old_fee = wtx . GetDebit ( ISMINE_SPENDABLE ) - wtx . tx - > GetValueOut ( ) ;
CFeeRate nOldFeeRate ( old_fee , txSize ) ;
2017-03-03 16:15:47 +01:00
CFeeRate nNewFeeRate ;
// The wallet uses a conservative WALLET_INCREMENTAL_RELAY_FEE value to
// future proof against changes to network wide policy for incremental relay
// fee that our node may not be aware of.
CFeeRate walletIncrementalRelayFee = CFeeRate ( WALLET_INCREMENTAL_RELAY_FEE ) ;
if ( : : incrementalRelayFee > walletIncrementalRelayFee ) {
walletIncrementalRelayFee = : : incrementalRelayFee ;
}
2017-09-20 22:04:05 +02:00
if ( total_fee > 0 ) {
2017-03-03 16:15:47 +01:00
CAmount minTotalFee = nOldFeeRate . GetFee ( maxNewTxSize ) + : : incrementalRelayFee . GetFee ( maxNewTxSize ) ;
2017-09-20 22:04:05 +02:00
if ( total_fee < minTotalFee ) {
errors . push_back ( strprintf ( " Insufficient totalFee, must be at least %s (oldFee %s + incrementalFee %s) " ,
2017-03-03 16:15:47 +01:00
FormatMoney ( minTotalFee ) , FormatMoney ( nOldFeeRate . GetFee ( maxNewTxSize ) ) , FormatMoney ( : : incrementalRelayFee . GetFee ( maxNewTxSize ) ) ) ) ;
2017-06-15 16:34:17 +02:00
return Result : : INVALID_PARAMETER ;
2017-03-03 16:15:47 +01:00
}
2018-04-07 18:12:46 +02:00
CAmount requiredFee = GetRequiredFee ( * wallet , maxNewTxSize ) ;
2017-09-20 22:04:05 +02:00
if ( total_fee < requiredFee ) {
errors . push_back ( strprintf ( " Insufficient totalFee (cannot be less than required fee %s) " ,
2017-03-03 16:15:47 +01:00
FormatMoney ( requiredFee ) ) ) ;
2017-06-15 16:34:17 +02:00
return Result : : INVALID_PARAMETER ;
2017-03-03 16:15:47 +01:00
}
2017-09-20 22:04:05 +02:00
new_fee = total_fee ;
nNewFeeRate = CFeeRate ( total_fee , maxNewTxSize ) ;
2017-03-03 16:15:47 +01:00
} else {
2018-04-07 18:12:46 +02:00
new_fee = GetMinimumFee ( * wallet , maxNewTxSize , coin_control , mempool , : : feeEstimator , nullptr /* FeeCalculation */ ) ;
2017-09-20 22:04:05 +02:00
nNewFeeRate = CFeeRate ( new_fee , maxNewTxSize ) ;
2017-03-03 16:15:47 +01:00
// New fee rate must be at least old rate + minimum incremental relay rate
// walletIncrementalRelayFee.GetFeePerK() should be exact, because it's initialized
// in that unit (fee per kb).
// However, nOldFeeRate is a calculated value from the tx fee/size, so
// 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 ( ) ) ;
2017-09-20 22:04:05 +02:00
new_fee = nNewFeeRate . GetFee ( maxNewTxSize ) ;
2017-03-03 16:15:47 +01:00
}
}
// Check that in all cases the new fee doesn't violate maxTxFee
2017-09-20 22:04:05 +02:00
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 ) ) ) ;
2017-06-15 16:34:17 +02:00
return Result : : WALLET_ERROR ;
2017-03-03 16:15:47 +01:00
}
// 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
2017-09-20 22:04:05 +02:00
// moment earlier. In this case, we report an error to the user, who may use total_fee to make an adjustment.
2017-08-01 21:17:40 +02:00
CFeeRate minMempoolFeeRate = mempool . GetMinFee ( gArgs . GetArg ( " -maxmempool " , DEFAULT_MAX_MEMPOOL_SIZE ) * 1000000 ) ;
2017-03-03 16:15:47 +01:00
if ( nNewFeeRate . GetFeePerK ( ) < minMempoolFeeRate . GetFeePerK ( ) ) {
2017-09-20 22:04:05 +02:00
errors . push_back ( strprintf (
2017-11-02 19:29:07 +01:00
" 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 ( ) ) ) ) ;
2017-06-15 16:34:17 +02:00
return Result : : WALLET_ERROR ;
2017-03-03 16:15:47 +01:00
}
// Now modify the output to increase the fee.
// If the output is not large enough to pay the fee, fail.
2017-09-20 22:04:05 +02:00
CAmount nDelta = new_fee - old_fee ;
2017-03-03 16:15:47 +01:00
assert ( nDelta > 0 ) ;
2018-05-04 23:38:49 +02:00
mtx = CMutableTransaction { * wtx . tx } ;
2017-03-03 16:15:47 +01:00
CTxOut * poutput = & ( mtx . vout [ nOutput ] ) ;
if ( poutput - > nValue < nDelta ) {
2017-09-20 22:04:05 +02:00
errors . push_back ( " Change output is too small to bump the fee " ) ;
2017-06-15 16:34:17 +02:00
return Result : : WALLET_ERROR ;
2017-03-03 16:15:47 +01:00
}
// If the output would become dust, discard it (converting the dust to fee)
poutput - > nValue - = nDelta ;
2018-04-07 18:12:46 +02:00
if ( poutput - > nValue < = GetDustThreshold ( * poutput , GetDiscardRate ( * wallet , : : feeEstimator ) ) ) {
2017-03-03 16:15:47 +01:00
LogPrint ( BCLog : : RPC , " Bumping fee and discarding dust output \n " ) ;
2017-09-20 22:04:05 +02:00
new_fee + = poutput - > nValue ;
2017-03-03 16:15:47 +01:00
mtx . vout . erase ( mtx . vout . begin ( ) + nOutput ) ;
}
// Mark new tx not replaceable, if requested.
2018-04-07 18:12:46 +02:00
if ( ! coin_control . m_signal_bip125_rbf . get_value_or ( wallet - > m_signal_rbf ) ) {
2017-03-03 16:15:47 +01:00
for ( auto & input : mtx . vin ) {
if ( input . nSequence < 0xfffffffe ) input . nSequence = 0xfffffffe ;
}
}
2018-01-23 19:57:10 +01:00
2017-06-15 16:34:17 +02:00
return Result : : OK ;
2017-03-03 16:15:47 +01:00
}
2017-06-15 16:34:17 +02:00
bool SignTransaction ( CWallet * wallet , CMutableTransaction & mtx ) {
LOCK2 ( cs_main , wallet - > cs_wallet ) ;
return wallet - > SignTransaction ( mtx ) ;
2017-03-30 09:26:21 +02:00
}
2017-06-15 16:34:17 +02:00
Result CommitTransaction ( CWallet * wallet , const uint256 & txid , CMutableTransaction & & mtx , std : : vector < std : : string > & errors , uint256 & bumped_txid )
2017-03-03 16:15:47 +01:00
{
2017-06-15 16:34:17 +02:00
LOCK2 ( cs_main , wallet - > cs_wallet ) ;
if ( ! errors . empty ( ) ) {
return Result : : MISC_ERROR ;
2017-03-28 09:08:01 +02:00
}
2017-09-20 22:04:05 +02:00
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 " ) ;
2017-06-15 16:34:17 +02:00
return Result : : MISC_ERROR ;
2017-03-03 16:15:47 +01:00
}
2017-08-13 16:04:57 +02:00
CWalletTx & oldWtx = it - > second ;
2017-03-03 16:15:47 +01:00
2017-05-18 09:50:11 +02:00
// make sure the transaction still has no descendants and hasn't been mined in the meantime
2017-06-15 16:34:17 +02:00
Result result = PreconditionChecks ( wallet , oldWtx , errors ) ;
if ( result ! = Result : : OK ) {
return result ;
2017-05-11 09:34:39 +02:00
}
2017-03-03 16:15:47 +01:00
// commit/broadcast the tx
2017-02-02 21:30:03 +01:00
CTransactionRef tx = MakeTransactionRef ( std : : move ( mtx ) ) ;
mapValue_t mapValue = oldWtx . mapValue ;
mapValue [ " replaces_txid " ] = oldWtx . GetHash ( ) . ToString ( ) ;
2017-09-20 22:04:05 +02:00
CReserveKey reservekey ( wallet ) ;
2017-03-03 16:15:47 +01:00
CValidationState state ;
2017-02-02 21:30:03 +01:00
if ( ! wallet - > CommitTransaction ( tx , std : : move ( mapValue ) , oldWtx . vOrderForm , oldWtx . strFromAccount , reservekey , g_connman . get ( ) , state ) ) {
2017-03-03 16:15:47 +01:00
// NOTE: CommitTransaction never returns false, so this should never happen.
2018-02-07 00:47:51 +01:00
errors . push_back ( strprintf ( " The transaction was rejected: %s " , FormatStateMessage ( state ) ) ) ;
2017-06-15 16:34:17 +02:00
return Result : : WALLET_ERROR ;
2017-03-03 16:15:47 +01:00
}
2017-02-02 21:30:03 +01:00
bumped_txid = tx - > GetHash ( ) ;
2017-03-03 16:15:47 +01:00
if ( state . IsInvalid ( ) ) {
// This can happen if the mempool rejected the transaction. Report
// what happened in the "errors" response.
2017-06-15 16:34:17 +02:00
errors . push_back ( strprintf ( " Error: The transaction was rejected: %s " , FormatStateMessage ( state ) ) ) ;
2017-03-03 16:15:47 +01:00
}
// mark the original tx as bumped
2017-02-02 21:30:03 +01:00
if ( ! wallet - > MarkReplaced ( oldWtx . GetHash ( ) , bumped_txid ) ) {
2017-03-03 16:15:47 +01:00
// 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.
2017-09-20 22:04:05 +02:00
errors . push_back ( " Created new bumpfee transaction but could not mark the original transaction as replaced " ) ;
2017-03-03 16:15:47 +01:00
}
2017-06-15 16:34:17 +02:00
return Result : : OK ;
2017-03-03 16:15:47 +01:00
}
2017-09-20 22:19:30 +02:00
} // namespace feebumper