Class: Nanook::Block
- Inherits:
-
Object
- Object
- Nanook::Block
- Defined in:
- lib/nanook/block.rb
Overview
The Nanook::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"
Initialize this class through the convenient Nanook#block method:
nanook = Nanook.new
block = nanook.block("FBF8B0E...")
Or compose the longhand way like this:
rpc_conn = Nanook::Rpc.new
block = Nanook::Block.new(rpc_conn, "FBF8B0E...")
Instance Method Summary collapse
-
#account ⇒ Nanook::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(rpc, block) ⇒ Block
constructor
A new instance of Block.
- #inspect ⇒ Object
-
#is_valid_work?(work) ⇒ Boolean
Example:.
-
#pending? ⇒ Boolean
Example:.
-
#publish ⇒ String
(also: #process)
Publish the block to the nano 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(rpc, block) ⇒ Block
Returns a new instance of Block.
21 22 23 24 25 |
# File 'lib/nanook/block.rb', line 21 def initialize(rpc, block) @rpc = rpc @block = block block_required! # All methods expect a block end |
Instance Method Details
#account ⇒ Nanook::Account
33 34 35 |
# File 'lib/nanook/block.rb', line 33 def account Nanook::Account.new(@rpc, rpc(:block_account, :hash)[:account]) end |
#cancel_work ⇒ Boolean
Stop generating work for a block.
Example:
block.cancel_work # => true
44 45 46 |
# File 'lib/nanook/block.rb', line 44 def cancel_work rpc(:work_cancel, :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)
Example reponse:
[
"36A0FB717368BA8CF8D255B63DC207771EABC6C6FFC22A7F455EC2209464897E",
"FBF8B0E6623A31AB528EBD839EEAA91CAFD25C12294C46754E45FD017F7939EB"
]
69 70 71 72 |
# File 'lib/nanook/block.rb', line 69 def chain(limit: 1000, offset: 0) response = rpc(:chain, :block, count: limit, offset: offset)[:blocks] Nanook::Util.coerce_empty_string_to_type(response, Array) 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. If confirmed, your block should appear in Node#confirmation_history within a short amount of time, or you can use the convenience method #confirmed_recently?
Example:
block.confirm # => true
86 87 88 |
# File 'lib/nanook/block.rb', line 86 def confirm rpc(:block_confirm, :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. The full list of blocks can be queried for with Node#confirmation_history.
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
110 111 112 |
# File 'lib/nanook/block.rb', line 110 def confirmed_recently? @rpc.call(:confirmation_history)[:confirmations].map{|h| h[:hash]}.include?(@block) end |
#generate_work(use_peers: false) ⇒ String
Generate work for a block.
Example:
block.generate_work # => "2bf29ef00786a6bc"
124 125 126 |
# File 'lib/nanook/block.rb', line 124 def generate_work(use_peers: false) rpc(:work_generate, :hash, 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)
Example response:
[
{
:account=>"nano_3x7cjioqahgs5ppheys6prpqtb4rdknked83chf97bot1unrbdkaux37t31b",
:amount=>539834279601145558517940224,
:hash=>"36A0FB717368BA8CF8D255B63DC207771EABC6C6FFC22A7F455EC2209464897E",
:type=>"send"
}
]
148 149 150 |
# File 'lib/nanook/block.rb', line 148 def history(limit: 1000) rpc(:history, :hash, count: limit)[:history] end |
#id ⇒ String
Returns the block hash id.
Example:
block.id #=> "FBF8B0E..."
159 160 161 |
# File 'lib/nanook/block.rb', line 159 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)
Example response:
{
:id=>"36A0FB717368BA8CF8D255B63DC207771EABC6C6FFC22A7F455EC2209464897E",
:type=>"send",
:previous=>"FBF8B0E6623A31AB528EBD839EEAA91CAFD25C12294C46754E45FD017F7939EB",
:destination=>"nano_3x7cjioqahgs5ppheys6prpqtb4rdknked83chf97bot1unrbdkaux37t31b",
:balance=>"00000000000000000000000000000000",
:work=>"44cc24b60705083a",
:signature=>"42ADFEFE7C3FFF188AE92A202F8A5734DE91779C454613E446EEC93D001D6C953E9FD16730AF32C891791BA8EDAECEB059A213E2FE1EEB7ADF9D5D0815464D06"
}
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/nanook/block.rb', line 184 def info(allow_unchecked: false) if allow_unchecked response = rpc(:unchecked_get, :hash) unless response.has_key?(:error) return _parse_info_response(response) end # If unchecked not found, continue to checked block end response = rpc(:block, :hash) _parse_info_response(response) end |
#inspect ⇒ Object
281 282 283 |
# File 'lib/nanook/block.rb', line 281 def inspect "#{self.class.name}(id: \"#{id}\", object_id: \"#{"0x00%x" % (object_id << 1)}\")" end |
#is_valid_work?(work) ⇒ Boolean
Example:
block.is_valid_work?("2bf29ef00786a6bc") # => true
203 204 205 206 |
# File 'lib/nanook/block.rb', line 203 def is_valid_work?(work) response = rpc(:work_validate, :hash, work: work) !response.empty? && response[:valid] == 1 end |
#pending? ⇒ Boolean
Example:
block.pending? #=> false
239 240 241 242 |
# File 'lib/nanook/block.rb', line 239 def pending? response = rpc(:pending_exists, :hash) !response.empty? && response[:exists] == 1 end |
#publish ⇒ String Also known as: process
Publish the block to the nano network.
Note, if block has previously been published, use #republish instead.
Examples:
block.publish # => "FBF8B0E..."
253 254 255 |
# File 'lib/nanook/block.rb', line 253 def publish rpc(:process, :block)[:hash] || false 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
Example response:
["36A0FB717368BA8CF8D255B63DC207771EABC6C6FFC22A7F455EC2209464897E"]
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/nanook/block.rb', line 220 def republish(destinations:nil, sources:nil) if !destinations.nil? && !sources.nil? raise ArgumentError.new("You must provide 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(:republish, :hash, 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
Example response:
["36A0FB717368BA8CF8D255B63DC207771EABC6C6FFC22A7F455EC2209464897E"]
276 277 278 279 |
# File 'lib/nanook/block.rb', line 276 def successors(limit: 1000, offset: 0) response = rpc(:successors, :block, count: limit, offset: offset)[:blocks] Nanook::Util.coerce_empty_string_to_type(response, Array) end |