zold-score 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51d91d7f9367ae85f63b248adbf08a23b14c258cd288ade1127496f621ab73f7
4
- data.tar.gz: 339c967d7dbaae591e63a173875726e0bb9f51e6c04b91dc1ab3ae94208d4b7d
3
+ metadata.gz: 633fc21de75740f2a881f83a3316a8c487e4c961869cd1b2fd06914e77672a03
4
+ data.tar.gz: 83382113630773e7b161fdac29130a6e4a8eaec9621ed5b793a1769e9b2981a2
5
5
  SHA512:
6
- metadata.gz: e51a0ee045e0f4ea19ffaddec0ca61601b3e341ad584dc84c74189f83525734703ee82f14beb33ff274923ab1feadc654dab76683dd32901d7e630c813869784
7
- data.tar.gz: bbe46c4db547135d7e450bd10540df76b2a3e5eac86bcb3c642e4a7769b35b95d129477c2b9d35de2a5ec00c9e573855ec177d6ced1e511835e01a02dc3613ce
6
+ metadata.gz: 114608fa7b37a278418bb7a0ea3e5a157c21dbca65a992da76c9857c94ec6eb31fe22b2f5c0fdd2e2a3f2762fd46d4274d2f28e6a495cd06543a5f2b82ffc1f4
7
+ data.tar.gz: e0b5ab868f2a2e9505051a0c2b5aecb082c8ff8372ca491d51b12370b9c5d25a393a486d6230ac4ca6f0c869e0adfeba08d58eac880dc8d110853354fa846c2e
data/.gitignore CHANGED
@@ -5,3 +5,5 @@ coverage/
5
5
  .DS_Store
6
6
  rdoc/
7
7
  Gemfile.lock
8
+ tmp/
9
+ lib/score_suffix
data/.travis.yml CHANGED
@@ -9,8 +9,10 @@ branches:
9
9
  install:
10
10
  - travis_retry bundle update
11
11
  - gem install pdd
12
+ - sudo apt-get install cppcheck
12
13
  script:
13
14
  - pdd -f /dev/null
15
+ - cppcheck --enable=all --quiet --suppress=unusedFunction .
14
16
  - export RUBYOPT="-W0"
15
17
  - rake --quiet
16
18
  after_success:
data/Rakefile CHANGED
@@ -33,7 +33,12 @@ def version
33
33
  Gem::Specification.load(Dir['*.gemspec'].first).version
34
34
  end
35
35
 
36
- task default: %i[clean test rubocop copyright]
36
+ task default: %i[clean compile test rubocop copyright]
37
+
38
+ require 'rake/extensiontask'
39
+ Rake::ExtensionTask.new 'score_suffix' do |ext|
40
+ ext.lib_dir = 'lib/score_suffix'
41
+ end
37
42
 
38
43
  require 'rake/testtask'
39
44
  desc 'Run all unit tests'
data/appveyor.yml CHANGED
@@ -8,13 +8,14 @@ branches:
8
8
  - gh-pages
9
9
  os: Windows Server 2012
10
10
  environment:
11
+ MAKE: mingw32-make
11
12
  matrix:
12
13
  - ruby_version: "23-x64"
13
14
  - ruby_version: "24-x64"
14
15
  - ruby_version: "25-x64"
15
16
  install:
16
17
  - ps: |
