Class: Casper::CasperClient

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

Overview

Interacting with the network

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip_address) ⇒ CasperClient

Constructor

Parameters:

  • ip_address (String)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/casper_network.rb', line 27

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 = {}
 
  @rpc_error = Casper::RpcError::ErrorHandle.new
  @err = @rpc_error.error_handling(@url)

  @pr = []

end

Instance Attribute Details

#ip_addressObject

Returns the value of attribute ip_address.



23
24
25
# File 'lib/casper_network.rb', line 23

def ip_address
  @ip_address
end

#portObject

Returns the value of attribute port.



23
24
25
# File 'lib/casper_network.rb', line 23

def port
  @port
end

#state_root_hashObject

Returns the value of attribute state_root_hash.



23
24
25
# File 'lib/casper_network.rb', line 23

def state_root_hash
  @state_root_hash
end

#urlObject

Returns the value of attribute url.



23
24
25
# File 'lib/casper_network.rb', line 23

def url
  @url
end

Instance Method Details

#chain_get_block(block_hash) ⇒ Hash

Returns block_info.

Parameters:

  • block_hash (String)

Returns:

  • (Hash)

    block_info



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/casper_network.rb', line 174

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.deep_symbolize_keys!
        Casper::Entity::Block.new(@block_info[:hash], @block_info[:header], @block_info[:body], @block_info[:proofs])
      end
    }
  rescue
    Casper::RpcError::InvalidParameter.error
  end
end

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

Returns block_transfers.

Parameters:

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

Returns:

  • (Array<Hash>)

    block_transfers



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

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}
      })
      @transfers = response["transfers"]
      @transfers.map { |transfer| Casper::Entity::Transfer.new(transfer.deep_symbolize_keys!)}
    }
  rescue
    Casper::RpcError::InvalidParameter.error
  end
end

#chain_get_eraInfo_by_SwitchBlock(block_hash) ⇒ Hash

Returns era_summary.

Parameters:

  • block_hash (String)

Returns:

  • (Hash)

    era_summary



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

def chain_get_eraInfo_by_SwitchBlock(block_hash)
  begin
    state = Timeout::timeout(10) {
    client = Jimson::Client.new(@url)
    response = client.chain_get_era_info_by_switch_block("block_identifier" => {"Hash" => block_hash})
    @era_summary = response["era_summary"]
    
    if @era_summary == nil
      Casper::RpcError::InvalidParameter.error
    else
      @era_summary.deep_symbolize_keys!
      Casper::Entity::EraSummary.new(@era_summary)
    end
    }
  rescue
    Casper::RpcError::InvalidParameter.error
  end
end

#chain_get_StateRootHash(block_hash = "") ⇒ String

Returns state_root_hash value.

Returns:

  • (String)

    state_root_hash value



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/casper_network.rb', line 82

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
    @rpc_error = Casper::RpcError::ErrorHandle.new
    @error = @rpc_error.error_handling(@url)
  end
end

#get_errorObject



48
49
50
# File 'lib/casper_network.rb', line 48

def get_error 
  @err
end

#info_get_deploy(deploy_hash) ⇒ Hash

Get information about a single deploy by hash. def info_get_deploy(deploy_hash)

begin
  status = Timeout::timeout(10) {
    client = Jimson::Client.new(@url)
    response = client.info_get_deploy({"deploy_hash"=> deploy_hash })
    if (deploy_hash == "" || deploy_hash == nil)
      Casper::RpcError::InvalidParameter.error
    end
    @deploy = response["deploy"]
    # @deploy.keys.each do |key|  
    #     @deploy[(key.to_sym rescue key) || key] = @deploy.delete(key)
    # end
    @deploy
  }
rescue
  Casper::RpcError::InvalidParameter.error 
end

end

Parameters:

  • deploy_hash (String)

Returns:

  • (Hash)

    Deploy



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/casper_network.rb', line 116

