casper_network 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,3 @@
1
- # casper_network.rb
2
1
  require 'jimson'
3
2
  require 'json'
4
3
  require 'rdoc/rdoc'
@@ -7,208 +6,229 @@ require 'resolv'
7
6
  require 'rest-client'
8
7
  require 'active_support/core_ext/hash/keys'
9
8
  require 'timeout'
10
-
9
+ require 'net/http'
10
+ # require "./rpc/rpc.rb"
11
+ require_relative './rpc/rpc_error.rb'
11
12
  # Class for interacting with the network via RPC
12
- class CasperClient
13
- attr_accessor :ip_address, :port, :url, :state_root_hash
13
+ module Casper
14
+ class CasperClient
15
+ attr_accessor :ip_address, :port, :url, :state_root_hash
14
16
 
15
- # Constructor
16
- # @param [String] ip_address
17
- def initialize(ip_address)
18
- @ip_address = ip_address
19
- @url = "http://" + @ip_address + ":7777/rpc"
20
- @state_root_hash = ""
21
- @peer_array = []
22
- @block_hash = ""
23
- @deploy = {}
24
- @node_status = {}
25
- @block_transfers = []
26
- @block_info = {}
27
- @era_summary = {}
28
- @balance_value = ""
29
- @auction_state = {}
30
- end
17
+ # Constructor
18
+ # @param [String] ip_address
19
+ def initialize(ip_address)
20
+ @ip_address = ip_address
21
+ @url = "http://" + @ip_address + ":7777/rpc"
22
+ @state_root_hash = ""
23
+ @peer_array = []
24
+ @block_hash = ""
25
+ @deploy = {}
26
+ @node_status = {}
27
+ @block_transfers = []
28
+ @block_info = {}
29
+ @era_summary = {}
30
+ @balance_value = ""
31
+ @auction_state = {}
32
+
33
+ @rpc_error = Casper::RpcError::ErrorHandle.new
34
+ @err = @rpc_error.error_handling(@url)
31
35
 
32
- # @return [Array<Hash>] peers array
33
- def info_get_peers
34
- begin
35
- status = Timeout::timeout(20) {
36
- client = Jimson::Client.new(@url)
37
- result = client.info_get_peers
38
- @peer_array = result["peers"]
39
- }
40
- rescue Timeout::Error
41
- 'Timeout expired to retrieve peers!'
42
36
  end
43
- end
44
37
 
45
- # @return [String] state_root_hash value
46
- def chain_get_StateRootHash(block_hash = "")
47
- begin
48
- status = Timeout::timeout(10) {
49
- client = Jimson::Client.new(@url)
50
- result = client.chain_get_state_root_hash
51
- @state_root_hash = result["state_root_hash"]
52
- }
53
- rescue
54
- 'Timeout expired to retrieve state_root_hash value!'
38
+ def get_error
39
+ @err
55
40
  end
56
- end
57
41
 
58
- # Get information about a single deploy by hash.
59
- # @param [String] deploy_hash
60
- # @return [Hash] Deploy
61
- def info_get_deploy(deploy_hash)
62
- begin
63
- status = Timeout::timeout(20) {
64
- client = Jimson::Client.new(@url)
65
- response = client.info_get_deploy({"deploy_hash"=> deploy_hash })
66
- @deploy = response["deploy"]
67
- # @deploy.keys.each do |key|
68
- # @deploy[(key.to_sym rescue key) || key] = @deploy.delete(key)
69
- # end
70
- @deploy
71
- }
72
- rescue
73
- 'Timeout expired to retrieve Deploy!'
42
+ # @return [Array<Hash>] peers array
43
+ def info_get_peers
44
+ begin
45
+ client = Jimson::Client.new(@url)
46
+ response = client.info_get_peers
47
+ @peer_array = response["peers"]
48
+ rescue
49
+ @rpc_error = Casper::RpcError::ErrorHandle.new
50
+ @error = @rpc_error.error_handling(@url)
51
+ end
74
52
  end
75
- end
76
53
 
