class Banano::Block

The Banano::Block class contains methods to discover publicly-available information about blocks on the nano network.

A block is represented by a unique id like this:

"FBF8B0E6623A31AB528EBD839EEAA91CAFD25C12294C46754E45FD017F7939EB"

Public Class Methods

new(node:, block:) click to toggle source
# File lib/banano/block.rb, line 12
def initialize(node:, block:)
  @node = node
  @block = block
  block_required! # All methods expect a block
end

Public Instance Methods

account() click to toggle source

Returns the {Banano::Account} of the block.

Example:

block.account # => Banano::Account

@return [Banano::Account] the account of the block

# File lib/banano/block.rb, line 24
def account
  address = rpc(action: :block_account, param_name: :hash)[:account]
  Banano::Account.new(node: @node, address: address)
end
ancestors(limit: 1000, offset: 0)
Alias for: chain
cancel_work() click to toggle source

Stop generating work for a block.

Example:

block.cancel_work # => true

@return [Boolean] signalling if the action was successful

# File lib/banano/block.rb, line 36
def cancel_work
  rpc(action: :work_cancel, param_name: :hash).empty?
end
chain(limit: 1000, offset: 0) click to toggle source

Returns a consecutive list of block hashes in the account chain starting at block back to count (direction from frontier back to open block, from newer blocks to older). Will list all blocks back to the open block of this chain when count is set to “-1”. The requested block hash is included in the answer.

See also successors.

Example:

block.chain(limit: 2)

@param limit [Integer] maximum number of block hashes to return (default is 1000) @param offset [Integer] return the account chain block hashes offset by

the specified number of blocks (default is 0)
# File lib/banano/block.rb, line 55
def chain(limit: 1000, offset: 0)
  params = {count: limit, offset: offset}
  rpc(action: :chain, param_name: :block, params: params)[:blocks]
end
Also aliased as: ancestors
confirm() click to toggle source

Request confirmation for a block from online representative nodes. Will return immediately with a boolean to indicate if the request for confirmation was successful. Note that this boolean does not indicate the confirmation status of the block.

Example:

block.confirm # => true

@return [Boolean] if the confirmation request was sent successful

# File lib/banano/block.rb, line 70
def confirm
  rpc(action: :block_confirm, param_name: :hash)[:started] == '1'
end
confirmed_recently?() click to toggle source

This call is for internal diagnostics/debug purposes only. Do not rely on this interface being stable and do not use in a production system.

Check if the block appears in the list of recently confirmed blocks by online representatives.

