From e6f4f895d5e42feaf7bfa5f41e80292aaa73cd7d Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Tue, 17 Sep 2019 18:28:03 -0400 Subject: Pass NodeContext, ConnMan, BanMan references more places So g_connman and g_banman globals can be removed next commit. --- src/node/context.cpp | 10 ++++++++++ src/node/context.h | 17 ++++++++++++++++- src/node/transaction.cpp | 3 ++- src/node/transaction.h | 5 ++++- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/node/context.cpp (limited to 'src/node') diff --git a/src/node/context.cpp b/src/node/context.cpp new file mode 100644 index 000000000..98cb061f1 --- /dev/null +++ b/src/node/context.cpp @@ -0,0 +1,10 @@ +// Copyright (c) 2019 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include + +NodeContext::NodeContext() {} +NodeContext::~NodeContext() {} diff --git a/src/node/context.h b/src/node/context.h index 3dd90ba96..f98550105 100644 --- a/src/node/context.h +++ b/src/node/context.h @@ -13,11 +13,26 @@ class Chain; class ChainClient; } // namespace interfaces -//! Pointers to interfaces used during init and destroyed on shutdown. +//! NodeContext struct containing references to chain state and connection +//! state. +//! +//! This is used by init, rpc, and test code to pass object references around +//! without needing to declare the same variables and parameters repeatedly, or +//! to use globals. More variables could be added to this struct (particularly +//! references to validation and mempool objects) to eliminate use of globals +//! and make code more modular and testable. The struct isn't intended to have +//! any member functions. It should just be a collection of references that can +//! be used without pulling in unwanted dependencies or functionality. struct NodeContext { std::unique_ptr chain; std::vector> chain_clients; + + //! Declare default constructor and destructor that are not inline, so code + //! instantiating the NodeContext struct doesn't need to #include class + //! definitions for all the unique_ptr members. + NodeContext(); + ~NodeContext(); }; #endif // BITCOIN_NODE_CONTEXT_H diff --git a/src/node/transaction.cpp b/src/node/transaction.cpp index 7783671a6..2577c2559 100644 --- a/src/node/transaction.cpp +++ b/src/node/transaction.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -13,7 +14,7 @@ #include -TransactionError BroadcastTransaction(const CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback) +TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback) { // BroadcastTransaction can be called by either sendrawtransaction RPC or wallet RPCs. // g_connman is assigned both before chain clients and before RPC server is accepting calls, diff --git a/src/node/transaction.h b/src/node/transaction.h index a3e56544a..35873d837 100644 --- a/src/node/transaction.h +++ b/src/node/transaction.h @@ -9,6 +9,8 @@ #include #include +struct NodeContext; + /** * Submit a transaction to the mempool and (optionally) relay it to all P2P peers. * @@ -18,6 +20,7 @@ * NOT be set while cs_main, cs_mempool or cs_wallet are held to avoid * deadlock. * + * @param[in] node reference to node context * @param[in] tx the transaction to broadcast * @param[out] &err_string reference to std::string to fill with error string if available * @param[in] max_tx_fee reject txs with fees higher than this (if 0, accept any fee) @@ -25,6 +28,6 @@ * @param[in] wait_callback, wait until callbacks have been processed to avoid stale result due to a sequentially RPC. * return error */ -NODISCARD TransactionError BroadcastTransaction(CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback); +NODISCARD TransactionError BroadcastTransaction(NodeContext& node, CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback); #endif // BITCOIN_NODE_TRANSACTION_H -- cgit v1.2.3