Class: Beowulf::Api

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/beowulf/api.rb

Overview

Beowulf::Api allows you to call remote methods to interact with the Beowulf blockchain. The ‘Api` class is a shortened name for `Beowulf::CondenserApi`.

Examples:

api = Beowulf::Api.new
response = api.get_dynamic_global_properties
virtual_supply = response.result.virtual_supply

… or …

api = Beowulf::Api.new
virtual_supply = api.get_dynamic_global_properties do |prop|
  prop.virtual_supply
end

If you need access to the ‘error` property, they can be accessed as follows:

api = Beowulf::Api.new
response = api.get_dynamic_global_properties
if response.result.nil?
  puts response.error
  exit
end

virtual_supply = response.result.virtual_supply

… or …

api = Beowulf::Api.new
virtual_supply = api.get_dynamic_global_properties do |prop, error|
  if prop.nil?
    puts error
    exis
  end

  prop.virtual_supply
end

List of remote methods:

get_version
get_block_header
get_block
get_config
get_dynamic_global_properties
get_supernode_schedule
get_hardfork_version
get_next_scheduled_hardfork
get_accounts

lookup_accounts

get_owner_history
get_transaction_hex
get_transaction
get_transaction_with_status
get_pending_transaction_count
get_required_signatures
get_potential_signatures
verify_authority
get_supernodes
get_supernode_by_accounts
get_supernodes_by_vote
lookup_supernode_accounts
get_supernode_count
get_active_supernodes

Constant Summary collapse

DEFAULT_BEOWULF_URL =
'https://testnet-bw.beowulfchain.com/rpc'
DEFAULT_BEOWULF_FAILOVER_URLS =
[
    DEFAULT_BEOWULF_URL
]
DEFAULT_RESTFUL_URL =
'https://testnet-bw.beowulfchain.com/rpc'
POST_HEADERS =
{
    'Content-Type' => 'application/json',
    'User-Agent' => Beowulf::AGENT_ID
}
HEALTH_URI =
'/health'

Class Method Summary collapse

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 = {}) ⇒ Api

Create a new instance of Beowulf::Api.

Examples:

api = Beowulf::Api.new(url: 'https://api.example.com')

Parameters:

  • options (::Hash) (defaults to: {})

    The attributes to initialize the Beowulf::Api with.

Options Hash (options):

  • :url (String)

    URL that points at a full node, like ‘testnet-bw.beowulfchain.com/rpc`. Default from DEFAULT_URL.

  • :failover_urls (::Array<String>)

    An array that contains one or more full nodes to fall back on. Default from DEFAULT_FAILOVER_URLS.

  • :logger (Logger)

    An instance of ‘Logger` to send debug messages to.

  • :recover_transactions_on_error (Boolean)

    Have Beowulf try to recover transactions that are accepted but could not be confirmed due to an error like network timeout. Default: ‘true`

  • :max_requests (Integer)

    Maximum number of requests on a connection before it is considered expired and automatically closed.

  • :pool_size (Integer)

    Maximum number of connections allowed.

  • :reuse_ssl_sessions (Boolean)

    Reuse a previously opened SSL session for a new connection. There’s a slight performance improvement by enabling this, but at the expense of reliability during long execution. Default false.

  • :persist (Boolean)

    Enable or disable Persistent HTTP. Using Persistent HTTP keeps the connection alive between API calls. Default: ‘true`



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/beowulf/api.rb', line 135

