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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c509663910bc3e5dd790fd40e4e0d04ac4f589c44a0253b8b1b353b17687d82
4
- data.tar.gz: 30249e26671d18d7568c58df2926a5aa071a4ca7b25af443315c8a92a65d292f
3
+ metadata.gz: 45e42a3b0f9d6934412cf21cf4eff6576846b914e1f93f136ba5cf9188cca349
4
+ data.tar.gz: 905d9f7d1fdeed1ef44050e5d187e775fed65e322acd9e0d3ed6c638b8676f40
5
5
  SHA512:
6
- metadata.gz: 4ce9f292eeca2be4bc9f2967f9806730bedfd2fc14b4837a6d3cbea2808f62fbadffd31a40e218968ad2bac886338359bb01c164ed9a781186d78f54dbfb7293
7
- data.tar.gz: 590a67087cdd8fe40717a0ec255bf0c824e807eec1f5f476c856b5002280f7680f0534559648e3ec3470a66194fd9fa141a1b1a277bbebc8dea15cc838027c5c
6
+ metadata.gz: 15a61c8fe8bc16594feff4c4fe6898bb9a826a3a1bb22eec7f6f483cb5f2a053d6ac69a6431a2bcf8ce41cfbf41a04860799c2d625b232953be0386a948464e7
7
+ data.tar.gz: e74c876481194337664d0e0df5ed1862452262cb7beb2e575912a043e24268c404b55ada4ee3b908f07511b029ad3399d63894e0503ce6b9fa04f0a35ebac735
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  /.idea
11
11
  /target
12
12
  .DS_Store
13
+ /scripts/tmp.rb
data/Cargo.lock CHANGED
@@ -1,5 +1,7 @@
1
1
  # This file is automatically @generated by Cargo.
2
2
  # It is not intended for manual editing.
3
+ version = 3
4
+
3
5
  [[package]]
4
6
  name = "Inflector"
5
7
  version = "0.11.4"
@@ -15,6 +17,8 @@ name = "SCALE-testing-interface"
15
17
  version = "0.1.0"
16
18
  dependencies = [
17
19
  "frame-support",
20
+ "hex",
21
+ "libc",
18
22
  "parity-scale-codec",
19
23
  "sp-core",
20
24
  ]
