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/lib/scale/block.rb CHANGED
@@ -2,11 +2,11 @@ module Scale
2
2
  module Types
3
3
 
4
4
  class Extrinsic
5
- include SingleValue
5
+ include Base
6
6
  # attr_accessor :address, :signature, :nonce, :era, :extrinsic_hash, :call_index, :params_raw, :params
7
7
 
8
- def self.generate_hash(data)
9
- Blake2b.hex data, Blake2b::Key.none, 32
8
+ def self.generate_hash(bytes)
9
+ Crypto.blake2_256 bytes
10
10
  end
11
11
 
12
12
  def self.decode(scale_bytes)
@@ -61,10 +61,10 @@ module Scale
61
61
 
62
62
  if contains_transaction
63
63
  result[:address] = Scale::Types.get("Address").decode(scale_bytes).value
64
- result[:signature] = scale::types.get("MultiSignature").decode(scale_bytes).value
65
- result[:era] = scale::types.get("era").decode(scale_bytes).value
66
- result[:nonce] = scale::types.get("compact").decode(scale_bytes).value
67
- result[:tip] = scale::types.get("compact").decode(scale_bytes).value
64
+ result[:signature] = Scale::Types.get("MultiSignature").decode(scale_bytes).value
65
+ result[:era] = Scale::Types.get("Era").decode(scale_bytes).value
66
+ result[:nonce] = Scale::Types.get("Compact").decode(scale_bytes).value
67
+ result[:tip] = Scale::Types.get("Compact").decode(scale_bytes).value
68
68
  result[:extrinsic_hash] = generate_hash(scale_bytes.bytes)
69
69
  end
70
70
  result[:call_index] = scale_bytes.get_next_bytes(2).bytes_to_hex[2..]
@@ -116,7 +116,7 @@ module Scale
116
116
  end
117
117
 
118
118
  class EventRecords
119
- include SingleValue
119
+ include Base
120
120
 
121
121
  def self.decode(scale_bytes)
122
122
 
@@ -129,7 +129,7 @@ module Scale
129
129
  end
130
130
 
131
131
  class EventRecord
132
- include SingleValue
132
+ include Base
133
133
 
134
134
  def self.decode(scale_bytes)
135
135
  metadata = Scale::TypeRegistry.instance.metadata.value
@@ -141,9 +141,13 @@ module Scale
141
141
  result[:extrinsic_idx] = U32.decode(scale_bytes).value
142
142
  end
143
143
 
144
- type = scale_bytes.get_next_bytes(2).bytes_to_hex[2..]
145
- event = metadata.event_index[type][1]
146
- # mod = metadata.event_index[type][0]
144
+ index = scale_bytes.get_next_bytes(2).bytes_to_hex[2..]
145
+ event = metadata.event_index[index][1]
146
+ the_module = metadata.event_index[index][0]
147
+
148
+ result[:event_index] = index
149
+ result[:event_metadata] = event
150
+ result[:module_metadata] = the_module
147
151
 
148
152
  result[:params] = []
149
153
  event[:args].each do |arg_type|
@@ -207,7 +211,7 @@ module Scale
207
211
 
208
212
  class LogDigest
209
213
  include Enum
210
- items %w[Other AuthoritiesChange ChangesTrieRoot SealV0 Consensus Seal PreRuntime]
214
+ inner_types *%w[Other AuthoritiesChange ChangesTrieRoot SealV0 Consensus Seal PreRuntime]
211
215
  end
212
216
 
213
217
  end
data/lib/scale/trie.rb CHANGED
@@ -2,7 +2,7 @@ module Scale
2
2
  module Types
3
3
 
4
4
  class TrieNode
5
- include SingleValue
5
+ include Base
6
6
  EMPTY = 0
7
7
  NIBBLE_PER_BYTE = 2
8
8
  BITMAP_LENGTH = 2
data/lib/scale/types.rb CHANGED
@@ -2,15 +2,18 @@ module Scale
2
2
  module Types
3
3
 
4
4
  class Bool
5
- include SingleValue
5
+ include Base
6
6
  BYTES_LENGTH = 1
7
7
 
8
8
  def self.decode(scale_bytes)
9
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
9
10
  bytes = scale_bytes.get_next_bytes(self::BYTES_LENGTH)
