1

Sorry for this noob question. I have wrapped some Bitcoin Core JSON RPC APIs. The tests I've made so far are working fime but now I have some issues mapping input arguments with the correct C++ data types.

For instance: bitcoin-cli help getmempoolinfo returns:

{
  "size": xxxxx,               (numeric) Current tx count
  "bytes": xxxxx,              (numeric) Sum of all virtual transaction sizes as defined in BIP 141. Differs from actual serialized size because witness data is discounted
  "usage": xxxxx,              (numeric) Total memory usage for the mempool
  "maxmempool": xxxxx,         (numeric) Maximum memory usage for the mempool
  "mempoolminfee": xxxxx       (numeric) Minimum fee rate in BTC/kB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee
  "minrelaytxfee": xxxxx       (numeric) Current minimum relay fee for transactions
}

Numeric is too generic

The file blockchain.cpp defines:

static UniValue getmempoolinfo(const JSONRPCRequest& request)
{
            RPCHelpMan{"getmempoolinfo",
                "\nReturns details on the active state of the TX memory pool.\n",
                {},
                RPCResult{
                    RPCResult::Type::OBJ, "", "",
                    {
                        {RPCResult::Type::BOOL, "loaded", "True if the mempool is fully loaded"},
                        {RPCResult::Type::NUM, "size", "Current tx count"},
                        {RPCResult::Type::NUM, "bytes", "Sum of all virtual transaction sizes as defined in BIP 141. Differs from actual serialized size because witness data is discounted"},
                        {RPCResult::Type::NUM, "usage", "Total memory usage for the mempool"},
                        {RPCResult::Type::NUM, "maxmempool", "Maximum memory usage for the mempool"},
                        {RPCResult::Type::STR_AMOUNT, "mempoolminfee", "Minimum fee rate in " + CURRENCY_UNIT + "/kB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee"},
                        {RPCResult::Type::STR_AMOUNT, "minrelaytxfee", "Current minimum relay fee for transactions"},
                        {RPCResult::Type::NUM, "unbroadcastcount", "Current number of transactions that haven't passed initial broadcast yet"}
                    }},
                RPCExamples{
                    HelpExampleCli("getmempoolinfo", "")
            + HelpExampleRpc("getmempoolinfo", "")
                },
            }.Check(request);

    return MempoolInfoToJSON(EnsureMemPool(request.context));
}

again, what C++ type is RPCResult::Type::NUM ?

0

As you can see from the definition in rpc/util.h, RPCResult::Type is just an enumeration, meaning they are numbers to represent different types of values. As the values returned by the RPC are simply strings, those numbers help describing them in the output (e.g. as 'numeric'). In the enum definition, you can find a description for the values that are less intuitive (such as STR_AMOUNT).

That said, you can map those objects to C++ types by looking at the variables that are converted to such objects.

In your example, the RPC object is built by MempoolInfoToJSON. If you look at that function (here), you will see a number of pushKV operations, which convert the original variables to the corresponding object. So, you can easily see that:

RPCResult::Type::BOOL is a bool

RPCResult::Type::NUM is an int64_t (which is a signed long int)

and so on.

Note that these JSON objects can actually be mapped to more than a type in C++: you can convert a STR to a string or a char*, and you can convert a NUM to an int or to a long, depending on its size. Similarly, a string hex can be mapped to a string or it can be converted to its numeric value. So, eventually, it's up to you to decide the actual mapping, depending on your needs.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.