Module: CardanoUp::Launcher

Defined in:
lib/cardano-up/launcher.rb

Overview

Start/stop cardano-node and cardano-wallet on your system.

For Linux and MacOS it will attempt to start separate screen sessions for wallet and node respectively, therefore ‘screen’ tool is required on your system. If screen is not present you can install it using your package manager for instance:

# MacOS:
brew install screen
# Linux:
sudo apt-get install screen

For Windows it will attepmt to install cardano-node and cardano-wallet as Windows services using ‘nssm’ tool. Nssm can be installed via choco package manager:

choco install nssm

Class Method Summary collapse

Class Method Details

.node_down(env, session_name = '0') ⇒ Object

Raises:

  • CardanoUp::EnvNotSupportedError

  • CardanoUp::NoScreenError



233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/cardano-up/launcher.rb', line 233

def self.node_down(env, session_name = '0')
  raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include? env

  raise CardanoUp::NoScreenError if !CardanoUp::Utils.win? && !CardanoUp::Utils.screen?

  if CardanoUp::Utils.win?
    CardanoUp::Utils.cmd "nssm stop cardano-node-#{env}-#{session_name}"
    CardanoUp::Utils.cmd "nssm remove cardano-node-#{env}-#{session_name} confirm"
  else
    CardanoUp::Utils.cmd "screen -S NODE_#{env}_#{session_name} -X at '0' stuff '^C'"
    CardanoUp::Utils.cmd "screen -XS NODE_#{env}_#{session_name} quit"
  end
  CardanoUp::Session.remove(session_name, { network: env, service: 'node' })
end

.node_up(configuration) ⇒ Object

Parameters:

  • configuration (Hash)

    output of setup

Raises:

  • CardanoUp::SessionHasNodeError

  • CardanoUp::SessionHasWalletError

  • CardanoUp::NoScreenError



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
# File 'lib/cardano-up/launcher.rb', line 77

def self.node_up(configuration)
  env = configuration[:env]
  bin_dir = configuration[:bin_dir]
  config_dir = configuration[:config_dir]
  log_dir = configuration[:log_dir]
  node_db_dir = configuration[:node_db_dir]
  node_socket = configuration[:node_socket]
  session_name = configuration[:session_name]

  raise CardanoUp::NoScreenError if !CardanoUp::Utils.win? && !CardanoUp::Utils.screen?

  exe = CardanoUp::Utils.win? ? '.exe' : ''
  cardano_node = "#{File.join(bin_dir, 'cardano-node')}#{exe}"
  version = CardanoUp::Utils.cmd "#{cardano_node} version"
  node_cmd = ["#{cardano_node} run",
              "--config #{File.join(config_dir, 'config.json')}",
              "--topology #{File.join(config_dir, 'topology.json')}",
              "--database-path #{node_db_dir}",
              "--socket-path #{node_socket}"].join(' ')
  node_service = if CardanoUp::Utils.win?
                   "cardano-node-#{env}-#{session_name}"
                 else
                   "NODE_#{env}_#{session_name}"
                 end
  logfile = File.join(log_dir, 'node.log')
  service_details = {
    network: env,
    node: {
      service: node_service,
      version: version,
      log: logfile,
      db_dir: node_db_dir,
      socket_path: node_socket,
      protocol_magic: get_protocol_magic(config_dir),
      bin: node_cmd.split.first,
      cmd: node_cmd
    }
  }
  CardanoUp::Session.create_or_update(session_name, service_details)

  # Turn off p2p in config.json and topology.json
  config_json = CardanoUp::Utils.from_json("#{config_dir}/config.json")
  config_json[:EnableP2P] = false
  CardanoUp::Utils.to_json("#{config_dir}/config.json", config_json)
  addr = env == 'mainnet' ? 'relays-new.cardano-mainnet.iohk.io' : "#{env}-node.world.dev.cardano.org"
  port = case env
         when 'mainnet'
           3001
         when 'preprod'
           30_000
         when 'preview'
           30_002
         else
           30_000
         end
  topology_json = {
    Producers: [
      {
        addr: addr,
        port: port,
        valency: 2
      }
    ]
  }
  CardanoUp::Utils.to_json("#{config_dir}/topology.json", topology_json)

  if CardanoUp::Utils.win?
    # create cardano-node.bat file
    File.write("#{bin_dir}/cardano-node.bat", node_cmd)
    install_node = "nssm install #{node_service} #{bin_dir}/cardano-node.bat"
    log_stdout_node = "nssm set #{node_service} AppStdout #{logfile}"
    log_stderr_node = "nssm set #{node_service} AppStderr #{logfile}"
    start_node = "nssm start #{node_service}"

    CardanoUp::Utils.cmd install_node
    CardanoUp::Utils.cmd log_stdout_node
    CardanoUp::Utils.cmd log_stderr_node
    CardanoUp::Utils.cmd start_node
  else
    screen_cmd = "screen -dmS #{node_service} -L -Logfile #{logfile} #{node_cmd}"
    CardanoUp::Utils.cmd screen_cmd
  end
  service_details
end

.setup(opt = { env: 'mainnet', wallet_port: '8090', session_name: '0' }) ⇒ Object

Create common set of variables for getting node and wallet up

Parameters:

  • env (Hash)

    provide env and wallet_port, e.g. { env: ‘mainnet’, wallet_port: ‘8090’, session_name: ‘0’ }

Raises:

  • CardanoUp::EnvNotSupportedError

  • CardanoUp::WalletPortError



27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/cardano-up/launcher.rb', line 27

