zold 0.1.1 → 0.1.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
  SHA1:
3
- metadata.gz: 45f2fb7dcf69ce479ef15281f2e2ac5e60347eac
4
- data.tar.gz: f177e99ee0e5925fb55a8c151301bc6d0fdfffce
3
+ metadata.gz: 6fcd55eac89ac43ca95643798755ef3a9ead2e5d
4
+ data.tar.gz: 8a002e688b432c4f1ab1c728db82488c9d8e3b50
5
5
  SHA512:
6
- metadata.gz: 9444742027fab53cdbad0dbe72db8f0640c3e58a6b0c713e0f7e17dea39dc867cd9c44cdb0dfc4a695126a3ca3cd0dfa82cdf0629a560fc3f29c1dd1aec1fc48
7
- data.tar.gz: 86ac46ae6fea92ea34f57a4d74ef06cbcffd7a807954ff4b74d16f541b76ac916b4222a760b1f588c63625d8aaeb5066a1b02ff3b1fb4626f8e0764579f187c3
6
+ metadata.gz: 7ebc8d2d233d646fe7f49c1b910a943b28353e52f4114f5b4fe6b8689481ac3823bfbe16b0e0a15a02406bcdafd444098e922dd021ae90a9af2314bd9dad708c
7
+ data.tar.gz: 7d6a387331acf862a6c47a0096547b019b865c367d0fd0c7d5d01c24167d974d23b6c8aa8e43d4fb66bd699c4c0c789d4261f9562e37ce8630bb931262b1118b
data/README.md CHANGED
@@ -95,7 +95,7 @@ arbitrary text (has to match `[a-zA-Z0-9]+`) to the end of it and to calculate S
95
95
  in the hexadecimal format, for example:
96
96
 
97
97
  ```
98
- Input: "2017-07-19T21:24:51Z b1.zold.io 80 the-suffix"
98
+ Input: "2017-07-19T21:24:51Z b1.zold.io 4096 the-suffix"
99
99
  SHA-256: "eba36e52e1ee674d198f486e07c8496853ffc8879e7fe25329523177646a96a0"
100
100
  ```
101
101
 
@@ -104,7 +104,7 @@ SHA-256 hash that ends with `00000000` (eight zeros). For example, this
104
104
  suffix `11edb424c` works (it took 212 minutes to find it on 2.3GHz Intel Core i7):
105
105
 
106
106
  ```
107
- Input: "2017-07-19T21:24:51Z b1.zold.io 80 11edb424c"
107
+ Input: "2017-07-19T21:24:51Z b1.zold.io 4096 11edb424c"
108
108
  SHA-256: "34f48e0eee1ed12ad74cb39418f2f6e7442a776a7b6182697957650e00000000"
109
109
  ```
110
110
 
@@ -276,7 +276,7 @@ The directory `.zold` is automatically created and contains system data.
276
276
  these columns:
277
277
 
278
278
  * Address or IP;
279
- * TPC port (usually 80);
279
+ * TCP port (usually 4096);
280
280
  * Score (positive integer);
281
281
  * Time of score update, ISO 8601.
282
282
 
data/bin/zold CHANGED
@@ -155,7 +155,7 @@ Available options:"
155
155
  require_relative '../lib/zold/commands/fetch'
156
156
  args.each do |id|
