Class: 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.
12 13 14 15 16 17 |
# File 'lib/tapyrus/store/db/level_db.rb', line 12 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.
9 10 11 |
# File 'lib/tapyrus/store/db/level_db.rb', line 9 def db @db end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/tapyrus/store/db/level_db.rb', line 10 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.
35 36 37 |
# File 'lib/tapyrus/store/db/level_db.rb', line 35 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.
40 41 42 |
# File 'lib/tapyrus/store/db/level_db.rb', line 40 def delete(key) db.delete(key) end |
#get(key) ⇒ Object
get value from specified key. @return the stored value.
30 31 32 |
# File 'lib/tapyrus/store/db/level_db.rb', line 30 def get(key) db.get(key) end |
#get_entry_payload_from_hash(hash) ⇒ String
get entry payload
57 58 59 |
# File 'lib/tapyrus/store/db/level_db.rb', line 57 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
45 46 47 |
# File 'lib/tapyrus/store/db/level_db.rb', line 45 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
50 51 52 |
# File 'lib/tapyrus/store/db/level_db.rb', line 50 def next_hash(hash) db.get(KEY_PREFIX[:next] + hash) end |
#put(key, value) ⇒ Object
put data into LevelDB.
22 23 24 25 |
# File 'lib/tapyrus/store/db/level_db.rb', line 22 def put(key, value) # logger.debug "put #{key} data" db.put(key, value) end |
#save_entry(entry) ⇒ Object
Save entry.
63 64 65 66 67 68 69 70 |
# File 'lib/tapyrus/store/db/level_db.rb', line 63 def save_entry(entry) db.batch do db.put(entry.key ,entry.to_payload) db.put(height_key(entry.height), entry.block_hash) add_agg_pubkey(entry.height == 0 ? 0 : entry.height + 1, entry.header.x_field) if entry.header.upgrade_agg_pubkey? connect_entry(entry) end end |