Class: StellarCoreCommander::Transactor

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

Overview

A transactor plays transactions against a stellar-core test node.

Defined Under Namespace

Classes: FailedTransaction

Instance Method Summary collapse

Constructor Details

#initialize(process) ⇒ Transactor

Returns a new instance of Transactor.



15
16
17
18
19
20
21
# File 'lib/stellar_core_commander/transactor.rb', line 15

def initialize(process)
  @process    = process
  @named      = {}.with_indifferent_access
  @unverified = []
  @operation_builder = OperationBuilder.new(self)
   :master, Stellar::KeyPair.from_raw_seed("allmylifemyhearthasbeensearching")
end

Instance Method Details

#account(name, keypair = Stellar::KeyPair.random) ⇒ Object

Registered an account for this scenario. Future calls may refer to the name provided.

Parameters:

  • name (Symbol)

    the name to register the keypair at

  • keypair=Stellar::KeyPair.random (Stellar::KeyPair)

    the keypair to use for this account



45
46
47
48
49
50
51
# File 'lib/stellar_core_commander/transactor.rb', line 45

def (name, keypair=Stellar::KeyPair.random)
  unless keypair.is_a?(Stellar::KeyPair)
    raise ArgumentError, "`#{keypair.class.name}` is not `Stellar::KeyPair`"
  end

  add_named name, keypair
end

#allow_trust(*args) ⇒ Object



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

def allow_trust(*args)
  envelope = @operation_builder.allow_trust(*args)
  submit_transaction envelope
end

#change_trust(*args) ⇒ Object



74
75
76
77
# File 'lib/stellar_core_commander/transactor.rb', line 74

def change_trust(*args)
  envelope = @operation_builder.change_trust(*args)
  submit_transaction envelope
end

#close_ledgerObject

Triggers a ledger close. Any unvalidated transaction will be validated, which will trigger an error if any fail to be validated



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/stellar_core_commander/transactor.rb', line 112

def close_ledger
  @process.close_ledger

  @unverified.each do |eb|
    begin
      envelope, after_confirmation = *eb
      result = validate_transaction envelope
      after_confirmation.call(result) if after_confirmation
    rescue FailedTransaction
      $stderr.puts "Failed to validate tx: #{Convert.to_hex envelope.tx.hash}"
      $stderr.puts "failed result: #{result.to_xdr(:hex)}"
      exit 1
    end
  end

  # TODO: validate in-flight transactions
  @unverified.clear
end

#get_account(name) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/stellar_core_commander/transactor.rb', line 132

def (name)
  @named[name].tap do |found|
    unless found.is_a?(Stellar::KeyPair)
      raise ArgumentError, "#{name.inspect} is not account"
    end
  end
end

#next_sequence(account) ⇒ Object



141
142
143
144
145
146
# File 'lib/stellar_core_commander/transactor.rb', line 141

def next_sequence()
  base_sequence  = @process.sequence_for()
  inflight_count = @unverified.select{|e| e.first.tx. == .public_key}.length
  
  base_sequence + inflight_count + 1
end

#offer(*args) ⇒ Object



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

def offer(*args)
  envelope = @operation_builder.offer(*args)
  submit_transaction envelope
end

#payment(*args) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/stellar_core_commander/transactor.rb', line 56

def payment(*args)
  envelope = @operation_builder.payment(*args)

  submit_transaction envelope do |result|
    payment_result = result.result.results!.first.tr!.payment_result!
    raise FailedTransaction unless payment_result.code.value >= 0
  end
end

#require_trust_auth(*args) ⇒ Object



88
89
90
91
# File 'lib/stellar_core_commander/transactor.rb', line 88

def require_trust_auth(*args)
  envelope = @operation_builder.require_trust_auth(*args)
  submit_transaction envelope
end

#run_recipe(recipe_path) ⇒ Object

Runs the provided recipe against the process identified by @process

Parameters:

  • recipe_path (String)

    path to the recipe file



29
30
31
32
33
34
# File 'lib/stellar_core_commander/transactor.rb', line 29

def run_recipe(recipe_path)
  raise "stellar-core not running" unless @process.running? 

  recipe_content = IO.read(recipe_path)
  instance_eval recipe_content
end

#set_flags(*args) ⇒ Object



95
96
97
98
# File 'lib/stellar_core_commander/transactor.rb', line 95

def set_flags(*args)
  envelope = @operation_builder.set_flags(*args)
  submit_transaction envelope
end

#trust(*args) ⇒ Object



67
68
69
70
# File 'lib/stellar_core_commander/transactor.rb', line 67

def trust(*args)
  envelope = @operation_builder.trust(*args)
  submit_transaction envelope
end