Class: Casper::RpcClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rpc/rpc.rb,
lib/rpc/rpc_client.rb

Overview

Interacting with the network via RPC

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip_address) ⇒ RpcClient

Constructor

Parameters:

  • ip_address (String)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rpc/rpc.rb', line 18

def initialize(ip_address)
  @ip_address = ip_address
  @url = "http://" + @ip_address + ":7777/rpc"
  @state_root_hash = ""
  @peer_array = []
  @block_hash = ""
  @deploy = {}
  @node_status = {}
  @block_transfers = []
  @block_info = {}
  @era_summary = {}
  @balance_value = ""
  @auction_state = {}

  @peers = []
end

Instance Attribute Details

#ip_addressObject

Returns the value of attribute ip_address.



14
15
16
# File 'lib/rpc/rpc.rb', line 14

def ip_address
  @ip_address
end

#portObject

Returns the value of attribute port.



14
15
16
# File 'lib/rpc/rpc.rb', line 14

def port
  @port
end

#state_root_hashObject

Returns the value of attribute state_root_hash.



14
15
16
# File 'lib/rpc/rpc.rb', line 14

def state_root_hash
  @state_root_hash
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/rpc/rpc.rb', line 14

def url
  @url
end

Instance Method Details

#chain_get_block(block_hash) ⇒ Hash

Returns block_info.

Parameters:

  • block_hash (String)

Returns:

  • (Hash)

    block_info



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rpc/rpc.rb', line 122

def chain_get_block(block_hash)
  begin 
    state = Timeout::timeout(10) {
      client = Jimson::Client.new(@url)
      result = client.chain_get_block({"block_identifier" => {"Hash" => block_hash}})
      @block_info = result["block"]
      if (!@block_info.empty?() && @block_info["hash"] != block_hash)
        raise("Returned block does not have a matching hash.")
      else
        @block_info
      end
    }
  rescue
    'Timeout expired to retrieve block_info'
  end
end

#chain_get_block_transfers(block_hash = "") ⇒ Array<Hash>

Returns block_transfers.

Parameters:

  • block_hash (String) (defaults to: "")

Returns:

  • (Array<Hash>)

    block_transfers



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rpc/rpc.rb', line 105

def chain_get_block_transfers(block_hash = "") 
  begin
    status = Timeout::timeout(5) {
      client = Jimson::Client.new(@url)
      response = client.chain_get_block_transfers({
        "block_identifier" => {"Hash" => block_hash}
      })
      @block_transfers = response["transfers"]
      @block_transfers
    }
  rescue
    'Timeout expired to retrieve block_transfers'
  end
end

#chain_get_eraInfo_by_SwitchBlock(block_hash) ⇒ Hash

Returns era_summary.

Parameters:

  • block_hash (String)

Returns:

  • (Hash)

    era_summary



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rpc/rpc.rb', line 141

def chain_get_eraInfo_by_SwitchBlock(block_hash)
  begin
    state = Timeout::timeout(60) {
    client = Jimson::Client.new(@url)
    response = client.chain_get_era_info_by_switch_block("block_identifier" => {"Hash" => block_hash})
    @era_summary = response["era_summary"]
    @era_summary
    }
  rescue
    'Timeout expired to retrieve era_summary'
  end
end

#chain_get_StateRootHash(block_hash = "") ⇒ String

Returns state_root_hash value.

Returns:

  • (String)

    state_root_hash value



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

def chain_get_StateRootHash(block_hash = "")
  begin 
    status = Timeout::timeout(10) {
      client = Jimson::Client.new(@url)
      result = client.chain_get_state_root_hash
      @state_root_hash = result["state_root_hash"]
    }
  rescue
    'Timeout expired to retrieve state_root_hash value!'
  end
end

#get_errorObject



37
38
39
# File 'lib/rpc/rpc_client.rb', line 37

def get_error 
  @err
end

#info_get_deploy(deploy_hash) ⇒ Hash

Get information about a single deploy by hash.

Parameters:

  • deploy_hash (String)

Returns:

  • (Hash)

    Deploy



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rpc/rpc.rb', line 70