@@ -642,9 +646,9 @@ dependencies = [
642
646
 
643
647
  [[package]]
644
648
  name = "hex"
645
- version = "0.4.2"
649
+ version = "0.4.3"
646
650
  source = "registry+https://github.com/rust-lang/crates.io-index"
647
- checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35"
651
+ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
648
652
 
649
653
  [[package]]
650
654
  name = "hmac"
@@ -744,9 +748,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
744
748
 
745
749
  [[package]]
746
750
  name = "libc"
747
- version = "0.2.82"
751
+ version = "0.2.94"
748
752
  source = "registry+https://github.com/rust-lang/crates.io-index"
749
- checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929"
753
+ checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
750
754
 
751
755
  [[package]]
752
756
  name = "libsecp256k1"
data/Cargo.toml CHANGED
@@ -8,11 +8,10 @@ edition = "2018"
8
8
  parity-scale-codec = { version = "1.3.6" }
9
9
  sp-core = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", features = ["full_crypto"]}
10
10
  frame-support = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git" }
11
+ libc = { version = "0.2.94" }
12
+ hex = { version = "0.4.3" }
11
13
 
12
14
  [lib]
13
15
  name = "scale_ffi"
14
16
  crate-type = ["dylib"]
15
17
 
16
- [[bin]]
17
- name = "storage_key"
18
- path = "src/storage_key.rs"
data/Dockerfile CHANGED
@@ -15,7 +15,10 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
15
15
  export RUSTFLAGS='-C target-feature=-crt-static' && \
16
16
  make
17
17
 
18
- RUN gem install bundler:1.17.3 && \
18
+ ENV RUSTFLAGS='-C target-feature=-crt-static'
19
+ ENV PATH=/root/.cargo/bin:$PATH
20
+
21
+ RUN gem install bundler:2.2.13 && \
19
22
  bundle install && \
20
23
  rake install:local
21
24
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scale.rb (0.2.18)
4
+ scale.rb (0.3.2)
5
5
  base58
6
6
  blake2b_rs (~> 0.1.2)
7
7
  faye-websocket
@@ -19,7 +19,7 @@ GEM
19
19
  coderay (1.1.3)
20
20
  diff-lcs (1.4.4)
21
21
  eventmachine (1.2.7)
22
- faye-websocket (0.11.0)
22
+ faye-websocket (0.11.1)
23
23
  eventmachine (>= 0.12.0)
24
24
  websocket-driver (>= 0.5.1)
25
25
  ffi (1.15.0)
@@ -49,7 +49,7 @@ GEM
49
49
  tomlrb (~> 1.2)
50
50
  thor (1.1.0)
51
51
  tomlrb (1.3.0)
52
- websocket-driver (0.7.3)
52
+ websocket-driver (0.7.5)
53
53
  websocket-extensions (>= 0.1.0)
54
54
  websocket-extensions (0.1.5)
55
55
  xxhash (0.4.0)
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  ![grants_badge](./grants_badge.png)
2
2
 
3
+ **Warning: After v0.2.19, the develop branch will do a structure refactor and big optimization.**
4
+
3
5
  # scale.rb
4
6
 
5
7
  **Ruby SCALE Codec Library**
@@ -19,7 +21,7 @@ Because the feature of ruby 2.6 is used, the ruby version is required to be >= 2
19
21
  Add this line to your application's Gemfile:
20
22
 
21
23
  ```ruby
22
- gem 'scale.rb', '0.2.18'
24
+ gem 'scale.rb', '0.2.19'
23
25
  ```
24
26
 
25
27
  And then execute:
@@ -73,9 +75,23 @@ p o.encode # "080001"
73
75
  3. client
74
76
  ```ruby
75
77
  require "scale"
76
-
77
- client = SubstrateClient.new "wss://pangolin-rpc.darwinia.network"
78
- client.get_storage("EthereumRelay", "ConfirmedHeaderParcels", [0])
78
+ client = SubstrateClient.new "wss://rpc.darwinia.network"
79
+
80
+ v = Scale::Types.get("EthereumTransactionIndex")
81
+ .new(
82
+ [
83
+ Scale::Types::H256.new("0x803054c2beacabc36e15c3147bb87d8320a02e9b601be28820a622dedd1c7717"),
84
+ Scale::Types::U64.new(266)
85
+ ]
86
+ )
87
+
88
+ storage = client.get_storage("EthereumBacking", "VerifiedProof", [v])
89
+ puts storage.to_human
90
+
91
+ # get the raw data
92
+ key = client.generate_storage_key("EthereumBacking", "VerifiedProof", [v])[0]
93
+ storage_raw = client.state_getStorageAt(key)
94
+ puts storage_raw
79
95
 
80
96
  ```
81
97
  Please go to `spec` dir for more examples.
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require 'bundler/gem_tasks'
2
+ require "scale"
2
3
  task default: :spec
4
+
5
+ desc 'Check types of a spec'
6
+ task :check_types do
7
+ TypeRegistry.instance.load spec_name: 'darwinia'
8
+ end
data/lib/helper.rb CHANGED
@@ -18,16 +18,30 @@ class SubstrateClient::Helper
18
18
  return_type = map[:value]
19
19
  # TODO: decode to account id if param is address
20
20
  # params[0] = decode(params[0]) if map[:key] == "AccountId"
21
+
21
22
  type = Scale::Types.get(map[:key])
22
- params[0] = type.new(params[0]).encode
23
+ if params[0].class != type
24
+ raise Scale::StorageInputTypeError.new("The type of first param is not equal to the type from metadata: #{map[:key]} => #{type}")
25
+ end
26
+ params[0] = params[0].encode
23
27
  elsif map = storage_item[:type][:DoubleMap]
24
28
  raise "Storage call of type \"DoubleMapType\" requires 2 parameters" if params.nil? || params.length != 2
25
29
 
26
30
  hasher = map[:hasher]
27
31
  hasher2 = map[:key2Hasher]
28
32
  return_type = map[:value]
29
- params[0] = Scale::Types.get(map[:key1]).new(params[0]).encode
30
- params[1] = Scale::Types.get(map[:key2]).new(params[1]).encode
33
+
34
+ type1 = Scale::Types.get(map[:key1])
35
+ if params[0].class != type1
36
+ raise Scale::StorageInputTypeError.new("The type of 1st param is not equal to the type from metadata: #{map[:key1]} => #{type1.class.name}")
37
+ end
38
+ params[0] = params[0].encode
39
+
40
+ type2 = Scale::Types.get(map[:key2])
41
+ if params[1].class != type2
42
+ raise Scale::StorageInputTypeError.new("The type of 2nd param is not equal to the type from metadata: #{map[:key2]} => #{type2.class.name}")
43
+ end
44
+ params[1] = params[1].encode
31
45
  else
32
46
  raise NotImplementedError
33
47
  end
@@ -41,7 +55,8 @@ class SubstrateClient::Helper
41
55
  hasher2,
42
56
  metadata.value.value[:metadata][:version]
43
57
  )
