Class: Beowulf::Transaction

Inherits:
Object
  • Object
show all
Includes:
ChainConfig, Type, Utils
Defined in:
lib/beowulf/transaction.rb

Constant Summary collapse

VALID_OPTIONS =
%w(
  wif private_key ref_block_num ref_block_prefix expiration
  chain use_condenser_namespace
).map(&:to_sym)

Constants included from ChainConfig

ChainConfig::EXPIRE_IN_SECS, ChainConfig::EXPIRE_IN_SECS_PROPOSAL, ChainConfig::NETWORKS_BEOWULF_ADDRESS_PREFIX, ChainConfig::NETWORKS_BEOWULF_BWF_ASSET, ChainConfig::NETWORKS_BEOWULF_CHAIN_ID, ChainConfig::NETWORKS_BEOWULF_DEFAULT_NODE, ChainConfig::NETWORKS_BEOWULF_VEST_ASSET, ChainConfig::NETWORKS_BEOWULF_W_ASSET, ChainConfig::NETWORK_CHAIN_IDS

Instance Method Summary collapse

Methods included from Utils

#debug, #error, #extract_signatures, #hexlify, #pakArr, #pakC, #pakHash, #pakI, #pakL!, #pakPubKey, #pakQ, #pakS, #pakStr, #pakc, #pakq, #paks, #send_log, #unhexlify, #varint, #warning

Constructor Details

#initialize(options = {}) ⇒ Transaction

Returns a new instance of Transaction.



17
18
19
20
21
22
23
24
25
26
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
72
73
74
75
76
77
78
79
80
# File 'lib/beowulf/transaction.rb', line 17

def initialize(options = {})
  #puts 'Transaction.initialize.options1', options.to_json
  options = options.dup
  options.each do |k, v|
    k = k.to_sym
    if VALID_OPTIONS.include?(k.to_sym)
      options.delete(k)
      send("#{k}=", v)
    end
  end
  #puts 'Transaction.initialize.options2', options.to_json

  @chain ||= :beowulf
  @chain = @chain.to_sym
  @chain_id = chain_id options[:chain_id]
  @url = options[:url] || url
  @operations = options[:operations] || []
  
  @self_logger = false
  @logger = if options[:logger].nil?
    @self_logger = true
    Beowulf.logger
  else
    options[:logger]
  end
  
  unless NETWORK_CHAIN_IDS.include? @chain_id
    warning "Unknown chain id: #{@chain_id}"
  end

  if !!wif && !!private_key
    raise TransactionError, "Do not pass both wif and private_key.  That's confusing."
  end

  if !!wif
    @private_key = Bitcoin::Key.from_base58 wif
  end

  @ref_block_num ||= nil
  @ref_block_prefix ||= nil
  @expiration ||= nil
  @extensions ||= []
  @created_time ||= Time.now.utc.to_i
  @immutable_expiration = !!@expiration

  options = options.merge(
    url: @url,
    chain: @chain,
    pool_size: 1,
    persist: false,
    reuse_ssl_sessions: false
  )

  @api = Api.new(options)
  @network_broadcast_api = NetworkBroadcastApi.new(options)
  
  @use_condenser_namespace = if options.keys.include? :use_condenser_namespace
    options[:use_condenser_namespace]
  else
    true
  end
  
  ObjectSpace.define_finalizer(self, self.class.finalize(@api, @network_broadcast_api, @self_logger, @logger))
end

Instance Method Details

#chain_id(chain_id = nil) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/beowulf/transaction.rb', line 82

def chain_id(chain_id = nil)
  return chain_id if !!chain_id

  case chain.to_s.downcase.to_sym
  when :beowulf then NETWORKS_BEOWULF_CHAIN_ID
  # when :test then NETWORKS_TEST_CHAIN_ID
  end
end

#extensionsObject



137
138
139
140
141
142
143
144
# File 'lib/beowulf/transaction.rb', line 137

def extensions
  @extensions = @extensions.map do |ex|
    case ex
    when Extension then ex
    else; Extension.new(ex)
    end
  end
end

#extensions=(extensions) ⇒ Object



146
147
148
# File 'lib/beowulf/transaction.rb', line 146

def extensions=(extensions)
  @extensions = extensions
end

#inspectObject



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/beowulf/transaction.rb', line 167

def inspect
  properties = %w(
    url ref_block_num ref_block_prefix expiration chain
    use_condenser_namespace immutable_expiration payload
  ).map do |prop|
    if !!(v = instance_variable_get("@#{prop}"))
      "@#{prop}=#{v}" 
    end
  end.compact.join(', ')
  
  "#<#{self.class.name} [#{properties}]>"
end

#operationsObject



124
125
126
127
128
129
130
131
# File 'lib/beowulf/transaction.rb', line 124

def operations
  @operations = @operations.map do |op|
    case op
    when Operation then op
    else; Operation.new(op)
    end
  end
end

#operations=(operations) ⇒ Object



133
134
135
# File 'lib/beowulf/transaction.rb', line 133

def operations=(operations)
  @operations = operations
end

#process(broadcast = false) ⇒ Object



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
# File 'lib/beowulf/transaction.rb', line 98

def process(broadcast = false)
  prepare

  if broadcast
    loop do
      response = broadcast_payload(payload)

      if !!response.error
        parser = ErrorParser.new(response)

        if parser.can_reprepare?
          debug "Error code: #{parser}, repreparing transaction ..."
          prepare
          redo
        end
      end

      return response
    end
  else
    self
  end
ensure
  shutdown
end

#shutdownObject



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/beowulf/transaction.rb', line 150

def shutdown
  @api.shutdown if !!@api
  @network_broadcast_api.shutdown if !!@network_broadcast_api

  if @self_logger
    if !!@logger && defined?(@logger.close)
      if defined?(@logger.closed?)
        @logger.close unless @logger.closed?
      end
    end
  end
end

#urlObject



91
92
93
94
95
96
# File 'lib/beowulf/transaction.rb', line 91

def url
  case chain.to_s.downcase.to_sym
  when :beowulf then NETWORKS_BEOWULF_DEFAULT_NODE
  # when :test then NETWORKS_TEST_DEFAULT_NODE
  end
end

#use_condenser_namespace?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/beowulf/transaction.rb', line 163

def use_condenser_namespace?
  !!@use_condenser_namespace
end