ciri 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +8 -3
- data/README.md +5 -0
- data/Rakefile +24 -2
- data/ciri-rlp/.gitignore +11 -0
- data/ciri-rlp/.rspec +3 -0
- data/ciri-rlp/.travis.yml +5 -0
- data/ciri-rlp/CODE_OF_CONDUCT.md +74 -0
- data/ciri-rlp/Gemfile +6 -0
- data/ciri-rlp/Gemfile.lock +39 -0
- data/ciri-rlp/LICENSE.txt +21 -0
- data/ciri-rlp/README.md +98 -0
- data/ciri-rlp/Rakefile +6 -0
- data/ciri-rlp/bin/console +14 -0
- data/ciri-rlp/bin/setup +8 -0
- data/ciri-rlp/ciri-rlp.gemspec +28 -0
- data/{lib → ciri-rlp/lib}/ciri/rlp.rb +2 -2
- data/ciri-rlp/lib/ciri/rlp/decode.rb +144 -0
- data/ciri-rlp/lib/ciri/rlp/encode.rb +140 -0
- data/ciri-rlp/lib/ciri/rlp/serializable.rb +241 -0
- data/ciri-rlp/lib/ciri/rlp/version.rb +5 -0
- data/ciri-rlp/spec/ciri/fixture_spec.rb +95 -0
- data/ciri-rlp/spec/ciri/rlp/decode_spec.rb +68 -0
- data/ciri-rlp/spec/ciri/rlp/encode_spec.rb +72 -0
- data/ciri-rlp/spec/ciri/rlp/serializable_spec.rb +147 -0
- data/ciri-rlp/spec/ciri/rlp_spec.rb +5 -0
- data/ciri-rlp/spec/spec_helper.rb +14 -0
- data/ciri-utils/.gitignore +11 -0
- data/ciri-utils/.rspec +3 -0
- data/ciri-utils/.travis.yml +5 -0
- data/ciri-utils/CODE_OF_CONDUCT.md +74 -0
- data/ciri-utils/Gemfile +6 -0
- data/ciri-utils/Gemfile.lock +37 -0
- data/ciri-utils/LICENSE.txt +21 -0
- data/ciri-utils/README.md +61 -0
- data/ciri-utils/Rakefile +6 -0
- data/ciri-utils/bin/console +14 -0
- data/ciri-utils/bin/setup +8 -0
- data/ciri-utils/ciri-utils.gemspec +28 -0
- data/{lib → ciri-utils/lib}/ciri/utils.rb +9 -33
- data/{lib → ciri-utils/lib}/ciri/utils/logger.rb +0 -0
- data/{lib → ciri-utils/lib}/ciri/utils/number.rb +15 -12
- data/ciri-utils/lib/ciri/utils/version.rb +5 -0
- data/ciri-utils/spec/ciri/utils_spec.rb +5 -0
- data/ciri-utils/spec/spec_helper.rb +14 -0
- data/ciri.gemspec +6 -3
- data/lib/ciri/bloom_filter.rb +83 -0
- data/lib/ciri/chain.rb +67 -130
- data/lib/ciri/chain/header.rb +2 -2
- data/lib/ciri/chain/header_chain.rb +136 -0
- data/lib/ciri/chain/transaction.rb +1 -1
- data/lib/ciri/crypto.rb +2 -2
- data/lib/ciri/db/account_db.rb +145 -0
- data/lib/ciri/db/backend/memory.rb +24 -1
- data/lib/ciri/devp2p/peer.rb +1 -1
- data/lib/ciri/devp2p/rlpx/connection.rb +5 -5
- data/lib/ciri/eth/peer.rb +3 -3
- data/lib/ciri/eth/protocol_manage.rb +3 -3
- data/lib/ciri/eth/protocol_messages.rb +27 -26
- data/lib/ciri/ethash.rb +18 -3
- data/lib/ciri/evm.rb +101 -56
- data/lib/ciri/{utils/lib_c.rb → evm/errors.rb} +28 -18
- data/lib/ciri/evm/instruction.rb +3 -1
- data/lib/ciri/evm/{serialize.rb → log_entry.rb} +9 -27
- data/lib/ciri/evm/machine_state.rb +21 -2
- data/lib/ciri/evm/op.rb +19 -16
- data/lib/ciri/evm/state.rb +61 -0
- data/lib/ciri/evm/vm.rb +78 -102
- data/lib/ciri/forks.rb +1 -4
- data/lib/ciri/forks/base.rb +55 -0
- data/lib/ciri/forks/frontier.rb +37 -10
- data/lib/ciri/forks/frontier/cost.rb +186 -0
- data/lib/ciri/key.rb +1 -1
- data/lib/ciri/pow.rb +1 -1
- data/lib/ciri/rlp/decode.rb +6 -3
- data/lib/ciri/rlp/encode.rb +10 -10
- data/lib/ciri/rlp/serializable.rb +12 -9
- data/lib/ciri/serialize.rb +58 -0
- data/lib/ciri/trie.rb +362 -0
- data/lib/ciri/trie/nibbles.rb +97 -0
- data/lib/ciri/trie/nodes.rb +187 -0
- data/lib/ciri/{evm → types}/account.rb +20 -13
- data/lib/ciri/types/address.rb +16 -11
- data/lib/ciri/types/number.rb +73 -0
- data/lib/ciri/types/receipt.rb +57 -0
- data/lib/ciri/version.rb +1 -1
- metadata +82 -19
- data/lib/ciri/evm/forks/frontier.rb +0 -183
data/ciri-utils/Gemfile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ciri-utils (0.1.0)
|
5
|
+
digest-sha3 (~> 1.1.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.3)
|
11
|
+
digest-sha3 (1.1.0)
|
12
|
+
rake (10.3.2)
|
13
|
+
rspec (3.7.0)
|
14
|
+
rspec-core (~> 3.7.0)
|
15
|
+
rspec-expectations (~> 3.7.0)
|
16
|
+
rspec-mocks (~> 3.7.0)
|
17
|
+
rspec-core (3.7.1)
|
18
|
+
rspec-support (~> 3.7.0)
|
19
|
+
rspec-expectations (3.7.0)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.7.0)
|
22
|
+
rspec-mocks (3.7.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.7.0)
|
25
|
+
rspec-support (3.7.1)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
bundler (~> 1.16)
|
32
|
+
ciri-utils!
|
33
|
+
rake (~> 10.0)
|
34
|
+
rspec (~> 3.0)
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
1.16.1
|
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jiang Jinyang
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Ciri::Utils
|
2
|
+
|
3
|
+
Toolkit module of [Ciri](https://github.com/ciri-ethereum/ciri)
|
4
|
+
|
5
|
+
Functions include: big endian encode/decode, hex/bytes convert, keccak256 ...
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'ciri-utils'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ciri-utils
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
# keccak256
|
27
|
+
Ciri::Utils.sha3("...")
|
28
|
+
|
29
|
+
# secret_compare
|
30
|
+
Ciri::Utils.sha3(password, secret)
|
31
|
+
|
32
|
+
# convert between hex and bytes
|
33
|
+
Ciri::Utils.to_bytes("0x...")
|
34
|
+
Ciri::Utils.to_hex("\x00...")
|
35
|
+
|
36
|
+
# big endian encode and decode
|
37
|
+
Ciri::Utils.big_endian_encode(200)
|
38
|
+
Ciri::Utils.big_endian_decode("....")
|
39
|
+
|
40
|
+
# ....
|
41
|
+
```
|
42
|
+
|
43
|
+
See source code to find other methods.
|
44
|
+
|
45
|
+
## Development
|
46
|
+
|
47
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
48
|
+
|
49
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ciri-utils. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
58
|
+
|
59
|
+
## Code of Conduct
|
60
|
+
|
61
|
+
Everyone interacting in the Ciri::Utils project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ciri-utils/blob/master/CODE_OF_CONDUCT.md).
|
data/ciri-utils/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ciri/utils"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "ciri/utils/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ciri-utils"
|
8
|
+
spec.version = Ciri::Utils::VERSION
|
9
|
+
spec.authors = ["Jiang Jinyang"]
|
10
|
+
spec.email = ["jjyruby@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Toolkit module of [Ciri](https://github.com/ciri-ethereum/ciri)}
|
13
|
+
spec.description = %q{Functions include: big endian encode/decode, hex/bytes convert, keccak256 ...}
|
14
|
+
spec.homepage = "https://github.com/ciri-ethereum/ciri"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency 'digest-sha3', '~> 1.1.0'
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
@@ -1,25 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# Copyright (c) 2018, by Jiang Jinyang. <https://justjjy.com>
|
4
|
-
#
|
5
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
# of this software and associated documentation files (the "Software"), to deal
|
7
|
-
# in the Software without restriction, including without limitation the rights
|
8
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
# copies of the Software, and to permit persons to whom the Software is
|
10
|
-
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be included in
|
13
|
-
# all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
22
|
-
|
1
|
+
require "ciri/utils/version"
|
23
2
|
|
24
3
|
require 'digest/sha3'
|
25
4
|
require_relative 'utils/number'
|
@@ -41,24 +20,22 @@ module Ciri
|
|
41
20
|
s1.size == s2.size && s1.each_byte.each_with_index.map {|b, i| b ^ s2[i].ord}.reduce(0, :+) == 0
|
42
21
|
end
|
43
22
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
data
|
23
|
+
def to_bytes(hex)
|
24
|
+
hex = hex[2..-1] if hex.start_with?('0x')
|
25
|
+
[hex].pack("H*")
|
48
26
|
end
|
49
27
|
|
50
28
|
def hex_to_number(hex)
|
51
|
-
big_endian_decode
|
29
|
+
big_endian_decode to_bytes(hex)
|
52
30
|
end
|
53
31
|
|
54
|
-
def
|
32
|
+
def to_hex(data)
|
55
33
|
hex = data.to_s.unpack("H*").first
|
56
|
-
|
57
|
-
hex
|
34
|
+
'0x' + hex
|
58
35
|
end
|
59
36
|
|
60
37
|
def number_to_hex(number)
|
61
|
-
|
38
|
+
to_hex big_endian_encode(number)
|
62
39
|
end
|
63
40
|
|
64
41
|
def create_ec_pk(raw_pubkey: nil, raw_privkey: nil)
|
@@ -78,7 +55,7 @@ module Ciri
|
|
78
55
|
str.gsub(/[A-Z]/) {|a| "_" + a.downcase}
|
79
56
|
end
|
80
57
|
|
81
|
-
def
|
58
|
+
def blank_bytes?(item)
|
82
59
|
return true if item.is_a?(String) && item.each_byte.all?(&:zero?)
|
83
60
|
blank?(item)
|
84
61
|
end
|
@@ -102,6 +79,5 @@ module Ciri
|
|
102
79
|
end
|
103
80
|
|
104
81
|
BLANK_SHA3 = Utils.sha3(''.b).freeze
|
105
|
-
|
106
82
|
end
|
107
83
|
end
|
File without changes
|
@@ -26,18 +26,9 @@ module Ciri
|
|
26
26
|
module Number
|
27
27
|
extend self
|
28
28
|
|
29
|
-
def big_endian_encode(n, zero = ''.b)
|
30
|
-
|
31
|
-
|
32
|
-
elsif n > 0
|
33
|
-
big_endian_encode(n / 256) + (n % 256).chr
|
34
|
-
else
|
35
|
-
raise ArgumentError.new("can't encode negative number #{n}")
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def big_endian_encode_to_size(n, zero = ''.b, size:)
|
40
|
-
big_endian_encode(n, zero).rjust(size, "\x00".b)
|
29
|
+
def big_endian_encode(n, zero = ''.b, size: nil)
|
30
|
+
b = big_endian_encode_raw(n, zero)
|
31
|
+
size.nil? ? b : b.rjust(size, "\x00".b)
|
41
32
|
end
|
42
33
|
|
43
34
|
def big_endian_decode(input)
|
@@ -62,6 +53,18 @@ module Ciri
|
|
62
53
|
m.zero? ? size : size + 1
|
63
54
|
end
|
64
55
|
|
56
|
+
private
|
57
|
+
|
58
|
+
def big_endian_encode_raw(n, zero = ''.b)
|
59
|
+
if n == 0
|
60
|
+
zero
|
61
|
+
elsif n > 0
|
62
|
+
big_endian_encode(n / 256) + (n % 256).chr
|
63
|
+
else
|
64
|
+
raise ArgumentError.new("can't encode negative number #{n}")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
65
68
|
end
|
66
69
|
end
|
67
70
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "ciri/utils"
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
# Enable flags like --only-failures and --next-failure
|
6
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
7
|
+
|
8
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
9
|
+
config.disable_monkey_patching!
|
10
|
+
|
11
|
+
config.expect_with :rspec do |c|
|
12
|
+
c.syntax = :expect
|
13
|
+
end
|
14
|
+
end
|
data/ciri.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["Jiang Jinyang"]
|
9
9
|
spec.email = ["jjyruby@gmail.com"]
|
10
10
|
|
11
|
-
spec.summary = %q{Ciri
|
12
|
-
spec.description = %q{Ciri
|
11
|
+
spec.summary = %q{Ciri project intent to implement a full feature set Ethereum client.}
|
12
|
+
spec.description = %q{Ciri aims to be a feature complete, long maintained and stable Ethereum client.}
|
13
13
|
spec.homepage = "https://github.com/ciri-ethereum/ciri"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -20,9 +20,12 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f)}
|
21
21
|
spec.require_paths = ["lib"]
|
22
22
|
|
23
|
+
# components
|
24
|
+
spec.add_dependency 'ciri-utils', '~> 0.1.0'
|
25
|
+
spec.add_dependency 'ciri-rlp', '~> 0.1.1'
|
26
|
+
|
23
27
|
spec.add_dependency 'ffi', '~> 1.9.23'
|
24
28
|
spec.add_dependency 'lru_redux', '~> 1.1.0'
|
25
|
-
spec.add_dependency 'digest-sha3', '~> 1.1.0'
|
26
29
|
spec.add_dependency 'bitcoin-secp256k1', '~> 0.4.0'
|
27
30
|
spec.add_dependency 'concurrent-ruby', '~> 1.0.5'
|
28
31
|
spec.add_dependency 'snappy', '~> 0.0.17'
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2018, by Jiang Jinyang. <https://justjjy.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
|
24
|
+
require 'ciri/utils'
|
25
|
+
module Ciri
|
26
|
+
|
27
|
+
# modified from py-evm
|
28
|
+
class BloomFilter
|
29
|
+
|
30
|
+
def initialize(value = 0)
|
31
|
+
@value = value
|
32
|
+
end
|
33
|
+
|
34
|
+
def <<(value)
|
35
|
+
get_bloom_bits(value).each do |v|
|
36
|
+
@value |= v
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def extend(list)
|
41
|
+
list.each do |value|
|
42
|
+
self << value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def |(value)
|
47
|
+
BloomFilter.new(@value | value.to_i)
|
48
|
+
end
|
49
|
+
|
50
|
+
def include?(value)
|
51
|
+
get_bloom_bits(value).all? do |bits|
|
52
|
+
@value & bits != 0
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_i
|
57
|
+
@value
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.from_iterable(list)
|
61
|
+
b = BloomFilter.new
|
62
|
+
b.extend(list)
|
63
|
+
b
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def get_bloom_bits(value)
|
69
|
+
value_hash = Utils.sha3(value)
|
70
|
+
get_chunks_for_bloom(value_hash).map {|v| chunk_to_bloom_bits(v)}
|
71
|
+
end
|
72
|
+
|
73
|
+
def get_chunks_for_bloom(value)
|
74
|
+
value[0..5].each_char.each_slice(2).map(&:join)
|
75
|
+
end
|
76
|
+
|
77
|
+
def chunk_to_bloom_bits(value)
|
78
|
+
high, low = value.each_byte.to_a
|
79
|
+
1 << ((low + (high << 8)) & 2047)
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|