54
+ # @return [String] state_root_hash value
55
+ def chain_get_StateRootHash(block_hash = "")
56
+ begin
57
+ status = Timeout::timeout(10) {
58
+ client = Jimson::Client.new(@url)
59
+ result = client.chain_get_state_root_hash
60
+ @state_root_hash = result["state_root_hash"]
61
+ }
62
+ rescue
63
+ @rpc_error = Casper::RpcError::ErrorHandle.new
64
+ @error = @rpc_error.error_handling(@url)
65
+ end
66
+ end
77
67
 
78
- # Receive node status information
79
- # @return node_status
80
- def info_get_status
81
- begin
82
- status = Timeout::timeout(10) {
83
- client = Jimson::Client.new(@url)
84
- @node_status = client.info_get_status
85
- }
86
- rescue
87
- 'Timeout expired to retrieve node status information'
68
+ # Get information about a single deploy by hash.
69
+ # @param [String] deploy_hash
70
+ # @return [Hash] Deploy
71
+ def info_get_deploy(deploy_hash)
72
+ begin
73
+ status = Timeout::timeout(10) {
74
+ client = Jimson::Client.new(@url)
75
+ response = client.info_get_deploy({"deploy_hash"=> deploy_hash })
76
+ if (deploy_hash == "" || deploy_hash == nil)
77
+ Casper::RpcError::InvalidParameter.error
78
+ end
79
+ @deploy = response["deploy"]
80
+ # @deploy.keys.each do |key|
81
+ # @deploy[(key.to_sym rescue key) || key] = @deploy.delete(key)
82
+ # end
83
+ @deploy
84
+ }
85
+ rescue
86
+ Casper::RpcError::InvalidParameter.error
87
+ end
88
88
  end
89
- end
90
89
 
91
- # @param [String] block_hash
92
- # @return [Array<Hash>] block_transfers
93
- def chain_get_block_transfers(block_hash = "")
94
- begin
95
- status = Timeout::timeout(5) {
96
- client = Jimson::Client.new(@url)
97
- response = client.chain_get_block_transfers({
98
- "block_identifier" => {"Hash" => block_hash}
99
- })
100
- @block_transfers = response["transfers"]
101
- @block_transfers
102
- }
103
- rescue
104
- 'Timeout expired to retrieve block_transfers'
90
+
91
+ # Receive node status information
92
+ # @return node_status
93
+ def info_get_status
94
+ begin
95
+ status = Timeout::timeout(10) {
96
+ client = Jimson::Client.new(@url)
97
+ @node_status = client.info_get_status
98
+ }
99
+ rescue
100
+ @rpc_error = Casper::RpcError::ErrorHandle.new
101
+ @error = @rpc_error.error_handling(@url)
102
+ end
105
103
  end
106
- end
107
104
 
108
- # @param [String] block_hash
109
- # @return [Hash] block_info
110
- def chain_get_block(block_hash)
111
- begin
112
- state = Timeout::timeout(10) {
113
- client = Jimson::Client.new(@url)
114
- result = client.chain_get_block({"block_identifier" => {"Hash" => block_hash}})
115
- @block_info = result["block"]
116
- if (!@block_info.empty?() && @block_info["hash"] != block_hash)
117
- raise("Returned block does not have a matching hash.")
118
- else
119
- @block_info
120
- end
121
- }
122
- rescue
123
- 'Timeout expired to retrieve block_info'
105
+ # @param [String] block_hash
106
+ # @return [Array<Hash>] block_transfers
107
+ def chain_get_block_transfers(block_hash = "")
108
+ begin
109
+ status = Timeout::timeout(5) {
110
+ client = Jimson::Client.new(@url)
111
+ response = client.chain_get_block_transfers({
112
+ "block_identifier" => {"Hash" => block_hash}
113
+ })
114
+ @block_transfers = response["transfers"]
115
+ @block_transfers
116
+ }
117
+ rescue
118
+ Casper::RpcError::InvalidParameter.error
119
+ end
124
120
  end
125
- end
126
121
 
