scale.rb 0.2.17 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Cargo.lock +8 -4
  4. data/Cargo.toml +2 -3
  5. data/Dockerfile +4 -1
  6. data/Gemfile.lock +23 -21
  7. data/README.md +44 -1
  8. data/Rakefile +6 -0
  9. data/exe/scale +49 -0
  10. data/lib/helper.rb +19 -4
  11. data/lib/metadata/metadata.rb +27 -17
  12. data/lib/metadata/metadata_v0.rb +24 -20
  13. data/lib/metadata/metadata_v1.rb +13 -9
  14. data/lib/metadata/metadata_v10.rb +2 -2
  15. data/lib/metadata/metadata_v11.rb +2 -2
  16. data/lib/metadata/metadata_v12.rb +9 -8
  17. data/lib/metadata/metadata_v13.rb +161 -0
  18. data/lib/metadata/metadata_v2.rb +2 -2
  19. data/lib/metadata/metadata_v3.rb +2 -2
  20. data/lib/metadata/metadata_v4.rb +21 -11
  21. data/lib/metadata/metadata_v5.rb +21 -11
  22. data/lib/metadata/metadata_v6.rb +9 -9
  23. data/lib/metadata/metadata_v7.rb +26 -15
  24. data/lib/metadata/metadata_v8.rb +9 -9
  25. data/lib/metadata/metadata_v9.rb +2 -2
  26. data/lib/scale.rb +40 -339
  27. data/lib/scale/base.rb +175 -93
  28. data/lib/scale/block.rb +10 -10
  29. data/lib/scale/trie.rb +1 -1
  30. data/lib/scale/types.rb +139 -40
  31. data/lib/scale/version.rb +1 -1
  32. data/lib/scale_bytes.rb +63 -0
  33. data/lib/substrate_client.rb +11 -8
  34. data/lib/type_builder.rb +280 -0
  35. data/lib/type_registry.rb +91 -0
  36. data/lib/type_registry/crab.json +676 -595
  37. data/lib/type_registry/darwinia.json +730 -554
  38. data/lib/type_registry/default.json +3 -2
  39. data/lib/type_registry/pangolin.json +771 -0
  40. data/scale.gemspec +1 -0
  41. data/scripts/mmr_root_to_sign.rb +10 -0
  42. data/src/lib.rs +80 -25
  43. metadata +25 -10
  44. data/lib/type_registry/edgeware.json +0 -124
  45. data/lib/type_registry/joystream.json +0 -49
  46. data/lib/type_registry/kulupu.json +0 -15
  47. data/lib/type_registry/plasm.json +0 -89
  48. data/lib/type_registry/robonomics.json +0 -39
  49. data/lib/type_registry/westend.json +0 -63
  50. data/src/storage_key.rs +0 -41
data/scale.gemspec CHANGED
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
38
38
  spec.add_dependency "base58"
39
39
  spec.add_dependency "json", "~> 2.3.0"
40
40
  spec.add_dependency "faye-websocket"
41
+ spec.add_dependency "thor", '~> 1.0'
41
42
 
42
43
  spec.add_development_dependency "bundler"
43
44
  spec.add_development_dependency "pry"
@@ -0,0 +1,10 @@
1
+ client = SubstrateClient.new "wss://pangolin-rpc.darwinia.network"
2
+
3
+ begin
4
+ block_number = Scale::Types.get("BlockNumberFor").new(670820)
5
+ puts client.get_storage("EthereumRelayAuthorities", "MmrRootsToSign", [block_number]).to_human
6
+ rescue => ex
7
+ puts ex.message
8
+ puts ex.backtrace
9
+ end
10
+
data/src/lib.rs CHANGED
@@ -1,10 +1,15 @@
1
1
  extern crate parity_scale_codec;
2
2
  use std::slice;
3
- use parity_scale_codec::{Encode, Decode};
3
+ use parity_scale_codec::Decode;
4
4
  use frame_support::Twox128;
5
+ use frame_support::Twox64Concat;
6
+ use frame_support::Identity;
5
7
  use frame_support::Blake2_128Concat;
6
8
  use frame_support::StorageHasher;
7
9
 
