scale.rb 0.2.18 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ module Scale
2
+
3
+ class TypeRegistry
4
+ include Singleton
5
+
6
+ # init by load, and will not change
7
+ attr_reader :spec_name, :types
8
+ attr_reader :versioning, :custom_types # optional
9
+
10
+ # will change by different spec version
11
+ attr_accessor :spec_version # optional
12
+ attr_accessor :metadata
13
+
14
+ def load(spec_name: nil, custom_types: nil)
15
+ @spec_name = nil
16
+ @types = nil
17
+ @versioning = nil
18
+ @custom_types = nil
19
+
20
+ default_types, _, _ = load_chain_spec_types("default")
21
+
22
+ if spec_name
23
+ begin
24
+ @spec_name = spec_name
25
+ spec_types, @versioning, @spec_version = load_chain_spec_types(spec_name)
26
+ @types = default_types.merge(spec_types)
27
+ rescue => ex
28
+ # TODO: check different errors
29
+ Scale::Types.logger.error "There is no types json file named #{spec_name}"
30
+ @types = default_types
31
+ end
32
+ else
33
+ @spec_name = "default"
34
+ @types = default_types
35
+ end
36
+
37
+ self.custom_types = custom_types
38
+ true
39
+ end
40
+
41
+ def get(type_name)
42
+ all_types = self.all_types
43
+ type_traverse(type_name, all_types)
44
+ end
45
+
46
+ def custom_types=(custom_types)
47
+ @custom_types = custom_types.stringify_keys if (not custom_types.nil?) && custom_types.class.name == "Hash"
48
+ end
49
+
50
+ def all_types
51
+ all_types = {}.merge(@types)
52
+
53
+ if @spec_version && @versioning
54
+ @versioning.each do |item|
55
+ if @spec_version >= item["runtime_range"][0] &&
56
+ ( item["runtime_range"][1].nil? || @spec_version <= item["runtime_range"][1] )
57
+ all_types.merge!(item["types"])
58
+ end
59
+ end
60
+ end
61
+
62
+ all_types.merge!(@custom_types) if @custom_types
63
+ all_types
64
+ end
65
+
66
+ private
67
+
68
+ def load_chain_spec_types(spec_name)
69
+ file = File.join File.expand_path("../..", __FILE__), "lib", "type_registry", "#{spec_name}.json"
70
+ json_string = File.open(file).read
71
+ json = JSON.parse(json_string)
72
+
73
+ runtime_id = json["runtime_id"]
74
+
75
+ [json["types"], json["versioning"], runtime_id]
76
+ end
77
+
78
+ def type_traverse(type, types)
79
+ if type.class == ::String
80
+ real_type = types[type]
81
+
82
+ return type if real_type.nil? || real_type == type
83
+
84
+ type_traverse(real_type, types)
85
+ else
86
+ type
87
+ end
88
+ end
89
+ end
90
+
91
+ 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,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.18
4
+ version: 0.3.2
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-04-05 00:00:00.000000000 Z
11
+ date: 2021-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: blake2b_rs
@@ -196,6 +196,7 @@ files:
196
196
  - lib/metadata/metadata_v10.rb
197
197
  - lib/metadata/metadata_v11.rb
198
198
  - lib/metadata/metadata_v12.rb
199
+ - lib/metadata/metadata_v13.rb
199
200
  - lib/metadata/metadata_v2.rb
200
201
  - lib/metadata/metadata_v3.rb
201
202
  - lib/metadata/metadata_v4.rb
@@ -210,7 +211,10 @@ files:
210
211
  - lib/scale/trie.rb
211
212
  - lib/scale/types.rb
212
213
  - lib/scale/version.rb
214
+ - lib/scale_bytes.rb
213
215
  - lib/substrate_client.rb
216
+ - lib/type_builder.rb
217
+ - lib/type_registry.rb
214
218
  - lib/type_registry/crab.json
215
219
  - lib/type_registry/darwinia.json
216
220
  - lib/type_registry/default.json
@@ -221,8 +225,8 @@ files:
221
225
  - scale.gemspec
222
226
  - scripts/block_events.rb
223
227
  - scripts/example.rb
228
+ - scripts/mmr_root_to_sign.rb
224
229
  - src/lib.rs
225
- - src/storage_key.rs
226
230
  homepage: https://github.com/itering/scale.rb
227
231
  licenses:
228
232
  - MIT
data/src/storage_key.rs DELETED
@@ -1,41 +0,0 @@
1
- use frame_support::Twox128;
2
- use parity_scale_codec::Encode;
3
- use frame_support::{Identity, Blake2_128Concat, Twox64Concat};
4
- use frame_support::StorageHasher;
5
-
6
- pub fn to_hex_string(bytes: Vec<u8>) -> String {
7
- let strs: Vec<String> = bytes.iter()
8
- .map(|b| format!("{:02x}", b))
9
- .collect();
10
- strs.join("")
11
- }
12
-
13
- fn main() {
14
- // value
15
- let k = [Twox128::hash(b"Sudo"), Twox128::hash(b"Key")].concat();
16
- println!("{}", to_hex_string(k));
17
-
18
- // map
19
- let mut k = [Twox128::hash(b"ModuleAbc"), Twox128::hash(b"Map1")].concat();
20
- k.extend(vec![1u8, 0, 0, 0].using_encoded(Blake2_128Concat::hash));
21
- println!("{}", to_hex_string(k));
22
-
23
- let mut k = [Twox128::hash(b"ModuleAbc"), Twox128::hash(b"Map2")].concat();
24
- k.extend(1u32.using_encoded(Twox64Concat::hash));
25
- println!("{}", to_hex_string(k));
26
-
27
- let mut k = [Twox128::hash(b"ModuleAbc"), Twox128::hash(b"Map3")].concat();
28
- k.extend(1u32.using_encoded(Identity::hash));
29
- println!("{}", to_hex_string(k));
30
-
31
- // double map
32
- let mut k = [Twox128::hash(b"ModuleAbc"), Twox128::hash(b"DoubleMap1")].concat();
33
- k.extend(1u32.using_encoded(Blake2_128Concat::hash));
34
- k.extend(2u32.using_encoded(Blake2_128Concat::hash));
35
- println!("{}", to_hex_string(k));
36
-
37
- let mut k = [Twox128::hash(b"ModuleAbc"), Twox128::hash(b"DoubleMap2")].concat();
38
- k.extend(1u32.using_encoded(Blake2_128Concat::hash));
39
- k.extend(2u32.using_encoded(Twox64Concat::hash));
40
- println!("{}", to_hex_string(k));
41
- }