Class: Tapyrus::Store::DB::LevelDB
- Inherits:
-
Object
- Object
- Tapyrus::Store::DB::LevelDB
- Defined in:
- lib/tapyrus/store/db/level_db.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#add_agg_pubkey(activate_height, agg_pubkey) ⇒ Object
Add aggregated public key.
-
#agg_pubkey(index) ⇒ Array
Get aggregated public key by specifying
index. -
#agg_pubkey_with_height(height) ⇒ String
Get aggregated public key by specifying block
height. -
#agg_pubkeys ⇒ Array[Array]
Get aggregated public key list.
-
#best_hash ⇒ Object
get best block hash.
- #close ⇒ Object
-
#delete(key) ⇒ Object
delete specified key data.
-
#get(key) ⇒ Object
get value from specified key.
-
#get_entry_payload_from_hash(hash) ⇒ String
get entry payload.
-
#get_hash_from_height(height) ⇒ Object
get block hash specified
height. -
#initialize(path = "#{Tapyrus.base_dir}/db/spv") ⇒ LevelDB
constructor
A new instance of LevelDB.
-
#latest_agg_pubkey ⇒ Array
Get latest aggregated public key.
-
#next_hash(hash) ⇒ Object
get next block hash specified
hash. -
#put(key, value) ⇒ Object
put data into LevelDB.
-
#save_entry(entry) ⇒ Object
Save entry.
Constructor Details
#initialize(path = "#{Tapyrus.base_dir}/db/spv") ⇒ LevelDB
Returns a new instance of LevelDB.
10 11 12 13 14 15 |
# File 'lib/tapyrus/store/db/level_db.rb', line 10 def initialize(path = "#{Tapyrus.base_dir}/db/spv") # @logger = Tapyrus::Logger.create(:debug) FileUtils.mkdir_p(path) @db = ::LevelDBNative::DB.new(path) # logger.debug 'Opened LevelDB successfully.' end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
7 8 9 |
# File 'lib/tapyrus/store/db/level_db.rb', line 7 def db @db end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/tapyrus/store/db/level_db.rb', line 8 def logger @logger end |
Instance Method Details
#add_agg_pubkey(activate_height, agg_pubkey) ⇒ Object
Add aggregated public key.
75 76 77 78 79 80 81 82 83 |
# File 'lib/tapyrus/store/db/level_db.rb', line 75 def add_agg_pubkey(activate_height, agg_pubkey) payload = activate_height.to_even_length_hex + agg_pubkey index = latest_agg_pubkey_index next_index = (index.nil? ? 0 : index + 1).to_even_length_hex db.batch do db.put(KEY_PREFIX[:agg_pubkey] + next_index, payload) db.put(KEY_PREFIX[:latest_agg_pubkey], next_index) end end |
#agg_pubkey(index) ⇒ Array
Get aggregated public key by specifying index.
88 89 90 91 |
# File 'lib/tapyrus/store/db/level_db.rb', line 88 def agg_pubkey(index) payload = db.get(KEY_PREFIX[:agg_pubkey] + index.to_even_length_hex) [payload[0...(payload.length - 66)].to_i(16), payload[(payload.length - 66)..-1]] end |
#agg_pubkey_with_height(height) ⇒ String
Get aggregated public key by specifying block height.
96 97 98 99 100 101 102 103 104 |
# File 'lib/tapyrus/store/db/level_db.rb', line 96 def agg_pubkey_with_height(height) index = latest_agg_pubkey_index index ||= 0 (index + 1).times do |i| target = index - i active_height, pubkey = agg_pubkey(target) return pubkey unless active_height > height end end |
#agg_pubkeys ⇒ Array[Array]
Get aggregated public key list.
114 115 116 117 |
# File 'lib/tapyrus/store/db/level_db.rb', line 114 def agg_pubkeys index = latest_agg_pubkey_index (index + 1).times.map { |i| agg_pubkey(i) } end |
#best_hash ⇒ Object
get best block hash.
33 34 35 |
# File 'lib/tapyrus/store/db/level_db.rb', line 33 def best_hash db.get(KEY_PREFIX[:best]) end |
#close ⇒ Object
119 120 121 |
# File 'lib/tapyrus/store/db/level_db.rb', line 119 def close db.close end |
#delete(key) ⇒ Object
delete specified key data.
38 39 40 |
# File 'lib/tapyrus/store/db/level_db.rb', line 38 def delete(key) db.delete(key) end |
#get(key) ⇒ Object
get value from specified key. @return the stored value.
28 29 30 |
# File 'lib/tapyrus/store/db/level_db.rb', line 28 def get(key) db.get(key) end |
#get_entry_payload_from_hash(hash) ⇒ String
get entry payload
55 56 57 |
# File 'lib/tapyrus/store/db/level_db.rb', line 55 def get_entry_payload_from_hash(hash) db.get(KEY_PREFIX[:entry] + hash) end |
#get_hash_from_height(height) ⇒ Object
get block hash specified height
43 44 45 |
# File 'lib/tapyrus/store/db/level_db.rb', line 43 def get_hash_from_height(height) db.get(height_key(height)) end |
#latest_agg_pubkey ⇒ Array
Get latest aggregated public key.
108 109 110 |
# File 'lib/tapyrus/store/db/level_db.rb', line 108 def latest_agg_pubkey agg_pubkey(latest_agg_pubkey_index)[1] end |
#next_hash(hash) ⇒ Object
get next block hash specified hash
48 49 50 |
# File 'lib/tapyrus/store/db/level_db.rb', line 48 def next_hash(hash) db.get(KEY_PREFIX[:next] + hash) end |
#put(key, value) ⇒ Object
put data into LevelDB.
20 21 22 23 |
# File 'lib/tapyrus/store/db/level_db.rb', line 20 def put(key, value) # logger.debug "put #{key} data" db.put(key, value) end |
#save_entry(entry) ⇒ Object
Save entry.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tapyrus/store/db/level_db.rb', line 61 def save_entry(entry) db.batch do db.put(entry.key, entry.to_payload) db.put(height_key(entry.height), entry.block_hash) if entry.header.upgrade_agg_pubkey? add_agg_pubkey(entry.height == 0 ? 0 : entry.height + 1, entry.header.x_field) end connect_entry(entry) end end |