Class: Zold::Node

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

Overview

NODE command

Defined Under Namespace

Classes: NohupLog, WebrickLog

Instance Method Summary collapse

Constructor Details

#initialize(wallets:, remotes:, copies:, log: Log::Quiet.new) ⇒ Node

Returns a new instance of Node.


47
48
49
50
51
52
# File 'lib/zold/commands/node.rb', line 47

def initialize(wallets:, remotes:, copies:, log: Log::Quiet.new)
  @wallets = wallets
  @remotes = remotes
  @copies = copies
  @log = log
end

Instance Method Details

#run(args = []) ⇒ Object


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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/zold/commands/node.rb', line 54

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',
      required: true
    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 (will attempt to auto-detect it, if not specified)'
    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 '--ignore-score-weakness',
      'Ignore score weakness of incoming requests and register those nodes anyway',
      default: false
    o.boolean '--nohup',
      'Run it in background, rebooting when a higher version is available in the network',
      default: false
    o.string '--nohup-command',
      'The command to run in server "nohup" mode (default: "gem install zold")',
      default: 'gem install zold'
    o.string '--nohup-log',
      'The file to log output into (default: zold.log)',
      default: 'zold.log'
    o.string '--save-pid',
      'The file to save process ID into right after start (only in NOHUP mode)'
    o.bool '--never-reboot',
      'Don\'t reboot when a new version shows up in the network',
      default: false
    o.bool '--routine-immediately',
      'Run all routines immediately, without waiting between executions (for testing mostly)',
      default: false
    o.string '--expose-version',
      "The version of the software to expose in JSON (default: #{VERSION})",
      default: VERSION
    o.string '--bonus-wallet',
      'The ID of the wallet to regularly send bonuses from (for nodes online)'
    o.integer '--bonus-time',
      'The amount of minutes to wait between bonus awards (default: 60)',
      default: 60
    o.string '--bonus-amount',
      'The amount of ZLD to pay to each remote as a bonus',
      default: '1'
    o.string '--private-key',
      'The location of RSA private key (default: ~/.ssh/id_rsa)',
      default: '~/.ssh/id_rsa'
    o.string '--network',
      "The name of the network (default: #{Wallet::MAIN_NETWORK})",
      require: true,
      default: Wallet::MAIN_NETWORK
    o.bool '--help', 'Print instructions'
  end
  if opts.help?
    @log.info(opts.to_s)
    return
  end
  raise '--invoice is mandatory' unless opts['invoice']
  if opts[:nohup]
    pid = nohup(opts)
    File.write(opts['save-pid'], pid) if opts['save-pid']
    @log.debug("Process ID #{pid} saved into \"#{opts['save-pid']}\"")
    @log.info(pid)
    return
  end
  Front.set(:log, @log)
  Front.set(:version, opts['expose-version'])
  Front.set(:logging, @log.debug?)
  Front.set(:home, Dir.pwd)
  @log.info("Home directory: #{Dir.pwd}")
  @log.info("Ruby version: #{RUBY_VERSION}")
  @log.info("Zold gem version: #{Zold::VERSION}")
  @log.info("Network ID: #{opts['network']}")
  host = opts[:host] || ip
  address = "#{host}:#{opts[:port]}".downcase
  @log.info("Node location: #{address}")
  Front.set(
    :server_settings,
    Logger: WebrickLog.new(@log),
    AccessLog: []
  )
  if opts['standalone']
    @remotes = Remotes::Empty.new
    @log.debug('Running in standalone mode! (will never talk to other remotes)')
  end
  Front.set(:ignore_score_weakness, opts['ignore-score-weakness'])
  Front.set(:network, opts['network'])
  Front.set(:wallets, @wallets)
  Front.set(:remotes, @remotes)
  Front.set(:copies, @copies)
  Front.set(:address, address)
  Front.set(:root, Dir.pwd)
  Front.set(:port, opts['bind-port'])
  Front.set(:reboot, !opts['never-reboot'])
  invoice = opts[:invoice]
  unless invoice.include?('@')
    if @wallets.find(Id.new(invoice)).exists?
      @log.info("Wallet #{invoice} already exists locally, won't pull")
    else
      @log.info("The wallet #{invoice} is not available locally, will pull now...")
      require_relative 'pull'
      Pull.new(wallets: @wallets, remotes: @remotes, copies: @copies, log: @log).run(['pull', invoice])
    end
    require_relative 'invoice'
    invoice = Invoice.new(wallets: @wallets, log: @log).run(['invoice', invoice])
  end
  SafeEntrance.new(
    AsyncEntrance.new(
      SpreadEntrance.new(
        Entrance.new(@wallets, @remotes, @copies, address, log: @log),
        @wallets, @remotes, address,
        log: @log,
        ignore_score_weakeness: opts['ignore-score-weakness']
      ), log: @log
    ), network: opts['network']
  ).start do |entrance|
    Front.set(:entrance, entrance)
    Farm.new(invoice, File.join(Dir.pwd, 'farm'), log: @log)
      .start(host, opts[:port], threads: opts[:threads], strength: opts[:strength]) do |farm|
      Front.set(:farm, farm)
      metronome(farm, entrance, opts).start do |metronome|
        Front.set(:metronome, metronome)
        @log.info("Starting up the web front at http://#{host}:#{opts[:port]}...")
        Front.run!
        @log.info("The web front stopped at http://#{host}:#{opts[:port]}")
      end
    end
  end
  @log.info("The node #{host}:#{opts[:port]} is shut down, thanks for helping Zold network!")
end