17
- $Env:PATH = "C:\Ruby${Env:ruby_version}\bin;${Env:PATH}"
18
+ $Env:PATH = "C:\Ruby${Env:ruby_version}\bin;C:\msys64\mingw64\bin;${Env:PATH}"
18
19
  if ($Env:ruby_version -match "^23" ) {
19
20
  # RubyInstaller; download OpenSSL headers from OpenKnapsack Project
20
21
  $Env:openssl_dir = "C:\Ruby${Env:ruby_version}"
@@ -33,6 +34,6 @@ build_script:
33
34
  - bundle update
34
35
  - bundle install
35
36
  test_script:
36
- - bundle exec rake --quiet
37
+ - bundle exec rake --trace
37
38
  cache:
38
39
  - vendor/bundle
@@ -0,0 +1,128 @@
1
+ // Copyright (c) 2018 Yegor Bugayenko
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the 'Software'), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ // SOFTWARE.
20
+
21
+ #include <array>
22
+ #include <random>
23
+ #include <vector>
24
+ #include <openssl/sha.h>
25
+ #include <ruby.h>
26
+
27
+ using namespace std;
28
+
29
+ static
30
+ array<uint8_t, SHA256_DIGEST_LENGTH> sha256(const string &string)
31
+ {
32
+ SHA256_CTX ctx;
33
+ SHA256_Init(&ctx);
34
+ SHA256_Update(&ctx, string.data(), string.size());
35
+ array<uint8_t, SHA256_DIGEST_LENGTH> hash;
36
+ SHA256_Final(&hash[0], &ctx);
37
+ return hash;
38
+ }
39
+
40
+ static
41
+ bool check_hash(const array<uint8_t, SHA256_DIGEST_LENGTH> &hash, int strength)
42
+ {
43
+ int current_strength = 0;
44
+ const auto rend = hash.rend();
45
+ for (auto h = hash.rbegin(); h != rend; ++h) {
46
+ if ((*h & 0x0f) != 0) {
47
+ break;
48
+ }
49
+ current_strength += (*h == 0) ? 2 : 1;
50
+ if (*h != 0) {
51
+ break;
52
+ }
53
+ }
54
+ return current_strength >= strength;
55
+ }
56
+
57
+ static
58
+ string create_nonce(uint64_t i)
59
+ {
60
+ const string chars =
61
+ "0123456789"
62
+ "abcdefghijklmnopqrstuvwxyz"
63
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
64
+
65
+ string rv;
66
+ for (int l = 0; l < 6; l++) { // Cut to 6 sym
67
+ rv += chars[i % chars.size()];
68
+ if (i < chars.size()) {
69
+ break;
70
+ }
71
+ i /= chars.size();
72
+ }
73
+ return {rv.rbegin(), rv.rend()};
74
+ }
75
+
76
+ static
77
+ string index(const string &prefix, int strength)
78
+ {
79
+ mt19937_64 random(uint64_t(time(nullptr)));
80
+
81
+ for (uint64_t i = random(); ; i++) {
82
+ const auto hash = sha256(prefix + " " + create_nonce(i));
83
+ if (check_hash(hash, strength)) {
84
+ return create_nonce(i);
85
+ }
86
+ }
87
+ }
88
+
89
+ static
90
+ VALUE ScoreSuffix_initialize(VALUE self, VALUE prefix, VALUE strength)
91
+ {
92
+ rb_iv_set(self, "@prefix", prefix);
93
+ rb_iv_set(self, "@strength", strength);
94
+ return self;
95
+ }
96
+
97
+ static
98
+ VALUE ScoreSuffix_value(VALUE self)
99
+ {
100
+ auto prefix_value = rb_iv_get(self, "@prefix");
101
+ const string prefix = StringValuePtr(prefix_value);
102
+ const int strength = NUM2INT(rb_iv_get(self, "@strength"));
103
+ const auto nonce = index(prefix, strength);
104
+ return rb_str_new2(nonce.c_str());
105
+ }
106
+
107
+ extern "C"
108
+ void Init_score_suffix()
109
+ {
110
+ VALUE module = rb_define_module("Zold");
111
+ VALUE score_suffix = rb_define_class_under(
112
+ module,
113
+ "ScoreSuffix",
114
+ rb_cObject
115
+ );
116
+ rb_define_method(
117
+ score_suffix,
118
+ "initialize",
119
+ reinterpret_cast<VALUE(*)(...)>(ScoreSuffix_initialize),
120
+ 2
121
+ );
122
+ rb_define_method(
123
+ score_suffix,
124
+ "value",
125
+ reinterpret_cast<VALUE(*)(...)>(ScoreSuffix_value),
126
+ 0
127
+ );
128
+ }
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2018 Yegor Bugayenko
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 all
13
+ # 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 NONINFINGEMENT. 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 THE
21
+ # SOFTWARE.
22
+
23
+ require 'mkmf'
24
+
25
+ # rubocop:disable Style/GlobalVars
26
+ $warnflags = ''
27
+ $CXXFLAGS << ' -std=c++11'
28
+ $CXXFLAGS.gsub!(/-Wimplicit-int/, '')
29
+ $CXXFLAGS.gsub!(/-Wdeclaration-after-statement/, '')
30
+ $CXXFLAGS.gsub!(/-Wimplicit-function-declaration/, '')
31
+ # rubocop:enable Style/GlobalVars
32
+
33
+ create_makefile 'score_suffix/score_suffix'
data/lib/zold/score.rb CHANGED
@@ -21,6 +21,7 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'openssl'
24
+ require 'score_suffix/score_suffix'
24
25
  require 'time'