157
157
  Zold::Fetch.new(
158
- wallet: wallets.find(Zold::Id.new(id)),
158
+ id: Zold::Id.new(id),
159
159
  remotes: remotes,
160
160
  copies: Zold::Copies.new(File.join(copies, id)),
161
161
  log: log
@@ -32,8 +32,8 @@ require_relative '../score.rb'
32
32
  module Zold
33
33
  # FETCH pulling command
34
34
  class Fetch
35
- def initialize(wallet:, remotes:, copies:, log: Log::Quiet.new)
36
- @wallet = wallet
35
+ def initialize(id:, remotes:, copies:, log: Log::Quiet.new)
36
+ @id = id
37
37
  @remotes = remotes
38
38
  @copies = copies
39
39
  @log = log
@@ -42,7 +42,7 @@ module Zold
42
42
  def run(_ = [])
43
43
  total = 0
44
44
  @remotes.all.each do |r|
45
- uri = URI("#{r[:home]}wallet/#{@wallet.id}")
45
+ uri = URI("#{r[:home]}wallet/#{@id}")
46
46
  res = Http.new(uri).get
47
47
  unless res.code == '200'
48
48
  @log.error("#{r[:host]}:#{r[:port]} \
@@ -37,10 +37,12 @@ module Zold
37
37
  def run(args = [])
38
38
  opts = Slop.parse(args, help: true) do |o|
39
39
  o.banner = 'Usage: zold node [options]'
40
- o.integer '--port', 'TCP port to open for the Net (default: 80)',
41
- default: 80
42
- o.integer '--bind-port', 'TCP port to listen on (default: 80)',
43
- default: 80
40
+ o.integer '--port',
41
+ "TCP port to open for the Net (default: #{Remotes::PORT})",
42
+ default: Remotes::PORT
43
+ o.integer '--bind-port',
44
+ "TCP port to listen on (default: #{Remotes::PORT})",
45
+ default: Remotes::PORT
44
46
  o.string '--host', 'Host name (default: 127.0.0.1)',
45
47
  default: '127.0.0.1'
46
48
  o.string '--home', 'Home directory (default: current directory)',
@@ -40,6 +40,24 @@ module Zold
40
40
  end
41
41
 
42
42
  def run(args = [])
43
+ opts = Slop.parse(args, help: true) do |o|
44
+ o.banner = "Usage: zold remote <command> [options]
45
+ Available commands:
46
+ #{Rainbow('remote show').green}
47
+ Show all registered remote nodes
48
+ #{Rainbow('remote clean').green}
49
+ Remove all registered remote nodes
50
+ #{Rainbow('remote reset').green}
51
+ Restore it back to the default list of nodes
52
+ #{Rainbow('remote add').green} host port
53
+ Add a new remote node
54
+ #{Rainbow('remote remove').green} host port
55
+ Remove the remote node
56
+ #{Rainbow('remote update').green}
57
+ Check each registered remote node for availability
58
+ Available options:"
59
+ o.bool '--help', 'Print instructions'
60
+ end
43
61
  command = args[0]
44
62
  case command
45
63
  when 'show'
@@ -75,21 +93,7 @@ module Zold
75
93
  @log.debug("There are #{total} known remotes")
76
94
  end
77
95
  else
78
- @log.info(
79
- "Available commands:
80
- #{Rainbow('remote show').green}
81
- Show all registered remote nodes
82
- #{Rainbow('remote clean').green}
83
- Remove all registered remote nodes
84
- #{Rainbow('remote reset').green}
85
- Restore it back to the default list of nodes
86
- #{Rainbow('remote add').green} host port
87
- Add a new remote node
88
- #{Rainbow('remote remove').green} host port
89
- Remove the remote node
90
- #{Rainbow('remote update').green}
91
- Check each registered remote node for availability"
92
- )
96
+ @log.info(opts.to_s)
93
97
  end
94
98
  end
95
99
 
@@ -123,7 +127,7 @@ module Zold
123
127
  @remotes.rescore(r[:host], r[:port], score.value)
124
128
  json['all'].each do |s|
125
129
  unless @remotes.exists?(s['host'], s['port'])
126
- run(['add', s['host'], s['port']])
130
+ run(['add', s['host'], s['port'].to_s])
127
131
  end
128
132
  end
129
133
  @log.info("#{r[:host]}:#{r[:port]}: #{Rainbow(score.value).green} \
@@ -115,7 +115,7 @@ module Zold
115
115
  wallet = wallets.find(id)
116
116
  request.body.rewind
117
117
  cps = copies(id)
118
- cps.add(request.body.read, 'remote', 80, 0)
118
+ cps.add(request.body.read, 'remote', Remotes::PORT, 0)
119
119
  Zold::Merge.new(wallet: wallet, copies: cps).run
120
120
  "Success, #{wallet.id} balance is #{wallet.balance}"
121
121
  end
data/lib/zold/remotes.rb CHANGED
@@ -29,6 +29,8 @@ require 'fileutils'
29
29
  module Zold
30
30
  # All remotes
31
31
  class Remotes
32
+ PORT = 4096
33
+
32
34
  def initialize(file)
33
35
  @file = file
34
36
  end
@@ -53,24 +55,24 @@ module Zold
53
55
  )
54
56
  end
55
57
 
56
- def exists?(host, port = 80)
58
+ def exists?(host, port = Remotes::PORT)
57
59
  !load.find { |r| r[:host] == host && r[:port] == port }.nil?
58
60
  end
59
61
 
60
- def add(host, port = 80)
62
+ def add(host, port = Remotes::PORT)
61
63
  list = load
62
64
  list << { host: host, port: port, score: 0 }
63
65
  list.uniq! { |r| "#{r[:host]}:#{r[:port]}" }
64
66
  save(list)
65
67
  end
66
68
 
67
- def remove(host, port = 80)
69
+ def remove(host, port = Remotes::PORT)
68
70
  list = load
69
71
  list.reject! { |r| r[:host] == host && r[:port] == port }
70
72
  save(list)
71
73
  end
72
74
 
73
- def score(host, port = 80)
75
+ def score(host, port = Remotes::PORT)
74
76
  load.find { |r| r[:host] == host && r[:port] == port }[:score]
75
77
  end
76
78
 
data/lib/zold/score.rb CHANGED
@@ -20,6 +20,7 @@
20
20
 
21
21
  require 'digest'
22
22
  require 'time'
23
+ require_relative 'remotes'
23
24
 
24
25
  # The score.
25
26
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -41,7 +42,7 @@ module Zold
41
42
  @strength = strength
42
43
  end
43
44
 
44
- ZERO = Score.new(Time.now, 'localhost', 80)
45
+ ZERO = Score.new(Time.now, 'localhost', Remotes::PORT)
45
46
 
46
47
  def self.parse(text, strength: DEFAULT_STRENGTH)
47
48
  _, time, host, port, suffixes = text.split(' ', 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.1.1'.freeze
26
+ VERSION = '0.1.2'.freeze
27
27
  end
@@ -57,7 +57,27 @@ class TestFetch < Minitest::Test
57
57
  )
58
58
  remotes.add('fake-1', 80)
59
59
  remotes.add('fake-2', 80)
60
- Zold::Fetch.new(wallet: wallet, copies: copies, remotes: remotes).run
60
+ Zold::Fetch.new(id: id, copies: copies, remotes: remotes).run
61
+ assert_equal(copies.all[0][:name], '1')
62
+ assert_equal(copies.all[0][:score], 0)
63
+ end
64
+ end
65
+
66
+ def test_fetches_empty_wallet
67
+ Dir.mktmpdir 'test' do |dir|
68
+ id = Zold::Id.new
69
+ copies = Zold::Copies.new(File.join(dir, 'copies'))
70
+ remotes = Zold::Remotes.new(File.join(dir, 'remotes.csv'))
71
+ remotes.clean
72
+ stub_request(:get, "http://fake-1/wallet/#{id}").to_return(
73
+ status: 200,
74
+ body: {
75
+ 'score': Zold::Score::ZERO.to_h,
76
+ 'body': 'the body'
77
+ }.to_json
78
+ )
79
+ remotes.add('fake-1', 80)
80
+ Zold::Fetch.new(id: id, copies: copies, remotes: remotes).run
61
81
  assert_equal(copies.all[0][:name], '1')
62
82
  assert_equal(copies.all[0][:score], 0)
63
83
  end
@@ -59,4 +59,30 @@ class TestMerge < Minitest::Test
59
59
  Zold::Merge.new(wallet: wallet, copies: copies).run
60
60
  end
61
61
  end
62
+
63
+ def test_merges_into_empty_wallet
64
+ Dir.mktmpdir 'test' do |dir|
65
+ id = Zold::Id.new
66
+ file = File.join(dir, id.to_s)
67
+ wallet = Zold::Wallet.new(file)
68
+ wallet.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
69
+ first = Zold::Wallet.new(File.join(dir, 'copy-1'))
70
+ File.write(first.path, File.read(wallet.path))
71
+ second = Zold::Wallet.new(File.join(dir, 'copy-2'))
72
+ File.write(second.path, File.read(wallet.path))
73
+ Zold::Pay.new(
74
+ payer: first,
75
+ receiver: second,
76
+ amount: Zold::Amount.new(zld: 14.95),
77
+ pvtkey: Zold::Key.new(file: 'fixtures/id_rsa')
78
+ ).run(['--force'])
79
+ copies = Zold::Copies.new(File.join(dir, 'copies'))
80
+ copies.add(File.read(first.path), 'host-1', 80, 5)
81
+ copies.add(File.read(second.path), 'host-2', 80, 5)
82
+ Zold::Merge.new(
83
+ wallet: Zold::Wallet.new(File.join(dir, Zold::Id.new.to_s)),
84
+ copies: copies
85
+ ).run
86
+ end
87
+ end
62
88
  end
data/test/test_remotes.rb CHANGED
@@ -66,7 +66,7 @@ class TestRemotes < Minitest::Test
66
66
  remotes = Zold::Remotes.new(file)
67
67
  remotes.add('127.0.0.1', 80)
68
68
  remotes.rescore('127.0.0.1', 80, 15)
69
- assert_equal(remotes.score('127.0.0.1'), 15)
69
+ assert_equal(remotes.score('127.0.0.1', 80), 15)
70
70
  end
71
71
  end
72
72
  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.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko