Class: Bitcoin::BloomFilter
- Inherits:
-
Object
- Object
- Bitcoin::BloomFilter
- Defined in:
- lib/bitcoin/bloom_filter.rb
Constant Summary collapse
- SEED_SHIFT =
0xfba4c795
- BIT_MASK =
[0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80]
- MAX_FILTER_SIZE =
36000
- MAX_HASH_FUNCS =
50
- BLOOM_UPDATE_NONE =
flags for filterload message
0
- BLOOM_UPDATE_ALL =
1
- BLOOM_UPDATE_P2PUBKEY_ONLY =
2
Instance Attribute Summary collapse
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
-
#nfunc ⇒ Object
readonly
Returns the value of attribute nfunc.
-
#tweak ⇒ Object
readonly
Returns the value of attribute tweak.
Instance Method Summary collapse
- #add_address(address) ⇒ Object
- #add_data(data) ⇒ Object
- #add_outpoint(prev_tx_hash, prev_output) ⇒ Object
- #contains?(data) ⇒ Boolean
-
#initialize(elements, fp_rate, tweak) ⇒ BloomFilter
constructor
A new instance of BloomFilter.
Constructor Details
#initialize(elements, fp_rate, tweak) ⇒ BloomFilter
Returns a new instance of BloomFilter.
16 17 18 19 |
# File 'lib/bitcoin/bloom_filter.rb', line 16 def initialize(elements, fp_rate, tweak) init_filter(elements, fp_rate) @tweak = tweak end |
Instance Attribute Details
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
14 15 16 |
# File 'lib/bitcoin/bloom_filter.rb', line 14 def filter @filter end |
#nfunc ⇒ Object (readonly)
Returns the value of attribute nfunc.
14 15 16 |
# File 'lib/bitcoin/bloom_filter.rb', line 14 def nfunc @nfunc end |
#tweak ⇒ Object (readonly)
Returns the value of attribute tweak.
14 15 16 |
# File 'lib/bitcoin/bloom_filter.rb', line 14 def tweak @tweak end |
Instance Method Details
#add_address(address) ⇒ Object
37 38 39 |
# File 'lib/bitcoin/bloom_filter.rb', line 37 def add_address(address) add_data(Bitcoin.hash160_from_address(address).htb) end |
#add_data(data) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/bitcoin/bloom_filter.rb', line 21 def add_data(data) @nfunc.times.each do |fi| idx = calc_index(data, fi) i = idx / 8 @filter[i] = (@filter[i].ord | BIT_MASK[idx % 8]).chr end end |
#add_outpoint(prev_tx_hash, prev_output) ⇒ Object
41 42 43 |
# File 'lib/bitcoin/bloom_filter.rb', line 41 def add_outpoint(prev_tx_hash, prev_output) add_data(prev_tx_hash.htb_reverse + [prev_output].pack('V')) end |
#contains?(data) ⇒ Boolean
29 30 31 32 33 34 35 |
# File 'lib/bitcoin/bloom_filter.rb', line 29 def contains?(data) @nfunc.times.all? do |fi| idx = calc_index(data, fi) i = idx / 8 @filter[i].ord & BIT_MASK[idx % 8] != 0 end end |