Class: Nanook::Node
- Inherits:
-
Object
- Object
- Nanook::Node
- Defined in:
- lib/nanook/node.rb
Overview
The Nanook::Node
class contains methods to manage your nano
node and query its data of the nano network.
Your node is constantly syncing data with other nodes on the network. When your node first starts up after being built, its database will be empty and it will begin synchronizing and downloading data of the nano ledger to its local database. The ledger is the central record of all accounts and transactions. Some of the methods in this class query your node's database formed from the nano ledger, and so the responses are determined by the completeness of your node's database.
You can determine how synchronized your node is with the nano ledger with the #sync_progress method.
Initializing
Initialize this class through the convenient #node method:
node = Nanook.new.node
Or compose the longhand way like this:
rpc_conn = Nanook::Rpc.new
node = Nanook::Node.new(rpc_conn)
Instance Method Summary collapse
-
#account_count ⇒ Integer
(also: #frontier_count)
The number of accounts in the nano ledger–essentially all accounts with open blocks.
-
#block_count ⇒ Hash{Symbol=>Integer}
The count of all blocks downloaded to the node, and blocks still to be synchronized by the node.
-
#block_count_by_type ⇒ Hash{Symbol=>Integer}
(also: #block_count_type)
The count of all known blocks by their type.
-
#bootstrap(address:, port:) ⇒ Boolean
Initialize bootstrap to a specific IP address and port.
-
#bootstrap_any ⇒ Boolean
Initialize multi-connection bootstrap to random peers.
-
#confirmation_history ⇒ Hash{Symbol=>String|Integer}
Returns block and tally weight (in raw) for recent elections winners.
-
#initialize(rpc) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ String
- #peers ⇒ Object
-
#representatives(unit: Nanook.default_unit) ⇒ Hash{Symbol=>Integer}
All representatives and their voting weight.
-
#representatives_online ⇒ Array<String>
All online representatives that have voted recently.
-
#stop ⇒ Boolean
Safely shuts down the node.
-
#sync_progress ⇒ Float
The percentage completeness of the synchronization process for your node as it downloads the nano ledger.
-
#synced? ⇒ Boolean
This method is deprecated and will be removed in 3.0, as a node never reaches 100% synchronization.
-
#synchronizing_blocks(limit: 1000) ⇒ Hash{Symbol=>String}
(also: #unchecked)
Information about the synchronizing blocks for this node.
-
#version ⇒ Hash{Symbol=>Integer|String}
(also: #info)
Version information for this node.
Constructor Details
#initialize(rpc) ⇒ Node
Returns a new instance of Node
29 30 31 |
# File 'lib/nanook/node.rb', line 29 def initialize(rpc) @rpc = rpc end |
Instance Method Details
#account_count ⇒ Integer Also known as: frontier_count
The number of accounts in the nano ledger–essentially all accounts with
open blocks. An open block is the type of block written
to the nano ledger when an account receives its first payment (see
WalletAccount#receive). All accounts that respond
true
to Account#exists? have open blocks in the
ledger.
40 41 42 |
# File 'lib/nanook/node.rb', line 40 def account_count rpc(:frontier_count)[:count] end |
#block_count ⇒ Hash{Symbol=>Integer}
The count of all blocks downloaded to the node, and blocks still to be synchronized by the node.
Example:
54 55 56 |
# File 'lib/nanook/node.rb', line 54 def block_count rpc(:block_count) end |
#block_count_by_type ⇒ Hash{Symbol=>Integer} Also known as: block_count_type
The count of all known blocks by their type.
Example:
node.block_count_by_type
Example response:
{
send: 1000,
receive: 900,
open: 900,
change: 50
}
74 75 76 |
# File 'lib/nanook/node.rb', line 74 def block_count_by_type rpc(:block_count_type) end |
#bootstrap(address:, port:) ⇒ Boolean
Initialize bootstrap to a specific IP address and port.
82 83 84 |
# File 'lib/nanook/node.rb', line 82 def bootstrap(address:, port:) rpc(:bootstrap, address: address, port: port).has_key?(:success) end |
#bootstrap_any ⇒ Boolean
Initialize multi-connection bootstrap to random peers
89 90 91 |
# File 'lib/nanook/node.rb', line 89 def bootstrap_any rpc(:bootstrap_any).has_key?(:success) end |
#confirmation_history ⇒ Hash{Symbol=>String|Integer}
Returns block and tally weight (in raw) for recent elections winners
Example:
node.confirmation_history
Example response:
[
{
block: "EA70B32C55C193345D625F766EEA2FCA52D3F2CCE0B3A30838CC543026BB0FEA",
tally: 80394786589602980996311817874549318248
},
{
block: "F2F8DA6D2CA0A4D78EB043A7A29E12BDE5B4CE7DE1B99A93A5210428EE5B8667",
tally: 68921714529890443063672782079965877749
}
]
113 114 115 116 117 118 119 |
# File 'lib/nanook/node.rb', line 113 def confirmation_history rpc(:confirmation_history)[:confirmations].map do |history| # Rename hash key to block block = history.delete(:hash) {block: block}.merge(history) end end |
#inspect ⇒ String
122 123 124 |
# File 'lib/nanook/node.rb', line 122 def inspect "#{self.class.name}(object_id: \"#{"0x00%x" % (object_id << 1)}\")" end |
#peers ⇒ Object
126 127 128 |
# File 'lib/nanook/node.rb', line 126 def peers rpc(:peers)[:peers] end |
#representatives(unit: Nanook.default_unit) ⇒ Hash{Symbol=>Integer}
All representatives and their voting weight.
Example:
node.representatives
Example response:
{
xrb_1111111111111111111111111111111111111111111111111117353trpda: 3822372327060170000000000000000000000,
xrb_1111111111111111111111111111111111111111111111111awsq94gtecn: 30999999999999999999999999000000,
xrb_114nk4rwjctu6n6tr6g6ps61g1w3hdpjxfas4xj1tq6i8jyomc5d858xr1xi: 0
}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/nanook/node.rb', line 145 def representatives(unit: Nanook.default_unit) unless Nanook::UNITS.include?(unit) raise ArgumentError.new("Unsupported unit: #{unit}") end response = rpc(:representatives)[:representatives] return response if unit == :raw r = response.map do |account_id, balance| balance = Nanook::Util.raw_to_NANO(balance) [account_id, balance] end Hash[r].to_symbolized_hash end |
#representatives_online ⇒ Array<String>
All online representatives that have voted recently. Note, due to the design of the nano RPC, this method cannot return the voting weight of the representatives.
Example:
node.representatives_online # => ["xrb_111...", "xrb_222"]
171 172 173 |
# File 'lib/nanook/node.rb', line 171 def representatives_online rpc(:representatives_online)[:representatives].keys.map(&:to_s) end |
#stop ⇒ Boolean
Safely shuts down the node.
178 179 180 |
# File 'lib/nanook/node.rb', line 178 def stop rpc(:stop).has_key?(:success) end |
#sync_progress ⇒ Float
The percentage completeness of the synchronization process for your node as it downloads the nano ledger. Note, it's normal for your progress to not ever reach 100. The closer to 100, the more complete your node's data is, and so the query methods in this class become more reliable.
201 202 203 204 205 206 207 208 209 |
# File 'lib/nanook/node.rb', line 201 def sync_progress response = rpc(:block_count) count = response[:count] unchecked = response[:unchecked] total = count + unchecked count.to_f * 100 / total.to_f end |
#synced? ⇒ Boolean
This method is deprecated and will be removed in 3.0, as a node never reaches 100% synchronization.
215 216 217 218 |
# File 'lib/nanook/node.rb', line 215 def synced? warn "[DEPRECATION] `synced?` is deprecated and will be removed in 3.0" rpc(:block_count)[:unchecked] == 0 end |
#synchronizing_blocks(limit: 1000) ⇒ Hash{Symbol=>String} Also known as: unchecked
Returns information about the synchronizing blocks for this node
184 185 186 187 188 189 190 |
# File 'lib/nanook/node.rb', line 184 def synchronizing_blocks(limit: 1000) response = rpc(:unchecked, count: limit)[:blocks] response = response.map do |block, info| [block, JSON.parse(info).to_symbolized_hash] end Hash[response.sort].to_symbolized_hash end |
#version ⇒ Hash{Symbol=>Integer|String} Also known as: info
Returns version information for this node
221 222 223 |
# File 'lib/nanook/node.rb', line 221 def version rpc(:version) end |