mirror of
https://codeberg.org/anoncontributorxmr/monero.git
synced 2024-11-23 11:23:26 +01:00
import of BlockchainDB files
tried rebasing, tree-filter, and many other things. at this point, the history of these files previous to this can live on in my bc2 branch, as I'm importing them as-is to here.
This commit is contained in:
parent
54fbf2afb3
commit
aba548cbf7
2137
src/cryptonote_core/blockchain.cpp
Normal file
2137
src/cryptonote_core/blockchain.cpp
Normal file
File diff suppressed because it is too large
Load Diff
221
src/cryptonote_core/blockchain.h
Normal file
221
src/cryptonote_core/blockchain.h
Normal file
@ -0,0 +1,221 @@
|
||||
// Copyright (c) 2014, The Monero Project
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
||||
|
||||
#pragma once
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
#include <boost/serialization/version.hpp>
|
||||
#include <boost/serialization/list.hpp>
|
||||
#include <boost/multi_index_container.hpp>
|
||||
#include <boost/multi_index/global_fun.hpp>
|
||||
#include <boost/multi_index/hashed_index.hpp>
|
||||
#include <boost/multi_index/member.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
#include <atomic>
|
||||
|
||||
#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"
|
||||
#include "rpc/core_rpc_server_commands_defs.h"
|
||||
#include "difficulty.h"
|
||||
#include "cryptonote_core/cryptonote_format_utils.h"
|
||||
#include "verification_context.h"
|
||||
#include "crypto/hash.h"
|
||||
#include "checkpoints.h"
|
||||
#include "blockchain_db.h"
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
|
||||
/************************************************************************/
|
||||
/* */
|
||||
/************************************************************************/
|
||||
class Blockchain
|
||||
{
|
||||
public:
|
||||
struct transaction_chain_entry
|
||||
{
|
||||
transaction tx;
|
||||
uint64_t m_keeper_block_height;
|
||||
size_t m_blob_size;
|
||||
std::vector<uint64_t> m_global_output_indexes;
|
||||
};
|
||||
|
||||
struct block_extended_info
|
||||
{
|
||||
block bl;
|
||||
uint64_t height;
|
||||
size_t block_cumulative_size;
|
||||
difficulty_type cumulative_difficulty;
|
||||
uint64_t already_generated_coins;
|
||||
};
|
||||
|
||||
Blockchain(tx_memory_pool& tx_pool);
|
||||
|
||||
bool init() { return init(tools::get_default_data_dir()); }
|
||||
bool init(const std::string& config_folder);
|
||||
bool deinit();
|
||||
|
||||
void set_checkpoints(checkpoints&& chk_pts) { m_checkpoints = chk_pts; }
|
||||
|
||||
//bool push_new_block();
|
||||
bool get_blocks(uint64_t start_offset, size_t count, std::list<block>& blocks, std::list<transaction>& txs);
|
||||
bool get_blocks(uint64_t start_offset, size_t count, std::list<block>& blocks);
|
||||
bool get_alternative_blocks(std::list<block>& blocks);
|
||||
size_t get_alternative_blocks_count();
|
||||
crypto::hash get_block_id_by_height(uint64_t height);
|
||||
bool get_block_by_hash(const crypto::hash &h, block &blk);
|
||||
void get_all_known_block_ids(std::list<crypto::hash> &main, std::list<crypto::hash> &alt, std::list<crypto::hash> &invalid);
|
||||
|
||||
template<class archive_t>
|
||||
void serialize(archive_t & ar, const unsigned int version);
|
||||
|
||||
bool have_tx(const crypto::hash &id);
|
||||
bool have_tx_keyimges_as_spent(const transaction &tx);
|
||||
bool have_tx_keyimg_as_spent(const crypto::key_image &key_im);
|
||||
|
||||
template<class visitor_t>
|
||||
bool scan_outputkeys_for_indexes(const txin_to_key& tx_in_to_key, visitor_t& vis, uint64_t* pmax_related_block_height = NULL);
|
||||
|
||||
uint64_t get_current_blockchain_height();
|
||||
crypto::hash get_tail_id();
|
||||
crypto::hash get_tail_id(uint64_t& height);
|
||||
difficulty_type get_difficulty_for_next_block();
|
||||
bool add_new_block(const block& bl_, block_verification_context& bvc);
|
||||
bool reset_and_set_genesis_block(const block& b);
|
||||
bool create_block_template(block& b, const account_public_address& miner_address, difficulty_type& di, uint64_t& height, const blobdata& ex_nonce);
|
||||
bool have_block(const crypto::hash& id);
|
||||
size_t get_total_transactions();
|
||||
bool get_short_chain_history(std::list<crypto::hash>& ids);
|
||||
bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp);
|
||||
bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, uint64_t& starter_offset);
|
||||
bool find_blockchain_supplement(const uint64_t req_start_block, const std::list<crypto::hash>& qblock_ids, std::list<std::pair<block, std::list<transaction> > >& blocks, uint64_t& total_height, uint64_t& start_height, size_t max_count);
|
||||
bool handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NOTIFY_RESPONSE_GET_OBJECTS::request& rsp);
|
||||
bool handle_get_objects(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res);
|
||||
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);
|
||||
bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs);
|
||||
bool store_blockchain();
|
||||
bool check_tx_input(const txin_to_key& txin, const crypto::hash& tx_prefix_hash, const std::vector<crypto::signature>& sig, uint64_t* pmax_related_block_height = NULL);
|
||||
bool check_tx_inputs(const transaction& tx, uint64_t* pmax_used_block_height = NULL);
|
||||
bool check_tx_inputs(const transaction& tx, uint64_t& pmax_used_block_height, crypto::hash& max_used_block_id);
|
||||
uint64_t get_current_cumulative_blocksize_limit();
|
||||
bool is_storing_blockchain(){return m_is_blockchain_storing;}
|
||||
uint64_t block_difficulty(uint64_t i);
|
||||
|
||||
template<class t_ids_container, class t_blocks_container, class t_missed_container>
|
||||
void get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs);
|
||||
|
||||
template<class t_ids_container, class t_tx_container, class t_missed_container>
|
||||
void 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);
|
||||
|
||||
private:
|
||||
typedef std::unordered_map<crypto::hash, size_t> blocks_by_id_index;
|
||||
typedef std::unordered_map<crypto::hash, transaction_chain_entry> transactions_container;
|
||||
typedef std::unordered_set<crypto::key_image> key_images_container;
|
||||
typedef std::vector<block_extended_info> blocks_container;
|
||||
typedef std::unordered_map<crypto::hash, block_extended_info> blocks_ext_by_hash;
|
||||
typedef std::unordered_map<crypto::hash, block> blocks_by_hash;
|
||||
typedef std::map<uint64_t, std::vector<std::pair<crypto::hash, size_t>>> outputs_container; //crypto::hash - tx hash, size_t - index of out in transaction
|
||||
|
||||
BlockchainDB* m_db;
|
||||
|
||||
tx_memory_pool& m_tx_pool;
|
||||
epee::critical_section m_blockchain_lock; // TODO: add here reader/writer lock
|
||||
|
||||
// main chain
|
||||
blocks_container m_blocks; // height -> block_extended_info
|
||||
blocks_by_id_index m_blocks_index; // crypto::hash -> height
|
||||
transactions_container m_transactions;
|
||||
key_images_container m_spent_keys;
|
||||
size_t m_current_block_cumul_sz_limit;
|
||||
|
||||
|
||||
// all alternative chains
|
||||
blocks_ext_by_hash m_alternative_chains; // crypto::hash -> block_extended_info
|
||||
|
||||
// some invalid blocks
|
||||
blocks_ext_by_hash m_invalid_blocks; // crypto::hash -> block_extended_info
|
||||
outputs_container m_outputs;
|
||||
|
||||
|
||||
std::string m_config_folder;
|
||||
checkpoints m_checkpoints;
|
||||
std::atomic<bool> m_is_in_checkpoint_zone;
|
||||
std::atomic<bool> m_is_blockchain_storing;
|
||||
|
||||
bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain, bool discard_disconnected_chain);
|
||||
block pop_block_from_blockchain();
|
||||
bool purge_transaction_from_blockchain(const crypto::hash& tx_id);
|
||||
bool purge_transaction_keyimages_from_blockchain(const transaction& tx, bool strict_check);
|
||||
|
||||
bool handle_block_to_main_chain(const block& bl, block_verification_context& bvc);
|
||||
bool handle_block_to_main_chain(const block& bl, const crypto::hash& id, block_verification_context& bvc);
|
||||
bool handle_alternative_block(const block& b, const crypto::hash& id, block_verification_context& bvc);
|
||||
difficulty_type get_next_difficulty_for_alternative_chain(const std::list<blocks_ext_by_hash::iterator>& alt_chain, block_extended_info& bei);
|
||||
bool prevalidate_miner_transaction(const block& b, uint64_t height);
|
||||
bool validate_miner_transaction(const block& b, size_t cumulative_block_size, uint64_t fee, uint64_t& base_reward, uint64_t already_generated_coins);
|
||||
bool validate_transaction(const block& b, uint64_t height, const transaction& tx);
|
||||
bool rollback_blockchain_switching(std::list<block>& original_chain);
|
||||
bool add_transaction_from_block(const transaction& tx, const crypto::hash& tx_id, const crypto::hash& bl_id, uint64_t bl_height);
|
||||
bool push_transaction_to_global_outs_index(const transaction& tx, const crypto::hash& tx_id, std::vector<uint64_t>& global_indexes);
|
||||
bool pop_transaction_from_global_index(const transaction& tx, const crypto::hash& tx_id);
|
||||
void get_last_n_blocks_sizes(std::vector<size_t>& sz, size_t count);
|
||||
void add_out_to_get_random_outs(COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& result_outs, uint64_t amount, size_t i);
|
||||
bool is_tx_spendtime_unlocked(uint64_t unlock_time);
|
||||
bool add_block_as_invalid(const block& bl, const crypto::hash& h);
|
||||
bool add_block_as_invalid(const block_extended_info& bei, const crypto::hash& h);
|
||||
bool check_block_timestamp(const block& b);
|
||||
bool check_block_timestamp(const std::vector<uint64_t>& timestamps, const block& b);
|
||||
uint64_t get_adjusted_time();
|
||||
bool complete_timestamps_vector(uint64_t start_height, std::vector<uint64_t>& timestamps);
|
||||
bool update_next_cumulative_size_limit();
|
||||
|
||||
bool check_for_double_spend(const transaction& tx, key_images_container& keys_this_block);
|
||||
};
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/* */
|
||||
/************************************************************************/
|
||||
|
||||
#define CURRENT_BLOCKCHAIN_STORAGE_ARCHIVE_VER 12
|
||||
|
||||
//------------------------------------------------------------------
|
||||
|
||||
} // namespace cryptonote
|
||||
|
||||
BOOST_CLASS_VERSION(cryptonote::Blockchain, CURRENT_BLOCKCHAIN_STORAGE_ARCHIVE_VER)
|
135
src/cryptonote_core/blockchain_db.cpp
Normal file
135
src/cryptonote_core/blockchain_db.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
// Copyright (c) 2014, The Monero Project
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include "cryptonote_core/blockchain_db.h"
|
||||
#include "cryptonote_format_utils.h"
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
|
||||
void BlockchainDB::pop_block()
|
||||
{
|
||||
block blk;
|
||||
std::vector<transaction> txs;
|
||||
pop_block(blk, txs);
|
||||
}
|
||||
|
||||
void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transaction& tx)
|
||||
{
|
||||
crypto::hash tx_hash = get_transaction_hash(tx);
|
||||
|
||||
add_transaction_data(blk_hash, tx);
|
||||
|
||||
// iterate tx.vout using indices instead of C++11 foreach syntax because
|
||||
// we need the index
|
||||
for (uint64_t i = 0; i < tx.vout.size(); ++i)
|
||||
{
|
||||
add_output(tx_hash, tx.vout[i], i);
|
||||
}
|
||||
|
||||
for (const txin_v& tx_input : tx.vin)
|
||||
{
|
||||
if (tx_input.type() == typeid(txin_to_key))
|
||||
{
|
||||
add_spent_key(boost::get<txin_to_key>(tx_input).k_image);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t BlockchainDB::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
|
||||
)
|
||||
{
|
||||
try
|
||||
{
|
||||
// call out to subclass implementation to add the block & metadata
|
||||
add_block(blk, block_size, cumulative_difficulty, coins_generated);
|
||||
|
||||
crypto::hash blk_hash = get_block_hash(blk);
|
||||
// call out to add the transactions
|
||||
for (const transaction& tx : txs)
|
||||
{
|
||||
add_transaction(blk_hash, tx);
|
||||
}
|
||||
}
|
||||
// in case any of the add_block process goes awry, undo
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
LOG_ERROR("Error adding block to db: " << e.what());
|
||||
try
|
||||
{
|
||||
pop_block();
|
||||
}
|
||||
// if undoing goes wrong as well, we need to throw, as blockchain
|
||||
// will be in a bad state
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
LOG_ERROR("Error undoing partially added block: " << e.what());
|
||||
throw;
|
||||
}
|
||||
throw;
|
||||
}
|
||||
|
||||
return height();
|
||||
}
|
||||
|
||||
void BlockchainDB::pop_block(block& blk, std::vector<transaction>& txs)
|
||||
{
|
||||
blk = get_top_block();
|
||||
|
||||
for (const auto& h : blk.tx_hashes)
|
||||
{
|
||||
txs.push_back(get_tx(h));
|
||||
remove_transaction(h);
|
||||
}
|
||||
}
|
||||
|
||||
void BlockchainDB::remove_transaction(const crypto::hash& tx_hash)
|
||||
{
|
||||
transaction tx = get_tx(tx_hash);
|
||||
|
||||
for (const tx_out& tx_output : tx.vout)
|
||||
{
|
||||
remove_output(tx_output);
|
||||
}
|
||||
|
||||
for (const txin_v& tx_input : tx.vin)
|
||||
{
|
||||
if (tx_input.type() == typeid(txin_to_key))
|
||||
{
|
||||
remove_spent_key(boost::get<txin_to_key>(tx_input).k_image);
|
||||
}
|
||||
}
|
||||
|
||||
remove_transaction_data(tx_hash);
|
||||
}
|
||||
|
||||
} // namespace cryptonote
|
510
src/cryptonote_core/blockchain_db.h
Normal file
510
src/cryptonote_core/blockchain_db.h
Normal file
@ -0,0 +1,510 @@
|
||||
// Copyright (c) 2014, The Monero Project
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <exception>
|
||||
#include "crypto/hash.h"
|
||||
#include "cryptonote_core/cryptonote_basic.h"
|
||||
#include "cryptonote_core/difficulty.h"
|
||||
|
||||
/* DB Driver Interface
|
||||
*
|
||||
* The DB interface is a store for the canonical block chain.
|
||||
* It serves as a persistent storage for the blockchain.
|
||||
*
|
||||
* For the sake of efficiency, the reference implementation will also
|
||||
* store some blockchain data outside of the blocks, such as spent
|
||||
* transfer key images, unspent transaction outputs, etc.
|
||||
*
|
||||
* Transactions are duplicated so that we don't have to fetch a whole block
|
||||
* in order to fetch a transaction from that block. If this is deemed
|
||||
* unnecessary later, this can change.
|
||||
*
|
||||
* Spent key images are duplicated outside of the blocks so it is quick
|
||||
* to verify an output hasn't already been spent
|
||||
*
|
||||
* Unspent transaction outputs are duplicated to quickly gather random
|
||||
* outputs to use for mixins
|
||||
*
|
||||
* IMPORTANT:
|
||||
* A concrete implementation of this interface should populate these
|
||||
* duplicated members! It is possible to have a partial implementation
|
||||
* of this interface call to private members of the interface to be added
|
||||
* later that will then populate as needed.
|
||||
*
|
||||
* General:
|
||||
* open()
|
||||
* close()
|
||||
* sync()
|
||||
* reset()
|
||||
*
|
||||
* Lock and unlock provided for reorg externally, and for block
|
||||
* additions internally, this way threaded reads are completely fine
|
||||
* unless the blockchain is changing.
|
||||
* bool lock()
|
||||
* unlock()
|
||||
*
|
||||
* Blocks:
|
||||
* bool block_exists(hash)
|
||||
* height add_block(block, block_size, cumulative_difficulty, coins_generated, transactions)
|
||||
* block get_block(hash)
|
||||
* height get_block_height(hash)
|
||||
* header get_block_header(hash)
|
||||
* block get_block_from_height(height)
|
||||
* size_t get_block_size(height)
|
||||
* difficulty get_block_cumulative_difficulty(height)
|
||||
* uint64_t get_block_already_generated_coins(height)
|
||||
* uint64_t get_block_timestamp(height)
|
||||
* uint64_t get_top_block_timestamp()
|
||||
* hash get_block_hash_from_height(height)
|
||||
* blocks get_blocks_range(height1, height2)
|
||||
* hashes get_hashes_range(height1, height2)
|
||||
* hash top_block_hash()
|
||||
* block get_top_block()
|
||||
* height height()
|
||||
* void pop_block(block&, tx_list&)
|
||||
*
|
||||
* Transactions:
|
||||
* bool tx_exists(hash)
|
||||
* uint64_t get_tx_unlock_time(hash)
|
||||
* tx get_tx(hash)
|
||||
* uint64_t get_tx_count()
|
||||
* tx_list get_tx_list(hash_list)
|
||||
* height get_tx_block_height(hash)
|
||||
*
|
||||
* Outputs:
|
||||
* index get_random_output(amount)
|
||||
* uint64_t get_num_outputs(amount)
|
||||
* pub_key get_output_key(amount, index)
|
||||
* tx_out get_output(tx_hash, index)
|
||||
* hash,index get_output_tx_and_index(amount, index)
|
||||
* vec<uint64> get_tx_output_indices(tx_hash)
|
||||
*
|
||||
*
|
||||
* Spent Output Key Images:
|
||||
* bool has_key_image(key_image)
|
||||
*
|
||||
* Exceptions:
|
||||
* DB_ERROR -- generic
|
||||
* DB_OPEN_FAILURE
|
||||
* DB_CREATE_FAILURE
|
||||
* DB_SYNC_FAILURE
|
||||
* BLOCK_DNE
|
||||
* BLOCK_PARENT_DNE
|
||||
* BLOCK_EXISTS
|
||||
* BLOCK_INVALID -- considering making this multiple errors
|
||||
* TX_DNE
|
||||
* TX_EXISTS
|
||||
* OUTPUT_DNE
|
||||
*/
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
|
||||
/***********************************
|
||||
* Exception Definitions
|
||||
***********************************/
|
||||
class DB_ERROR : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
DB_ERROR() : m("Generic DB Error") { }
|
||||
DB_ERROR(const char* s) : m(s) { }
|
||||
|
||||
virtual ~DB_ERROR() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class DB_OPEN_FAILURE : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
DB_OPEN_FAILURE() : m("Failed to open the db") { }
|
||||
DB_OPEN_FAILURE(const char* s) : m(s) { }
|
||||
|
||||
virtual ~DB_OPEN_FAILURE() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class DB_CREATE_FAILURE : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
DB_CREATE_FAILURE() : m("Failed to create the db") { }
|
||||
DB_CREATE_FAILURE(const char* s) : m(s) { }
|
||||
|
||||
virtual ~DB_CREATE_FAILURE() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class DB_SYNC_FAILURE : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
DB_SYNC_FAILURE() : m("Failed to sync the db") { }
|
||||
DB_SYNC_FAILURE(const char* s) : m(s) { }
|
||||
|
||||
virtual ~DB_SYNC_FAILURE() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class BLOCK_DNE : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
BLOCK_DNE() : m("The block requested does not exist") { }
|
||||
BLOCK_DNE(const char* s) : m(s) { }
|
||||
|
||||
virtual ~BLOCK_DNE() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class BLOCK_PARENT_DNE : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
BLOCK_PARENT_DNE() : m("The parent of the block does not exist") { }
|
||||
BLOCK_PARENT_DNE(const char* s) : m(s) { }
|
||||
|
||||
virtual ~BLOCK_PARENT_DNE() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class BLOCK_EXISTS : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
BLOCK_EXISTS() : m("The block to be added already exists!") { }
|
||||
BLOCK_EXISTS(const char* s) : m(s) { }
|
||||
|
||||
virtual ~BLOCK_EXISTS() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class BLOCK_INVALID : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
BLOCK_INVALID() : m("The block to be added did not pass validation!") { }
|
||||
BLOCK_INVALID(const char* s) : m(s) { }
|
||||
|
||||
virtual ~BLOCK_INVALID() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class TX_DNE : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
TX_DNE() : m("The transaction requested does not exist") { }
|
||||
TX_DNE(const char* s) : m(s) { }
|
||||
|
||||
virtual ~TX_DNE() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class TX_EXISTS : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
TX_EXISTS() : m("The transaction to be added already exists!") { }
|
||||
TX_EXISTS(const char* s) : m(s) { }
|
||||
|
||||
virtual ~TX_EXISTS() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class OUTPUT_DNE : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string m;
|
||||
public:
|
||||
OUTPUT_DNE() : m("The transaction requested does not exist") { }
|
||||
OUTPUT_DNE(const char* s) : m(s) { }
|
||||
|
||||
virtual ~OUTPUT_DNE() { }
|
||||
|
||||
const char* what() const throw()
|
||||
{
|
||||
return m.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
/***********************************
|
||||
* End of Exception Definitions
|
||||
***********************************/
|
||||
|
||||
|
||||
class BlockchainDB
|
||||
{
|
||||
private:
|
||||
/*********************************************************************
|
||||
* private virtual members
|
||||
*********************************************************************/
|
||||
|
||||
// tells the subclass to add the block and metadata to storage
|
||||
virtual void add_block( const block& blk
|
||||
, const size_t& block_size
|
||||
, const difficulty_type& cumulative_difficulty
|
||||
, const uint64_t& coins_generated
|
||||
) = 0;
|
||||
|
||||
// tells the subclass to remove data about a block
|
||||
virtual void remove_block(const crypto::hash& blk_hash) = 0;
|
||||
|
||||
// tells the subclass to store the transaction and its metadata
|
||||
virtual void add_transaction_data(const crypto::hash& blk_hash, const transaction& tx) = 0;
|
||||
|
||||
// tells the subclass to remove data about a transaction
|
||||
virtual void remove_transaction_data(const crypto::hash& tx_hash) = 0;
|
||||
|
||||
// tells the subclass to store an output
|
||||
virtual void add_output(const crypto::hash& tx_hash, const tx_out& tx_output, const uint64_t& local_index) = 0;
|
||||
|
||||
// tells the subclass to remove an output
|
||||
virtual void remove_output(const tx_out& tx_output) = 0;
|
||||
|
||||
// tells the subclass to store a spent key
|
||||
virtual void add_spent_key(const crypto::key_image& k_image) = 0;
|
||||
|
||||
// tells the subclass to remove a spent key
|
||||
virtual void remove_spent_key(const crypto::key_image& k_image) = 0;
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* private concrete members
|
||||
*********************************************************************/
|
||||
// private version of pop_block, for undoing if an add_block goes tits up
|
||||
void pop_block();
|
||||
|
||||
// helper function for add_transactions, to add each individual tx
|
||||
void add_transaction(const crypto::hash& blk_hash, const transaction& tx);
|
||||
|
||||
// helper function to remove transaction from blockchain
|
||||
void remove_transaction(const crypto::hash& tx_hash);
|
||||
|
||||
|
||||
public:
|
||||
// open the db at location <filename>, or create it if there isn't one.
|
||||
virtual void open(const std::string& filename) = 0;
|
||||
|
||||
// make sure implementation has a create function as well
|
||||
virtual void create(const std::string& filename) = 0;
|
||||
|
||||
// close and sync the db
|
||||
virtual void close() = 0;
|
||||
|
||||
// sync the db
|
||||
virtual void sync() = 0;
|
||||
|
||||
// reset the db -- USE WITH CARE
|
||||
virtual void reset() = 0;
|
||||
|
||||
|
||||
// FIXME: these are just for functionality mocking, need to implement
|
||||
// RAII-friendly and multi-read one-write friendly locking mechanism
|
||||
//
|
||||
// acquire db lock
|
||||
virtual bool lock() = 0;
|
||||
|
||||
// release db lock
|
||||
virtual void unlock() = 0;
|
||||
|
||||
|
||||
// adds a block with the given metadata to the top of the blockchain, returns the new height
|
||||
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
|
||||
);
|
||||
|
||||
// return true if a block with hash <h> exists in the blockchain
|
||||
virtual bool block_exists(const crypto::hash& h) = 0;
|
||||
|
||||
// return block with hash <h>
|
||||
virtual block get_block(const crypto::hash& h) = 0;
|
||||
|
||||
// return the height of the block with hash <h> on the blockchain,
|
||||
// throw if it doesn't exist
|
||||
virtual uint64_t get_block_height(const crypto::hash& h) = 0;
|
||||
|
||||
// return header for block with hash <h>
|
||||
virtual block_header get_block_header(const crypto::hash& h) = 0;
|
||||
|
||||
// return block at height <height>
|
||||
virtual block get_block_from_height(const uint64_t& height) = 0;
|
||||
|
||||
// return timestamp of block at height <height>
|
||||
virtual uint64_t get_block_timestamp(const uint64_t& height) = 0;
|
||||
|
||||
// return timestamp of most recent block
|
||||
virtual uint64_t get_top_block_timestamp() = 0;
|
||||
|
||||
// return block size of block at height <height>
|
||||
virtual size_t get_block_size(const uint64_t& height) = 0;
|
||||
|
||||
// return cumulative difficulty up to and including block at height <height>
|
||||
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) = 0;
|
||||
|
||||
// return difficulty of block at height <height>
|
||||
virtual difficulty_type get_block_difficulty(const uint64_t& height) = 0;
|
||||
|
||||
// return number of coins generated up to and including block at height <height>
|
||||
virtual uint64_t get_block_already_generated_coins(const uint64_t& height) = 0;
|
||||
|
||||
// return hash of block at height <height>
|
||||
virtual crypto::hash get_block_hash_from_height(const uint64_t& height) = 0;
|
||||
|
||||
// return list of blocks in range <h1,h2> of height.
|
||||
virtual std::list<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) = 0;
|
||||
|
||||
// return list of block hashes in range <h1, h2> of height
|
||||
virtual std::list<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) = 0;
|
||||
|
||||
// return the hash of the top block on the chain
|
||||
virtual crypto::hash top_block_hash() = 0;
|
||||
|
||||
// return the block at the top of the blockchain
|
||||
virtual block get_top_block() = 0;
|
||||
|
||||
// return the index of the top block on the chain
|
||||
// NOTE: for convenience using heights as indices, this is not the total
|
||||
// size of the blockchain, but rather the index of the top block. As the
|
||||
// chain is 0-indexed, the total size will be height() + 1.
|
||||
virtual uint64_t height() = 0;
|
||||
|
||||
// pops the top block off the blockchain.
|
||||
// Returns by reference the popped block and its associated transactions
|
||||
//
|
||||
// IMPORTANT:
|
||||
// When a block is popped, the transactions associated with it need to be
|
||||
// removed, as well as outputs and spent key images associated with
|
||||
// those transactions.
|
||||
void pop_block(block& blk, std::vector<transaction>& txs);
|
||||
|
||||
|
||||
// return true if a transaction with hash <h> exists
|
||||
virtual bool tx_exists(const crypto::hash& h) = 0;
|
||||
|
||||
// return unlock time of tx with hash <h>
|
||||
virtual uint64_t get_tx_unlock_time(const crypto::hash& h) = 0;
|
||||
|
||||
// return tx with hash <h>
|
||||
// throw if no such tx exists
|
||||
virtual transaction get_tx(const crypto::hash& h) = 0;
|
||||
|
||||
// returns the total number of transactions in all blocks
|
||||
virtual uint64_t get_tx_count() = 0;
|
||||
|
||||
// return list of tx with hashes <hlist>.
|
||||
// TODO: decide if a missing hash means return empty list
|
||||
// or just skip that hash
|
||||
virtual std::list<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) = 0;
|
||||
|
||||
// returns height of block that contains transaction with hash <h>
|
||||
virtual uint64_t get_tx_block_height(const crypto::hash& h) = 0;
|
||||
|
||||
// return global output index of a random output of amount <amount>
|
||||
virtual uint64_t get_random_output(const uint64_t& amount) = 0;
|
||||
|
||||
// returns the total number of outputs of amount <amount>
|
||||
virtual uint64_t get_num_outputs(const uint64_t& amount) = 0;
|
||||
|
||||
// return public key for output with global output amount <amount> and index <index>
|
||||
virtual crypto::public_key get_output_key(const uint64_t& amount, const uint64_t& index) = 0;
|
||||
|
||||
// returns the output indexed by <index> in the transaction with hash <h>
|
||||
virtual tx_out get_output(const crypto::hash& h, const uint64_t& index) = 0;
|
||||
|
||||
// returns the transaction-local reference for the output with <amount> at <index>
|
||||
// return type is pair of tx hash and index
|
||||
virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) = 0;
|
||||
|
||||
// return a vector of indices corresponding to the global output index for
|
||||
// each output in the transaction with hash <h>
|
||||
virtual std::vector<uint64_t> get_tx_output_indices(const crypto::hash& h) = 0;
|
||||
|
||||
// returns true if key image <img> is present in spent key images storage
|
||||
virtual bool has_key_image(const crypto::key_image& img) = 0;
|
||||
|
||||
}; // class BlockchainDB
|
||||
|
||||
|
||||
} // namespace cryptonote
|
Loading…
Reference in New Issue
Block a user