zold 0.11.6 → 0.11.7

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: d4b85a4409d351be25bfa99d529cb7805474c413
4
- data.tar.gz: 17c7832d676da4213cccf791dc38a175d2a45667
3
+ metadata.gz: 12b394868e7a954a6a72411f2326339ca57cd6c2
4
+ data.tar.gz: e39cfab10878262a6431c4bd2bd5a06a3939a973
5
5
  SHA512:
6
- metadata.gz: 214f26718d2dcf269e355bb574edb51c4b2d15dc6b0679ef8a721e77595751b551d4e48c3fe83d65889b612453a2a7e7cea45be8a22f063629ffa33ab663d0dc
7
- data.tar.gz: f488b84aa410a24cf9c73e3acfebe7943378889a183fa923937856bbdb8ee27ec11e8bdd3a00cc75bbf4ad5b5cbe431abeb3ea291581161a0bff63abb3b0a0f8
6
+ metadata.gz: b414866acdc3a4a099e293c5ec6358d9ad0ffbd223306469e38477736430818d84b5bc7eda44e120f69af5c9aa202e5ebae1989a8b5257f7e8a7ff8e5e701977
7
+ data.tar.gz: 6edc6beb6b837bf7f9151cb3f9e652bd737c2c8362d558d0c039a6edc6806f2344bec791bda91fb3544966bd78a8508ebcd0fe7e31b85feccfaabcd2a4633097
@@ -0,0 +1,48 @@
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 all
11
+ # 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
+ # Atomic file.
22
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
23
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
24
+ # License:: MIT
25
+ module Zold
26
+ # Atomic file
27
+ class AtomicFile
28
+ def initialize(file)
29
+ raise 'File can\'t be nil' if file.nil?
30
+ @file = file
31
+ end
32
+
33
+ def read
34
+ File.open(@file, 'r') do |f|
35
+ f.flock(File::LOCK_SH)
36
+ f.read
37
+ end
38
+ end
39
+
40
+ def write(content)
41
+ raise 'Content can\'t be nil' if content.nil?
42
+ File.open(@file, 'w+') do |f|
43
+ f.flock(File::LOCK_EX)
44
+ f.write(content)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -70,7 +70,7 @@ Available options:"
70
70
  cps[1..-1].each do |c|
71
71
  extra = Wallet.new(c[:path])
72
72
  if extra.network != main.network
73
- @log.error("The wallet is from a different network '#{wallet.version}', ours is '#{@network}'")
73
+ @log.error("The wallet is from a different network '#{extra.network}', ours is '#{main.network}'")
74
74
  next
75
75
  end
76
76
  if extra.key != main.key
@@ -114,7 +114,7 @@ Available options:"
114
114
 
115
115
  def reset
116
116
  @remotes.reset
117
- @log.debug('Remote nodes set back to default')
117
+ @log.debug("Remote nodes set back to default, #{@remotes.all.count} total")
118
118
  end
119
119
 
120
120
  def add(host, port, opts)
@@ -123,7 +123,7 @@ Available options:"
123
123
  @log.info("#{host}:#{port} already exists in the list")
124
124
  else
125
125
  @remotes.add(host, port)
126
- @log.info("#{host}:#{port} added to the list")
126
+ @log.info("#{host}:#{port} added to the list, #{@remotes.all.count} total")
127
127
  end
128
128
  @log.info("There are #{@remotes.all.count} remote nodes in the list")
129
129
  end
@@ -143,6 +143,7 @@ Available options:"
143
143
  @remotes.all.each do |r|
144
144
  remove(r[:host], r[:port], opts) if r[:errors] > 20
145
145
  end
146
+ @log.info("The list of remotes trimmed, #{@remotes.all.count} nodes left there")
146
147
  end
147
148
 
148
149
  def update(opts, deep = true)
data/lib/zold/copies.rb CHANGED
@@ -20,6 +20,7 @@
20
20
 
21
21
  require 'time'
22
22
  require 'csv'
23
+ require_relative 'atomic_file'
23
24
 
24
25
  # The list of copies.
25
26
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -74,7 +75,7 @@ module Zold
74
75
  .max
75
76
  max = 0 if max.nil?
76
77
  name = (max + 1).to_s
77
- File.write(File.join(@dir, name), content)
78
+ AtomicFile.new(File.join(@dir, name)).write(content)
78
79
  else
79
80
  name = target[:name]
80
81
  end
@@ -118,8 +119,7 @@ module Zold
118
119
  end
119
120
 
120
121
  def save(list)
121
- File.write(
122
- file,
122
+ AtomicFile.new(file).write(
123
123
  list.map do |r|
124
124
  [
125
125
  r[:name], r[:host],
@@ -22,6 +22,7 @@ require 'time'
22
22
  require_relative '../log'
23
23
  require_relative '../score'
24
24
  require_relative '../verbose_thread'
25
+ require_relative '../atomic_file'
25
26
 
26
27
  # The farm of scores.
27
28
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -105,7 +106,7 @@ module Zold
105
106
  private
106
107
 
107
108
  def save(score)
108
- File.write(@cache, (history + [score]).map(&:to_s).join("\n"))
109
+ AtomicFile.new(@cache).write((history + [score]).map(&:to_s).join("\n"))
109
110
  end
110
111
 
111
112
  def history(max = 16)
@@ -87,13 +87,20 @@ module Zold
87
87
  end
88
88
 
89
89
  get '/robots.txt' do
90
+ content_type 'text/plain'
90
91
  'User-agent: *'
91
92
  end
92
93
 
93
- get '/v' do
94
+ get '/version' do
95
+ content_type 'text/plain'
94
96
  VERSION
95
97
  end
96
98
 
99
+ get '/score' do
100
+ content_type 'text/plain'
101
+ score.to_s
102
+ end
103
+
97
104
  get '/favicon.ico' do
98
105
  if score.value >= 16
99
106
  redirect 'https://www.zold.io/images/logo-green.png'
data/lib/zold/remotes.rb CHANGED
@@ -22,6 +22,7 @@ require 'csv'
22
22
  require 'uri'
23
23
  require 'fileutils'
24
24
  require_relative 'node/farm'
25
+ require_relative 'atomic_file'
25
26
 
26
27
  # The list of remotes.
27
28
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -64,7 +65,7 @@ module Zold
64
65
  end
65
66
 
66
67
  def assert_code(code, response)
67
- msg = response.message
68
+ msg = response.message.strip
68
69
  return if response.code.to_i == code
69
70
  @log.debug("#{response.code} \"#{response.message}\" at \"#{response.body}\"")
70
71
  raise "Unexpected HTTP code #{response.code}, instead of #{code}" if msg.empty?
@@ -113,7 +114,7 @@ module Zold
113
114
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
114
115
  raise 'Port can\'t be negative' if port < 0
115
116
  raise 'Port can\'t be over 65536' if port > 0xffff
116
- raise "#{host}:#{port} alread exists" if exists?(host, port)
117
+ raise "#{host}:#{port} already exists" if exists?(host, port)
117
118
  list = load
118
119
  list << { host: host.downcase, port: port, score: 0 }
119
120
  list.uniq! { |r| "#{r[:host]}:#{r[:port]}" }
@@ -145,11 +146,9 @@ module Zold
145
146
 
146
147
  def error(host, port = Remotes::PORT)
147
148
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
148
- raise "#{host}:#{port} is absent" unless exists?(host, port)
149
149
  list = load
150
- list.find do |r|
151
- r[:host] == host.downcase && r[:port] == port
152
- end[:errors] += 1
150
+ raise "#{host}:#{port} is absent among #{list.count} remotes" unless exists?(host, port)
151
+ list.find { |r| r[:host] == host.downcase && r[:port] == port }[:errors] += 1
153
152
  save(list)
154
153
  end
155
154
 
@@ -157,9 +156,7 @@ module Zold
157
156
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
158
157
  raise "#{host}:#{port} is absent" unless exists?(host, port)
159
158
  list = load
160
- list.find do |r|
161
- r[:host] == host.downcase && r[:port] == port
162
- end[:score] = score
159
+ list.find { |r| r[:host] == host.downcase && r[:port] == port }[:score] = score
163
160
  save(list)
164
161
  end
165
162
 
@@ -178,8 +175,7 @@ module Zold
178
175
  end
179
176
 
180
177
  def save(list)
181
- File.write(
182
- file,
178
+ AtomicFile.new(file).write(
183
179
  list.map do |r|
184
180
  [
185
181
  r[:host],
@@ -34,7 +34,8 @@ module Zold
34
34
  def run(safe = false)
35
35
  yield
36
36
  rescue StandardError => e
37
- @log.error("#{e.class.name}: #{e.message} #{e.backtrace.join("\n\t")}")
37
+ @log.error("#{e.class.name}: #{e.message}")
38
+ @log.debug(e.backtrace.join("\n\t"))
38
39
  raise e unless safe
39
40
  end
40
41
  end
data/lib/zold/version.rb CHANGED
@@ -23,5 +23,5 @@
23
23
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
24
24
  # License:: MIT
25
25
  module Zold
26
- VERSION = '0.11.6'.freeze
26
+ VERSION = '0.11.7'.freeze
27
27
  end
data/lib/zold/wallet.rb CHANGED
@@ -26,6 +26,7 @@ require_relative 'txn'
26
26
  require_relative 'tax'
27
27
  require_relative 'amount'
28
28
  require_relative 'signature'
29
+ require_relative 'atomic_file'
29
30
 
30
31
  # The wallet.
31
32
  #
@@ -78,7 +79,7 @@ module Zold
78
79
  def init(id, pubkey, overwrite: false, network: 'test')
79
80
  raise "File '#{@file}' already exists" if File.exist?(@file) && !overwrite
80
81
  raise "Invalid network name '#{network}'" unless network =~ /^[a-z]{4,16}$/
81
- File.write(@file, "#{network}\n#{VERSION}\n#{id}\n#{pubkey.to_pub}\n\n")
82
+ AtomicFile.new(@file).write("#{network}\n#{VERSION}\n#{id}\n#{pubkey.to_pub}\n\n")
82
83
  end
83
84
 
84
85
  def root?
@@ -61,6 +61,7 @@ class TestRemote < Minitest::Test
61
61
  )
62
62
  cmd = Zold::Remote.new(remotes: remotes, log: test_log)
63
63
  cmd.run(%w[remote clean])
64
+ assert(remotes.all.empty?)
64
65
  cmd.run(['remote', 'add', zero.host, zero.port.to_s])
65
66
  cmd.run(%w[remote add localhost 2])
66
67
  assert_equal(2, remotes.all.count)
@@ -33,7 +33,8 @@ class FrontTest < Minitest::Test
33
33
  '/robots.txt',
34
34
  '/',
35
35
  '/remotes',
36
- '/v'
36
+ '/version',
37
+ '/score'
37
38
  ],
38
39
  '404' => [
39
40
  '/this-is-absent',
@@ -0,0 +1,39 @@
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 all
11
+ # 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
+ require 'minitest/autorun'
22
+ require 'tmpdir'
23
+ require_relative '../lib/zold/atomic_file'
24
+
25
+ # AtomicFile test.
26
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
27
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
28
+ # License:: MIT
29
+ class TestAtomicFile < Minitest::Test
30
+ def test_writes_and_reads
31
+ Dir.mktmpdir 'test' do |dir|
32
+ file = Zold::AtomicFile.new(File.join(dir, 'test.txt'))
33
+ ['', 'hello, dude!'].each do |t|
34
+ file.write(t)
35
+ assert_equal(t, file.read)
36
+ end
37
+ end
38
+ end
39
+ end
data/test/test_copies.rb CHANGED
@@ -37,7 +37,7 @@ class TestCopies < Minitest::Test
37
37
  copies.add('hello 1', '192.168.0.4', 80, 10)
38
38
  copies.add('hello-to-delete', '192.168.0.5', 80, 10)
39
39
  copies.remove('192.168.0.5', 80)
40
- assert(copies.all.count == 2, "#{copies.all.count} is not equal to 2")
40
+ assert(2, copies.all.count)
41
41
  assert(copies.all.find { |c| c[:name] == '1' }[:score] == 11)
42
42
  end
43
43
  end
data/test/test_remotes.rb CHANGED
@@ -20,6 +20,8 @@
20
20
 
21
21
  require 'minitest/autorun'
22
22
  require 'tmpdir'
23
+ require_relative 'test__helper'
24
+ require_relative '../lib/zold/log'
23
25
  require_relative '../lib/zold/remotes'
24
26
 
25
27
  # Remotes test.
@@ -37,6 +39,18 @@ class TestRemotes < Minitest::Test
37
39
  end
38
40
  end
39
41
 
42
+ def test_iterates_and_fails
43
+ Dir.mktmpdir 'test' do |dir|
44
+ file = File.join(dir, 'remotes')
45
+ FileUtils.touch(file)
46
+ remotes = Zold::Remotes.new(file)
47
+ ips = (0..50)
48
+ ips.each { |i| remotes.add("0.0.0.#{i}", 9999) }
49
+ remotes.iterate(Zold::Log::Quiet.new) { raise 'Intended' }
50
+ ips.each { |i| assert(1, remotes.all[i][:errors]) }
51
+ end
52
+ end
53
+
40
54
  def test_removes_remotes
41
55
  Dir.mktmpdir 'test' do |dir|
42
56
  file = File.join(dir, 'remotes')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.6
4
+ version: 0.11.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -320,6 +320,7 @@ files:
320
320
  - fixtures/scripts/push-and-pull.sh
321
321
  - lib/zold.rb
322
322
  - lib/zold/amount.rb
323
+ - lib/zold/atomic_file.rb
323
324
  - lib/zold/commands/args.rb
324
325
  - lib/zold/commands/calculate.rb
325
326
  - lib/zold/commands/clean.rb
@@ -381,6 +382,7 @@ files:
381
382
  - test/node/test_front.rb
382
383
  - test/test__helper.rb
383
384
  - test/test_amount.rb
385
+ - test/test_atomic_file.rb
384
386
  - test/test_copies.rb
385
387
  - test/test_hexnum.rb
386
388
  - test/test_http.rb
@@ -450,6 +452,7 @@ test_files:
450
452
  - test/node/test_front.rb
451
453
  - test/test__helper.rb
452
454
  - test/test_amount.rb
455
+ - test/test_atomic_file.rb
453
456
  - test/test_copies.rb
454
457
  - test/test_hexnum.rb
455
458
  - test/test_http.rb