zold 0.18.1 → 0.18.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 219994932a033cc330b4634c4a4d2345cc94ed3b1c5b0c9cbd30ec84ec09476c
4
- data.tar.gz: b76b4323a3f3ba7f3c086c5efccca46d319fecaf82a16b5e66cbcb91f945ce40
3
+ metadata.gz: 2544c7f82192df9838d17ce34db7e71f43bf354f74a4021a6567e015feb88c78
4
+ data.tar.gz: b7d465bedb331452972844bcd7808182633ff9eabf3c7e1da795dc6d64c1483b
5
5
  SHA512:
6
- metadata.gz: 0140be60b68cdcd39da884f1eac46712a402168a6a6a0bb5738a6484d070130d7cab6843f6e1b5bf9f631a80a94a46dfcb838d387a7026de854fdd01633596ab
7
- data.tar.gz: 2228eb60ffc8cbcbf9561e44c9d06b8a6aa102dd8441e9e03ed6f73a4a415f94c8651d6e58e2d8edbadf59659a93af0973e1143c220d6c80d2fdb60bd17d79c3
6
+ metadata.gz: 8ae9482cf55549e0c5e44063369b5dede47e9550c995fc2d5cdb83fd89b315cbcfb0e41a2381ded0fcb60d230c91c6ec0cf4e87ce0d3d397983e670de183c06d
7
+ data.tar.gz: 054a46f86e5538edd0aaf7bf0f0b9fc116b83fb86d1810d8dfb11b7e4d49bd7e042acc07a3a0d5be34b44e262a4967dd18ba7129a65b23a0890def81196761de
data/.rubocop.yml CHANGED
@@ -16,7 +16,7 @@ Layout/MultilineMethodCallIndentation:
16
16
  Metrics/AbcSize:
17
17
  Enabled: false
18
18
  Metrics/BlockLength:
19
- Max: 100
19
+ Max: 120
20
20
  Metrics/ClassLength:
21
21
  Max: 400
22
22
  Layout/EndOfLine:
@@ -74,16 +74,10 @@ Available options:"
74
74
  private
75
75
 
76
76
  def merge(id, cps, opts)
77
+ start = Time.now
77
78
  cps = cps.all.sort_by { |c| c[:score] }.reverse
78
79
  patch = Patch.new(@wallets, log: @log)
79
80
  score = 0
80
- cps.each_with_index do |c, idx|
81
- wallet = Wallet.new(c[:path])
82
- name = "#{c[:name]}/#{idx}/#{c[:score]}"
83
- merge_one(opts, patch, wallet, name)
84
- score += c[:score]
85
- end
86
- start = Time.now
87
81
  @wallets.acq(id) do |w|
88
82
  if w.exists?
89
83
  merge_one(opts, patch, w, 'localhost')
@@ -92,6 +86,12 @@ Available options:"
92
86
  @log.debug("Local copy of #{id} is absent, nothing to merge")
93
87
  end
94
88
  end
89
+ cps.each_with_index do |c, idx|
90
+ wallet = Wallet.new(c[:path])
91
+ name = "#{c[:name]}/#{idx}/#{c[:score]}"
92
+ merge_one(opts, patch, wallet, name)
93
+ score += c[:score]
94
+ end
95
95
  modified = @wallets.acq(id, exclusive: true) { |w| patch.save(w.path, overwrite: true) }
96
96
  if modified
