Merge bitcoin/bitcoin#25218: refactor: introduce generic 'Result' class and connect it to CreateTransaction and GetNewDestination
111ea3ab71wallet: refactor GetNewDestination, use BResult (furszy)22351725bcsend: refactor CreateTransaction flow to return a BResult<CTransactionRef> (furszy)198fcca162wallet: refactor, include 'FeeCalculation' inside 'CreatedTransactionResult' (furszy)7a45c33d1fIntroduce generic 'Result' class (furszy) Pull request description: Based on a common function signature pattern that we have all around the sources: ```cpp bool doSomething(arg1, arg2, arg3, arg4, &result_obj, &error_string) { // do something... if (error) { error_string = "something bad happened"; return false; } result = goodResult; return true; } ``` Introduced a generic class `BResult` that encapsulate the function boolean result, the result object (in case of having it) and, in case of failure, the string error reason. Obtaining in this way cleaner function signatures and removing boilerplate code: ```cpp BResult<Obj> doSomething(arg1, arg2, arg3, arg4) { // do something... if (error) return "something bad happened"; return goodResult; } ``` Same cleanup applies equally to the function callers' side as well. There is no longer need to add the error string and the result object declarations before calling the function: Before: ```cpp Obj result_obj; std::string error_string; if (!doSomething(arg1, arg2, arg3, arg4, result_obj, error_string)) { LogPrintf("Error: %s", error_string); } return result_obj; ``` Now: ```cpp BResult<Obj> op_res = doSomething(arg1, arg2, arg3, arg4); if (!op_res) { LogPrintf("Error: %s", op_res.GetError()); } return op_res.GetObjResult(); ``` ### Initial Implementation: Have connected this new concept to two different flows for now: 1) The `CreateTransaction` flow. --> 7ba2b87c 2) The `GetNewDestination` flow. --> bcee0912 Happy note: even when introduced a new class into the sources, the amount of lines removed is almost equal to added ones :). Extra note: this work is an extended version (and a decoupling) of the work that is inside #24845 (which does not contain the `GetNewDestination` changes nor the inclusion of the `FeeCalculation` field inside `CreatedTransactionResult`). ACKs for top commit: achow101: ACK111ea3ab71w0xlt: reACK https://github.com/bitcoin/bitcoin/pull/25218/commits/111ea3ab711414236f8678566a7884d48619b2d8 theStack: re-ACK111ea3ab71MarcoFalke: review ACK111ea3ab71🎏 Tree-SHA512: 6d84d901a4cb923727067f25ff64542a40edd1ea84fdeac092312ac684c34e3688a52ac5eb012717d2b73f4cb742b9d78e458eb0e9cb9d6d72a916395be91f69
This commit is contained in:
21 files changed
+202
-194
No files matched your search
@@ -74,11 +74,9 @@ static void add_coin(std::vector<COutput>& coins, CWallet& wallet, const CAmount
|
||||
tx.vout.resize(nInput + 1);
|
||||
tx.vout[nInput].nValue = nValue;
|
||||
if (spendable) {
|
||||
CTxDestination dest;
|
||||
bilingual_str error;
|
||||
const bool destination_ok = wallet.GetNewDestination(OutputType::BECH32, "", dest, error);
|
||||
assert(destination_ok);
|
||||
tx.vout[nInput].scriptPubKey = GetScriptForDestination(dest);
|
||||
auto op_dest = wallet.GetNewDestination(OutputType::BECH32, "");
|
||||
assert(op_dest.HasRes());
|
||||
tx.vout[nInput].scriptPubKey = GetScriptForDestination(op_dest.GetObj());
|
||||
}
|
||||
uint256 txid = tx.GetHash();
|
||||
|
||||
|
||||
@@ -69,15 +69,14 @@ struct FuzzedWallet {
|
||||
CScript GetScriptPubKey(FuzzedDataProvider& fuzzed_data_provider)
|
||||
{
|
||||
auto type{fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES)};
|
||||
CTxDestination dest;
|
||||
bilingual_str error;
|
||||
BResult<CTxDestination> op_dest;
|
||||
if (fuzzed_data_provider.ConsumeBool()) {
|
||||
assert(wallet->GetNewDestination(type, "", dest, error));
|
||||
op_dest = wallet->GetNewDestination(type, "");
|
||||
} else {
|
||||
assert(wallet->GetNewChangeDestination(type, dest, error));
|
||||
op_dest = wallet->GetNewChangeDestination(type);
|
||||
}
|
||||
assert(error.empty());
|
||||
return GetScriptForDestination(dest);
|
||||
assert(op_dest.HasRes());
|
||||
return GetScriptForDestination(op_dest.GetObj());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -28,19 +28,18 @@ BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)
|
||||
auto check_tx = [&wallet](CAmount leftover_input_amount) {
|
||||
CRecipient recipient{GetScriptForRawPubKey({}), 50 * COIN - leftover_input_amount, true /* subtract fee */};
|
||||
constexpr int RANDOM_CHANGE_POSITION = -1;
|
||||
bilingual_str error;
|
||||
CCoinControl coin_control;
|
||||
coin_control.m_feerate.emplace(10000);
|
||||
coin_control.fOverrideFeeRate = true;
|
||||
// We need to use a change type with high cost of change so that the leftover amount will be dropped to fee instead of added as a change output
|
||||
coin_control.m_change_type = OutputType::LEGACY;
|
||||
FeeCalculation fee_calc;
|
||||
std::optional<CreatedTransactionResult> txr = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, error, coin_control, fee_calc);
|
||||
BOOST_CHECK(txr.has_value());
|
||||
BOOST_CHECK_EQUAL(txr->tx->vout.size(), 1);
|
||||
BOOST_CHECK_EQUAL(txr->tx->vout[0].nValue, recipient.nAmount + leftover_input_amount - txr->fee);
|
||||
BOOST_CHECK_GT(txr->fee, 0);
|
||||
return txr->fee;
|
||||
auto res = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, coin_control);
|
||||
BOOST_CHECK(res);
|
||||
const auto& txr = res.GetObj();
|
||||
BOOST_CHECK_EQUAL(txr.tx->vout.size(), 1);
|
||||
BOOST_CHECK_EQUAL(txr.tx->vout[0].nValue, recipient.nAmount + leftover_input_amount - txr.fee);
|
||||
BOOST_CHECK_GT(txr.fee, 0);
|
||||
return txr.fee;
|
||||
};
|
||||
|
||||
// Send full input amount to recipient, check that only nonzero fee is
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
#include <wallet/wallet.h>
|
||||
|
||||
#include <any>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <stdint.h>
|
||||
@@ -13,7 +12,6 @@
|
||||
#include <interfaces/chain.h>
|
||||
#include <key_io.h>
|
||||
#include <node/blockstorage.h>
|
||||
#include <node/context.h>
|
||||
#include <policy/policy.h>
|
||||
#include <rpc/server.h>
|
||||
#include <test/util/logging.h>
|
||||
@@ -536,14 +534,12 @@ public:
|
||||
CWalletTx& AddTx(CRecipient recipient)
|
||||
{
|
||||
CTransactionRef tx;
|
||||
bilingual_str error;
|
||||
CCoinControl dummy;
|
||||
FeeCalculation fee_calc_out;
|
||||
{
|
||||
constexpr int RANDOM_CHANGE_POSITION = -1;
|
||||
std::optional<CreatedTransactionResult> txr = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, error, dummy, fee_calc_out);
|
||||
BOOST_CHECK(txr.has_value());
|
||||
tx = txr->tx;
|
||||
auto res = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, dummy);
|
||||
BOOST_CHECK(res);
|
||||
tx = res.GetObj().tx;
|
||||
}
|
||||
wallet->CommitTransaction(tx, {}, {});
|
||||
CMutableTransaction blocktx;
|
||||
@@ -629,9 +625,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
|
||||
wallet->SetMinVersion(FEATURE_LATEST);
|
||||
wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
|
||||
BOOST_CHECK(!wallet->TopUpKeyPool(1000));
|
||||
CTxDestination dest;
|
||||
bilingual_str error;
|
||||
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error));
|
||||
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, ""));
|
||||
}
|
||||
{
|
||||
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
|
||||
@@ -639,9 +633,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
|
||||
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
|
||||
wallet->SetMinVersion(FEATURE_LATEST);
|
||||
wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
|
||||
CTxDestination dest;
|
||||
bilingual_str error;
|
||||
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error));
|
||||
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, ""));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user