zold 0.10.12 → 0.10.13

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
  SHA1:
3
- metadata.gz: 1231985c6293cf6bd772a545a6a384f697e365c8
4
- data.tar.gz: 1a07fc356b7424aa715389bfbd71bbbb2f58ad43
3
+ metadata.gz: e6232c5057d899ff2c3600af61ca53b872a01c79
4
+ data.tar.gz: 5ad79a8d968e3e03fa70c3955bfe813c1c493bec
5
5
  SHA512:
6
- metadata.gz: da488d1c4d6948209c0cf22d903dd039950e24c184be3362fdeb641edc458ba843eafb7fbfbf439d92db69d600fea84bfd13e7ec49234436c980b266475803d4
7
- data.tar.gz: 4bc5b94357ee8a02beaa41526d5b9a468d8ee857bd0a1287902a41efd1880eb2c0b4f6b934d354b297e4a2c340f19f7ffedfc0ffacc995f4d39915e31087a6cb
6
+ metadata.gz: '01845774b4f1783571f47097513d006cbb670da6e0ca040df72039092cdafabd2e588bc470932eb8ee4bb84f423d52f4eabaa5a9c0ab353937d7ccde37e89e48'
7
+ data.tar.gz: 20cca37c6f77efdc6a150e73d574db3c92da458311197c639cc493c7b6f2297f956b90aeada211e44143ee2e7cc7d8ec16e62b870f910888e91406fd4708a1ff
@@ -0,0 +1,54 @@
1
+ <img src="http://www.zold.io/logo.svg" width="92px" height="92px"/>
2
+
3
+ This is how you install `zold` Ruby gem on different platform.
4
+
5
+ We are very interested in your contribution to this document.
6
+ If and when you experience any problems, make changes here via a pull request.
7
+
8
+ Basically, you need to
9
+ install [Ruby 2.3+](https://www.ruby-lang.org/en/documentation/installation/),
10
+ [Rubygems](https://rubygems.org/pages/download), and
11
+ then the [gem](https://rubygems.org/gems/zold).
12
+
13
+ ## Ubuntu 16.04
14
+
15
+ ```bash
16
+ $ sudo apt-get update -y
17
+ $ sudo apt-get install -y ruby-dev rubygems zlib1g-dev libssl-dev
18
+ $ gem install zold
19
+ ```
20
+
21
+ ## OSX
22
+
23
+ With homebrew:
24
+
25
+ ```
26
+ $ brew install rbenv ruby-build
27
+ $ rbenv install 2.5.1
28
+ $ rbenv global 2.5.1
29
+ $ ruby -v
30
+ $ gem install zold
31
+ ```
32
+
33
+ Without homebrew:
34
+
35
+ ... no idea ...
36
+
37
+ ## Windows
38
+
39
+ ... please contribute ...
40
+
41
+ ## CentOS
42
+
43
+ ... please contribute ...
44
+
45
+ ## Amazon Linux (AWS EC2 default image)
46
+
47
+ ```
48
+ sudo yum install zlib-devel gcc gcc-c++ ruby-devel rubygems ruby
49
+ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
50
+ curl -sSL https://get.rvm.io | bash -s stable
51
+ source $HOME/.rvm/scripts/rvm
52
+ rvm install 2.5.1
53
+ gem install zold
54
+ ```
data/README.md CHANGED
@@ -27,12 +27,8 @@ The license is [MIT](https://github.com/yegor256/zold/blob/master/LICENSE.txt).
27
27
 
28
28
  First, install [Ruby 2.3+](https://www.ruby-lang.org/en/documentation/installation/),
29
29
  [Rubygems](https://rubygems.org/pages/download), and
30
- the [gem](https://rubygems.org/gems/zold):
31
-
32
- ```bash
33
- $ sudo apt-get install ruby-dev rubygems zlib1g-dev libssl-dev
34
- $ gem install zold
35
- ```
30
+ the [gem](https://rubygems.org/gems/zold).
31
+ Here is [how](https://github.com/yegor256/zold/blob/master/INSTALL.md).
36
32
 
37
33
  To make sure it's installed, try:
38
34
 
@@ -98,7 +94,9 @@ and your own public IP address instead of `4.4.4.4`):
98
94
  $ zold node --trace --verbose --invoice=5f96e731e48ae21f --host=4.4.4.4
99
95
  ```
100
96
 
101
- Then, open the page `4.4.4.4:4096` in your browser.
97
+ Then, open the page `4.4.4.4:4096` in your browser
98
+ (you may need to open the inbound port at your
99
+ [IP firewall](https://www.howtogeek.com/177621/the-beginners-guide-to-iptables-the-linux-firewall/)).
102
100
  If you see a simple JSON document, everything is fine.
103
101
  Next, hit <kbd>Ctrl</kbd>+<kbd>c</kbd> and run this line, in order
104
102
  to start the node and make sure it will be online even when you log off
@@ -63,6 +63,8 @@ Available options:"
63
63
  calculate(opts)
64
64
  end
65
65
 
66
+ private
67
+
66
68
  def calculate(opts)
67
69
  start = Time.now
68
70
  mstart = Time.now
@@ -55,6 +55,8 @@ Available options:"
55
55
  create(mine.empty? ? Id.new : Id.new(mine[0]), opts)
56
56
  end
57
57
 
58
+ private
59
+
58
60
  def create(id, opts)
59
61
  wallet = @wallets.find(id)
60
62
  key = Zold::Key.new(file: opts['public-key'])
@@ -59,6 +59,8 @@ Available options:"
59
59
  stdout
60
60
  end
61
61
 
62
+ private
63
+
62
64
  def diff(wallet, cps, _)
63
65
  raise 'There are no remote copies, try FETCH first' if cps.all.empty?
64
66
  cps = cps.all.sort_by { |c| c[:score] }.reverse
@@ -62,6 +62,8 @@ Available options:"
62
62
  end
63
63
  end
64
64
 
65
+ private
66
+
65
67
  def fetch(id, cps, opts)
66
68
  total = 0
67
69
  @remotes.iterate(@log) do |r|
@@ -54,6 +54,8 @@ Available options:"
54
54
  invoice(wallet, opts)
55
55
  end
56
56
 
57
+ private
58
+
57
59
  def invoice(wallet, opts)
58
60
  invoice = "#{Prefixes.new(wallet).create(opts[:length])}@#{wallet.id}"
59
61
  @log.info(invoice)
@@ -59,6 +59,8 @@ Available options:"
59
59
  modified
60
60
  end
61
61
 
62
+ private
63
+
62
64
  def merge(wallet, cps, _)
63
65
  raise 'There are no remote copies, try FETCH first' if cps.all.empty?
64
66
  cps = cps.all.sort_by { |c| c[:score] }.reverse
@@ -62,6 +62,9 @@ module Zold
62
62
  o.bool '--standalone',
63
63
  'Never communicate with other nodes (mostly for testing)',
64
64
  default: false
65
+ o.bool '--ignore-score-weakness',
66
+ 'Ignore score weakness of incoming requests and register those nodes anyway',
67
+ default: false
65
68
  o.bool '--never-reboot',
66
69
  'Don\'t reboot when a new version shows up in the network',
67
70
  default: false
@@ -87,6 +90,7 @@ module Zold
87
90
  else
88
91
  remotes = Remotes.new(File.join(opts[:home], 'zold-remotes'))
89
92
  end
93
+ Front.set(:ignore_score_weakness, opts['ignore-score-weakness'])
90
94
  wallets = Wallets.new(File.join(opts[:home], 'zold-wallets'))
91
95
  Front.set(:wallets, wallets)
92
96
  Front.set(:remotes, remotes)
@@ -124,7 +128,7 @@ module Zold
124
128
  end
125
129
  end
126
130
  end
127
- @log.debug('Starting up the web front...')
131
+ @log.debug("Starting up the web front at http://#{opts[:host]}:#{opts[:port]}...")
128
132
  begin
129
133
  Front.run!
130
134
  ensure
@@ -79,6 +79,8 @@ Available options:"
79
79
  pay(from, invoice, amount, details, opts)
80
80
  end
81
81
 
82
+ private
83
+
82
84
  def pay(from, invoice, amount, details, opts)
83
85
  unless opts.force?
84
86
  raise 'The amount can\'t be zero' if amount.zero?
@@ -54,6 +54,8 @@ Available options:"
54
54
  modified
55
55
  end
56
56
 
57
+ private
58
+
57
59
  # Returns list of Wallet IDs which were affected
58
60
  def propagate(wallet, _)
59
61
  me = wallet.id
@@ -58,6 +58,8 @@ Available options:"
58
58
  end
59
59
  end
60
60
 
61
+ private
62
+
61
63
  def push(wallet, opts)
62
64
  total = 0
63
65
  @remotes.iterate(@log) do |r|
@@ -62,6 +62,9 @@ Available options:"
62
62
  o.bool '--ignore-score-weakness',
63
63
  'Don\'t complain when their score is too weak',
64
64
  default: false
65
+ o.bool '--force',
66
+ 'Add/remove if if this operation is not possible',
67
+ default: false
65
68
  o.bool '--reboot',
66
69
  'Exit if any node reports version higher than we have',
67
70
  default: false
@@ -77,9 +80,9 @@ Available options:"
77
80
  when 'reset'
78
81
  reset
79
82
  when 'add'
80
- add(mine[1], mine[2] ? mine[2].to_i : Remotes::PORT)
83
+ add(mine[1], mine[2] ? mine[2].to_i : Remotes::PORT, opts)
81
84
  when 'remove'
82
- remove(mine[1], mine[2] ? mine[2].to_i : Remotes::PORT)
85
+ remove(mine[1], mine[2] ? mine[2].to_i : Remotes::PORT, opts)
83
86
  when 'update'
84
87
  update(opts)
85
88
  update(opts, false)
@@ -88,6 +91,8 @@ Available options:"
88
91
  end
89
92
  end
90
93
 
94
+ private
95
+
91
96
  def show
92
97
  @remotes.all.each do |r|
93
98
  score = Rainbow("/#{r[:score]}").color(r[:score] > 0 ? :green : :red)
@@ -105,15 +110,25 @@ Available options:"
105
110
  @log.debug('Remote nodes set back to default')
106
111
  end
107
112
 
108
- def add(host, port)
109
- @remotes.add(host, port)
110
- @log.info("#{host}:#{port} added to the list")
113
+ def add(host, port, opts)
114
+ if @remotes.exists?(host, port)
115
+ raise "#{host}:#{port} already exists in the list" unless opts['force']
116
+ @log.info("#{host}:#{port} already exists in the list")
117
+ else
118
+ @remotes.add(host, port)
119
+ @log.info("#{host}:#{port} added to the list")
120
+ end
111
121
  @log.info("There are #{@remotes.all.count} remote nodes in the list")
112
122
  end
113
123
 
114
- def remove(host, port)
115
- @remotes.remove(host, port)
116
- @log.info("#{host}:#{port} removed from the list")
124
+ def remove(host, port, opts)
125
+ if @remotes.exists?(host, port)
126
+ @remotes.remove(host, port)
127
+ @log.info("#{host}:#{port} removed from the list")
128
+ else
129
+ raise "#{host}:#{port} is not in the list" unless opts['force']
130
+ @log.info("#{host}:#{port} is not in the list")
131
+ end
117
132
  @log.info("There are #{@remotes.all.count} remote nodes in the list")
118
133
  end
119
134
 
@@ -135,7 +150,7 @@ Available options:"
135
150
  end
136
151
  if deep
137
152
  json['all'].each do |s|
138
- add(s['host'], s['port']) unless @remotes.exists?(s['host'], s['port'])
153
+ add(s['host'], s['port'], opts) unless @remotes.exists?(s['host'], s['port'])
139
154
  end
140
155
  end
141
156
  capacity << { host: score.host, port: score.port, count: json['all'].count }
@@ -57,6 +57,8 @@ Available options:"
57
57
  end
58
58
  end
59
59
 
60
+ private
61
+
60
62
  def show(wallet, _)
61
63
  balance = wallet.balance
62
64
  wallet.txns.each do |t|
@@ -82,6 +82,8 @@ Available options:"
82
82
  end
83
83
  end
84
84
 
85
+ private
86
+
85
87
  def pay(wallet, _)
86
88
  raise 'The wallet is absent' unless wallet.exists?
87
89
  tax = Tax.new(wallet)
@@ -113,8 +115,6 @@ Available options:"
113
115
  raise 'Not implemented yet'
114
116
  end
115
117
 
116
- private
117
-
118
118
  def top_scores
119
119
  best = []
120
120
  @remotes.iterate(@log) do |r|
@@ -98,7 +98,7 @@ module Zold
98
98
  'Connection': 'close'
99
99
  }
100
100
  headers[Http::VERSION_HEADER] = VERSION
101
- headers[Http::SCORE_HEADER] = @score.reduced(4).to_s if @score.valid? && @score.value >= 3 && !score.expired?
101
+ headers[Http::SCORE_HEADER] = @score.reduced(4).to_text if @score.valid? && !@score.expired?
102
102
  headers
103
103
  end
104
104
  end
@@ -45,6 +45,7 @@ module Zold
45
45
  set :lock, false
46
46
  set :show_exceptions, false
47
47
  set :server, 'webrick'
48
+ set :ignore_score_weakness, false # to be injected at node.rb
48
49
  set :reboot, false # to be injected at node.rb
49
50
  set :home, nil? # to be injected at node.rb
50
51
  set :logging, true # to be injected at node.rb
@@ -58,11 +59,18 @@ module Zold
58
59
  end
59
60
 
60
61
  before do
61
- return unless request.env[Http::SCORE_HEADER]
62
- return unless settings.remotes.empty?
63
- s = Score.parse(request.env[Http::SCORE_HEADER])
62
+ header = request.env["HTTP-#{Http::SCORE_HEADER}".upcase.tr('-', '_')]
63
+ return unless header
64
+ return if settings.remotes.all.empty?
65
+ s = Score.parse_text(header)
64
66
  error(400, 'The score is invalid') unless s.valid?
65
- settings.remotes.add(s.host, s.port) if s.value > 3
67
+ error(400, 'The score is weak') if s.strength < Score::STRENGTH && !settings.ignore_score_weakness
68
+ if s.value > 3
69
+ require_relative '../commands/remote'
70
+ Remote.new(remotes: settings.remotes, log: settings.log).run(
71
+ ['remote', 'add', s.host, s.port.to_s, '--force']
72
+ )
73
+ end
66
74
  end
67
75
 
68
76
  after do
@@ -70,6 +78,7 @@ module Zold
70
78
  headers['Connection'] = 'close'
71
79
  headers['X-Zold-Version'] = VERSION
72
80
  headers['Access-Control-Allow-Origin'] = '*'
81
+ headers[Http::SCORE_HEADER] = score.reduced(16).to_s
73
82
  end
74
83
 
75
84
  get '/robots.txt' do
@@ -64,10 +64,10 @@ module Zold
64
64
  )
65
65
  end
66
66
 
67
- def self.parse(text, strength: STRENGTH)
67
+ def self.parse(text)
68
68
  m = Regexp.new(
69
69
  '^' + [
70
- '([0-9]+:)',
70
+ '([0-9]+)/(?<strength>[0-9]+):',
71
71
  '(?<time>[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)',
72
72
  '(?<host>[0-9a-z\.\-]+)',
73
73
  '(?<port>[0-9]+)',
@@ -80,19 +80,19 @@ module Zold
80
80
  Time.parse(m[:time]), m[:host],
81
81
  m[:port].to_i, m[:invoice],
82
82
  m[:suffixes].split(' '),
83
- strength: strength
83
+ strength: m[:strength].to_i
84
84
  )
85
85
  end
86
86
 
87
- def self.parse_text(text, strength: STRENGTH)
88
- parts = text.split(' ', 6)
87
+ def self.parse_text(text)
88
+ parts = text.split(' ', 7)
89
89
  Score.new(
90
- Time.at(parts[0].hex),
91
- parts[1],
92
- parts[2].hex,
93
- "#{parts[3]}@#{parts[4]}",
94
- parts[5].split(' '),
95
- strength: strength
90
+ Time.at(parts[1].hex),
91
+ parts[2],
92
+ parts[3].hex,
93
+ "#{parts[4]}@#{parts[5]}",
94
+ parts[6] ? parts[6].split(' ') : [],
95
+ strength: parts[0].to_i
96
96
  )
97
97
  end
98
98
 
@@ -106,6 +106,7 @@ module Zold
106
106
  def to_text
107
107
  pfx, bnf = @invoice.split('@')
108
108
  [
109
+ @strength,
109
110
  @time.to_i.to_s(16),
110
111
  @host,
111
112
  @port.to_s(16),
@@ -117,7 +118,7 @@ module Zold
117
118
 
118
119
  def to_s
119
120
  [
120
- "#{value}:",
121
+ "#{value}/#{@strength}:",
121
122
  @time.utc.iso8601,
122
123
  @host,
123
124
  @port,
@@ -140,10 +141,9 @@ module Zold
140
141
  end
141
142
 
142
143
  def reduced(max = 4)
143
- raise "The length of the score is #{@suffixes.count}, can't reduce to #{max}" if max > @suffixes.count
144
144
  Score.new(
145
145
  @time, @host, @port, @invoice,
146
- @suffixes[0..max - 1], strength: @strength
146
+ @suffixes[0..[max, @suffixes.count].min - 1], strength: @strength
147
147
  )
148
148
  end
149
149
 
@@ -23,5 +23,5 @@
23
23
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
24
24
  # License:: MIT
25
25
  module Zold
26
- VERSION = '0.10.12'.freeze
26
+ VERSION = '0.10.13'.freeze
27
27
  end
@@ -33,7 +33,7 @@ class FakeNode
33
33
  @log = log
34
34
  end
35
35
 
36
- def run
36
+ def run(args = ['--standalone'])
37
37
  WebMock.allow_net_connect!
38
38
  Dir.mktmpdir 'test' do |dir|
39
39
  server = TCPServer.new('127.0.0.1', 0)
@@ -46,14 +46,13 @@ class FakeNode
46
46
  require_relative '../../lib/zold/commands/node'
47
47
  Zold::Node.new(log: @log).run(
48
48
  [
49
- '--standalone',
50
- '--invoice', 'NOPREFIX@ffffffffffffffff',
51
49
  '--port', port.to_s,
52
50
  '--host=locahost',
53
51
  '--bind-port', port.to_s,
54
52
  '--threads=1',
55
- '--home', home
56
- ]
53
+ '--home', home,
54
+ '--invoice=NOPREFIX@ffffffffffffffff'
55
+ ] + args
57
56
  )
58
57
  end
59
58
  end
@@ -19,18 +19,15 @@
19
19
  # SOFTWARE.
20
20
 
21
21
  require 'minitest/autorun'
22
- require 'tmpdir'
23
- require_relative '../../lib/zold/key'
24
- require_relative '../../lib/zold/id'
25
- require_relative '../../lib/zold/log'
26
- require_relative '../../lib/zold/amount'
27
- require_relative '../../lib/zold/wallet'
28
- require_relative '../../lib/zold/http'
22
+ require 'json'
23
+ require_relative '../test__helper'
29
24
  require_relative 'fake_node'
25
+ require_relative '../../lib/zold/http'
26
+ require_relative '../../lib/zold/score'
30
27
 
31
28
  class FrontTest < Minitest::Test
32
29
  def test_renders_public_pages
33
- FakeNode.new.run do |port|
30
+ FakeNode.new(log: $log).run(['--ignore-score-weakness']) do |port|
34
31
  {
35
32
  '200' => [
36
33
  '/robots.txt',
@@ -52,6 +49,14 @@ class FrontTest < Minitest::Test
52
49
  )
53
50
  end
54
51
  end
52
+ score = Zold::Score.new(
53
+ Time.now, 'localhost', 999,
54
+ 'NOPREFIX@ffffffffffffffff',
55
+ strength: 1
56
+ ).next.next.next.next
57
+ Zold::Http.new(URI("http://localhost:#{port}/"), score).get
58
+ json = JSON.parse(Zold::Http.new(URI("http://localhost:#{port}/remotes"), score).get.body)
59
+ assert(json['all'].find { |r| r['host'] == 'localhost' })
55
60
  end
56
61
  end
57
62
  end
@@ -35,3 +35,6 @@ $log = Zold::Log::Verbose.new
35
35
 
36
36
  require 'minitest/autorun'
37
37
  require_relative '../lib/zold'
38
+
39
+ gem 'openssl'
40
+ require 'openssl'
@@ -52,10 +52,10 @@ class TestScore < Minitest::Test
52
52
  score = Zold::Score.parse(
53
53
  Zold::Score.new(
54
54
  time, 'localhost', 999, 'NOPREFIX@ffffffffffffffff',
55
- %w[FIRST SECOND THIRD]
56
- ).to_s
55
+ strength: 1
56
+ ).next.next.to_s
57
57
  )
58
- assert_equal(3, score.value)
58
+ assert_equal(2, score.value)
59
59
  assert_equal(score.time.to_s, time.to_s)
60
60
  assert_equal('localhost', score.host)
61
61
  assert_equal(999, score.port)
@@ -66,11 +66,11 @@ class TestScore < Minitest::Test
66
66
  score = Zold::Score.parse_text(
67
67
  Zold::Score.new(
68
68
  time, 'a.example.com', 999, 'NOPREFIX@ffffffffffffffff',
69
- %w[FIRST SECOND THIRD]
70
- ).to_text
69
+ strength: 1
70
+ ).next.next.next.to_text
71
71
  )
72
72
  assert_equal(3, score.value)
73
- assert_equal(score.time.to_s, time.to_s)
73
+ assert_equal(score.time.utc.to_s, time.utc.to_s)
74
74
  assert_equal('a.example.com', score.host)
75
75
  assert_equal(999, score.port)
76
76
  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.10.12
4
+ version: 0.10.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -268,6 +268,7 @@ files:
268
268
  - ".simplecov"
269
269
  - ".travis.yml"
270
270
  - Gemfile
271
+ - INSTALL.md
271
272
  - LICENSE.txt
272
273
  - Procfile
273
274
  - README.md