Skip to content

Commit 8c27149

Browse files
committed
net: rpc: return peer's mapped AS in getrawaddrman
This information can be used to check bucketing logic.
1 parent 62ef33a commit 8c27149

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/rpc/net.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,20 +1093,28 @@ static RPCHelpMan getaddrmaninfo()
10931093
};
10941094
}
10951095

1096-
UniValue AddrmanEntryToJSON(const AddrInfo& info)
1096+
UniValue AddrmanEntryToJSON(const AddrInfo& info, CConnman& connman)
10971097
{
10981098
UniValue ret(UniValue::VOBJ);
10991099
ret.pushKV("address", info.ToStringAddr());
1100+
const auto mapped_as{connman.GetMappedAS(info)};
1101+
if (mapped_as != 0) {
1102+
ret.pushKV("mapped_as", mapped_as);
1103+
}
11001104
ret.pushKV("port", info.GetPort());
11011105
ret.pushKV("services", (uint64_t)info.nServices);
11021106
ret.pushKV("time", int64_t{TicksSinceEpoch<std::chrono::seconds>(info.nTime)});
11031107
ret.pushKV("network", GetNetworkName(info.GetNetClass()));
11041108
ret.pushKV("source", info.source.ToStringAddr());
11051109
ret.pushKV("source_network", GetNetworkName(info.source.GetNetClass()));
1110+
const auto source_mapped_as{connman.GetMappedAS(info.source)};
1111+
if (source_mapped_as != 0) {
1112+
ret.pushKV("source_mapped_as", source_mapped_as);
1113+
}
11061114
return ret;
11071115
}
11081116

1109-
UniValue AddrmanTableToJSON(const std::vector<std::pair<AddrInfo, AddressPosition>>& tableInfos)
1117+
UniValue AddrmanTableToJSON(const std::vector<std::pair<AddrInfo, AddressPosition>>& tableInfos, CConnman& connman)
11101118
{
11111119
UniValue table(UniValue::VOBJ);
11121120
for (const auto& e : tableInfos) {
@@ -1117,7 +1125,7 @@ UniValue AddrmanTableToJSON(const std::vector<std::pair<AddrInfo, AddressPositio
11171125
// Address manager tables have unique entries so there is no advantage
11181126
// in using UniValue::pushKV, which checks if the key already exists
11191127
// in O(N). UniValue::pushKVEnd is used instead which currently is O(1).
1120-
table.pushKVEnd(key.str(), AddrmanEntryToJSON(info));
1128+
table.pushKVEnd(key.str(), AddrmanEntryToJSON(info, connman));
11211129
}
11221130
return table;
11231131
}
@@ -1133,12 +1141,14 @@ static RPCHelpMan getrawaddrman()
11331141
{RPCResult::Type::OBJ_DYN, "table", "buckets with addresses in the address manager table ( new, tried )", {
11341142
{RPCResult::Type::OBJ, "bucket/position", "the location in the address manager table (<bucket>/<position>)", {
11351143
{RPCResult::Type::STR, "address", "The address of the node"},
1144+
{RPCResult::Type::NUM, "mapped_as", /*optional=*/true, "The ASN mapped to the IP of this peer per our current ASMap"},
11361145
{RPCResult::Type::NUM, "port", "The port number of the node"},
11371146
{RPCResult::Type::STR, "network", "The network (" + Join(GetNetworkNames(), ", ") + ") of the address"},
11381147
{RPCResult::Type::NUM, "services", "The services offered by the node"},
11391148
{RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " when the node was last seen"},
11401149
{RPCResult::Type::STR, "source", "The address that relayed the address to us"},
11411150
{RPCResult::Type::STR, "source_network", "The network (" + Join(GetNetworkNames(), ", ") + ") of the source address"},
1151+
{RPCResult::Type::NUM, "source_mapped_as", /*optional=*/true, "The ASN mapped to the IP of this peer's source per our current ASMap"}
11421152
}}
11431153
}}
11441154
}
@@ -1149,10 +1159,12 @@ static RPCHelpMan getrawaddrman()
11491159
},
11501160
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
11511161
AddrMan& addrman = EnsureAnyAddrman(request.context);
1162+
NodeContext& node_context = EnsureAnyNodeContext(request.context);
1163+
CConnman& connman = EnsureConnman(node_context);
11521164

11531165
UniValue ret(UniValue::VOBJ);
1154-
ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false)));
1155-
ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true)));
1166+
ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false), connman));
1167+
ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true), connman));
11561168
return ret;
11571169
},
11581170
};

0 commit comments

Comments
 (0)