Bitcoin Forum
May 02, 2024, 02:52:28 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: New RPC- Paramaters are not parsed correctly  (Read 104 times)
liorko87 (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 6


View Profile
May 31, 2021, 08:22:32 AM
Merited by NotATether (2)
 #1

I wrote a new RPC and my parameters are not parsed correctly.
This is the definition of the RPCHelpMan:

Code:
return RPCHelpMan{"mycustomrpc",
                "\nchange definitions(before the RPC call returns)\n",
                {
                    {"Param1", RPCArg::Type::NUM, RPCArg::Optional::NO, ""},
                    {"Param2", RPCArg::Type::STR, RPCArg::Optional::NO, ""},
                    {"Param3", RPCArg::Type::STR, RPCArg::Optional::NO, ""},
                    {"Param4", RPCArg::Type::NUM, /* default */ ToString(1000), "How many iterations to try."}
                },
                RPCResult{
                    RPCResult::Type::ARR, "", "",
                    {
                        {RPCResult::Type::STR_HEX, "", "param"},
                    }},
                RPCExamples{
            "\nChange definitions to bitcoin core\n"
            + HelpExampleCli("mycustomrpc", "param1 \"param2\" \"param3\" ")
                },
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue

When I'm running this command from my CLI, I got this error:

Code:
error code: -1
error message:
JSON value is not an integer as expected

Can someone tell me what's wrong in the function definition?
1714661548
Hero Member
*
Offline Offline

Posts: 1714661548

View Profile Personal Message (Offline)

Ignore
1714661548
Reply with quote  #2

1714661548
Report to moderator
1714661548
Hero Member
*
Offline Offline

Posts: 1714661548

View Profile Personal Message (Offline)

Ignore
1714661548
Reply with quote  #2

1714661548
Report to moderator
In order to achieve higher forum ranks, you need both activity points and merit points.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6720


bitcoincleanup.com / bitmixlist.org


View Profile WWW
May 31, 2021, 08:25:42 AM
 #2

Check this line:

Code:
"Param4", RPCArg::Type::NUM, /* default */ ToString(1000), "How many iterations to try."

The type of this parameter is NUM but its default value is a stringified version of the number 1000.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
liorko87 (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 6


View Profile
May 31, 2021, 08:27:57 AM
 #3

I checked it in the debugger and it says it for the first parameter.
In addition when I'm changing the first parameter to string, I got another error for the third parameter:

Code:
error: Error parsing JSON: "abc"
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6720


bitcoincleanup.com / bitmixlist.org


View Profile WWW
May 31, 2021, 08:30:20 AM
 #4

Well then, where is the rest of the source code for your custom RPC since that's where the execution is happening, and not in the RPCHelpMan?

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
liorko87 (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 6


View Profile
May 31, 2021, 08:33:01 AM
 #5

This is the beginning of the function, it fails in the parsing parameters

Code:
const int64_t num_blocks{request.params[0].get_int64()};
const uint64_t max_tries{request.params[3].isNull() ? DEFAULT_MAX_TRIES : request.params[3].get_int64()};
std::string str1 = request.params[2].get_str();
CTxDestination destination = DecodeDestination(request.params[1].get_str());
...
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6720


bitcoincleanup.com / bitmixlist.org


View Profile WWW
May 31, 2021, 08:46:20 AM
 #6

Well that sounds like a JSON formatting problem. Have you verified that your client program is properly escaping double quotes and braces?

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
liorko87 (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 6


View Profile
May 31, 2021, 08:50:26 AM
 #7

I'm not trying to pass a JSON at all.
This is the CLI call from the terminal:

Code:
./bitcoin-cli --datadir=<path_to_my_datadir> mycustomrpc 1 abc abc

When I'm printing the parsed values at the beginning this the output I'm receiving:
Code:
param value: 1 param type: 3
param value: abc param type: 3
param value: abc param type: 3
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6720


bitcoincleanup.com / bitmixlist.org


View Profile WWW
May 31, 2021, 09:47:19 AM
 #8

So I inspected the source code and found this enumeration of types that corresponds to RPCArg types: https://github.com/bitcoin/bitcoin/blob/7fcf53f7b4524572d1d0c9a5fdc388e87eb02416/src/rpc/util.h#L119-L130

And "3" stands for "NUM", as they start counting from zero. That explains why your string parameters are not being parsed properly.

So to be clear, you said:

When I'm printing the parsed values at the beginning this the output I'm receiving:
Code:
param value: 1 param type: 3
param value: abc param type: 3
param value: abc param type: 3


Are you printing these values after the code block you posted? It seems so, because I don't see how get_int64() and get_str() would generate this exception. In that case, on which line does the debugger catches the error?

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
liorko87 (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 6


View Profile
May 31, 2021, 10:14:39 AM
 #9

This is the updated code:

Code:
for (int i = 0; i < request.params.size(); i++) {
        std::cout << "param value: " << request.params[i].get_str() << " param type: " <<
            request.params[i].getType() << std::endl;
    }
    std::cout << "inside function" << std:: endl;
    const int64_t num_blocks{request.params[0].get_int64()};
    const uint64_t max_tries{request.params[3].isNull() ? DEFAULT_MAX_TRIES : request.params[3].get_int64()};

For me the getType() function redirects to UniValue class at:
https://github.com/bitcoin/bitcoin/blob/master/src/univalue/include/univalue.h#L64
Type 3 is VSTR according to the VType enum
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6720


bitcoincleanup.com / bitmixlist.org


View Profile WWW
May 31, 2021, 11:06:27 AM
 #10

It appears that the bitcoin-cli client is not performing any type conversion unless you specifically code it in this file and is passing encoding everything to the JSONRPC as string. Try adding your method in the vRPCConvertParams array along with the parameter number and name like so: {"mycustomrpc", 0, Param1"}, {"mycustomrpc", 3, Param4"}.

The file src/rpc/client.cpp contains a function that calls Univalue.read() on the string value to autodetect the type.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
liorko87 (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 6


View Profile
May 31, 2021, 11:22:53 AM
 #11

I never thought even looking there!
Now he notify me about another error:

Code:
error code: -1
error message:
JSON value is not a string as expected

Do I need to sign my strings somewhere??
liorko87 (OP)
Newbie
*
Offline Offline

Activity: 29
Merit: 6


View Profile
May 31, 2021, 11:50:37 AM
 #12

Ignore my last message, the error occurred because my for loop, when I deleted it, the error disappeared.
Thank you for your kind help!
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!