2015-12-31 06:39:56 +00:00
|
|
|
// Copyright (c) 2014-2016, The Monero Project
|
2015-02-10 23:13:32 +00:00
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
2015-03-25 15:41:30 +00:00
|
|
|
#include "cryptonote_core/blockchain.h" // BlockchainDB
|
2015-02-10 23:13:32 +00:00
|
|
|
#include "cryptonote_core/blockchain_storage.h" // in-memory DB
|
2015-03-25 15:41:30 +00:00
|
|
|
#include "blockchain_db/blockchain_db.h"
|
|
|
|
#include "blockchain_db/lmdb/db_lmdb.h"
|
2016-02-17 19:58:37 +00:00
|
|
|
#if defined(BERKELEY_DB)
|
2016-02-14 20:20:14 +00:00
|
|
|
#include "blockchain_db/berkeleydb/db_bdb.h"
|
2016-02-17 19:58:37 +00:00
|
|
|
#endif
|
2015-02-10 23:13:32 +00:00
|
|
|
|
|
|
|
using namespace cryptonote;
|
|
|
|
|
2016-02-05 07:20:48 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// NOTE: These values should match blockchain.cpp
|
|
|
|
// TODO: Refactor
|
|
|
|
const uint64_t mainnet_hard_fork_version_1_till = 1009826;
|
|
|
|
const uint64_t testnet_hard_fork_version_1_till = 624633;
|
|
|
|
}
|
|
|
|
|
2015-02-10 23:13:32 +00:00
|
|
|
|
|
|
|
#if !defined(BLOCKCHAIN_DB) || BLOCKCHAIN_DB == DB_LMDB
|
|
|
|
|
2016-02-14 19:32:54 +00:00
|
|
|
struct fake_core_db
|
2015-02-10 23:13:32 +00:00
|
|
|
{
|
|
|
|
Blockchain m_storage;
|
2016-02-05 07:20:48 +00:00
|
|
|
HardFork* m_hardfork = nullptr;
|
2015-02-10 23:13:32 +00:00
|
|
|
tx_memory_pool m_pool;
|
|
|
|
bool support_batch;
|
|
|
|
bool support_add_block;
|
|
|
|
|
|
|
|
// for multi_db_runtime:
|
|
|
|
#if !defined(BLOCKCHAIN_DB)
|
2016-02-14 19:35:30 +00:00
|
|
|
fake_core_db(const boost::filesystem::path &path, const bool use_testnet=false, const bool do_batch=true, const std::string& db_type="lmdb", const int db_flags=0) : m_pool(&m_storage), m_storage(m_pool)
|
2015-02-10 23:13:32 +00:00
|
|
|
// for multi_db_compile:
|
|
|
|
#else
|
2016-02-14 19:35:30 +00:00
|
|
|
fake_core_db(const boost::filesystem::path &path, const bool use_testnet=false, const bool do_batch=true, const std::string& db_type="lmdb", const int db_flags=0) : m_pool(m_storage), m_storage(m_pool)
|
2015-02-10 23:13:32 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
m_pool.init(path.string());
|
2015-03-25 15:41:30 +00:00
|
|
|
|
2016-02-14 20:20:14 +00:00
|
|
|
BlockchainDB* db = nullptr;
|
|
|
|
if (db_type == "lmdb")
|
|
|
|
db = new BlockchainLMDB();
|
|
|
|
#if defined(BERKELEY_DB)
|
|
|
|
else if (db_type == "berkeley")
|
|
|
|
db = new BlockchainBDB();
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_ERROR("Attempted to use non-existent database type: " << db_type);
|
2016-03-19 21:48:36 +00:00
|
|
|
throw tools::runtime_error("Attempting to use non-existent database type");
|
2016-02-14 20:20:14 +00:00
|
|
|
}
|
2015-03-25 15:41:30 +00:00
|
|
|
|
|
|
|
boost::filesystem::path folder(path);
|
|
|
|
|
|
|
|
folder /= db->get_db_name();
|
|
|
|
|
2015-05-08 18:32:20 +00:00
|
|
|
LOG_PRINT_L0("Loading blockchain from folder " << folder.string() << " ...");
|
2015-03-25 15:41:30 +00:00
|
|
|
|
|
|
|
const std::string filename = folder.string();
|
|
|
|
try
|
|
|
|
{
|
2016-02-14 19:32:54 +00:00
|
|
|
db->open(filename, db_flags);
|
2015-03-25 15:41:30 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
LOG_PRINT_L0("Error opening database: " << e.what());
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2016-02-05 04:09:51 +00:00
|
|
|
db->check_hard_fork_info();
|
|
|
|
|
2016-02-05 07:20:48 +00:00
|
|
|
uint64_t hard_fork_version_1_till = use_testnet ? testnet_hard_fork_version_1_till : mainnet_hard_fork_version_1_till;
|
|
|
|
m_hardfork = new HardFork(*db, 1, hard_fork_version_1_till);
|
|
|
|
|
|
|
|
m_storage.init(db, m_hardfork, use_testnet);
|
2016-02-05 04:09:51 +00:00
|
|
|
|
2015-02-10 23:13:32 +00:00
|
|
|
if (do_batch)
|
2015-03-22 17:57:18 +00:00
|
|
|
m_storage.get_db().set_batch_transactions(do_batch);
|
2015-02-10 23:13:32 +00:00
|
|
|
support_batch = true;
|
|
|
|
support_add_block = true;
|
|
|
|
}
|
2016-02-14 19:32:54 +00:00
|
|
|
~fake_core_db()
|
2015-02-10 23:13:32 +00:00
|
|
|
{
|
2016-01-15 14:00:58 +00:00
|
|
|
m_storage.get_db().check_hard_fork_info();
|
2015-02-10 23:13:32 +00:00
|
|
|
m_storage.deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t add_block(const block& blk
|
|
|
|
, const size_t& block_size
|
|
|
|
, const difficulty_type& cumulative_difficulty
|
|
|
|
, const uint64_t& coins_generated
|
|
|
|
, const std::vector<transaction>& txs
|
|
|
|
)
|
|
|
|
{
|
2015-03-22 17:57:18 +00:00
|
|
|
return m_storage.get_db().add_block(blk, block_size, cumulative_difficulty, coins_generated, txs);
|
2015-02-10 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 19:28:20 +00:00
|
|
|
void batch_start(uint64_t batch_num_blocks = 0)
|
2015-02-10 23:13:32 +00:00
|
|
|
{
|
2015-07-11 19:28:20 +00:00
|
|
|
m_storage.get_db().batch_start(batch_num_blocks);
|
2015-02-10 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void batch_stop()
|
|
|
|
{
|
2015-03-22 17:57:18 +00:00
|
|
|
m_storage.get_db().batch_stop();
|
2015-02-10 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(BLOCKCHAIN_DB) || BLOCKCHAIN_DB == DB_MEMORY
|
|
|
|
|
|
|
|
struct fake_core_memory
|
|
|
|
{
|
|
|
|
blockchain_storage m_storage;
|
|
|
|
tx_memory_pool m_pool;
|
|
|
|
bool support_batch;
|
|
|
|
bool support_add_block;
|
|
|
|
|
|
|
|
// for multi_db_runtime:
|
|
|
|
#if !defined(BLOCKCHAIN_DB)
|
|
|
|
fake_core_memory(const boost::filesystem::path &path, const bool use_testnet=false) : m_pool(&m_storage), m_storage(m_pool)
|
|
|
|
#else
|
|
|
|
// for multi_db_compile:
|
|
|
|
fake_core_memory(const boost::filesystem::path &path, const bool use_testnet=false) : m_pool(m_storage), m_storage(&m_pool)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
m_pool.init(path.string());
|
|
|
|
m_storage.init(path.string(), use_testnet);
|
|
|
|
support_batch = false;
|
|
|
|
support_add_block = false;
|
|
|
|
}
|
|
|
|
~fake_core_memory()
|
|
|
|
{
|
|
|
|
LOG_PRINT_L3("fake_core_memory() destructor called - want to see it ripple down");
|
|
|
|
m_storage.deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t add_block(const block& blk
|
|
|
|
, const size_t& block_size
|
|
|
|
, const difficulty_type& cumulative_difficulty
|
|
|
|
, const uint64_t& coins_generated
|
|
|
|
, const std::vector<transaction>& txs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// TODO:
|
|
|
|
// would need to refactor handle_block_to_main_chain() to have a direct add_block() method like Blockchain class
|
2016-03-19 21:48:36 +00:00
|
|
|
throw tools::runtime_error("direct add_block() method not implemented for in-memory db");
|
2015-02-10 23:13:32 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2015-07-11 19:28:20 +00:00
|
|
|
void batch_start(uint64_t batch_num_blocks = 0)
|
2015-02-10 23:13:32 +00:00
|
|
|
{
|
|
|
|
LOG_PRINT_L0("WARNING: [batch_start] opt_batch set, but this database doesn't support/need transactions - ignoring");
|
|
|
|
}
|
|
|
|
|
|
|
|
void batch_stop()
|
|
|
|
{
|
|
|
|
LOG_PRINT_L0("WARNING: [batch_stop] opt_batch set, but this database doesn't support/need transactions - ignoring");
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|