[wallet] Change feebumper from class to functions

Change feebumper from a stateful class into a namespace of stateless
functions.

Having the results of feebumper calls persist in an object makes process
separation between Qt and wallet awkward, because it means the feebumper object
either has to be serialized back and forth between Qt and wallet processes
between fee bump calls, or that the feebumper object needs to stay alive in the
wallet process with an object reference passed back to Qt. It's simpler just to
have fee bumper calls return their results immediately instead of storing them
in an object with an extended lifetime.

In addition to making feebumper stateless, also:

- Move LOCK calls from Qt code to feebumper
- Move TransactionCanBeBumped implementation from Qt code to feebumper
This commit is contained in:
Russell Yanofsky
2017-06-15 10:34:17 -04:00
committed by John Newbery
parent 37bdcca3c3
commit aed1d90aca
4 changed files with 114 additions and 138 deletions

View File

@@ -3125,45 +3125,50 @@ UniValue bumpfee(const JSONRPCRequest& request)
LOCK2(cs_main, pwallet->cs_wallet);
EnsureWalletIsUnlocked(pwallet);
feebumper::FeeBumper feeBump(pwallet, hash, coin_control, totalFee);
feebumper::Result res = feeBump.getResult();
if (res != feebumper::Result::OK)
{
std::vector<std::string> errors;
CAmount old_fee;
CAmount new_fee;
CMutableTransaction mtx;
feebumper::Result res = feebumper::CreateTransaction(pwallet, hash, coin_control, totalFee, errors, old_fee, new_fee, mtx);
if (res != feebumper::Result::OK) {
switch(res) {
case feebumper::Result::INVALID_ADDRESS_OR_KEY:
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, feeBump.getErrors()[0]);
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, errors[0]);
break;
case feebumper::Result::INVALID_REQUEST:
throw JSONRPCError(RPC_INVALID_REQUEST, feeBump.getErrors()[0]);
throw JSONRPCError(RPC_INVALID_REQUEST, errors[0]);
break;
case feebumper::Result::INVALID_PARAMETER:
throw JSONRPCError(RPC_INVALID_PARAMETER, feeBump.getErrors()[0]);
throw JSONRPCError(RPC_INVALID_PARAMETER, errors[0]);
break;
case feebumper::Result::WALLET_ERROR:
throw JSONRPCError(RPC_WALLET_ERROR, feeBump.getErrors()[0]);
throw JSONRPCError(RPC_WALLET_ERROR, errors[0]);
break;
default:
throw JSONRPCError(RPC_MISC_ERROR, feeBump.getErrors()[0]);
throw JSONRPCError(RPC_MISC_ERROR, errors[0]);
break;
}
}
// sign bumped transaction
if (!feeBump.signTransaction(pwallet)) {
if (!feebumper::SignTransaction(pwallet, mtx)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Can't sign transaction.");
}
// commit the bumped transaction
if(!feeBump.commit(pwallet)) {
throw JSONRPCError(RPC_WALLET_ERROR, feeBump.getErrors()[0]);
uint256 txid;
if (feebumper::CommitTransaction(pwallet, hash, std::move(mtx), errors, txid) != feebumper::Result::OK) {
throw JSONRPCError(RPC_WALLET_ERROR, errors[0]);
}
UniValue result(UniValue::VOBJ);
result.push_back(Pair("txid", feeBump.getBumpedTxId().GetHex()));
result.push_back(Pair("origfee", ValueFromAmount(feeBump.getOldFee())));
result.push_back(Pair("fee", ValueFromAmount(feeBump.getNewFee())));
UniValue errors(UniValue::VARR);
for (const std::string& err: feeBump.getErrors())
errors.push_back(err);
result.push_back(Pair("errors", errors));
result.push_back(Pair("txid", txid.GetHex()));
result.push_back(Pair("origfee", ValueFromAmount(old_fee)));
result.push_back(Pair("fee", ValueFromAmount(new_fee)));
UniValue result_errors(UniValue::VARR);
for (const std::string& error : errors) {
result_errors.push_back(error);
}
result.push_back(Pair("errors", result_errors));
return result;
}