diff options
author | Jim Posen <jim.posen@gmail.com> | 2018-08-27 15:42:35 -0700 |
---|---|---|
committer | Jim Posen <jim.posen@gmail.com> | 2019-04-06 12:03:21 -0700 |
commit | ba6ff9a6f70139594362b4b4a6b816707bb165c8 (patch) | |
tree | bfd72dd218f42fd57a402cb9dece4d56ab1e9659 /src | |
parent | 62b7a4f09409f60a35fc99ddb70f9199ac511a53 (diff) |
blockfilter: Functions to translate filter types to/from names.
Diffstat (limited to 'src')
-rw-r--r-- | src/blockfilter.cpp | 21 | ||||
-rw-r--r-- | src/blockfilter.h | 7 | ||||
-rw-r--r-- | src/test/blockfilter_tests.cpp | 12 |
3 files changed, 40 insertions, 0 deletions
diff --git a/src/blockfilter.cpp b/src/blockfilter.cpp index e15213c552..b96956212b 100644 --- a/src/blockfilter.cpp +++ b/src/blockfilter.cpp @@ -15,6 +15,10 @@ static constexpr int GCS_SER_TYPE = SER_NETWORK; /// Protocol version used to serialize parameters in GCS filter encoding. static constexpr int GCS_SER_VERSION = 0; +static const std::map<BlockFilterType, std::string> g_filter_types = { + {BlockFilterType::BASIC, "basic"}, +}; + template <typename OStream> static void GolombRiceEncode(BitStreamWriter<OStream>& bitwriter, uint8_t P, uint64_t x) { @@ -197,6 +201,23 @@ bool GCSFilter::MatchAny(const ElementSet& elements) const 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) { diff --git a/src/blockfilter.h b/src/blockfilter.h index e5e087ed5a..7a219ff22b 100644 --- a/src/blockfilter.h +++ b/src/blockfilter.h @@ -6,6 +6,7 @@ #define BITCOIN_BLOCKFILTER_H #include <stdint.h> +#include <string> #include <unordered_set> #include <vector> @@ -89,6 +90,12 @@ enum class BlockFilterType : uint8_t 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/test/blockfilter_tests.cpp b/src/test/blockfilter_tests.cpp index cd0c36d802..92f9de78e7 100644 --- a/src/test/blockfilter_tests.cpp +++ b/src/test/blockfilter_tests.cpp @@ -174,4 +174,16 @@ BOOST_AUTO_TEST_CASE(blockfilters_json_test) } } +BOOST_AUTO_TEST_CASE(blockfilter_type_names) +{ + BOOST_CHECK_EQUAL(BlockFilterTypeName(BlockFilterType::BASIC), "basic"); + BOOST_CHECK_EQUAL(BlockFilterTypeName(static_cast<BlockFilterType>(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() |