scale_rb 0.3.2 → 0.3.3

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: 6b1c3f0276dedfa00a80fed47d5647ced3d6128667e0b6e3ac38733351cb3dac
4
- data.tar.gz: 5e137b28a94a898949937e3d10d043b80627f46232a9f5d49ba27766fbac6659
3
+ metadata.gz: 6df0775bbed0fb5886ee7aba23efa8660dfde37a1f437d1d83958a6cc3f82c1f
4
+ data.tar.gz: a6ad3ab392fdaa8b9a3f431f98a6d1e7b099b5ce43cb966e5055267d5515322d
5
5
  SHA512:
6
- metadata.gz: 0416b944d3a0ccb254390e47c019c5692d04dc3f1c86f6033e6ceaac8e5e8a18c530e20a224ff19975b5acf8413f9e12da16cc6a24253a6f08d09972f981bdea
7
- data.tar.gz: 74e2acc6ee10c7928c7528fa42cdf1dc8c9a778f56c77136a5d9305619b0eb386d752ce3a9902589bed61ffe9eaf74a14446b7579c622fea73d34b960693287e
6
+ metadata.gz: fd7a0fbc2f605c3fef4a0b88e7c8475db9ac7e63223130536fddb786040fc4af79e9306f101697e2ae990f04d913755086df2965e8b922638c7c8498455c0fc8
7
+ data.tar.gz: 378a219709b0cce2ea277b3e717bd2b0653984519c7593e5525a841155a47b1478c6a10cebf29480cc862cac78b33287e34c1a9a7f433679a8d9e66cfe0ce356
data/.gitignore CHANGED
File without changes
data/.rspec CHANGED
File without changes
data/.rubocop.yml CHANGED
File without changes
data/.travis.yml CHANGED
File without changes
data/CODE_OF_CONDUCT.md CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scale_rb (0.3.2)
4
+ scale_rb (0.3.3)
5
5
  async
6
6
  async-http (~> 0.69.0)
7
7
  async-websocket (~> 0.26.2)
@@ -50,7 +50,7 @@ GEM
50
50
  io-stream (0.4.0)
51
51
  json (2.7.2)
52
52
  minitar (0.9)
53
- protocol-hpack (1.4.3)
53
+ protocol-hpack (1.5.0)
54
54
  protocol-http (0.28.1)
55
55
  protocol-http1 (0.19.1)
56
56
  protocol-http (~> 0.22)
data/LICENSE.txt CHANGED
File without changes
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
1
  # ScaleRb
2
2
 
3
+ It is still under heavy development. Use the latest version.
4
+
3
5
  ## Installation
4
6
 
5
7
  Add this line to your application's Gemfile:
6
8
 
7
9
  ```ruby
8
- gem 'scale_rb'
10
+ gem 'scale_rb', '~> 0.3.3'
9
11
  ```
10
12
 
11
13
  And then execute:
data/Rakefile CHANGED
File without changes
File without changes
@@ -3,7 +3,13 @@ require 'scale_rb'
3
3
  ScaleRb.logger.level = Logger::DEBUG
4
4
 
5
5
  client = ScaleRb::HttpClient.new('https://polkadot-rpc.dwellir.com')
6
- block_number = 21585684
6
+ block_number = 21711742
7
7
  block_hash = client.chain_getBlockHash(block_number)
8
- storage = client.get_storage(block_hash, 'System', 'Events')
8
+ metadata = client.get_metadata(block_hash)
9
+
10
+ storage_query = ScaleRb::WsClient::StorageQuery.new(
11
+ pallet_name: 'System',
12
+ storage_name: 'Events',
13
+ )
14
+ storage = client.get_storage(block_hash, storage_query, metadata)
9
15
  puts "block #{block_number}(#{block_hash}) has #{storage.length} events"
@@ -1,10 +1,10 @@
1
1
  require 'scale_rb'
2
2
 
3
- # ScaleRb.logger.level = Logger::DEBUG
3
+ ScaleRb.logger.level = Logger::DEBUG
4
4
 
5
5
  ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
