zold 0.11.21 → 0.11.22

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: c624d076515e55c78c91bbe5724d85ec5a7451ae
4
- data.tar.gz: 2076bb04386f2502fc74984ad6f04ff703a75994
3
+ metadata.gz: bc7e111fc1e96f2796c64c793edd9714972680d8
4
+ data.tar.gz: 5149c712d5e30dd6e92f7e3ead90d0329d904399
5
5
  SHA512:
6
- metadata.gz: 6d6311dfacdf2772f7050ce8cc6e69c7f6c67164249ef6a5f5e6ea51dc37b0c019d9558da00a7855c3ae70a3048546f4dc2ca60d53dd6ac76a807f54e3ed0fa0
7
- data.tar.gz: 4d7d7c7e6f5565c6ddfceafb325040a66e567439c16e580f81d96506f7bf6b4f5e7e2dd4e17aa66ff3998d929aad944feaf8fad066af8fb543de18a4c0636a2b
6
+ metadata.gz: 29bf131b899ac382814b6ffcefa8b472167387b081269b9224b972ec4d3229bf40aad759c4cd39298e11af193a0d27f49b448679138c434d0c46d0d91f29b700
7
+ data.tar.gz: f676f48ea7719ccf1552220381e0df2e0a732791686d0b8128a5c57c2d3ca2711318910c70a24f2141d31dad3d835ebc78ca766e817ef963917b89a6aa023c88
@@ -57,12 +57,15 @@ def exec(cmd)
57
57
  Open3.popen2e(cmd) do |stdin, stdout, thr|
58
58
  log("Started: #{cmd}\n")
59
59
  stdin.close
60
- loop do
61
- line = stdout.gets
62
- break if line.nil?
63
- log(line)
60
+ until stdout.eof?
61
+ begin
62
+ data = stdout.read_nonblock(1024) unless stdout.eof?
63
+ rescue EOFError => _
64
+ break
65
+ end
66
+ log(data)
64
67
  end
65
- code = thr.value.exitstatus
68
+ code = thr.value.to_i
66
69
  log("Exit code is #{code}: #{cmd}\n")
67
70
  raise "Exit code #{code} (non zero)" unless code.zero?
68
71
  end
