Skip to content

Commit 56accfe

Browse files
committed
merge bitcoin#25721: Replace BResult with util::Result
1 parent 03939f2 commit 56accfe

26 files changed

+261
-142
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ BITCOIN_TESTS =\
162162
test/random_tests.cpp \
163163
test/ratecheck_tests.cpp \
164164
test/rest_tests.cpp \
165+
test/result_tests.cpp \
165166
test/reverselock_tests.cpp \
166167
test/rpc_tests.cpp \
167168
test/sanity_tests.cpp \

src/bench/wallet_loading.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ static void BenchUnloadWallet(std::shared_ptr<CWallet>&& wallet)
4747

4848
static void AddTx(CWallet& wallet)
4949
{
50-
const auto& dest = wallet.GetNewDestination("");
51-
assert(dest.HasRes());
52-
5350
CMutableTransaction mtx;
54-
mtx.vout.push_back({COIN, GetScriptForDestination(dest.GetObj())});
51+
mtx.vout.push_back({COIN, GetScriptForDestination(*Assert(wallet.GetNewDestination("")))});
5552
mtx.vin.push_back(CTxIn());
5653

5754
wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInactive{});

src/coinjoin/util.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ bool CTransactionBuilder::Commit(bilingual_str& strResult)
284284
LOCK2(m_wallet.cs_wallet, ::cs_main);
285285
auto ret = wallet::CreateTransaction(m_wallet, vecSend, nChangePosRet, coinControl);
286286
if (ret) {
287-
tx = ret.GetObj().tx;
288-
nFeeRet = ret.GetObj().fee;
289-
nChangePosRet = ret.GetObj().change_pos;
287+
tx = ret->tx;
288+
nFeeRet = ret->fee;
289+
nChangePosRet = ret->change_pos;
290290
} else {
291-
strResult = ret.GetError();
291+
strResult = util::ErrorString(ret);
292292
return false;
293293
}
294294
}

src/interfaces/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class Wallet
112112
virtual std::string getWalletName() = 0;
113113

114114
// Get a new address.
115-
virtual BResult<CTxDestination> getNewDestination(const std::string label) = 0;
115+
virtual util::Result<CTxDestination> getNewDestination(const std::string label) = 0;
116116

117117
//! Get public key.
118118
virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;
@@ -167,7 +167,7 @@ class Wallet
167167
virtual std::vector<COutPoint> listProTxCoins() = 0;
168168

169169
//! Create transaction.
170-
virtual BResult<CTransactionRef> createTransaction(const std::vector<wallet::CRecipient>& recipients,
170+
virtual util::Result<CTransactionRef> createTransaction(const std::vector<wallet::CRecipient>& recipients,
171171
const wallet::CCoinControl& coin_control,
172172
bool sign,
173173
int& change_pos,
@@ -360,7 +360,7 @@ class WalletLoader : public ChainClient
360360
virtual std::string getWalletDir() = 0;
361361

362362
//! Restore backup wallet
363-
virtual BResult<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) = 0;
363+
virtual util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) = 0;
364364

365365
//! Return available wallets in wallet directory.
366366
virtual std::vector<std::string> listWalletDir() = 0;

src/qt/addresstablemodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
380380
return QString();
381381
}
382382
}
383-
strAddress = EncodeDestination(op_dest.GetObj());
383+
strAddress = EncodeDestination(*op_dest);
384384
}
385385
else
386386
{

src/qt/walletcontroller.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ void RestoreWalletActivity::restore(const fs::path& backup_file, const std::stri
375375
QTimer::singleShot(0, worker(), [this, backup_file, wallet_name] {
376376
auto wallet{node().walletLoader().restoreWallet(backup_file, wallet_name, m_warning_message)};
377377

378-
m_error_message = wallet ? bilingual_str{} : wallet.GetError();
379-
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(wallet.ReleaseObj());
378+
m_error_message = util::ErrorString(wallet);
379+
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet));
380380

381381
QTimer::singleShot(0, this, &RestoreWalletActivity::finish);
382382
});

src/qt/walletmodel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
255255

256256
auto& newTx = transaction.getWtx();
257257
const auto& res = m_wallet->createTransaction(vecSend, coinControl, !wallet().privateKeysDisabled() /* sign */, nChangePosRet, nFeeRequired);
258-
newTx = res ? res.GetObj() : nullptr;
258+
newTx = res ? *res : nullptr;
259259
transaction.setTransactionFee(nFeeRequired);
260260
if (fSubtractFeeFromAmount && newTx)
261261
transaction.reassignAmounts(nChangePosRet);
@@ -266,7 +266,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
266266
{
267267
return SendCoinsReturn(AmountWithFeeExceedsBalance);
268268
}
269-
Q_EMIT message(tr("Send Coins"), QString::fromStdString(res.GetError().translated),
269+
Q_EMIT message(tr("Send Coins"), QString::fromStdString(util::ErrorString(res).translated),
270270
CClientUIInterface::MSG_ERROR);
271271
return TransactionCreationFailed;
272272
}