25
26
 
26
27
  # Zold score.
@@ -188,23 +189,17 @@ module Zold
188
189
  # on the CPU power and the <tt>strength</tt> of the current score.
189
190
  def next
190
191
  raise 'This score is not valid' unless valid?
191
- idx = 0
192
- loop do
193
- suffix = idx.to_s(16)
194
- score = Score.new(
195
- time: @time, host: @host, port: @port,
196
- invoice: @invoice, suffixes: @suffixes + [suffix],
197
- strength: @strength
192
+ if expired?
193
+ return Score.new(
194
+ time: Time.now, host: @host, port: @port, invoice: @invoice,
195
+ suffixes: [], strength: @strength
198
196
  )
199
- return score if score.valid?
200
- if score.expired?
201
- return Score.new(
202
- time: Time.now, host: @host, port: @port, invoice: @invoice,
203
- suffixes: [], strength: @strength
204
- )
205
- end
206
- idx += 1
207
197
  end
198
+ suffix = ScoreSuffix.new(suffixes.empty? ? prefix : hash, @strength)
199
+ Score.new(
200
+ time: @time, host: @host, port: @port, invoice: @invoice,
201
+ suffixes: @suffixes + [suffix.value], strength: @strength
202
+ )
208
203
  end
209
204
 
210
205
  # The age of the score in seconds.
data/zold-score.gemspec CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
  s.rubygems_version = '2.2'
28
28
  s.required_ruby_version = '>=2.3'
29
29
  s.name = 'zold-score'
30
- s.version = '0.2.2'
30
+ s.version = '0.3.0'
31
31
  s.license = 'MIT'
32
32
  s.summary = 'Zold score'
33
33
  s.description = 'Score calculating Ruby Gem for Zold'
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
  s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
42
42
  s.add_development_dependency 'codecov', '~>0'
43
43
  s.add_development_dependency 'minitest', '~>5'
44
+ s.add_development_dependency 'rake-compiler', '~>1.0.4'
44
45
  s.add_development_dependency 'rdoc', '~>4'
45
46
  s.add_development_dependency 'rspec-rails', '~>3'
46
47
  s.add_development_dependency 'rubocop', '0.58.1'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zold-score
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-12 00:00:00.000000000 Z
11
+ date: 2018-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codecov
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.4
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.4
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rdoc
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +129,8 @@ files:
115
129
  - README.md
116
130
  - Rakefile
117
131
  - appveyor.yml
132
+ - ext/score_suffix/ScoreSuffix.cpp
133
+ - ext/score_suffix/extconf.rb
118
134
  - lib/zold/score.rb
119
135
  - test/zold/test_score.rb
120
136
  - zold-score.gemspec