scale.rb 0.2.14 → 0.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/scale/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Scale
2
- VERSION = "0.2.14".freeze
2
+ VERSION = "0.2.15".freeze
3
3
  end
@@ -0,0 +1,159 @@
1
+
2
+ def ws_request(url, payload)
3
+ result = nil
4
+ Kontena::Websocket::Client.connect(url, {}) do |client|
5
+ client.send(payload.to_json)
6
+
7
+ client.read do |message|
8
+ result = JSON.parse message
9
+ client.close(1000)
10
+ end
11
+ end
12
+
13
+ return result
14
+ rescue Kontena::Websocket::CloseError => e
15
+ raise SubstrateClient::WebsocketError, e.reason
16
+ rescue Kontena::Websocket::Error => e
17
+ raise SubstrateClient::WebsocketError, e.reason
18
+ end
19
+
20
+ class SubstrateClient
21
+ class WebsocketError < StandardError; end
22
+ class RpcError < StandardError; end
23
+ class RpcTimeout < StandardError; end
24
+
25
+ attr_reader :metadata
26
+ attr_reader :spec_name, :spec_version
27
+
28
+ def initialize(url)
29
+ @url = url
30
+ @request_id = 1
31
+ @metadata_cache = {}
32
+ end
33
+
34
+ def request(method, params)
35
+ payload = {
36
+ "jsonrpc" => "2.0",
37
+ "method" => method,
38
+ "params" => params,
39
+ "id" => @request_id
40
+ }
41
+
42
+ data = ws_request(@url, payload)
43
+ if data["error"]
44
+ raise RpcError, data["error"]
45
+ else
46
+ data["result"]
47
+ end
48
+ end
49
+
50
+ def init_types_and_metadata(block_hash=nil)
51
+ runtime_version = self.state_getRuntimeVersion(block_hash)
52
+ spec_name = runtime_version["specName"].downcase
53
+ spec_version = runtime_version["specVersion"]
54
+
55
+ registry = Scale::TypeRegistry.instance
56
+
57
+ # load types
58
+ if registry.types == nil
59
+ registry.load(spec_name: spec_name)
60
+ end
61
+ registry.spec_version = spec_version
62
+
63
+ # set current metadata
64
+ metadata = @metadata_cache[spec_version]
65
+ if metadata.nil?
66
+ hex = self.state_getMetadata(block_hash)
67
+ metadata = Scale::Types::Metadata.decode(Scale::Bytes.new(hex))
68
+ @metadata_cache[spec_version] = metadata
69
+ end
70
+
71
+ @metadata = metadata
72
+ registry.metadata = metadata
73
+
74
+ true
75
+ end
76
+
77
+ def get_metadata_from_cache(spec_version)
78
+
79
+ end
80
+
81
+ def invoke(method, *params)
82
+ request(method, params)
83
+ end
84
+
85
+ # ################################################
86
+ # origin rpc methods
87
+ # ################################################
88
+ def method_missing(method, *args)
89
+ invoke method, *args
90
+ end
91
+
92
+ # ################################################
93
+ # custom methods based on origin rpc methods
94
+ # ################################################
95
+ def methods
96
+ invoke("rpc_methods")["methods"]
97
+ end
98
+
99
+ def get_block_number(block_hash)
100
+ header = self.chain_getHeader(block_hash)
101
+ header["number"].to_i(16)
102
+ end
103
+
104
+ def get_metadata(block_hash=nil)
105
+ self.init_types_and_metadata(block_hash)
106
+ @metadata
107
+ end
108
+
109
+ def get_block(block_hash=nil)
110
+ self.init_types_and_metadata(block_hash)
111
+ block = self.chain_getBlock(block_hash)
112
+ SubstrateClient::Helper.decode_block(block)
113
+ rescue => ex
114
+ puts ex.message
115
+ puts ex.backtrace.join("\n\t")
116
+ end
117
+
118
+ def get_block_events(block_hash=nil)
119
+ self.init_types_and_metadata(block_hash)
120
+
121
+ storage_key = "0x26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7"
122
+ events_data = state_getStorage storage_key, block_hash
123
+
124
+ scale_bytes = Scale::Bytes.new(events_data)
125
+ Scale::Types.get("Vec<EventRecord>").decode(scale_bytes).to_human
126
+ end
127
+
128
+ # Plain: client.get_storage("Sudo", "Key")
129
+ # Plain: client.get_storage("Balances", "TotalIssuance")
130
+ # Map: client.get_storage("System", "Account", ["0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"])
131
+ # DoubleMap: client.get_storage("ImOnline", "AuthoredBlocks", [2818, "0x749ddc93a65dfec3af27cc7478212cb7d4b0c0357fef35a0163966ab5333b757"])
132
+ def get_storage(module_name, storage_name, params = nil, block_hash = nil)
133
+ self.init_types_and_metadata(block_hash)
134
+
135
+ storage_key, return_type = SubstrateClient::Helper.generate_storage_key_from_metadata(@metadata, module_name, storage_name, params)
136
+ data = self.state_getStorage(storage_key, block_hash)
137
+ return unless data
138
+
139
+ bytes = Scale::Bytes.new(data)
140
+ type = Scale::Types.get(return_type)
141
+ type.decode(bytes)
142
+ end
143
+
144
+ def generate_storage_key(module_name, storage_name, params = nil, block_hash = nil)
145
+ self.init_types_and_metadata(block_hash)
146
+ SubstrateClient::Helper.generate_storage_key_from_metadata(@metadata, module_name, storage_name, params)
147
+ end
148
+
149
+ # compose_call "Balances", "Transfer", { dest: "0x586cb27c291c813ce74e86a60dad270609abf2fc8bee107e44a80ac00225c409", value: 1_000_000_000_000 }
150
+ def compose_call(module_name, call_name, params, block_hash=nil)
151
+ self.init_types_and_metadata(block_hash)
152
+ SubstrateClient::Helper.compose_call_from_metadata(@metadata, module_name, call_name, params)
153
+ end
154
+
155
+ def generate_storage_hash_from_data(storage_hex_data)
156
+ "0x" + Crypto.blake2_256(Scale::Bytes.new(storage_hex_data).bytes)
157
+ end
158
+
159
+ end
@@ -486,32 +486,6 @@
486
486
  ]
