bluzelle 0.1.0 → 0.1.1

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bluzelle.rb +159 -56
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be7bb808d24a2b87f8d1f128d49b7076dd51d4357e6b5dc81c1302d2e1c1d9b3
4
- data.tar.gz: 2576a055d888bcf7726584541274a1dbd3bef680019ddfd0c97fede963cb82f5
3
+ metadata.gz: 3856b2d51c46b960730b654272313166a81f6ec5317d7810e7d2efc3a79877d8
4
+ data.tar.gz: 130f1f531f074595fcce80da2d1b64239891db846dbeed7be5f77d3e270477c4
5
5
  SHA512:
6
- metadata.gz: 3d124f196fff63444c10197f0b9b6108f70eab8aaf2fdb6e80a85e1815efcc1abd7b9f058b0cd362001b8c583637da9641a90a8bed2ef294580b514494040f11
7
- data.tar.gz: e20c60cb8c21bae2fa4b35903f7f6fef8fc1ed1b40470241bafb6756733279c3900bba7d654b9dabfaa26e206f3b6e2c8fd53f3087783cabebdc1985c72320b8
6
+ metadata.gz: e65c93bb1f263c796cf6bfd68c9e52c2a2a3348feedf2db8ffb4bc2fd6390a97cad21b52615270d42479959e583351e49b7ce1fcdd727b850474d8caeaa32172
7
+ data.tar.gz: 707efb828462ecf5bf91eb907f377b79c90f4e36cf2903e49ec0ab6db4b7eaff3651283206c839450335c6512a4bd7714fac3798f036395e1e57fc61a7a34dce
@@ -31,6 +31,7 @@ MSG_KEYS_ORDER = %w[
31
31
  UUID
32
32
  Value
33
33
  ].freeze
34
+ BLOCK_TIME_IN_SECONDS = 5
34
35
 
35
36
  module Bluzelle
36
37
  class OptionsError < StandardError
@@ -43,20 +44,7 @@ module Bluzelle
43
44
  raise OptionsError, 'address is required' unless options.fetch('address', nil)
44
45
  raise OptionsError, 'mnemonic is required' unless options.fetch('mnemonic', nil)
45
46
 
46
- gas_info = options.fetch('gas_info', {})
47
- unless gas_info.class.equal?(Hash)
48
- raise OptionsError, 'gas_info should be a dict of {gas_price, max_fee, max_gas}'
49
- end
50
-
51
- gas_info_keys = %w[gas_price max_fee max_gas]
52
- gas_info_keys.each do |k|
53
- v = gas_info.fetch(k, 0)
54
- unless v.class.equal?(Integer)
55
- raise OptionsError, 'gas_info[%s] should be an int' % k
56
- end
57
-
58
- gas_info[k] = v
59
- end
47
+ Bluzelle::validate_gas_info options.fetch('gas_info', {})
60
48
 
61
49
  options['debug'] = false unless options.fetch('debug', false)
62
50
  options['chain_id'] = DEFAULT_CHAIN_ID unless options.fetch('chain_id', nil)
@@ -114,43 +102,83 @@ module Bluzelle
114
102
 
115
103
  # mutate
116
104
 
117
- def create(key, value, lease: 0)
118
- send_transaction('post', '/crud/create', { 'Key' => key, 'Lease' => lease.to_s, 'Value' => value })
105
+ def create(key, value, lease_info: nil, gas_info: nil)
106
+ payload = {"Key" => key}
107
+ payload["Lease"] = Bluzelle::lease_info_to_blocks(lease_info).to_s if lease_info
108
+ payload["Value"] = value
109
+ send_transaction(
110
+ 'post',
111
+ '/crud/create',
112
+ payload,
113
+ gas_info: gas_info
114
+ )
119
115
  end
120
116
 
121
- def update(key, value, lease: nil)
117
+ def update(key, value, lease_info: nil, gas_info: nil)
122
118
  payload = {"Key" => key}
123
- payload["Lease"] = lease.to_s if lease.is_a? Integer
119
+ payload["Lease"] = Bluzelle::lease_info_to_blocks(lease_info).to_s if lease_info
124
120
  payload["Value"] = value
125
- send_transaction("post", "/crud/update", payload)
121
+ send_transaction(
122
+ "post",
123
+ "/crud/update",
124
+ payload,
125
+ gas_info: gas_info
126
+ )
126
127
  end
127
128
 
128
- def delete(key)
129
- send_transaction("delete", "/crud/delete", {"Key" => key})
129
+ def delete(key, gas_info: nil)
130
+ send_transaction(
131
+ "delete",
132
+ "/crud/delete",
133
+ {"Key" => key},
134
+ gas_info: gas_info
135
+ )
130
136
  end
131
137
 
132
- def rename(key, new_key)
133
- send_transaction("post", "/crud/rename", {"Key" => key, "NewKey" => new_key})
138
+ def rename(key, new_key, gas_info: nil)
139
+ send_transaction(
140
+ "post",
141
+ "/crud/rename",
142
+ {"Key" => key, "NewKey" => new_key},
143
+ gas_info: gas_info
144
+ )
134
145
  end
