CatapultServer  v0.5.0.1 (Elephant)
NetworkCensusTool.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "Tool.h"
23 #include "ToolConfigurationUtils.h"
24 #include "ToolNetworkUtils.h"
25 #include "catapult/ionet/Node.h"
27 
28 namespace catapult { namespace tools {
29 
31  template<typename TNodeInfo>
32  class NetworkCensusTool : public Tool {
33  public:
35  using NodeInfoPointer = std::shared_ptr<TNodeInfo>;
36 
39 
40  public:
42  explicit NetworkCensusTool(const std::string& censusName) : m_censusName(censusName)
43  {}
44 
45  public:
46  std::string name() const override final {
47  return "Catapult Block Chain " + m_censusName + " Tool";
48  }
49 
50  void prepareOptions(OptionsBuilder& optionsBuilder, OptionsPositional& positional) override final {
51  optionsBuilder("resources,r",
52  OptionsValue<std::string>(m_resourcesPath)->default_value(".."),
53  "the path to the resources directory");
54  positional.add("resources", -1);
55  }
56 
57  int run(const Options&) override final {
58  auto config = LoadConfiguration(m_resourcesPath);
59  auto p2pNodes = LoadPeers(m_resourcesPath, config.BlockChain.Network.Identifier);
60  auto apiNodes = LoadOptionalApiPeers(m_resourcesPath, config.BlockChain.Network.Identifier);
61 
62  MultiNodeConnector connector;
63  std::vector<NodeInfoFuture> nodeInfoFutures;
64  auto addNodeInfoFutures = [this, &connector, &nodeInfoFutures](const auto& nodes) {
65  for (const auto& node : nodes) {
66  CATAPULT_LOG(debug) << "preparing to get stats from node " << node;
67  nodeInfoFutures.push_back(this->createNodeInfoFuture(connector, node));
68  }
69  };
70 
71  addNodeInfoFutures(p2pNodes);
72  addNodeInfoFutures(apiNodes);
73 
74  auto finalFuture = thread::when_all(std::move(nodeInfoFutures)).then([this](auto&& allFutures) {
75  std::vector<NodeInfoPointer> nodeInfos;
76  for (auto& nodeInfoFuture : allFutures.get())
77  nodeInfos.push_back(nodeInfoFuture.get());
78 
79  return this->processNodeInfos(nodeInfos);
80  });
81 
82  auto result = utils::checked_cast<size_t, unsigned int>(finalFuture.get());
83  return std::min(static_cast<int>(result), 255);
84  }
85 
86  private:
88  auto pNodeInfo = std::make_shared<TNodeInfo>(node);
89  return thread::compose(connector.connect(node), [this, node, pNodeInfo, &connector](auto&& ioFuture) {
90  try {
91  auto pIo = ioFuture.get();
92  auto infoFutures = this->getNodeInfoFutures(connector.pool(), *pIo, *pNodeInfo);
93 
94  // capture pIo so that it stays alive until all dependent futures are complete
95  return thread::when_all(std::move(infoFutures)).then([pIo, pNodeInfo](auto&&) {
96  return pNodeInfo;
97  });
98  } catch (...) {
99  // suppress
100  CATAPULT_LOG(error) << node << " appears to be offline";
101  return thread::make_ready_future(NodeInfoPointer(pNodeInfo));
102  }
103  });
104  }
105 
106  private:
108  virtual std::vector<thread::future<bool>> getNodeInfoFutures(
109  thread::IoThreadPool& pool,
110  ionet::PacketIo& io,
111  TNodeInfo& nodeInfo) = 0;
112 
114  virtual size_t processNodeInfos(const std::vector<NodeInfoPointer>& nodeInfos) = 0;
115 
116  private:
117  std::string m_censusName;
118  std::string m_resourcesPath;
119  };
120 }}
catapult::tools::LoadConfiguration
config::CatapultConfiguration LoadConfiguration(const std::string &resourcesPathStr)
Loads the configuration from resourcesPath.
Definition: ToolConfigurationUtils.cpp:26
FutureUtils.h
catapult::validators::DEFINE_STATEFUL_VALIDATOR
DEFINE_STATEFUL_VALIDATOR(EligibleHarvester, [](const auto &notification, const auto &context) { cache::ImportanceView view(context.Cache.template sub< cache::AccountStateCache >());return view.canHarvest(notification.Signer, context.Height) ? ValidationResult::Success :Failure_Core_Block_Harvester_Ineligible;})
catapult::tools::NetworkCensusTool::getNodeInfoFutures
virtual std::vector< thread::future< bool > > getNodeInfoFutures(thread::IoThreadPool &pool, ionet::PacketIo &io, TNodeInfo &nodeInfo)=0
Gets all futures to fill nodeInfo using pool and io.
CATAPULT_LOG
#define CATAPULT_LOG(SEV)
Writes a log entry to the default logger with SEV severity.
Definition: Logging.h:340
catapult::tools::NetworkCensusTool::prepareOptions
void prepareOptions(OptionsBuilder &optionsBuilder, OptionsPositional &positional) override final
Prepare named (optionsBuilder) and positional options of the tool.
Definition: NetworkCensusTool.h:50
catapult::thread::future::get
T get()
Returns the result of this future and blocks until the result is available.
Definition: Future.h:50
catapult::tools::NetworkCensusTool::m_resourcesPath
std::string m_resourcesPath
Definition: NetworkCensusTool.h:118
Parser.debug
def debug(*args)
Definition: Parser.py:46
NodeRequestResult.h
ValidatorContext.h
catapult::Height
utils::BaseValue< uint64_t, Height_tag > Height
Definition: src/catapult/types.h:85
catapult::validators::Notification
model::AccountAddressNotification Notification
Definition: AddressValidator.cpp:27
catapult::tools::Options
boost::program_options::variables_map Options
Provides access to parsed options.
Definition: Options.h:34
MacroBasedEnum.h
catapult::thread::compose
auto compose(future< TSeed > &&startFuture, TCreateNextFuture createNextFuture)
Definition: FutureUtils.h:102
catapult::tools::OptionsPositional
boost::program_options::positional_options_description OptionsPositional
Definition of positional arguments.
Definition: Options.h:31
catapult::thread::IoThreadPool
Represents a thread pool that shares a single io context across multiple threads.
Definition: IoThreadPool.h:30
catapult::tools::Tool
Interface for the tools.
Definition: Tool.h:27
PeerConnectCode.h
catapult::tools::NetworkCensusTool::NodeInfoPointer
std::shared_ptr< TNodeInfo > NodeInfoPointer
A node info shared pointer.
Definition: NetworkCensusTool.h:35
ToolNetworkUtils.h
Validators.h
catapult::thread::when_all
future< std::vector< future< T > > > when_all(std::vector< future< T >> &&allFutures)
Returns a future that is signaled when all futures in allFutures complete.
Definition: FutureUtils.h:31
NetworkInfo.h
catapult::tools::MultiNodeConnector::pool
thread::IoThreadPool & pool()
Gets the underlying pool used by the connector.
Definition: ToolNetworkUtils.cpp:100
Tool.h
catapult::tools::LoadOptionalApiPeers
std::vector< ionet::Node > LoadOptionalApiPeers(const std::string &resourcesPath, model::NetworkIdentifier networkIdentifier)
Loads optional api peers configuration from resourcesPath for network networkIdentifier.
Definition: ToolConfigurationUtils.cpp:33
ToolConfigurationUtils.h
catapult::ionet::PacketIo
An interface for reading and writing packets.
Definition: PacketIo.h:31
catapult::tools::OptionsBuilder
boost::program_options::options_description_easy_init OptionsBuilder
Options builder.
Definition: Options.h:28
catapult::tools::NetworkCensusTool::m_censusName
std::string m_censusName
Definition: NetworkCensusTool.h:117
catapult::thread::future
Provides a way to access the result of an asynchronous operation.
Definition: Future.h:29
Node.h
MacroBasedEnumIncludes.h
catapult::tools::NetworkCensusTool
A base class for a tool that performs a network census by communicating with all nodes.
Definition: NetworkCensusTool.h:32
catapult::thread::make_ready_future
future< T > make_ready_future(T &&value)
Produces a future that is ready immediately and holds the given value.
Definition: Future.h:126
catapult::tools::MultiNodeConnector::connect
PacketIoFuture connect(const ionet::Node &node)
Connects to node.
Definition: ToolNetworkUtils.cpp:104
catapult::validators::ValidationResult::Success
Validation succeeded.
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::tools::LoadPeers
std::vector< ionet::Node > LoadPeers(const std::string &resourcesPath, model::NetworkIdentifier networkIdentifier)
Loads p2p peers configuration from resourcesPath for network networkIdentifier.
Definition: ToolConfigurationUtils.cpp:42
catapult::tools::MultiNodeConnector
Helper class for connecting to multiple nodes.
Definition: ToolNetworkUtils.h:53
catapult::tools::NetworkCensusTool::NetworkCensusTool
NetworkCensusTool(const std::string &censusName)
Creates a census tool with census name (censusName).
Definition: NetworkCensusTool.h:42
catapult::tools::NetworkCensusTool::name
std::string name() const override final
Returns name of the command.
Definition: NetworkCensusTool.h:46
catapult::tools::NetworkCensusTool::createNodeInfoFuture
NodeInfoFuture createNodeInfoFuture(MultiNodeConnector &connector, const ionet::Node &node)
Definition: NetworkCensusTool.h:87
catapult::ionet::Node
A node in the catapult network.
Definition: Node.h:82
catapult::tools::NetworkCensusTool::run
int run(const Options &) override final
Run the tool passing options collected from the command line.
Definition: NetworkCensusTool.h:57
catapult::tools::NetworkCensusTool::processNodeInfos
virtual size_t processNodeInfos(const std::vector< NodeInfoPointer > &nodeInfos)=0
Processes nodeInfos after all futures complete.