def info_get_deploy(deploy_hash)
  begin
    status = Timeout::timeout(20) {
      if (deploy_hash == "" || deploy_hash == nil)
        return "Server error -32602: Invalid params"
      end
      client = Jimson::Client.new(@url)
      response = client.info_get_deploy({"deploy_hash"=> deploy_hash })
      @deploy = response["deploy"]
      # @deploy.keys.each do |key|  
      #     @deploy[(key.to_sym rescue key) || key] = @deploy.delete(key)
      # end
      @deploy
    }
  rescue
    'Timeout expired to retrieve Deploy!'
  end
end

#info_get_peersArray<Hash>

Returns peers array.

Returns:

  • (Array<Hash>)

    peers array



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rpc/rpc.rb', line 36

def info_get_peers
  begin
    status = Timeout::timeout(20) {
      client = Jimson::Client.new(@url)
      response = client.info_get_peers
      response.deep_symbolize_keys!
      @peer_array = response[:peers]
      @peer_array.map { |item| @peers << Casper::Entity::Peer.new(item) }
      # @peers[0].get_node_id
      # @peers[0].get_node_id
      # @peers[0].get_address
      @peer_array
    }
  rescue Timeout::Error 
    'Timeout expired to retrieve peers!'
  end
end

#info_get_statusObject

Receive node status information

Returns:

  • node_status



92
93
94
95
96
97
98
99
100
101
# File 'lib/rpc/rpc.rb', line 92

def info_get_status
  begin
    status = Timeout::timeout(10) {
      client = Jimson::Client.new(@url)
      @node_status = client.info_get_status
    }
  rescue
    'Timeout expired to retrieve node status information'
  end
end

#state_get_AuctionInfoHash

Returns current auction system contract information.

Returns:

  • (Hash)

    auction_state



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rpc/rpc.rb', line 213

def state_get_AuctionInfo
  begin 
    state = Timeout::timeout(50) {
    client = Jimson::Client.new(@url)
    response = client.state_get_auction_info
    @auction_state = response['auction_state']
    @auction_state
    }
  rescue
    'Timeout expired to retrieve auction_state information!'
  end
end

#state_get_balance(state_root_hash, balance_uref) ⇒ Object

Parameters:

  • state_root_hash (String)
  • balance_uref (String)


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rpc/rpc.rb', line 195

def state_get_balance(state_root_hash, balance_uref)
  begin
    state = Timeout::timeout(5) {
      client = Jimson::Client.new(@url)
      response = client.state_get_balance({
        "state_root_hash" => state_root_hash,
        "purse_uref" => balance_uref
      })
      @balance_value = response["balance_value"]
      @balance_value
    }
  rescue
    'Timeout expired to retrieve balance_value'
  end
end

#state_get_dictionary_item(state_root_hash, item_key, uref) ⇒ Object

Parameters:

  • state_root_hash (String)
  • item_key (String)
  • uref (String)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rpc/rpc.rb', line 177

def state_get_dictionary_item(state_root_hash, item_key, uref)
  begin
    state = Timeout::timeout(5) {
      client = Jimson::Client.new(@url)
      response = client.state_get_dictionary_item({
        "state_root_hash" => state_root_hash,
        "dictionary_identifier" => {'URef' => 
          {'seed_uref' => uref, 'dictionary_item_key' => item_key} }})
      @stored_value = response["stored_value"]
      @stored_value
    }
  rescue
    'Timeout expired to retrieve stored_value'
  end
end

#state_get_item(state_root_hash, key, path) ⇒ Object

Parameters:

  • state_root_hash (String)
  • key (String)
  • path (Array)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rpc/rpc.rb', line 157

def state_get_item(state_root_hash, key, path)
  begin
    state = Timeout::timeout(20) {
      client = Jimson::Client.new(@url)
      response = client.state_get_item({ 
        "state_root_hash" => state_root_hash, 
        "key" => key,
        "path" => path
      })
      @stored_value = response["stored_value"]
      @stored_value
    }
  rescue
    'Timeout expired to retrieve stored_value'
  end
end