135
146
 
136
- def delete_all()
137
- send_transaction("post", "/crud/deleteall", {})
147
+ def delete_all(gas_info: nil)
148
+ send_transaction(
149
+ "post",
150
+ "/crud/deleteall",
151
+ {},
152
+ gas_info: gas_info
153
+ )
138
154
  end
139
155
 
140
- def multi_update(payload)
156
+ def multi_update(payload, gas_info: nil)
141
157
  list = []
142
158
  payload.each do |key, value|
143
159
  list.append({"key" => key, "value" => value})
144
160
  end
145
- send_transaction("post", "/crud/multiupdate", {"KeyValues" => list})
161
+ send_transaction(
162
+ "post",
163
+ "/crud/multiupdate", {"KeyValues" => list},
164
+ gas_info: gas_info
165
+ )
146
166
  end
147
167
 
148
- def renew_lease(key, lease)
149
- send_transaction("post", "/crud/renewlease", {"Key" => key, "Lease" => lease.to_s})
168
+ def renew_lease(key, lease_info, gas_info: nil)
169
+ send_transaction(
170
+ "post", "/crud/renewlease",
171
+ {"Key" => key, "Lease" => Bluzelle::lease_info_to_blocks(lease_info).to_s},
172
+ gas_info: gas_info
173
+ )
150
174
  end
151
175
 
152
- def renew_all_leases(lease)
153
- send_transaction("post", "/crud/renewleaseall", {"Lease" => lease.to_s})
176
+ def renew_all_leases(lease_info, gas_info: nil)
177
+ send_transaction(
178
+ "post", "/crud/renewleaseall",
179
+ {"Lease" => Bluzelle::lease_info_to_blocks(lease_info).to_s},
180
+ gas_info: gas_info
181
+ )
154
182
  end
155
183
 
156
184
  # query
@@ -187,49 +215,57 @@ module Bluzelle
187
215
 
188
216
  def get_lease(key)
189
217
  url = "/crud/getlease/#{@options["uuid"]}/#{key}"
190
- api_query(url)["result"]["lease"].to_i
218
+ Bluzelle::lease_blocks_to_seconds api_query(url)["result"]["lease"].to_i
191
219
  end
192
220
 
193
221
  def get_n_shortest_leases(n)
194
- url = "/crud/getnshortestlease/#{@options["uuid"]}/#{n}"
195
- api_query(url)["result"]["keyleases"]
222
+ url = "/crud/getnshortestleases/#{@options["uuid"]}/#{n}"
223
+ kls = api_query(url)["result"]["keyleases"]
224
+ kls.each do |kl|
225
+ kl["lease"] = Bluzelle::lease_blocks_to_seconds kl["lease"]
226
+ end
227
+ kls
196
228
  end
197
229
 
198
230
  #
199
231
 
200
- def tx_read(key)
201
- res = send_transaction("post", "/crud/read", {"Key" => key})
232
+ def tx_read(key, gas_info: nil)
233
+ res = send_transaction("post", "/crud/read", {"Key" => key}, gas_info: gas_info)
202
234
  res["value"]
203
235
  end
204
236
 
205
- def tx_has(key)
206
- res = send_transaction("post", "/crud/has", {"Key" => key})
237
+ def tx_has(key, gas_info: nil)
238
+ res = send_transaction("post", "/crud/has", {"Key" => key}, gas_info: gas_info)
207
239
  res["has"]
208
240
  end
209
241
 
210
- def tx_count()
211
- res = send_transaction("post", "/crud/count", {})
242
+ def tx_count(gas_info: nil)
243
+ res = send_transaction("post", "/crud/count", {}, gas_info: gas_info)
212
244
  res["count"].to_i
213
245
  end
214
246
 
215
- def tx_keys()
216
- res = send_transaction("post", "/crud/keys", {})
247
+ def tx_keys(gas_info: nil)
248
+ res = send_transaction("post", "/crud/keys", {}, gas_info: gas_info)
217
249
  res["keys"]
218
250
  end
219
251
 
220
- def tx_key_values()
221
- res = send_transaction("post", "/crud/keyvalues", {})
252
+ def tx_key_values(gas_info: nil)
253
+ res = send_transaction("post", "/crud/keyvalues", {}, gas_info: gas_info)
222
254
  res["keyvalues"]
223
255
  end
224
256
 
225
- def tx_get_lease(key)
226
- res = send_transaction("post", "/crud/getlease", {"Key" => key})
227
- res["lease"].to_i
257
+ def tx_get_lease(key, gas_info: nil)
258
+ res = send_transaction("post", "/crud/getlease", {"Key" => key}, gas_info: gas_info)
259
+ Bluzelle::lease_blocks_to_seconds res["lease"].to_i
228
260
  end
229
261
 
