mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-06-02 15:32:34 +02:00
wallet: Avoid use of Chain::Lock in CWallet::CreateTransaction
This is a step toward removing the Chain::Lock class and reducing cs_main locking. This change only affects behavior in the case where wallet last block processed falls behind the chain tip, where it may set a different lock time.
This commit is contained in:
parent
c0d07dc4cb
commit
e958ff9ab5
@ -2620,13 +2620,15 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nC
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, interfaces::Chain::Lock& locked_chain)
|
static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, const uint256& block_hash)
|
||||||
{
|
{
|
||||||
if (chain.isInitialBlockDownload()) {
|
if (chain.isInitialBlockDownload()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
constexpr int64_t MAX_ANTI_FEE_SNIPING_TIP_AGE = 8 * 60 * 60; // in seconds
|
constexpr int64_t MAX_ANTI_FEE_SNIPING_TIP_AGE = 8 * 60 * 60; // in seconds
|
||||||
if (locked_chain.getBlockTime(*locked_chain.getHeight()) < (GetTime() - MAX_ANTI_FEE_SNIPING_TIP_AGE)) {
|
int64_t block_time;
|
||||||
|
CHECK_NONFATAL(chain.findBlock(block_hash, FoundBlock().time(block_time)));
|
||||||
|
if (block_time < (GetTime() - MAX_ANTI_FEE_SNIPING_TIP_AGE)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -2636,9 +2638,8 @@ static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, interfaces::Cha
|
|||||||
* Return a height-based locktime for new transactions (uses the height of the
|
* Return a height-based locktime for new transactions (uses the height of the
|
||||||
* current chain tip unless we are not synced with the current chain
|
* current chain tip unless we are not synced with the current chain
|
||||||
*/
|
*/
|
||||||
static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, interfaces::Chain::Lock& locked_chain)
|
static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, const uint256& block_hash, int block_height)
|
||||||
{
|
{
|
||||||
uint32_t const height = locked_chain.getHeight().get_value_or(-1);
|
|
||||||
uint32_t locktime;
|
uint32_t locktime;
|
||||||
// Discourage fee sniping.
|
// Discourage fee sniping.
|
||||||
//
|
//
|
||||||
@ -2660,8 +2661,8 @@ static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, interface
|
|||||||
// enough, that fee sniping isn't a problem yet, but by implementing a fix
|
// enough, that fee sniping isn't a problem yet, but by implementing a fix
|
||||||
// now we ensure code won't be written that makes assumptions about
|
// now we ensure code won't be written that makes assumptions about
|
||||||
// nLockTime that preclude a fix later.
|
// nLockTime that preclude a fix later.
|
||||||
if (IsCurrentForAntiFeeSniping(chain, locked_chain)) {
|
if (IsCurrentForAntiFeeSniping(chain, block_hash)) {
|
||||||
locktime = height;
|
locktime = block_height;
|
||||||
|
|
||||||
// Secondly occasionally randomly pick a nLockTime even further back, so
|
// Secondly occasionally randomly pick a nLockTime even further back, so
|
||||||
// that transactions that are delayed after signing for whatever reason,
|
// that transactions that are delayed after signing for whatever reason,
|
||||||
@ -2675,7 +2676,6 @@ static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, interface
|
|||||||
// unique "nLockTime fingerprint", set nLockTime to a constant.
|
// unique "nLockTime fingerprint", set nLockTime to a constant.
|
||||||
locktime = 0;
|
locktime = 0;
|
||||||
}
|
}
|
||||||
assert(locktime <= height);
|
|
||||||
assert(locktime < LOCKTIME_THRESHOLD);
|
assert(locktime < LOCKTIME_THRESHOLD);
|
||||||
return locktime;
|
return locktime;
|
||||||
}
|
}
|
||||||
@ -2735,9 +2735,6 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
|
|||||||
}
|
}
|
||||||
|
|
||||||
CMutableTransaction txNew;
|
CMutableTransaction txNew;
|
||||||
|
|
||||||
txNew.nLockTime = GetLocktimeForNewTransaction(chain(), locked_chain);
|
|
||||||
|
|
||||||
FeeCalculation feeCalc;
|
FeeCalculation feeCalc;
|
||||||
CAmount nFeeNeeded;
|
CAmount nFeeNeeded;
|
||||||
int nBytes;
|
int nBytes;
|
||||||
@ -2745,6 +2742,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
|
|||||||
std::set<CInputCoin> setCoins;
|
std::set<CInputCoin> setCoins;
|
||||||
auto locked_chain = chain().lock();
|
auto locked_chain = chain().lock();
|
||||||
LOCK(cs_wallet);
|
LOCK(cs_wallet);
|
||||||
|
txNew.nLockTime = GetLocktimeForNewTransaction(chain(), GetLastBlockHash(), GetLastBlockHeight());
|
||||||
{
|
{
|
||||||
std::vector<COutput> vAvailableCoins;
|
std::vector<COutput> vAvailableCoins;
|
||||||
AvailableCoins(*locked_chain, vAvailableCoins, true, &coin_control, 1, MAX_MONEY, MAX_MONEY, 0);
|
AvailableCoins(*locked_chain, vAvailableCoins, true, &coin_control, 1, MAX_MONEY, MAX_MONEY, 0);
|
||||||
|
Loading…
Reference in New Issue
Block a user