10
11
  if bytes == [0]
11
- Bool.new(false)
12
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
13
+ new(false)
12
14
  elsif bytes == [1]
13
- Bool.new(true)
15
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
16
+ new(true)
14
17
  else
15
18
  raise "Bad data"
16
19
  end
@@ -21,6 +24,35 @@ module Scale
21
24
  end
22
25
  end
23
26
 
27
+ class OptionBool
28
+ include Base
29
+ def self.decode(scale_bytes)
30
+ byte = scale_bytes.get_next_bytes(1)[0]
31
+ value = if byte == 0x00
32
+ nil
33
+ elsif byte == 0x01
34
+ true
35
+ elsif byte == 0x02
36
+ false
37
+ else
38
+ raise BadDataError.new("Bad scale data for OptionBool #{byte.to_s(16)}")
39
+ end
40
+ new(value)
41
+ end
42
+
43
+ def encode
44
+ if value.nil?
45
+ "00"
46
+ elsif value === true
47
+ "01"
48
+ elsif value === false
49
+ "02"
50
+ else
51
+ raise WrongValueError.new("OptionBool can not have value other than `nil`, `true`, `false`")
52
+ end
53
+ end
54
+ end
55
+
24
56
  class U8
25
57
  include FixedWidthUInt
26
58
  BYTE_LENGTH = 1
@@ -51,6 +83,11 @@ module Scale
51
83
  BYTE_LENGTH = 32
52
84
  end
53
85
 
86
+ class U512
87
+ include FixedWidthUInt
88
+ BYTE_LENGTH = 64
89
+ end
90
+
54
91
  class I8
55
92
  include FixedWidthInt
56
93
  BYTE_LENGTH = 1
@@ -77,9 +114,10 @@ module Scale
77
114
  end
78
115
 
79
116
  class Compact
80
- include SingleValue
117
+ include Base
81
118
 
82
119
  def self.decode(scale_bytes)
120
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
83
121
  first_byte = scale_bytes.get_next_bytes(1)[0]
84
122
  first_byte_in_bin = first_byte.to_s(2).rjust(8, "0")
85
123
 
@@ -113,7 +151,8 @@ module Scale
113
151
  .to_i(16)
114
152
  end
115
153
 
116
- Compact.new(value)
154
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
155
+ new(value)
117
156
  end
118
157
 
119
158
  def encode
@@ -138,9 +177,10 @@ module Scale
138
177
  end
139
178
 
140
179
  class Bytes
141
- include SingleValue
180
+ include Base
142
181
 
143
182
  def self.decode(scale_bytes)
183
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
144
184
  length = Scale::Types::Compact.decode(scale_bytes).value
145
185
  bytes = scale_bytes.get_next_bytes(length)
146
186
 
@@ -148,9 +188,11 @@ module Scale
148
188
  # => "Café"
149
189
  str = bytes.pack("C*").force_encoding("utf-8")
150
190
  if str.valid_encoding?
151
- Bytes.new str
191
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
192
+ new str
152
193
  else
153
- Bytes.new bytes.bytes_to_hex
194
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
195
+ new bytes.bytes_to_hex
154
196
  end
155
197
  end
156
198
 
@@ -168,29 +210,40 @@ module Scale
168
210
  end
169
211
 
170
212
  class Hex
171
- include SingleValue
213
+ include Base
172
214
 
173
215
  def self.decode(scale_bytes)
216
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
174
217
  length = Scale::Types::Compact.decode(scale_bytes).value
175
218
  hex_string = scale_bytes.get_next_bytes(length).bytes_to_hex
176
- Hex.new(hex_string)
219
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
220
+ new(hex_string)
221
+ end
222
+
223
+ def encode
224
+ length = Compact.new((value.length - 2)/2).encode
225
+ "#{length}#{value[2..]}"
177
226
  end
178
227
  end
179
228
 
180
229
  class String
181
- include SingleValue
230
+ include Base
182
231
  def self.decode(scale_bytes)
232
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
183
233
  length = Scale::Types::Compact.decode(scale_bytes).value
184
234
  bytes = scale_bytes.get_next_bytes(length)