def initialize(options = {})
  @user = options[:user]
  @password = options[:password]
  @chain = options[:chain] || :beowulf
  @url = options[:url] || Api::default_url(@chain)
  @restful_url = options[:restful_url] || Api::default_restful_url(@chain)
  @preferred_url = @url.dup
  @failover_urls = options[:failover_urls]
  @debug = !!options[:debug]
  @max_requests = options[:max_requests] || 30
  @ssl_verify_mode = options[:ssl_verify_mode] || OpenSSL::SSL::VERIFY_PEER
  @ssl_version = options[:ssl_version]

  @self_logger = false
  @logger = if options[:logger].nil?
              @self_logger = true
              Beowulf.logger
            else
              options[:logger]
            end

  @self_hashie_logger = false
  @hashie_logger = if options[:hashie_logger].nil?
                     @self_hashie_logger = true
                     Logger.new(nil)
                   else
                     options[:hashie_logger]
                   end

  if @failover_urls.nil?
    @failover_urls = Api::default_failover_urls(@chain) - [@url]
  end

  @failover_urls = [@failover_urls].flatten.compact
  @preferred_failover_urls = @failover_urls.dup

  unless @hashie_logger.respond_to? :warn
    @hashie_logger = Logger.new(@hashie_logger)
  end

  @recover_transactions_on_error = if options.keys.include? :recover_transactions_on_error
                                     options[:recover_transactions_on_error]
                                   else
                                     true
                                   end

  @persist_error_count = 0
  @persist = if options.keys.include? :persist
               options[:persist]
             else
               true
             end

  @reuse_ssl_sessions = if options.keys.include? :reuse_ssl_sessions
                          options[:reuse_ssl_sessions]
                        else
                          true
                        end

  @use_condenser_namespace = if options.keys.include? :use_condenser_namespace
                               options[:use_condenser_namespace]
                             else
                               true
                             end

  if defined? Net::HTTP::Persistent::DEFAULT_POOL_SIZE
    @pool_size = options[:pool_size] || Net::HTTP::Persistent::DEFAULT_POOL_SIZE
  end

  Hashie.logger = @hashie_logger
  @method_names = nil
  @uri = nil
  @http_id = nil
  @http_memo = {}
  @api_options = options.dup.merge(chain: @chain)
  @api = nil
  @block_api = nil
  @backoff_at = nil
  @jussi_supported = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/beowulf/api.rb', line 312

