scale.rb 0.2.12 → 0.2.17

Sign up to get free protection for your applications and to get access to all the features.
data/scale.gemspec CHANGED
@@ -33,14 +33,15 @@ 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.9"
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 "activesupport", ">= 4.0.0"
39
- spec.add_dependency "thor", "~> 0.19.0"
40
+ spec.add_dependency "faye-websocket"
40
41
 
41
- spec.add_development_dependency "bundler", "~> 1.17"
42
+ spec.add_development_dependency "bundler"
42
43
  spec.add_development_dependency "pry"
43
44
  spec.add_development_dependency "rake", "~> 13.0"
44
45
  spec.add_development_dependency "rspec", "~> 3.2"
45
- spec.add_development_dependency "ffi", "~> 1.12"
46
+ spec.add_development_dependency "ffi", "~> 1.15.0"
46
47
  end
@@ -2,7 +2,6 @@ require "scale"
2
2
 
3
3
 
4
4
  client = SubstrateClient.new("ws://127.0.0.1:9944/")
5
- client.init
6
5
  metadata = client.get_metadata nil
7
6
 
8
7
  the_module = metadata.get_module("CertificateModule")
@@ -23,7 +22,7 @@ puts "CertificateModule storages:"
23
22
  puts "---------------------------------------"
24
23
  puts the_module[:storage][:items]
25
24
 
26
- # Scale::TypeRegistry.instance.metadata = metadata.value
25
+ # Scale::TypeRegistry.instance.metadata = metadata
27
26
  # puts metadata.value.event_index["0400"][1]
28
27
  # puts metadata.value.event_index["0401"][1]
29
28
  # puts metadata.value.event_index["0402"][1]
@@ -31,4 +30,4 @@ puts the_module[:storage][:items]
31
30
  # scale_bytes = Scale::Bytes.new(hex_events)
32
31
  # Scale::Types.get("Vec<EventRecord>").decode(scale_bytes).value.each do |er|
33
32
  # puts er.value
34
- # end
33
+ # end
data/src/lib.rs CHANGED
@@ -1,6 +1,17 @@
1
1
  extern crate parity_scale_codec;
2
2
  use std::slice;
3
- use parity_scale_codec::{Decode};
3
+ use parity_scale_codec::{Encode, Decode};
4
+ use frame_support::Twox128;
5
+ use frame_support::Blake2_128Concat;
6
+ use frame_support::StorageHasher;
7
+
8
+ fn to_u8_vec(v_pointer: *const u8, len: usize) -> Vec<u8> {
9
+ let data_slice = unsafe {
10
+ assert!(!v_pointer.is_null());
11
+ slice::from_raw_parts(v_pointer, len)
12
+ };
13
+ data_slice.to_vec()
14
+ }
4
15
 
5
16
  fn decode_from_raw_parts<T: Decode + PartialEq + std::fmt::Debug>(v_pointer: *const u8, len: usize) -> T {
6
17
  let data_slice = unsafe {
@@ -49,6 +60,36 @@ pub extern fn parse_opt_bool(v_pointer: *const u8, len: usize, inner_value: bool
49
60
  assert_eq!(decode_from_raw_parts::<Option<bool>>(v_pointer, len), expectation);
50
61
  }
51
62
 
63
+ #[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);
75
+ }
76
+
77
+ #[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);
91
+ }
92
+
52
93
  #[test]
53
94
  fn opt_bool_is_broken()
54
95
  {
@@ -0,0 +1,41 @@
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
+ }
metadata CHANGED
@@ -1,85 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scale.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.12
4
+ version: 0.2.17
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: 2020-11-01 00:00:00.000000000 Z
11
+ date: 2021-03-22 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.9
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.9
26
+ version: 0.1.2
27
27
  - !ruby/object:Gem::Dependency
28
- name: json
28
+ name: xxhash
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 2.3.0
33
+ version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 2.3.0
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: activesupport
42
+ name: base58
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 4.0.0
47
+ version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 4.0.0
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: thor
56
+ name: json
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.19.0
61
+ version: 2.3.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.19.0
68
+ version: 2.3.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: faye-websocket
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bundler
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - "~>"
87
+ - - ">="
74
88
  - !ruby/object:Gem::Version
75
- version: '1.17'
89
+ version: '0'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - "~>"
94
+ - - ">="
81
95
  - !ruby/object:Gem::Version
82
- version: '1.17'
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: pry
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -128,23 +142,21 @@ dependencies:
128
142
  requirements:
129
143
  - - "~>"
130
144
  - !ruby/object:Gem::Version
131
- version: '1.12'
145
+ version: 1.15.0
132
146
  type: :development
133
147
  prerelease: false
134
148
  version_requirements: !ruby/object:Gem::Requirement
135
149
  requirements:
136
150
  - - "~>"
137
151
  - !ruby/object:Gem::Version
138
- version: '1.12'
152
+ version: 1.15.0
139
153
  description: Ruby implementation of the parity SCALE data format
140
154
  email:
141
155
  - wuminzhe@gmail.com
142
- executables:
143
- - scale
156
+ executables: []
144
157
  extensions: []
145
158
  extra_rdoc_files: []
146
159
  files:
147
- - ".DS_Store"
148
160
  - ".dockerignore"
149
161
  - ".gitignore"
150
162
  - Cargo.lock
@@ -158,8 +170,10 @@ files:
158
170
  - Rakefile
159
171
  - bin/console
