Class: Bitcoin::BloomFilter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#filterObject (readonly)

Returns the value of attribute filter.



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

def filter
  @filter
end

#nfuncObject (readonly)

Returns the value of attribute nfunc.



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

def nfunc
  @nfunc
end

#tweakObject (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

Returns:

  • (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