Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature restructure db #4

Open
wants to merge 28 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fd0f23d
Improve build times
fpelliccioni Dec 21, 2020
bbb8e5c
remove implementation files
fpelliccioni Dec 21, 2020
978f52e
remove implementation files
fpelliccioni Dec 21, 2020
70d2a5c
remove implementation files
fpelliccioni Dec 21, 2020
9d19fb0
remove implementation files
fpelliccioni Dec 21, 2020
0d58dce
remove implementation files
fpelliccioni Dec 21, 2020
9fbd712
remove implementation files
fpelliccioni Dec 21, 2020
6e5029f
remove implementation files
fpelliccioni Dec 21, 2020
2993922
remove implementation files
fpelliccioni Dec 21, 2020
09fc9f0
Improve compilation times
fpelliccioni Dec 21, 2020
6f20b0d
Modularize DB parameters
fpelliccioni Dec 21, 2020
4b8be65
remove internal_database_basis template parameter
fpelliccioni Dec 21, 2020
60a303e
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
2f3748c
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
1f5d081
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
f4551d2
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
9b58d10
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
ab0000c
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
fe3a3c1
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
983b6db
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
8223b10
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
3920841
move the code from implementation file to translation unit
fpelliccioni Dec 21, 2020
7dfc3bc
refactor database class
fpelliccioni Dec 21, 2020
892cae4
add header file
fpelliccioni Dec 21, 2020
3dcbf66
add UTXO set header file
fpelliccioni Dec 21, 2020
282f134
Merge branch 'dev' into feature-restructure-db
fpelliccioni Dec 21, 2020
1400d5b
Merge branch 'dev' into feature-restructure-db
fpelliccioni Dec 21, 2020
c99de22
db tests
fpelliccioni Feb 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,15 @@ set(kth_database_sources_just_kth
if (DB_NEW)
set(kth_database_sources_just_kth
${kth_database_sources_just_kth}
src/databases/block_database.cpp
src/databases/internal_database.cpp
src/databases/header_database.cpp
src/databases/history_database.cpp
src/databases/reorg_database.cpp
src/databases/spend_database.cpp
src/databases/transaction_database.cpp
src/databases/transaction_unconfirmed_database.cpp
src/databases/utxo_database.cpp
src/databases/utxo_entry.cpp
)
endif()
Expand Down
3 changes: 1 addition & 2 deletions include/kth/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <kth/database/legacy/databases/transaction_unconfirmed_database.hpp>
#endif // KTH_DB_TRANSACTION_UNCONFIRMED

#ifdef KTH_DB_LEGACY
#include <kth/database/legacy/memory/accessor.hpp>
#include <kth/database/legacy/memory/allocator.hpp>
#include <kth/database/legacy/memory/memory.hpp>
Expand All @@ -67,7 +68,6 @@
#include <kth/database/legacy/primitives/slab_hash_table.hpp>
#include <kth/database/legacy/primitives/slab_manager.hpp>

#ifdef KTH_DB_LEGACY
#include <kth/database/legacy/result/block_result.hpp>
#include <kth/database/legacy/result/transaction_result.hpp>
#endif // KTH_DB_LEGACY
Expand All @@ -76,5 +76,4 @@
#include <kth/database/legacy/result/transaction_unconfirmed_result.hpp>
#endif // KTH_DB_TRANSACTION_UNCONFIRMED


#endif
42 changes: 42 additions & 0 deletions include/kth/database/databases/db_parameters.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2016-2020 Knuth Project developers.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef KTH_DATABASE_DB_PARAMETERS_HPP_
#define KTH_DATABASE_DB_PARAMETERS_HPP_

#ifdef KTH_INTERNAL_DB_4BYTES_INDEX
#define KTH_INTERNAL_DB_WIRE true
#else
#define KTH_INTERNAL_DB_WIRE false
#endif

#if defined(KTH_DB_READONLY)
#define KTH_DB_CONDITIONAL_CREATE 0
#else
#define KTH_DB_CONDITIONAL_CREATE KTH_DB_CREATE
#endif

#if defined(KTH_DB_READONLY)
#define KTH_DB_CONDITIONAL_READONLY KTH_DB_RDONLY
#else
#define KTH_DB_CONDITIONAL_READONLY 0
#endif

namespace kth::database {

#if defined(KTH_DB_NEW_FULL)
constexpr size_t max_dbs_ = 13;
#elif defined(KTH_DB_NEW_BLOCKS)
constexpr size_t max_dbs_ = 8;
#else
constexpr size_t max_dbs_ = 7;
#endif

// constexpr size_t env_open_mode_ = 0664;
constexpr size_t env_open_mode_ = 0644;
constexpr int directory_exists = 0;

} // namespace kth::database

#endif // KTH_DATABASE_DB_PARAMETERS_HPP_
134 changes: 28 additions & 106 deletions include/kth/database/databases/internal_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,25 @@
// #include <lmdb.h>
// #endif

#include <kth/database/databases/generic_db.hpp>

#include <kth/domain.hpp>
#include <kth/domain/chain/input_point.hpp>

#include <kth/database/define.hpp>

#include <kth/database/databases/result_code.hpp>
#include <kth/database/databases/db_parameters.hpp>
#include <kth/database/databases/generic_db.hpp>
#include <kth/database/databases/history_entry.hpp>
#include <kth/database/databases/property_code.hpp>
#include <kth/database/databases/result_code.hpp>
#include <kth/database/databases/tools.hpp>
#include <kth/database/databases/utxo_entry.hpp>
#include <kth/database/databases/history_entry.hpp>
#include <kth/database/databases/transaction_entry.hpp>
#include <kth/database/databases/transaction_unconfirmed_entry.hpp>
#include <kth/database/databases/utxo_entry.hpp>
#include <kth/database/define.hpp>

#include <kth/infrastructure.hpp>

#ifdef KTH_INTERNAL_DB_4BYTES_INDEX
#define KTH_INTERNAL_DB_WIRE true
#else
#define KTH_INTERNAL_DB_WIRE false
#endif

#if defined(KTH_DB_READONLY)
#define KTH_DB_CONDITIONAL_CREATE 0
#else
#define KTH_DB_CONDITIONAL_CREATE KTH_DB_CREATE
#endif

#if defined(KTH_DB_READONLY)
#define KTH_DB_CONDITIONAL_READONLY KTH_DB_RDONLY
#else
#define KTH_DB_CONDITIONAL_READONLY 0
#endif

namespace kth::database {

#if defined(KTH_DB_NEW_FULL)
constexpr size_t max_dbs_ = 13;
#elif defined(KTH_DB_NEW_BLOCKS)
constexpr size_t max_dbs_ = 8;
#else
constexpr size_t max_dbs_ = 7;
#endif

constexpr size_t env_open_mode_ = 0664;
constexpr int directory_exists = 0;

template <typename Clock = std::chrono::system_clock>
// template <typename Clock = std::chrono::system_clock>
class KD_API internal_database_basis {
public:
using path = kth::path;
Expand Down Expand Up @@ -107,10 +77,14 @@ class KD_API internal_database_basis {
bool close();

#if ! defined(KTH_DB_READONLY)

void print_stats(KTH_DB_txn* db_txn);

result_code push_genesis(domain::chain::block const& block);

//TODO(fernando): optimization: consider passing a list of outputs to insert and a list of inputs to delete instead of an entire Block.
// avoiding inserting and erasing internal spenders
template <typename Clock>
result_code push_block(domain::chain::block const& block, uint32_t height, uint32_t median_time_past);
#endif

Expand Down Expand Up @@ -165,6 +139,7 @@ class KD_API internal_database_basis {

bool open_internal();

template <typename Clock>
bool is_old_block(domain::chain::block const& block) const;

size_t get_db_page_size() const;
Expand All @@ -178,12 +153,6 @@ class KD_API internal_database_basis {
utxo_entry get_utxo(domain::chain::output_point const& point, KTH_DB_txn* db_txn) const;

#if ! defined(KTH_DB_READONLY)
result_code insert_reorg_pool(uint32_t height, KTH_DB_val& key, KTH_DB_txn* db_txn);

result_code remove_utxo(uint32_t height, domain::chain::output_point const& point, bool insert_reorg, KTH_DB_txn* db_txn);

result_code insert_utxo(domain::chain::output_point const& point, domain::chain::output const& output, data_chunk const& fixed_data, KTH_DB_txn* db_txn);

result_code remove_inputs(hash_digest const& tx_id, uint32_t height, domain::chain::input::list const& inputs, bool insert_reorg, KTH_DB_txn* db_txn);

result_code insert_outputs(hash_digest const& tx_id, uint32_t height, domain::chain::output::list const& outputs, data_chunk const& fixed_data, KTH_DB_txn* db_txn);
Expand All @@ -201,16 +170,12 @@ class KD_API internal_database_basis {

result_code push_block_header(domain::chain::block const& block, uint32_t height, KTH_DB_txn* db_txn);

result_code push_block_reorg(domain::chain::block const& block, uint32_t height, KTH_DB_txn* db_txn);

result_code push_block(domain::chain::block const& block, uint32_t height, uint32_t median_time_past, bool insert_reorg, KTH_DB_txn* db_txn);

result_code push_genesis(domain::chain::block const& block, KTH_DB_txn* db_txn);

result_code remove_outputs(hash_digest const& txid, domain::chain::output::list const& outputs, KTH_DB_txn* db_txn);

result_code insert_output_from_reorg_and_remove(domain::chain::output_point const& point, KTH_DB_txn* db_txn);

result_code insert_inputs(domain::chain::input::list const& inputs, KTH_DB_txn* db_txn);

template <typename I>
Expand All @@ -224,26 +189,15 @@ class KD_API internal_database_basis {

result_code remove_block_header(hash_digest const& hash, uint32_t height, KTH_DB_txn* db_txn);

result_code remove_block_reorg(uint32_t height, KTH_DB_txn* db_txn);

result_code remove_reorg_index(uint32_t height, KTH_DB_txn* db_txn);

result_code remove_block(domain::chain::block const& block, uint32_t height, KTH_DB_txn* db_txn);
#endif

domain::chain::header get_header(uint32_t height, KTH_DB_txn* db_txn) const;

domain::chain::block get_block_reorg(uint32_t height, KTH_DB_txn* db_txn) const;
domain::chain::block get_block_reorg(uint32_t height) const;

#if ! defined(KTH_DB_READONLY)
result_code remove_block(domain::chain::block const& block, uint32_t height);
result_code prune_reorg_index(uint32_t remove_until, KTH_DB_txn* db_txn);
result_code prune_reorg_block(uint32_t amount_to_delete, KTH_DB_txn* db_txn);
#endif

result_code get_first_reorg_block_height(uint32_t& out_height) const;

//TODO(fernando): is taking KTH_DB_val by value, is that Ok?
result_code insert_reorg_into_pool(utxo_pool_t& pool, KTH_DB_val key_point, KTH_DB_txn* db_txn) const;

Expand Down Expand Up @@ -359,65 +313,33 @@ class KD_API internal_database_basis {
#endif
};

template <typename Clock>
constexpr char internal_database_basis<Clock>::block_header_db_name[]; //key: block height, value: block header

template <typename Clock>
constexpr char internal_database_basis<Clock>::block_header_by_hash_db_name[]; //key: block hash, value: block height

template <typename Clock>
constexpr char internal_database_basis<Clock>::utxo_db_name[]; //key: point, value: output

template <typename Clock>
constexpr char internal_database_basis<Clock>::reorg_pool_name[]; //key: key: point, value: output

template <typename Clock>
constexpr char internal_database_basis<Clock>::reorg_index_name[]; //key: block height, value: point list

template <typename Clock>
constexpr char internal_database_basis<Clock>::reorg_block_name[]; //key: block height, value: block

template <typename Clock>
constexpr char internal_database_basis<Clock>::db_properties_name[]; //key: propery, value: data
constexpr char internal_database_basis::block_header_db_name[]; //key: block height, value: block header
constexpr char internal_database_basis::block_header_by_hash_db_name[]; //key: block hash, value: block height
constexpr char internal_database_basis::utxo_db_name[]; //key: point, value: output
constexpr char internal_database_basis::reorg_pool_name[]; //key: key: point, value: output
constexpr char internal_database_basis::reorg_index_name[]; //key: block height, value: point list
constexpr char internal_database_basis::reorg_block_name[]; //key: block height, value: block
constexpr char internal_database_basis::db_properties_name[]; //key: propery, value: data

#if defined(KTH_DB_NEW_BLOCKS) || defined(KTH_DB_NEW_FULL)
template <typename Clock>
constexpr char internal_database_basis<Clock>::block_db_name[]; //key: block height, value: block
//key: block height, value: tx hashes
constexpr char internal_database_basis::block_db_name[]; //key: block height, value: block
//key: block height, value: tx hashes
#endif

#ifdef KTH_DB_NEW_FULL
template <typename Clock>
constexpr char internal_database_basis<Clock>::transaction_db_name[]; //key: tx hash, value: tx

template <typename Clock>
constexpr char internal_database_basis<Clock>::transaction_hash_db_name[]; //key: tx hash, value: tx

template <typename Clock>
constexpr char internal_database_basis<Clock>::history_db_name[]; //key: tx hash, value: tx

template <typename Clock>
constexpr char internal_database_basis<Clock>::spend_db_name[]; //key: output_point, value: input_point

template <typename Clock>
constexpr char internal_database_basis<Clock>::transaction_unconfirmed_db_name[]; //key: tx hash, value: tx

constexpr char internal_database_basis::transaction_db_name[]; //key: tx hash, value: tx
constexpr char internal_database_basis::transaction_hash_db_name[]; //key: tx hash, value: tx
constexpr char internal_database_basis::history_db_name[]; //key: tx hash, value: tx
constexpr char internal_database_basis::spend_db_name[]; //key: output_point, value: input_point
constexpr char internal_database_basis::transaction_unconfirmed_db_name[];//key: tx hash, value: tx

#endif

using internal_database = internal_database_basis<std::chrono::system_clock>;
// using internal_database = internal_database_basis<std::chrono::system_clock>;
using internal_database = internal_database_basis;

} // namespace kth::database


#include <kth/database/databases/block_database.ipp>
#include <kth/database/databases/header_database.ipp>
#include <kth/database/databases/history_database.ipp>
#include <kth/database/databases/spend_database.ipp>
#include <kth/database/databases/transaction_unconfirmed_database.ipp>
#include <kth/database/databases/internal_database.ipp>
#include <kth/database/databases/reorg_database.ipp>
#include <kth/database/databases/transaction_database.ipp>
#include <kth/database/databases/utxo_database.ipp>

#endif // KTH_DATABASE_INTERNAL_DATABASE_HPP_
Loading