Move reused async rpc send logic to separate file

This commit is contained in:
Eirik0 2019-05-10 12:20:52 -06:00
parent 3220d99360
commit a7a52d2424
6 changed files with 60 additions and 98 deletions

View File

@ -216,6 +216,7 @@ BITCOIN_CORE_H = \
utiltime.h \
validationinterface.h \
version.h \
wallet/asyncrpcoperation_common.h \
wallet/asyncrpcoperation_mergetoaddress.h \
wallet/asyncrpcoperation_saplingmigration.h \
wallet/asyncrpcoperation_sendmany.h \
@ -306,6 +307,7 @@ libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_wallet_a_SOURCES = \
zcbenchmarks.cpp \
zcbenchmarks.h \
wallet/asyncrpcoperation_common.cpp \
wallet/asyncrpcoperation_mergetoaddress.cpp \
wallet/asyncrpcoperation_saplingmigration.cpp \
wallet/asyncrpcoperation_sendmany.cpp \

View File

@ -0,0 +1,22 @@
#include "asyncrpcoperation_common.h"
#include "core_io.h"
#include "init.h"
#include "wallet.h"
UniValue SendTransaction(CTransaction& tx, bool testmode) {
UniValue o(UniValue::VOBJ);
// Send the transaction
if (!testmode) {
CWalletTx wtx(pwalletMain, tx);
pwalletMain->CommitTransaction(wtx, boost::none);
o.push_back(Pair("txid", tx.GetHash().ToString()));
} else {
// Test mode does not send the transaction to the network.
UniValue o(UniValue::VOBJ);
o.push_back(Pair("test", 1));
o.push_back(Pair("txid", tx.GetHash().ToString()));
o.push_back(Pair("hex", EncodeHexTx(tx)));
}
return o;
}

View File

@ -0,0 +1,13 @@
// Copyright (c) 2019 The Zcash developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef ASYNCRPCOPERATION_COMMON_H
#define ASYNCRPCOPERATION_COMMON_H
#include "primitives/transaction.h"
#include "univalue.h"
UniValue SendTransaction(CTransaction& tx, bool testmode);
#endif /* ASYNCRPCOPERATION_COMMON_H */

View File

@ -5,6 +5,7 @@
#include "asyncrpcoperation_mergetoaddress.h"
#include "amount.h"
#include "asyncrpcoperation_common.h"
#include "asyncrpcqueue.h"
#include "core_io.h"
#include "init.h"
@ -355,26 +356,11 @@ bool AsyncRPCOperation_mergetoaddress::main_impl()
builder_.AddSaplingOutput(ovk.get(), *saplingPaymentAddress, sendAmount, hexMemo);
}
// Build the transaction
tx_ = builder_.Build().GetTxOrThrow();
// Send the transaction
if (!testmode) {
CWalletTx wtx(pwalletMain, tx_);
pwalletMain->CommitTransaction(wtx, boost::none);
UniValue o(UniValue::VOBJ);
o.push_back(Pair("txid", tx_.GetHash().ToString()));
set_result(o);
} else {
// Test mode does not send the transaction to the network.
UniValue o(UniValue::VOBJ);
o.push_back(Pair("test", 1));
o.push_back(Pair("txid", tx_.GetHash().ToString()));
o.push_back(Pair("hex", EncodeHexTx(tx_)));
set_result(o);
}
UniValue sendResult = SendTransaction(tx_, testmode);
set_result(sendResult);
return true;
}
@ -769,21 +755,9 @@ void AsyncRPCOperation_mergetoaddress::sign_send_raw_transaction(UniValue obj)
CTransaction tx;
stream >> tx;
tx_ = tx;
// Send the signed transaction
if (!testmode) {
CWalletTx wtx(pwalletMain, tx_);
pwalletMain->CommitTransaction(wtx, boost::none);
UniValue o(UniValue::VOBJ);
o.push_back(Pair("txid", tx_.GetHash().ToString()));
set_result(o);
} else {
// Test mode does not send the transaction to the network.
UniValue o(UniValue::VOBJ);
o.push_back(Pair("test", 1));
o.push_back(Pair("txid", tx_.GetHash().ToString()));
o.push_back(Pair("hex", signedtxn));
set_result(o);
}
UniValue sendResult = SendTransaction(tx_, testmode);
set_result(sendResult);
}

View File

