zold 0.6.3 → 0.6.4

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.
data/lib/zold/remotes.rb CHANGED
@@ -44,9 +44,9 @@ module Zold
44
44
 
45
45
  def all
46
46
  list = load
47
- max_score = list.map { |r| r[:score] }.max
47
+ max_score = list.map { |r| r[:score] }.max || 0
48
48
  max_score = 1 if max_score.zero?
49
- max_errors = list.map { |r| r[:errors] }.max
49
+ max_errors = list.map { |r| r[:errors] }.max || 0
50
50
  max_errors = 1 if max_errors.zero?
51
51
  list.sort_by do |r|
52
52
  (1 - r[:errors] / max_errors) * 5 + (r[:score] / max_score)
data/lib/zold/tax.rb CHANGED
@@ -34,6 +34,9 @@ module Zold
34
34
  # The minimum score a wallet can buy in order to pay taxes.
35
35
  MIN_SCORE = 16
36
36
 
37
+ # The maximum allowed amount in one transaction.
38
+ MAX_PAYMENT = Amount.new(zld: 1.0)
39
+
37
40
  # This is how much we charge per one transaction per hour
38
41
  # of storage. A wallet of 4096 transactions will pay
39
42
  # approximately 16ZLD per year.
@@ -57,20 +60,15 @@ module Zold
57
60
 
58
61
  def debt
59
62
  txns = @wallet.txns
60
- return Amount::ZERO if txns.empty?
61
- paid = txns.map do |t|
63
+ scores = txns.map do |t|
62
64
  pfx, body = t.details.split(' ')
63
- if pfx != 'TAXES' || body.nil?
64
- Amount::ZERO
65
- else
66
- score = Score.parse_text(body)
67
- if score.valid? && score.value >= MIN_SCORE
68
- t.amount
69
- else
70
- Amount::ZERO
71
- end
72
- end
73
- end.inject(&:+) * -1
65
+ next if pfx != 'TAXES' || body.nil?
66
+ score = Score.parse_text(body)
67
+ next if !score.valid? || score.value < MIN_SCORE
68
+ next if t.amount > Tax::MAX_PAYMENT
69
+ score
70
+ end.reject(&:nil?).uniq(&:hash)
71
+ paid = scores.empty? ? Amount::ZERO : scores.map(&:amount).inject(&:+) * -1
74
72
  age_hours = (Time.now - txns.sort_by(&:date)[0].date) / 60
75
73
  owned = Tax::FEE_TXN_HOUR * txns.count * age_hours
76
74
  owned - paid
data/lib/zold/txn.rb CHANGED
@@ -20,6 +20,7 @@
20
20
 
21
21
  require 'time'
22
22
  require_relative 'id'
23
+ require_relative 'hexnum'
23
24
  require_relative 'amount'
24
25
  require_relative 'signature'
25
26
 
@@ -65,9 +66,9 @@ module Zold
65
66
 
66
67
  def to_s
