Class: Banano::Block
- Inherits:
-
Object
- Object
- Banano::Block
- 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
-
#account ⇒ Banano::Account
Returns the Account of the block.
-
#cancel_work ⇒ Boolean
Stop generating work for a block.
-
#chain(limit: 1000, offset: 0) ⇒ Object
(also: #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).
-
#confirm ⇒ Boolean
Request confirmation for a block from online representative nodes.
-
#confirmed_recently? ⇒ Boolean
(also: #recently_confirmed?)
This call is for internal diagnostics/debug purposes only.
-
#generate_work(use_peers: false) ⇒ String
Generate work for a block.
-
#history(limit: 1000) ⇒ Object
Returns Array of Hashes containing information about a chain of send/receive blocks, starting from this block.
-
#id ⇒ String
Returns the block hash id.
-
#info(allow_unchecked: false) ⇒ Object
Returns a Hash of information about the block.
-
#initialize(node:, block:) ⇒ Block
constructor
A new instance of Block.
-
#is_valid_work?(work) ⇒ Boolean
Example:.
-
#pending? ⇒ Boolean
Example:.
-
#publish(subtype = '') ⇒ String
(also: #process)
Publish the block to the banano network.
-
#republish(destinations: nil, sources: nil) ⇒ Array<String>
Republish blocks starting at this block up the account chain back to the nano network.
-
#successors(limit: 1000, offset: 0) ⇒ Array<String>
Returns an Array of block hashes in the account chain ending at this block.
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
#account ⇒ Banano::Account
24 25 26 27 |
# 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 |
#cancel_work ⇒ Boolean
Stop generating work for a block.
Example:
block.cancel_work # => true
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)
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 |
#confirm ⇒ Boolean
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
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
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"
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)
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 |
#id ⇒ String
Returns the block hash id.
Example:
block.id #=> "FBF8B0E..."
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)
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
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
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..."
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
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
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 |