230
- def tx_get_n_shortest_leases(n)
231
- res = send_transaction("post", "/crud/getnshortestlease", {"N" => n.to_s})
232
- res["keyleases"]
262
+ def tx_get_n_shortest_leases(n, gas_info: nil)
263
+ res = send_transaction("post", "/crud/getnshortestleases", {"N" => n.to_s}, gas_info: gas_info)
264
+ kls = res["keyleases"]
265
+ kls.each do |kl|
266
+ kl["lease"] = Bluzelle::lease_blocks_to_seconds kl["lease"]
267
+ end
268
+ kls
233
269
  end
234
270
 
235
271
  #
@@ -268,10 +304,10 @@ module Bluzelle
268
304
  data
269
305
  end
270
306
 
271
- def send_transaction(method, endpoint, payload)
307
+ def send_transaction(method, endpoint, payload, gas_info: nil)
272
308
  @broadcast_retries = 0
273
309
  txn = validate_transaction(method, endpoint, payload)
274
- broadcast_transaction(txn)
310
+ broadcast_transaction(txn, gas_info: gas_info)
275
311
  end
276
312
 
277
313
  def validate_transaction(method, endpoint, payload)
@@ -287,11 +323,17 @@ module Bluzelle
287
323
  api_mutate(method, endpoint, payload)['value']
288
324
  end
289
325
 
290
- def broadcast_transaction(data)
326
+ def broadcast_transaction(data, gas_info: nil)
291
327
  # fee
292
328
  fee = data['fee']
293
329
  fee_gas = fee['gas'].to_i
294
- gas_info = @options['gas_info']
330
+ if gas_info == nil
331
+ gas_info = @options['gas_info']
332
+ end
333
+ if gas_info == nil
334
+ raise OptionsError, 'please provide gas_info when initializing the client or in the transaction'
335
+ end
336
+ Bluzelle::validate_gas_info gas_info
295
337
  if gas_info['max_gas'] != 0 && fee_gas > gas_info['max_gas']
296
338
  fee['gas'] = gas_info['max_gas'].to_s
297
339
  end
@@ -346,7 +388,7 @@ module Bluzelle
346
388
 
347
389
  sleep BROADCAST_RETRY_INTERVAL_SECONDS
348
390
  set_account()
349
- broadcast_transaction(txn)
391
+ broadcast_transaction(txn, gas_info: gas_info)
350
392
  return
351
393
  end
352
394
 
@@ -422,7 +464,7 @@ module Bluzelle
422
464
 
423
465
  def self.hex_to_ascii(h)
424
466
  [h].pack('H*')
425
- end
467
+ end
426
468
 
427
469
  def self.make_random_string(size)
428
470
  SecureRandom.alphanumeric size
@@ -433,4 +475,65 @@ module Bluzelle
433
475
  # h.to_json
434
476
  # Hash[*h.sort.flatten].to_json
435
477
  end
478
+
479
+ def self.lease_info_to_blocks(lease_info)
480
+ if !lease_info
481
+ raise OptionsError, 'provided lease info is nil'
482
+ end
483
+ unless lease_info.class.equal?(Hash)
484
+ raise OptionsError, 'lease_info should be a hash of {days, hours, minutes, seconds}'
485
+ end
486
+
487
+ days = lease_info.fetch('days', 0)
488
+ hours = lease_info.fetch('hours', 0)
489
+ minutes = lease_info.fetch('minutes', 0)
490
+ seconds = lease_info.fetch('seconds', 0)
491
+
492
+ if seconds
493
+ unless seconds.class.equal?(Integer)
494
+ raise OptionsError, 'lease_info[seconds] should be an int'
495
+ end
496
+ end
497
+ if minutes
498
+ unless minutes.class.equal?(Integer)
499
+ raise OptionsError, 'lease_info[minutes] should be an int'
500
+ end
501
+ end
502
+ if hours
503
+ unless hours.class.equal?(Integer)
504
+ raise OptionsError, 'lease_info[hours] should be an int'
505
+ end
506
+ end
507
+ if days
508
+ unless days.class.equal?(Integer)
509
+ raise OptionsError, 'lease_info[days] should be an int'
510
+ end
511
+ end
512
+
513
+ seconds += days * 24 * 60 * 60
514
+ seconds += hours * 60 * 60
515
+ seconds += minutes * 60
516
+ seconds / BLOCK_TIME_IN_SECONDS # rounded down
517
+ end
518
+
519
+ def self.lease_blocks_to_seconds(blocks)
520
+ blocks * BLOCK_TIME_IN_SECONDS
521
+ end
522
+
523
+ def self.validate_gas_info gas_info
524
+ unless gas_info.class.equal?(Hash)
525
+ raise OptionsError, 'gas_info should be a hash of {gas_price, max_fee, max_gas}'
526
+ end
527
+
528
+ gas_info_keys = %w[gas_price max_fee max_gas]
529
+ gas_info_keys.each do |k|
530
+ v = gas_info.fetch(k, 0)
531
+ unless v.class.equal?(Integer)
532
+ raise OptionsError, "gas_info[#{k}] should be an int"
533
+ end
534
+
535
+ gas_info[k] = v
536
+ end
537
+ gas_info
538
+ end
436
539
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bluzelle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - vbstreetz