diff options
author | Jim Posen <jim.posen@gmail.com> | 2018-08-27 15:04:43 -0700 |
---|---|---|
committer | Jim Posen <jim.posen@gmail.com> | 2018-11-06 09:12:54 -0800 |
commit | 20b812993ae584e4a4b12faa5c8751aa04dfde81 (patch) | |
tree | a7fb1808f9b0b3b01dea63e1e1073d962710e9b8 /src/blockfilter.h | |
parent | 6af27b81572b7b8e08ebcfe7eb533f40c66be4af (diff) | |
download | bitcoin-20b812993ae584e4a4b12faa5c8751aa04dfde81.tar.xz |
blockfilter: Refactor GCS params into struct.
Diffstat (limited to 'src/blockfilter.h')
-rw-r--r-- | src/blockfilter.h | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/src/blockfilter.h b/src/blockfilter.h index 871be11769..e53f813ab3 100644 --- a/src/blockfilter.h +++ b/src/blockfilter.h @@ -25,11 +25,20 @@ public: typedef std::vector<unsigned char> Element; typedef std::unordered_set<Element, ByteVectorHash> ElementSet; + struct Params + { + uint64_t m_siphash_k0; + uint64_t m_siphash_k1; + uint8_t m_P; //!< Golomb-Rice coding parameter + uint32_t m_M; //!< Inverse false positive rate + + Params(uint64_t siphash_k0 = 0, uint64_t siphash_k1 = 0, uint8_t P = 0, uint32_t M = 1) + : m_siphash_k0(siphash_k0), m_siphash_k1(siphash_k1), m_P(P), m_M(M) + {} + }; + private: - uint64_t m_siphash_k0; - uint64_t m_siphash_k1; - uint8_t m_P; //!< Golomb-Rice coding parameter - uint32_t m_M; //!< Inverse false positive rate + Params m_params; uint32_t m_N; //!< Number of elements in the filter uint64_t m_F; //!< Range of element hashes, F = N * M std::vector<unsigned char> m_encoded; @@ -45,19 +54,16 @@ private: public: /** Constructs an empty filter. */ - GCSFilter(uint64_t siphash_k0 = 0, uint64_t siphash_k1 = 0, uint8_t P = 0, uint32_t M = 0); + explicit GCSFilter(const Params& params = Params()); /** Reconstructs an already-created filter from an encoding. */ - GCSFilter(uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32_t M, - std::vector<unsigned char> encoded_filter); + GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter); /** Builds a new filter from the params and set of elements. */ - GCSFilter(uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32_t M, - const ElementSet& elements); + GCSFilter(const Params& params, const ElementSet& elements); - uint8_t GetP() const { return m_P; } uint32_t GetN() const { return m_N; } - uint32_t GetM() const { return m_M; } + const Params& GetParams() const { return m_params; } const std::vector<unsigned char>& GetEncoded() const { return m_encoded; } /** @@ -93,6 +99,8 @@ private: uint256 m_block_hash; GCSFilter m_filter; + bool BuildParams(GCSFilter::Params& params) const; + public: // Construct a new BlockFilter of the specified type from a block. @@ -131,15 +139,11 @@ public: m_filter_type = static_cast<BlockFilterType>(filter_type); - switch (m_filter_type) { - case BlockFilterType::BASIC: - m_filter = GCSFilter(m_block_hash.GetUint64(0), m_block_hash.GetUint64(1), - BASIC_FILTER_P, BASIC_FILTER_M, std::move(encoded_filter)); - break; - - default: + GCSFilter::Params params; + if (!BuildParams(params)) { throw std::ios_base::failure("unknown filter_type"); } + m_filter = GCSFilter(params, std::move(encoded_filter)); } }; |