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.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +1 -0
- data/LICENSE +201 -0
- data/README.md +154 -0
- data/SECURITY.md +12 -0
- data/lib/casper_network.rb +200 -180
- data/lib/entity/auction_state.rb +36 -0
- data/lib/entity/bid.rb +24 -0
- data/lib/entity/bid_info.rb +51 -0
- data/lib/entity/delegator.rb +37 -0
- data/lib/entity/deploy.rb +44 -0
- data/lib/entity/deploy_approval.rb +23 -0
- data/lib/entity/deploy_executable.rb +9 -0
- data/lib/entity/deploy_hash.rb +16 -0
- data/lib/entity/deploy_header.rb +58 -0
- data/lib/entity/era_summary.rb +44 -0
- data/lib/entity/era_validator.rb +23 -0
- data/lib/entity/peer.rb +25 -0
- data/lib/entity/validator_weight.rb +23 -0
- data/lib/rpc/rpc_client.rb +227 -0
- data/lib/rpc/rpc_error.rb +79 -0
- data/spec/client_spec.rb +25 -0
- data/spec/mainnet_spec.rb +452 -0
- data/spec/spec_helper.rb +100 -0
- data/spec/testnet_spec.rb +498 -0
- metadata +34 -7
data/lib/casper_network.rb
CHANGED
@@ -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
|
-
|
13
|
-
|
13
|
+
module Casper
|
14
|
+
class CasperClient
|
15
|
+
attr_accessor :ip_address, :port, :url, :state_root_hash
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
46
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
@
|
102
|
-
|
103
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
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
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
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
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
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.
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
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
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
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
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
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
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
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
|