160
172
  - bin/setup
161
- - exe/scale
162
173
  - grants_badge.png
174
+ - lib/address.rb
175
+ - lib/common.rb
176
+ - lib/helper.rb
163
177
  - lib/metadata/metadata.rb
164
178
  - lib/metadata/metadata_v0.rb
165
179
  - lib/metadata/metadata_v1.rb
@@ -177,8 +191,11 @@ files:
177
191
  - lib/scale.rb
178
192
  - lib/scale/base.rb
179
193
  - lib/scale/block.rb
194
+ - lib/scale/trie.rb
180
195
  - lib/scale/types.rb
181
196
  - lib/scale/version.rb
197
+ - lib/substrate_client.rb
198
+ - lib/type_registry/crab.json
182
199
  - lib/type_registry/darwinia.json
183
200
  - lib/type_registry/default.json
184
201
  - lib/type_registry/edgeware.json
@@ -194,13 +211,14 @@ files:
194
211
  - scripts/block_events.rb
195
212
  - scripts/example.rb
196
213
  - src/lib.rs
214
+ - src/storage_key.rs
197
215
  homepage: https://github.com/itering/scale.rb
198
216
  licenses:
199
217
  - MIT
200
218
  metadata:
201
219
  allowed_push_host: https://rubygems.org
202
220
  homepage_uri: https://github.com/itering/scale.rb
203
- post_install_message:
221
+ post_install_message:
204
222
  rdoc_options: []
205
223
  require_paths:
206
224
  - lib
@@ -215,8 +233,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
233
  - !ruby/object:Gem::Version
216
234
  version: '0'
217
235
  requirements: []
218
- rubygems_version: 3.0.6
219
- signing_key:
236
+ rubygems_version: 3.1.4
237
+ signing_key:
220
238
  specification_version: 4
221
239
  summary: Ruby SCALE Codec Library
222
240
  test_files: []
data/.DS_Store DELETED
Binary file
data/exe/scale DELETED
@@ -1,80 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'scale'
4
- require 'thor'
5
-
6
- class ScaleCli < Thor
7
- desc "specs", "list all chain specs"
8
- def specs
9
- path = File.join File.expand_path('../..', __FILE__), "lib", "type_registry", "*.json"
10
- specs = Dir[path].map do |file|
11
- File.basename file, ".json"
12
- end
13
- puts specs.join(", ")
14
- end
15
-
16
- # exmaples:
17
- #
18
- # scale types darwinia
19
- # =>
20
- # ProposalCategory => Scale::Types::ProposalCategory
21
- # VoteStage => Scale::Types::VoteStage
22
- # TallyType => Scale::Types::TallyType
23
- # ...
24
- #
25
- # Count: 279
26
- desc "types SPEC_NAME SPEC_VERSION", "list all types implemented for chain"
27
- def types(spec_name = "default", spec_version = nil)
28
- Scale::TypeRegistry.instance.load(spec_name, spec_version)
29
- Scale::Types.list.each_pair do |type_name, type|
30
- puts "#{green(type_name)} => #{yellow(type.to_s)}"
31
- end
32
-
33
- puts "\nCount: #{Scale::Types.list.length}"
34
- end
35
-
36
- # exmaples:
37
- #
38
- # scale type Compact
39
- # => Scale::Types::Compact
40
- #
41
- # scale type SessionKeysPolkadot kusama
42
- # => nil
43
- #
44
- # scale type SessionKeysPolkadot kusama 1054
45
- # => Scale::Types::Struct_Of_AccountId_AccountId_AccountId_AccountId_AccountId_70247479160460
46
- #
47
- # scale type "UnappliedSlash<AccountId, BalanceOf>"
48
- # => Scale::Types::Struct_Of_AccountId_AccountId_Vec˂UnappliedSlashOther˃_Vec˂AccountId˃_Balance_70145184872260
49
- #
50
- desc "type TYPE_NAME SPEC_NAME SPEC_VERSION", "show type's ruby class"
51
- def type(type_name, spec_name = "default", spec_version = nil)
52
- Scale::TypeRegistry.instance.load(spec_name, spec_version)
53
- p Scale::Types.get(type_name)
54
- end
55
-
56
- # exmaples:
57
- #
58
- # scale decode Compact 0x0300000040
59
- # => #<Scale::Types::Compact:0x00007fc86b9d1198 @value=1073741824>
60
- desc "decode TYPE_NAME HEX SPEC_NAME SPEC_VERSION", "decode HEX string using TYPE_NAME"
61
- def decode(type_name, hex, spec_name = "default", spec_version = nil)
62
- Scale::TypeRegistry.instance.load(spec_name, spec_version)
63
- type = Scale::Types.get(type_name)
64
- scale_bytes = Scale::Bytes.new(hex)
65
- p type.decode(scale_bytes)
66
- end
67
-
68
- # exmaples:
69
- #
70
- # scale encode SessionIndex 2818
71
- # => #<Scale::Types::Compact:0x00007fc86b9d1198 @value=1073741824>
72
- desc "encode TYPE_NAME VALUE SPEC_NAME SPEC_VERSION", "encode value"
73
- def encode(type_name, value, spec_name = "default", spec_version = nil)
74
- Scale::TypeRegistry.instance.load(spec_name, spec_version)
75
- type = Scale::Types.get(type_name)
76
- p "0x" + type.new(value.to_i).encode
77
- end
78
- end
79
-
80
- ScaleCli.start(ARGV)