Class: Bitcoin::BlockFilter

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

Overview

Compact Block Filter github.com/bitcoin/bips/blob/master/bip-0158.mediawiki This implementation ported the implementation of Bitcoin Core’s blockfilter.cpp. github.com/bitcoin/bitcoin/blob/master/src/blockfilter.cpp

Constant Summary collapse

TYPE =
{basic: 0}
BASIC_FILTER_P =

basic filter params

19
BASIC_FILTER_M =
784931

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_type, filter, block_hash) ⇒ Bitcoin::BlockFilter

Constructor

Parameters:



24
25
26
27
28
# File 'lib/bitcoin/block_filter.rb', line 24

def initialize(filter_type, filter, block_hash)
  @filter_type = filter_type
  @filter = filter
  @block_hash = block_hash
end

Instance Attribute Details

#block_hashObject

Returns the value of attribute block_hash.



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

def block_hash
  @block_hash
end

#filterObject

Returns the value of attribute filter.



16
17
18
# File 'lib/bitcoin/block_filter.rb', line 16

def filter
  @filter
end

#filter_typeObject

Returns the value of attribute filter_type.



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

def filter_type
  @filter_type
end

Class Method Details

.build_from_block(filter_type, block, prev_out_scripts) ⇒ Bitcoin::BlockFilter

Build BlockFilter from the block data.

Parameters:

  • filter_type (Integer)

    a filter type(basic or extended).

  • block (Bitcoin::Block)

    target block object.

  • prev_out_scripts (Array[Bitcoin::Script])

    The previous output script (the script being spent) for each input, except for the coinbase transaction.

Returns:



35
36
37
38
39
40
41
42
43
44
# File 'lib/bitcoin/block_filter.rb', line 35

def self.build_from_block(filter_type, block, prev_out_scripts)
  block_hash = block.block_hash.htb[0...16]
  filter = case filter_type
           when TYPE[:basic]
             GCSFilter.new(block_hash, BASIC_FILTER_P, BASIC_FILTER_M, elements: build_basic_filter_elements(block, prev_out_scripts))
           else
             raise "unknown filter type: #{filter_type}."
           end
  BlockFilter.new(filter_type, filter, block.block_hash)
end

Instance Method Details

#encoded_filterObject

get encoded filter.



61
62
63
# File 'lib/bitcoin/block_filter.rb', line 61

def encoded_filter
  filter.encoded
end

#filter_hashString

calculate filter hash.

Returns:

  • (String)

    this filter hash with hex format.



49
50
51
# File 'lib/bitcoin/block_filter.rb', line 49

def filter_hash
  Bitcoin.double_sha256(encoded_filter.htb).bth
end

#header(prev_header) ⇒ String

calculate filter header which calculates from previous filter header and current filter hash.

Parameters:

  • prev_header (String)

    a previous header with hex format.

Returns:

  • (String)

    header of this filter with hex format.



56
57
58
# File 'lib/bitcoin/block_filter.rb', line 56

def header(prev_header)
  Bitcoin.double_sha256(filter_hash.htb + prev_header.htb).bth
end