Class: Bluzelle::Swarm::Client

Inherits:
Object
  • Object
show all
Includes:
Constants, Utils
Defined in:
lib/bluzelle/swarm/client.rb

Constant Summary

Constants included from Constants

Constants::BLOCK_TIME_IN_SECONDS, Constants::BROADCAST_RETRY_SECONDS, Constants::PATH, Constants::PREFIX, Constants::TOKEN_NAME, Constants::TX_COMMAND

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

bech32_convert_bits, bech32_encode, bip32_from_seed, bip39_mnemonic_to_seed, compressed_pub_key, convert_lease, create_ec_pair, decode_json, ecdsa_sign, encode_json, extract_error_message, get_address, get_ec_private_key, get_ec_public_key_from_priv, hex_to_bin, make_random_string, open_key, rmd_160_digest, sha_256_digest, sort_hash, stringify_keys, to_base64, to_bytes, validate_address

Constructor Details

#initialize(options = {}) ⇒ Bluzelle::Swarm::Client

Parameters:

  • options (Hash) (defaults to: {})


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bluzelle/swarm/client.rb', line 18

def initialize(options = {})
  options = stringify_keys(options)

  validate_string(options['mnemonic'], 'Mnemonic must be a string.')
  validate_string(options['uuid'], 'UUID must be a string.')

  @mnemonic = options['mnemonic']
  @uuid = options['uuid']
  @chain_id = options['chain_id'] || 'bluzelle'
  @endpoint = options['endpoint'] || 'http://localhost:1317'
  @app_service = 'crud'

  @cosmos = Cosmos.new(
    mnemonic: @mnemonic,
    endpoint: @endpoint,
    chain_id: @chain_id
  )

  @address = @cosmos.address
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



11
12
13
# File 'lib/bluzelle/swarm/client.rb', line 11

def address
  @address
end

#app_serviceObject (readonly)

Returns the value of attribute app_service.



11
12
13
# File 'lib/bluzelle/swarm/client.rb', line 11

def app_service
  @app_service
end

#chain_idObject (readonly)

Returns the value of attribute chain_id.



11
12
13
# File 'lib/bluzelle/swarm/client.rb', line 11

def chain_id
  @chain_id
end

#cosmosObject (readonly)

Returns the value of attribute cosmos.



12
13
14
# File 'lib/bluzelle/swarm/client.rb', line 12

def cosmos
  @cosmos
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



11
12
13
# File 'lib/bluzelle/swarm/client.rb', line 11

def endpoint
  @endpoint
end

#mnemonicObject (readonly)

Returns the value of attribute mnemonic.



11
12
13
# File 'lib/bluzelle/swarm/client.rb', line 11

def mnemonic
  @mnemonic
end

#uuidObject (readonly)

Returns the value of attribute uuid.



11
12
13
# File 'lib/bluzelle/swarm/client.rb', line 11

def uuid
  @uuid
end

Instance Method Details

#accountHash

Retrieve information about the currently active Bluzelle account

Returns:

  • (Hash)


403
404
405
406
# File 'lib/bluzelle/swarm/client.rb', line 403

def 
  @cosmos.query("auth/accounts/#{@address}")
         .dig('result', 'value')
end

#countInteger

Retrieve the number of keys in the current database/uuid. This function bypasses the consensus and cryptography mechanisms in favor of speed

Returns:

  • (Integer)


214
215
216
217
# File 'lib/bluzelle/swarm/client.rb', line 214

def count
  @cosmos.query("#{app_service}/count/#{@uuid}")
         .dig('result', 'count')
end

#create(key, value, gas_info, lease_info = nil) ⇒ void

This method returns an undefined value.

Create a field in the database

Parameters:

  • key (String)

    The name of the key to create

  • value (String)

    The string value to set the key

  • gas_info (Hash)

    Hash containing gas parameters

  • lease_info (Hash) (defaults to: nil)

    Minimum time for key to remain in database



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bluzelle/swarm/client.rb', line 47

