Class: Bitcoin::BloomFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/bloom_filter.rb

Overview

Constant Summary collapse

SEED_SHIFT =
0xfba4c795
BIT_MASK =
[0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80].freeze
MAX_FILTER_SIZE =
36_000
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

Instance Method Summary collapse

Constructor Details

#initialize(elements, fp_rate, tweak) ⇒ BloomFilter

Returns a new instance of BloomFilter.



17
18
19
20
# File 'lib/bitcoin/bloom_filter.rb', line 17

def initialize(elements, fp_rate, tweak)
  init_filter(elements, fp_rate)
  @tweak = tweak
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



15
16
17
# File 'lib/bitcoin/bloom_filter.rb', line 15

def filter
  @filter
end

#nfuncObject (readonly)

Returns the value of attribute nfunc.



15
16
17
# File 'lib/bitcoin/bloom_filter.rb', line 15

def nfunc
  @nfunc
end

#tweakObject (readonly)

Returns the value of attribute tweak.



15
16
17
# File 'lib/bitcoin/bloom_filter.rb', line 15

def tweak
  @tweak
end

Instance Method Details

#add_address(address) ⇒ Object



38
39
40
# File 'lib/bitcoin/bloom_filter.rb', line 38

def add_address(address)
  add_data(Bitcoin.hash160_from_address(address).htb)
end

#add_data(data) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/bitcoin/bloom_filter.rb', line 22

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



42
43
44
# File 'lib/bitcoin/bloom_filter.rb', line 42

def add_outpoint(prev_tx_hash, prev_output)
  add_data(prev_tx_hash.htb_reverse + [prev_output].pack('V'))
end

#contains?(data) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/bitcoin/bloom_filter.rb', line 30

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