6
6
  block_hash = client.chain_getBlockHash(21585684)
7
7
  runtime_version = client.state_getRuntimeVersion(block_hash)
8
- puts runtime_version['specName']
9
- puts runtime_version['specVersion']
8
+ puts runtime_version[:specName]
9
+ puts runtime_version[:specVersion]
10
10
  end
@@ -1,9 +1,14 @@
1
1
  require 'scale_rb'
2
2
 
3
- ScaleRb.logger.level = Logger::DEBUG
4
-
3
+ # You can have multiple subscriptions at the same time
5
4
  ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
6
5
  client.chain_subscribeNewHead do |head|
7
- puts "Received new head at height: #{head['number'].to_i(16)}"
6
+ puts "Received new head at height: #{head[:number].to_i(16)}"
7
+ end
8
+
9
+ client.state_subscribeStorage do |storage|
10
+ block_hash = storage[:block]
11
+ changes = storage[:changes]
12
+ puts "Received #{changes.size} storage changes at block: #{block_hash}"
8
13
  end
9
14
  end
@@ -2,6 +2,7 @@ require 'scale_rb'
2
2
 
3
3
  # ScaleRb.logger.level = Logger::DEBUG
4
4
 
5
+ # Unsubscribe after receiving 5 new heads
5
6
  ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
6
7
  count = 0
7
8
 
@@ -9,12 +10,12 @@ ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
9
10
  count = count + 1
10
11
 
11
12
  if count < 5
12
- block_number = head['number'].to_i(16)
13
+ block_number = head[:number].to_i(16)
13
14
  block_hash = client.chain_getBlockHash(block_number)
14
15
  puts "Received new head at height: #{block_number}, block hash: #{block_hash}"
15
16
  else
16
17
  unsub_result = client.chain_unsubscribeNewHead(subscription_id)
17
- puts "Unsubscribed from new heads: #{unsub_result}"
18
+ puts "Unsubscribe result: #{unsub_result}"
18
19
  end
19
20
  end
20
21
 
data/lib/address.rb CHANGED
File without changes
@@ -42,7 +42,7 @@ module ScaleRb
42
42
  def query_storage_at(block_hash, storage_keys, type_id, default, registry)
43
43
  result = state_queryStorageAt(storage_keys, block_hash)
44
44
  result.map do |item|
45
- item['changes'].map do |change|
45
+ item[:changes].map do |change|
46
46
  storage_key = change[0]
47
47
  data = change[1] || default
48
48
  storage = data.nil? ? nil : PortableCodec.decode(type_id, data._to_bytes, registry)[0]
@@ -17,7 +17,7 @@ module ScaleRb
17
17
  raise 'url format is not correct' unless url.match?(url_regex)
18
18
 
19
19
  @uri = URI.parse(url)
20
- @supported_methods = request('rpc_methods', [])['methods']
20
+ @supported_methods = request('rpc_methods', [])[:methods]
21
21
  end
22
22
 
23
23
  def request(method, params = [])
@@ -37,11 +37,12 @@ module ScaleRb
37
37
  response = http.request(request)
38
38
  raise response unless response.is_a?(Net::HTTPOK)
39
39
 
40
- body = JSON.parse(response.body)
40
+ # parse response, make key symbol
41
+ body = JSON.parse(response.body, symbolize_names: true)
41
42
  ScaleRb.logger.debug "Response: #{body}"
42
- raise body['error'] if body['error']
43
+ raise body[:error] if body[:error]
43
44
 
44
- body['result']
45
+ body[:result]
45
46
  end
46
47
 
47
48
  def respond_to_missing?(*_args)
@@ -54,4 +55,4 @@ module ScaleRb
54
55
  request(method.to_s, args)
55
56
  end
56
57
  end
57
- end
58
+ end
@@ -9,50 +9,34 @@ require_relative 'client_ext'
9
9
  module ScaleRb
10
10
  class WsClient
11
11
  def self.start(url)