def create(key, value, gas_info, lease_info = nil)
  validate_string(key, 'key must be a string')
  validate_string(value, 'value must be a string')

  lease = convert_lease(lease_info)

  validate_lease(lease, 'invalid lease time')

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/create",
    build_params({ 'Key' => key, 'Value' => value, 'Lease' => lease }),
    gas_info
  )
end

#delete(key, gas_info) ⇒ void

This method returns an undefined value.

Delete a field from the database

Parameters:

  • key (String)

    The name of the key to delete

  • gas_info (Hash)

    Hash containing gas parameters



126
127
128
129
130
131
132
133
134
135
# File 'lib/bluzelle/swarm/client.rb', line 126

def delete(key, gas_info)
  validate_string(key, 'Key must be a string')

  @cosmos.send_transaction(
    'delete',
    "#{@app_service}/delete",
    build_params({ Key: key }),
    gas_info
  )
end

#delete_all(gas_info) ⇒ void

This method returns an undefined value.

Remove all keys in the current database/uuid

Parameters:

  • gas_info (Hash)

    Hash containing gas parameters



238
239
240
241
242
243
244
245
# File 'lib/bluzelle/swarm/client.rb', line 238

def delete_all(gas_info)
  @cosmos.send_transaction(
    'post',
    "#{@app_service}/deleteall",
    build_params({}),
    gas_info
  )
end

#get_lease(key) ⇒ String

Retrieve the minimum time remaining on the lease for a key. This function bypasses the consensus and cryptography mechanisms in favor of speed

Parameters:

  • key (String)

Returns:

  • (String)


296
297
298
299
300
301
# File 'lib/bluzelle/swarm/client.rb', line 296

def get_lease(key)
  validate_string(key, 'key must be a string')

  @cosmos.query("#{@app_service}/getlease/#{@uuid}/#{key}")
         .dig('result', 'lease').to_i * BLOCK_TIME_IN_SECONDS
end

#get_n_shortest_leases(n) ⇒ Array

Retrieve a list of the n keys in the database with the shortest leases. This function bypasses the consensus and cryptography mechanisms in favor of speed

Parameters:

  • n (Integer)

    The number of keys to retrieve the lease information for

Returns:

  • (Array)


363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/bluzelle/swarm/client.rb', line 363

def get_n_shortest_leases(n)
  validate_lease(n, 'invalid value specified')

  @cosmos.query("#{@app_service}/getnshortestleases/#{@uuid}/#{n}")
         .dig('result', 'keyleases')
         .map do |key_lease|
    {
      'key' => key_lease['key'],
      'lease' => key_lease['lease'].to_i * BLOCK_TIME_IN_SECONDS
    }
  end
end

#has(key) ⇒ Boolean

Query to see if a key is in the database. This function bypasses the consensus and cryptography mechanisms in favour of speed.

Parameters:

  • key (String)

    The name of the key to query

Returns:

  • (Boolean)


143
144
145
146
147
148
# File 'lib/bluzelle/swarm/client.rb', line 143

def has(key)
  validate_string(key, 'Key must be a string')

  @cosmos.query("#{@app_service}/has/#{@uuid}/#{key}")
         .dig('result', 'has')
end

#key_valuesArray

Enumerate all keys and values in the current database/uuid. This function bypasses the consensus and cryptography mechanisms in favor of speed

Returns:

  • (Array)


251
252
253
254
# File 'lib/bluzelle/swarm/client.rb', line 251

def key_values
  @cosmos.query("#{app_service}/keyvalues/#{@uuid}")
         .dig('result', 'keyvalues') || []
end

#keysArray

Retrieve a list of all keys. This function bypasses the consensus and cryptography mechanisms in favour of speed.

Returns:

  • (Array)


171
172
173
174
# File 'lib/bluzelle/swarm/client.rb', line 171

def keys
  @cosmos.query("#{@app_service}/keys/#{@uuid}")
         .dig('result', 'keys') || []
end

