zold 0.14.12 → 0.14.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: eebbf694c3af2594b3ff82b988bfdc53a5c926f2
4
- data.tar.gz: a10ed4895198184566d4495fa8ddc7a5d6fb42bb
3
+ metadata.gz: 15c46c2740b4034dae78391ab88d747116977858
4
+ data.tar.gz: 89bcbf8445379dddbff308e0dfb0a60fb11157e6
5
5
  SHA512:
6
- metadata.gz: 2b064373631472b0ea87c017d662ba5886fc6a4258a60387431bc4cc71fd65b47de25c20f5aea3f3031680bc07d1c53d6ffd2de8523698be265794f8d96b5936
7
- data.tar.gz: c39bd06b4edf7f3f520b7ed7732eb4130662354a0845897ac0087ac39684d9d8671da753d2bae087c68a4f02e5d158fa3c191ef67a4abf33e56caa57509bf5d6
6
+ metadata.gz: 1236cdbd4219b2b61dd11ffe556f26e867a2a91985d14c1d670ed2388393451e38fef70baefbaa0dd699c56b754798350a9e9afbdd116fdb7f5e21b0074ee586
7
+ data.tar.gz: ae55db8d0224a10c1facebf9a3e52dc2f7be2baf9285138af232b808bba340171f74335237abf0a13909e24bf772671016de48873ec04858d552c8fc44905975
@@ -50,6 +50,10 @@ module Zold
50
50
  # production one.
51
51
  NETWORK_HEADER = 'X-Zold-Network'
52
52
 
53
+ # HTTP header we add, in order to inform the node about our
54
+ # protocol.
55
+ PROTOCOL_HEADER = 'X-Zold-Protocol'
56
+
53
57
  # @todo #98:30m/DEV The following two statements are seen as issues by rubocop
54
58
  # raising a Lint/AmbiguousBlockAssociation offense. It is somthing
55
59
  # that could be solved by changing the TargetRubyVersion in .rubocop.yml
@@ -119,7 +123,8 @@ module Zold
119
123
  'Connection': 'close',
120
124
  'Accept-Encoding': 'gzip'
121
125
  }
122
- headers[Http::VERSION_HEADER] = VERSION
126
+ headers[Http::VERSION_HEADER] = Zold::VERSION
127
+ headers[Http::PROTOCOL_HEADER] = Zold::PROTOCOL.to_s
123
128
  headers[Http::NETWORK_HEADER] = network
124
129
  headers[Http::SCORE_HEADER] = score.reduced(4).to_text if score.valid? && !score.expired? && score.value > 3
125
130
  headers
@@ -45,6 +45,17 @@ module Zold
45
45
  @mutex = Mutex.new
46
46
  end
47
47
 
48
+ def to_json
49
+ json = {
50
+ 'queue': queue.count,
51
+ 'pool.length': @pool.length,
52
+ 'pool.running': @pool.running?
53
+ }
54
+ opts = queue
55
+ json['queue_age'] = opts.empty? ? 0 : Time.now - File.mtime(File.join(@dir, opts[0]))
56
+ @entrance.to_json.merge(json)
57
+ end
58
+
48
59
  def start
49
60
  @entrance.start do
50
61
  FileUtils.mkdir_p(@dir)
@@ -53,8 +64,8 @@ module Zold
53
64
  )
54
65
  AsyncEntrance::THREADS.times do
55
66
  @pool.post do
56
- VerboseThread.new(@log).run(true) do
57
- loop do
67
+ loop do
68
+ VerboseThread.new(@log).run(true) do
58
69
  take
59
70
  break if @pool.shuttingdown?
60
71
  sleep Random.rand(100) / 100
@@ -77,14 +88,6 @@ module Zold
77
88
  end
78
89
  end
79
90
 
80
- def to_json
81
- @entrance.to_json.merge(
82
- 'queue': queue.count,
83
- 'pool.length': @pool.length,
84
- 'pool.running': @pool.running?
85
- )
86
- end
87
-
88
91
  def push(id, body)
89
92
  @mutex.synchronize do
90
93
  AtomicFile.new(File.join(@dir, id.to_s)).write(body)
@@ -74,6 +74,11 @@ module Zold
74
74
  raise "Network name mismatch, you are in '#{header}', we are in '#{settings.network}'"
75
75
  end
76
76
  end
77
+ check_header(Http::PROTOCOL_HEADER) do |header|
78
+ if header != settings.protocol.to_s
79
+ raise "Protocol mismatch, you are in '#{header}', we are in '#{settings.protocol}'"
80
+ end
81
+ end
77
82
  check_header(Http::SCORE_HEADER) do |header|
78
83
  if settings.remotes.all.empty?
79
84
  settings.log.debug("#{request.url}: we are in standalone mode, won't update remotes")
@@ -98,7 +103,7 @@ module Zold
98
103
  headers['Cache-Control'] = 'no-cache'
99
104
  headers['Connection'] = 'close'
100
105
  headers['X-Zold-Version'] = settings.version
101
- headers['X-Zold-Protocol'] = settings.protocol.to_s
106
+ headers[Http::PROTOCOL_HEADER] = settings.protocol.to_s
102
107
  headers['Access-Control-Allow-Origin'] = '*'
103
108
  headers[Http::SCORE_HEADER] = score.reduced(16).to_s
104
109
  end
@@ -161,6 +166,7 @@ module Zold
161
166
  wallets: settings.wallets.all.count,