12
- Async do |task|
12
+
13
+ Sync do |task|
13
14
  endpoint = Async::HTTP::Endpoint.parse(url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
14
- client = WsClient.new
15
-
16
- task.async do
17
- Async::WebSocket::Client.connect(endpoint) do |connection|
18
- Async do
19
- while request = client.next_request
20
- ScaleRb.logger.debug "Sending request: #{request.to_json}"
21
- connection.write(request.to_json)
22
- end
23
- end
24
15
 
25
- # inside main task
16
+ Async::WebSocket::Client.connect(endpoint) do |connection|
17
+ client = WsClient.new(connection)
18
+
19
+ recv_task = task.async do
26
20
  while message = connection.read
27
- data = JSON.parse(message)
21
+ data = message.parse
28
22
  ScaleRb.logger.debug "Received message: #{data}"
29
23
 
30
- Async do
24
+ task.async do
31
25
  client.handle_response(data)
32
- rescue => e
33
- ScaleRb.logger.error "#{e.class}: #{e.message}"
34
- ScaleRb.logger.error e.backtrace.join("\n")
35
- task.stop
36
26
  end
37
27
  end
38
- rescue => e
39
- ScaleRb.logger.error "#{e.class}: #{e.message}"
40
- ScaleRb.logger.error e.backtrace.join("\n")
41
- ensure
42
- task.stop
43
28
  end
44
- end
45
29
 
46
- task.async do
47
- client.supported_methods = client.rpc_methods()['methods']
30
+ client.supported_methods = client.rpc_methods()[:methods]
48
31
  yield client
49
- rescue => e
50
- ScaleRb.logger.error "#{e.class}: #{e.message}"
51
- ScaleRb.logger.error e.backtrace.join("\n")
52
- task.stop
32
+
33
+ recv_task.wait
34
+ ensure
35
+ recv_task&.stop
53
36
  end
54
- end
55
- end
37
+ end # Sync
38
+
39
+ end # start
56
40
  end
57
41
  end
58
42
 
@@ -61,8 +45,8 @@ module ScaleRb
61
45
  include ClientExt
62
46
  attr_accessor :supported_methods
63
47
 
64
- def initialize
65
- @queue = Async::Queue.new
48
+ def initialize(connection)
49
+ @connection = connection
66
50
  @response_handler = ResponseHandler.new
67
51
  @subscription_handler = SubscriptionHandler.new
68
52
  @request_id = 1
@@ -87,7 +71,7 @@ module ScaleRb
87
71
  raise "A subscribe method needs a block" unless block_given?
88
72
 
89
73
  subscribe(method, args) do |notification|
90
- yield notification['params']['result']
74
+ yield notification[:params][:result]
91
75
  end
92
76
  else
93
77
  request(method, args)
@@ -111,14 +95,10 @@ module ScaleRb
111
95
  end
112
96
  end
113
97
 
114
- def next_request
115
- @queue.dequeue
116
- end
117
-
118
98
  def handle_response(response)
119
- if response.key?('id')
99
+ if response.key?(:id)
120
100
  @response_handler.handle(response)
121
- elsif response.key?('method')
101
+ elsif response.key?(:method)
122
102
  @subscription_handler.handle(response)
123
103
  else
124
104
  puts "Received an unknown message: #{response}"
@@ -131,53 +111,34 @@ module ScaleRb
131
111
  response_future = Async::Notification.new
132
112
 
133
113
  @response_handler.register(@request_id, proc { |response|
134
- # this is running in the main task
135
- response_future.signal(response['result'])
114
+ response_future.signal(response[:result])
136
115
  })
137
116
 
138
- request = JsonRpcRequest.new(@request_id, method, params)
139
- @queue.enqueue(request)
117
+ request = { jsonrpc: '2.0', id: @request_id, method: method, params: params }
118
+ ScaleRb.logger.debug "Sending request: #{request}"
119
+ @connection.write(request.to_json)
140
120
 
141
121
  @request_id += 1
142
-
143
122
  response_future.wait
144
123
  end
145
124
  end
146
125
 
147
- class JsonRpcRequest
148
- attr_reader :id, :method, :params
149
-
150
- def initialize(id, method, params = {})
151
- @id = id
152
- @method = method
153
- @params = params
154
- end
155
-
156
- def to_json(*_args)
157
- { jsonrpc: '2.0', id: @id, method: @method, params: @params }.to_json
158
- end
159
-
160
- # def to_s
161
- # to_json
162
- # end
163
- end
164
-
165
126
  class ResponseHandler
166
127
  def initialize
167
- @handlers = {}
128
+ @callbacks = {}
168
129
  end
169
130
 
170
- # handler: a proc with response data as param
171
- def register(id, handler)
172
- @handlers[id] = handler
131
+ # callback: a proc with response data as param
132
+ def register(id, callback)
133
+ @callbacks[id] = callback
173
134
  end
174
135
 
175
136
  def handle(response)
176
- id = response['id']
177
- if @handlers.key?(id)
178
- handler = @handlers[id]
179
- handler.call(response)
180
- @handlers.delete(id)
137
+ id = response[:id]
138
+ if @callbacks.key?(id)
139
+ callback = @callbacks[id]
140
+ callback.call(response)
141
+ @callbacks.delete(id)
181
142
  else
182
143
  ScaleRb.logger.debug "Received a message with unknown id: #{response}"
183
144
  end
@@ -186,33 +147,23 @@ module ScaleRb
186
147
 
187
148
  class SubscriptionHandler
188
149
  def initialize
189
- @subscriptions = {}
150
+ @callbacks = {}
190
151
  end
191
152
 
192
- def subscribe(subscription_id, handler)
193
- @subscriptions[subscription_id] = handler
153
+ def subscribe(subscription_id, callback)
154
+ @callbacks[subscription_id] = callback
194
155
  end
195
156
 
196
157
  def unsubscribe(subscription_id)
197
- @subscriptions.delete(subscription_id)
158
+ @callbacks.delete(subscription_id)
198
159
  end
199
160
 
200
161
  def handle(notification)
201
- subscription_id = notification.dig('params', 'subscription')
162
+ subscription_id = notification.dig(:params, :subscription)
202
163
  return if subscription_id.nil?
203
164
 
204
- if @subscriptions.key?(subscription_id)
205
- @subscriptions[subscription_id].call(notification)
206
- else
207
- # the subscription_id may be not registered.
208
- # in client.subscribe function,
209
- # ...
210
- # subscription_id = request(method, params)
211
- # @subscription_handler.subscribe(subscription_id, block)
212
- # ...
213
- # the request(method, params) may be slow, so the subscription_id may be not registered when the first notification comes.
214
- sleep 0.01
215
- handle(notification)
165
+ if @callbacks.key?(subscription_id)
166
+ @callbacks[subscription_id].call(notification)
216
167
  end
217
168
  end
218
169
  end
data/lib/codec.rb CHANGED
File without changes
data/lib/hasher.rb CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
data/lib/registry.rb CHANGED
File without changes
@@ -1,3 +1,3 @@
1
1
  module ScaleRb
2
- VERSION = '0.3.2'
2
+ VERSION = '0.3.3'
3
3
  end
data/lib/scale_rb.rb CHANGED
File without changes
data/scale_rb.gemspec CHANGED
File without changes
data/tea.yaml CHANGED
File without changes
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.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aki Wu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-19 00:00:00.000000000 Z
11
+ date: 2024-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base58
@@ -149,7 +149,7 @@ licenses:
149
149
  metadata:
150
150
  bug_tracker_uri: https://github.com/wuminzhe/scale_rb/issues/
151
151
  source_code_uri: https://github.com/wuminzhe/scale_rb.git
152
- post_install_message:
152
+ post_install_message:
153
153
  rdoc_options: []
154
154
  require_paths:
155
155
  - lib
@@ -164,8 +164,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  - !ruby/object:Gem::Version
165
165
  version: '0'
166
166
  requirements: []
167
- rubygems_version: 3.3.7
168
- signing_key:
167
+ rubygems_version: 3.4.19
168
+ signing_key:
169
169
  specification_version: 4
170
170
  summary: A Ruby SCALE Codec Library, and, Substrate RPC Client
171
171
  test_files: []