zold 0.14.23 → 0.14.24

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e655198a8ea3c0a9138c5f9b9d2659163cf5a64a
4
- data.tar.gz: 6439cd7caedd7c330fe7290a5287dcd0c47d789c
3
+ metadata.gz: e8d71880e2c5c0b0d18b9af54c062dc5782bdca4
4
+ data.tar.gz: b25ac38601d91489b7da16813a96784edd57bcb7
5
5
  SHA512:
6
- metadata.gz: ac548ed4b690c03c0bcf84c1961faff7c6460d8af7e677619a5ef15049f460767cf911bdef2c2512964f4bf70ffacf1872e23a8647dd3eaa8a1d3e2592667eb5
7
- data.tar.gz: d278d016675843be28fa12df17c2958bdbf07fbc46e283aa3fd6a6e1fe922deb7893e28035c66578b57e98fbfa7b5ab65530fc94fb6940616cd610078520c884
6
+ metadata.gz: fd62a234221ae8733f83d090637c66730fb694743a63bd126604f92384986d6db36d1cd865cc6002ff384fb68411e4cfcae929abe478a3a7932b3687c193ca3d
7
+ data.tar.gz: 89f71e08e1dfa5ca33cd63f2274c78f2441098053d5145dfdbf01cb0b9f0dc19a5179b58bbe317476a70eb7eaaa62cadaf19e2d130da7bb4880eeaf56079c516
data/.rultor.yml CHANGED
@@ -7,6 +7,7 @@ install: |
7
7
  sudo gem install pdd
8
8
  release:
9
9
  script: |-
10
+ export RUBYOPT="-W0"
10
11
  [[ "${tag}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || exit -1
11
12
  rm -rf *.gem
12
13
  sed -i "s/0\.0\.0/${tag}/g" lib/zold/version.rb
data/.travis.yml CHANGED
@@ -11,6 +11,7 @@ install:
11
11
  - gem install pdd
12
12
  script:
13
13
  - pdd -f /dev/null
14
+ - export RUBYOPT="-W0"
14
15
  - rake --quiet
15
16
  after_success:
16
17
  - "bash <(curl -s https://codecov.io/bash)"
@@ -12,10 +12,11 @@ function reserve_port {
12
12
  }
13
13
 
14
14
  function wait_for_url {
15
+ i=0
15
16
  while ! curl --silent --fail $1 > /dev/null; do
16
- ((p++)) || sleep 5
17
- if ((p==30)); then
18
- echo "URL $1 is not available after $p seconds of waiting"
17
+ ((i++)) || sleep 0
18
+ if ((i==30)); then
19
+ echo "URL $1 is not available after ${i} attempts"
19
20
  exit 12
20
21
  fi
21
22
  sleep 5
@@ -23,10 +24,11 @@ function wait_for_url {
23
24
  }
24
25
 
25
26
  function wait_for_port {
27
+ i=0
26
28
  while ! nc -z localhost $1; do
27
- ((p++)) || sleep 5
28
- if ((p==30)); then
29
- echo "Port $1 is not available after $p seconds of waiting"
29
+ ((i++)) || sleep 0
30
+ if ((i==30)); then
31
+ echo "Port $1 is not available after ${i} attempts"
30
32
  exit 13
31
33
  fi
32
34
  sleep 5
@@ -34,10 +36,11 @@ function wait_for_port {
34
36
  }
35
37
 
36
38
  function wait_for_file {
39
+ i=0
37
40
  while [ ! -f $1 ]; do
38
- ((c++)) || sleep 5
39
- if ((c==30)); then
40
- echo "File $1 not found, giving up after $c seconds of waiting"
41
+ ((i++)) || sleep 0
42
+ if ((i==30)); then
43
+ echo "File $1 not found, giving up after ${i} attempts"
41
44
  exit 14
42
45
  fi
43
46
  sleep 5
@@ -48,13 +51,14 @@ function halt_nodes {
48
51
  for p in "$@"; do
49
52
  pid=$(curl --silent "http://localhost:$p/pid?halt=test" || echo 'absent')
50
53
  if [[ "${pid}" =~ ^[0-9]+$ ]]; then
54
+ i=0
51
55
  while kill -0 ${pid}; do
52
- ((c++)) || sleep 5
53
- if ((c==30)); then
54
- echo "Waiting for process ${pid} to die"
56
+ ((i++)) || sleep 0
57
+ if ((i==30)); then
58
+ echo "Process ${pid} didn't die, it's a bug"
55
59
  exit 15
56
60
  fi
57
- echo "Still waiting for process ${pid} to die, cycle no.${c}"
61
+ echo "Still waiting for process ${pid} to die, attempt no.${i}"
58
62
  sleep 5
59
63
  done
60
64
  echo "Process ${pid} is dead!"
@@ -44,15 +44,16 @@ zold remote add localhost ${second}
44
44
  # to be visible there. We are doing a number of attempts with a small
45
45
  # delay between them, in order to give the first node a chance to distribute
46
46
  # the wallet.
47
+ i=0
47
48
  until zold fetch 0000000000000000 --ignore-score-weakness; do
48
49
  echo 'Failed to fetch, let us try again'
49
- ((i++)) || sleep 1
50
+ ((i++)) || sleep 0
50
51
  if ((i==5)); then
51
52
  cat ${first}/log.txt
52
53
  echo "The wallet has not been distributed, after ${i} attempts"
53
54
  exit 9
54
55
  fi
55
- sleep 1
56
+ sleep 5
56
57
  done
57
58
 
58
59
  # Here we check the JSON of the first node to make sure all status
@@ -74,14 +75,15 @@ fi
74
75
  # Now, we remove the wallet from the second node and expect the first
75
76
  # one to "spread" it again, almost immediately.
76
77
  rm ${second}/0000000000000000.z
78
+ i=0
77
79
  until zold fetch 0000000000000000 --ignore-score-weakness; do
78
80
  echo 'Failed to fetch, let us try again'
79
- ((i++)) || sleep 1
81
+ ((i++)) || sleep 0
80
82
  if ((i==5)); then
81
83
  cat ${first}/log.txt
82
84
  echo "The wallet 0000000000000000 has not been spread, after ${i} attempts"
83
85
  exit 8
84
86
  fi
85
- sleep 1
87
+ sleep 5
86
88
  done
87
89
 
@@ -30,9 +30,10 @@ zold push 0000000000000000
30
30
  zold remote clean
31
31
  zold remote add localhost ${second}
32
32
 
33
+ i=0
33
34
  until zold fetch 0000000000000000 --ignore-score-weakness; do
34
35
  echo 'Failed to fetch, let us try again'
35
- ((i++)) || sleep 2
36
+ ((i++)) || sleep 0
36
37
  if ((i==5)); then
37
38
  cat ${first}/log.txt
38
39
  echo "The wallet has not been distributed, after ${i} attempts"
@@ -83,6 +83,9 @@ Available options:"
83
83
  o.integer '--min-score',
84
84
  "The minimum score required for winning the election (default: #{Tax::EXACT_SCORE})",
85
85
  default: Tax::EXACT_SCORE
86
+ o.integer '--max-winners',
87
+ 'The maximum amount of election winners the election (default: 1)',
88
+ default: 1
86
89
  o.bool '--force',
87
90
  'Add/remove if if this operation is not possible',
88
91
  default: false
@@ -197,11 +200,11 @@ Available options:"
197
200
  r.assert_score_value(score, opts['min-score']) unless opts['ignore-score-value']
198
201
  scores << score
199
202
  end
200
- scores = scores.sample(1)
203
+ scores = scores.sample(opts['max-winners'])
201
204
  if scores.empty?
202
205
  @log.info("No winners elected out of #{@remotes.all.count} remotes")
203
206
  else
204
- @log.info("Elected: #{scores[0]}")
207
+ scores.each { |s| @log.info("Elected: #{s}") }
205
208
  end
206
209
  scores
207
210
  end
@@ -239,7 +242,7 @@ it's recommended to reboot, but I don't do it because of --never-reboot")
239
242
  end
240
243
  if deep
241
244
  json['all'].each do |s|
242
- add(s['host'], s['port'], opts) unless @remotes.exists?(s['host'], s['port'])
245
+ @remotes.add(s['host'], s['port'])
243
246
  end
244
247
  end
245
248
  capacity << { host: score.host, port: score.port, count: json['all'].count }
@@ -32,7 +32,12 @@ require_relative '../verbose_thread'
32
32
  module Zold
33
33
  # The entrance
34
34
  class AsyncEntrance
35
- THREADS = Concurrent.processor_count * 4
35
+ # How many threads to use for processing
36
+ THREADS = Concurrent.processor_count * 8
37
+
38
+ # Max items in the queue. If there will be more, push() requests
39
+ # will be rejected.
40
+ MAX_QUEUE = 128
36
41
 
37
42
  def initialize(entrance, dir, log: Log::Quiet.new)
38
43
  raise 'Entrance can\'t be nil' if entrance.nil?
@@ -90,6 +95,7 @@ module Zold
90
95
 
91
96
  # Always returns an array with a single ID of the pushed wallet
92
97
  def push(id, body)
98
+ raise 'Queue is too long, try again later' if Dir.new(@dir).count > AsyncEntrance::MAX_QUEUE
93
99
  @mutex.synchronize do
94
100
  AtomicFile.new(File.join(@dir, id.to_s)).write(body)
95
101
  end
@@ -25,6 +25,7 @@ STDOUT.sync = true
25
25
  require 'json'
26
26
  require 'sinatra/base'
27
27
  require 'webrick'
28
+ require 'get_process_mem'
28
29
  require 'diffy'
29
30
  require 'concurrent'
30
31
  require_relative '../backtrace'
@@ -94,9 +95,10 @@ while #{settings.address} is in '#{settings.network}'"
94
95
  error(400, 'The score is weak') if s.strength < Score::STRENGTH && !settings.ignore_score_weakness
95
96
  if s.value > 3
96
97
  require_relative '../commands/remote'
97
- Remote.new(remotes: settings.remotes, log: settings.log).run(
98
- ['remote', 'add', s.host, s.port.to_s, '--force', "--network=#{settings.network}"]
99
- )
98
+ cmd = Remote.new(remotes: settings.remotes, log: settings.log)
99
+ cmd.run(['remote', 'add', s.host, s.port.to_s, '--force', "--network=#{settings.network}"])
100
+ cmd.run(%w[remote trim])
101
+ cmd.run(%w[remote select])
100
102
  else
101
103
  settings.log.debug("#{request.url}: the score is too weak: #{s}")
102
104
  end
@@ -153,6 +155,7 @@ while #{settings.address} is in '#{settings.network}'"
153
155
  score: score.to_h,
154
156
  pid: Process.pid,
155
157
  cpus: Concurrent.processor_count,
158
+ memory: GetProcessMem.new.inspect.bytes,
156
159
  platform: RUBY_PLATFORM,
157
160
  uptime: `uptime`.strip,
158
161
  threads: "#{Thread.list.select { |t| t.status == 'run' }.count}/#{Thread.list.count}",
data/lib/zold/remotes.rb CHANGED
@@ -149,7 +149,6 @@ module Zold
149
149
  raise 'Port can\'t be zero' if port.zero?
150
150
  raise 'Port can\'t be negative' if port.negative?
151
151
  raise 'Port can\'t be over 65536' if port > 0xffff
152
- raise "#{host}:#{port} already exists" if exists?(host, port)
153
152
  modify do |list|
154
153
  list + [{ host: host.downcase, port: port, score: 0 }]
155
154
  end
data/lib/zold/score.rb CHANGED
@@ -128,7 +128,7 @@ module Zold
128
128
  port,
129
129
  invoice,
130
130
  suffixes.join(' ')
131
- ].join(' ')
131
+ ].join(' ').strip
132
132
  end
133
133
 
134
134
  def to_h
data/lib/zold/version.rb CHANGED
@@ -25,6 +25,6 @@
25
25
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
26
26
  # License:: MIT
27
27
  module Zold
28
- VERSION = '0.14.23'
28
+ VERSION = '0.14.24'
29
29
  PROTOCOL = 2
30
30
  end
@@ -178,4 +178,43 @@ class FrontTest < Minitest::Test
178
178
  sec = (Time.now - start) / total
179
179
  test_log.info("Average response time is #{sec.round(2)}s")
180
180
  end
181
+
182
+ def app
183
+ Zold::Front
184
+ end
185
+
186
+ def test_headers_are_being_set_correctly
187
+ Time.stub :now, Time.at(0) do
188
+ FakeNode.new(log: test_log).run(['--ignore-score-weakness']) do |port|
189
+ response = Zold::Http.new(uri: URI("http://localhost:#{port}/"), score: nil).get
190
+ assert_equal(
191
+ 'no-cache',
192
+ response.header['Cache-Control']
193
+ )
194
+ assert_equal(
195
+ 'close',
196
+ response.header['Connection']
197
+ )
198
+ assert_equal(
199
+ app.settings.version,
200
+ response.header['X-Zold-Version']
201
+ )
202
+ assert_equal(
203
+ app.settings.protocol.to_s,
204
+ response.header[Zold::Http::PROTOCOL_HEADER]
205
+ )
206
+ assert_equal(
207
+ '*',
208
+ response.header['Access-Control-Allow-Origin']
209
+ )
210
+ score = Zold::Score.new(
211
+ time: Time.now, host: 'localhost', port: port, invoice: 'NOPREFIX@ffffffffffffffff', strength: 2
212
+ )
213
+ assert_equal(
214
+ score.to_s,
215
+ response.header[Zold::Http::SCORE_HEADER]
216
+ )
217
+ end
218
+ end
219
+ end
181
220
  end
data/zold.gemspec CHANGED
@@ -59,6 +59,7 @@ and suggests a different architecture for digital wallet maintenance.'
59
59
  s.add_runtime_dependency 'diffy', '~>3.2'
60
60
  s.add_runtime_dependency 'dry-struct', '~>0.5.0'
61
61
  s.add_runtime_dependency 'dry-types', '~>0.13.2'
62
+ s.add_runtime_dependency 'get_process_mem', '0.2.2'
62
63
  s.add_runtime_dependency 'json', '~>1.8'
63
64
  s.add_runtime_dependency 'openssl', '~>2.1'
64
65
  s.add_runtime_dependency 'rainbow', '~>3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.23
4
+ version: 0.14.24
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-07-28 00:00:00.000000000 Z
11
+ date: 2018-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.13.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: get_process_mem
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.2.2
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.2.2
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: json
85
99
  requirement: !ruby/object:Gem::Requirement