refactor: pass CWallet reference to CTransactionBuilder

This commit is contained in:
Konstantin Akimov 2024-12-03 20:21:32 +07:00
parent 02948b2695
commit 43778b07f5
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524
4 changed files with 25 additions and 26 deletions

View File

@ -1482,7 +1482,7 @@ bool CCoinJoinClientSession::MakeCollateralAmounts(const CompactTallyItem& tally
return false; return false;
} }
CTransactionBuilder txBuilder(m_wallet, tallyItem); CTransactionBuilder txBuilder(*m_wallet, tallyItem);
WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::%s -- Start %s\n", __func__, txBuilder.ToString()); WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::%s -- Start %s\n", __func__, txBuilder.ToString());
@ -1645,7 +1645,7 @@ bool CCoinJoinClientSession::CreateDenominated(CAmount nBalanceToDenominate, con
return false; return false;
} }
CTransactionBuilder txBuilder(m_wallet, tallyItem); CTransactionBuilder txBuilder(*m_wallet, tallyItem);
WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::%s -- Start %s\n", __func__, txBuilder.ToString()); WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::%s -- Start %s\n", __func__, txBuilder.ToString());

View File

@ -87,15 +87,14 @@ void CKeyHolderStorage::ReturnAll()
} }
} }
CTransactionBuilderOutput::CTransactionBuilderOutput(CTransactionBuilder* pTxBuilderIn, CTransactionBuilderOutput::CTransactionBuilderOutput(CTransactionBuilder* pTxBuilderIn, CWallet& wallet, CAmount nAmountIn) :
const std::shared_ptr<CWallet>& wallet, CAmount nAmountIn) :
pTxBuilder(pTxBuilderIn), pTxBuilder(pTxBuilderIn),
dest(wallet.get()), dest(&wallet),
nAmount(nAmountIn) nAmount(nAmountIn)
{ {
assert(pTxBuilder); assert(pTxBuilder);
CTxDestination txdest; CTxDestination txdest;
LOCK(wallet->cs_wallet); LOCK(wallet.cs_wallet);
dest.GetReservedDestination(txdest, false); dest.GetReservedDestination(txdest, false);
script = ::GetScriptForDestination(txdest); script = ::GetScriptForDestination(txdest);
} }
@ -109,15 +108,15 @@ bool CTransactionBuilderOutput::UpdateAmount(const CAmount nNewAmount)
return true; return true;
} }
CTransactionBuilder::CTransactionBuilder(const std::shared_ptr<CWallet>& wallet, const CompactTallyItem& tallyItemIn) : CTransactionBuilder::CTransactionBuilder(CWallet& wallet, const CompactTallyItem& tallyItemIn) :
m_wallet(wallet), m_wallet(wallet),
dummyReserveDestination(wallet.get()), dummyReserveDestination(&wallet),
tallyItem(tallyItemIn) tallyItem(tallyItemIn)
{ {
// Generate a feerate which will be used to consider if the remainder is dust and will go into fees or not // Generate a feerate which will be used to consider if the remainder is dust and will go into fees or not
coinControl.m_discard_feerate = ::GetDiscardRate(*m_wallet); coinControl.m_discard_feerate = ::GetDiscardRate(m_wallet);
// Generate a feerate which will be used by calculations of this class and also by CWallet::CreateTransaction // Generate a feerate which will be used by calculations of this class and also by CWallet::CreateTransaction
coinControl.m_feerate = std::max(GetRequiredFeeRate(*m_wallet), m_wallet->m_pay_tx_fee); coinControl.m_feerate = std::max(GetRequiredFeeRate(m_wallet), m_wallet.m_pay_tx_fee);
// Change always goes back to origin // Change always goes back to origin
coinControl.destChange = tallyItemIn.txdest; coinControl.destChange = tallyItemIn.txdest;
// Only allow tallyItems inputs for tx creation // Only allow tallyItems inputs for tx creation
@ -132,16 +131,16 @@ CTransactionBuilder::CTransactionBuilder(const std::shared_ptr<CWallet>& wallet,
// Get a comparable dummy scriptPubKey, avoid writing/flushing to the actual wallet db // Get a comparable dummy scriptPubKey, avoid writing/flushing to the actual wallet db
CScript dummyScript; CScript dummyScript;
{ {
LOCK(m_wallet->cs_wallet); LOCK(m_wallet.cs_wallet);
WalletBatch dummyBatch(m_wallet->GetDatabase(), false); WalletBatch dummyBatch(m_wallet.GetDatabase(), false);
dummyBatch.TxnBegin(); dummyBatch.TxnBegin();
CKey secret; CKey secret;
secret.MakeNewKey(m_wallet->CanSupportFeature(FEATURE_COMPRPUBKEY)); secret.MakeNewKey(m_wallet.CanSupportFeature(FEATURE_COMPRPUBKEY));
CPubKey dummyPubkey = secret.GetPubKey(); CPubKey dummyPubkey = secret.GetPubKey();
dummyBatch.TxnAbort(); dummyBatch.TxnAbort();
dummyScript = ::GetScriptForDestination(PKHash(dummyPubkey)); dummyScript = ::GetScriptForDestination(PKHash(dummyPubkey));
// Calculate required bytes for the dummy signed tx with tallyItem's inputs only // Calculate required bytes for the dummy signed tx with tallyItem's inputs only
nBytesBase = CalculateMaximumSignedTxSize(CTransaction(dummyTx), m_wallet.get(), false); nBytesBase = CalculateMaximumSignedTxSize(CTransaction(dummyTx), &m_wallet, false);
} }
// Calculate the output size // Calculate the output size
nBytesOutput = ::GetSerializeSize(CTxOut(0, dummyScript), PROTOCOL_VERSION); nBytesOutput = ::GetSerializeSize(CTxOut(0, dummyScript), PROTOCOL_VERSION);
@ -234,12 +233,12 @@ CAmount CTransactionBuilder::GetAmountUsed() const
CAmount CTransactionBuilder::GetFee(unsigned int nBytes) const CAmount CTransactionBuilder::GetFee(unsigned int nBytes) const
{ {
CAmount nFeeCalc = coinControl.m_feerate->GetFee(nBytes); CAmount nFeeCalc = coinControl.m_feerate->GetFee(nBytes);
CAmount nRequiredFee = GetRequiredFee(*m_wallet, nBytes); CAmount nRequiredFee = GetRequiredFee(m_wallet, nBytes);
if (nRequiredFee > nFeeCalc) { if (nRequiredFee > nFeeCalc) {
nFeeCalc = nRequiredFee; nFeeCalc = nRequiredFee;
} }
if (nFeeCalc > m_wallet->m_default_max_tx_fee) { if (nFeeCalc > m_wallet.m_default_max_tx_fee) {
nFeeCalc = m_wallet->m_default_max_tx_fee; nFeeCalc = m_wallet.m_default_max_tx_fee;
} }
return nFeeCalc; return nFeeCalc;
} }
@ -274,9 +273,9 @@ bool CTransactionBuilder::Commit(bilingual_str& strResult)
CTransactionRef tx; CTransactionRef tx;
{ {
LOCK2(m_wallet->cs_wallet, cs_main); LOCK2(m_wallet.cs_wallet, cs_main);
FeeCalculation fee_calc_out; FeeCalculation fee_calc_out;
if (!m_wallet->CreateTransaction(vecSend, tx, nFeeRet, nChangePosRet, strResult, coinControl, fee_calc_out)) { if (!m_wallet.CreateTransaction(vecSend, tx, nFeeRet, nChangePosRet, strResult, coinControl, fee_calc_out)) {
return false; return false;
} }
} }
@ -313,8 +312,8 @@ bool CTransactionBuilder::Commit(bilingual_str& strResult)
} }
{ {
LOCK2(m_wallet->cs_wallet, cs_main); LOCK2(m_wallet.cs_wallet, cs_main);
m_wallet->CommitTransaction(tx, {}, {}); m_wallet.CommitTransaction(tx, {}, {});
} }
fKeepKeys = true; fKeepKeys = true;

View File

@ -54,7 +54,7 @@ class CTransactionBuilderOutput
CScript script; CScript script;
public: public:
CTransactionBuilderOutput(CTransactionBuilder* pTxBuilderIn, const std::shared_ptr<CWallet>& wallet, CAmount nAmountIn); CTransactionBuilderOutput(CTransactionBuilder* pTxBuilderIn, CWallet& wallet, CAmount nAmountIn);
CTransactionBuilderOutput(CTransactionBuilderOutput&&) = delete; CTransactionBuilderOutput(CTransactionBuilderOutput&&) = delete;
CTransactionBuilderOutput& operator=(CTransactionBuilderOutput&&) = delete; CTransactionBuilderOutput& operator=(CTransactionBuilderOutput&&) = delete;
/// Get the scriptPubKey of this output /// Get the scriptPubKey of this output
@ -77,7 +77,7 @@ public:
class CTransactionBuilder class CTransactionBuilder
{ {
/// Wallet the transaction will be build for /// Wallet the transaction will be build for
const std::shared_ptr<CWallet>& m_wallet; CWallet& m_wallet;
/// See CTransactionBuilder() for initialization /// See CTransactionBuilder() for initialization
CCoinControl coinControl; CCoinControl coinControl;
/// Dummy since we anyway use tallyItem's destination as change destination in coincontrol. /// Dummy since we anyway use tallyItem's destination as change destination in coincontrol.
@ -100,7 +100,7 @@ class CTransactionBuilder
friend class CTransactionBuilderOutput; friend class CTransactionBuilderOutput;
public: public:
CTransactionBuilder(const std::shared_ptr<CWallet>& wallet, const CompactTallyItem& tallyItemIn); CTransactionBuilder(CWallet& wallet, const CompactTallyItem& tallyItemIn);
~CTransactionBuilder(); ~CTransactionBuilder();
/// Check it would be possible to add a single output with the amount nAmount. Returns true if its possible and false if not. /// Check it would be possible to add a single output with the amount nAmount. Returns true if its possible and false if not.
bool CouldAddOutput(CAmount nAmountOutput) const EXCLUSIVE_LOCKS_REQUIRED(!cs_outputs); bool CouldAddOutput(CAmount nAmountOutput) const EXCLUSIVE_LOCKS_REQUIRED(!cs_outputs);

View File

@ -231,7 +231,7 @@ BOOST_FIXTURE_TEST_CASE(CTransactionBuilderTest, CTransactionBuilderTestSetup)
// Tests with single outpoint tallyItem // Tests with single outpoint tallyItem
{ {
CompactTallyItem tallyItem = GetTallyItem({4999}); CompactTallyItem tallyItem = GetTallyItem({4999});
CTransactionBuilder txBuilder(wallet, tallyItem); CTransactionBuilder txBuilder(*wallet, tallyItem);
BOOST_CHECK_EQUAL(txBuilder.CountOutputs(), 0); BOOST_CHECK_EQUAL(txBuilder.CountOutputs(), 0);
BOOST_CHECK_EQUAL(txBuilder.GetAmountInitial(), tallyItem.nAmount); BOOST_CHECK_EQUAL(txBuilder.GetAmountInitial(), tallyItem.nAmount);
@ -268,7 +268,7 @@ BOOST_FIXTURE_TEST_CASE(CTransactionBuilderTest, CTransactionBuilderTestSetup)
// Tests with multiple outpoint tallyItem // Tests with multiple outpoint tallyItem
{ {
CompactTallyItem tallyItem = GetTallyItem({10000, 20000, 30000, 40000, 50000}); CompactTallyItem tallyItem = GetTallyItem({10000, 20000, 30000, 40000, 50000});
CTransactionBuilder txBuilder(wallet, tallyItem); CTransactionBuilder txBuilder(*wallet, tallyItem);
std::vector<CTransactionBuilderOutput*> vecOutputs; std::vector<CTransactionBuilderOutput*> vecOutputs;
bilingual_str strResult; bilingual_str strResult;