scale.rb 0.2.16 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) 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 +43 -35
  7. data/README.md +44 -1
  8. data/Rakefile +6 -0
  9. data/exe/scale +39 -79
  10. data/lib/address.rb +3 -0
  11. data/lib/common.rb +163 -0
  12. data/lib/helper.rb +25 -8
  13. data/lib/metadata/metadata.rb +28 -18
  14. data/lib/metadata/metadata_v0.rb +24 -20
  15. data/lib/metadata/metadata_v1.rb +13 -9
  16. data/lib/metadata/metadata_v10.rb +2 -2
  17. data/lib/metadata/metadata_v11.rb +2 -2
  18. data/lib/metadata/metadata_v12.rb +9 -8
  19. data/lib/metadata/metadata_v13.rb +161 -0
  20. data/lib/metadata/metadata_v2.rb +2 -2
  21. data/lib/metadata/metadata_v3.rb +2 -2
  22. data/lib/metadata/metadata_v4.rb +21 -11
  23. data/lib/metadata/metadata_v5.rb +21 -11
  24. data/lib/metadata/metadata_v6.rb +9 -9
  25. data/lib/metadata/metadata_v7.rb +26 -15
  26. data/lib/metadata/metadata_v8.rb +9 -9
  27. data/lib/metadata/metadata_v9.rb +2 -2
  28. data/lib/scale.rb +41 -341
  29. data/lib/scale/base.rb +177 -95
  30. data/lib/scale/block.rb +17 -13
  31. data/lib/scale/trie.rb +1 -1
  32. data/lib/scale/types.rb +139 -40
  33. data/lib/scale/version.rb +1 -1
  34. data/lib/scale_bytes.rb +63 -0
  35. data/lib/substrate_client.rb +31 -17
  36. data/lib/type_builder.rb +279 -0
  37. data/lib/type_registry.rb +91 -0
  38. data/lib/type_registry/crab.json +676 -595
  39. data/lib/type_registry/darwinia.json +730 -554
  40. data/lib/type_registry/default.json +3 -2
  41. data/lib/type_registry/pangolin.json +771 -0
  42. data/scale.gemspec +7 -5
  43. data/scripts/mmr_root_to_sign.rb +10 -0
  44. data/src/lib.rs +80 -25
  45. metadata +59 -30
  46. data/lib/type_registry/edgeware.json +0 -124
  47. data/lib/type_registry/joystream.json +0 -49
  48. data/lib/type_registry/kulupu.json +0 -15
  49. data/lib/type_registry/plasm.json +0 -89
  50. data/lib/type_registry/robonomics.json +0 -39
  51. data/lib/type_registry/westend.json +0 -63
  52. data/src/storage_key.rs +0 -41
data/scale.gemspec CHANGED
@@ -33,14 +33,16 @@ Gem::Specification.new do |spec|
33
33
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
34
  spec.require_paths = ["lib"]
35
35
 
36
- spec.add_dependency "substrate_common.rb", "~> 0.1.10"
36
+ spec.add_dependency "blake2b_rs", "~> 0.1.2"
37
+ spec.add_dependency "xxhash"
38
+ spec.add_dependency "base58"
37
39
  spec.add_dependency "json", "~> 2.3.0"
38
- spec.add_dependency "thor", "~> 0.19.0"
39
- spec.add_dependency "kontena-websocket-client", "~> 0.1.1"
40
+ spec.add_dependency "faye-websocket"
41
+ spec.add_dependency "thor", '~> 1.0'
40
42
 
41
- spec.add_development_dependency "bundler", "~> 1.17"
43
+ spec.add_development_dependency "bundler"
42
44
  spec.add_development_dependency "pry"
43
45
  spec.add_development_dependency "rake", "~> 13.0"
44
46
  spec.add_development_dependency "rspec", "~> 3.2"
45
- spec.add_development_dependency "ffi", "~> 1.12"
47
+ spec.add_development_dependency "ffi", "~> 1.15.0"
46
48
  end
@@ -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,29 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scale.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.16
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wu Minzhe
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-23 00:00:00.000000000 Z
11
+ date: 2021-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: substrate_common.rb
14
+ name: blake2b_rs
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.10
19
+ version: 0.1.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.10
26
+ version: 0.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: xxhash
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: base58
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: json
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -39,47 +67,47 @@ dependencies:
39
67
  - !ruby/object:Gem::Version
40
68
  version: 2.3.0
41
69
  - !ruby/object:Gem::Dependency
42
- name: thor
70
+ name: faye-websocket
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
- - - "~>"
73
+ - - ">="
46
74
  - !ruby/object:Gem::Version
47
- version: 0.19.0
75
+ version: '0'
48
76
  type: :runtime
49
77
  prerelease: false
50
78
  version_requirements: !ruby/object:Gem::Requirement