def method_missing(m, *args, &block)
  super unless respond_to_missing?(m)

  current_rpc_id = rpc_id
  method_name = [api_name, m].join('.')
  response = nil
  options = if api_name == :condenser_api
              {
                  jsonrpc: "2.0",
                  method: method_name,
                  params: args,
                  id: current_rpc_id,
              }
            else
              rpc_args = if args.empty?
                           {}
                         else
                           args.first
                         end
              {
                  jsonrpc: "2.0",
                  method: method_name,
                  params: rpc_args,
                  id: current_rpc_id,
              }
            end

  tries = 0
  timestamp = Time.now.utc

  loop do
    tries += 1

    if tries > 5 && flappy? && !check_file_open?
      raise ApiError, 'PANIC: Out of file resources'
    end

    begin
      if tries > 1 && @recover_transactions_on_error && api_name == :network_broadcast_api
        signatures, exp = extract_signatures(options)

        if !!signatures && signatures.any?
          offset = [(exp - timestamp).abs, 30].min

          if !!(response = recover_transaction(signatures, current_rpc_id, timestamp - offset))
            response = Hashie::Mash.new(response)
          end
        end
      end

      if response.nil?
        response = request(options)

        response = if response.nil?
                     error "No response, retrying ...", method_name
                   elsif !response.kind_of? Net::HTTPSuccess
                     warning "Unexpected response (code: #{response.code}): #{response.inspect}, retrying ...", method_name, true
                   else
                     detect_jussi(response)

                     case response.code
                     when '200'
                       body = response.body
                       response = JSON[body]

                       if response['id'] != options[:id]
                         debug_payload(options, body) if ENV['DEBUG'] == 'true'

                         if !!response['id']
                           warning "Unexpected rpc_id (expected: #{options[:id]}, got: #{response['id']}), retrying ...", method_name, true
                         else
                           # The node has broken the jsonrpc spec.
                           warning "Node did not provide jsonrpc id (expected: #{options[:id]}, got: nothing), retrying ...", method_name, true
                         end

                         if response.keys.include?('error')
                           handle_error(response, options, method_name, tries)
                         end
                       elsif response.keys.include?('error')
                         handle_error(response, options, method_name, tries)
                       else
                         Hashie::Mash.new(response)
                       end
                     when '400' then warning 'Code 400: Bad Request, retrying ...', method_name, true
                     when '429' then warning 'Code 429: Too Many Requests, retrying ...', method_name, true
                     when '502' then warning 'Code 502: Bad Gateway, retrying ...', method_name, true
                     when '503' then warning 'Code 503: Service Unavailable, retrying ...', method_name, true
                     when '504' then warning 'Code 504: Gateway Timeout, retrying ...', method_name, true
                     else
                       warning "Unknown code #{response.code}, retrying ...", method_name, true
                       warning response
                     end
                   end
      end
    rescue Net::HTTP::Persistent::Error => e
      warning "Unable to perform request: #{e} :: #{!!e.cause ? "cause: #{e.cause.message}" : ''}, retrying ...", method_name, true
      if e.cause.class == Net::HTTPMethodNotAllowed
        warning 'Node upstream is misconfigured.'
        drop_current_failover_url method_name
      end

      @persist_error_count += 1
    rescue ConnectionPool::Error => e
      warning "Connection Pool Error (#{e.message}), retrying ...", method_name, true
    rescue Errno::ECONNREFUSED => e
      warning 'Connection refused, retrying ...', method_name, true
    rescue Errno::EADDRNOTAVAIL => e
      warning 'Node not available, retrying ...', method_name, true
    rescue Errno::ECONNRESET => e
      warning "Connection Reset (#{e.message}), retrying ...", method_name, true
    rescue Errno::EBUSY => e
      warning "Resource busy (#{e.message}), retrying ...", method_name, true
    rescue Errno::ENETDOWN => e
      warning "Network down (#{e.message}), retrying ...", method_name, true
    rescue Net::ReadTimeout => e
      warning 'Node read timeout, retrying ...', method_name, true
    rescue Net::OpenTimeout => e
      warning 'Node timeout, retrying ...', method_name, true
    rescue RangeError => e
      warning 'Range Error, retrying ...', method_name, true
    rescue OpenSSL::SSL::SSLError => e
      warning "SSL Error (#{e.message}), retrying ...", method_name, true
    rescue SocketError => e
      warning "Socket Error (#{e.message}), retrying ...", method_name, true
    rescue JSON::ParserError => e
      warning "JSON Parse Error (#{e.message}), retrying ...", method_name, true
      drop_current_failover_url method_name if tries > 5
      response = nil
    rescue ApiError => e
      warning "ApiError (#{e.message}), retrying ...", method_name, true
      # rescue => e
      #   warning "Unknown exception from request, retrying ...", method_name, true
      #   warning e
    end

    if !!response
      @persist_error_count = 0

      if !!block
        if api_name == :condenser_api
          return yield(response.result, response.error, response.id)
        else
          if defined?(response.result.size) && response.result.size == 0
            return yield(nil, response.error, response.id)
          elsif (defined?(response.result.size) && response.result.size == 1 && defined?(response.result.values))
            return yield(response.result.values.first, response.error, response.id)
          else
            return yield(response.result, response.error, response.id)
          end
        end
      else
        return response
      end
    end

    backoff
  end # loop
end

Class Method Details

.default_failover_urls(chain) ⇒ Object



113
114
115
116
117
118
# File 'lib/beowulf/api.rb', line 113

def self.default_failover_urls(chain)
  case chain.to_sym
  when :beowulf then DEFAULT_BEOWULF_FAILOVER_URLS
  else; raise ApiError, "Unsupported chain: #{chain}"
  end
end

.default_restful_url(chain) ⇒ Object



106
107
108
109
110
111
# File 'lib/beowulf/api.rb', line 106

def self.default_restful_url(chain)
  case chain.to_sym
  when :beowulf then DEFAULT_RESTFUL_URL
  else; raise ApiError, "Unsupported chain: #{chain}"
  end