487
487
  ]
488
488
  },
489
- "EthereumRelayHeaderParcel": {
490
- "type": "struct",
491
- "type_mapping": [
492
- [
493
- "header",
494
- "EthereumHeader"
495
- ],
496
- [
497
- "mmr_root",
498
- "H256"
499
- ]
500
- ]
501
- },
502
- "EthereumRelayProofs": {
503
- "type": "struct",
504
- "type_mapping": [
505
- [
506
- "ethash_proof",
507
- "Vec<EthashProof>"
508
- ],
509
- [
510
- "mmr_proof",
511
- "Vec<H256>"
512
- ]
513
- ]
514
- },
515
489
  "OtherSignature": {
516
490
  "type": "enum",
517
491
  "type_mapping": [
@@ -540,106 +514,6 @@
540
514
  ]
541
515
  },
542
516
  "AddressT": "[u8; 20]",
543
- "MerkleMountainRangeRootLog": {
544
- "type": "struct",
545
- "type_mapping": [
546
- [
547
- "prefix",
548
- "[u8; 4]"
549
- ],
550
- [
551
- "mmr_root",
552
- "Hash"
553
- ]
554
- ]
555
- },
556
- "RelayHeaderId": "EthereumBlockNumber",
557
- "RelayHeaderParcel": "EthereumRelayHeaderParcel",
558
- "RelayProofs": "EthereumRelayProofs",
559
- "RelayAffirmationId": {
560
- "type": "struct",
561
- "type_mapping": [
562
- [
563
- "game_id",
564
- "EthereumBlockNumber"
565
- ],
566
- [
567
- "round",
568
- "u32"
569
- ],
570
- [
571
- "index",
572
- "u32"
573
- ]
574
- ]
575
- },
576
- "RelayAffirmationT": {
577
- "type": "struct",
578
- "type_mapping": [
579
- [
580
- "relayer",
581
- "AccountId"
582
- ],
583
- [
584
- "relay_header_parcels",
585
- "EthereumRelayHeaderParcel"
586
- ],
587
- [
588
- "bond",
589
- "Balance"
590
- ],
591
- [
592
- "maybe_extended_relay_affirmation_id",
593
- "Option<RelayAffirmationId>"
594
- ],
595
- [
596
- "verified",
597
- "bool"
598
- ]
599
- ]
600
- },
601
- "RelayVotingState": {
602
- "type": "struct",
603
- "type_mapping": [
604
- [
605
- "ayes",
606
- "Vec<AccountId>"
607
- ],
608
- [
609
- "nays",
610
- "Vec<AccountId>"
611
- ]
612
- ]
613
- },
614
- "ProxyType": {
615
- "type": "enum",
616
- "type_mapping": [
617
- [
618
- "Any",
619
- "Null"
620
- ],
621
- [
622
- "NonTransfer",
623
- "Null"
624
- ],
625
- [
626
- "Governance",
627
- "Null"
628
- ],
629
- [
630
- "Staking",
631
- "Null"
632
- ],
633
- [
634
- "IdentityJudgement",
635
- "Null"
636
- ],
637
- [
638
- "EthereumBridge",
639
- "Null"
640
- ]
641
- ]
642
- },
643
517
  "BalancesRuntimeDispatchInfo": {
644
518
  "type": "struct",
645
519
  "type_mapping": [
@@ -658,5 +532,398 @@
658
532
  ]
659
533
  ]
