Skip to content

Commit 83ae1ba

Browse files
committed
Merge bitcoin/bitcoin#30062: net: add ASMap info in getrawaddrman RPC
1e54d61 test: add coverage for `mapped_as` from `getrawaddrman` (brunoerg) 8c27149 net: rpc: return peer's mapped AS in getrawaddrman (brunoerg) Pull request description: This PR adds two new fields in `getrawaddrman` RPC: "mapped_as" and "source_mapped_as". These fields are used to return the ASN (Autonomous System Number) mapped to the peer and its source. With these informations we can have a better view of the bucketing logic with ASMap specially in projects like [addrman-observer](https://github.com/0xb10c/addrman-observer). ACKs for top commit: fjahr: Code review ACK 1e54d61 virtu: ACK [1e54d61](bitcoin/bitcoin@1e54d61) 0xB10C: ACK 1e54d61 glozow: ACK 1e54d61 Tree-SHA512: af86bcc7a2e69bebd3fa9eaa2e527e0758c44c0a958de7292514d5f99f8f01f5df3bae11400451268e0255f738ff3acccc77f48fe129937512f1e9d9963c4c5e
2 parents ef44726 + 1e54d61 commit 83ae1ba

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/rpc/net.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,20 +1099,28 @@ static RPCHelpMan getaddrmaninfo()
10991099
};
11001100
}
11011101

1102-
UniValue AddrmanEntryToJSON(const AddrInfo& info)
1102+
UniValue AddrmanEntryToJSON(const AddrInfo& info, CConnman& connman)
11031103
{
11041104
UniValue ret(UniValue::VOBJ);
11051105
ret.pushKV("address", info.ToStringAddr());
1106+
const auto mapped_as{connman.GetMappedAS(info)};
1107+
if (mapped_as != 0) {
1108+
ret.pushKV("mapped_as", mapped_as);
1109+
}
11061110
ret.pushKV("port", info.GetPort());
11071111
ret.pushKV("services", (uint64_t)info.nServices);
11081112
ret.pushKV("time", int64_t{TicksSinceEpoch<std::chrono::seconds>(info.nTime)});
11091113
ret.pushKV("network", GetNetworkName(info.GetNetClass()));
11101114
ret.pushKV("source", info.source.ToStringAddr());
11111115
ret.pushKV("source_network", GetNetworkName(info.source.GetNetClass()));
1116+
const auto source_mapped_as{connman.GetMappedAS(info.source)};
1117+
if (source_mapped_as != 0) {
1118+
ret.pushKV("source_mapped_as", source_mapped_as);
1119+
}
11121120
return ret;
11131121
}
11141122

1115-
UniValue AddrmanTableToJSON(const std::vector<std::pair<AddrInfo, AddressPosition>>& tableInfos)
1123+
UniValue AddrmanTableToJSON(const std::vector<std::pair<AddrInfo, AddressPosition>>& tableInfos, CConnman& connman)
11161124
{
11171125
UniValue table(UniValue::VOBJ);
11181126
for (const auto& e : tableInfos) {
@@ -1123,7 +1131,7 @@ UniValue AddrmanTableToJSON(const std::vector<std::pair<AddrInfo, AddressPositio
11231131
// Address manager tables have unique entries so there is no advantage
11241132
// in using UniValue::pushKV, which checks if the key already exists
11251133
// in O(N). UniValue::pushKVEnd is used instead which currently is O(1).
1126-
table.pushKVEnd(key.str(), AddrmanEntryToJSON(info));
1134+
table.pushKVEnd(key.str(), AddrmanEntryToJSON(info, connman));
11271135
}
11281136
return table;
11291137
}
@@ -1139,12 +1147,14 @@ static RPCHelpMan getrawaddrman()
11391147
{RPCResult::Type::OBJ_DYN, "table", "buckets with addresses in the address manager table ( new, tried )", {
11401148
{RPCResult::Type::OBJ, "bucket/position", "the location in the address manager table (<bucket>/<position>)", {
11411149
{RPCResult::Type::STR, "address", "The address of the node"},
1150+
{RPCResult::Type::NUM, "mapped_as", /*optional=*/true, "The ASN mapped to the IP of this peer per our current ASMap"},
11421151
{RPCResult::Type::NUM, "port", "The port number of the node"},
11431152
{RPCResult::Type::STR, "network", "The network (" + Join(GetNetworkNames(), ", ") + ") of the address"},
11441153
{RPCResult::Type::NUM, "services", "The services offered by the node"},
11451154
{RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " when the node was last seen"},
11461155
{RPCResult::Type::STR, "source", "The address that relayed the address to us"},
11471156
{RPCResult::Type::STR, "source_network", "The network (" + Join(GetNetworkNames(), ", ") + ") of the source address"},
1157+
{RPCResult::Type::NUM, "source_mapped_as", /*optional=*/true, "The ASN mapped to the IP of this peer's source per our current ASMap"}
11481158
}}
11491159
}}
11501160
}
@@ -1155,10 +1165,12 @@ static RPCHelpMan getrawaddrman()
11551165
},
11561166
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
11571167
AddrMan& addrman = EnsureAnyAddrman(request.context);
1168+
NodeContext& node_context = EnsureAnyNodeContext(request.context);
1169+
CConnman& connman = EnsureConnman(node_context);
11581170

11591171
UniValue ret(UniValue::VOBJ);
1160-
ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false)));
1161-
ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true)));
1172+
ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false), connman));
1173+
ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true), connman));
11621174
return ret;
11631175
},
11641176
};

test/functional/feature_asmap.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import shutil
2828

2929
from test_framework.test_framework import BitcoinTestFramework
30+
from test_framework.util import assert_equal
3031

3132
DEFAULT_ASMAP_FILENAME = 'ip_asn.map' # defined in src/init.cpp
3233
ASMAP = '../../src/test/data/asmap.raw' # path to unit test skeleton asmap
@@ -118,6 +119,14 @@ def test_asmap_health_check(self):
118119
msg = "ASMap Health Check: 4 clearnet peers are mapped to 3 ASNs with 0 peers being unmapped"
119120
with self.node.assert_debug_log(expected_msgs=[msg]):
120121
self.start_node(0, extra_args=['-asmap'])
122+
raw_addrman = self.node.getrawaddrman()
123+
asns = []
124+
for _, entries in raw_addrman.items():
125+
for _, entry in entries.items():
126+
asn = entry['mapped_as']
127+
if asn not in asns:
128+
asns.append(asn)
129+
assert_equal(len(asns), 3)
121130
os.remove(self.default_asmap)
122131

123132
def run_test(self):

0 commit comments

Comments
 (0)