mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-15 12:19:46 +01:00
refactor: Use span of bytes for asmap
This prevents holding the asmap data in memory twice. Also modernizes the logging in util/asmap.cpp (LogPrintf -> LogInfo).
This commit is contained in:
parent
79bbb381a1
commit
6e97b04d19
15 changed files with 162 additions and 154 deletions
|
@ -24,7 +24,7 @@
|
|||
static constexpr size_t NUM_SOURCES = 64;
|
||||
static constexpr size_t NUM_ADDRESSES_PER_SOURCE = 256;
|
||||
|
||||
static NetGroupManager EMPTY_NETGROUPMAN{std::vector<bool>()};
|
||||
static NetGroupManager EMPTY_NETGROUPMAN{NetGroupManager::NoAsmap()};
|
||||
static constexpr uint32_t ADDRMAN_CONSISTENCY_CHECK_RATIO{0};
|
||||
|
||||
static std::vector<CAddress> g_sources;
|
||||
|
|
17
src/init.cpp
17
src/init.cpp
|
@ -1431,9 +1431,10 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
ApplyArgsManOptions(args, peerman_opts);
|
||||
|
||||
{
|
||||
|
||||
// Read asmap file if configured
|
||||
std::vector<bool> asmap;
|
||||
// Read asmap file or embedded data if configured and initialize
|
||||
// Netgroupman with or without it
|
||||
assert(!node.netgroupman);
|
||||
uint256 asmap_version;
|
||||
if (args.IsArgSet("-asmap") && !args.IsArgNegated("-asmap")) {
|
||||
fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME);
|
||||
if (!asmap_path.is_absolute()) {
|
||||
|
@ -1443,21 +1444,19 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
|
||||
return false;
|
||||
}
|
||||
asmap = DecodeAsmap(asmap_path);
|
||||
std::vector<std::byte> asmap{DecodeAsmap(asmap_path)};
|
||||
if (asmap.size() == 0) {
|
||||
InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
|
||||
return false;
|
||||
}
|
||||
const uint256 asmap_version = (HashWriter{} << asmap).GetHash();
|
||||
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::WithLoadedAsmap(asmap));
|
||||
asmap_version = AsmapChecksum(asmap);
|
||||
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
|
||||
} else {
|
||||
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::NoAsmap());
|
||||
LogPrintf("Using /16 prefix for IP bucketing\n");
|
||||
}
|
||||
|
||||
// Initialize netgroup manager
|
||||
assert(!node.netgroupman);
|
||||
node.netgroupman = std::make_unique<NetGroupManager>(std::move(asmap));
|
||||
|
||||
// Initialize addrman
|
||||
assert(!node.addrman);
|
||||
uiInterface.InitMessage(_("Loading P2P addresses…"));
|
||||
|
|
|
@ -6,13 +6,13 @@
|
|||
|
||||
#include <hash.h>
|
||||
#include <logging.h>
|
||||
#include <uint256.h>
|
||||
#include <util/asmap.h>
|
||||
|
||||
uint256 NetGroupManager::GetAsmapChecksum() const
|
||||
{
|
||||
if (!m_asmap.size()) return {};
|
||||
|
||||
return (HashWriter{} << m_asmap).GetHash();
|
||||
return AsmapChecksum(m_asmap);
|
||||
}
|
||||
|
||||
std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
|
||||
|
@ -81,30 +81,25 @@ std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) co
|
|||
uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
|
||||
{
|
||||
uint32_t net_class = address.GetNetClass();
|
||||
if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
|
||||
if (m_asmap.empty() || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
|
||||
return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
|
||||
}
|
||||
std::vector<bool> ip_bits(128);
|
||||
std::vector<std::byte> ip_bits(16);
|
||||
if (address.HasLinkedIPv4()) {
|
||||
// For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
|
||||
for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
|
||||
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
|
||||
ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
|
||||
}
|
||||
ip_bits[byte_i] = static_cast<std::byte>(IPV4_IN_IPV6_PREFIX[byte_i]);
|
||||
}
|
||||
uint32_t ipv4 = address.GetLinkedIPv4();
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
ip_bits[12 + i] = static_cast<std::byte>((ipv4 >> (24 - i * 8)) & 0xFF);
|
||||
}
|
||||
} else {
|
||||
// Use all 128 bits of the IPv6 address otherwise
|
||||
assert(address.IsIPv6());
|
||||
auto addr_bytes = address.GetAddrBytes();
|
||||
for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
|
||||
uint8_t cur_byte = addr_bytes[byte_i];
|
||||
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
|
||||
ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
|
||||
}
|
||||
ip_bits[byte_i] = static_cast<std::byte>(addr_bytes[byte_i]);
|
||||
}
|
||||
}
|
||||
uint32_t mapped_as = Interpret(m_asmap, ip_bits);
|
||||
|
|
|
@ -15,9 +15,18 @@
|
|||
*/
|
||||
class NetGroupManager {
|
||||
public:
|
||||
explicit NetGroupManager(std::vector<bool> asmap)
|
||||
: m_asmap{std::move(asmap)}
|
||||
{}
|
||||
static NetGroupManager WithEmbeddedAsmap(std::span<const std::byte> asmap) {
|
||||
return NetGroupManager(asmap, {});
|
||||
}
|
||||
|
||||
static NetGroupManager WithLoadedAsmap(std::vector<std::byte> loaded_asmap) {
|
||||
std::span<const std::byte> asmap_span(loaded_asmap);
|
||||
return NetGroupManager(asmap_span, std::move(loaded_asmap));
|
||||
}
|
||||
|
||||
static NetGroupManager NoAsmap() {
|
||||
return NetGroupManager({}, {});
|
||||
}
|
||||
|
||||
/** Get a checksum identifying the asmap being used. */
|
||||
uint256 GetAsmapChecksum() const;
|
||||
|
@ -52,7 +61,10 @@ public:
|
|||
bool UsingASMap() const;
|
||||
|
||||
private:
|
||||
/** Compressed IP->ASN mapping, loaded from a file when a node starts.
|
||||
/** Compressed IP->ASN mapping.
|
||||
*
|
||||
* Data may beloaded from a file when a node starts or embedded in the
|
||||
* binary.
|
||||
*
|
||||
* This mapping is then used for bucketing nodes in Addrman and for
|
||||
* ensuring we connect to a diverse set of peers in Connman. The map is
|
||||
|
@ -69,8 +81,17 @@ private:
|
|||
* re-bucketed.
|
||||
*
|
||||
* This is initialized in the constructor, const, and therefore is
|
||||
* thread-safe. */
|
||||
const std::vector<bool> m_asmap;
|
||||
* thread-safe. m_asmap can either point to m_loaded_asmap which holds
|
||||
* data loaded from an external file at runtime or it can point to embedded
|
||||
* asmap data.
|
||||
*/
|
||||
const std::span<const std::byte> m_asmap;
|
||||
std::vector<std::byte> m_loaded_asmap;
|
||||
|
||||
explicit NetGroupManager(std::span<const std::byte> asmap, std::vector<std::byte> loaded_asmap)
|
||||
: m_asmap(asmap.empty() ? std::span<const std::byte>() : asmap),
|
||||
m_loaded_asmap(std::move(loaded_asmap))
|
||||
{}
|
||||
};
|
||||
|
||||
#endif // BITCOIN_NETGROUP_H
|
||||
|
|
|
@ -24,7 +24,7 @@ using namespace std::literals;
|
|||
using node::NodeContext;
|
||||
using util::ToString;
|
||||
|
||||
static NetGroupManager EMPTY_NETGROUPMAN{std::vector<bool>()};
|
||||
static NetGroupManager EMPTY_NETGROUPMAN{NetGroupManager::NoAsmap()};
|
||||
static const bool DETERMINISTIC{true};
|
||||
|
||||
static int32_t GetCheckRatio(const NodeContext& node_ctx)
|
||||
|
@ -46,20 +46,6 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0)
|
|||
return serv.value_or(CService{});
|
||||
}
|
||||
|
||||
|
||||
static std::vector<bool> FromBytes(std::span<const std::byte> source)
|
||||
{
|
||||
int vector_size(source.size() * 8);
|
||||
std::vector<bool> result(vector_size);
|
||||
for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
|
||||
uint8_t cur_byte{std::to_integer<uint8_t>(source[byte_i])};
|
||||
for (int bit_i = 0; bit_i < 8; ++bit_i) {
|
||||
result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(addrman_simple)
|
||||
|
@ -592,8 +578,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy)
|
|||
// 101.8.0.0/16 AS8
|
||||
BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
|
||||
{
|
||||
std::vector<bool> asmap = FromBytes(test::data::asmap);
|
||||
NetGroupManager ngm_asmap{asmap};
|
||||
NetGroupManager ngm_asmap{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)};
|
||||
|
||||
CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
|
||||
CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
|
||||
|
@ -646,8 +631,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
|
||||
{
|
||||
std::vector<bool> asmap = FromBytes(test::data::asmap);
|
||||
NetGroupManager ngm_asmap{asmap};
|
||||
NetGroupManager ngm_asmap{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)};
|
||||
|
||||
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
|
||||
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
|
||||
|
@ -724,8 +708,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(addrman_serialization)
|
||||
{
|
||||
std::vector<bool> asmap1 = FromBytes(test::data::asmap);
|
||||
NetGroupManager netgroupman{asmap1};
|
||||
NetGroupManager netgroupman{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)};
|
||||
|
||||
const auto ratio = GetCheckRatio(m_node);
|
||||
auto addrman_asmap1 = std::make_unique<AddrMan>(netgroupman, DETERMINISTIC, ratio);
|
||||
|
|
|
@ -6,28 +6,18 @@
|
|||
#include <netgroup.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <util/asmap.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
//! asmap code that consumes nothing
|
||||
static const std::vector<bool> IPV6_PREFIX_ASMAP = {};
|
||||
static const std::vector<std::byte> IPV6_PREFIX_ASMAP = {};
|
||||
|
||||
//! asmap code that consumes the 96 prefix bits of ::ffff:0/96 (IPv4-in-IPv6 map)
|
||||
static const std::vector<bool> IPV4_PREFIX_ASMAP = {
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
|
||||
true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, // Match 0xFF
|
||||
true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true // Match 0xFF
|
||||
};
|
||||
static const auto IPV4_PREFIX_ASMAP = "fb03ec0fb03fc0fe00fb03ec0fb03fc0fe00fb03ec0fb0fffffeff"_hex_v;
|
||||
|
||||
FUZZ_TARGET(asmap)
|
||||
{
|
||||
|
@ -37,13 +27,14 @@ FUZZ_TARGET(asmap)
|
|||
bool ipv6 = buffer[0] & 128;
|
||||
const size_t addr_size = ipv6 ? ADDR_IPV6_SIZE : ADDR_IPV4_SIZE;
|
||||
if (buffer.size() < size_t(1 + asmap_size + addr_size)) return;
|
||||
std::vector<bool> asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP;
|
||||
asmap.reserve(asmap.size() + 8 * asmap_size);
|
||||
std::vector<std::byte> asmap_vec = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP;
|
||||
for (int i = 0; i < asmap_size; ++i) {
|
||||
uint8_t byte = buffer[1 + i];
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
asmap.push_back((buffer[1 + i] >> j) & 1);
|
||||
asmap_vec.push_back(static_cast<std::byte>((byte >> j) & 1));
|
||||
}
|
||||
}
|
||||
std::span<const std::byte> asmap(asmap_vec);
|
||||
if (!SanityCheckASMap(asmap, 128)) return;
|
||||
|
||||
const uint8_t* addr_data = buffer.data() + 1 + asmap_size;
|
||||
|
@ -57,6 +48,6 @@ FUZZ_TARGET(asmap)
|
|||
memcpy(&ipv4, addr_data, addr_size);
|
||||
net_addr.SetIP(CNetAddr{ipv4});
|
||||
}
|
||||
NetGroupManager netgroupman{asmap};
|
||||
NetGroupManager netgroupman{NetGroupManager::WithEmbeddedAsmap(asmap)};
|
||||
(void)netgroupman.GetMappedAS(net_addr);
|
||||
}
|
||||
|
|
|
@ -31,19 +31,21 @@ FUZZ_TARGET(asmap_direct)
|
|||
if (buffer.size() - sep_pos - 1 > 128) return; // At most 128 bits in IP address
|
||||
|
||||
// Checks on asmap
|
||||
std::vector<bool> asmap(buffer.begin(), buffer.begin() + sep_pos);
|
||||
if (SanityCheckASMap(asmap, buffer.size() - 1 - sep_pos)) {
|
||||
std::vector<std::byte> asmap(reinterpret_cast<const std::byte*>(buffer.data()),
|
||||
reinterpret_cast<const std::byte*>(buffer.data() + sep_pos));
|
||||
if (SanityCheckASMap(std::span<const std::byte>(asmap), buffer.size() - 1 - sep_pos)) {
|
||||
// Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid.
|
||||
std::vector<bool> asmap_prefix = asmap;
|
||||
while (!asmap_prefix.empty() && asmap_prefix.size() + 7 > asmap.size() && asmap_prefix.back() == false) {
|
||||
std::vector<std::byte> asmap_prefix = asmap;
|
||||
while (!asmap_prefix.empty() && asmap_prefix.size() + 7 > asmap.size() && asmap_prefix.back() == std::byte{0}) {
|
||||
asmap_prefix.pop_back();
|
||||
}
|
||||
while (!asmap_prefix.empty()) {
|
||||
asmap_prefix.pop_back();
|
||||
assert(!SanityCheckASMap(asmap_prefix, buffer.size() - 1 - sep_pos));
|
||||
assert(!SanityCheckASMap(std::span<const std::byte>(asmap_prefix), buffer.size() - 1 - sep_pos));
|
||||
}
|
||||
// No address input should trigger assertions in interpreter
|
||||
std::vector<bool> addr(buffer.begin() + sep_pos + 1, buffer.end());
|
||||
(void)Interpret(asmap, addr);
|
||||
std::vector<std::byte> addr(reinterpret_cast<const std::byte*>(buffer.data() + sep_pos + 1),
|
||||
reinterpret_cast<const std::byte*>(buffer.data() + buffer.size()));
|
||||
(void)Interpret(std::span<const std::byte>(asmap), std::span<const std::byte>(addr));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize)
|
|||
chainman.ResetIbd();
|
||||
|
||||
node::Warnings warnings{};
|
||||
NetGroupManager netgroupman{{}};
|
||||
NetGroupManager netgroupman{NetGroupManager::NoAsmap()};
|
||||
AddrMan addrman{netgroupman, /*deterministic=*/true, 0};
|
||||
auto peerman = PeerManager::make(connman, addrman,
|
||||
/*banman=*/nullptr, chainman,
|
||||
|
|
|
@ -65,11 +65,6 @@ template<typename B = uint8_t>
|
|||
return ret;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::vector<bool> ConsumeRandomLengthBitVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
|
||||
{
|
||||
return BytesToBits(ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline DataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
|
||||
{
|
||||
return DataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
|
||||
|
|
|
@ -210,9 +210,11 @@ public:
|
|||
|
||||
[[nodiscard]] inline NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider& fuzzed_data_provider) noexcept
|
||||
{
|
||||
std::vector<bool> asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
|
||||
if (!SanityCheckASMap(asmap, 128)) asmap.clear();
|
||||
return NetGroupManager(asmap);
|
||||
std::vector<std::byte> asmap{ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider)};
|
||||
if (!SanityCheckASMap(std::span<std::byte>(asmap), 128)) {
|
||||
return NetGroupManager::NoAsmap();
|
||||
}
|
||||
return NetGroupManager::WithLoadedAsmap(asmap);
|
||||
}
|
||||
|
||||
inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept
|
||||
|
|
|
@ -323,7 +323,7 @@ BOOST_AUTO_TEST_CASE(subnet_test)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(netbase_getgroup)
|
||||
{
|
||||
NetGroupManager netgroupman{std::vector<bool>()}; // use /16
|
||||
NetGroupManager netgroupman{NetGroupManager::NoAsmap()}; // use /16
|
||||
BOOST_CHECK(netgroupman.GetGroup(ResolveIP("127.0.0.1")) == std::vector<unsigned char>({0})); // Local -> !Routable()
|
||||
BOOST_CHECK(netgroupman.GetGroup(ResolveIP("257.0.0.1")) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
|
||||
BOOST_CHECK(netgroupman.GetGroup(ResolveIP("10.0.0.1")) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
|
||||
|
|
|
@ -317,7 +317,7 @@ TestingSetup::TestingSetup(
|
|||
|
||||
if (!opts.setup_net) return;
|
||||
|
||||
m_node.netgroupman = std::make_unique<NetGroupManager>(/*asmap=*/std::vector<bool>());
|
||||
m_node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::NoAsmap());
|
||||
m_node.addrman = std::make_unique<AddrMan>(*m_node.netgroupman,
|
||||
/*deterministic=*/false,
|
||||
m_node.args->GetIntArg("-checkaddrman", 0));
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <util/asmap.h>
|
||||
|
||||
#include <clientversion.h>
|
||||
#include <hash.h>
|
||||
#include <logging.h>
|
||||
#include <serialize.h>
|
||||
#include <streams.h>
|
||||
|
@ -14,6 +15,7 @@
|
|||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <span>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
|
@ -21,15 +23,14 @@ namespace {
|
|||
|
||||
constexpr uint32_t INVALID = 0xFFFFFFFF;
|
||||
|
||||
uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos, uint8_t minval, const std::vector<uint8_t> &bit_sizes)
|
||||
uint32_t DecodeBits(size_t& bitpos, const std::span<const std::byte>& data, uint8_t minval, const std::vector<uint8_t>& bit_sizes)
|
||||
{
|
||||
uint32_t val = minval;
|
||||
bool bit;
|
||||
for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
|
||||
bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
|
||||
for (auto bit_sizes_it = bit_sizes.begin(); bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
|
||||
if (bit_sizes_it + 1 != bit_sizes.end()) {
|
||||
if (bitpos == endpos) break;
|
||||
bit = *bitpos;
|
||||
if (bitpos >= data.size() * 8) break;
|
||||
bit = (std::to_integer<uint8_t>(data[bitpos / 8]) >> (bitpos % 8)) & 1;
|
||||
bitpos++;
|
||||
} else {
|
||||
bit = 0;
|
||||
|
@ -38,8 +39,8 @@ uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos, const std::vector
|
|||
val += (1 << *bit_sizes_it);
|
||||
} else {
|
||||
for (int b = 0; b < *bit_sizes_it; b++) {
|
||||
if (bitpos == endpos) return INVALID; // Reached EOF in mantissa
|
||||
bit = *bitpos;
|
||||
if (bitpos >= data.size() * 8) return INVALID; // Reached EOF in mantissa
|
||||
bit = (std::to_integer<uint8_t>(data[bitpos / 8]) >> (bitpos % 8)) & 1;
|
||||
bitpos++;
|
||||
val += bit << (*bit_sizes_it - 1 - b);
|
||||
}
|
||||
|
@ -58,69 +59,68 @@ enum class Instruction : uint32_t
|
|||
};
|
||||
|
||||
const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
|
||||
Instruction DecodeType(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
|
||||
Instruction DecodeType(size_t& bitpos, const std::span<const std::byte>& data)
|
||||
{
|
||||
return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
|
||||
return Instruction(DecodeBits(bitpos, data, 0, TYPE_BIT_SIZES));
|
||||
}
|
||||
|
||||
const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
|
||||
uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
|
||||
uint32_t DecodeASN(size_t& bitpos, const std::span<const std::byte>& data)
|
||||
{
|
||||
return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
|
||||
return DecodeBits(bitpos, data, 1, ASN_BIT_SIZES);
|
||||
}
|
||||
|
||||
|
||||
const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
|
||||
uint32_t DecodeMatch(size_t& bitpos, const std::span<const std::byte>& data)
|
||||
{
|
||||
return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
|
||||
return DecodeBits(bitpos, data, 2, MATCH_BIT_SIZES);
|
||||
}
|
||||
|
||||
|
||||
const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
|
||||
uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
|
||||
uint32_t DecodeJump(size_t& bitpos, const std::span<const std::byte>& data)
|
||||
{
|
||||
return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
|
||||
return DecodeBits(bitpos, data, 17, JUMP_BIT_SIZES);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
|
||||
uint32_t Interpret(const std::span<const std::byte>& asmap, const std::span<const std::byte>& ip)
|
||||
{
|
||||
std::vector<bool>::const_iterator pos = asmap.begin();
|
||||
const std::vector<bool>::const_iterator endpos = asmap.end();
|
||||
uint8_t bits = ip.size();
|
||||
size_t pos{0};
|
||||
uint8_t bits = ip.size() * 8;
|
||||
uint32_t default_asn = 0;
|
||||
uint32_t jump, match, matchlen;
|
||||
Instruction opcode;
|
||||
while (pos != endpos) {
|
||||
opcode = DecodeType(pos, endpos);
|
||||
while (pos < asmap.size() * 8) {
|
||||
opcode = DecodeType(pos, asmap);
|
||||
if (opcode == Instruction::RETURN) {
|
||||
default_asn = DecodeASN(pos, endpos);
|
||||
default_asn = DecodeASN(pos, asmap);
|
||||
if (default_asn == INVALID) break; // ASN straddles EOF
|
||||
return default_asn;
|
||||
} else if (opcode == Instruction::JUMP) {
|
||||
jump = DecodeJump(pos, endpos);
|
||||
jump = DecodeJump(pos, asmap);
|
||||
if (jump == INVALID) break; // Jump offset straddles EOF
|
||||
if (bits == 0) break; // No input bits left
|
||||
if (int64_t{jump} >= int64_t{endpos - pos}) break; // Jumping past EOF
|
||||
if (ip[ip.size() - bits]) {
|
||||
if (int64_t{jump} >= static_cast<int64_t>(asmap.size() * 8 - pos)) break; // Jumping past EOF
|
||||
if ((std::to_integer<uint8_t>(ip[(ip.size() * 8 - bits) / 8]) >> (7 - ((ip.size() * 8 - bits) % 8))) & 1) {
|
||||
pos += jump;
|
||||
}
|
||||
bits--;
|
||||
} else if (opcode == Instruction::MATCH) {
|
||||
match = DecodeMatch(pos, endpos);
|
||||
match = DecodeMatch(pos, asmap);
|
||||
if (match == INVALID) break; // Match bits straddle EOF
|
||||
matchlen = std::bit_width(match) - 1;
|
||||
if (bits < matchlen) break; // Not enough input bits
|
||||
for (uint32_t bit = 0; bit < matchlen; bit++) {
|
||||
if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
|
||||
if (((std::to_integer<uint8_t>(ip[(ip.size() * 8 - bits) / 8]) >> (7 - ((ip.size() * 8 - bits) % 8))) & 1) != ((match >> (matchlen - 1 - bit)) & 1)) {
|
||||
return default_asn;
|
||||
}
|
||||
bits--;
|
||||
}
|
||||
} else if (opcode == Instruction::DEFAULT) {
|
||||
default_asn = DecodeASN(pos, endpos);
|
||||
default_asn = DecodeASN(pos, asmap);
|
||||
if (default_asn == INVALID) break; // ASN straddles EOF
|
||||
} else {
|
||||
break; // Instruction straddles EOF
|
||||
|
@ -130,50 +130,48 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
|
|||
return 0; // 0 is not a valid ASN
|
||||
}
|
||||
|
||||
bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
|
||||
bool SanityCheckASMap(const std::span<const std::byte>& asmap, int bits)
|
||||
{
|
||||
const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
|
||||
std::vector<bool>::const_iterator pos = begin;
|
||||
size_t pos{0};
|
||||
size_t endpos{asmap.size() * 8};
|
||||
std::vector<std::pair<uint32_t, int>> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left)
|
||||
jumps.reserve(bits);
|
||||
Instruction prevopcode = Instruction::JUMP;
|
||||
bool had_incomplete_match = false;
|
||||
while (pos != endpos) {
|
||||
uint32_t offset = pos - begin;
|
||||
if (!jumps.empty() && offset >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction
|
||||
Instruction opcode = DecodeType(pos, endpos);
|
||||
if (!jumps.empty() && pos >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction
|
||||
Instruction opcode = DecodeType(pos, asmap);
|
||||
if (opcode == Instruction::RETURN) {
|
||||
if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN)
|
||||
uint32_t asn = DecodeASN(pos, endpos);
|
||||
uint32_t asn = DecodeASN(pos, asmap);
|
||||
if (asn == INVALID) return false; // ASN straddles EOF
|
||||
if (jumps.empty()) {
|
||||
// Nothing to execute anymore
|
||||
if (endpos - pos > 7) return false; // Excessive padding
|
||||
while (pos != endpos) {
|
||||
if (*pos) return false; // Nonzero padding bit
|
||||
if ((std::to_integer<uint8_t>(asmap[pos / 8]) >> (pos % 8)) & 1) return false; // Nonzero padding bit
|
||||
++pos;
|
||||
}
|
||||
return true; // Sanely reached EOF
|
||||
} else {
|
||||
// Continue by pretending we jumped to the next instruction
|
||||
offset = pos - begin;
|
||||
if (offset != jumps.back().first) return false; // Unreachable code
|
||||
if (pos != jumps.back().first) return false; // Unreachable code
|
||||
bits = jumps.back().second; // Restore the number of bits we would have had left after this jump
|
||||
jumps.pop_back();
|
||||
prevopcode = Instruction::JUMP;
|
||||
}
|
||||
} else if (opcode == Instruction::JUMP) {
|
||||
uint32_t jump = DecodeJump(pos, endpos);
|
||||
uint32_t jump = DecodeJump(pos, asmap);
|
||||
if (jump == INVALID) return false; // Jump offset straddles EOF
|
||||
if (int64_t{jump} > int64_t{endpos - pos}) return false; // Jump out of range
|
||||
if (int64_t{jump} > static_cast<int64_t>(endpos - pos)) return false; // Jump out of range
|
||||
if (bits == 0) return false; // Consuming bits past the end of the input
|
||||
--bits;
|
||||
uint32_t jump_offset = pos - begin + jump;
|
||||
uint32_t jump_offset = pos + jump;
|
||||
if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps
|
||||
jumps.emplace_back(jump_offset, bits);
|
||||
prevopcode = Instruction::JUMP;
|
||||
} else if (opcode == Instruction::MATCH) {
|
||||
uint32_t match = DecodeMatch(pos, endpos);
|
||||
uint32_t match = DecodeMatch(pos, asmap);
|
||||
if (match == INVALID) return false; // Match bits straddle EOF
|
||||
int matchlen = std::bit_width(match) - 1;
|
||||
if (prevopcode != Instruction::MATCH) had_incomplete_match = false;
|
||||
|
@ -184,7 +182,7 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
|
|||
prevopcode = Instruction::MATCH;
|
||||
} else if (opcode == Instruction::DEFAULT) {
|
||||
if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one)
|
||||
uint32_t asn = DecodeASN(pos, endpos);
|
||||
uint32_t asn = DecodeASN(pos, asmap);
|
||||
if (asn == INVALID) return false; // ASN straddles EOF
|
||||
prevopcode = Instruction::DEFAULT;
|
||||
} else {
|
||||
|
@ -194,30 +192,46 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
|
|||
return false; // Reached EOF without RETURN instruction
|
||||
}
|
||||
|
||||
std::vector<bool> DecodeAsmap(fs::path path)
|
||||
std::span<const std::byte> CheckAsmap(const std::span<const std::byte>& data)
|
||||
{
|
||||
if (data.empty()) {
|
||||
return {};
|
||||
}
|
||||
if (!SanityCheckASMap(data, 128)) {
|
||||
LogInfo("Sanity check of asmap data failed\n");
|
||||
return {};
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<std::byte> DecodeAsmap(fs::path path)
|
||||
{
|
||||
std::vector<bool> bits;
|
||||
FILE *filestr = fsbridge::fopen(path, "rb");
|
||||
AutoFile file{filestr};
|
||||
if (file.IsNull()) {
|
||||
LogPrintf("Failed to open asmap file from disk\n");
|
||||
return bits;
|
||||
}
|
||||
file.seek(0, SEEK_END);
|
||||
int length = file.tell();
|
||||
LogPrintf("Opened asmap file %s (%d bytes) from disk\n", fs::quoted(fs::PathToString(path)), length);
|
||||
file.seek(0, SEEK_SET);
|
||||
uint8_t cur_byte;
|
||||
for (int i = 0; i < length; ++i) {
|
||||
file >> cur_byte;
|
||||
for (int bit = 0; bit < 8; ++bit) {
|
||||
bits.push_back((cur_byte >> bit) & 1);
|
||||
}
|
||||
}
|
||||
if (!SanityCheckASMap(bits, 128)) {
|
||||
LogPrintf("Sanity check of asmap file %s failed\n", fs::quoted(fs::PathToString(path)));
|
||||
LogInfo("Failed to open asmap file from disk\n");
|
||||
return {};
|
||||
}
|
||||
return bits;
|
||||
|
||||
file.seek(0, SEEK_END);
|
||||
int length = file.tell();
|
||||
LogInfo("Opened asmap file %s (%d bytes) from disk\n", fs::quoted(fs::PathToString(path)), length);
|
||||
file.seek(0, SEEK_SET);
|
||||
|
||||
std::vector<std::byte> buffer(length);
|
||||
file.read(buffer);
|
||||
|
||||
if (!SanityCheckASMap(buffer, 128)) {
|
||||
LogInfo("Sanity check of asmap data failed\n");
|
||||
return {};
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
uint256 AsmapChecksum(const std::span<const std::byte>& data)
|
||||
{
|
||||
HashWriter asmap_hasher;
|
||||
asmap_hasher << data;
|
||||
return asmap_hasher.GetHash();
|
||||
}
|
||||
|
|
|
@ -5,16 +5,22 @@
|
|||
#ifndef BITCOIN_UTIL_ASMAP_H
|
||||
#define BITCOIN_UTIL_ASMAP_H
|
||||
|
||||
#include <uint256.h>
|
||||
#include <util/fs.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip);
|
||||
uint32_t Interpret(const std::span<const std::byte>& asmap, const std::span<const std::byte>& ip);
|
||||
|
||||
bool SanityCheckASMap(const std::vector<bool>& asmap, int bits);
|
||||
bool SanityCheckASMap(const std::span<const std::byte>& asmap, int bits);
|
||||
|
||||
/** Read asmap from provided binary file */
|
||||
std::vector<bool> DecodeAsmap(fs::path path);
|
||||
/** Read and check asmap from provided binary file */
|
||||
std::vector<std::byte> DecodeAsmap(fs::path path);
|
||||
/** Check asmap from embedded data */
|
||||
std::span<const std::byte> CheckAsmap(const std::span<const std::byte>& data);
|
||||
/** Calculate asmap checksum */
|
||||
uint256 AsmapChecksum(const std::span<const std::byte>& data);
|
||||
|
||||
#endif // BITCOIN_UTIL_ASMAP_H
|
||||
|
|
|
@ -31,7 +31,7 @@ from test_framework.util import assert_equal
|
|||
|
||||
DEFAULT_ASMAP_FILENAME = 'ip_asn.map' # defined in src/init.cpp
|
||||
ASMAP = 'src/test/data/asmap.raw' # path to unit test skeleton asmap
|
||||
VERSION = 'fec61fa21a9f46f3b17bdcd660d7f4cd90b966aad3aec593c99b35f0aca15853'
|
||||
VERSION = 'bafc9da308f45179443bd1d22325400ac9104f741522d003e3fac86700f68895'
|
||||
|
||||
def expected_messages(filename):
|
||||
return [f'Opened asmap file "{filename}" (59 bytes) from disk',
|
||||
|
|
Loading…
Add table
Reference in a new issue