end

.default_url(chain) ⇒ Object



99
100
101
102
103
104
# File 'lib/beowulf/api.rb', line 99

def self.default_url(chain)
  case chain.to_sym
  when :beowulf then DEFAULT_BEOWULF_URL
  else; raise ApiError, "Unsupported chain: #{chain}"
  end
end

Instance Method Details

#api_nameObject



302
303
304
# File 'lib/beowulf/api.rb', line 302

def api_name
  :condenser_api
end

#get_blocks(block_number, &block) ⇒ ::Array

Get a specific block or range of blocks.

Example:

api = Beowulf::Api.new
blocks = api.get_blocks(10..20)
transactions = blocks.flat_map(&:transactions)

… or …

api = Beowulf::Api.new
transactions = []
api.get_blocks(10..20) do |block|
  transactions += block.transactions
end

Parameters:

  • block_number (Fixnum || ::Array<Fixnum>)
  • block

    the block to execute for each result, optional.

Returns:

  • (::Array)


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/beowulf/api.rb', line 235

def get_blocks(block_number, &block)
  block_number = [*(block_number)].flatten

  if !!block
    block_number.each do |i|
      if use_condenser_namespace?
        yield api.get_block(i)
      else
        yield block_api.get_block(block_num: i).result, i
      end
    end
  else
    block_number.map do |i|
      if use_condenser_namespace?
        api.get_block(i)
      else
        block_api.get_block(block_num: i).result
      end
    end
  end
end

#inspectObject



471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/beowulf/api.rb', line 471

def inspect
  properties = %w(
    chain url backoff_at max_requests ssl_verify_mode ssl_version persist
    recover_transactions_on_error reuse_ssl_sessions pool_size
    use_condenser_namespace
  ).map do |prop|
    if !!(v = instance_variable_get("@#{prop}"))
      "@#{prop}=#{v}"
    end
  end.compact.join(', ')

  "#<#{self.class.name} [#{properties}]>"
end

#method_namesObject



292
293
294
295
296
297
298
299
# File 'lib/beowulf/api.rb', line 292

def method_names
  return @method_names if !!@method_names
  return CondenserApi::METHOD_NAMES if api_name == :condenser_api

  @method_names = Beowulf::Api.methods(api_name).map do |e|
    e['method'].to_sym
  end
end

#respond_to_missing?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/beowulf/api.rb', line 307

def respond_to_missing?(m, include_private = false)
  method_names.nil? ? false : method_names.include?(m.to_sym)
end

#shutdownObject

Stops the persistent http connections.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/beowulf/api.rb', line 259

def shutdown
  @uri = nil
  @http_id = nil
  @http_memo.each do |k|
    v = @http_memo.delete(k)
    if defined?(v.shutdown)
      debug "Shutting down instance #{k} (#{v})"
      v.shutdown
    end
  end
  @api.shutdown if !!@api && @api != self
  @api = nil
  @block_api.shutdown if !!@block_api && @block_api != self
  @block_api = nil

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

  if @self_hashie_logger
    if !!@hashie_logger && defined?(@hashie_logger.close)
      if defined?(@hashie_logger.closed?)
        @hashie_logger.close unless @hashie_logger.closed?
      end
    end
  end
end

#stopped?Boolean

Returns:

  • (Boolean)


485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/beowulf/api.rb', line 485

def stopped?
  http_active = if @http_memo.nil?
                  false
                else
                  @http_memo.values.map do |http|
                    if defined?(http.active?)
                      http.active?
                    else
                      false
                    end
                  end.include?(true)
                end

  @uri.nil? && @http_id.nil? && !http_active && @api.nil? && @block_api.nil?
end

#use_condenser_namespace?Boolean

Returns:

  • (Boolean)


501
502
503
# File 'lib/beowulf/api.rb', line 501

def use_condenser_namespace?
  @use_condenser_namespace
end