lib/zold/commands/fetch.rb in zold-0.14.3 vs lib/zold/commands/fetch.rb in zold-0.14.4

- old
+ new

@@ -53,10 +53,13 @@ 'Don\'t complain when their score is too weak', default: false o.array '--ignore-node', 'Ignore this node and don\'t fetch from it', default: [] + o.string '--network', + 'The name of the network we work in', + default: 'test' o.bool '--help', 'Print instructions' end mine = Args.new(opts, @log).take || return mine = @wallets.all if mine.empty? mine.map { |i| Id.new(i) }.each do |id| @@ -97,13 +100,23 @@ r.assert_score_strength(score) unless opts['ignore-score-weakness'] Tempfile.open(['', Wallet::EXTENSION]) do |f| body = json['body'] File.write(f, body) wallet = Wallet.new(f.path) - copy = cps.add(body, score.host, score.port, score.value) - @log.info("#{r} returned #{body.length}b/#{wallet.txns.count}t/#{digest(json)}/#{age(json)} as copy #{copy} \ -of #{id} in #{(Time.now - start).round(2)}s: #{Rainbow(score.value).green} (#{json['version']})") + wallet.refurbish + if wallet.protocol != Zold::PROTOCOL + raise "Protocol #{wallet.protocol} doesn't match #{Zold::PROTOCOL} in #{id}" + end + if wallet.network != opts['network'] + raise "The wallet #{id} is in network #{wallet.network}, while we are in #{opts['network']}" + end + if wallet.balance.negative? && !wallet.root? + raise "The balance of #{id} is #{wallet.balance} and it's not a root wallet" + end + copy = cps.add(File.read(f), score.host, score.port, score.value) + @log.info("#{r} returned #{body.length}b/#{wallet.balance}/#{wallet.txns.count}t/#{digest(json)}/#{age(json)} \ +as copy #{copy} of #{id} in #{(Time.now - start).round(2)}s: #{Rainbow(score.value).green} (#{json['version']})") end score.value end def digest(json) @@ -115,10 +128,10 @@ def age(json) mtime = json['mtime'] return '?' if mtime.nil? sec = Time.now - Time.parse(mtime) if sec < 60 - "#{sec}s" + "#{sec.round(2)}s" elsif sec < 60 * 60 "#{(sec / 60).round}m" else "#{(sec / 3600).round}h" end