67
68
  [
68
- @id,
69
+ Hexnum.new(@id, 4).to_s,
69
70
  @date.utc.iso8601,
70
- @amount.to_i,
71
+ Hexnum.new(@amount.to_i, 16),
71
72
  @prefix,
72
73
  @bnf,
73
74
  @details,
@@ -95,11 +96,11 @@ module Zold
95
96
  def self.parse(line, idx = 0)
96
97
  regex = Regexp.new(
97
98
  '^' + [
98
- '([0-9]+)',
99
+ '([0-9a-f]{4})',
99
100
  '([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)',
100
- '(-?[0-9]+)',
101
+ '([0-9a-f]{16})',
101
102
  '([A-Za-z0-9]{8,32})',
102
- '([a-f0-9]{16})',
103
+ '([0-9a-f]{16})',
103
104
  '([a-zA-Z0-9 -\.,]{1,128})',
104
105
  '([A-Za-z0-9+/]+={0,3})?'
105
106
  ].join(';') + '$'
@@ -108,9 +109,9 @@ module Zold
108
109
  raise "Invalid line ##{idx}: #{line.inspect}" unless regex.match(clean)
109
110
  parts = clean.split(';')
110
111
  txn = Txn.new(
111
- parts[0].to_i,
112
+ Hexnum.parse(parts[0]).to_i,
112
113
  Time.parse(parts[1]),
113
- Amount.new(coins: parts[2].to_i),
114
+ Amount.new(coins: Hexnum.parse(parts[2]).to_i),
114
115
  parts[3],
115
116
  Id.new(parts[4]),
116
117
  parts[5]
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.6.3'.freeze
26
+ VERSION = '0.6.4'.freeze
27
27
  end
data/lib/zold/wallets.rb CHANGED
@@ -37,6 +37,7 @@ module Zold
37
37
  File.expand_path(@dir)
38
38
  end
39
39
 
40
+ # Returns the list of their IDs (as plain text)
40
41
  def all
41
42
  Dir.new(path).select do |f|
42
43
  file = File.join(@dir, f)
@@ -55,7 +55,7 @@ class TestDiff < Minitest::Test
55
55
  wallets: Zold::Wallets.new(dir),
56
56
  copies: copies.root
57
57
  ).run(['diff', id.to_s])
58
- assert(diff.include?('-1;'))
58
+ assert(diff.include?('-0001;'))
59
59
  end
60
60
  end
61
61
  end
@@ -33,16 +33,18 @@ require_relative '../../lib/zold/commands/pay'
33
33
  class TestPay < Minitest::Test
34
34
  def test_sends_from_wallet_to_wallet
35
35
  Dir.mktmpdir 'test' do |dir|
36
- id = Zold::Id.new
37
36
  wallets = Zold::Wallets.new(dir)
38
- source = wallets.find(id)
39
- source.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
40
- target = Zold::Id.new
37
+ sid = Zold::Id.new
38
+ source = wallets.find(sid)
39
+ source.init(sid, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
40
+ tid = Zold::Id.new
41
+ target = wallets.find(tid)
42
+ target.init(tid, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
41
43
  amount = Zold::Amount.new(zld: 14.95)
42
44
  Zold::Pay.new(wallets: wallets).run(
43
45
  [
44
46
  'pay', '--force', '--private-key=fixtures/id_rsa',
45
- id.to_s, target.to_s, amount.to_zld, 'For the car'
47
+ sid.to_s, tid.to_s, amount.to_zld, 'For the car'
46
48
  ]
47
49
  )
48
50
  assert_equal(amount * -1, source.balance)
@@ -0,0 +1,52 @@
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 'json'
24
+ require 'time'
25
+ require 'webmock/minitest'
26
+ require_relative '../../lib/zold/wallet'
27
+ require_relative '../../lib/zold/wallets'
28
+ require_relative '../../lib/zold/remotes'
29
+ require_relative '../../lib/zold/id'
30
+ require_relative '../../lib/zold/key'
31
+ require_relative '../../lib/zold/commands/push'
32
+
33
+ # PUSH test.
34
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
35
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
36
+ # License:: MIT
37
+ class TestPush < Minitest::Test
38
+ def test_pushes_wallet
39
+ Dir.mktmpdir 'test' do |dir|
40
+ id = Zold::Id.new
41
+ wallets = Zold::Wallets.new(dir)
42
+ wallet = wallets.find(id)
43
+ wallet.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
44
+ remotes = Zold::Remotes.new(File.join(dir, 'remotes.csv'))
45
+ remotes.clean
46
+ stub_request(:put, "http://fake-1/wallet/#{id}").to_return(status: 304)
47
+ Zold::Push.new(wallets: wallets, remotes: remotes).run(
48
+ ['--ignore-this-stupid-option', 'push', id.to_s]
49
+ )
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,34 @@
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_relative '../lib/zold/hexnum'
23
+
24
+ # Hexnum test.
25
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
26
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
27
+ # License:: MIT
28
+ class TestHexnum < Minitest::Test
29
+ def test_prints_and_parses
30
+ [0, 1, 3, 5, 7, 13, 6447, 897_464, -1, -3, -7584, -900_098].each do |n|
31
+ assert_equal(n, Zold::Hexnum.parse(Zold::Hexnum.new(n, 6).to_s).to_i)
32
+ end
33
+ end
34
+ end
data/test/test_patch.rb CHANGED
@@ -40,12 +40,12 @@ class TestPatch < Minitest::Test
40
40
  third = Zold::Wallet.new(File.join(dir, 'third'))
41
41
  first.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
42
42
  File.write(second.path, File.read(first.path))
43
- first.sub(Zold::Amount.new(zld: 39), "NOPREFIX@#{Zold::Id.new}", key)
44
- first.sub(Zold::Amount.new(zld: 11), "NOPREFIX@#{Zold::Id.new}", key)
45
- first.sub(Zold::Amount.new(zld: 3), "NOPREFIX@#{Zold::Id.new}", key)
46
- second.sub(Zold::Amount.new(zld: 44), "NOPREFIX@#{Zold::Id.new}", key)
43
+ first.sub(Zold::Amount.new(zld: 39.0), "NOPREFIX@#{Zold::Id.new}", key)
44
+ first.sub(Zold::Amount.new(zld: 11.0), "NOPREFIX@#{Zold::Id.new}", key)
45
+ first.sub(Zold::Amount.new(zld: 3.0), "NOPREFIX@#{Zold::Id.new}", key)
46
+ second.sub(Zold::Amount.new(zld: 44.0), "NOPREFIX@#{Zold::Id.new}", key)
47
47
  File.write(third.path, File.read(first.path))
48
- t = third.sub(Zold::Amount.new(zld: 10), "NOPREFIX@#{Zold::Id.new}", key)
48
+ t = third.sub(Zold::Amount.new(zld: 10.0), "NOPREFIX@#{Zold::Id.new}", key)
49
49
  third.add(t.inverse(id))
50
50
  patch = Zold::Patch.new
51
51
  patch.start(first)
@@ -53,7 +53,7 @@ class TestPatch < Minitest::Test
53
53
  patch.join(third)
54
54
  FileUtils.rm(first.path)
55
55
  assert_equal(true, patch.save(first.path))
56
- assert_equal(Zold::Amount.new(zld: -53), first.balance)
56
+ assert_equal(Zold::Amount.new(zld: -53.0), first.balance)
57
57
  end
58
58
  end
59
59
  end
data/test/test_txn.rb CHANGED
@@ -34,13 +34,13 @@ class TestTxn < Minitest::Test
34
34
  time = Time.now
35
35
  txn = Zold::Txn.parse(
36
36
  Zold::Txn.new(
37
- 123, time, Zold::Amount.new(zld: 99.95),
37
+ 123, time, Zold::Amount.new(zld: -99.95),
38
38
  'NOPREFIX', Zold::Id.new,
39
39
  'Some details to see 123. Works, or not.'
40
40
  ).to_s
41
41
  )
42
42
  assert_equal(123, txn.id)
43
- assert_equal('99.95', txn.amount.to_zld)
43
+ assert_equal('-99.95', txn.amount.to_zld)
44
44
  assert_equal('NOPREFIX', txn.prefix)
45
45
  end
46
46
  end