Class: Universa::Client
- Inherits:
-
Object
- Object
- Universa::Client
- Includes:
- Universa
- Defined in:
- lib/universa/client.rb
Overview
Universa network client reads current network configuration and provides access to each node independently and also implement newtor-wide procedures.
Constant Summary
Constants included from Universa
Instance Attribute Summary collapse
-
#connection_key ⇒ Object
readonly
Returns the value of attribute connection_key.
Instance Method Summary collapse
-
#get_state(obj) ⇒ ContractState
Perform fats consensus state check.
-
#initialize(private_key = nil) ⇒ Client
constructor
Create client.
-
#private_key ⇒ Object
private key used by the connection (might be generated).
-
#random_connection ⇒ Connection
Random connection.
-
#random_connections(count = 1) ⇒ Array(Connection)
Array of count randomly selected connections.
- #register_single(contract) ⇒ Object
-
#size ⇒ Object
Number of accessible nodes.
Methods included from Universa
Constructor Details
#initialize(private_key = nil) ⇒ Client
Create client
19 20 21 22 |
# File 'lib/universa/client.rb', line 19 def initialize private_key = nil @connection_key = private_key scan_network() end |
Instance Attribute Details
#connection_key ⇒ Object (readonly)
Returns the value of attribute connection_key.
15 16 17 |
# File 'lib/universa/client.rb', line 15 def connection_key @connection_key end |
Instance Method Details
#get_state(obj) ⇒ ContractState
Perform fats consensus state check. E.g. it scans up to 2/3 of the network until the positive or negative consensus will be found. So far you can only rely on result.approved? as it returns some last node result which, though, match the consensus. Aggregation of parameters is under way.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/universa/client.rb', line 50 def get_state obj result = Concurrent::IVar.new negative_votes = Concurrent::AtomicFixnum.new(@nodes.size * 11 / 100) positive_votes = Concurrent::AtomicFixnum.new(@nodes.size * 30 / 100) retry_with_timeout(20, 3) { random_connections(@nodes.size).par.each {|conn| if result.incomplete? if (state = conn.get_state(obj)).approved? result.try_set(state) if positive_votes.decrement < 0 else result.try_set(state) if negative_votes.decrement < 0 end end } result.value } end |
#private_key ⇒ Object
private key used by the connection (might be generated)
30 31 32 |
# File 'lib/universa/client.rb', line 30 def private_key @connection_key ||= PrivateKey.new(2048) end |
#random_connection ⇒ Connection
Returns random connection.
35 36 37 |
# File 'lib/universa/client.rb', line 35 def random_connection @nodes.sample end |
#random_connections(count = 1) ⇒ Array(Connection)
Returns array of count randomly selected connections.
69 70 71 |
# File 'lib/universa/client.rb', line 69 def random_connections count = 1 @nodes.sample(count) end |
#register_single(contract) ⇒ Object
39 40 41 |
# File 'lib/universa/client.rb', line 39 def register_single contract random_connection.register_single contract end |
#size ⇒ Object
Number of accessible nodes
25 26 27 |
# File 'lib/universa/client.rb', line 25 def size @nodes.size end |