Class: Banano::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri: Client::LOCAL_ENDPOINT, timeout: Client::DEFAULT_TIMEOUT) ⇒ Node

Returns a new instance of Node.



7
8
9
# File 'lib/banano/node.rb', line 7

def initialize(uri: Client::LOCAL_ENDPOINT, timeout: Client::DEFAULT_TIMEOUT)
  @client = Client.new(uri: uri, timeout: timeout)
end

Instance Attribute Details

#timeoutObject (readonly)

Returns the value of attribute timeout.



5
6
7
# File 'lib/banano/node.rb', line 5

def timeout
  @timeout
end

#uriObject (readonly)

Returns the value of attribute uri.



5
6
7
# File 'lib/banano/node.rb', line 5

def uri
  @uri
end

Instance Method Details

#account_countInteger 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 Nanook::WalletAccount#receive). All accounts that respond true to Nanook::Account#exists? have open blocks in the ledger.

Returns:

  • (Integer)

    number of accounts with open blocks.



22
23
24
# File 'lib/banano/node.rb', line 22

def 
  rpc(action: :frontier_count)[:count]
end

#block_countHash{Symbol=>Integer}

The count of all blocks downloaded to the node, and blocks still to be synchronized by the node.

Returns:

  • (Hash{Symbol=>Integer})

    number of blocks and unchecked synchronizing blocks



32
33
34
# File 'lib/banano/node.rb', line 32

def block_count
  rpc(action: :block_count)
end

#block_count_by_typeHash{Symbol=>Integer} Also known as: block_count_type

The count of all known blocks by their type.

Returns:

  • (Hash{Symbol=>Integer})

    number of blocks by type



39
40
41
# File 'lib/banano/node.rb', line 39

def block_count_by_type
  rpc(action: :block_count_type)
end

#peersHash{Symbol=>String}

Returns information about the node peers.

Returns:

  • (Hash{Symbol=>String})

    information about the node peers



47
48
49
50
# File 'lib/banano/node.rb', line 47

def peers
  h = -> (h) { Hash[h.map{ |k,v| [k.to_s, v] }] }
  h.call(rpc(action: :peers)[:peers])
end

#representatives(raw = true) ⇒ Hash{Symbol=>Integer}

All representatives and their voting weight.

Parameters:

  • raw (Boolean) (defaults to: true)

    if true return raw balances, else banano units

Returns:

  • (Hash{Symbol=>Integer})

    known representatives and their voting weight



56
57
58
59
60
61
62
63
64
65
# File 'lib/banano/node.rb', line 56

def representatives(raw = true)
  response = rpc(action: :representatives)[:representatives]
  response = response.delete_if {|_, balance| balance.to_s == '0' } # remove 0 balance reps
  return response if raw == true

  r = response.map do |address, balance|
    [address.to_s, Banano::Unit.raw_to_ban(balance).to_f]
  end
  Hash[r]
end

#representatives_onlineArray<String> Also known as: reps_online

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 # => ["ban_111...", "ban_311..."]

Returns:

  • (Array<String>)

    array of representative account ids



76
77
78
# File 'lib/banano/node.rb', line 76

def representatives_online
  rpc(action: :representatives_online)[:representatives]
end

#rpc(action:, params: {}) ⇒ Object



11
12
13
# File 'lib/banano/node.rb', line 11

def rpc(action:, params: {})
  @client.rpc_call(action: action, params: params)
end

#sync_progressFloat

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.

Returns:

  • (Float)

    the percentage completeness of the synchronization process for your node



101
102
103
104
105
106
107
108
109
# File 'lib/banano/node.rb', line 101

def sync_progress
  response = block_count

  count = response[:count].to_i
  unchecked = response[:unchecked].to_i
  total = count + unchecked

  count.to_f * 100 / total.to_f
end

#synchronizing_blocks(limit: 1000) ⇒ Hash{Symbol=>String} Also known as: unchecked

Returns information about the synchronizing blocks for this node.

Parameters:

  • limit (Integer) (defaults to: 1000)

    number of synchronizing blocks to return

Returns:

  • (Hash{Symbol=>String})

    information about the synchronizing blocks for this node



83
84
85
86
87
88
89
90
# File 'lib/banano/node.rb', line 83

def synchronizing_blocks(limit: 1000)
  response = rpc(action: :unchecked, params: {count: limit})[:blocks]
  # response = response.map do |block, info|
  #  [block, JSON.parse(info).to_symbolized_hash]
  # end
  # Hash[response.sort].to_symbolized_hash
  response
end

#versionHash{Symbol=>Integer|String} Also known as: info

Returns version information for this node.

Returns:

  • (Hash{Symbol=>Integer|String})

    version information for this node



112
113
114
# File 'lib/banano/node.rb', line 112

def version
  rpc(action: :version)
end