Class: Zold::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/commands/node.rb

Overview

NODE command

Defined Under Namespace

Classes: WebrickLog

Instance Method Summary collapse

Constructor Details

#initialize(log: Log::Quiet.new) ⇒ Node

Returns a new instance of Node.



36
37
38
# File 'lib/zold/commands/node.rb', line 36

def initialize(log: Log::Quiet.new)
  @log = log
end

Instance Method Details

#run(args = []) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/zold/commands/node.rb', line 40

def run(args = [])
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = 'Usage: zold node [options]'
    o.string '--invoice',
      'The invoice you want to collect money to or the wallet ID'
    o.integer '--port',
      "TCP port to open for the Net (default: #{Remotes::PORT})",
      default: Remotes::PORT
    o.integer '--bind-port',
      "TCP port to listen on (default: #{Remotes::PORT})",
      default: Remotes::PORT
    o.string '--host', 'Host name (default: 127.0.0.1)',
      default: '127.0.0.1'
    o.string '--home', 'Home directory (default: .)',
      default: Dir.pwd
    o.integer '--strength',
      "The strength of the score (default: #{Score::STRENGTH})",
      default: Score::STRENGTH
    o.integer '--threads',
      'How many threads to use for scores finding (default: 4)',
      default: 4
    o.bool '--standalone',
      'Never communicate with other nodes (mostly for testing)',
      default: false
    o.bool '--never-reboot',
      'Don\'t reboot when a new version shows up in the network',
      default: false
    o.bool '--help', 'Print instructions'
  end
  if opts.help?
    @log.info(opts.to_s)
    return
  end
  raise '--invoice is mandatory' unless opts[:invoice]
  Zold::Front.set(:log, @log)
  Zold::Front.set(:logging, @log.debug?)
  FileUtils.mkdir_p(opts[:home])
  Zold::Front.set(:home, opts[:home])
  Zold::Front.set(
    :server_settings,
    Logger: WebrickLog.new(@log),
    AccessLog: []
  )
  if opts['standalone']
    remotes = Remotes::Empty.new
    @log.debug('Running in standalone mode!')
  else
    remotes = Remotes.new(File.join(opts[:home], 'zold-remotes'))
  end
  wallets = Wallets.new(File.join(opts[:home], 'zold-wallets'))
  Zold::Front.set(:wallets, wallets)
  Zold::Front.set(:remotes, remotes)
  copies = File.join(opts[:home], 'zold-copies')
  Zold::Front.set(:copies, copies)
  address = "#{opts[:host]}:#{opts[:port]}".downcase
  Zold::Front.set(:address, address)
  Zold::Front.set(
    :entrance, Entrance.new(wallets, remotes, copies, address, log: @log)
  )
  Zold::Front.set(:root, Dir.pwd)
  Zold::Front.set(:port, opts['bind-port'])
  Zold::Front.set(:reboot, !opts['never-rebootw'])
  invoice = opts[:invoice]
  unless invoice.include?('@')
    require_relative 'pull'
    Pull.new(wallets: wallets, remotes: remotes, copies: copies, log: @log).run(['pull', invoice])
    require_relative 'invoice'
    invoice = Invoice.new(wallets: wallets, log: @log).run(['invoice', invoice])
  end
  farm = Farm.new(invoice, log: @log)
  farm.start(
    opts[:host],
    opts[:port],
    threads: opts[:threads], strength: opts[:strength]
  )
  Zold::Front.set(:farm, farm)
  Thread.start do
    Zold::Remote.new(remotes: remotes, farm: farm, log: @log).run(%w[remote update]) unless best.nil?
  end
  @log.debug('Starting up the web front...')
  begin
    Zold::Front.run!
  ensure
    farm.stop
  end
end