zold 0.14.50 → 0.14.51

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: c93f5cab672e8cda8e44e15b589f97d593486c089f4428487370835207087b00
4
- data.tar.gz: 5a14a57fcf20345a21acb27086042dac6cb7df2abfb5b226bae85d6014cc9899
3
+ metadata.gz: 53fed8a92732a47d8473dd93dcfccf0cc5aac6cd2dad450068779d3faabc8849
4
+ data.tar.gz: 6eb21513a8b304eb81ad3ef3d46f8c8d802aa0be3b409e06bdadaf5c6021ee88
5
5
  SHA512:
6
- metadata.gz: 8f4ac2585a5eaf88260243cdad9326eaee19a8a5c7db6689a3bd0a1fd9ba69d466aa95a390f4937ec9ae3fa916c5cb72214a9d4d20ae563e9e4c2d65ff41e018
7
- data.tar.gz: 992a00ef00711065ee79e191b3a1ac40d96c70e3074331298484a5a90f5f27170925c5420cac43b617aaabdc1a3712c5729a4d2ff452923faeec3e797c2a89bb
6
+ metadata.gz: 86cee566355856078b67b2f90f150458158f24a3e5d1681c580c86227dbdffac875c01377e3b69987dca088ed026351a623d02a1f51e653fbfb1ee1b8bd7372f
7
+ data.tar.gz: 55d61bc63a682151a23a35098449c5798f4ba783101ec8d609a0511cb0462c4f1344e9c191e4b53ae2093c1c576463438c247afea8ef84c72afd6785556f263d
@@ -63,9 +63,14 @@ Available options:"
63
63
  def propagate(id, _)
64
64
  start = Time.now
65
65
  modified = []
66
+ total = 0
66
67
  @wallets.find(id) do |wallet|
67
68
  wallet.txns.select { |t| t.amount.negative? }.each do |t|
68
- next if t.bnf == id
69
+ total += 1
70
+ if t.bnf == id
71
+ @log.error("Paying itself in #{id}? #{t}")
72
+ next
73
+ end
69
74
  @wallets.find(t.bnf) do |target|
70
75
  unless target.exists?
71
76
  @log.debug("#{t.amount * -1} to #{t.bnf}: wallet is absent")
@@ -75,7 +80,7 @@ Available options:"
75
80
  @log.error("#{t.amount * -1} to #{t.bnf}: network mismatch, '#{target.network}'!='#{wallet.network}'")
76
81
  next
77
82
  end
78
- next if target.has?(t.id, id)
83
+ next if target.includes_positive?(t.id, id)
79
84
  unless target.prefix?(t.prefix)
80
85
  @log.error("#{t.amount * -1} to #{t.bnf}: wrong prefix")
81
86
  next
@@ -87,7 +92,8 @@ Available options:"
87
92
  end
88
93
  end
89
94
  modified.uniq!
