Class: Ethereum::Bloom
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`.
Constant Summary collapse
- BITS =
2048- MASK =
2047- BUCKETS =
3
Class Method Summary collapse
- .b256(int_bloom) ⇒ Object
- .bits(v) ⇒ Object
- .bits_in_number(v) ⇒ Object
- .combine(*args) ⇒ Object
- .from(v) ⇒ Object
- .from_array(args) ⇒ Object
-
.get_index(hash, pos) ⇒ Integer
Get index for hash double-byte in bloom.
- .insert(bloom, v) ⇒ Object
- .query(bloom, v) ⇒ Object
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.
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 |