44
- [storage_key, return_type]
58
+ storage_modifier = storage_item[:modifier]
59
+ [storage_key, return_type, storage_item]
45
60
  end
46
61
 
47
62
  def generate_storage_key(module_name, storage_name, params = nil, hasher = nil, hasher2 = nil, metadata_version = nil)
@@ -1,12 +1,16 @@
1
1
  module Scale
2
2
  module Types
3
3
  class Metadata
4
- include SingleValue
4
+ include Base
5
5
  attr_accessor :version
6
6
  def self.decode(scale_bytes)
7
7
  bytes = scale_bytes.get_next_bytes(4)
8
8
  if bytes.bytes_to_utf8 == "meta"
9
- metadata_version = Scale::Types.type_of("Enum", %w[MetadataV0 MetadataV1 MetadataV2 MetadataV3 MetadataV4 MetadataV5 MetadataV6 MetadataV7 MetadataV8 MetadataV9 MetadataV10 MetadataV11 MetadataV12]).decode(scale_bytes).value
9
+ version_enum = {
10
+ "type" => "enum",
11
+ "value_list" => %w[MetadataV0 MetadataV1 MetadataV2 MetadataV3 MetadataV4 MetadataV5 MetadataV6 MetadataV7 MetadataV8 MetadataV9 MetadataV10 MetadataV11 MetadataV12 MetadataV13]
12
+ }
13
+ metadata_version = Scale::Types.get(version_enum).decode(scale_bytes).value
10
14
  metadata = Metadata.new "Scale::Types::#{metadata_version}".constantize2.decode(scale_bytes)
11
15
  metadata.version = metadata_version[9..].to_i
12
16
  else
@@ -48,7 +52,7 @@ module Scale
48
52
  end
49
53
 
50
54
  class MetadataModule
51
- include SingleValue
55
+ include Base
52
56
  def self.decode(scale_bytes)
53
57
  name = String.decode(scale_bytes).value
54
58
  prefix = String.decode(scale_bytes).value
@@ -60,19 +64,19 @@ module Scale
60
64
 
61
65
  has_storage = Bool.decode(scale_bytes).value
62
66
  if has_storage
63
- storages = Scale::Types.type_of("Vec<MetadataModuleStorage>").decode(scale_bytes).value
67
+ storages = Scale::Types.get("Vec<MetadataModuleStorage>").decode(scale_bytes).value
64
68
  result[:storage] = storages.map(&:value)
65
69
  end
66
70
 
67
71
  has_calls = Bool.decode(scale_bytes).value
68
72
  if has_calls
69
- calls = Scale::Types.type_of("Vec<MetadataModuleCall>").decode(scale_bytes).value
73
+ calls = Scale::Types.get("Vec<MetadataModuleCall>").decode(scale_bytes).value
70
74
  result[:calls] = calls.map(&:value)
71
75
  end
72
76
 
73
77
  has_events = Bool.decode(scale_bytes).value
74
78
  if has_events
75
- events = Scale::Types.type_of("Vec<MetadataModuleEvent>").decode(scale_bytes).value
79
+ events = Scale::Types.get("Vec<MetadataModuleEvent>").decode(scale_bytes).value
76
80
  result[:events] = events.map(&:value)
77
81
  end
78
82
 
@@ -85,11 +89,17 @@ module Scale
85
89
  end
86
90
 
87
91
  class MetadataModuleStorage
88
- include SingleValue
92
+ include Base
89
93
  def self.decode(scale_bytes)
94
+ name = String.decode(scale_bytes).value
95
+ enum = {
96
+ "type" => "enum",
97
+ "value_list" => ["Optional", "Default"]
98
+ }
99
+ modifier = Scale::Types.get(enum).decode(scale_bytes).value
90
100
  result = {
91
- name: String.decode(scale_bytes).value,
92
- modifier: Scale::Types.type_of("Enum", %w[Optional Default]).decode(scale_bytes).value
101
+ name: name,
102
+ modifier: modifier
93
103
  }
