class Bitcoin::Store::DB::LevelDB

Attributes

db[R]
logger[R]

Public Class Methods

new(path = " click to toggle source
# File lib/bitcoin/store/db/level_db.rb, line 12
def initialize(path = "#{Bitcoin.base_dir}/db/spv")
  # @logger = Bitcoin::Logger.create(:debug)
  FileUtils.mkdir_p(path)
  @db = ::LevelDBNative::DB.new(path)
  # logger.debug 'Opened LevelDB successfully.'
end

Public Instance Methods

best_hash() click to toggle source

get best block hash.

# File lib/bitcoin/store/db/level_db.rb, line 35
def best_hash
  db.get(KEY_PREFIX[:best])
end
close() click to toggle source
# File lib/bitcoin/store/db/level_db.rb, line 69
def close
  db.close
end
delete(key) click to toggle source

delete specified key data.

# File lib/bitcoin/store/db/level_db.rb, line 40
def delete(key)
  db.delete(key)
end
get(key) click to toggle source

get value from specified key. @param [Object] key a key. @return the stored value.

# File lib/bitcoin/store/db/level_db.rb, line 30
def get(key)
  db.get(key)
end
get_entry_payload_from_hash(hash) click to toggle source

get entry payload @param [String] hash the hash with hex format. @return [String] the ChainEntry payload.

# File lib/bitcoin/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) click to toggle source

get block hash specified height

# File lib/bitcoin/store/db/level_db.rb, line 45
def get_hash_from_height(height)
  db.get(height_key(height))
end
next_hash(hash) click to toggle source

get next block hash specified hash

# File lib/bitcoin/store/db/level_db.rb, line 50
def next_hash(hash)
  db.get(KEY_PREFIX[:next] + hash)
end
put(key, value) click to toggle source

put data into LevelDB. @param [Object] key a key. @param [Object] value a value.

# File lib/bitcoin/store/db/level_db.rb, line 22
def put(key, value)
  # logger.debug "put #{key} data"
  db.put(key, value)
end
save_entry(entry) click to toggle source
# File lib/bitcoin/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)
    connect_entry(entry)
  end
end

Private Instance Methods

connect_entry(entry) click to toggle source
# File lib/bitcoin/store/db/level_db.rb, line 81
def connect_entry(entry)
  unless entry.genesis?
    tip_block = Bitcoin::Store::ChainEntry.parse_from_payload(get_entry_payload_from_hash(best_hash))
    unless tip_block.block_hash == entry.prev_hash
      raise "entry(#{entry.block_hash}) does not reference current best block hash(#{tip_block.block_hash})"
    end
    unless tip_block.height + 1 == entry.height
      raise "block height is small than current best block."
    end
  end
  db.put(KEY_PREFIX[:best], entry.block_hash)
  db.put(KEY_PREFIX[:next] + entry.prev_hash, entry.block_hash)
end
height_key(height) click to toggle source

generate height key

# File lib/bitcoin/store/db/level_db.rb, line 76
def height_key(height)
  height = height.to_even_length_hex
  KEY_PREFIX[:height] + height.rhex
end