185
- String.new bytes.pack("C*").force_encoding("utf-8")
235
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
236
+ new bytes.pack("C*").force_encoding("utf-8")
186
237
  end
187
238
  end
188
239
 
189
240
  class H160
190
- include SingleValue
241
+ include Base
191
242
  def self.decode(scale_bytes)
243
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
192
244
  bytes = scale_bytes.get_next_bytes(20)
193
- H160.new(bytes.bytes_to_hex)
245
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
246
+ new(bytes.bytes_to_hex)
194
247
  end
195
248
 
196
249
  def encode
@@ -200,10 +253,12 @@ module Scale
200
253
  end
201
254
 
202
255
  class H256
203
- include SingleValue
256
+ include Base
204
257
  def self.decode(scale_bytes)
258
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
205
259
  bytes = scale_bytes.get_next_bytes(32)
206
- H256.new(bytes.bytes_to_hex)
260
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
261
+ new(bytes.bytes_to_hex)
207
262
  end
208
263
 
209
264
  def encode
@@ -213,10 +268,12 @@ module Scale
213
268
  end
214
269
 
215
270
  class H512
216
- include SingleValue
271
+ include Base
217
272
  def self.decode(scale_bytes)
273
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
218
274
  bytes = scale_bytes.get_next_bytes(64)
219
- H512.new(bytes.bytes_to_hex)
275
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
276
+ new(bytes.bytes_to_hex)
220
277
  end
221
278
 
222
279
  def encode
@@ -226,7 +283,7 @@ module Scale
226
283
  end
227
284
 
228
285
  class GenericAddress
229
- include SingleValue
286
+ include Base
230
287
 
231
288
  # https://github.com/paritytech/substrate/wiki/External-Address-Format-(SS58)
232
289
  # base58encode ( concat ( <address-type>, <address>, <checksum> ) )
@@ -234,13 +291,15 @@ module Scale
234
291
  # the <address> is 32 byte account id or 1, 2, 4, 8 byte account index
235
292
  # scale_bytes: account length byte + <address>'s bytes
236
293
  def self.decode(scale_bytes)
294
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
237
295
  account_length = scale_bytes.get_next_bytes(1).first
238
296
 
239
297
  if account_length == 0xff # 32 bytes address(Public key)
240
298
  account_id = scale_bytes.get_next_bytes(32).bytes_to_hex
241
299
  account_length = [account_length].bytes_to_hex
242
300
 
