From 74a1a89e27c719811a1fcc0578e4795802555225 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Mon, 27 Oct 2014 23:44:45 -0400 Subject: Integrate BlockchainDB into cryptonote_core Probably needs more looking at -- lot of things were done...in a rushed sort of way. That said, it all builds and *should* be at least testable. update for rebase (warptangent 2015-01-04) fix conflicts with upstream CMakeLists.txt files src/CMakeLists.txt (remove edits from original commit) tests/CMakeLists.txt (remove edits from original commit) src/cryptonote_core/CMakeLists.txt (edit) - use blockchain db .cpp and .h files - add LMDB_LIBRARIES --- src/cryptonote_core/BlockchainDB_impl/db_lmdb.cpp | 1 + src/cryptonote_core/CMakeLists.txt | 7 ++++ src/cryptonote_core/blockchain.cpp | 39 +++++++++++++++++++---- src/cryptonote_core/blockchain.h | 10 ++++-- src/cryptonote_core/cryptonote_core.cpp | 6 ++-- src/cryptonote_core/cryptonote_core.h | 6 ++-- src/cryptonote_core/tx_pool.cpp | 4 +-- src/cryptonote_core/tx_pool.h | 9 ++---- 8 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/cryptonote_core/BlockchainDB_impl/db_lmdb.cpp b/src/cryptonote_core/BlockchainDB_impl/db_lmdb.cpp index 218bd4b63..6275ae388 100644 --- a/src/cryptonote_core/BlockchainDB_impl/db_lmdb.cpp +++ b/src/cryptonote_core/BlockchainDB_impl/db_lmdb.cpp @@ -672,6 +672,7 @@ void BlockchainLMDB::open(const std::string& filename) // unused for now, create will happen on open if doesn't exist void BlockchainLMDB::create(const std::string& filename) { + throw DB_CREATE_FAILURE("create() is not implemented for this BlockchainDB, open() will create files if needed."); } void BlockchainLMDB::close() diff --git a/src/cryptonote_core/CMakeLists.txt b/src/cryptonote_core/CMakeLists.txt index 3abf93f3c..93c3cb51e 100644 --- a/src/cryptonote_core/CMakeLists.txt +++ b/src/cryptonote_core/CMakeLists.txt @@ -29,6 +29,9 @@ set(cryptonote_core_sources account.cpp blockchain_storage.cpp + blockchain.cpp + blockchain_db.cpp + BlockchainDB_impl/db_lmdb.cpp checkpoints.cpp checkpoints_create.cpp cryptonote_basic_impl.cpp @@ -45,6 +48,9 @@ set(cryptonote_core_private_headers account_boost_serialization.h blockchain_storage.h blockchain_storage_boost_serialization.h + blockchain.h + blockchain_db.h + BlockchainDB_impl/db_lmdb.h checkpoints.h checkpoints_create.h connection_context.h @@ -77,4 +83,5 @@ target_link_libraries(cryptonote_core ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_THREAD_LIBRARY} + ${LMDB_LIBRARIES} ${EXTRA_LIBRARIES}) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index a35ce846c..0f5765281 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -32,10 +32,14 @@ #include #include #include +#include #include "include_base_utils.h" #include "cryptonote_basic_impl.h" +#include "tx_pool.h" #include "blockchain.h" +#include "cryptonote_core/blockchain_db.h" +#include "cryptonote_core/BlockchainDB_impl/db_lmdb.h" #include "cryptonote_format_utils.h" #include "cryptonote_boost_serialization.h" #include "cryptonote_config.h" @@ -62,10 +66,6 @@ DISABLE_VS_WARNINGS(4267) // TODO: initialize m_db with a concrete implementation of BlockchainDB Blockchain::Blockchain(tx_memory_pool& tx_pool):m_db(), m_tx_pool(tx_pool), m_current_block_cumul_sz_limit(0), m_is_in_checkpoint_zone(false), m_is_blockchain_storing(false) { - if (m_db == NULL) - { - throw new DB_ERROR("database pointer null in blockchain init"); - } } //------------------------------------------------------------------ //TODO: is this still needed? I don't think so - tewinget @@ -222,11 +222,23 @@ bool Blockchain::init(const std::string& config_folder, bool testnet) { CRITICAL_REGION_LOCAL(m_blockchain_lock); + m_db = new BlockchainLMDB(); + m_config_folder = config_folder; + m_testnet = testnet; + + boost::filesystem::path folder(m_config_folder); + + // append "testnet" directory as needed + if (testnet) + { + folder /= "testnet"; + } + LOG_PRINT_L0("Loading blockchain..."); //FIXME: update filename for BlockchainDB - const std::string filename = m_config_folder + "/" CRYPTONOTE_BLOCKCHAINDATA_FILENAME; + const std::string filename = folder.string(); try { m_db->open(filename); @@ -328,6 +340,8 @@ bool Blockchain::deinit() { LOG_PRINT_L0("There was an issue closing/storing the blockchain, shutting down now to prevent issues!"); } + + delete m_db; return true; } //------------------------------------------------------------------ @@ -1450,7 +1464,7 @@ uint64_t Blockchain::block_difficulty(uint64_t i) } //------------------------------------------------------------------ template -void Blockchain::get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) +bool Blockchain::get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) { CRITICAL_REGION_LOCAL(m_blockchain_lock); @@ -1464,11 +1478,16 @@ void Blockchain::get_blocks(const t_ids_container& block_ids, t_blocks_container { missed_bs.push_back(block_hash); } + catch (const std::exception& e) + { + return false; + } } + return true; } //------------------------------------------------------------------ template -void Blockchain::get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) +bool Blockchain::get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) { CRITICAL_REGION_LOCAL(m_blockchain_lock); @@ -1482,7 +1501,13 @@ void Blockchain::get_transactions(const t_ids_container& txs_ids, t_tx_container { missed_txs.push_back(tx_hash); } + //FIXME: is this the correct way to handle this? + catch (const std::exception& e) + { + return false; + } } + return true; } //------------------------------------------------------------------ void Blockchain::print_blockchain(uint64_t start_index, uint64_t end_index) diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 884de122d..4024fa741 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -41,7 +41,6 @@ #include "syncobj.h" #include "string_tools.h" -#include "tx_pool.h" #include "cryptonote_basic.h" #include "common/util.h" #include "cryptonote_protocol/cryptonote_protocol_defs.h" @@ -55,6 +54,7 @@ namespace cryptonote { + class tx_memory_pool; /************************************************************************/ /* */ @@ -131,16 +131,19 @@ namespace cryptonote uint64_t block_difficulty(uint64_t i); template - void get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs); + bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs); template - void get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs); + bool get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs); //debug functions void print_blockchain(uint64_t start_index, uint64_t end_index); void print_blockchain_index(); void print_blockchain_outs(const std::string& file); + void set_enforce_dns_checkpoints(bool enforce) { } + bool update_checkpoints(const std::string& path, bool dns) { return true; } + private: typedef std::unordered_map blocks_by_id_index; typedef std::unordered_map transactions_container; @@ -175,6 +178,7 @@ namespace cryptonote checkpoints m_checkpoints; std::atomic m_is_in_checkpoint_zone; std::atomic m_is_blockchain_storing; + bool m_testnet; bool switch_to_alternative_blockchain(std::list& alt_chain, bool discard_disconnected_chain); block pop_block_from_blockchain(); diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index b8b5dc008..e2c533fe5 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -290,9 +290,9 @@ namespace cryptonote return false; } - if(!keeped_by_block && get_object_blobsize(tx) >= m_blockchain_storage.get_current_comulative_blocksize_limit() - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE) + if(!keeped_by_block && get_object_blobsize(tx) >= m_blockchain_storage.get_current_cumulative_blocksize_limit() - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE) { - LOG_PRINT_RED_L1("tx is too large " << get_object_blobsize(tx) << ", expected not bigger than " << m_blockchain_storage.get_current_comulative_blocksize_limit() - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE); + LOG_PRINT_RED_L1("tx is too large " << get_object_blobsize(tx) << ", expected not bigger than " << m_blockchain_storage.get_current_cumulative_blocksize_limit() - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE); return false; } @@ -577,7 +577,7 @@ namespace cryptonote m_starter_message_showed = true; } - m_store_blockchain_interval.do_call(boost::bind(&blockchain_storage::store_blockchain, &m_blockchain_storage)); + m_store_blockchain_interval.do_call(boost::bind(&Blockchain::store_blockchain, &m_blockchain_storage)); m_miner.on_idle(); m_mempool.on_idle(); return true; diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h index 748f2b665..8ee0d8a8d 100644 --- a/src/cryptonote_core/cryptonote_core.h +++ b/src/cryptonote_core/cryptonote_core.h @@ -39,7 +39,7 @@ #include "cryptonote_protocol/cryptonote_protocol_handler_common.h" #include "storages/portable_storage_template_helper.h" #include "tx_pool.h" -#include "blockchain_storage.h" +#include "blockchain.h" #include "miner.h" #include "connection_context.h" #include "cryptonote_core/cryptonote_stat_info.h" @@ -112,7 +112,7 @@ namespace cryptonote bool get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res); void pause_mine(); void resume_mine(); - blockchain_storage& get_blockchain_storage(){return m_blockchain_storage;} + Blockchain& get_blockchain_storage(){return m_blockchain_storage;} //debug functions void print_blockchain(uint64_t start_index, uint64_t end_index); void print_blockchain_index(); @@ -149,7 +149,7 @@ namespace cryptonote tx_memory_pool m_mempool; - blockchain_storage m_blockchain_storage; + Blockchain m_blockchain_storage; i_cryptonote_protocol* m_pprotocol; epee::critical_section m_incoming_tx_lock; //m_miner and m_miner_addres are probably temporary here diff --git a/src/cryptonote_core/tx_pool.cpp b/src/cryptonote_core/tx_pool.cpp index 691d16492..e6c20d814 100644 --- a/src/cryptonote_core/tx_pool.cpp +++ b/src/cryptonote_core/tx_pool.cpp @@ -37,7 +37,7 @@ #include "cryptonote_format_utils.h" #include "cryptonote_boost_serialization.h" #include "cryptonote_config.h" -#include "blockchain_storage.h" +#include "blockchain.h" #include "common/boost_serialization_helper.h" #include "common/int-util.h" #include "misc_language.h" @@ -54,7 +54,7 @@ namespace cryptonote } //--------------------------------------------------------------------------------- - tx_memory_pool::tx_memory_pool(blockchain_storage& bchs): m_blockchain(bchs) + tx_memory_pool::tx_memory_pool(Blockchain& bchs): m_blockchain(bchs) { } diff --git a/src/cryptonote_core/tx_pool.h b/src/cryptonote_core/tx_pool.h index 9c1c2b1aa..7ff8c5e1c 100644 --- a/src/cryptonote_core/tx_pool.h +++ b/src/cryptonote_core/tx_pool.h @@ -47,7 +47,7 @@ namespace cryptonote { - class blockchain_storage; + class Blockchain; /************************************************************************/ /* */ /************************************************************************/ @@ -55,7 +55,7 @@ namespace cryptonote class tx_memory_pool: boost::noncopyable { public: - tx_memory_pool(blockchain_storage& bchs); + tx_memory_pool(Blockchain& bchs); bool add_tx(const transaction &tx, const crypto::hash &id, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block); bool add_tx(const transaction &tx, tx_verification_context& tvc, bool keeped_by_block); //gets tx and remove it from pool @@ -127,7 +127,7 @@ namespace cryptonote //transactions_container m_alternative_transactions; std::string m_config_folder; - blockchain_storage& m_blockchain; + Blockchain& m_blockchain; /************************************************************************/ /* */ /************************************************************************/ @@ -170,9 +170,6 @@ namespace cryptonote uint64_t operator()(const txin_to_scripthash& tx) const {return 0;} }; -#if defined(DEBUG_CREATE_BLOCK_TEMPLATE) - friend class blockchain_storage; -#endif }; } -- cgit v1.2.3