#multi_update(key_values, gas_info) ⇒ Object

Update multiple fields in the database

Parameters:

  • (Array)
  • gas_info (Hash)

    Hash containing gas parameters



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/bluzelle/swarm/client.rb', line 274

def multi_update(key_values, gas_info)
  validate_array(key_values, 'key_values must be an array')

  key_values.each do |key_value|
    validate_string(key_value.dig('key'), 'All keys must be strings')
    validate_string(key_value.dig('value'), 'All values must be string')
  end

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/multiupdate",
    build_params({ KeyValues: key_values }),
    gas_info
  )
end

#read(key, prove = false) ⇒ String

Retrieve the value of a key without consensus verification

Parameters:

  • key (String)

    The key to retrieve

  • prove (Boolean) (defaults to: false)

Returns:

  • (String)

    String value of the key



93
94
95
96
97
98
99
100
101
# File 'lib/bluzelle/swarm/client.rb', line 93

def read(key, prove = false)
  validate_string(key, 'Key must be a string')

  path = prove ? 'pread' : 'read'
  url = "#{@app_service}/#{path}/#{@uuid}/#{key}"

  @cosmos.query(url)
         .dig('result', 'value')
end

#rename(key, new_key, gas_info) ⇒ void

This method returns an undefined value.

Change the name of an existing key

Parameters:

  • key (String)
  • new_key (String)
  • gas_info (Hash)

    Hash containing gas parameters



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/bluzelle/swarm/client.rb', line 197

def rename(key, new_key, gas_info)
  validate_string(key, 'key must be a string')
  validate_string(new_key, 'new_key must be a string')

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/rename",
    build_params({ Key: key, NewKey: new_key }),
    gas_info
  )
end

#renew_lease(key, lease, gas_info) ⇒ Object

Update the minimum time remaining on the lease for a key

Parameters:

  • key (String)

    The key to retrieve the lease information for

  • gas_info (Hash)

    Hash containing gas parameters

  • lease (Hash)

    Minimum time for key to remain in database



325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/bluzelle/swarm/client.rb', line 325

def renew_lease(key, lease, gas_info)
  validate_string(key, 'key must be a string')

  lease = convert_lease(lease)

  validate_lease(lease, 'invalid lease time')

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/renewlease",
    build_params({ Key: key, Lease: lease }),
    gas_info
  )
end

#renew_lease_all(lease, gas_info) ⇒ Object

Update the minimum time remaining on the lease for all keys

Parameters:

  • gas_info (Hash)

    Hash containing gas parameters

  • lease (Hash)

    Minimum time for key to remain in database



344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/bluzelle/swarm/client.rb', line 344

def renew_lease_all(lease, gas_info)
  lease = convert_lease(lease)

  validate_lease(lease, 'invalid lease time')

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/renewleaseall",
    build_params({ Lease: lease }),
    gas_info
  )
end

#tx_count(gas_info) ⇒ Integer

Retrieve the number of keys in the current database/uuid via a transaction

Parameters:

  • gas_info (Hash)

    Hash containing gas parameters

Returns:

  • (Integer)


224
225
226
227
228
229
230
231
# File 'lib/bluzelle/swarm/client.rb', line 224

def tx_count(gas_info)
  @cosmos.send_transaction(
    'post',
    "#{@app_service}/count",
    build_params({}),
    gas_info
  ).dig('count')
end

#tx_get_lease(key, gas_info) ⇒ String

Retrieve the minimum time remaining on the lease for a key, using a transaction

Parameters:

  • key (String)

    The key to retrieve the lease information for

  • gas_info (Hash)

    Hash containing gas parameters

Returns:

  • (String)


309
310
311
312
313
314
315
316
317
318
# File 'lib/bluzelle/swarm/client.rb', line 309

def tx_get_lease(key, gas_info)
  validate_string(key, 'key must be a string')

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/getlease",
    build_params({ Key: key }),
    gas_info
  ).dig('lease').to_i * BLOCK_TIME_IN_SECONDS