@ -3,8 +3,10 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "asyncrpcoperation_sendmany.h"
#include "asyncrpcqueue.h"
#include "amount.h"
#include "asyncrpcoperation_common.h"
#include "asyncrpcqueue.h"
#include "consensus/upgrades.h"
#include "core_io.h"
#include "init.h"
@ -460,22 +462,8 @@ bool AsyncRPCOperation_sendmany::main_impl() {
// Build the transaction
tx_ = builder_.Build().GetTxOrThrow();
// Send the transaction
if (!testmode) {
CWalletTx wtx(pwalletMain, tx_);
pwalletMain->CommitTransaction(wtx, boost::none);
UniValue o(UniValue::VOBJ);
o.push_back(Pair("txid", tx_.GetHash().ToString()));
set_result(o);
} else {
// Test mode does not send the transaction to the network.
UniValue o(UniValue::VOBJ);
o.push_back(Pair("test", 1));
o.push_back(Pair("txid", tx_.GetHash().ToString()));
o.push_back(Pair("hex", EncodeHexTx(tx_)));
set_result(o);
}
UniValue sendResult = SendTransaction(tx_, testmode);
set_result(sendResult);
return true;
}
@ -936,21 +924,9 @@ void AsyncRPCOperation_sendmany::sign_send_raw_transaction(UniValue obj)
CTransaction tx;
stream >> tx;
tx_ = tx;
// Send the signed transaction
if (!testmode) {
CWalletTx wtx(pwalletMain, tx_);
pwalletMain->CommitTransaction(wtx, boost::none);
UniValue o(UniValue::VOBJ);
o.push_back(Pair("txid", tx_.GetHash().ToString()));
set_result(o);
} else {
// Test mode does not send the transaction to the network.
UniValue o(UniValue::VOBJ);
o.push_back(Pair("test", 1));
o.push_back(Pair("txid", tx_.GetHash().ToString()));
o.push_back(Pair("hex", signedtxn));
set_result(o);
}
UniValue sendResult = SendTransaction(tx_, testmode);
set_result(sendResult);
}

View File

@ -2,8 +2,11 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "asyncrpcqueue.h"
#include "asyncrpcoperation_shieldcoinbase.h"
#include "amount.h"
#include "asyncrpcoperation_common.h"
#include "asyncrpcqueue.h"
#include "consensus/upgrades.h"
#include "core_io.h"
#include "init.h"
@ -31,8 +34,6 @@
#include <thread>
#include <string>
#include "asyncrpcoperation_shieldcoinbase.h"
using namespace libzcash;
static int find_output(UniValue obj, int n) {
@ -260,22 +261,8 @@ bool ShieldToAddress::operator()(const libzcash::SaplingPaymentAddress &zaddr) c
// Build the transaction
m_op->tx_ = m_op->builder_.Build().GetTxOrThrow();
// Send the transaction
if (!m_op->testmode) {
CWalletTx wtx(pwalletMain, m_op->tx_);
pwalletMain->CommitTransaction(wtx, boost::none);
UniValue o(UniValue::VOBJ);
o.push_back(Pair("txid", m_op->tx_.GetHash().ToString()));
m_op->set_result(o);
} else {
// Test mode does not send the transaction to the network.
UniValue o(UniValue::VOBJ);
o.push_back(Pair("test", 1));
o.push_back(Pair("txid", m_op->tx_.GetHash().ToString()));
o.push_back(Pair("hex", EncodeHexTx(m_op->tx_)));
m_op->set_result(o);
}
UniValue sendResult = SendTransaction(m_op->tx_, m_op->testmode);
m_op->set_result(sendResult);
return true;
}
@ -318,21 +305,9 @@ void AsyncRPCOperation_shieldcoinbase::sign_send_raw_transaction(UniValue obj)
CTransaction tx;
stream >> tx;
tx_ = tx;
// Send the signed transaction
if (!testmode) {
CWalletTx wtx(pwalletMain, tx_);
pwalletMain->CommitTransaction(wtx, boost::none);
UniValue o(UniValue::VOBJ);
o.push_back(Pair("txid", tx_.GetHash().ToString()));
set_result(o);
} else {
// Test mode does not send the transaction to the network.
UniValue o(UniValue::VOBJ);
o.push_back(Pair("test", 1));
o.push_back(Pair("txid", tx_.GetHash().ToString()));
o.push_back(Pair("hex", signedtxn));
set_result(o);
}
UniValue sendResult = SendTransaction(tx_, testmode);
set_result(sendResult);
}