127
- # @param [String] block_hash
128
- # @return [Hash] era_summary
129
- def chain_get_eraInfo_by_SwitchBlock(block_hash)
130
- begin
131
- state = Timeout::timeout(60) {
132
- client = Jimson::Client.new(@url)
133
- response = client.chain_get_era_info_by_switch_block("block_identifier" => {"Hash" => block_hash})
134
- @era_summary = response["era_summary"]
135
- @era_summary
136
- }
137
- rescue
138
- 'Timeout expired to retrieve era_summary'
122
+ # @param [String] block_hash
123
+ # @return [Hash] block_info
124
+ def chain_get_block(block_hash)
125
+ begin
126
+ state = Timeout::timeout(10) {
127
+ client = Jimson::Client.new(@url)
128
+ result = client.chain_get_block({"block_identifier" => {"Hash" => block_hash}})
129
+ @block_info = result["block"]
130
+ if (!@block_info.empty?() && @block_info["hash"] != block_hash)
131
+ raise("Returned block does not have a matching hash.")
132
+ else
133
+ @block_info
134
+ end
135
+ }
136
+ rescue
137
+ Casper::RpcError::InvalidParameter.error
138
+ end
139
139
  end
140
- end
141
-
142
- # @param [String] state_root_hash
143
- # @param [String] key
144
- # @param [Array] path
145
- def state_get_item(state_root_hash, key, path)
146
- begin
147
- state = Timeout::timeout(20) {
140
+
141
+ # @param [String] block_hash
142
+ # @return [Hash] era_summary
143
+ def chain_get_eraInfo_by_SwitchBlock(block_hash)
144
+ begin
145
+ state = Timeout::timeout(10) {
148
146
  client = Jimson::Client.new(@url)
149
- response = client.state_get_item({
150
- "state_root_hash" => state_root_hash,
151
- "key" => key,
152
- "path" => path
153
- })
154
- @stored_value = response["stored_value"]
155
- @stored_value
156
- }
157
- rescue
158
- 'Timeout expired to retrieve stored_value'
147
+ response = client.chain_get_era_info_by_switch_block("block_identifier" => {"Hash" => block_hash})
148
+ @era_summary = response["era_summary"]
149
+
150
+ if @era_summary == nil
151
+ Casper::RpcError::InvalidParameter.error
152
+ else
153
+ @era_summary
154
+ end
155
+ }
156
+ rescue
157
+ Casper::RpcError::InvalidParameter.error
158
+ end
159
+ end
160
+
161
+ # @param [String] state_root_hash
162
+ # @param [String] key
163
+ # @param [Array] path
164
+ def state_get_item(state_root_hash, key, path)
165
+ begin
166
+ state = Timeout::timeout(20) {
167
+ client = Jimson::Client.new(@url)
168
+ response = client.state_get_item({
169
+ "state_root_hash" => state_root_hash,
170
+ "key" => key,
171
+ "path" => path
172
+ })
173
+ @stored_value = response["stored_value"]
174
+ @stored_value
175
+ }
176
+ rescue
177
+ Casper::RpcError::InvalidParameter.error
178
+ end
159
179
  end
160
- end
161
180
 
162
- # @param [String] state_root_hash
163
- # @param [String] item_key
164
- # @param [String] uref
165
- def state_get_dictionary_item(state_root_hash, item_key, uref)
166
- begin
167
- state = Timeout::timeout(5) {
168
- client = Jimson::Client.new(@url)
169
- response = client.state_get_dictionary_item({
170
- "state_root_hash" => state_root_hash,
171
- "dictionary_identifier" => {'URef' =>
172
- {'seed_uref' => uref, 'dictionary_item_key' => item_key} }})
173
- @stored_value = response["stored_value"]
174
- @stored_value
175
- }
176
- rescue
177
- 'Timeout expired to retrieve stored_value'
181
+ # @param [String] state_root_hash
182
+ # @param [String] item_key
183
+ # @param [String] uref
184
+ def state_get_dictionary_item(state_root_hash, item_key, uref)
185
+ begin
186
+ state = Timeout::timeout(10) {
187
+ client = Jimson::Client.new(@url)
188
+ response = client.state_get_dictionary_item({
189
+ "state_root_hash" => state_root_hash,
190
+ "dictionary_identifier" => {'URef' =>
191
+ {'seed_uref' => uref, 'dictionary_item_key' => item_key} }})
192
+ @stored_value = response["stored_value"]
193
+ @stored_value
194
+ }
195
+ rescue
196
+ Casper::RpcError::InvalidParameter.error
197
+ end
178
198
  end
179
- end
180
199
 