94
104
 
95
105
  is_key_value = Bool.decode(scale_bytes).value
@@ -108,25 +118,25 @@ module Scale
108
118
  end
109
119
 
110
120
  result[:fallback] = Hex.decode(scale_bytes).value
111
- result[:documentation] = Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
121
+ result[:documentation] = Scale::Types.get("Vec<String>").decode(scale_bytes).value.map(&:value)
112
122
 
113
123
  MetadataModuleStorage.new(result)
114
124
  end
115
125
  end
116
126
 
117
127
  class MetadataModuleCall
118
- include SingleValue
128
+ include Base
119
129
  def self.decode(scale_bytes)
120
130
  result = {}
121
131
  result[:name] = String.decode(scale_bytes).value
122
- result[:args] = Scale::Types.type_of("Vec<MetadataModuleCallArgument>").decode(scale_bytes).value.map(&:value)
123
- result[:documentation] = Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
132
+ result[:args] = Scale::Types.get("Vec<MetadataModuleCallArgument>").decode(scale_bytes).value.map(&:value)
133
+ result[:documentation] = Scale::Types.get("Vec<String>").decode(scale_bytes).value.map(&:value)
124
134
  MetadataModuleCall.new(result)
125
135
  end
126
136
  end
127
137
 
128
138
  class MetadataModuleCallArgument
129
- include SingleValue
139
+ include Base
130
140
  def self.decode(scale_bytes)
131
141
  result = {}
132
142
  result[:name] = String.decode(scale_bytes).value
@@ -137,12 +147,12 @@ module Scale
137
147
  end
138
148
 
139
149
  class MetadataModuleEvent
140
- include SingleValue
150
+ include Base
141
151
  def self.decode(scale_bytes)
142
152
  result = {}
143
153
  result[:name] = String.decode(scale_bytes).value
144
- result[:args] = Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
145
- result[:documentation] = Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
154
+ result[:args] = Scale::Types.get("Vec<String>").decode(scale_bytes).value.map(&:value)
155
+ result[:documentation] = Scale::Types.get("Vec<String>").decode(scale_bytes).value.map(&:value)
146
156
 
147
157
  MetadataModuleEvent.new(result)
148
158
  end
@@ -1,7 +1,7 @@
1
1
  module Scale
2
2
  module Types
3
3
  class MetadataV0
4
- include SingleValue
4
+ include Base
5
5
  attr_accessor :call_index, :event_index
6
6
 
7
7
  def initialize(value)
@@ -26,12 +26,12 @@ module Scale
26
26
  }
27
27
  }
28
28
 
29
- events_modules = Scale::Types.type_of("Vec<MetadataV0EventModule>").decode(scale_bytes).value.map(&:value)
30
- modules = Scale::Types.type_of("Vec<MetadataV0Module>").decode(scale_bytes).value.map(&:value)
29
+ events_modules = Scale::Types.get("Vec<MetadataV0EventModule>").decode(scale_bytes).value.map(&:value)
30
+ modules = Scale::Types.get("Vec<MetadataV0Module>").decode(scale_bytes).value.map(&:value)
31
31
 
32
32
  Bytes.decode(scale_bytes).value
33
33
 
34
- sections = Scale::Types.type_of("Vec<MetadataV0Section>").decode(scale_bytes).value.map(&:value)
34
+ sections = Scale::Types.get("Vec<MetadataV0Section>").decode(scale_bytes).value.map(&:value)
35
35
 
36
36
  value[:metadata][:outerEvent][:events] = events_modules
37
37
  value[:metadata][:modules] = modules
@@ -65,11 +65,11 @@ module Scale
65
65
  end
66
66
 
67
67
  class MetadataV0EventModule
68
- include SingleValue
68
+ include Base
69
69
 
70
70
  def self.decode(scale_bytes)
71
71
  name = Bytes.decode(scale_bytes).value
