diff --git a/src/blockfilter.h b/src/blockfilter.h --- a/src/blockfilter.h +++ b/src/blockfilter.h @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -88,6 +89,14 @@ INVALID = 255, }; +/** Get the human-readable name for a filter type. Returns empty string for + * unknown types. */ +const std::string &BlockFilterTypeName(BlockFilterType filter_type); + +/** Find a filter type by its human-readable name. */ +bool BlockFilterTypeByName(const std::string &name, + BlockFilterType &filter_type); + /** * Complete block filter struct as defined in BIP 157. Serialization matches * payload of "cfilter" messages. diff --git a/src/blockfilter.cpp b/src/blockfilter.cpp --- a/src/blockfilter.cpp +++ b/src/blockfilter.cpp @@ -15,6 +15,10 @@ /// Protocol version used to serialize parameters in GCS filter encoding. static constexpr int GCS_SER_VERSION = 0; +static const std::map g_filter_types = { + {BlockFilterType::BASIC, "basic"}, +}; + template static void GolombRiceEncode(BitStreamWriter &bitwriter, uint8_t P, uint64_t x) { @@ -194,6 +198,23 @@ return MatchInternal(queries.data(), queries.size()); } +const std::string &BlockFilterTypeName(BlockFilterType filter_type) { + static std::string unknown_retval = ""; + auto it = g_filter_types.find(filter_type); + return it != g_filter_types.end() ? it->second : unknown_retval; +} + +bool BlockFilterTypeByName(const std::string &name, + BlockFilterType &filter_type) { + for (const auto &entry : g_filter_types) { + if (entry.second == name) { + filter_type = entry.first; + return true; + } + } + return false; +} + static GCSFilter::ElementSet BasicFilterElements(const CBlock &block, const CBlockUndo &block_undo) { GCSFilter::ElementSet elements; diff --git a/src/test/blockfilter_tests.cpp b/src/test/blockfilter_tests.cpp --- a/src/test/blockfilter_tests.cpp +++ b/src/test/blockfilter_tests.cpp @@ -191,4 +191,16 @@ } } +BOOST_AUTO_TEST_CASE(blockfilter_type_names) { + BOOST_CHECK_EQUAL(BlockFilterTypeName(BlockFilterType::BASIC), "basic"); + BOOST_CHECK_EQUAL(BlockFilterTypeName(static_cast(255)), + ""); + + BlockFilterType filter_type; + BOOST_CHECK(BlockFilterTypeByName("basic", filter_type)); + BOOST_CHECK_EQUAL(filter_type, BlockFilterType::BASIC); + + BOOST_CHECK(!BlockFilterTypeByName("unknown", filter_type)); +} + BOOST_AUTO_TEST_SUITE_END()