rpc_stats.cpp raw

   1  // Copyright (c) 2016 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <rpc/server.h>
   6  #include <rpc/util.h>
   7  #include <stats/stats.h>
   8  #include <util/strencodings.h>
   9  
  10  #include <stdint.h>
  11  
  12  #include <univalue.h>
  13  
  14  static RPCHelpMan getmempoolstats()
  15  {
  16      return RPCHelpMan{"getmempoolstats",
  17                  "\nReturns the collected mempool statistics (non-linear non-interpolated samples).\n",
  18                  {},
  19                  RPCResult{
  20                      RPCResult::Type::OBJ, "", "",
  21                      {
  22                          {RPCResult::Type::NUM_TIME, "time_from", "Timestamp, first sample"},
  23                          {RPCResult::Type::NUM_TIME, "time_to", "Timestamp, last sample"},
  24                          {RPCResult::Type::ARR, "samples", "",
  25                          {
  26                              {RPCResult::Type::ARR_FIXED, "", "",
  27                              {
  28                                  {RPCResult::Type::NUM, "", "Sample time in seconds (relative to other sample times only)"},
  29                                  {RPCResult::Type::NUM, "", "Number of transactions in the memory pool"},
  30                                  {RPCResult::Type::NUM, "", "Memory usage by memory pool"},
  31                                  {RPCResult::Type::NUM, "", "Minimum fee per kB"},
  32                              }},
  33                          }},
  34                      }},
  35                  RPCExamples{
  36                      HelpExampleCli("getmempoolstats", "")
  37              + HelpExampleRpc("getmempoolstats", "")
  38                  },
  39          [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
  40  {
  41      // get stats from the core stats model
  42      uint64_t timeFrom = 0;
  43      uint64_t timeTo = 0;
  44      mempoolSamples_t samples = CStats::DefaultStats()->mempoolGetValuesInRange(timeFrom, timeTo);
  45  
  46      // use "flat" json encoding for performance reasons
  47      UniValue samplesObj(UniValue::VARR);
  48      for (struct CStatsMempoolSample& sample : samples) {
  49          UniValue singleSample(UniValue::VARR);
  50          singleSample.push_back(UniValue((uint64_t)sample.m_time_delta));
  51          singleSample.push_back(UniValue(sample.m_tx_count));
  52          singleSample.push_back(UniValue(sample.m_dyn_mem_usage));
  53          singleSample.push_back(UniValue(sample.m_min_fee_per_k));
  54          samplesObj.push_back(singleSample);
  55      }
  56  
  57      UniValue result(UniValue::VOBJ);
  58      result.pushKV("time_from", timeFrom);
  59      result.pushKV("time_to", timeTo);
  60      result.pushKV("samples", samplesObj);
  61  
  62      return result;
  63  },
  64      };
  65  }
  66  
  67  void RegisterStatsRPCCommands(CRPCTable& t)
  68  {
  69  // clang-format off
  70  static const CRPCCommand commands[] =
  71  { //  category              actor (function)
  72    //  --------------------- ------------------------
  73      { "stats",              &getmempoolstats,                    },
  74  };
  75  // clang-format on
  76      for (const auto& c : commands) {
  77          t.appendCommand(c.name, &c);
  78      }
  79  }
  80