90
- @log.debug("Wallet #{id} propagated successfully in #{Age.new(start)}, #{modified.count} wallets affected")
95
+ @log.debug("Wallet #{id} propagated successfully, #{total} txns in #{Age.new(start)}, \
96
+ #{modified.count} wallets affected")
91
97
  modified
92
98
  end
93
99
  end
data/lib/zold/patch.rb CHANGED
@@ -105,7 +105,7 @@ module Zold
105
105
  @log.error("Paying wallet file is absent: #{txn.to_text}")
106
106
  next
107
107
  end
108
- unless @wallets.find(txn.bnf) { |p| p.has?(txn.id, wallet.id) }
108
+ unless @wallets.find(txn.bnf) { |p| p.includes_negative?(txn.id, wallet.id) }
109
109
  @log.error("Paying wallet #{txn.bnf} doesn't have transaction ##{txn.id} \
110
110
  among #{payer.txns.count} transactions: #{txn.to_text}")
111
111
  next
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.50'
28
+ VERSION = '0.14.51'
29
29
  PROTOCOL = 2
30
30
  end
data/lib/zold/wallet.rb CHANGED
@@ -130,18 +130,28 @@ module Zold
130
130
 
131
131
  def add(txn)
132
132
  raise 'The txn has to be of type Txn' unless txn.is_a?(Txn)
133
- dup = txns.find { |t| t.bnf == txn.bnf && t.id == txn.id && t.amount.negative? && txn.amount.negative? }
134
133
  raise "Wallet #{id} can't pay itself: #{txn}" if txn.bnf == id
135
- raise "The transaction with the same ID and BNF already exists in #{id}: #{dup}" unless dup.nil?
134
+ raise "The amount can't be zero in #{id}: #{txn}" if txn.amount.zero?
135
+ if txn.amount.negative? && includes_negative?(txn.id)
136
+ raise "Negative transaction with the same ID #{txn.id} already exists in #{id}"
137
+ end
138
+ if txn.amount.positive? && includes_positive?(txn.id, txn.bnf)
139
+ raise "Positive transaction with the same ID #{txn.id} and BNF #{txn.bnf} already exists in #{id}"
140
+ end
136
141
  raise "The tax payment already exists in #{id}: #{txn}" if Tax.new(self).exists?(txn.details)
137
142
  File.open(@file, 'a') { |f| f.print "#{txn}\n" }
138
143
  @txns.flush
139
144
  end
140
145
 
141
- def has?(id, bnf)
146
+ def includes_negative?(id, bnf = nil)
147
+ raise 'The txn ID has to be of type Integer' unless id.is_a?(Integer)
148
+ !txns.find { |t| t.id == id && (bnf.nil? || t.bnf == bnf) && t.amount.negative? }.nil?
149
+ end
150
+
151
+ def includes_positive?(id, bnf)
142
152
  raise 'The txn ID has to be of type Integer' unless id.is_a?(Integer)
143
153
  raise 'The bnf has to be of type Id' unless bnf.is_a?(Id)
144
- !txns.find { |t| t.id == id && t.bnf == bnf }.nil?
154
+ !txns.find { |t| t.id == id && t.bnf == bnf && !t.amount.negative? }.nil?
145
155
  end
146
156
 
147
157
  def prefix?(prefix)
data/test/test_wallet.rb CHANGED
@@ -79,10 +79,29 @@ class TestWallet < Minitest::Test
79
79
  id = Zold::Id.new
80
80
  wallet.sub(amount, "NOPREFIX@#{id}", key)
81
81
  wallet.add(Zold::Txn.new(1, Time.now, amount, 'NOPREFIX', id, '-'))
82
+ assert_raises do
83
+ wallet.add(Zold::Txn.new(1, Time.now, amount, 'NOPREFIX', id, '-'))
84
+ end
85
+ assert_raises do
86
+ wallet.add(Zold::Txn.new(1, Time.now, amount * -1, 'NOPREFIX', id, '-'))
87
+ end
82
88
  assert(wallet.balance.zero?)
83
89
  end
84
90
  end
85
91
 
92
+ def test_checks_similar_transaction
93
+ FakeHome.new.run do |home|
94
+ wallet = home.create_wallet
95
+ amount = Zold::Amount.new(zld: 39.99)
96
+ key = Zold::Key.new(file: 'fixtures/id_rsa')
97
+ id = Zold::Id.new
98
+ wallet.sub(amount, "NOPREFIX@#{id}", key)
99
+ wallet.add(Zold::Txn.new(1, Time.now, amount, 'NOPREFIX', id, '-'))
100
+ assert(wallet.includes_negative?(1))
101
+ assert(wallet.includes_positive?(1, id))
102
+ end
103
+ end
104
+
86
105
  def test_refurbishes_wallet
87
106
  FakeHome.new.run do |home|
88
107
  wallet = home.create_wallet
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.14.50
4
+ version: 0.14.51
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko