Class: StellarCoreCommander::Process
- Inherits:
-
Object
- Object
- StellarCoreCommander::Process
- Includes:
- Contracts
- Defined in:
- lib/stellar_core_commander/process.rb
Instance Attribute Summary collapse
-
#base_port ⇒ Object
readonly
Returns the value of attribute base_port.
-
#identity ⇒ Object
readonly
Returns the value of attribute identity.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#wait ⇒ Object
readonly
Returns the value of attribute wait.
-
#working_dir ⇒ Object
readonly
Returns the value of attribute working_dir.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #close_ledger ⇒ Object
- #create_database ⇒ Object
- #database ⇒ Object
- #database_name ⇒ Object
- #drop_database ⇒ Object
- #dsn ⇒ Object
- #dump_database ⇒ Object
- #forcescp ⇒ Object
- #http_port ⇒ Object
-
#initialize(working_dir, base_port, identity) ⇒ Process
constructor
A new instance of Process.
- #initialize_database ⇒ Object
- #initialize_history ⇒ Object
- #latest_ledger ⇒ Object
- #peer_port ⇒ Object
- #rm_working_dir ⇒ Object
- #run ⇒ Object
- #running? ⇒ Boolean
- #sequence_for(account) ⇒ Object
- #setup ⇒ Object
- #shutdown(graceful = true) ⇒ Object
- #submit_transaction(envelope_hex) ⇒ Object
- #transaction_result(hex_hash) ⇒ Object
- #wait_for_ready ⇒ Object
- #write_config ⇒ Object
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_port ⇒ Object (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 |
#identity ⇒ Object (readonly)
Returns the value of attribute identity.
8 9 10 |
# File 'lib/stellar_core_commander/process.rb', line 8 def identity @identity end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
9 10 11 |
# File 'lib/stellar_core_commander/process.rb', line 9 def pid @pid end |
#wait ⇒ Object (readonly)
Returns the value of attribute wait.
10 11 12 |
# File 'lib/stellar_core_commander/process.rb', line 10 def wait @wait end |
#working_dir ⇒ Object (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
#cleanup ⇒ Object
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_ledger ⇒ Object
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_database ⇒ Object
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 |
#database ⇒ Object
195 196 197 |
# File 'lib/stellar_core_commander/process.rb', line 195 def database @database ||= Sequel.postgres(database_name) end |
#database_name ⇒ Object
200 201 202 |
# File 'lib/stellar_core_commander/process.rb', line 200 def database_name "stellar_core_tmp_#{basename}" end |
#drop_database ⇒ Object
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 |
#dsn ⇒ Object
205 206 207 |
# File 'lib/stellar_core_commander/process.rb', line 205 def dsn "postgresql://dbname=#{database_name}" end |
#dump_database ⇒ Object
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 |
#forcescp ⇒ Object
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_port ⇒ Object
210 211 212 |
# File 'lib/stellar_core_commander/process.rb', line 210 def http_port @base_port end |
#initialize_database ⇒ Object
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_history ⇒ Object
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_ledger ⇒ Object
168 169 170 |
# File 'lib/stellar_core_commander/process.rb', line 168 def latest_ledger database[:ledgerheaders].max(:ledgerseq) end |
#peer_port ⇒ Object
215 216 217 |
# File 'lib/stellar_core_commander/process.rb', line 215 def peer_port @base_port + 1 end |
#rm_working_dir ⇒ Object
59 60 61 |
# File 'lib/stellar_core_commander/process.rb', line 59 def rm_working_dir FileUtils.rm_rf @working_dir end |
#run ⇒ Object
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
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(account) row = database[:accounts].where(:accountid => account.address).first row[:seqnum] end |
#setup ⇒ Object
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_ready ⇒ Object
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_config ⇒ Object
54 55 56 |
# File 'lib/stellar_core_commander/process.rb', line 54 def write_config IO.write("#{@working_dir}/stellar-core.cfg", config) end |