This method can work in conjunction with {Banano::Block#confirm}, whereby you can send any block (old or new) out to online representatives to confirm. The confirmation process can take up to a couple of minutes.

The method returning false can indicate that the block is still in the process of being confirmed and that you should call the method again soon, or that it was confirmed earlier than the list available in {Banano::Node#confirmation_history}, or that it was not confirmed.

Example:

block.confirmed_recently? # => true

@return [Boolean] true if the block has been recently confirmed by

online representatives.
# File lib/banano/block.rb, line 94
def confirmed_recently?
  @node.rpc(action: :confirmation_history)[:confirmations].map do |h|
    h[:hash]
  end.include?(@block)
end
Also aliased as: recently_confirmed?
generate_work(use_peers: false) click to toggle source

Generate work for a block.

Example:

block.generate_work # => "2bf29ef00786a6bc"

@param use_peers [Boolean] if set to true, then the node will query

its work peers (if it has any, see {Banano::WorkPeer#list}).
When +false+, the node will only generate work locally (default is +false+)

@return [String] the work id of the work completed.

# File lib/banano/block.rb, line 110
def generate_work(use_peers: false)
  rpc(action: :work_generate, param_name: :hash, params: {use_peers: use_peers})[:work]
end
history(limit: 1000) click to toggle source

Returns Array of Hashes containing information about a chain of send/receive blocks, starting from this block.

Example:

block.history(limit: 1)

@param limit [Integer] maximum number of send/receive block hashes

to return in the chain (default is 1000)
# File lib/banano/block.rb, line 123
def history(limit: 1000)
  response = rpc(action: :history, param_name: :hash, params: {count: limit})
  response[:history].collect {|entry| Banano::Util.symbolize_keys(entry) }
end
id() click to toggle source

Returns the block hash id.

Example:

block.id #=> "FBF8B0E..."

@return [String] the block hash id

# File lib/banano/block.rb, line 135
def id
  @block
end
info(allow_unchecked: false) click to toggle source

Returns a Hash of information about the block.

Examples:

block.info
block.info(allow_unchecked: true)

@param allow_unchecked [Boolean] (default is false). If true,

information can be returned about blocks that are unchecked (unverified).
# File lib/banano/block.rb, line 148
def info(allow_unchecked: false)
  if allow_unchecked
    response = rpc(action: :unchecked_get, param_name: :hash)
    return _parse_info_response(response) unless response.key?(:error)
    # If unchecked not found, continue to checked block
  end

  response = rpc(action: :block, param_name: :hash)
  _parse_info_response(response)
end
is_valid_work?(work) click to toggle source

Example:

block.is_valid_work?("2bf29ef00786a6bc") # => true

@param work [String] the work id to check is valid @return [Boolean] signalling if work is valid for the block

# File lib/banano/block.rb, line 165
def is_valid_work?(work)
  response = rpc(action: :work_validate, param_name: :hash, params: {work: work})
  !response.empty? && response[:valid] == '1'
end
pending?() click to toggle source

Example:

block.pending? #=> false

@return [Boolean] signalling if the block is a pending block.

# File lib/banano/block.rb, line 198
def pending?
  response = rpc(action: :pending_exists, param_name: :hash)
  !response.empty? && response[:exists] == '1'
end
process(subtype = '')
Alias for: publish
publish(subtype = '') click to toggle source

Publish the block to the banano network.

Note, if block has previously been published, use republish instead.

Examples:

block.publish # => "FBF8B0E..."

@param [String] subtype: 'send', 'receive', 'open', 'epoch' etc. @return [String] the block hash, or false.

# File lib/banano/block.rb, line 213
def publish(subtype = '')
  json_rpc(action: :process, params: {subtype: subtype})
end
Also aliased as: process
recently_confirmed?()
Alias for: confirmed_recently?
republish(destinations: nil, sources: nil) click to toggle source

Republish blocks starting at this block up the account chain back to the nano network.

@return [Array<String>] block hashes that were republished

Example:

block.republish
# File lib/banano/block.rb, line 179
def republish(destinations: nil, sources: nil)
  if !destinations.nil? && !sources.nil?
    raise ArgumentError, "Either destinations or sources but not both"
  end

  # Add in optional arguments
  params = {}
  params[:destinations] = destinations unless destinations.nil?
  params[:sources] = sources unless sources.nil?
  params[:count] = 1 unless params.empty?

  rpc(action: :republish, param_name: :hash, params: params)[:blocks]
end
successors(limit: 1000, offset: 0) click to toggle source

Returns an Array of block hashes in the account chain ending at this block.

See also chain.

Example:

block.successors

@param limit [Integer] maximum number of send/receive block hashes

to return in the chain (default is 1000)

@param offset [Integer] return the account chain block hashes offset

by the specified number of blocks (default is 0)

@return [Array<String>] block hashes in the account chain ending at this block

# File lib/banano/block.rb, line 232
def successors(limit: 1000, offset: 0)
  params = {count: limit, offset: offset}
  rpc(action: :successors, param_name: :block, params: params)[:blocks]
end

Private Instance Methods

_parse_info_response(response) click to toggle source
# File lib/banano/block.rb, line 257
def _parse_info_response(response)
  parsed_contents = Util.symbolize_keys(JSON.parse(response[:contents]))
  response[:contents] = parsed_contents
  response
end
block_required!() click to toggle source
# File lib/banano/block.rb, line 253
def block_required!
  raise ArgumentError, "Block must be present" if @block.nil?
end
json_rpc(action:, params: {}) click to toggle source

Special RPC - publish require {block: block.info} parameter

# File lib/banano/block.rb, line 248
def json_rpc(action:, params: {})
  p = {block: JSON.dump(info), json_block: true}
  @node.rpc(action: action, params: p.merge(params))
end
rpc(action:, param_name:, params: {}) click to toggle source

Some RPC calls expect the param that represents the block to be named “hash”, and others “block”. The param_name argument allows us to specify which it should be for this call.

# File lib/banano/block.rb, line 242
def rpc(action:, param_name:, params: {})
  p = @block.nil? ? {} : {param_name.to_sym => @block}
  @node.rpc(action: action, params: p.merge(params))
end