162
167
  mtime: wallet.mtime.utc.iso8601,
163
168
  digest: wallet.digest,
169
+ balance: wallet.balance.to_i,
164
170
  body: AtomicFile.new(wallet.path).read
165
171
  }.to_json
166
172
  end
@@ -58,13 +58,24 @@ module Zold
58
58
  end
59
59
  @network = wallet.network
60
60
  end
61
- if wallet.network != @network
62
- raise "The wallet is from a different network '#{wallet.network}', ours is '#{@network}'"
61
+ unless wallet.network == @network
62
+ @log.error("The wallet is from a different network '#{wallet.network}', ours is '#{@network}'")
63
+ return
64
+ end
65
+ unless wallet.key == @key
66
+ @log.error('Public key mismatch')
67
+ return
68
+ end
69
+ unless wallet.id == @id
70
+ @log.error("Wallet ID mismatch, ours is #{@id}, theirs is #{wallet.id}")
71
+ return
63
72
  end
64
- raise 'Public key mismatch' if wallet.key != @key
65
- raise "Wallet ID mismatch: #{@id} != #{wallet.id}" if wallet.id != @id
66
73
  wallet.txns.each do |txn|
67
74
  next if @txns.find { |t| t == txn }
75
+ if @txns.find { |t| t.id == txn.id && t.bnf == txn.bnf }
76
+ @log.error("A transaction with the same ID #{t.id} and BNF #{t.bnf} already exists")
77
+ next
78
+ end
68
79
  if txn.amount.negative?
69
80
  dup = @txns.find { |t| t.id == txn.id && t.amount.negative? }
70
81
  if dup
@@ -110,27 +110,27 @@ module Zold
110
110
  def self.parse(line, idx = 0)
111
111
  regex = Regexp.new(
112
112
  '^' + [
113
- '([0-9a-f]{4})',
114
- '([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)',
115
- '([0-9a-f]{16})',
116
- "(#{Txn::RE_PREFIX})",
117
- '([0-9a-f]{16})',
118
- "(#{Txn::RE_DETAILS})",
119
- '([A-Za-z0-9+/]+={0,3})?'
113
+ '(?<id>[0-9a-f]{4})',
114
+ '(?<date>[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)',
115
+ '(?<amount>[0-9a-f]{16})',
116
+ "(?<prefix>#{Txn::RE_PREFIX})",
117
+ '(?<bnf>[0-9a-f]{16})',
118
+ "(?<details>#{Txn::RE_DETAILS})",
119
+ '(?<sign>[A-Za-z0-9+/]+={0,3})?'
120
120
  ].join(';') + '$'
121
121
  )
122
122
  clean = line.strip
123
- raise "Invalid line ##{idx}: #{line.inspect} #{regex}" unless regex.match(clean)
124
- parts = clean.split(';')
123
+ parts = regex.match(clean)
124
+ raise "Invalid line ##{idx}: #{line.inspect} #{regex}" unless parts
125
125
  txn = Txn.new(
126
- Hexnum.parse(parts[0]).to_i,
127
- Time.parse(parts[1]),
128
- Amount.new(coins: Hexnum.parse(parts[2]).to_i),
129
- parts[3],
130
- Id.new(parts[4]),
131
- parts[5]
126
+ Hexnum.parse(parts[:id]).to_i,
127
+ Time.parse(parts[:date]),
128
+ Amount.new(coins: Hexnum.parse(parts[:amount]).to_i),
129
+ parts[:prefix],
130
+ Id.new(parts[:bnf]),
131
+ parts[:details]
132
132
  )
133
- txn.sign = parts[6]
133
+ txn.sign = parts[:sign]
134
134
  txn
135
135
  end
136
136
  end
@@ -25,6 +25,6 @@
25
25
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
26
26
  # License:: MIT
27
27
  module Zold
28
- VERSION = '0.14.12'
28
+ VERSION = '0.14.13'
29
29
  PROTOCOL = 2
30
30
  end
@@ -59,11 +59,18 @@ class TestFetch < Minitest::Test
59
59
  remotes.add('localhost', 80)
60
60
  remotes.add('localhost', 81)
61
61
  copies = home.copies(wallet)
62
- Zold::Fetch.new(wallets: home.wallets, copies: copies.root, remotes: remotes, log: test_log).run(
63
- ['fetch', '--ignore-score-weakness', wallet.id.to_s]
64
- )
65
- assert_equal(copies.all[0][:name], '1')
66
- assert_equal(copies.all[0][:score], 0)
62
+ begin
63
+ retries ||= 0
64
+ Zold::Fetch.new(wallets: home.wallets, copies: copies.root, remotes: remotes, log: test_log).run(
65
+ ['fetch', '--ignore-score-weakness', wallet.id.to_s]
66
+ )
67
+ rescue StandardError => _
68
+ sleep 1
69
+ retry if (retries += 1) < 3
70
+ end
71
+ assert_equal(1, copies.all.count)
72
+ assert_equal('1', copies.all[0][:name])
73
+ assert_equal(0, copies.all[0][:score])
67
74
  end
68
75
  end
69
76
  end
@@ -50,7 +50,7 @@ class TestZold < Minitest::Test
50
50
  out << line
51
51
  end
52
52
  code = thr.value.to_i
53
- assert_equal(0, code, f + out.join("\n"))
53
+ assert_equal(0, code, f + out.join)
54
54
  end
55
55
  end
56
56
  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.14.12
4
+ version: 0.14.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko