Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: add getnodeaddresses #2646

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ static const CRPCConvertParam vRPCConvertParams[] =

// Network
{ "getaddednodeinfo" , 0 },
{ "getnodeaddresses" , 0 },
{ "getblock" , 1 },
{ "getblockbynumber" , 0 },
{ "getblockbynumber" , 1 },
Expand Down
32 changes: 32 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,38 @@ UniValue addnode(const UniValue& params, bool fHelp)
return result;
}

UniValue getnodeaddresses(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() > 1)
throw runtime_error(
"getnodeaddresses [count]\n"
"\nReturn known addresses which can potentially be used to find new nodes in the network\n"
"count: How many addresses to return. Limited to the smaller of " + std::to_string(ADDRMAN_GETADDR_MAX) +
" or " + std::to_string(ADDRMAN_GETADDR_MAX_PCT) + "% of all known addresses. (default = 1)\n");
int count = 1;
if(!params[0].isNull())
count = params[0].get_int();

if (count <= 0)
throw JSONRPCError(-8, "Address count out of range");

// returns a shuffled list of CAddress
std::vector<CAddress> vAddr = addrman.GetAddr();
UniValue ret(UniValue::VARR);

int address_return_count = std::min<int>(count, vAddr.size());
for (int i = 0; i < address_return_count; ++i) {
UniValue obj(UniValue::VOBJ);
const CAddress& addr = vAddr[i];
obj.pushKV("time", (int)addr.nTime);
obj.pushKV("services", (uint64_t)addr.nServices);
obj.pushKV("address", addr.ToStringIP());
obj.pushKV("port", addr.GetPort());
ret.push_back(obj);
}
return ret;
}

UniValue getaddednodeinfo(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ static const CRPCCommand vRPCCommands[] =
{ "clearbanned", &clearbanned, cat_network },
{ "currenttime", &currenttime, cat_network },
{ "getaddednodeinfo", &getaddednodeinfo, cat_network },
{ "getnodeaddresses", &getnodeaddresses, cat_network },
{ "getbestblockhash", &getbestblockhash, cat_network },
{ "getblock", &getblock, cat_network },
{ "getblockbynumber", &getblockbynumber, cat_network },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ extern UniValue changesettings(const UniValue& params, bool fHelp);
extern UniValue clearbanned(const UniValue& params, bool fHelp);
extern UniValue currenttime(const UniValue& params, bool fHelp);
extern UniValue getaddednodeinfo(const UniValue& params, bool fHelp);
extern UniValue getnodeaddresses(const UniValue& params, bool fHelp);
extern UniValue getbestblockhash(const UniValue& params, bool fHelp);
extern UniValue getblock(const UniValue& params, bool fHelp);
extern UniValue getblockbynumber(const UniValue& params, bool fHelp);
Expand Down