10
+ use libc::c_char;
11
+ use std::ffi::{CStr, CString};
12
+
8
13
  fn to_u8_vec(v_pointer: *const u8, len: usize) -> Vec<u8> {
9
14
  let data_slice = unsafe {
10
15
  assert!(!v_pointer.is_null());
@@ -13,6 +18,38 @@ fn to_u8_vec(v_pointer: *const u8, len: usize) -> Vec<u8> {
13
18
  data_slice.to_vec()
14
19
  }
15
20
 
21
+ fn to_string(str_p: *const c_char) -> String {
22
+ let s = unsafe {
23
+ assert!(!str_p.is_null());
24
+ CStr::from_ptr(str_p)
25
+ };
26
+
27
+ s.to_str().unwrap().to_string()
28
+ }
29
+
30
+ fn v8_vec_to_pointer(key: Vec<u8>) -> *const c_char {
31
+ let key = CString::new(hex::encode(key)).unwrap();
32
+ let result = key.as_ptr();
33
+ std::mem::forget(key);
34
+ result
35
+ }
36
+
37
+ fn gen_param_hash(param_pointer: *const u8, param_len: usize, param_hasher: *const c_char) -> Vec<u8> {
38
+ let param_hasher = to_string(param_hasher);
39
+ let param_hasher = param_hasher.as_str();
40
+ let param = to_u8_vec(param_pointer, param_len);
41
+
42
+ if param_hasher == "blake2_128_concat" {
43
+ Blake2_128Concat::hash(&param).to_vec()
44
+ } else if param_hasher == "twox64_concat" {
45
+ Twox64Concat::hash(&param).to_vec()
46
+ } else if param_hasher == "identity" {
47
+ Identity::hash(&param).to_vec()
48
+ } else {
49
+ panic!("Not supported hasher type")
50
+ }
51
+ }
52
+
16
53
  fn decode_from_raw_parts<T: Decode + PartialEq + std::fmt::Debug>(v_pointer: *const u8, len: usize) -> T {
17
54
  let data_slice = unsafe {
18
55
  assert!(!v_pointer.is_null());
@@ -61,33 +98,51 @@ pub extern fn parse_opt_bool(v_pointer: *const u8, len: usize, inner_value: bool
61
98
  }
62
99
 
63
100
  #[no_mangle]
64
- pub extern fn assert_storage_key_for_value(
65
- mv_pointer: *const u8, mv_len: usize,
66
- sv_pointer: *const u8, sv_len: usize,
67
- ev_pointer: *const u8, ev_len: usize
68
- ) {
69
- let m = to_u8_vec(mv_pointer, mv_len);
70
- let s = to_u8_vec(sv_pointer, sv_len);
71
- let e = to_u8_vec(ev_pointer, ev_len);
72
-
73
- let k = [Twox128::hash(&m), Twox128::hash(&s)].concat();
74
- assert_eq!(k, e);
101
+ pub extern fn get_storage_key_for_value(
102
+ module_name: *const c_char,
103
+ storage_name: *const c_char,
104
+ ) -> *const c_char {
105
+ let module_name = to_string(module_name);
106
+ let storage_name = to_string(storage_name);
107
+
108
+ let storage_key = [Twox128::hash(module_name.as_bytes()), Twox128::hash(storage_name.as_bytes())].concat();
109
+ v8_vec_to_pointer(storage_key)
110
+ }
111
+
112
+ #[no_mangle]
113
+ pub extern fn get_storage_key_for_map(
114
+ module_name: *const c_char,
115
+ storage_name: *const c_char,
116
+ param_pointer: *const u8, param_len: usize, param_hasher: *const c_char,
117
+ ) -> *const c_char {
118
+ let module_name = to_string(module_name);
119
+ let storage_name = to_string(storage_name);
120
+ let mut storage_key = [Twox128::hash(module_name.as_bytes()), Twox128::hash(storage_name.as_bytes())].concat();
121
+
122
+ let param_hash = gen_param_hash(param_pointer, param_len, param_hasher);
123
+ storage_key.extend(param_hash);
124
+
125
+ v8_vec_to_pointer(storage_key)
75
126
  }
76
127
 
77
128
  #[no_mangle]
78
- pub extern fn assert_storage_key_for_map_black2128concat(
79
- mv_pointer: *const u8, mv_len: usize,
80
- sv_pointer: *const u8, sv_len: usize,
81
- pv_pointer: *const u8, pv_len: usize,
82
- ev_pointer: *const u8, ev_len: usize,
83
- ) {
84
- let m = to_u8_vec(mv_pointer, mv_len);
85
- let s = to_u8_vec(sv_pointer, sv_len);
86
- let p = to_u8_vec(pv_pointer, pv_len);
87
- let e = to_u8_vec(ev_pointer, ev_len);
88
- let mut k = [Twox128::hash(&m), Twox128::hash(&s)].concat();
89
- k.extend(p.using_encoded(Blake2_128Concat::hash));
90
- assert_eq!(k, e);
129
+ pub extern fn get_storage_key_for_double_map(
130
+ module_name: *const c_char,
131
+ storage_name: *const c_char,
132
+ param1_pointer: *const u8, param1_len: usize, param1_hasher: *const c_char,
133
+ param2_pointer: *const u8, param2_len: usize, param2_hasher: *const c_char,
134
+ ) -> *const c_char {
135
+ let module_name = to_string(module_name);
136
+ let storage_name = to_string(storage_name);
137
+ let mut storage_key = [Twox128::hash(module_name.as_bytes()), Twox128::hash(storage_name.as_bytes())].concat();
138
+
139
+ let param1_hash = gen_param_hash(param1_pointer, param1_len, param1_hasher);
140
+ storage_key.extend(param1_hash);
141
+
142
+ let param2_hash = gen_param_hash(param2_pointer, param2_len, param2_hasher);
143
+ storage_key.extend(param2_hash);
144
+
145
+ v8_vec_to_pointer(storage_key)
91
146
  }
92
147
 
93
148
  #[test]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scale.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.17
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wu Minzhe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-22 00:00:00.000000000 Z
11
+ date: 2021-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: blake2b_rs
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: thor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: bundler
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -153,7 +167,8 @@ dependencies:
153
167
  description: Ruby implementation of the parity SCALE data format
154
168
  email:
155
169
  - wuminzhe@gmail.com
156
- executables: []
170
+ executables:
171
+ - scale
157
172
  extensions: []
158
173
  extra_rdoc_files: []
159
174
  files:
@@ -170,6 +185,7 @@ files:
170
185
  - Rakefile
171
186
  - bin/console
172
187
  - bin/setup
188
+ - exe/scale
173
189
  - grants_badge.png
174
190
  - lib/address.rb
175
191
  - lib/common.rb
@@ -180,6 +196,7 @@ files:
180
196
  - lib/metadata/metadata_v10.rb
181
197
  - lib/metadata/metadata_v11.rb
182
198
  - lib/metadata/metadata_v12.rb
199
+ - lib/metadata/metadata_v13.rb
183
200
  - lib/metadata/metadata_v2.rb
184
201
  - lib/metadata/metadata_v3.rb
185
202
  - lib/metadata/metadata_v4.rb
@@ -194,24 +211,22 @@ files:
194
211
  - lib/scale/trie.rb
195
212
  - lib/scale/types.rb
196
213
  - lib/scale/version.rb
214
+ - lib/scale_bytes.rb
197
215
  - lib/substrate_client.rb
216
+ - lib/type_builder.rb
217
+ - lib/type_registry.rb
198
218
  - lib/type_registry/crab.json
199
219
  - lib/type_registry/darwinia.json
200
220
  - lib/type_registry/default.json
201
- - lib/type_registry/edgeware.json
202
- - lib/type_registry/joystream.json
203
- - lib/type_registry/kulupu.json
204
221
  - lib/type_registry/kusama.json
205
- - lib/type_registry/plasm.json
222
+ - lib/type_registry/pangolin.json
206
223
  - lib/type_registry/polkadot.json
207
- - lib/type_registry/robonomics.json
208
224
  - lib/type_registry/test.json
209
- - lib/type_registry/westend.json
210
225
  - scale.gemspec
211
226
  - scripts/block_events.rb
212
227
  - scripts/example.rb
228
+ - scripts/mmr_root_to_sign.rb
213
229
  - src/lib.rs
214
- - src/storage_key.rs
215
230
  homepage: https://github.com/itering/scale.rb
216
231
  licenses:
217
232
  - MIT
@@ -1,124 +0,0 @@
1
- {
2
- "runtime_id": 27,
3
- "types": {
4
- "Keys": {
5
- "type": "struct",
6
- "type_mapping": [
7
- ["grandpa", "AccountId"],
8
- ["babe", "AccountId"],
9
- ["im_online", "AccountId"],
10
- ["authority_discovery", "AccountId"]
11
- ]
12
- },
13
- "BlockNumber": "U32",
14
- "SessionIndex": "U32",
15
- "AuctionIndex": "U32",
16
- "AuthIndex": "U32",
17
- "BalanceUpload": "(AccountId, u64)",
18
- "EgressQueueRoot": "(ParaId, Hash)",
19
- "EventIndex": "u32",
20
- "LeasePeriodOf": "LeasePeriod",
21
- "LeasePeriod": "BlockNumber",
22
- "MemberCount": "u32",
23
- "MomentOf": "Moment",
24
- "Offender": "(ValidatorId, Exposure)",
25
- "ReportIdOf": "Hash",
26
- "SubId": "u32",
27
- "Weight": "u32",
28
- "WeightMultiplier": "u64",
29
- "WinningData": "Vec<WinningDataEntry>",
30
- "Index": "U32",
31
- "Kind": "[u8; 16]",
32
- "OpaqueTimeSlot": "Bytes",
33
- "Box<<T as Trait<I>>::Proposal>": "BoxProposal",
34
- "Forcing": {
35
- "type": "enum",
36
- "value_list": [
37
- "NotForcing",
38
- "ForceNew",
39
- "ForceNone"
40
- ]
41
- },
42
- "VoteData": {
43
- "type": "struct",
44
- "type_mapping": [
45
- ["initiator", "AccountId"],
46
- ["stage", "VoteStage"],
47
- ["vote_type", "VoteType"],
48
- ["tally_type", "TallyType"],
49
- ["is_commit_reveal", "Bool"]
50
- ]
51
- },
52
- "VoteRecord": {
53
- "type": "struct",
54
- "type_mapping": [
55
- ["id", "u64"],
56
- ["commitments", "Vec<(AccountId, VoteOutcome)>"],
57
- ["reveals", "Vec<(AccountId, VoteOutcome)>"],
58
- ["data", "VoteData"],
59
- ["outcomes", "Vec<(VoteOutcome)>"]
60
- ]
61
- },
62
- "VoteType": {
63
- "type": "enum",
64
- "value_list": ["Binary", "MultiOption"]
65
- },
66
- "IdentityStage": {
67
- "type": "enum",
68
- "value_list": ["Registered", "Attested", "Verified"]
69
- },
70
- "IdentityRecord": {
71
- "type": "struct",
72
- "type_mapping": [
73
- ["account", "AccountId"],
74
- ["identity_type", "String"],
75
- ["identity", "Bytes"],
76
- ["stage", "IdentityStage"],
77
- ["expiration_time", "BlockNumber"],
78
- ["proof", "Bytes"],
79
- ["metadata", "Bytes"]
80
- ]
81
- },
82
- "MetadataRecord": {
83
- "type": "struct",
84
- "type_mapping": [
85
- ["avatar", "String"],
86
- ["display_name", "String"],
87
- ["tagline", "String"]
88
- ]
89
- },
90
- "ProposalCategory": {
91
- "type": "enum",
92
- "value_list": ["Signaling"]
93
- },
94
- "ProposalStage": {
95
- "type": "enum",
96
- "value_list": ["PreVoting", "Voting", "Completed"]
97
- },
98
- "ProposalRecord": {
99
- "type": "struct",
100
- "type_mapping": [
101
- ["index", "u32"],
102
- ["author", "AccountId"],
103
- ["stage", "ProposalStage"],
104
- ["transition_time", "BlockNumber"],
105
- ["category", "ProposalCategory"],
106
- ["title", "Vec<u8>"],
107
- ["contents", "Vec<u8>"],
108
- ["vote_id", "u64"],
109
- ["ProposalTitle", "Vec<u8>"],
110
- ["ProposalContents", "Vec<u8>"]
111
- ]
112
- },
113
- "Balance2": "Balance",
114
- "ReferendumInfo<BlockNumber, Proposal>": {
115
- "type": "struct",
116
- "type_mapping": [
117
- ["end", "BlockNumber"],
118
- ["proposal", "Proposal"],
119
- ["threshold", "VoteThreshold"],
120
- ["delay", "BlockNumber"]
121
- ]
122
- }
123
- }
124
- }
@@ -1,49 +0,0 @@
1
- {
2
- "runtime_id": 4,
3
- "types": {
4
- "ValidatorPrefs": "ValidatorPrefsLegacy",
5
- "Index": "U64",
6
- "<T as DDTrait>::ContentId": "ContentId",
7
- "RoleParameters": {
8
- "type": "struct",
9
- "type_mapping": [
10
- ["min_stake", "BalanceOf"],
11
- ["min_actors", "u32"],
12
- ["max_actors", "u32"],
13
- ["reward", "BalanceOf"],
14
- ["reward_period", "BlockNumber"],
15
- ["bonding_period", "BlockNumber"],
16
- ["unbonding_period", "BlockNumber"],
17
- ["min_service_period", "BlockNumber"],
18
- ["startup_grace_period", "BlockNumber"],
19
- ["entry_request_fee", "BalanceOf"]
20
- ]
21
- },
22
- "Actor": {
23
- "type": "struct",
24
- "type_mapping": [
25
- ["member_id", "MemberId"],
26
- ["role", "Role"],
27
- ["account", "AccountId"],
28
- ["joined_at", "BlockNumber"]
29
- ]
30
- },
31
- "DataObjectStorageRelationship": {
32
- "type": "struct",
33
- "type_mapping": [
34
- ["content_id", "ContentId"],
35
- ["storage_provider", "AccountId"],
36
- ["ready", "bool"]
37
- ]
38
- },
39
- "DataObjectType": {
40
- "type": "struct",
41
- "type_mapping": [
42
- ["description", "Vec<u8>"],
43
- ["active", "bool"]
44
- ]
45
- },
46
- "DataObjectTypeId": "u64",
47
- "<T as DOTRTrait>::DataObjectTypeId": "u64"
48
- }
49
- }
@@ -1,15 +0,0 @@
1
- {
2
- "runtime_id": 3,
3
- "types": {
4
- "BlockNumber": "U32",
5
- "Weight": "u64",
6
- "DispatchInfo": {
7
- "type": "struct",
8
- "type_mapping": [
9
- ["weight", "Weight"],
10
- ["class", "DispatchClass"],
11
- ["paysFee", "Pays"]
12
- ]
13
- }
14
- }
15
- }
@@ -1,89 +0,0 @@
1
- {
2
- "types": {
3
- "Keys": {
4
- "type": "struct",
5
- "type_mapping": [
6
- ["grandpa", "AccountId"],
7
- ["babe", "AccountId"]
8
- ]
9
- },
10
- "Balance": "u128",
11
- "Index": "U32",
12
- "Era": "[u8; 2]",
13
- "BlockNumber": "U32",
14
- "Range": {
15
- "type": "struct",
16
- "type_mapping": [
17
- ["start", "u128"],
18
- ["end", "u128"]
19
- ]
20
- },
21
- "StateObject": {
22
- "type": "struct",
23
- "type_mapping": [
24
- ["predicate", "AccountId"],
25
- ["data", "Vec<u8>"]
26
- ]
27
- },
28
- "StateUpdate": {
29
- "type": "struct",
30
- "type_mapping": [
31
- ["range", "Range"],
32
- ["state_object", "StateObject"],
33
- ["plasma_block_number", "BlockNumber"]
34
- ]
35
- },
36
- "Checkpoint": {
37
- "type": "struct",
38
- "type_mapping": [
39
- ["state_update", "StateUpdate"],
40
- ["sub_range", "Range"]
41
- ]
42
- },
43
- "Transaction": {
44
- "type": "struct",
45
- "type_mapping": [
46
- ["predicate", "AccountId"],
47
- ["range", "Range"],
48
- ["body", "Range"]
49
- ]
50
- },
51
- "TransactionBody": {
52
- "type": "struct",
53
- "type_mapping": [
54
- ["new_state", "StateObject"],
55
- ["origin_block", "BlockNumber"],
56
- ["max_block", "BlockNumber"]
57
- ]
58
- },
59
- "Challenge": {
60
- "type": "struct",
61
- "type_mapping": [
62
- ["challenged_checkpoint", "Checkpoint"],
63
- ["challenging_checkpoint", "Checkpoint"]
64
- ]
65
- },
66
- "MerkleIntervalTreeInternalNode": {
67
- "type": "struct",
68
- "type_mapping": [
69
- ["index", "Index"],
70
- ["hash", "Hash"]
71
- ]
72
- },
73
- "InclusionProof": {
74
- "type": "struct",
75
- "type_mapping": [
76
- ["proofs", "Vec<MerkleIntervalTreeInternalNode>"],
77
- ["idx", "u128"]
78
- ]
79
- },
80
- "Parameters": {
81
- "type": "struct",
82
- "type_mapping": [
83
- ["can_be_nominated", "bool"],
84
- ["option_expired", "u128"],
85
- ["option_p", "u32"]
86
- ]
87
- }
88
- }
89
- }