Class: Bluzelle::Swarm::Client
- Inherits:
-
Object
- Object
- Bluzelle::Swarm::Client
- 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
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#app_service ⇒ Object
readonly
Returns the value of attribute app_service.
-
#chain_id ⇒ Object
readonly
Returns the value of attribute chain_id.
-
#cosmos ⇒ Object
readonly
Returns the value of attribute cosmos.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#mnemonic ⇒ Object
readonly
Returns the value of attribute mnemonic.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Instance Method Summary collapse
-
#account ⇒ Hash
Retrieve information about the currently active Bluzelle account.
-
#count ⇒ Integer
Retrieve the number of keys in the current database/uuid.
-
#create(key, value, gas_info, lease_info = nil) ⇒ void
Create a field in the database.
-
#delete(key, gas_info) ⇒ void
Delete a field from the database.
-
#delete_all(gas_info) ⇒ void
Remove all keys in the current database/uuid.
-
#get_lease(key) ⇒ String
Retrieve the minimum time remaining on the lease for a key.
-
#get_n_shortest_leases(n) ⇒ Array
Retrieve a list of the n keys in the database with the shortest leases.
-
#has(key) ⇒ Boolean
Query to see if a key is in the database.
- #initialize(options = {}) ⇒ Bluzelle::Swarm::Client constructor
-
#key_values ⇒ Array
Enumerate all keys and values in the current database/uuid.
-
#keys ⇒ Array
Retrieve a list of all keys.
-
#multi_update(key_values, gas_info) ⇒ Object
Update multiple fields in the database.
-
#read(key, prove = false) ⇒ String
Retrieve the value of a key without consensus verification.
-
#rename(key, new_key, gas_info) ⇒ void
Change the name of an existing key.
-
#renew_lease(key, lease, gas_info) ⇒ Object
Update the minimum time remaining on the lease for a key.
-
#renew_lease_all(lease, gas_info) ⇒ Object
Update the minimum time remaining on the lease for all keys.
-
#tx_count(gas_info) ⇒ Integer
Retrieve the number of keys in the current database/uuid via a transaction.
-
#tx_get_lease(key, gas_info) ⇒ String
Retrieve the minimum time remaining on the lease for a key, using a transaction.
-
#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.
-
#tx_has(key, gas_info) ⇒ Boolean
Query to see if a key is in the database via a transaction (i.e uses consensus).
-
#tx_key_values(gas_info) ⇒ Array
Enumerate all keys and values in the current database/uuid via a transaction.
-
#tx_keys(gas_info) ⇒ Array
Retrieve a list of all keys via a transaction (i.e use consensus).
-
#tx_read(key, gas_info) ⇒ String
Retrieve the value of a key via a transaction (i.e uses consensus).
-
#update(key, value, gas_info, lease_info = nil) ⇒ void
Update a field in the database.
-
#version ⇒ String
Retrieve the version of the Bluzelle service.
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
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( = {}) = stringify_keys() validate_string(['mnemonic'], 'Mnemonic must be a string.') validate_string(['uuid'], 'UUID must be a string.') @mnemonic = ['mnemonic'] @uuid = ['uuid'] @chain_id = ['chain_id'] || 'bluzelle' @endpoint = ['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
#address ⇒ Object (readonly)
Returns the value of attribute address.
11 12 13 |
# File 'lib/bluzelle/swarm/client.rb', line 11 def address @address end |
#app_service ⇒ Object (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_id ⇒ Object (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 |
#cosmos ⇒ Object (readonly)
Returns the value of attribute cosmos.
12 13 14 |
# File 'lib/bluzelle/swarm/client.rb', line 12 def cosmos @cosmos end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
11 12 13 |
# File 'lib/bluzelle/swarm/client.rb', line 11 def endpoint @endpoint end |
#mnemonic ⇒ Object (readonly)
Returns the value of attribute mnemonic.
11 12 13 |
# File 'lib/bluzelle/swarm/client.rb', line 11 def mnemonic @mnemonic end |
#uuid ⇒ Object (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
#account ⇒ Hash
Retrieve information about the currently active Bluzelle account
403 404 405 406 |
# File 'lib/bluzelle/swarm/client.rb', line 403 def account @cosmos.query("auth/accounts/#{@address}") .dig('result', 'value') end |
#count ⇒ Integer
Retrieve the number of keys in the current database/uuid. This function bypasses the consensus and cryptography mechanisms in favor of speed
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
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
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
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
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
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.
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_values ⇒ Array
Enumerate all keys and values in the current database/uuid. This function bypasses the consensus and cryptography mechanisms in favor of speed
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 |
#keys ⇒ Array
Retrieve a list of all keys. This function bypasses the consensus and cryptography mechanisms in favour of speed.
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
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
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
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
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
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
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
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
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)
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
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)
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)
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
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 |
#version ⇒ String
Retrieve the version of the Bluzelle service
411 412 413 414 |
# File 'lib/bluzelle/swarm/client.rb', line 411 def version @cosmos.query('node_info') .dig('application_version', 'version') end |