Skip to content

Commit ba6ff9a

Browse files
committed
blockfilter: Functions to translate filter types to/from names.
1 parent 62b7a4f commit ba6ff9a

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/blockfilter.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ static constexpr int GCS_SER_TYPE = SER_NETWORK;
1515
/// Protocol version used to serialize parameters in GCS filter encoding.
1616
static constexpr int GCS_SER_VERSION = 0;
1717

18+
static const std::map<BlockFilterType, std::string> g_filter_types = {
19+
{BlockFilterType::BASIC, "basic"},
20+
};
21+
1822
template <typename OStream>
1923
static void GolombRiceEncode(BitStreamWriter<OStream>& bitwriter, uint8_t P, uint64_t x)
2024
{
@@ -197,6 +201,23 @@ bool GCSFilter::MatchAny(const ElementSet& elements) const
197201
return MatchInternal(queries.data(), queries.size());
198202
}
199203

204+
const std::string& BlockFilterTypeName(BlockFilterType filter_type)
205+
{
206+
static std::string unknown_retval = "";
207+
auto it = g_filter_types.find(filter_type);
208+
return it != g_filter_types.end() ? it->second : unknown_retval;
209+
}
210+
211+
bool BlockFilterTypeByName(const std::string& name, BlockFilterType& filter_type) {
212+
for (const auto& entry : g_filter_types) {
213+
if (entry.second == name) {
214+
filter_type = entry.first;
215+
return true;
216+
}
217+
}
218+
return false;
219+
}
220+
200221
static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
201222
const CBlockUndo& block_undo)
202223
{

src/blockfilter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_BLOCKFILTER_H
77

88
#include <stdint.h>
9+
#include <string>
910
#include <unordered_set>
1011
#include <vector>
1112

@@ -89,6 +90,12 @@ enum class BlockFilterType : uint8_t
8990
INVALID = 255,
9091
};
9192

93+
/** Get the human-readable name for a filter type. Returns empty string for unknown types. */
94+
const std::string& BlockFilterTypeName(BlockFilterType filter_type);
95+
96+
/** Find a filter type by its human-readable name. */
97+
bool BlockFilterTypeByName(const std::string& name, BlockFilterType& filter_type);
98+
9299
/**
93100
* Complete block filter struct as defined in BIP 157. Serialization matches
94101
* payload of "cfilter" messages.

src/test/blockfilter_tests.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,16 @@ BOOST_AUTO_TEST_CASE(blockfilters_json_test)
174174
}
175175
}
176176

177+
BOOST_AUTO_TEST_CASE(blockfilter_type_names)
178+
{
179+
BOOST_CHECK_EQUAL(BlockFilterTypeName(BlockFilterType::BASIC), "basic");
180+
BOOST_CHECK_EQUAL(BlockFilterTypeName(static_cast<BlockFilterType>(255)), "");
181+
182+
BlockFilterType filter_type;
183+
BOOST_CHECK(BlockFilterTypeByName("basic", filter_type));
184+
BOOST_CHECK_EQUAL(filter_type, BlockFilterType::BASIC);
185+
186+
BOOST_CHECK(!BlockFilterTypeByName("unknown", filter_type));
187+
}
188+
177189
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)