@@ -72,15 +75,20 @@ File.delete($opts['log-file']) if File.exist?($opts['log-file']) && !$opts['skip
72
75
 
73
76
  $pid = fork do
74
77
  Signal.trap('HUP') do
75
- log('Received HUP, ignoring...')
78
+ log("Received HUP, ignoring...\n")
76
79
  end
77
80
  Signal.trap('TERM') do
78
- log('Received TERM, terminating...')
81
+ log("Received TERM, terminating...\n")
79
82
  exit(-1)
80
83
  end
81
84
  loop do
82
- exec("zold #{$opts.arguments.join(' ')}")
83
- exec(($opts['sudo-install'] ? 'sudo ' : '') + 'gem install zold') unless $opts['skip-install']
85
+ begin
86
+ exec("zold #{$opts.arguments.join(' ')}")
87
+ exec(($opts['sudo-install'] ? 'sudo ' : '') + 'gem install zold') unless $opts['skip-install']
88
+ rescue StandardError => e
89
+ log("#{e.class.name}: #{e.message}\n#{e.backtrace.join("\n\t")}\n")
90
+ raise e
91
+ end
84
92
  end
85
93
  end
86
94
  Process.detach($pid)
@@ -65,14 +65,9 @@ Available options:"
65
65
  def diff(wallet, cps, _)
66
66
  raise "There are no remote copies, try 'zold fetch' first" if cps.all.empty?
67
67
  cps = cps.all.sort_by { |c| c[:score] }.reverse
68
- patch = Patch.new
68
+ patch = Patch.new(log: @log)
69
69
  cps.each do |c|
70
- begin
71
- patch.join(Wallet.new(c[:path]))
72
- rescue StandardError => e
73
- @log.error("Can't use a copy of #{wallet.id} from #{c[:host]}:#{c[:port]}; #{e.class.name}: #{e.message}")
74
- @log.debug(e.backtrace.join("\n\t"))
75
- end
70
+ patch.join(Wallet.new(c[:path]))
76
71
  end
77
72
  before = AtomicFile.new(wallet.path).read
78
73
  after = ''
@@ -66,7 +66,7 @@ Available options:"
66
66
  return
67
67
  end
68
68
  cps = cps.all.sort_by { |c| c[:score] }.reverse
69
- patch = Patch.new
69
+ patch = Patch.new(log: @log)
70
70
  cps.each do |c|
71
71
  merge_one(patch, Wallet.new(c[:path]), "#{c[:host]}:#{c[:port]}")
72
72
  @log.debug("#{c[:host]}:#{c[:port]} merged: #{patch}")
@@ -18,6 +18,7 @@
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
19
  # SOFTWARE.
20
20
 
21
+ require_relative 'log'
21
22
  require_relative 'wallet'
22
23
  require_relative 'signature'
23
24
  require_relative 'atomic_file'
@@ -30,6 +31,10 @@ require_relative 'atomic_file'
30
31
  module Zold
31
32
  # A patch
32
33
  class Patch
34
+ def initialize(log: Log::Quiet.new)
35
+ @log = log
36
+ end
37
+
33
38
  def to_s
34
39
  return 'empty' if @id.nil?
35
40
  "#{@txns.count} txns"
@@ -40,6 +45,7 @@ module Zold
40
45
  @id = wallet.id
41
46
  @key = wallet.key
42
47
  @txns = wallet.txns
48
+ @log.debug("The baseline: #{@txns.count} transactions, the balance is #{wallet.balance}")
43
49
  @network = wallet.network
44
50
  end
45
51
  if wallet.network != @network
@@ -51,17 +57,28 @@ module Zold
51
57
  max = negative.empty? ? 0 : negative.max_by(&:id).id
52
58
  wallet.txns.each do |txn|
53
59
  next if @txns.find { |t| t == txn }
54
- next if
55
- txn.amount.negative? && !@txns.empty? &&
56
- (txn.id <= max ||
57
- @txns.find { |t| t.id == txn.id } ||
58
- @txns.map(&:amount).inject(&:+) < txn.amount)
59
- if !txn.amount.negative? && !txn.sign.empty?
60
- raise "RSA signature is redundant at ##{txn.id} of #{wallet.id}: #{txn.to_text}"
61
- end
62
- if txn.amount.negative? && !Signature.new.valid?(@key, wallet.id, txn)
63
- raise "Invalid RSA signature at transaction ##{txn.id} of #{wallet.id}: #{txn.to_text}"
60
+ if txn.amount.negative?
61
+ if txn.id <= max
62
+ @log.debug("Transaction ID is less than max #{max}: #{txn.to_text}")
63
+ next
64
+ end
65
+ if @txns.find { |t| t.id == txn.id }
66
+ @log.debug("Transaction ##{txn.id} already exists: #{txn.to_text}")
67
+ next
68
+ end
69
+ if !@txns.empty? && @txns.map(&:amount).inject(&:+) < txn.amount
70
+ @log.debug("Transaction ##{txn.id} attempts to make the balance negative: #{txn.to_text}")
71
+ next
72
+ end
73
+ unless Signature.new.valid?(@key, wallet.id, txn)
74
+ @log.debug("Invalid RSA signature at transaction ##{txn.id} of #{wallet.id}: #{txn.to_text}")
75
+ next
76
+ end
77
+ elsif !txn.sign.nil? && !txn.sign.empty?
78
+ @log.debug("RSA signature is redundant at ##{txn.id} of #{wallet.id}: #{txn.to_text}")
79
+ next
64
80
  end
81
+ @log.debug("Merged on top: #{txn.to_text}")
65
82
  @txns << txn
66
83
  end
67
84
  end
@@ -23,5 +23,5 @@
23
23
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
24
24
  # License:: MIT
25
25
  module Zold
26
- VERSION = '0.11.21'.freeze
26
+ VERSION = '0.11.22'.freeze
27
27
  end
@@ -53,7 +53,7 @@ class TestDiff < Minitest::Test
53
53
  diff = Zold::Diff.new(wallets: home.wallets, copies: copies.root, log: test_log).run(
54
54
  ['diff', wallet.id.to_s]
55
55
  )
56
- assert(diff.include?('-0001;'))
56
+ assert(diff.include?('-0001;'), diff)
57
57
  end
58
58
  end
59
59
  end
@@ -20,6 +20,7 @@
20
20
 
21
21
  require 'minitest/autorun'
22
22
  require_relative 'fake_home'
23
+ require_relative 'test__helper'
23
24
  require_relative '../lib/zold/key'
24
25
  require_relative '../lib/zold/id'
25
26
  require_relative '../lib/zold/wallet'
@@ -45,17 +46,13 @@ class TestPatch < Minitest::Test
45
46
  File.write(third.path, File.read(first.path))
46
47
  t = third.sub(Zold::Amount.new(zld: 10.0), "NOPREFIX@#{Zold::Id.new}", key)
47
48
  third.add(t.inverse(first.id))
48
- patch = Zold::Patch.new
49
- begin
50
- patch.join(first)
51
- patch.join(second)
52
- patch.join(third)
53
- rescue StandardError => e
54
- test_log.info("Ignore it: #{e.message}")
55
- end
49
+ patch = Zold::Patch.new(log: test_log)
50
+ patch.join(first)
51
+ patch.join(second)
52
+ patch.join(third)
56
53
  FileUtils.rm(first.path)
57
54
  assert_equal(true, patch.save(first.path))
58
- assert_equal(Zold::Amount.new(zld: -53.0), first.balance)
55
+ assert_equal(Zold::Amount.new(zld: -43.0), first.balance)
59
56
  end
60
57
  end
61
58
  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.11.21
4
+ version: 0.11.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko