Class: Universa::Client

Inherits:
Object
  • Object
show all
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

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Universa

#retry_with_timeout

Constructor Details

#initialize(private_key = nil) ⇒ Client

Create client

Parameters:

  • private_key (PrivateKey) (defaults to: nil)

    to connect with. Generates new one if omitted.


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_keyObject (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

#[](name) ⇒ Object


73
74
75
# File 'lib/universa/client.rb', line 73

def [] name
  @nodes.find {|x| x.url =~ /#{name}/}
end

#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.

Parameters:

Returns:

  • (ContractState)

    of some final node check It does not aggregates (yet)


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_keyObject

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_connectionConnection

Returns random connection

Returns:


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

Returns:

  • (Array(Connection))

    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

#sizeObject

Number of accessible nodes


25
26
27
# File 'lib/universa/client.rb', line 25

def size
  @nodes.size
end