243
- Address.new({
301
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
302
+ new({
244
303
  account_id: account_id,
245
304
  account_length: account_length
246
305
  })
@@ -258,7 +317,8 @@ module Scale
258
317
  # TODO: add account_idx
259
318
  account_length = [account_length].bytes_to_hex
260
319
 
261
- Address.new({
320
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
321
+ new({
262
322
  account_index: account_index,
263
323
  account_length: account_length
264
324
  })
@@ -280,10 +340,14 @@ module Scale
280
340
 
281
341
  class AccountIdAddress < GenericAddress
282
342
  def self.decode(scale_bytes)
283
- AccountIdAddress.new({
343
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
344
+ result = new({
284
345
  account_id: AccountId.decode(scale_bytes).value,
285
346
  account_length: "0xff"
286
347
  })
348
+
349
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
350
+ result
287
351
  end
288
352
 
289
353
  def encode
@@ -291,6 +355,17 @@ module Scale
291
355
  end
292
356
  end
293
357
 
358
+ class GenericMultiAddress
359
+ include Enum
360
+ items(
361
+ Id: "AccountId",
362
+ Index: "Compact",
363
+ Raw: "Hex",
364
+ Address32: "H256",
365
+ Address20: "H160"
366
+ )
367
+ end
368
+
294
369
  class AccountId < H256; end
295
370
 
296
371
  class Balance < U128; end
@@ -302,14 +377,17 @@ module Scale
302
377
  class AccountIndex < U32; end
303
378
 
304
379
  class Era
305
- include SingleValue
380
+ include Base
306
381
  def self.decode(scale_bytes)
382
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
307
383
  byte = scale_bytes.get_next_bytes(1).bytes_to_hex
308
- if byte == "0x00"
309
- Era.new byte
384
+ result = if byte == "0x00"
385
+ new byte
310
386
  else
311
- Era.new byte + scale_bytes.get_next_bytes(1).bytes_to_hex()[2..]
387
+ new byte + scale_bytes.get_next_bytes(1).bytes_to_hex()[2..]
312
388
  end
389
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
390
+ result
313
391
  end
314
392
  end
315
393
 
@@ -318,14 +396,16 @@ module Scale
318
396
  class Moment < U64; end
319
397
 
320
398
  class CompactMoment
321
- include SingleValue
399
+ include Base
322
400
  def self.decode(scale_bytes)
401
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
323
402
  value = Compact.decode(scale_bytes).value
324
403
  if value > 10000000000
325
404
  value /= 1000
326
405
  end
327
406
 
328
- CompactMoment.new Time.at(value).to_datetime.strftime("%F %T")
407
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
408
+ new Time.at(value).strftime("%F %T")
329
409
  end
330
410
  end
331
411
 
@@ -429,9 +509,9 @@ module Scale
429
509
  end
430
510
 
431
511
  class Null
432
- include SingleValue
512
+ include Base
433
513
  def self.decode(scale_bytes)
434
- Null.new nil
514
+ new nil
435
515
  end
436
516
 
437
517
  def encode
@@ -622,18 +702,20 @@ module Scale
622
702
  include Struct
623
703
  items(
624
704
  id: "VecU8Length8",
625
- amount: "Balance",
705
+ amount: "Scale::Types::Balance",
626
706
  until: "U32",
627
- reasons: "WithdrawReasons"
707
+ reasons: "Scale::Types::WithdrawReasons"
628
708
  )
629
709
  end
630
710
 
631
711
  class EthereumAddress
632
- include SingleValue
712
+ include Base
633
713
 
634
714
  def self.decode(scale_bytes)
715
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
635
716
  bytes = scale_bytes.get_next_bytes(20)
636
- EthereumAddress.new(bytes.bytes_to_hex)
717
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
718
+ new(bytes.bytes_to_hex)
637
719
  end
638
720
 
639
721
  def encode
@@ -646,11 +728,13 @@ module Scale
646
728
  end
647
729
 
648
730
  class EcdsaSignature
649
- include SingleValue
731
+ include Base
650
732
 
651
733
  def self.decode(scale_bytes)
734
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
652
735
  bytes = scale_bytes.get_next_bytes(65)
653
- EcdsaSignature.new(bytes.bytes_to_hex)
736
+ new(bytes.bytes_to_hex)
737
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
654
738
  end
655
739
 
656
740
  def encode
@@ -756,9 +840,13 @@ module Scale
756
840
  end
757
841
 
758
842
  class VoteOutcome
759
- include SingleValue
843
+ include Base
760
844
  def self.decode(scale_bytes)
761
- new(scale_bytes.get_next_bytes(32))
845
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
846
+ result = new(scale_bytes.get_next_bytes(32))
847
+
848
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
849
+ result
762
850
  end
763
851
  end
764
852
 
@@ -796,9 +884,10 @@ module Scale
796
884
  end
797
885
 
798
886
  class BoxProposal
799
- include SingleValue
887
+ include Base
800
888
 
801
889
  def self.decode(scale_bytes, metadata, chain_spec)
890
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
802
891
  call_index = scale_bytes.get_next_bytes(2).bytes_to_hex[2..]
803
892
  call_module, call = metadata.value.call_index[call_index]
804
893
 
@@ -807,6 +896,7 @@ module Scale
807
896
  {name: arg[:name], type: arg[:type], value: arg_obj.encode, value_raw: arg_obj.value}
808
897
  end
809
898
 
899
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
810
900
  self.new({
811
901
  call_index: call_index,
812
902
  call_function: call[:name],
@@ -832,7 +922,7 @@ module Scale
832
922
  end
833
923
 
834
924
  class VecH512Length2
835
- include SingleValue
925
+ include Base
836
926
 
837
927
  def self.decode(scale_bytes)
838
928
  end
@@ -844,6 +934,15 @@ module Scale
844
934
  end
845
935
  end
846
936
 
937
+ class GenericBlock
938
+ include Base
939
+
940
+ def self.decode(scale_bytes)
941
+ end
942
+
943
+ def encode
944
+ end
945
+ end
847
946
 
848
947
  end
849
948
  end