Class: Banano::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/banano/block.rb

Overview

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"

Instance Method Summary collapse

Constructor Details

#initialize(node:, block:) ⇒ Block

Returns a new instance of Block.



12
13
14
15
16
# File 'lib/banano/block.rb', line 12

def initialize(node:, block:)
  @node = node
  @block = block
  block_required! # All methods expect a block
end

Instance Method Details

#accountBanano::Account

Returns the Account of the block.

Example:

block. # => Banano::Account

Returns:



24
25
26
27
# File 'lib/banano/block.rb', line 24

def 
  address = rpc(action: :block_account, param_name: :hash)[:account]
  Banano::.new(node: @node, address: address)
end

#cancel_workBoolean

Stop generating work for a block.

Example:

block.cancel_work # => true

Returns:

  • (Boolean)

    signalling if the action was successful



36
37
38
# File 'lib/banano/block.rb', line 36

def cancel_work
  rpc(action: :work_cancel, param_name: :hash).empty?
end

#chain(limit: 1000, offset: 0) ⇒ Object Also known as: ancestors

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)

Parameters:

  • limit (Integer) (defaults to: 1000)

    maximum number of block hashes to return (default is 1000)

  • offset (Integer) (defaults to: 0)

    return the account chain block hashes offset by the specified number of blocks (default is 0)



55
56
57
58
# 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

#confirmBoolean

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

Returns:

  • (Boolean)

    if the confirmation request was sent successful



70
71
72
# File 'lib/banano/block.rb', line 70

def confirm
  rpc(action: :block_confirm, param_name: :hash)[:started] == '1'
end

#confirmed_recently?Boolean Also known as: recently_confirmed?

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 #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 Node#confirmation_history, or that it was not confirmed.

Example:

block.confirmed_recently? # => true

Returns:

  • (Boolean)

    true if the block has been recently confirmed by online representatives.



94
95
96
97
98
# 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

#generate_work(use_peers: false) ⇒ String

Generate work for a block.

Example:

block.generate_work # => "2bf29ef00786a6bc"

Parameters:

  • use_peers (Boolean) (defaults to: false)

    if set to true, then the node will query its work peers (if it has any, see WorkPeer#list). When false, the node will only generate work locally (default is false)

Returns:

  • (String)

    the work id of the work completed.



110
111
112
# 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) ⇒ Object

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

Example:

block.history(limit: 1)

Parameters:

  • limit (Integer) (defaults to: 1000)

    maximum number of send/receive block hashes to return in the chain (default is 1000)



123
124
125
126
# 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

#idString

Returns the block hash id.

Example:

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

Returns:

  • (String)

    the block hash id



135
136
137
# File 'lib/banano/block.rb', line 135

def id
  @block
end

#info(allow_unchecked: false) ⇒ Object

Returns a Hash of information about the block.

Examples:

block.info
block.info(allow_unchecked: true)

Parameters:

  • allow_unchecked (Boolean) (defaults to: false)

    (default is false). If true, information can be returned about blocks that are unchecked (unverified).



148
149
150
151
152
153
154
155
156
157
# 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) ⇒ Boolean

Example:

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

Parameters:

  • work (String)

    the work id to check is valid

Returns:

  • (Boolean)

    signalling if work is valid for the block



165
166
167
168
# 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?Boolean

Example:

block.pending? #=> false

Returns:

  • (Boolean)

    signalling if the block is a pending block.



198
199
200
201
# File 'lib/banano/block.rb', line 198

def pending?
  response = rpc(action: :pending_exists, param_name: :hash)
  !response.empty? && response[:exists] == '1'
end

#publish(subtype = '') ⇒ String Also known as: process

Publish the block to the banano network.

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

Examples:

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

Parameters:

  • subtype: (String)

    ‘send’, ‘receive’, ‘open’, ‘epoch’ etc.

Returns:

  • (String)

    the block hash, or false.



213
214
215
# File 'lib/banano/block.rb', line 213

def publish(subtype = '')
  json_rpc(action: :process, params: {subtype: subtype})
end

#republish(destinations: nil, sources: nil) ⇒ Array<String>

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

Example:

block.republish

Returns:

  • (Array<String>)

    block hashes that were republished



179
180
181
182
183
184
185
186
187
188
189
190
191
# 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) ⇒ Array<String>

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

See also #chain.

Example:

block.successors

Parameters:

  • limit (Integer) (defaults to: 1000)

    maximum number of send/receive block hashes to return in the chain (default is 1000)

  • offset (Integer) (defaults to: 0)

    return the account chain block hashes offset by the specified number of blocks (default is 0)

Returns:

  • (Array<String>)

    block hashes in the account chain ending at this block



232
233
234
235
# 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