diff options
author | Ryan Ofsky <ryan@ofsky.org> | 2022-07-21 08:30:39 -0400 |
---|---|---|
committer | Ryan Ofsky <ryan@ofsky.org> | 2022-08-03 07:33:01 -0400 |
commit | a23cca56c0a7f4a267915b4beba3af3454c51603 (patch) | |
tree | 5d85193889f5a4552a568cfd2b472670c2c6d513 /src/wallet/rpc | |
parent | 4a4289e2c98cfbc51b05716f21065838afed80f6 (diff) | |
download | bitcoin-a23cca56c0a7f4a267915b4beba3af3454c51603.tar.xz |
refactor: Replace BResult with util::Result
Rename `BResult` class to `util::Result` and update the class interface to be
more compatible with `std::optional` and with a full-featured result class
implemented in https://github.com/bitcoin/bitcoin/pull/25665. Motivation for
this change is to update existing `BResult` usages now so they don't have to
change later when more features are added in #25665.
This change makes the following improvements originally implemented in #25665:
- More explicit API. Drops potentially misleading `BResult` constructor that
treats any bilingual string argument as an error. Adds `util::Error`
constructor so it is never ambiguous when a result is being assigned an error
or non-error value.
- Better type compatibility. Supports `util::Result<bilingual_str>` return
values to hold translated messages which are not errors.
- More standard and consistent API. `util::Result` supports most of the same
operators and methods as `std::optional`. `BResult` had a less familiar
interface with `HasRes`/`GetObj`/`ReleaseObj` methods. The Result/Res/Obj
naming was also not internally consistent.
- Better code organization. Puts `src/util/` code in the `util::` namespace so
naming reflects code organization and it is obvious where the class is coming
from. Drops "B" from name because it is undocumented what it stands for
(bilingual?)
- Has unit tests.
Diffstat (limited to 'src/wallet/rpc')
-rw-r--r-- | src/wallet/rpc/addresses.cpp | 8 | ||||
-rw-r--r-- | src/wallet/rpc/spend.cpp | 6 |
2 files changed, 7 insertions, 7 deletions
diff --git a/src/wallet/rpc/addresses.cpp b/src/wallet/rpc/addresses.cpp index 2a6eb96871..148343a8b0 100644 --- a/src/wallet/rpc/addresses.cpp +++ b/src/wallet/rpc/addresses.cpp @@ -60,10 +60,10 @@ RPCHelpMan getnewaddress() auto op_dest = pwallet->GetNewDestination(output_type, label); if (!op_dest) { - throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, op_dest.GetError().original); + throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, util::ErrorString(op_dest).original); } - return EncodeDestination(op_dest.GetObj()); + return EncodeDestination(*op_dest); }, }; } @@ -107,9 +107,9 @@ RPCHelpMan getrawchangeaddress() auto op_dest = pwallet->GetNewChangeDestination(output_type); if (!op_dest) { - throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, op_dest.GetError().original); + throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, util::ErrorString(op_dest).original); } - return EncodeDestination(op_dest.GetObj()); + return EncodeDestination(*op_dest); }, }; } diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp index e27cb1139e..628bf3cc99 100644 --- a/src/wallet/rpc/spend.cpp +++ b/src/wallet/rpc/spend.cpp @@ -158,14 +158,14 @@ UniValue SendMoney(CWallet& wallet, const CCoinControl &coin_control, std::vecto constexpr int RANDOM_CHANGE_POSITION = -1; auto res = CreateTransaction(wallet, recipients, RANDOM_CHANGE_POSITION, coin_control, true); if (!res) { - throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, res.GetError().original); + throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, util::ErrorString(res).original); } - const CTransactionRef& tx = res.GetObj().tx; + const CTransactionRef& tx = res->tx; wallet.CommitTransaction(tx, std::move(map_value), {} /* orderForm */); if (verbose) { UniValue entry(UniValue::VOBJ); entry.pushKV("txid", tx->GetHash().GetHex()); - entry.pushKV("fee_reason", StringForFeeReason(res.GetObj().fee_calc.reason)); + entry.pushKV("fee_reason", StringForFeeReason(res->fee_calc.reason)); return entry; } return tx->GetHash().GetHex(); |