Class: Tapyrus::Store::DB::LevelDB

Inherits:
Object
  • Object
show all
Defined in:
lib/tapyrus/store/db/level_db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dbObject (readonly)

Returns the value of attribute db.



7
8
9
# File 'lib/tapyrus/store/db/level_db.rb', line 7

def db
  @db
end

#loggerObject (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.

Parameters:

  • activate_height (Integer)
  • agg_pubkey (String)

    aggregated public key with hex format.



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.

Parameters:

Returns:

  • (Array)

    tupple of activate height and aggregated public key.



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.

Parameters:

  • height (Integer)

    block height.

Returns:

  • (String)

    aggregated public key with hex format.



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_pubkeysArray[Array]

Get aggregated public key list.

Returns:

  • (Array[Array])

    list of public key and index



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_hashObject

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

#closeObject



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.

Parameters:

  • key (Object)

    a key.



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

Parameters:

  • hash (String)

    the hash with hex format.

Returns:

  • (String)

    the ChainEntry 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_pubkeyArray

Get latest aggregated public key.

Returns:

  • (Array)

    aggregated public key with hex format.



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.

Parameters:

  • key (Object)

    a key.

  • value (Object)

    a value.



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