Remove CWallet member from WalletTxBuilder

This resolves a conflict where most usage is `const`, but some modifies the
wallet. Previously it held a const member and then used `pwalletMain` directly
for the mutating calls. This now passes `CWallet` explicitly where necessary,
using `const` when possible.

This also benefits a follow-up PR (#6408) that introduces locking, which also
mutates the wallet.
This commit is contained in:
Greg Pfeil 2023-03-22 15:34:18 -06:00
parent 33d9ff3d24
commit effbc33276
No known key found for this signature in database
GPG Key ID: 1193ACD196ED61F2
6 changed files with 22 additions and 14 deletions

View File

@ -138,9 +138,10 @@ void AsyncRPCOperation_sendmany::main() {
//
// At least #4 differs from the Rust transaction builder.
uint256 AsyncRPCOperation_sendmany::main_impl() {
auto spendable = builder_.FindAllSpendableInputs(ztxoSelector_, mindepth_);
auto spendable = builder_.FindAllSpendableInputs(*pwalletMain, ztxoSelector_, mindepth_);
auto preparedTx = builder_.PrepareTransaction(
*pwalletMain,
ztxoSelector_,
spendable,
recipients_,

View File

@ -284,7 +284,7 @@ TEST(WalletRPCTests, RPCZsendmanyTaddrToSapling)
pwalletMain->LoadWalletTx(wtx);
// Context that z_sendmany requires
auto builder = WalletTxBuilder(Params(), *pwalletMain, minRelayTxFee);
auto builder = WalletTxBuilder(Params(), minRelayTxFee);
mtx = CreateNewContextualCMutableTransaction(consensusParams, nextBlockHeight, false);
// we need AllowFullyTransparent because the transaction will result

View File

@ -5016,7 +5016,7 @@ UniValue z_sendmany(const UniValue& params, bool fHelp)
// Create operation and add to global queue
auto nAnchorDepth = std::min((unsigned int) nMinDepth, nAnchorConfirmations);
WalletTxBuilder builder(Params(), *pwalletMain, minRelayTxFee);
WalletTxBuilder builder(Params(), minRelayTxFee);
std::shared_ptr<AsyncRPCQueue> q = getAsyncRPCQueue();
std::shared_ptr<AsyncRPCOperation> operation(

View File

@ -1248,7 +1248,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
// confirm this.
TransparentCoinbasePolicy::Allow,
false).value();
WalletTxBuilder builder(Params(), *pwalletMain, minRelayTxFee);
WalletTxBuilder builder(Params(), minRelayTxFee);
std::vector<Payment> recipients = { Payment(zaddr1, 100*COIN, Memo::FromHexOrThrow("DEADBEEF")) };
TransactionStrategy strategy(PrivacyPolicy::AllowRevealedSenders);
std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany(std::move(builder), selector, recipients, 1, 1, strategy));
@ -1265,7 +1265,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
true,
TransparentCoinbasePolicy::Disallow,
false).value();
WalletTxBuilder builder(Params(), *pwalletMain, minRelayTxFee);
WalletTxBuilder builder(Params(), minRelayTxFee);
std::vector<Payment> recipients = { Payment(taddr1, 100*COIN, Memo::FromHexOrThrow("DEADBEEF")) };
TransactionStrategy strategy(PrivacyPolicy::AllowRevealedRecipients);
std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany(std::move(builder), selector, recipients, 1, 1, strategy));

View File

@ -13,6 +13,7 @@ int GetAnchorHeight(const CChain& chain, uint32_t anchorConfirmations)
}
PrepareTransactionResult WalletTxBuilder::PrepareTransaction(
CWallet& wallet,
const ZTXOSelector& selector,
SpendableInputs& spendable,
const std::vector<Payment>& payments,
@ -24,7 +25,7 @@ PrepareTransactionResult WalletTxBuilder::PrepareTransaction(
assert(fee < MAX_MONEY);
int anchorHeight = GetAnchorHeight(chain, anchorConfirmations);
auto selected = ResolveInputsAndPayments(selector, spendable, payments, chain, strategy, fee, anchorHeight);
auto selected = ResolveInputsAndPayments(wallet, selector, spendable, payments, chain, strategy, fee, anchorHeight);
if (std::holds_alternative<InputSelectionError>(selected)) {
return std::get<InputSelectionError>(selected);
}
@ -81,7 +82,7 @@ PrepareTransactionResult WalletTxBuilder::PrepareTransaction(
auto changeAddressForTransparentSelector = [&](const std::set<ReceiverType>& receiverTypes) {
return addChangePayment(
pwalletMain->GenerateChangeAddressForAccount(
wallet.GenerateChangeAddressForAccount(
sendFromAccount,
getAllowedChangePools(receiverTypes)));
};
@ -93,7 +94,7 @@ PrepareTransactionResult WalletTxBuilder::PrepareTransaction(
return addChangePayment(
sendFromAccount == ZCASH_LEGACY_ACCOUNT
? addr
: pwalletMain->GenerateChangeAddressForAccount(
: wallet.GenerateChangeAddressForAccount(
sendFromAccount,
getAllowedChangePools({ReceiverType::Sapling})));
};
@ -126,7 +127,7 @@ PrepareTransactionResult WalletTxBuilder::PrepareTransaction(
return changeAddressForSaplingAddress(fvk.DefaultAddress());
},
[&](const libzcash::UnifiedAddress& ua) -> ChangeAddress {
auto zufvk = pwalletMain->GetUFVKForAddress(ua);
auto zufvk = wallet.GetUFVKForAddress(ua);
assert(zufvk.has_value());
return changeAddressForZUFVK(zufvk.value(), ua.GetKnownReceiverTypes());
},
@ -137,14 +138,14 @@ PrepareTransactionResult WalletTxBuilder::PrepareTransaction(
},
[&](const AccountZTXOPattern& acct) -> ChangeAddress {
return addChangePayment(
pwalletMain->GenerateChangeAddressForAccount(
wallet.GenerateChangeAddressForAccount(
acct.GetAccountId(),
getAllowedChangePools(acct.GetReceiverTypes())));
}
}, selector.GetPattern());
}
auto ovks = SelectOVKs(selector, spendable);
auto ovks = SelectOVKs(wallet, selector, spendable);
return TransactionEffects(
anchorConfirmations,
@ -169,6 +170,7 @@ CAmount WalletTxBuilder::DefaultDustThreshold() const {
}
SpendableInputs WalletTxBuilder::FindAllSpendableInputs(
const CWallet& wallet,
const ZTXOSelector& selector,
int32_t minDepth) const
{
@ -177,6 +179,7 @@ SpendableInputs WalletTxBuilder::FindAllSpendableInputs(
}
InputSelectionResult WalletTxBuilder::ResolveInputsAndPayments(
const CWallet& wallet,
const ZTXOSelector& selector,
SpendableInputs& spendableMut,
const std::vector<Payment>& payments,
@ -370,6 +373,7 @@ GetOVKsForUFVK(const UnifiedFullViewingKey& ufvk, const SpendableInputs& spendab
}
std::pair<uint256, uint256> WalletTxBuilder::SelectOVKs(
const CWallet& wallet,
const ZTXOSelector& selector,
const SpendableInputs& spendable) const
{

View File

@ -305,7 +305,6 @@ typedef std::variant<
class WalletTxBuilder {
private:
const CChainParams& params;
const CWallet& wallet;
CFeeRate minRelayFee;
uint32_t maxOrchardActions;
@ -320,6 +319,7 @@ private:
* and the requested transaction strategy.
*/
InputSelectionResult ResolveInputsAndPayments(
const CWallet& wallet,
const ZTXOSelector& selector,
SpendableInputs& spendable,
const std::vector<Payment>& payments,
@ -332,18 +332,21 @@ private:
* the spendable inputs.
*/
std::pair<uint256, uint256> SelectOVKs(
const CWallet& wallet,
const ZTXOSelector& selector,
const SpendableInputs& spendable) const;
public:
WalletTxBuilder(const CChainParams& params, const CWallet& wallet, CFeeRate minRelayFee):
params(params), wallet(wallet), minRelayFee(minRelayFee), maxOrchardActions(nOrchardActionLimit) {}
WalletTxBuilder(const CChainParams& params, CFeeRate minRelayFee):
params(params), minRelayFee(minRelayFee), maxOrchardActions(nOrchardActionLimit) {}
SpendableInputs FindAllSpendableInputs(
const CWallet& wallet,
const ZTXOSelector& selector,
int32_t minDepth) const;
PrepareTransactionResult PrepareTransaction(
CWallet& wallet,
const ZTXOSelector& selector,
SpendableInputs& spendable,
const std::vector<Payment>& payments,