72
- events = Scale::Types.type_of('Vec<MetadataV0Event>').decode(scale_bytes).value.map(&:value)
72
+ events = Scale::Types.get('Vec<MetadataV0Event>').decode(scale_bytes).value.map(&:value)
73
73
  MetadataV0EventModule.new([
74
74
  name,
75
75
  events
@@ -78,25 +78,25 @@ module Scale
78
78
  end
79
79
 
80
80
  class MetadataV0Event
81
- include SingleValue
81
+ include Base
82
82
 
83
83
  def self.decode(scale_bytes)
84
84
  name = Bytes.decode(scale_bytes).value
85
- args = Scale::Types.type_of("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
86
- documentation = Scale::Types.type_of("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
85
+ args = Scale::Types.get("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
86
+ documentation = Scale::Types.get("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
87
87
  MetadataV0Event.new({name: name, args: args.map {|arg| arg }, documentation: documentation})
88
88
  end
89
89
  end
90
90
 
91
91
  class MetadataV0Module
92
- include SingleValue
92
+ include Base
93
93
 
94
94
  def self.decode(scale_bytes)
95
95
  prefix = Bytes.decode(scale_bytes).value
96
96
  name = Bytes.decode(scale_bytes).value
97
97
  call_name = Bytes.decode(scale_bytes).value
98
98
 
99
- functions = Scale::Types.type_of("Vec<MetadataV0ModuleFunction>").decode(scale_bytes).value.map(&:value)
99
+ functions = Scale::Types.get("Vec<MetadataV0ModuleFunction>").decode(scale_bytes).value.map(&:value)
100
100
 
101
101
  result = {
102
102
  prefix: prefix,
@@ -112,7 +112,7 @@ module Scale
112
112
  has_storage = Bool.decode(scale_bytes).value
113
113
  if has_storage
114
114
  storage_prefix = Bytes.decode(scale_bytes).value
115
- storage = Scale::Types.type_of("Vec<MetadataV0ModuleStorage>").decode(scale_bytes).value.map(&:value)
115
+ storage = Scale::Types.get("Vec<MetadataV0ModuleStorage>").decode(scale_bytes).value.map(&:value)
116
116
  result[:storage] = {
117
117
  prefix: storage_prefix,
118
118
  functions: storage
@@ -124,13 +124,13 @@ module Scale
124
124
  end
125
125
 
126
126
  class MetadataV0ModuleFunction
127
- include SingleValue
127
+ include Base
128
128
 
129
129
  def self.decode(scale_bytes)
130
130
  id = U16.decode(scale_bytes).value
131
131
  name = Bytes.decode(scale_bytes).value
132
- args = Scale::Types.type_of("Vec<MetadataV0ModuleCallArgument>").decode(scale_bytes).value.map(&:value)
133
- documentation = Scale::Types.type_of("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
132
+ args = Scale::Types.get("Vec<MetadataV0ModuleCallArgument>").decode(scale_bytes).value.map(&:value)
133
+ documentation = Scale::Types.get("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
134
134
  MetadataV0ModuleFunction.new({
135
135
  id: id,
136
136
  name: name,
@@ -141,7 +141,7 @@ module Scale
141
141
  end
142
142
 
143
143
  class MetadataV0ModuleCallArgument
144
- include SingleValue
144
+ include Base
145
145
 
146
146
  def self.decode(scale_bytes)
147
147
  name = Bytes.decode(scale_bytes).value
@@ -152,11 +152,15 @@ module Scale
152
152
  end
153
153
 
154
154
  class MetadataV0ModuleStorage
155
- include SingleValue
155
+ include Base
156
156
 
157
157
  def self.decode(scale_bytes)
158
158
  name = Bytes.decode(scale_bytes).value
159
- modifier = Scale::Types.type_of("Enum", ["Optional", "Default"]).decode(scale_bytes).value
159
+ enum = {
160
+ "type" => "enum",
161
+ "value_list" => ["Optional", "Default"]
162
+ }
163
+ modifier = Scale::Types.get(enum).decode(scale_bytes).value
160
164
 
161
165
  is_key_value = Bool.decode(scale_bytes).value
162
166
 
@@ -174,7 +178,7 @@ module Scale
174
178
  end
175
179
 
176
180
  fallback = Hex.decode(scale_bytes).value
177
- documentation = Scale::Types.type_of("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
181
+ documentation = Scale::Types.get("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
178
182
 
179
183
  MetadataV0ModuleStorage.new({
180
184
  name: name,
@@ -187,7 +191,7 @@ module Scale
187
191
  end
188
192
 
189
193
  class MetadataV0Section
190
- include SingleValue
194
+ include Base
191
195
 
192
196
  def self.decode(scale_bytes)
193
197
  name = Bytes.decode(scale_bytes).value