660
534
  }
661
- }
535
+ },
536
+ "versioning": [
537
+ {
538
+ "runtime_range": [
539
+ 20,
540
+ 28
541
+ ],
542
+ "types": {
543
+ "MerkleMountainRangeRootLog": {
544
+ "type": "struct",
545
+ "type_mapping": [
546
+ [
547
+ "prefix",
548
+ "[u8; 4]"
549
+ ],
550
+ [
551
+ "mmr_root",
552
+ "Hash"
553
+ ]
554
+ ]
555
+ }
556
+ }
557
+ },
558
+ {
559
+ "runtime_range": [
560
+ 29,
561
+ 30
562
+ ],
563
+ "types": {
564
+ "EcdsaMessage": "[u8; 32]",
565
+ "RelayAuthoritySigner": "EthereumAddress",
566
+ "RelayAuthorityMessage": "EcdsaMessage",
567
+ "RelayAuthoritySignature": "EcdsaSignature",
568
+ "Term": "u32",
569
+ "RelayAuthorityT": {
570
+ "type": "struct",
571
+ "type_mapping": [
572
+ [
573
+ "account_id",
574
+ "AccountId"
575
+ ],
576
+ [
577
+ "signer",
578
+ "Signer"
579
+ ],
580
+ [
581
+ "stake",
582
+ "Balance"
583
+ ],
584
+ [
585
+ "term",
586
+ "BlockNumber"
587
+ ]
588
+ ]
589
+ },
590
+ "MMRRoot": "Hash",
591
+ "MerkleMountainRangeRootLog": {
592
+ "type": "struct",
593
+ "type_mapping": [
594
+ [
595
+ "prefix",
596
+ "[u8; 4]"
597
+ ],
598
+ [
599
+ "parent_mmr_root",
600
+ "Hash"
601
+ ]
602
+ ]
603
+ },
604
+ "EthereumRelayHeaderParcel": {
605
+ "type": "struct",
606
+ "type_mapping": [
607
+ [
608
+ "header",
609
+ "EthereumHeader"
610
+ ],
611
+ [
612
+ "parent_mmr_root",
613
+ "H256"
614
+ ]
615
+ ]
616
+ }
617
+ }
618
+ },
619
+ {
620
+ "runtime_range": [
621
+ 25,
622
+ 30
623
+ ],
624
+ "types": {
625
+ "RelayProofs": "EthereumRelayProofs"
626
+ }
627
+ },
628
+ {
629
+ "runtime_range": [
630
+ 24,
631
+ 30
632
+ ],
633
+ "types": {
634
+ "RelayVotingState": {
635
+ "type": "struct",
636
+ "type_mapping": [
637
+ [
638
+ "ayes",
639
+ "Vec<AccountId>"
640
+ ],
641
+ [
642
+ "nays",
643
+ "Vec<AccountId>"
644
+ ]
645
+ ]
646
+ },
647
+ "ProxyType": {
648
+ "type": "enum",
649
+ "type_mapping": [
650
+ [
651
+ "Any",
652
+ "Null"
653
+ ],
654
+ [
655
+ "NonTransfer",
656
+ "Null"
657
+ ],
658
+ [
659
+ "Governance",
660
+ "Null"
661
+ ],
662
+ [
663
+ "Staking",
664
+ "Null"
665
+ ],
666
+ [
667
+ "IdentityJudgement",
668
+ "Null"
669
+ ],
670
+ [
671
+ "EthereumBridge",
672
+ "Null"
673
+ ]
674
+ ]
675
+ },
676
+ "RelayHeaderId": "EthereumBlockNumber",
677
+ "RelayHeaderParcel": "EthereumRelayHeaderParcel",
678
+ "RelayAffirmationId": {
679
+ "type": "struct",
680
+ "type_mapping": [
681
+ [
682
+ "game_id",
683
+ "EthereumBlockNumber"
684
+ ],
685
+ [
686
+ "round",
687
+ "u32"
688
+ ],
689
+ [
690
+ "index",
691
+ "u32"
692
+ ]
693
+ ]
694
+ },
695
+ "RelayAffirmationT": {
696
+ "type": "struct",
697
+ "type_mapping": [
698
+ [
699
+ "relayer",
700
+ "AccountId"
701
+ ],
702
+ [
703
+ "relay_header_parcels",
704
+ "EthereumRelayHeaderParcel"
705
+ ],
706
+ [
707
+ "bond",
708
+ "Balance"
709
+ ],
710
+ [
711
+ "maybe_extended_relay_affirmation_id",
712
+ "Option<RelayAffirmationId>"
713
+ ],
714
+ [
715
+ "verified",
716
+ "bool"
717
+ ]
718
+ ]
719
+ }
720
+ }
721
+ },
722
+ {
723
+ "runtime_range": [
724
+ 24,
725
+ 24
726
+ ],
727
+ "types": {
728
+ "RelayProofs": "EthereumRelayProof"
729
+ }
730
+ },
731
+ {
732
+ "runtime_range": [
733
+ 20,
734
+ 22
735
+ ],
736
+ "types": {
737
+ "EthereumHeaderBrief": {
738
+ "type": "struct",
739
+ "type_mapping": [
740
+ [
741
+ "total_difficulty",
742
+ "U256"
743
+ ],
744
+ [
745
+ "parent_hash",
746
+ "H256"
747
+ ],
748
+ [
749
+ "number",
750
+ "EthereumBlockNumber"
751
+ ],
752
+ [
753
+ "relayer",
754
+ "AccountId"
755
+ ]
756
+ ]
757
+ },
758
+ "EthereumHeaderThingWithProof": {
759
+ "type": "struct",
760
+ "type_mapping": [
761
+ [
762
+ "header",
763
+ "EthereumHeader"
764
+ ],
765
+ [
766
+ "ethash_proof",
767
+ "Vec<EthashProof>"
768
+ ],
769
+ [
770
+ "mmr_root",
771
+ "H256"
772
+ ],
773
+ [
774
+ "mmr_proof",
775
+ "Vec<H256>"
776
+ ]
777
+ ]
778
+ },
779
+ "ConfirmedEthereumHeaderInfo": {
780
+ "type": "struct",
781
+ "type_mapping": [
782
+ [
783
+ "header",
784
+ "EthereumHeader"
785
+ ],
786
+ [
787
+ "mmr_root",
788
+ "H256"
789
+ ]
790
+ ]
791
+ },
792
+ "EthereumHeaderThing": {
793
+ "type": "struct",
794
+ "type_mapping": [
795
+ [
796
+ "header",
797
+ "EthereumHeader"
798
+ ],
799
+ [
800
+ "mmr_root",
801
+ "H256"
802
+ ]
803
+ ]
804
+ },
805
+ "Round": "u64",
806
+ "TcHeaderThingWithProof": "EthereumHeaderThingWithProof",
807
+ "TcHeaderThing": "EthereumHeaderThing",
808
+ "TcBlockNumber": "u64",
809
+ "TcHeaderHash": "H256",
810
+ "GameId": "TcBlockNumber",
811
+ "RelayProposalT": {
812
+ "type": "struct",
813
+ "type_mapping": [
814
+ [
815
+ "relayer",
816
+ "AccountId"
817
+ ],
818
+ [
819
+ "bonded_proposal",
820
+ "Vec<(Balance, TcHeaderThing)>"
821
+ ],
822
+ [
823
+ "extend_from_header_hash",
824
+ "Option<TcHeaderHash>"
825
+ ]
826
+ ]
827
+ }
828
+ }
829
+ },
830
+
831
+ {
832
+ "runtime_range": [
833
+ 23,
834
+ 30
835
+ ],
836
+ "types": {
837
+ "EthereumRelayProofs": {
838
+ "type": "struct",
839
+ "type_mapping": [
840
+ [
841
+ "ethash_proof",
842
+ "Vec<EthashProof>"
843
+ ],
844
+ [
845
+ "mmr_proof",
846
+ "Vec<H256>"
847
+ ]
848
+ ]
849
+ }
850
+ }
851
+ },
852
+
853
+ {
854
+ "runtime_range": [
855
+ 23,
856
+ 28
857
+ ],
858
+ "types": {
859
+ "EthereumRelayHeaderParcel": {
860
+ "type": "struct",
861
+ "type_mapping": [
862
+ [
863
+ "header",
864
+ "EthereumHeader"
865
+ ],
866
+ [
867
+ "mmr_root",
868
+ "H256"
869
+ ]
870
+ ]
871
+ }
872
+ }
873
+ },
874
+
875
+ {
876
+ "runtime_range": [
877
+ 23,
878
+ 23
879
+ ],
880
+ "types": {
881
+ "RelayHeaderId": "Vec<u8>",
882
+ "RelayHeaderParcel": "Vec<u8>",
883
+ "RelayAffirmationId": {
884
+ "type": "struct",
885
+ "type_mapping": [
886
+ [
887
+ "relay_header_id",
888
+ "Vec<u8>"
889
+ ],
890
+ [
891
+ "round",
892
+ "u32"
893
+ ],
894
+ [
895
+ "index",
896
+ "u32"
897
+ ]
898
+ ]
899
+ },
900
+ "RelayAffirmationT": {
901
+ "type": "struct",
902
+ "type_mapping": [
903
+ [
904
+ "relayer",
905
+ "AccountId"
906
+ ],
907
+ [
908
+ "relay_header_parcels",
909
+ "Vec<u8>"
910
+ ],
911
+ [
912
+ "bond",
913
+ "Balance"
914
+ ],
915
+ [
916
+ "maybe_extended_relay_affirmation_id",
917
+ "Option<Vec<u8>>"
918
+ ],
919
+ [
920
+ "verified",
921
+ "bool"
922
+ ]
923
+ ]
924
+ },
925
+ "RelayProofs": "Vec<u8>"
926
+ }
927
+ }
928
+ ]
662
929
  }