Class: Ethereum::Bloom

Inherits:
Object show all
Defined in:
lib/ethereum/bloom.rb

Overview

Blooms are the 3-point, 2048-bit (11-bits/point) Bloom filter of each component (except data) of each log entry of each transaction.

We set the bits of a 2048-bit value whose indices are given by the low order 9-bits of the first three double-bytes of the SHA3 of each value.

Blooms are type of ‘Integer`.

Examples:

bloom(0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6)
sha3: bd2b01afcd27800b54d2179edc49e2bffde5078bb6d0b204694169b1643fb108
first 3 double-bytes: bd2b, 01af, cd27
bits in bloom: 1323, 431, 1319

Constant Summary collapse

BITS =
2048
MASK =
2047
BUCKETS =
3

Class Method Summary collapse

Class Method Details

.b256(int_bloom) ⇒ Object



60
61
62
# File 'lib/ethereum/bloom.rb', line 60

def b256(int_bloom)
  Utils.zpad Utils.int_to_big_endian(int_bloom), 256
end

.bits(v) ⇒ Object



51
52
53
54
# File 'lib/ethereum/bloom.rb', line 51

def bits(v)
  h = Utils.keccak256 v
  BUCKETS.times.map {|i| bits_in_number get_index(h, i) }
end

.bits_in_number(v) ⇒ Object



56
57
58
# File 'lib/ethereum/bloom.rb', line 56

def bits_in_number(v)
  BITS.times.select {|i| (1<<i) & v > 0 }
end

.combine(*args) ⇒ Object



47
48
49
# File 'lib/ethereum/bloom.rb', line 47

def combine(*args)
  args.reduce(0, &:|)
end

.from(v) ⇒ Object



27
28
29
# File 'lib/ethereum/bloom.rb', line 27

def from(v)
  insert(0, v)
end

.from_array(args) ⇒ Object



31
32
33
34
# File 'lib/ethereum/bloom.rb', line 31

def from_array(args)
  blooms = args.map {|arg| from(arg) }
  combine *blooms
end

.get_index(hash, pos) ⇒ Integer

Get index for hash double-byte in bloom.

Parameters:

  • hash (String)

    value hash

  • pos (Integer)

    double-byte position in hash, can only be 0, 1, 2

Returns:

  • (Integer)

    bloom index

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
# File 'lib/ethereum/bloom.rb', line 72

def get_index(hash, pos)
  raise ArgumentError, "invalid double-byte position" unless [0,1,2].include?(pos)

  i = pos*2
  hi = hash[i].ord << 8
  lo = hash[i+1].ord
  1 << ((hi+lo) & MASK)
end

.insert(bloom, v) ⇒ Object



36
37
38
39
40
# File 'lib/ethereum/bloom.rb', line 36

def insert(bloom, v)
  h = Utils.keccak256 v
  BUCKETS.times {|i| bloom |= get_index(h, i) }
  bloom
end

.query(bloom, v) ⇒ Object



42
43
44
45
# File 'lib/ethereum/bloom.rb', line 42

def query(bloom, v)
  b = from v
  (bloom & b) == b
end