181
- # @param [String] state_root_hash
182
- # @param [String] balance_uref
183
- def state_get_balance(state_root_hash, balance_uref)
184
- begin
185
- state = Timeout::timeout(5) {
186
- client = Jimson::Client.new(@url)
187
- response = client.state_get_balance({
188
- "state_root_hash" => state_root_hash,
189
- "purse_uref" => balance_uref
190
- })
191
- @balance_value = response["balance_value"]
192
- @balance_value
193
- }
194
- rescue
195
- 'Timeout expired to retrieve balance_value'
200
+ # @param [String] state_root_hash
201
+ # @param [String] balance_uref
202
+ def state_get_balance(state_root_hash, balance_uref)
203
+ begin
204
+ state = Timeout::timeout(5) {
205
+ client = Jimson::Client.new(@url)
206
+ response = client.state_get_balance({
207
+ "state_root_hash" => state_root_hash,
208
+ "purse_uref" => balance_uref
209
+ })
210
+ @balance_value = response["balance_value"]
211
+ @balance_value
212
+ }
213
+ rescue
214
+ Casper::RpcError::InvalidParameter.error
215
+ end
196
216
  end
197
- end
198
217
 
199
- # Returns current auction system contract information.
200
- # @return [Hash] auction_state
201
- def state_get_AuctionInfo
202
- begin
203
- state = Timeout::timeout(50) {
204
- client = Jimson::Client.new(@url)
205
- response = client.state_get_auction_info
206
- @auction_state = response['auction_state']
207
- @auction_state
208
- }
209
- rescue
210
- 'Timeout expired to retrieve auction_state information!'
218
+ # Returns current auction system contract information.
219
+ # @return [Hash] auction_state
220
+ def state_get_AuctionInfo
221
+ begin
222
+ state = Timeout::timeout(50) {
223
+ client = Jimson::Client.new(@url)
224
+ response = client.state_get_auction_info
225
+ @auction_state = response['auction_state']
226
+ @auction_state
227
+ }
228
+ rescue
229
+ @rpc_error = Casper::RpcError::ErrorHandle.new
230
+ @error = @rpc_error.error_handling(@url)
231
+ end
211
232
  end
212
233
  end
213
- end
214
-
234
+ end
@@ -0,0 +1,36 @@
1
+ module Casper
2
+ module Entity
3
+ class AuctionState
4
+ # @param [String] state_root_hash
5
+ # @param [Integer] block_height
6
+ # @param [Array] era_validators
7
+ # @param [Array] bids
8
+ def initialize(state_root_hash, block_height, era_validators, bids)
9
+ @state_root_hash = state_root_hash
10
+ @block_height = block_height
11
+ @era_validators = era_validators
12
+ @bids = bids
13
+ end
14
+
15
+ # @return [String] returns state root hash as a String
16
+ def get_state_root_hash
17
+ @state_root_hash
18
+ end
19
+
20
+ # @return [Integer] returns block height as an Integer
21
+ def get_block_height
22
+ @block_height
23
+ end
24
+
25
+ # @return [Array<Hash>] returns array of hashes
26
+ def get_era_validators
27
+ @era_validators
28
+ end
29
+
30
+ # @return [Array<Hash>] returns array of hashes
31
+ def get_bids
32
+ @bids
33
+ end
34
+ end
35
+ end
36
+ end
data/lib/entity/bid.rb ADDED
@@ -0,0 +1,24 @@
1
+ module Casper
2
+ module Entity
3
+ class Bid
4
+
5
+ # @param [String] public_key
6
+ # @param [BidInfo] bid_info
7
+ def initialize(public_key, bid_info)
8
+ @public_key = public_key
9
+ @BidInfo = bid_info
10
+
11
+ end
12
+
13
+ # @return [String] public_key
14
+ def get_public_key
15
+ @public_key
16
+ end
17
+
18
+ # @return [BidInfo] bid_info
19
+ def get_bid_info
20
+ @bid_info
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,51 @@
1
+ module Casper
2
+ module Entity
3
+ class BidInfo
4
+
5
+ # @param [String] bonding_purse
6
+ # @param [String] staked_amount
7
+ # @param [Integer] delegation_rate
8
+ # @param [VestingSchedule] vesting_schedule
9
+ # @param [Hash<Delegator>] delegators
10
+ # @param [Boolean] inactive
11
+ def initialize(bonding_purse, staked_amount, delegation_rate, vesting_schedule, delegators, inactive)
12
+ @bonding_purse = bonding_purse
13
+ @staked_amount = staked_amount
14
+ @delegation_rate = delegation_rate
15
+ @vesting_schedule = vesting_schedule
16
+ @delegators = delegators
17
+ @inactive = inactive
18
+ end
19
+
20
+ # @return [String] bonding_purse
21
+ def get_bonding_purse
22
+ @bonding_purse
23
+ end
24
+
25
+ # @return [String] staked_amount
26
+ def get_staked_amount
27
+ @staked_amount
28
+ end
29
+
30
+ # @return [Integer] delegation_rate
31
+ def get_delegation_rate
32
+ @delegation_rate
33
+ end
34
+
35
+ # @return [VestingSchedule] vesting_schedule
36
+ def get_vesting_schedule
37
+ @vesting_schedule
38
+ end
39
+
40
+ # @return [Delegator] delegators
41
+ def get_delegators
42
+ @delegators
43
+ end
44
+
45
+ # @return [true, false] inactive
46
+ def get_inactive
47
+ @inactive
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,37 @@
1
+ module Casper
2
+ module Entity
3
+ class Delegator
4
+
5
+ # @param [String] public_key
6
+ # @param [String] staked_amount
7
+ # @param [String] bonding_purse
8
+ # @param [String] delegatee
9
+ def initialize(public_key, staked_amount, bonding_purse, delegatee)
10
+ @public_key = public_key
11
+ @staked_amount = staked_amount
12
+ @bonding_purse = bonding_purse
13
+ @delegatee = delegatee
14
+ end
15
+
16
+ # @return [String] public_key
17
+ def get_public_key
18
+ @public_key
19
+ end
20
+
21
+ # @return [String] staked_amount
22
+ def get_staked_amount
23
+ @staked_amount
24
+ end
25
+
26
+ # @return [String] bonding_purse
27
+ def get_bonding_purse
28
+ @bonding_purse
29
+ end
30
+
31
+ # @return [String] delegatee
32
+ def get_delegatee
33
+ @delegatee
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,44 @@
1
+ module Casper
2
+ module Entity
3
+ class Deploy
4
+
5
+ # @param [String] hash
6
+ # @param [DeployHeader] header
7
+ # @param [DeployExecutable] payment
8
+ # @param [DeployExecutable] session
9
+ # @param [DeployApproval] approvals
10
+ def initialize(hash, header, payment, session, approvals)
11
+ @hash = hash
12
+ @header = header
13
+ @payment = payment
14
+ @session = session
15
+ @approvals = approvals
16
+ end
17
+
18
+ # @return [String] hash
19
+ def get_hash
20
+ @hash
21
+ end
22
+
23
+ # @return [DeployHeader] header
24
+ def get_header
25
+ @header
26
+ end
27
+
28
+ # @return [DeployExecutable] payment
29
+ def get_payment
30
+ @payment
31
+ end
32
+
33
+ # @return [DeployExecutable] session
34
+ def get_session
35
+ @session
36
+ end
37
+
38
+ # @return [DeployApproval] approvals
39
+ def get_approvals
40
+ @approvals
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,23 @@
1
+ module Casper
2
+ module Entity
3
+ class DeployApproval
4
+
5
+ # @param [String] signer
6
+ # @param [String] signature
7
+ def initialize(signer, signature)
8
+ @signer = signer
9
+ @signature = signature
10
+ end
11
+
12
+ # @return [String] signer
13
+ def get_signer
14
+ @signer
15
+ end
16
+
17
+ # return [String] signature
18
+ def get_signature
19
+ @signature
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ module Casper
2
+ module Entity
3
+ class DeployExecutable
4
+
5
+ def initialize
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module Casper
2
+ module Entity
3
+ class DeployHash
4
+
5
+ # @param [String] hash
6
+ def initialize(hash)
7
+ @hash = hash
8
+ end
9
+
10
+ # @return [String] hash
11
+ def get_hash
12
+ @hash
13
+ end
14
+ end
15
+ end
16
+ end