def self.setup(opt = { env: 'mainnet', wallet_port: '8090', session_name: '0' })
  env = opt[:env]
  raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include? env

  wallet_port = opt[:wallet_port]
  raise CardanoUp::WalletPortError if wallet_port.nil? || wallet_port.empty?

  session_name = opt[:session_name] || '0'

   = env == 'mainnet' ? CardanoUp::MAINNET_TOKEN_SERVER : CardanoUp::TESTNET_TOKEN_SERVER

  CardanoUp.configure_default unless CardanoUp.configured?
  configs = CardanoUp.config
  bin_dir = configs[:bin_dir]
  config_dir = File.join(configs[:config_dir], env)
  log_dir = File.join(configs[:log_dir], session_name, env)
  state_dir = File.join(configs[:state_dir], session_name, env)
  wallet_db_dir = File.join(state_dir, 'wallet-db')
  node_db_dir = File.join(state_dir, 'node-db')
  [bin_dir, config_dir, log_dir, state_dir, wallet_db_dir, node_db_dir].each do |dir|
    FileUtils.mkdir_p(dir)
  end

  node_socket = if CardanoUp::Utils.win?
                  "\\\\.\\pipe\\cardano-node-#{env}-#{session_name}"
                else
                  File.join(state_dir, 'node.socket')
                end
  network = env == 'mainnet' ? '--mainnet' : "--testnet #{File.join(config_dir, 'byron-genesis.json')}"

  {
    env: env,
    wallet_port: wallet_port,
    token_metadata_server: ,
    bin_dir: bin_dir,
    config_dir: config_dir,
    log_dir: log_dir,
    state_dir: state_dir,
    wallet_db_dir: wallet_db_dir,
    node_db_dir: node_db_dir,
    node_socket: node_socket,
    network: network,
    session_name: session_name
  }
end

.wallet_down(env, session_name = '0') ⇒ Object

Raises:

  • CardanoUp::EnvNotSupportedError

  • CardanoUp::NoScreenError



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/cardano-up/launcher.rb', line 250

def self.wallet_down(env, session_name = '0')
  raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include? env

  raise CardanoUp::NoScreenError if !CardanoUp::Utils.win? && !CardanoUp::Utils.screen?

  if CardanoUp::Utils.win?
    CardanoUp::Utils.cmd "nssm stop cardano-wallet-#{env}-#{session_name}"
    CardanoUp::Utils.cmd "nssm remove cardano-wallet-#{env}-#{session_name} confirm"
  else
    CardanoUp::Utils.cmd "screen -S WALLET_#{env}_#{session_name} -X at '0' stuff '^C'"
    CardanoUp::Utils.cmd "screen -XS WALLET_#{env}_#{session_name} quit"
  end
  CardanoUp::Session.remove(session_name, { network: env, service: 'wallet' })
end

.wallet_up(configuration) ⇒ Object

Parameters:

  • configuration (Hash)

    output of setup

Raises:

  • CardanoUp::NoScreenError

  • CardanoUp::WalletPortUsedError



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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/cardano-up/launcher.rb', line 165

def self.wallet_up(configuration)
  if CardanoUp::Utils.port_used?(configuration[:wallet_port].to_i)
    raise CardanoUp::WalletPortUsedError, configuration[:wallet_port]
  end

  raise CardanoUp::NoScreenError if !CardanoUp::Utils.win? && !CardanoUp::Utils.screen?

  env = configuration[:env]
  wallet_port = configuration[:wallet_port]
   = configuration[:token_metadata_server]
  bin_dir = configuration[:bin_dir]
  log_dir = configuration[:log_dir]
  wallet_db_dir = configuration[:wallet_db_dir]
  node_socket = configuration[:node_socket]
  network = configuration[:network]
  session_name = configuration[:session_name]

  exe = CardanoUp::Utils.win? ? '.exe' : ''
  cardano_wallet = "#{File.join(bin_dir, 'cardano-wallet')}#{exe}"
  wallet_cmd = ["#{cardano_wallet} serve",
                "--port #{wallet_port}",
                "--node-socket #{node_socket}",
                network.to_s,
                "--database #{wallet_db_dir}",
                "--token-metadata-server #{}"].join(' ')
  version = CardanoUp::Utils.cmd "#{bin_dir}/cardano-wallet#{exe} version"
  wallet_service = if CardanoUp::Utils.win?
                     "cardano-wallet-#{env}-#{session_name}"
                   else
                     "WALLET_#{env}_#{session_name}"
                   end
  logfile = File.join(log_dir, 'wallet.log')
  service_details = {
    network: env,
    wallet: {
      service: wallet_service,
      version: version,
      log: logfile,
      db_dir: wallet_db_dir,
      port: wallet_port.to_i,
      url: "http://localhost:#{wallet_port}/v2",
      bin: wallet_cmd.split.first,
      cmd: wallet_cmd
    }
  }

  CardanoUp::Session.create_or_update(session_name, service_details)
  if CardanoUp::Utils.win?
    # create cardano-wallet.bat file
    File.write("#{bin_dir}/cardano-wallet.bat", wallet_cmd)
    install_wallet = "nssm install #{wallet_service} #{bin_dir}/cardano-wallet.bat"
    log_stdout_wallet = "nssm set #{wallet_service} AppStdout #{logfile}"
    log_stderr_wallet = "nssm set #{wallet_service} AppStderr #{logfile}"
    start_wallet = "nssm start #{wallet_service}"

    CardanoUp::Utils.cmd install_wallet
    CardanoUp::Utils.cmd log_stdout_wallet
    CardanoUp::Utils.cmd log_stderr_wallet
    CardanoUp::Utils.cmd start_wallet
  else
    CardanoUp::Utils.cmd "screen -dmS #{wallet_service} -L -Logfile #{logfile} #{wallet_cmd}"
  end

  service_details
end