Class: StellarCoreCommander::Process

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

Direct Known Subclasses

DockerProcess, LocalProcess

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(working_dir, base_port, identity, opts) ⇒ Process

Returns a new instance of Process.



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

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

  @server = Faraday.new(url: "http://#{http_host}:#{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

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
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

#close_ledgerObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/stellar_core_commander/process.rb', line 50

def close_ledger
  prev_ledger = latest_ledger
  next_ledger = prev_ledger + 1

  server.get("manualclose")

  Timeout.timeout(close_timeout) 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

#close_timeoutObject



119
120
121
# File 'lib/stellar_core_commander/process.rb', line 119

def close_timeout
  5.0
end

#http_hostObject



114
115
116
# File 'lib/stellar_core_commander/process.rb', line 114

def http_host
  "127.0.0.1"
end

#http_portObject



76
77
78
# File 'lib/stellar_core_commander/process.rb', line 76

def http_port
  base_port
end

#latest_ledgerObject



103
104
105
# File 'lib/stellar_core_commander/process.rb', line 103

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

#peer_portObject



81
82
83
# File 'lib/stellar_core_commander/process.rb', line 81

def peer_port
  base_port + 1
end

#required_portsObject



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

def required_ports
  2
end

#rm_working_dirObject



28
29
30
# File 'lib/stellar_core_commander/process.rb', line 28

def rm_working_dir
  FileUtils.rm_rf working_dir
end

#sequence_for(account) ⇒ Object



97
98
99
100
# File 'lib/stellar_core_commander/process.rb', line 97

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

#submit_transaction(envelope_hex) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/stellar_core_commander/process.rb', line 86

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

  if body["status"] == "ERROR"
    raise "transaction failed: #{body.inspect}"
  end

end

#transaction_result(hex_hash) ⇒ Object



108
109
110
111
# File 'lib/stellar_core_commander/process.rb', line 108

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

#wait_for_readyObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/stellar_core_commander/process.rb', line 33

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