end

#tx_get_n_shortest_leases(n, gas_info) ⇒ Array

Retrieve a list of the N keys/values in the database with the shortest leases, using a transaction

Parameters:

  • n (Integer)

    The number of keys to retrieve the lease information for

  • gas_info (Hash)

    Hash containing lize(options = {})gas parameters

Returns:

  • (Array)


383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/bluzelle/swarm/client.rb', line 383

def tx_get_n_shortest_leases(n, gas_info)
  validate_lease(n, 'invalid value specified')

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/getnshortestleases",
    build_params({ N: n.to_s }),
    gas_info
  ).dig('keyleases')
         .map do |key_lease|
    {
      'key' => key_lease['key'],
      'lease' => key_lease['lease'].to_i * BLOCK_TIME_IN_SECONDS
    }
  end
end

#tx_has(key, gas_info) ⇒ Boolean

Query to see if a key is in the database via a transaction (i.e uses consensus)

Parameters:

  • key (String)

    The name of the key to query

  • gas_info (Hash)

    Hash containing gas parameters

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
165
# File 'lib/bluzelle/swarm/client.rb', line 156

def tx_has(key, gas_info)
  validate_string(key, 'Key must be a string')

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/has",
    build_params({ Key: key }),
    gas_info
  ).dig('has')
end

#tx_key_values(gas_info) ⇒ Array

Enumerate all keys and values in the current database/uuid via a transaction

Parameters:

  • gas_info (Hash)

    Hash containing gas parameters

Returns:

  • (Array)


261
262
263
264
265
266
267
268
# File 'lib/bluzelle/swarm/client.rb', line 261

def tx_key_values(gas_info)
  @cosmos.send_transaction(
    'post',
    "#{@app_service}/keyvalues",
    build_params({}),
    gas_info
  ).dig('keyvalues') || []
end

#tx_keys(gas_info) ⇒ Array

Retrieve a list of all keys via a transaction (i.e use consensus)

Parameters:

  • gas_info (Hash)

    Hash containing gas parameters

Returns:

  • (Array)


181
182
183
184
185
186
187
188
# File 'lib/bluzelle/swarm/client.rb', line 181

def tx_keys(gas_info)
  @cosmos.send_transaction(
    'post',
    "#{@app_service}/keys",
    build_params({}),
    gas_info
  ).dig('keys') || []
end

#tx_read(key, gas_info) ⇒ String

Retrieve the value of a key via a transaction (i.e uses consensus)

Parameters:

  • key (String)

    The key to retrieve

  • gas_info (Hash)

    Hash containing gas parameters

Returns:

  • (String)

    String value of the key



109
110
111
112
113
114
115
116
117
118
# File 'lib/bluzelle/swarm/client.rb', line 109

def tx_read(key, gas_info)
  validate_string(key, 'Key must be a string')

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/read",
    build_params({ Key: key }),
    gas_info
  ).dig('value')
end

#update(key, value, gas_info, lease_info = nil) ⇒ void

This method returns an undefined value.

Update a field in the database

Parameters:

  • key (String)

    The name of the key to update

  • value (String)

    The string value to set the key

  • gas_info (Hash)

    Hash containing gas parameters

  • lease_info (Hash) (defaults to: nil)

    Minimum time for key to remain in database



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bluzelle/swarm/client.rb', line 71

def update(key, value, gas_info, lease_info = nil)
  validate_string(key, 'Key must be a string')
  validate_string(value, 'Value must be a string')

  lease = convert_lease(lease_info)

  validate_lease(lease, 'invalid lease time')

  @cosmos.send_transaction(
    'post',
    "#{@app_service}/update",
    build_params({ Key: key, Value: value, Lease: lease }),
    gas_info
  )
end

#versionString

Retrieve the version of the Bluzelle service

Returns:

  • (String)


411
412
413
414
# File 'lib/bluzelle/swarm/client.rb', line 411

def version
  @cosmos.query('node_info')
         .dig('application_version', 'version')
end