src/rpc/evo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,10 @@ static void FundSpecialTx(CWallet& wallet, CMutableTransaction& tx, const Specia
283283

284284
auto res = CreateTransaction(wallet, vecSend, RANDOM_CHANGE_POSITION, coinControl, /*sign=*/true, tx.vExtraPayload.size());
285285
if (!res) {
286-
throw JSONRPCError(RPC_INTERNAL_ERROR, res.GetError().original);
286+
throw JSONRPCError(RPC_INTERNAL_ERROR, util::ErrorString(res).original);
287287
}
288288

289-
const CTransactionRef& newTx = res.GetObj().tx;
289+
const CTransactionRef& newTx = res->tx;
290290
tx.vin = newTx->vin;
291291
tx.vout = newTx->vout;
292292

src/test/result_tests.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <util/result.h>
6+
7+
#include <boost/test/unit_test.hpp>
8+
9+
inline bool operator==(const bilingual_str& a, const bilingual_str& b)
10+
{
11+
return a.original == b.original && a.translated == b.translated;
12+
}
13+
14+
inline std::ostream& operator<<(std::ostream& os, const bilingual_str& s)
15+
{
16+
return os << "bilingual_str('" << s.original << "' , '" << s.translated << "')";
17+
}
18+
19+
BOOST_AUTO_TEST_SUITE(result_tests)
20+
21+
struct NoCopy {
22+
NoCopy(int n) : m_n{std::make_unique<int>(n)} {}
23+
std::unique_ptr<int> m_n;
24+
};
25+
26+
bool operator==(const NoCopy& a, const NoCopy& b)
27+
{
28+
return *a.m_n == *b.m_n;
29+
}
30+
31+
std::ostream& operator<<(std::ostream& os, const NoCopy& o)
32+
{
33+
return os << "NoCopy(" << *o.m_n << ")";
34+
}
35+
36+
util::Result<int> IntFn(int i, bool success)
37+
{
38+
if (success) return i;
39+
return util::Error{Untranslated(strprintf("int %i error.", i))};
40+
}
41+
42+
util::Result<bilingual_str> StrFn(bilingual_str s, bool success)
43+
{
44+
if (success) return s;
45+
return util::Error{strprintf(Untranslated("str %s error."), s.original)};
46+
}
47+
48+
util::Result<NoCopy> NoCopyFn(int i, bool success)
49+
{
50+
if (success) return {i};
51+
return util::Error{Untranslated(strprintf("nocopy %i error.", i))};
52+
}
53+
54+
template <typename T>
55+
void ExpectResult(const util::Result<T>& result, bool success, const bilingual_str& str)
56+
{
57+
BOOST_CHECK_EQUAL(bool(result), success);
58+
BOOST_CHECK_EQUAL(util::ErrorString(result), str);
59+
}
60+
61+
template <typename T, typename... Args>
62+
void ExpectSuccess(const util::Result<T>& result, const bilingual_str& str, Args&&... args)
63+
{
64+
ExpectResult(result, true, str);
65+
BOOST_CHECK_EQUAL(result.has_value(), true);
66+
BOOST_CHECK_EQUAL(result.value(), T{std::forward<Args>(args)...});
67+
BOOST_CHECK_EQUAL(&result.value(), &*result);
68+
}
69+
70+
template <typename T, typename... Args>
71+
void ExpectFail(const util::Result<T>& result, const bilingual_str& str)
72+
{
73+
ExpectResult(result, false, str);
74+
}
75+
76+
BOOST_AUTO_TEST_CASE(check_returned)
77+
{
78+
ExpectSuccess(IntFn(5, true), {}, 5);
79+
ExpectFail(IntFn(5, false), Untranslated("int 5 error."));
80+
ExpectSuccess(NoCopyFn(5, true), {}, 5);
81+
ExpectFail(NoCopyFn(5, false), Untranslated("nocopy 5 error."));
82+
ExpectSuccess(StrFn(Untranslated("S"), true), {}, Untranslated("S"));
83+
ExpectFail(StrFn(Untranslated("S"), false), Untranslated("str S error."));
84+
}
85+
86+
BOOST_AUTO_TEST_CASE(check_value_or)
87+
{
88+
BOOST_CHECK_EQUAL(IntFn(10, true).value_or(20), 10);
89+
BOOST_CHECK_EQUAL(IntFn(10, false).value_or(20), 20);
90+
BOOST_CHECK_EQUAL(NoCopyFn(10, true).value_or(20), 10);
91+
BOOST_CHECK_EQUAL(NoCopyFn(10, false).value_or(20), 20);
92+
BOOST_CHECK_EQUAL(StrFn(Untranslated("A"), true).value_or(Untranslated("B")), Untranslated("A"));
93+
BOOST_CHECK_EQUAL(StrFn(Untranslated("A"), false).value_or(Untranslated("B")), Untranslated("B"));
94+
}
95+
96+
BOOST_AUTO_TEST_SUITE_END()

src/test/util/wallet.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <outputtype.h>
1313
#include <script/standard.h>
1414
#ifdef ENABLE_WALLET
15+
#include <util/check.h>
1516
#include <util/translation.h>
1617
#include <wallet/wallet.h>
1718
#endif
@@ -24,10 +25,7 @@ const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqq
2425
#ifdef ENABLE_WALLET
2526
std::string getnewaddress(CWallet& w)
2627
{
27-
auto op_dest = w.GetNewDestination("");
28-
assert(op_dest.HasRes());
29-
30-
return EncodeDestination(op_dest.GetObj());
28+
return EncodeDestination(*Assert(w.GetNewDestination("")));
3129
}
3230

3331
// void importaddress(CWallet& wallet, const std::string& address)

0 commit comments

Comments
 (0)