def info_get_deploy(deploy_hash)
  @dep = nil
  begin
    status = Timeout::timeout(60) {
      client = Jimson::Client.new(@url)
      response = client.info_get_deploy({"deploy_hash"=> deploy_hash })
      if (deploy_hash == "" || deploy_hash == nil)
        Casper::RpcError::InvalidParameter.error
      end
      # @deploy.keys.each do |key|  
      #     @deploy[(key.to_sym rescue key) || key] = @deploy.delete(key)
      # end
      temp = []
      @deploy = response["deploy"]
      @deploy.deep_symbolize_keys!
      Casper::Entity::Deploy.new(@deploy[:hash], @deploy[:header], @deploy[:payment], @deploy[:session], @deploy[:approvals])
    }
  rescue
    Casper::RpcError::InvalidParameter.error 
  end
end

#info_get_peersArray<Hash>

Returns peers array.

Returns:

  • (Array<Hash>)

    peers array



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/casper_network.rb', line 65

def info_get_peers
    begin
      client = Jimson::Client.new(@url)
      response = client.info_get_peers
      @peers = response["peers"]
      # @peers.map! do |peer|
      #   peer.symbolize_keys
      # end
      @peers.map { |item| @pr << Casper::Entity::Peer.new(item.symbolize_keys) }
      @pr
    rescue
      @rpc_error = Casper::RpcError::ErrorHandle.new
      @error = @rpc_error.error_handling(@url)
    end
end

#info_get_statusObject

Receive node status information

Returns:

  • node_status



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

def info_get_status
  begin
    status = Timeout::timeout(10) {
      client = Jimson::Client.new(@url)
      @node_status = client.info_get_status
      @node_status.deep_symbolize_keys!
      Casper::Entity::Status.new(@node_status)
    }
  rescue
    @rpc_error = Casper::RpcError::ErrorHandle.new
    @error = @rpc_error.error_handling(@url)
  end
end

#put_deploy(deploy) ⇒ Object

Parameters:

  • deploy (Deploy)


297
298
299
300
301
302
# File 'lib/casper_network.rb', line 297

def put_deploy(deploy)
  client = Jimson::Client.new(@url)
  response = client.({
    "deploy" => deploy
  })
end

#state_get_AuctionInfo(block_hash = "") ⇒ Hash

Returns current auction system contract information.

Returns:

  • (Hash)

    auction_state



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/casper_network.rb', line 276

def state_get_AuctionInfo(block_hash = "")
  begin 
    state = Timeout::timeout(50) {
    client = Jimson::Client.new(@url)
    response = client.state_get_auction_info
    @auction_state = response['auction_state']
    @auction_state.deep_symbolize_keys!
    state_root_hash = @auction_state[:state_root_hash]
    block_height = @auction_state[:block_height]
    era_validators = @auction_state[:era_validators]
    
    bids = @auction_state[:bids]
    Casper::Entity::AuctionState.new(state_root_hash, block_height, era_validators, bids)
    }
  rescue
    @rpc_error = Casper::RpcError::ErrorHandle.new
    @error = @rpc_error.error_handling(@url)
  end
end

#state_get_balance(state_root_hash, balance_uref) ⇒ Object

Parameters:

  • state_root_hash (String)
  • balance_uref (String)


258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/casper_network.rb', line 258

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
    Casper::RpcError::InvalidParameter.error
  end
end

#state_get_dictionary_item(state_root_hash, item_key, uref) ⇒ Object

Parameters:

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


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/casper_network.rb', line 237

def state_get_dictionary_item(state_root_hash, item_key, uref)
  begin
    state = Timeout::timeout(10) {
      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.deep_symbolize_keys!
        # cl_type = @stored_value[:CLValue][:cl_type]
        # bytes = @stored_value[:CLValue][:bytes]
        DeploySerializer.new().build_cl_value(@stored_value[:CLValue])
    }
  rescue
    Casper::RpcError::InvalidParameter.error
  end
end

#state_get_item(state_root_hash, key, path) ⇒ Object

Parameters:

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


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/casper_network.rb', line 216

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.deep_symbolize_keys!
      Casper::Entity::StoredValue.new(@stored_value)
    }
  rescue
    Casper::RpcError::InvalidParameter.error
  end
end