97
97
  @log.info("#{cps.count} copies with the total score of #{score} successfully merged \
@@ -136,6 +136,9 @@ module Zold
136
136
  o.integer '--queue-limit',
137
137
  'The maximum number of wallets to be accepted via PUSH and stored in the queue (default: 4096)',
138
138
  default: 4096
139
+ o.integer '--gc-age',
140
+ 'Maximum time in seconds to keep an empty and unused wallet on the disk',
141
+ default: 60 * 60 * 24 * 10
139
142
  o.string '--expose-version',
140
143
  "The version of the software to expose in JSON (default: #{VERSION})",
141
144
  default: VERSION
@@ -360,12 +363,10 @@ module Zold
360
363
  @log.info('Metronome hasn\'t been started because of --no-metronome')
361
364
  return metronome
362
365
  end
363
- # This was a good idea to spread wallets among other nodes,
364
- # but with the growing number of wallets it's obvious that this
365
- # doesn't make a lot of sense. We better focus of HungryWallets
366
- # implementation, where wallets are being pulled if they are missed.
367
- # require_relative 'routines/spread'
368
- # metronome.add(Routines::Spread.new(opts, @wallets, @remotes, log: @log))
366
+ require_relative 'routines/spread'
367
+ metronome.add(Routines::Spread.new(opts, @wallets, @remotes, log: @log))
368
+ require_relative 'routines/gc'
369
+ metronome.add(Routines::Gc.new(opts, @wallets, log: @log))
369
370
  unless opts['standalone']
370
371
  require_relative 'routines/reconnect'
371
372
  metronome.add(Routines::Reconnect.new(opts, @remotes, farm, network: opts['network'], log: @log))
@@ -0,0 +1,58 @@
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_relative '../../log'
24
+ require_relative '../remove'
25
+
26
+ # Gargage collecting. It goes through the list of all wallets and removes
27
+ # those that are older than 10 days and don't have any transactions inside.
28
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
29
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
30
+ # License:: MIT
31
+ module Zold
32
+ # Routines module
33
+ module Routines
34
+ # Garbage collector
35
+ class Gc
36
+ def initialize(opts, wallets, log: Log::NULL)
37
+ @opts = opts
38
+ @wallets = wallets
39
+ @log = log
40
+ end
41
+
42
+ def exec(_ = 0)
43
+ sleep(60) unless @opts['routine-immediately']
44
+ cmd = Remove.new(wallets: @wallets, log: @log)
45
+ args = ['remove']
46
+ seen = 0
47
+ removed = 0
48
+ @wallets.all.each do |id|
49
+ seen += 1
50
+ next unless @wallets.acq(id) { |w| w.txns.empty? && w.mtime < Time.now - @opts['gc-age'] }
51
+ cmd.run(args + [id.to_s])
52
+ removed += 1
53
+ end
54
+ @log.info("Removed #{removed} empty+old wallets out of #{seen} total")
55
+ end
56
+ end
57
+ end
58
+ end
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.18.1'
28
+ VERSION = '0.18.2'
29
29
  PROTOCOL = 2
30
30
  end
data/lib/zold/wallets.rb CHANGED
@@ -72,7 +72,10 @@ module Zold
72
72
  end
73
73
 
74
74
  def count
75
- Zold::DirItems.new(@dir).fetch(recursive: false).count
75
+ Zold::DirItems.new(@dir)
76
+ .fetch(recursive: false)
77
+ .select { |f| f.end_with?(Wallet::EXT) }
78
+ .count
76
79
  end
77
80
  end
78
81
  end
@@ -0,0 +1,70 @@
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 'minitest/autorun'
24
+ require_relative '../../test__helper'
25
+ require_relative '../../fake_home'
26
+ require_relative '../../../lib/zold/wallets'
27
+ require_relative '../../../lib/zold/commands/routines/gc.rb'
28
+
29
+ # Gc test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
32
+ # License:: MIT
33
+ class TestGc < Zold::Test
34
+ def test_collects_garbage
35
+ FakeHome.new(log: test_log).run do |home|
36
+ wallets = home.wallets
37
+ home.create_wallet
38
+ opts = { 'routine-immediately' => true, 'gc-age' => 0 }
39
+ assert_equal(1, wallets.count)
40
+ routine = Zold::Routines::Gc.new(opts, wallets, log: test_log)
41
+ routine.exec
42
+ assert_equal(0, wallets.count)
43
+ end
44
+ end
45
+
46
+ def test_doesnt_touch_non_empty_wallets
47
+ FakeHome.new(log: test_log).run do |home|
48
+ wallets = home.wallets
49
+ wallet = home.create_wallet
50
+ amount = Zold::Amount.new(zld: 39.99)
51
+ key = Zold::Key.new(file: 'fixtures/id_rsa')
52
+ wallet.sub(amount, "NOPREFIX@#{Zold::Id.new}", key)
53
+ opts = { 'routine-immediately' => true, 'gc-age' => 0 }
54
+ routine = Zold::Routines::Gc.new(opts, wallets, log: test_log)
55
+ routine.exec
56
+ assert_equal(1, wallets.count)
57
+ end
58
+ end
59
+
60
+ def test_doesnt_touch_fresh_wallets
61
+ FakeHome.new(log: test_log).run do |home|
62
+ wallets = home.wallets
63
+ home.create_wallet
64
+ opts = { 'routine-immediately' => true, 'gc-age' => 60 * 60 }
65
+ routine = Zold::Routines::Gc.new(opts, wallets, log: test_log)
66
+ routine.exec
67
+ assert_equal(1, wallets.count)
68
+ end
69
+ end
70
+ end
data/test/test_wallets.rb CHANGED
@@ -71,10 +71,15 @@ class TestWallets < Zold::Test
71
71
  def test_count_wallets
72
72
  Dir.mktmpdir do |dir|
73
73
  Dir.chdir(dir) do
74
- 5.times { |i| FileUtils.touch("wallet_#{i}") }
74
+ 5.times { |i| FileUtils.touch("wallet_#{i}#{Zold::Wallet::EXT}") }
75
75
  wallets = Zold::Wallets.new(Dir.pwd)
76
76
  assert_equal(5, wallets.count)
77
77
  end
78
78
  end
79
+ FakeHome.new(log: test_log).run do |home|
80
+ wallets = home.wallets
81
+ home.create_wallet
82
+ assert_equal(1, wallets.count)
83
+ end
79
84
  end
80
85
  end
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.18.1
4
+ version: 0.18.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -576,6 +576,7 @@ files:
576
576
  - lib/zold/commands/push.rb
577
577
  - lib/zold/commands/remote.rb
578
578
  - lib/zold/commands/remove.rb
579
+ - lib/zold/commands/routines/gc.rb
579
580
  - lib/zold/commands/routines/reconnect.rb
580
581
  - lib/zold/commands/routines/spread.rb
581
582
  - lib/zold/commands/show.rb
@@ -625,6 +626,7 @@ files:
625
626
  - lib/zold/wallets.rb
626
627
  - resources/remotes
627
628
  - resources/root.pub
629
+ - test/commands/routines/test_gc.rb
628
630
  - test/commands/routines/test_reconnect.rb
629
631
  - test/commands/routines/test_spread.rb
630
632
  - test/commands/test_alias.rb
@@ -701,7 +703,7 @@ licenses:
701
703
  - MIT
702
704
  metadata: {}
703
705
  post_install_message: |-
704
- Thanks for installing Zold 0.18.1!
706
+ Thanks for installing Zold 0.18.2!
705
707
  Study our White Paper: https://papers.zold.io/wp.pdf
706
708
  Read our blog posts: https://blog.zold.io
707
709
  Try online wallet at: https://wts.zold.io
@@ -733,6 +735,7 @@ test_files:
733
735
  - features/gem_package.feature
734
736
  - features/step_definitions/steps.rb
735
737
  - features/support/env.rb
738
+ - test/commands/routines/test_gc.rb
736
739
  - test/commands/routines/test_reconnect.rb
737
740
  - test/commands/routines/test_spread.rb
738
741
  - test/commands/test_alias.rb