lib/zold/node/front.rb in zold-0.16.18 vs lib/zold/node/front.rb in zold-0.16.19
- old
+ new
@@ -53,77 +53,77 @@
set :suppress_messages, true
set :start, Time.now
set :lock, false
set :show_exceptions, false
set :server, :thin
- set :log, nil? # to be injected at node.rb
- set :trace, nil? # to be injected at node.rb
- set :halt, '' # to be injected at node.rb
+ set :opts, nil # to be injected at node.rb
+ set :log, nil # to be injected at node.rb
+ set :trace, nil # to be injected at node.rb
set :dump_errors, false # to be injected at node.rb
- set :version, VERSION # to be injected at node.rb
set :protocol, PROTOCOL # to be injected at node.rb
- set :ignore_score_weakness, false # to be injected at node.rb
- set :reboot, false # to be injected at node.rb
set :nohup_log, false # to be injected at node.rb
- set :home, nil? # to be injected at node.rb
+ set :home, nil # to be injected at node.rb
set :logging, true # to be injected at node.rb
- set :logger, nil? # to be injected at node.rb
- set :address, nil? # to be injected at node.rb
- set :farm, nil? # to be injected at node.rb
- set :metronome, nil? # to be injected at node.rb
- set :entrance, nil? # to be injected at node.rb
- set :network, 'test' # to be injected at node.rb
- set :wallets, nil? # to be injected at node.rb
- set :remotes, nil? # to be injected at node.rb
- set :copies, nil? # to be injected at node.rb
- set :node_alias, nil? # to be injected at node.rb
+ set :logger, nil # to be injected at node.rb
+ set :address, nil # to be injected at node.rb
+ set :farm, nil # to be injected at node.rb
+ set :metronome, nil # to be injected at node.rb
+ set :entrance, nil # to be injected at node.rb
+ set :wallets, nil # to be injected at node.rb
+ set :remotes, nil # to be injected at node.rb
+ set :copies, nil # to be injected at node.rb
+ set :node_alias, nil # to be injected at node.rb
set :zache, Zache.new
end
use Rack::Deflater
before do
Thread.current.thread_variable_set(:uri, request.url)
Thread.current.thread_variable_set(:ip, request.ip)
@start = Time.now
- if !settings.halt.empty? && params[:halt] && params[:halt] == settings.halt
+ if !settings.opts['halt-code'].empty? && params[:halt] && params[:halt] == settings.opts['halt-code']
settings.log.error('Halt signal received, shutting the front end down...')
Front.stop!
end
check_header(Http::NETWORK_HEADER) do |header|
- if header != settings.network
+ if header != settings.opts['network']
error(400, "Network name mismatch at #{request.url}, #{request.ip} is in '#{header}', \
-while #{settings.address} is in '#{settings.network}'")
+while #{settings.address} is in '#{settings.opts['network']}'")
end
end
check_header(Http::PROTOCOL_HEADER) do |header|
if header != settings.protocol.to_s
error(400, "Protocol mismatch, you are in '#{header}', we are in '#{settings.protocol}'")
end
end
check_header(Http::SCORE_HEADER) do |header|
- settings.log.debug("#{request.url}: we are in standalone mode, won't update remotes") if all_remotes.empty?
- s = Score.parse_text(header)
- error(400, 'The score is invalid') unless s.valid?
- error(400, 'The score is weak') if s.strength < Score::STRENGTH && !settings.ignore_score_weakness
- if settings.address == "#{s.host}:#{s.port}" && !settings.ignore_score_weakness
- error(400, 'Self-requests are prohibited')
+ if settings.opts['standalone']
+ settings.log.debug("#{request.url}: we are in standalone mode, won't update remotes")
+ else
+ s = Score.parse_text(header)
+ error(400, 'The score is invalid') unless s.valid?
+ error(400, 'The score is weak') if s.strength < Score::STRENGTH && !settings.opts['ignore-score-weakness']
+ if settings.address == "#{s.host}:#{s.port}" && !settings.opts['ignore-score-weakness']
+ error(400, 'Self-requests are prohibited')
+ end
+ require_relative '../commands/remote'
+ begin
+ Remote.new(remotes: settings.remotes, log: settings.log).run(
+ ['remote', 'add', s.host, s.port.to_s, "--network=#{settings.opts['network']}", '--ignore-if-exists']
+ )
+ rescue StandardError => e
+ error(400, e.message)
+ end
end
- require_relative '../commands/remote'
- cmd = Remote.new(remotes: settings.remotes, log: settings.log)
- begin
- cmd.run(['remote', 'add', s.host, s.port.to_s, "--network=#{settings.network}"])
- rescue StandardError => e
- error(400, e.message)
- end
end
end
# @todo #357:30min Test that the headers are being set correctly.
# Currently there are no tests at all that would verify the headers.
after do
headers['Cache-Control'] = 'no-cache'
- headers['X-Zold-Version'] = settings.version
+ headers['X-Zold-Version'] = settings.opts['expose-version']
headers[Http::PROTOCOL_HEADER] = settings.protocol.to_s
headers['Access-Control-Allow-Origin'] = '*'
headers[Http::SCORE_HEADER] = score.reduced(16).to_s
headers['X-Zold-Thread'] = Thread.current.object_id.to_s
unless @start.nil?
@@ -140,11 +140,11 @@
'User-agent: *'
end
get '/version' do
content_type('text/plain')
- settings.version
+ settings.opts['expose-version']
end
get '/protocol' do
content_type('text/plain')
settings.protocol.to_s
@@ -184,13 +184,13 @@
end
get '/' do
content_type('application/json')
JSON.pretty_generate(
- version: settings.version,
+ version: settings.opts['expose-version'],
alias: settings.node_alias,
- network: settings.network,
+ network: settings.opts['network'],
protocol: settings.protocol,
score: score.to_h,
pid: Process.pid,
processes: processes_count,
cpus: settings.zache.get(:cpus) do
@@ -216,16 +216,16 @@
home: 'https://www.zold.io'
)
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
copy_of(id) do |wallet|
content_type('application/json')
JSON.pretty_generate(
- version: settings.version,
+ version: settings.opts['expose-version'],
alias: settings.node_alias,
protocol: settings.protocol,
id: wallet.id.to_s,
score: score.to_h,
wallets: total_wallets,
@@ -238,16 +238,16 @@
)
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16}).json} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
copy_of(id) do |wallet|
content_type('application/json')
JSON.pretty_generate(
- version: settings.version,
+ version: settings.opts['expose-version'],
alias: settings.node_alias,
protocol: settings.protocol,
id: wallet.id.to_s,
score: score.to_h,
wallets: total_wallets,
@@ -259,47 +259,47 @@
)
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/balance} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
copy_of(id) do |wallet|
content_type 'text/plain'
wallet.balance.to_i.to_s
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/key} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
copy_of(id) do |wallet|
content_type 'text/plain'
wallet.key.to_pub
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/mtime} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
copy_of(id) do |wallet|
content_type 'text/plain'
wallet.mtime.utc.iso8601.to_s
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/digest} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
copy_of(id) do |wallet|
content_type 'text/plain'
wallet.digest
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})\.txt} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
copy_of(id) do |wallet|
content_type 'text/plain'
[
wallet.network,
@@ -319,20 +319,20 @@
].join("\n")
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})\.bin} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
copy_of(id) do |wallet|
content_type 'text/plain'
IO.read(wallet.path)
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/copies} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
copy_of(id) do
content_type 'text/plain'
copies = Copies.new(File.join(settings.copies, id))
copies.load.map do |c|
@@ -346,11 +346,11 @@
end.join("\n")
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/copy/(?<name>[0-9]+)} do
- error(404, 'FETCH is disabled with --disable-fetch') if settings.disable_fetch
+ error(404, 'FETCH is disabled with --disable-fetch') if settings.opts['disable-fetch']
id = Id.new(params[:id])
name = params[:name]
copy_of(id) do
copy = Copies.new(File.join(settings.copies, id)).all.find { |c| c[:name] == name }
error 404 if copy.nil?
@@ -358,29 +358,29 @@
IO.read(copy[:path])
end
end
put %r{/wallet/(?<id>[A-Fa-f0-9]{16})/?} do
- error(404, 'PUSH is disabled with --disable-push') if settings.disable_fetch
+ error(404, 'PUSH is disabled with --disable-push') if settings.opts['disable-fetch']
request.body.rewind
modified = settings.entrance.push(Id.new(params[:id]), request.body.read.to_s)
if modified.empty?
status(304)
return
end
JSON.pretty_generate(
- version: settings.version,
+ version: settings.opts['expose-version'],
alias: settings.node_alias,
score: score.to_h,
wallets: total_wallets
)
end
get '/remotes' do
content_type('application/json')
JSON.pretty_generate(
- version: settings.version,
+ version: settings.opts['expose-version'],
alias: settings.node_alias,
score: score.to_h,
all: all_remotes,
mtime: settings.remotes.mtime.utc.iso8601
)
@@ -448,31 +448,31 @@
# @todo #513:30min This method is temporarily disabled since it
# takes a lot of time (when the amount of wallets is big, like 40K). However,
# we must find a way to count them somehow faster.
def total_wallets
- return 256 if settings.network == Wallet::MAIN_NETWORK
+ return 256 if settings.opts['network'] == Wallet::MAIN_NETWORK
settings.wallets.all.count
end
def all_remotes
- settings.zache.get(:remotes, lifetime: settings.network == Wallet::MAIN_NETWORK ? 60 : 0) do
+ settings.zache.get(:remotes, lifetime: settings.opts['network'] == Wallet::MAIN_NETWORK ? 60 : 0) do
settings.remotes.all
end
end
def processes_count
- settings.zache.get(:processes, lifetime: settings.network == Wallet::MAIN_NETWORK ? 60 : 0) do
+ settings.zache.get(:processes, lifetime: settings.opts['network'] == Wallet::MAIN_NETWORK ? 60 : 0) do
processes.count
end
end
def processes
POSIX::Spawn::Child.new('ps', 'ax').out.split("\n").select { |t| t.include?('zold') }
end
def score
- settings.zache.get(:score, lifetime: settings.network == Wallet::MAIN_NETWORK ? 60 : 0) do
+ settings.zache.get(:score, lifetime: settings.opts['network'] == Wallet::MAIN_NETWORK ? 60 : 0) do
b = settings.farm.best
raise 'Score is empty, there is something wrong with the Farm!' if b.empty?
b[0]
end
end