casper_network 0.2.1 → 1.0.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/README.md +15 -4
- data/lib/casper_network.rb +62 -8
- data/lib/crypto/00_asymmetric_key.rb +95 -0
- data/lib/crypto/01_ed25519.rb +67 -0
- data/lib/crypto/asymmetric_key.rb +87 -0
- data/lib/crypto/ed25519_key.rb +44 -0
- data/lib/crypto/key_pair.rb +40 -0
- data/lib/crypto/keys.rb +7 -0
- data/lib/crypto/secp256k1_key.rb +0 -0
- data/lib/crypto/test_ed25519_key.rb +44 -0
- data/lib/entity/account.rb +45 -0
- data/lib/entity/action_thresholds.rb +25 -0
- data/lib/entity/associated_key.rb +24 -0
- data/lib/entity/auction_state.rb +1 -0
- data/lib/entity/bid.rb +1 -0
- data/lib/entity/bid_info.rb +1 -0
- data/lib/entity/contract.rb +47 -0
- data/lib/entity/contract_package.rb +41 -0
- data/lib/entity/contract_version.rb +33 -0
- data/lib/entity/delegator.rb +1 -0
- data/lib/entity/deploy.rb +1 -0
- data/lib/entity/deploy_approval.rb +8 -6
- data/lib/entity/deploy_executable.rb +155 -1
- data/lib/entity/deploy_executable_item_internal.rb +27 -0
- data/lib/entity/deploy_executable_transfer.rb +51 -0
- data/lib/entity/deploy_hash.rb +1 -0
- data/lib/entity/deploy_header.rb +18 -15
- data/lib/entity/deploy_info.rb +45 -0
- data/lib/entity/deploy_named_argument.rb +20 -0
- data/lib/entity/deploy_transfer.rb +11 -0
- data/lib/entity/disabled_version.rb +26 -0
- data/lib/entity/era_info.rb +18 -0
- data/lib/entity/era_summary.rb +1 -0
- data/lib/entity/era_validator.rb +1 -0
- data/lib/entity/executable_deploy_item.rb +11 -0
- data/lib/entity/group.rb +25 -0
- data/lib/entity/module_bytes.rb +51 -0
- data/lib/entity/peer.rb +1 -0
- data/lib/entity/seigniorage_allocation.rb +18 -0
- data/lib/entity/stored_contract_by_hash.rb +51 -0
- data/lib/entity/stored_contract_by_name.rb +52 -0
- data/lib/entity/stored_value.rb +57 -0
- data/lib/entity/stored_versioned_contract_by_hash.rb +63 -0
- data/lib/entity/stored_versioned_contract_by_name.rb +63 -0
- data/lib/entity/transfer.rb +66 -0
- data/lib/entity/validator_weight.rb +1 -0
- data/lib/entity/vesting_schedule.rb +22 -0
- data/lib/include.rb +18 -0
- data/lib/rpc/rpc.rb +227 -0
- data/lib/rpc/rpc_client.rb +45 -39
- data/lib/rpc/rpc_error.rb +1 -0
- data/lib/serialization/cl_type_serializer.rb +77 -0
- data/lib/serialization/cl_value_bytes_parsers.rb +498 -0
- data/lib/serialization/cl_value_serializer.rb +260 -0
- data/lib/serialization/deploy_approval_serializer.rb +16 -0
- data/lib/serialization/deploy_executable_serializer.rb +27 -0
- data/lib/serialization/deploy_header_serializer.rb +49 -0
- data/lib/serialization/deploy_named_arg_serializer.rb +20 -0
- data/lib/serialization/deploy_serializer.rb +269 -0
- data/lib/serialization/test.rb +431 -0
- data/lib/types/cl_account_hash.rb +24 -0
- data/lib/types/cl_account_hash_type.rb +22 -0
- data/lib/types/cl_any.rb +25 -0
- data/lib/types/cl_any_type.rb +22 -0
- data/lib/types/cl_bool.rb +32 -0
- data/lib/types/cl_bool_type.rb +35 -0
- data/lib/types/cl_byte_array.rb +25 -0
- data/lib/types/cl_byte_array_type.rb +27 -0
- data/lib/types/cl_i32.rb +26 -0
- data/lib/types/cl_i32_type.rb +26 -0
- data/lib/types/cl_i64.rb +26 -0
- data/lib/types/cl_i64_type.rb +27 -0
- data/lib/types/cl_key.rb +39 -0
- data/lib/types/cl_key_type.rb +27 -0
- data/lib/types/cl_list.rb +25 -0
- data/lib/types/cl_list_type.rb +26 -0
- data/lib/types/cl_map.rb +25 -0
- data/lib/types/cl_map_type.rb +26 -0
- data/lib/types/cl_option.rb +33 -0
- data/lib/types/cl_option_type.rb +52 -0
- data/lib/types/cl_public_key.rb +152 -0
- data/lib/types/cl_public_key_type.rb +26 -0
- data/lib/types/cl_result.rb +25 -0
- data/lib/types/cl_result_type.rb +26 -0
- data/lib/types/cl_string.rb +39 -0
- data/lib/types/cl_string_type.rb +32 -0
- data/lib/types/cl_tuple.rb +151 -0
- data/lib/types/cl_tuple_type.rb +108 -0
- data/lib/types/cl_type.rb +96 -0
- data/lib/types/cl_type_tag.rb +51 -0
- data/lib/types/cl_u128.rb +26 -0
- data/lib/types/cl_u128_type.rb +26 -0
- data/lib/types/cl_u256.rb +26 -0
- data/lib/types/cl_u256_type.rb +26 -0
- data/lib/types/cl_u32.rb +26 -0
- data/lib/types/cl_u32_type.rb +26 -0
- data/lib/types/cl_u512.rb +26 -0
- data/lib/types/cl_u512_type.rb +26 -0
- data/lib/types/cl_u64.rb +26 -0
- data/lib/types/cl_u64_type.rb +27 -0
- data/lib/types/cl_u8.rb +26 -0
- data/lib/types/cl_u8_type.rb +26 -0
- data/lib/types/cl_unit.rb +38 -0
- data/lib/types/cl_unit_type.rb +22 -0
- data/lib/types/cl_uref.rb +120 -0
- data/lib/types/cl_uref_type.rb +46 -0
- data/lib/types/cl_value.rb +11 -0
- data/lib/types/constants.rb +50 -0
- data/lib/types/error.rb +7 -0
- data/lib/utils/base_16.rb +18 -0
- data/lib/utils/byte_utils.rb +107 -0
- data/lib/utils/find_byte_parser_by_cl_type.rb +53 -0
- data/lib/utils/hash_utils.rb +19 -0
- data/lib/utils/hex_utils.rb +12 -0
- data/lib/utils/time_utils.rb +85 -0
- data/lib/utils/utils.rb +2 -0
- data/lib/version.rb +3 -0
- data/spec/a_spec.rb +697 -0
- data/spec/byte_utils_spec.rb +72 -0
- data/spec/cl_public_spec.rb +169 -0
- data/spec/cl_types_spec.rb +715 -0
- data/spec/cl_value_serializer_spec.rb +140 -0
- data/spec/crypto_spec.rb +42 -0
- data/spec/deploy_approval_serializer_spec.rb +26 -0
- data/spec/deploy_executable_serializer_spec.rb +0 -0
- data/spec/deploy_header_serializer_spec.rb +21 -0
- data/spec/deploy_named_arg_serializer_spec.rb +49 -0
- data/spec/deploy_serializer_spec.rb +77 -0
- data/spec/deploy_serializer_test_spec.rb +225 -0
- data/spec/mainnet_spec.rb +8 -8
- data/spec/string_spec.rb +68 -0
- data/spec/testnet_spec.rb +11 -11
- data/spec/time_utils_spec.rb +87 -0
- metadata +130 -2
data/lib/entity/peer.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Casper
|
2
|
+
module Entity
|
3
|
+
|
4
|
+
# Information about seigniorage allocation.
|
5
|
+
class SeigniorageAllocation
|
6
|
+
|
7
|
+
# @param [Array] seigniorage_allocations rewards list allocated to validators and delegators
|
8
|
+
def initialize(seigniorage_allocations)
|
9
|
+
@seigniorage_allocations = seigniorage_allocations
|
10
|
+
end
|
11
|
+
|
12
|
+
# @return [Array]
|
13
|
+
def get_seigniorage_allocations
|
14
|
+
@seigniorage_allocations
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require_relative './deploy_executable_item_internal.rb'
|
2
|
+
|
3
|
+
module Casper
|
4
|
+
module Entity
|
5
|
+
# Stored contract referenced by its ContractHash, entry point and an instance of RuntimeArgs.
|
6
|
+
class StoredContractByHash < DeployExecutableItemInternal
|
7
|
+
|
8
|
+
# @param [String] hash
|
9
|
+
# @param [String] entry_point
|
10
|
+
# @param [Array<Array<DeployNamedArgument>>] args
|
11
|
+
def initialize(hash, entry_point, args)
|
12
|
+
@tag = 1
|
13
|
+
@hash = hash
|
14
|
+
@entry_point = entry_point
|
15
|
+
@args = args
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_tag
|
19
|
+
@tag
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_hash
|
23
|
+
@hash
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_entry_point
|
27
|
+
@entry_point
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_args
|
31
|
+
@args
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_bytes
|
35
|
+
serializer = DeployNamedArgSerializer.new
|
36
|
+
num_of_args = @args.length
|
37
|
+
bytes = Utils::ByteUtils.to_u8(@tag)
|
38
|
+
bytes += @hash
|
39
|
+
bytes += CLValueBytesParsers::CLStringBytesParser.to_bytes(@entry_point)
|
40
|
+
bytes += Utils::ByteUtils.to_u32(num_of_args)
|
41
|
+
@args.each do |arg|
|
42
|
+
arg.each do |item|
|
43
|
+
bytes += serializer.to_bytes(item)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
Utils::ByteUtils.hex_to_byte_array(bytes)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative './deploy_executable_item_internal.rb'
|
2
|
+
|
3
|
+
module Casper
|
4
|
+
module Entity
|
5
|
+
# Stored contract referenced by a named key existing in the signer's Account context,
|
6
|
+
# entry point and an instance of RuntimeArgs.
|
7
|
+
class StoredContractByName < DeployExecutableItemInternal
|
8
|
+
|
9
|
+
# @param [String] name
|
10
|
+
# @param [String] entry_point
|
11
|
+
# @param [Array<Array<DeployNamedArgument>>] args
|
12
|
+
def initialize(name, entry_point, args)
|
13
|
+
@tag = 2
|
14
|
+
@name = name
|
15
|
+
@entry_point = entry_point
|
16
|
+
@args = args
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_tag
|
20
|
+
@tag
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_name
|
24
|
+
@name
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_entry_point
|
28
|
+
@entry_point
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_args
|
32
|
+
@args
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def to_bytes
|
37
|
+
serializer = DeployNamedArgSerializer.new
|
38
|
+
num_of_args = @args.length
|
39
|
+
bytes = Utils::ByteUtils.to_u8(@tag)
|
40
|
+
bytes += CLValueBytesParsers::CLStringBytesParser.to_bytes(@name)
|
41
|
+
bytes += CLValueBytesParsers::CLStringBytesParser.to_bytes(@entry_point)
|
42
|
+
bytes += Utils::ByteUtils.to_u32(num_of_args)
|
43
|
+
@args.each do |arg|
|
44
|
+
arg.each do |item|
|
45
|
+
bytes += serializer.to_bytes(item)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
Utils::ByteUtils.hex_to_byte_array(bytes)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Casper
|
2
|
+
module Entity
|
3
|
+
|
4
|
+
# A class that represents a value stored in global state.
|
5
|
+
class StoredValue
|
6
|
+
|
7
|
+
def initialize(cl_value, account, contract, contract_package, transfer, deploy_info, era_info, bid, withdraw)
|
8
|
+
@cl_value = cl_value
|
9
|
+
@account = account
|
10
|
+
@contract = contract
|
11
|
+
@contract_package = contract_package
|
12
|
+
@transfer = transfer
|
13
|
+
@deploy_info = deploy_info
|
14
|
+
@era_info = era_info
|
15
|
+
@bid = bid
|
16
|
+
@withdraw = withdraw
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_cl_value
|
20
|
+
@cl_value
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_account
|
24
|
+
@account
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_contract
|
28
|
+
@contract
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_contract_package
|
32
|
+
@contract_package
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_transfer
|
36
|
+
@transfer
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_deploy_info
|
40
|
+
@deploy_info
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_era_info
|
44
|
+
@era_info
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_bid
|
48
|
+
@bid
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_withdraw
|
52
|
+
@withdraw
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative './deploy_executable_item_internal.rb'
|
2
|
+
|
3
|
+
module Casper
|
4
|
+
module Entity
|
5
|
+
# Stored versioned contract referenced by its ContractPackageHash,
|
6
|
+
# entry point and an instance of RuntimeArgs.
|
7
|
+
class StoredVersionedContractByHash < DeployExecutableItemInternal
|
8
|
+
|
9
|
+
# @param [String] hash
|
10
|
+
# @param [Integer] version
|
11
|
+
# @param [String] entry_point
|
12
|
+
# @param [Array<Array<DeployNamedArgument>>] args
|
13
|
+
def initialize(hash, version, entry_point, args)
|
14
|
+
@tag = 3
|
15
|
+
@hash = hash
|
16
|
+
@version = version
|
17
|
+
@entry_point = entry_point
|
18
|
+
@args = args
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_tag
|
22
|
+
@tag
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_hash
|
26
|
+
@hash
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_version
|
30
|
+
@version
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_entry_point
|
34
|
+
@entry_point
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_args
|
38
|
+
@args
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_bytes
|
42
|
+
serializer = DeployNamedArgSerializer.new
|
43
|
+
num_of_args = @args.length
|
44
|
+
bytes = Utils::ByteUtils.to_u8(@tag)
|
45
|
+
bytes += @hash
|
46
|
+
if @version == nil
|
47
|
+
bytes += Utils::ByteUtils.to_u8(0)
|
48
|
+
else
|
49
|
+
bytes += Utils::ByteUtils.to_u8(1)
|
50
|
+
end
|
51
|
+
bytes += CLValueBytesParsers::CLStringBytesParser.to_bytes(@entry_point)
|
52
|
+
bytes += Utils::ByteUtils.to_u32(num_of_args)
|
53
|
+
@args.each do |arg|
|
54
|
+
arg.each do |item|
|
55
|
+
bytes += serializer.to_bytes(item)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
Utils::ByteUtils.hex_to_byte_array(bytes)
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative './deploy_executable_item_internal.rb'
|
2
|
+
|
3
|
+
module Casper
|
4
|
+
module Entity
|
5
|
+
# Stored versioned contract referenced by a named key existing in the signer's Account context,
|
6
|
+
# entry point and an instance of RuntimeArgs.
|
7
|
+
class StoredVersionedContractByName < DeployExecutableItemInternal
|
8
|
+
|
9
|
+
# @param [String] name
|
10
|
+
# @param [Integer] version
|
11
|
+
# @param [String] entry_point
|
12
|
+
# @param [Array<Array<DeployNamedArgument>>] args
|
13
|
+
def initialize(name, version, entry_point, args)
|
14
|
+
@tag = 4
|
15
|
+
@name = name
|
16
|
+
@version = version
|
17
|
+
@entry_point = entry_point
|
18
|
+
@args = args
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_tag
|
22
|
+
@tag
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_name
|
26
|
+
@name
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_version
|
30
|
+
@version
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_entry_point
|
34
|
+
@entry_point
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_args
|
38
|
+
@args
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_bytes
|
42
|
+
serializer = DeployNamedArgSerializer.new
|
43
|
+
num_of_args = @args.length
|
44
|
+
bytes = Utils::ByteUtils.to_u8(@tag)
|
45
|
+
bytes += CLValueBytesParsers::CLStringBytesParser.to_bytes(@name)
|
46
|
+
if @version == nil
|
47
|
+
bytes += Utils::ByteUtils.to_u8(0)
|
48
|
+
else
|
49
|
+
bytes += Utils::ByteUtils.to_u8(1)
|
50
|
+
end
|
51
|
+
bytes += CLValueBytesParsers::CLStringBytesParser.to_bytes(@entry_point)
|
52
|
+
bytes += Utils::ByteUtils.to_u32(num_of_args)
|
53
|
+
@args.each do |arg|
|
54
|
+
arg.each do |item|
|
55
|
+
bytes += serializer.to_bytes(item)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
Utils::ByteUtils.hex_to_byte_array(bytes)
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Casper
|
2
|
+
module Entity
|
3
|
+
# Represents a transfer from one purse to another
|
4
|
+
class Transfer
|
5
|
+
|
6
|
+
# @param [Hash] transfer
|
7
|
+
# @option transfer [String] :deploy_hash
|
8
|
+
# @option transfer [String] :from
|
9
|
+
# @option transfer [String] :to
|
10
|
+
# @option transfer [String] :source
|
11
|
+
# @option transfer [String] :target
|
12
|
+
# @option transfer [String] :amount
|
13
|
+
# @option transfer [String] :gas
|
14
|
+
# @option transfer [Integer] :id
|
15
|
+
def initialize(transfer = {})
|
16
|
+
@deploy_hash = transfer[:deploy_hash]
|
17
|
+
@from = transfer[:from]
|
18
|
+
@to = transfer[:to]
|
19
|
+
@source = transfer[:source]
|
20
|
+
@target = transfer[:target]
|
21
|
+
@amount = transfer[:amount]
|
22
|
+
@gas = transfer[:gas]
|
23
|
+
@id = transfer[:id]
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [String] deploy_hash
|
27
|
+
def get_deploy_hash
|
28
|
+
@deploy_hash
|
29
|
+
end
|
30
|
+
|
31
|
+
# @return [String] from
|
32
|
+
def get_from
|
33
|
+
@from
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [String] to
|
37
|
+
def get_to
|
38
|
+
@to
|
39
|
+
end
|
40
|
+
|
41
|
+
# @return [String] source
|
42
|
+
def get_source
|
43
|
+
@source
|
44
|
+
end
|
45
|
+
|
46
|
+
# @return [String] target
|
47
|
+
def get_target
|
48
|
+
@target
|
49
|
+
end
|
50
|
+
|
51
|
+
# @return [String] amount
|
52
|
+
def get_amount
|
53
|
+
@amount
|
54
|
+
end
|
55
|
+
|
56
|
+
# @return [String] gas
|
57
|
+
def get_gas
|
58
|
+
@gas
|
59
|
+
end
|
60
|
+
# @return [Integer] id
|
61
|
+
def get_id
|
62
|
+
@id
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Casper
|
2
|
+
module Entity
|
3
|
+
|
4
|
+
# Vesting schedule for a genesis validator.
|
5
|
+
class VestingSchedule
|
6
|
+
|
7
|
+
def initialize(vesting_schedule = {})
|
8
|
+
@initial_release_timestamp_millis = vesting_schedule[:initial_release_timestamp_millis]
|
9
|
+
@locked_amounts = vesting_schedule[:locked_amounts]
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_initial_release_timestamp_millis
|
13
|
+
@initial_release_timestamp_millis
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_locked_amounts
|
17
|
+
@locked_amounts
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/include.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Dir["./types/*.rb"].each {|file| require file }
|
2
|
+
Dir[File.join(
|
3
|
+
File.dirname(File.dirname(File.absolute_path(__FILE__))), "/lib/types/*.rb")].each {|file| require file }
|
4
|
+
Dir[File.join(
|
5
|
+
File.dirname(File.dirname(File.absolute_path(__FILE__))), "/lib/entity/*.rb")].each {|file| require file }
|
6
|
+
Dir[File.join(
|
7
|
+
File.dirname(File.dirname(File.absolute_path(__FILE__))), "/lib/rpc/*.rb")].each {|file| require file }
|
8
|
+
Dir[File.join(
|
9
|
+
File.dirname(File.dirname(File.absolute_path(__FILE__))), "/lib/utils/*.rb")].each {|file| require file }
|
10
|
+
# path = File.join(
|
11
|
+
# File.dirname(File.dirname(File.absolute_path(__FILE__))),
|
12
|
+
# '/lib/types/cl_bool'
|
13
|
+
# )
|
14
|
+
# puts path
|
15
|
+
# require path
|
16
|
+
# require_relative "./types/cl_bool.rb"
|
17
|
+
# require_relative "./types/cl_string.rb"
|
18
|
+
|
data/lib/rpc/rpc.rb
ADDED
@@ -0,0 +1,227 @@
|
|
1
|
+
# casper_network.rb
|
2
|
+
require 'jimson'
|
3
|
+
require 'json'
|
4
|
+
require 'rdoc/rdoc'
|
5
|
+
require "ipaddress"
|
6
|
+
require 'resolv'
|
7
|
+
require 'rest-client'
|
8
|
+
require 'active_support/core_ext/hash/keys'
|
9
|
+
require 'timeout'
|
10
|
+
require_relative '../entity/peer.rb'
|
11
|
+
module Casper
|
12
|
+
# Class for interacting with the network via RPC
|
13
|
+
class RpcClient
|
14
|
+
attr_accessor :ip_address, :port, :url, :state_root_hash
|
15
|
+
|
16
|
+
# Constructor
|
17
|
+
# @param [String] ip_address
|
18
|
+
def initialize(ip_address)
|
19
|
+
@ip_address = ip_address
|
20
|
+
@url = "http://" + @ip_address + ":7777/rpc"
|
21
|
+
@state_root_hash = ""
|
22
|
+
@peer_array = []
|
23
|
+
@block_hash = ""
|
24
|
+
@deploy = {}
|
25
|
+
@node_status = {}
|
26
|
+
@block_transfers = []
|
27
|
+
@block_info = {}
|
28
|
+
@era_summary = {}
|
29
|
+
@balance_value = ""
|
30
|
+
@auction_state = {}
|
31
|
+
|
32
|
+
@peers = []
|
33
|
+
end
|
34
|
+
|
35
|
+
# @return [Array<Hash>] peers array
|
36
|
+
def info_get_peers
|
37
|
+
begin
|
38
|
+
status = Timeout::timeout(20) {
|
39
|
+
client = Jimson::Client.new(@url)
|
40
|
+
response = client.info_get_peers
|
41
|
+
response.deep_symbolize_keys!
|
42
|
+
@peer_array = response[:peers]
|
43
|
+
@peer_array.map { |item| @peers << Casper::Entity::Peer.new(item) }
|
44
|
+
# @peers[0].get_node_id
|
45
|
+
# @peers[0].get_node_id
|
46
|
+
# @peers[0].get_address
|
47
|
+
@peer_array
|
48
|
+
}
|
49
|
+
rescue Timeout::Error
|
50
|
+
'Timeout expired to retrieve peers!'
|
51
|
+
end
|
52
|
+
end
|
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
|
+
'Timeout expired to retrieve state_root_hash value!'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Get information about a single deploy by hash.
|
68
|
+
# @param [String] deploy_hash
|
69
|
+
# @return [Hash] Deploy
|
70
|
+
def info_get_deploy(deploy_hash)
|
71
|
+
begin
|
72
|
+
status = Timeout::timeout(20) {
|
73
|
+
if (deploy_hash == "" || deploy_hash == nil)
|
74
|
+
return "Server error -32602: Invalid params"
|
75
|
+
end
|
76
|
+
client = Jimson::Client.new(@url)
|
77
|
+
response = client.info_get_deploy({"deploy_hash"=> deploy_hash })
|
78
|
+
@deploy = response["deploy"]
|
79
|
+
# @deploy.keys.each do |key|
|
80
|
+
# @deploy[(key.to_sym rescue key) || key] = @deploy.delete(key)
|
81
|
+
# end
|
82
|
+
@deploy
|
83
|
+
}
|
84
|
+
rescue
|
85
|
+
'Timeout expired to retrieve Deploy!'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
# Receive node status information
|
91
|
+
# @return node_status
|
92
|
+
def info_get_status
|
93
|
+
begin
|
94
|
+
status = Timeout::timeout(10) {
|
95
|
+
client = Jimson::Client.new(@url)
|
96
|
+
@node_status = client.info_get_status
|
97
|
+
}
|
98
|
+
rescue
|
99
|
+
'Timeout expired to retrieve node status information'
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# @param [String] block_hash
|
104
|
+
# @return [Array<Hash>] block_transfers
|
105
|
+
def chain_get_block_transfers(block_hash = "")
|
106
|
+
begin
|
107
|
+
status = Timeout::timeout(5) {
|
108
|
+
client = Jimson::Client.new(@url)
|
109
|
+
response = client.chain_get_block_transfers({
|
110
|
+
"block_identifier" => {"Hash" => block_hash}
|
111
|
+
})
|
112
|
+
@block_transfers = response["transfers"]
|
113
|
+
@block_transfers
|
114
|
+
}
|
115
|
+
rescue
|
116
|
+
'Timeout expired to retrieve block_transfers'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# @param [String] block_hash
|
121
|
+
# @return [Hash] block_info
|
122
|
+
def chain_get_block(block_hash)
|
123
|
+
begin
|
124
|
+
state = Timeout::timeout(10) {
|
125
|
+
client = Jimson::Client.new(@url)
|
126
|
+
result = client.chain_get_block({"block_identifier" => {"Hash" => block_hash}})
|
127
|
+
@block_info = result["block"]
|
128
|
+
if (!@block_info.empty?() && @block_info["hash"] != block_hash)
|
129
|
+
raise("Returned block does not have a matching hash.")
|
130
|
+
else
|
131
|
+
@block_info
|
132
|
+
end
|
133
|
+
}
|
134
|
+
rescue
|
135
|
+
'Timeout expired to retrieve block_info'
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# @param [String] block_hash
|
140
|
+
# @return [Hash] era_summary
|
141
|
+
def chain_get_eraInfo_by_SwitchBlock(block_hash)
|
142
|
+
begin
|
143
|
+
state = Timeout::timeout(60) {
|
144
|
+
client = Jimson::Client.new(@url)
|
145
|
+
response = client.chain_get_era_info_by_switch_block("block_identifier" => {"Hash" => block_hash})
|
146
|
+
@era_summary = response["era_summary"]
|
147
|
+
@era_summary
|
148
|
+
}
|
149
|
+
rescue
|
150
|
+
'Timeout expired to retrieve era_summary'
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# @param [String] state_root_hash
|
155
|
+
# @param [String] key
|
156
|
+
# @param [Array] path
|
157
|
+
def state_get_item(state_root_hash, key, path)
|
158
|
+
begin
|
159
|
+
state = Timeout::timeout(20) {
|
160
|
+
client = Jimson::Client.new(@url)
|
161
|
+
response = client.state_get_item({
|
162
|
+
"state_root_hash" => state_root_hash,
|
163
|
+
"key" => key,
|
164
|
+
"path" => path
|
165
|
+
})
|
166
|
+
@stored_value = response["stored_value"]
|
167
|
+
@stored_value
|
168
|
+
}
|
169
|
+
rescue
|
170
|
+
'Timeout expired to retrieve stored_value'
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# @param [String] state_root_hash
|
175
|
+
# @param [String] item_key
|
176
|
+
# @param [String] uref
|
177
|
+
def state_get_dictionary_item(state_root_hash, item_key, uref)
|
178
|
+
begin
|
179
|
+
state = Timeout::timeout(5) {
|
180
|
+
client = Jimson::Client.new(@url)
|
181
|
+
response = client.state_get_dictionary_item({
|
182
|
+
"state_root_hash" => state_root_hash,
|
183
|
+
"dictionary_identifier" => {'URef' =>
|
184
|
+
{'seed_uref' => uref, 'dictionary_item_key' => item_key} }})
|
185
|
+
@stored_value = response["stored_value"]
|
186
|
+
@stored_value
|
187
|
+
}
|
188
|
+
rescue
|
189
|
+
'Timeout expired to retrieve stored_value'
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
# @param [String] state_root_hash
|
194
|
+
# @param [String] balance_uref
|
195
|
+
def state_get_balance(state_root_hash, balance_uref)
|
196
|
+
begin
|
197
|
+
state = Timeout::timeout(5) {
|
198
|
+
client = Jimson::Client.new(@url)
|
199
|
+
response = client.state_get_balance({
|
200
|
+
"state_root_hash" => state_root_hash,
|
201
|
+
"purse_uref" => balance_uref
|
202
|
+
})
|
203
|
+
@balance_value = response["balance_value"]
|
204
|
+
@balance_value
|
205
|
+
}
|
206
|
+
rescue
|
207
|
+
'Timeout expired to retrieve balance_value'
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Returns current auction system contract information.
|
212
|
+
# @return [Hash] auction_state
|
213
|
+
def state_get_AuctionInfo
|
214
|
+
begin
|
215
|
+
state = Timeout::timeout(50) {
|
216
|
+
client = Jimson::Client.new(@url)
|
217
|
+
response = client.state_get_auction_info
|
218
|
+
@auction_state = response['auction_state']
|
219
|
+
@auction_state
|
220
|
+
}
|
221
|
+
rescue
|
222
|
+
'Timeout expired to retrieve auction_state information!'
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|