Class: StellarCoreCommander::Process

Inherits:
Object
  • Object
show all
Includes:
Contracts
Defined in:
lib/stellar_core_commander/process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(working_dir, base_port, identity) ⇒ Process

Returns a new instance of Process.



12
13
14
15
16
17
18
19
20
21
# File 'lib/stellar_core_commander/process.rb', line 12

def initialize(working_dir, base_port, identity)
  @working_dir = working_dir
  @base_port   = base_port
  @identity    = identity

  @server = Faraday.new(url: "http://127.0.0.1:#{http_port}") do |conn|
    conn.request :url_encoded
    conn.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#base_portObject (readonly)

Returns the value of attribute base_port.



7
8
9
# File 'lib/stellar_core_commander/process.rb', line 7

def base_port
  @base_port
end

#identityObject (readonly)

Returns the value of attribute identity.



8
9
10
# File 'lib/stellar_core_commander/process.rb', line 8

def identity
  @identity
end

#pidObject (readonly)

Returns the value of attribute pid.



9
10
11
# File 'lib/stellar_core_commander/process.rb', line 9

def pid
  @pid
end

#waitObject (readonly)

Returns the value of attribute wait.



10
11
12
# File 'lib/stellar_core_commander/process.rb', line 10

def wait
  @wait
end

#working_dirObject (readonly)

Returns the value of attribute working_dir.



6
7
8
# File 'lib/stellar_core_commander/process.rb', line 6

def working_dir
  @working_dir
end

Instance Method Details

#cleanupObject



179
180
181
182
183
184
# File 'lib/stellar_core_commander/process.rb', line 179

def cleanup
  database.disconnect
  shutdown
  drop_database
  rm_working_dir
end

#close_ledgerObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/stellar_core_commander/process.rb', line 120

def close_ledger
  prev_ledger = latest_ledger
  next_ledger = prev_ledger + 1

  @server.get("manualclose")

  Timeout.timeout(5.0) do 
    loop do
      current_ledger = latest_ledger

      case
      when current_ledger == next_ledger
        break
      when current_ledger > next_ledger
        raise "whoa! we jumped two ledgers, from #{prev_ledger} to #{current_ledger}"
      else
        $stderr.puts "waiting for ledger #{next_ledger}"
        sleep 0.5
      end
    end
  end

  true
end

#create_databaseObject



42
43
44
45
# File 'lib/stellar_core_commander/process.rb', line 42

def create_database
  run_cmd "createdb", [database_name]
  raise "Could not create db: #{database_name}" unless $?.success?
end

#databaseObject



195
196
197
# File 'lib/stellar_core_commander/process.rb', line 195

def database
  @database ||= Sequel.postgres(database_name)
end

#database_nameObject



200
201
202
# File 'lib/stellar_core_commander/process.rb', line 200

def database_name
  "stellar_core_tmp_#{basename}"
end

#drop_databaseObject



48
49
50
51
# File 'lib/stellar_core_commander/process.rb', line 48

def drop_database
  run_cmd "dropdb", [database_name]
  raise "Could not drop db: #{database_name}" unless $?.success?
end

#dsnObject



205
206
207
# File 'lib/stellar_core_commander/process.rb', line 205

def dsn
  "postgresql://dbname=#{database_name}"
end

#dump_databaseObject



187
188
189
190
191
# File 'lib/stellar_core_commander/process.rb', line 187

def dump_database
  Dir.chdir(@working_dir) do
    `pg_dump #{database_name} --clean --no-owner`
  end
end

#forcescpObject



24
25
26
27
# File 'lib/stellar_core_commander/process.rb', line 24

def forcescp
  run_cmd "./stellar-core", ["--forcescp"]
  raise "Could not set --forcescp" unless $?.success?
end

#http_portObject



210
211
212
# File 'lib/stellar_core_commander/process.rb', line 210

def http_port
  @base_port
end

#initialize_databaseObject



36
37
38
39
# File 'lib/stellar_core_commander/process.rb', line 36

def initialize_database
  run_cmd "./stellar-core", ["--newdb"]
  raise "Could not initialize db" unless $?.success?
end

#initialize_historyObject



30
31
32
33
# File 'lib/stellar_core_commander/process.rb', line 30

def initialize_history
  run_cmd "./stellar-core", ["--newhist", "main"]
  raise "Could not initialize history" unless $?.success?
end

#latest_ledgerObject



168
169
170
# File 'lib/stellar_core_commander/process.rb', line 168

def latest_ledger
  database[:ledgerheaders].max(:ledgerseq)
end

#peer_portObject



215
216
217
# File 'lib/stellar_core_commander/process.rb', line 215

def peer_port
  @base_port + 1
end

#rm_working_dirObject



59
60
61
# File 'lib/stellar_core_commander/process.rb', line 59

def rm_working_dir
  FileUtils.rm_rf @working_dir
end

#runObject



72
73
74
75
76
77
# File 'lib/stellar_core_commander/process.rb', line 72

def run
  raise "already running!" if running?

  forcescp
  launch_stellar_core
end

#running?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
# File 'lib/stellar_core_commander/process.rb', line 98

def running?
  return false unless @pid
  ::Process.kill 0, @pid
  true
rescue Errno::ESRCH
  false
end

#sequence_for(account) ⇒ Object



161
162
163
164
# File 'lib/stellar_core_commander/process.rb', line 161

def sequence_for()
  row = database[:accounts].where(:accountid => .address).first
  row[:seqnum]
end

#setupObject



64
65
66
67
68
69
# File 'lib/stellar_core_commander/process.rb', line 64

def setup
  write_config
  create_database
  initialize_history
  initialize_database
end

#shutdown(graceful = true) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/stellar_core_commander/process.rb', line 107

def shutdown(graceful=true)
  return true if !running?

  if graceful
    ::Process.kill "INT", @pid
  else
    ::Process.kill "KILL", @pid
  end

  @wait.value.success?
end

#submit_transaction(envelope_hex) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/stellar_core_commander/process.rb', line 146

def submit_transaction(envelope_hex)
  response = @server.get("tx", blob: envelope_hex)
  body = ActiveSupport::JSON.decode(response.body)

  unless body["wasReceived"] == true
    raise "transaction failed: #{body.inspect}"
  end

  hex_tr = body["result"]
  raw_tr = Convert.from_hex(hex_tr)
  Stellar::TransactionResult.from_xdr(raw_tr)
end

#transaction_result(hex_hash) ⇒ Object



173
174
175
176
# File 'lib/stellar_core_commander/process.rb', line 173

def transaction_result(hex_hash)
  row = database[:txhistory].where(txid:hex_hash).first
  row[:txresult]
end

#wait_for_readyObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stellar_core_commander/process.rb', line 81

def wait_for_ready
  loop do

    response = @server.get("/info") rescue false

    if response
      body = ActiveSupport::JSON.decode(response.body)

      break if body["info"]["state"] == "Synced!"
    end

    $stderr.puts "waiting until stellar-core is synced"
    sleep 1
  end
end

#write_configObject



54
55
56
# File 'lib/stellar_core_commander/process.rb', line 54

def write_config
  IO.write("#{@working_dir}/stellar-core.cfg", config)
end