51
79
  requirements:
52
- - - "~>"
80
+ - - ">="
53
81
  - !ruby/object:Gem::Version
54
- version: 0.19.0
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
- name: kontena-websocket-client
84
+ name: thor
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
87
  - - "~>"
60
88
  - !ruby/object:Gem::Version
61
- version: 0.1.1
89
+ version: '1.0'
62
90
  type: :runtime
63
91
  prerelease: false
64
92
  version_requirements: !ruby/object:Gem::Requirement
65
93
  requirements:
66
94
  - - "~>"
67
95
  - !ruby/object:Gem::Version
68
- version: 0.1.1
96
+ version: '1.0'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: bundler
71
99
  requirement: !ruby/object:Gem::Requirement
72
100
  requirements:
73
- - - "~>"
101
+ - - ">="
74
102
  - !ruby/object:Gem::Version
75
- version: '1.17'
103
+ version: '0'
76
104
  type: :development
77
105
  prerelease: false
78
106
  version_requirements: !ruby/object:Gem::Requirement
79
107
  requirements:
80
- - - "~>"
108
+ - - ">="
81
109
  - !ruby/object:Gem::Version
82
- version: '1.17'
110
+ version: '0'
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: pry
85
113
  requirement: !ruby/object:Gem::Requirement
@@ -128,14 +156,14 @@ dependencies:
128
156
  requirements:
129
157
  - - "~>"
130
158
  - !ruby/object:Gem::Version
131
- version: '1.12'
159
+ version: 1.15.0
132
160
  type: :development
133
161
  prerelease: false
134
162
  version_requirements: !ruby/object:Gem::Requirement
135
163
  requirements:
136
164
  - - "~>"
137
165
  - !ruby/object:Gem::Version
138
- version: '1.12'
166
+ version: 1.15.0
139
167
  description: Ruby implementation of the parity SCALE data format
140
168
  email:
141
169
  - wuminzhe@gmail.com
@@ -159,6 +187,8 @@ files:
159
187
  - bin/setup
160
188
  - exe/scale
161
189
  - grants_badge.png
190
+ - lib/address.rb
191
+ - lib/common.rb
162
192
  - lib/helper.rb
163
193
  - lib/metadata/metadata.rb
164
194
  - lib/metadata/metadata_v0.rb
@@ -166,6 +196,7 @@ files:
166
196
  - lib/metadata/metadata_v10.rb
167
197
  - lib/metadata/metadata_v11.rb
168
198
  - lib/metadata/metadata_v12.rb
199
+ - lib/metadata/metadata_v13.rb
169
200
  - lib/metadata/metadata_v2.rb
170
201
  - lib/metadata/metadata_v3.rb
171
202
  - lib/metadata/metadata_v4.rb
@@ -180,31 +211,29 @@ files:
180
211
  - lib/scale/trie.rb
181
212
  - lib/scale/types.rb
182
213
  - lib/scale/version.rb
214
+ - lib/scale_bytes.rb
183
215
  - lib/substrate_client.rb
216
+ - lib/type_builder.rb
217
+ - lib/type_registry.rb
184
218
  - lib/type_registry/crab.json
185
219
  - lib/type_registry/darwinia.json
186
220
  - lib/type_registry/default.json
187
- - lib/type_registry/edgeware.json
188
- - lib/type_registry/joystream.json
189
- - lib/type_registry/kulupu.json
190
221
  - lib/type_registry/kusama.json
191
- - lib/type_registry/plasm.json
222
+ - lib/type_registry/pangolin.json
192
223
  - lib/type_registry/polkadot.json
193
- - lib/type_registry/robonomics.json
194
224
  - lib/type_registry/test.json
195
- - lib/type_registry/westend.json
196
225
  - scale.gemspec
197
226
  - scripts/block_events.rb
198
227
  - scripts/example.rb
228
+ - scripts/mmr_root_to_sign.rb
199
229
  - src/lib.rs
200
- - src/storage_key.rs
201
230
  homepage: https://github.com/itering/scale.rb
202
231
  licenses:
203
232
  - MIT
204
233
  metadata:
205
234
  allowed_push_host: https://rubygems.org
206
235
  homepage_uri: https://github.com/itering/scale.rb
207
- post_install_message:
236
+ post_install_message:
208
237
  rdoc_options: []
209
238
  require_paths:
210
239
  - lib
@@ -219,8 +248,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
248
  - !ruby/object:Gem::Version
220
249
  version: '0'
221
250
  requirements: []
222
- rubygems_version: 3.0.6
223
- signing_key:
251
+ rubygems_version: 3.1.4
252
+ signing_key:
224
253
  specification_version: 4
225
254
  summary: Ruby SCALE Codec Library
226
255
  test_files: []
@@ -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
- }