read txn/cursor stuff

Could wrap more later.
This commit is contained in:
Howard Chu 2016-02-18 12:09:57 +00:00
parent 86a7f2b1e7
commit 8cc7a36f0b
8 changed files with 339 additions and 166 deletions

View file

@ -1835,7 +1835,7 @@ void BlockchainBDB::set_batch_transactions(bool batch_transactions)
LOG_PRINT_L3("batch transactions " << (m_batch_transactions ? "enabled" : "disabled")); LOG_PRINT_L3("batch transactions " << (m_batch_transactions ? "enabled" : "disabled"));
} }
void BlockchainBDB::block_txn_start() void BlockchainBDB::block_txn_start(bool readonly)
{ {
// TODO // TODO
} }

View file

@ -328,7 +328,7 @@ public:
virtual void batch_stop(); virtual void batch_stop();
virtual void batch_abort(); virtual void batch_abort();
virtual void block_txn_start(); virtual void block_txn_start(bool readonly);
virtual void block_txn_stop(); virtual void block_txn_stop();
virtual void block_txn_abort(); virtual void block_txn_abort();

View file

@ -99,7 +99,7 @@ uint64_t BlockchainDB::add_block( const block& blk
, const std::vector<transaction>& txs , const std::vector<transaction>& txs
) )
{ {
block_txn_start(); block_txn_start(false);
TIME_MEASURE_START(time1); TIME_MEASURE_START(time1);
crypto::hash blk_hash = get_block_hash(blk); crypto::hash blk_hash = get_block_hash(blk);
@ -227,6 +227,9 @@ void BlockchainDB::fixup()
static const char * const mainnet_genesis_hex = "418015bb9ae982a1975da7d79277c2705727a56894ba0fb246adaabb1f4632e3"; static const char * const mainnet_genesis_hex = "418015bb9ae982a1975da7d79277c2705727a56894ba0fb246adaabb1f4632e3";
crypto::hash mainnet_genesis_hash; crypto::hash mainnet_genesis_hash;
epee::string_tools::hex_to_pod(mainnet_genesis_hex, mainnet_genesis_hash ); epee::string_tools::hex_to_pod(mainnet_genesis_hex, mainnet_genesis_hash );
set_batch_transactions(true);
batch_start();
if (get_block_hash_from_height(0) == mainnet_genesis_hash) if (get_block_hash_from_height(0) == mainnet_genesis_hash)
{ {
// block 202612 (511 key images in 511 transactions) // block 202612 (511 key images in 511 transactions)
@ -762,9 +765,6 @@ void BlockchainDB::fixup()
"633cdedeb3b96ec4f234c670254c6f721e0b368d00b48c6b26759db7d62cf52d", "633cdedeb3b96ec4f234c670254c6f721e0b368d00b48c6b26759db7d62cf52d",
}; };
set_batch_transactions(true);
batch_start();
if (height() > 202612) if (height() > 202612)
{ {
for (const auto &kis: key_images_202612) for (const auto &kis: key_images_202612)
@ -791,9 +791,8 @@ void BlockchainDB::fixup()
} }
} }
} }
batch_stop();
} }
batch_stop();
} }
} // namespace cryptonote } // namespace cryptonote

View file

@ -381,7 +381,7 @@ public:
virtual void batch_stop() = 0; virtual void batch_stop() = 0;
virtual void set_batch_transactions(bool) = 0; virtual void set_batch_transactions(bool) = 0;
virtual void block_txn_start() = 0; virtual void block_txn_start(bool readonly=false) = 0;
virtual void block_txn_stop() = 0; virtual void block_txn_stop() = 0;
virtual void block_txn_abort() = 0; virtual void block_txn_abort() = 0;

File diff suppressed because it is too large Load diff

View file

@ -30,6 +30,7 @@
#include "blockchain_db/blockchain_db.h" #include "blockchain_db/blockchain_db.h"
#include "cryptonote_protocol/blobdatatype.h" // for type blobdata #include "cryptonote_protocol/blobdatatype.h" // for type blobdata
#include <boost/thread/tss.hpp>
#include <lmdb.h> #include <lmdb.h>
@ -38,7 +39,7 @@
namespace cryptonote namespace cryptonote
{ {
struct mdb_txn_cursors typedef struct mdb_txn_cursors
{ {
MDB_cursor *m_txc_blocks; MDB_cursor *m_txc_blocks;
MDB_cursor *m_txc_block_heights; MDB_cursor *m_txc_block_heights;
@ -59,24 +60,58 @@ struct mdb_txn_cursors
MDB_cursor *m_txc_tx_outputs; MDB_cursor *m_txc_tx_outputs;
MDB_cursor *m_txc_spent_keys; MDB_cursor *m_txc_spent_keys;
};
#define m_cur_blocks m_cursors.m_txc_blocks MDB_cursor *m_txc_hf_versions;
#define m_cur_block_heights m_cursors.m_txc_block_heights } mdb_txn_cursors;
#define m_cur_block_hashes m_cursors.m_txc_block_hashes
#define m_cur_block_timestamps m_cursors.m_txc_block_timestamps #define m_cur_blocks m_cursors->m_txc_blocks
#define m_cur_block_sizes m_cursors.m_txc_block_sizes #define m_cur_block_heights m_cursors->m_txc_block_heights
#define m_cur_block_diffs m_cursors.m_txc_block_diffs #define m_cur_block_hashes m_cursors->m_txc_block_hashes
#define m_cur_block_coins m_cursors.m_txc_block_coins #define m_cur_block_timestamps m_cursors->m_txc_block_timestamps
#define m_cur_output_txs m_cursors.m_txc_output_txs #define m_cur_block_sizes m_cursors->m_txc_block_sizes
#define m_cur_output_indices m_cursors.m_txc_output_indices #define m_cur_block_diffs m_cursors->m_txc_block_diffs
#define m_cur_output_amounts m_cursors.m_txc_output_amounts #define m_cur_block_coins m_cursors->m_txc_block_coins
#define m_cur_output_keys m_cursors.m_txc_output_keys #define m_cur_output_txs m_cursors->m_txc_output_txs
#define m_cur_txs m_cursors.m_txc_txs #define m_cur_output_indices m_cursors->m_txc_output_indices
#define m_cur_tx_heights m_cursors.m_txc_tx_heights #define m_cur_output_amounts m_cursors->m_txc_output_amounts
#define m_cur_tx_unlocks m_cursors.m_txc_tx_unlocks #define m_cur_output_keys m_cursors->m_txc_output_keys
#define m_cur_tx_outputs m_cursors.m_txc_tx_outputs #define m_cur_txs m_cursors->m_txc_txs
#define m_cur_spent_keys m_cursors.m_txc_spent_keys #define m_cur_tx_heights m_cursors->m_txc_tx_heights
#define m_cur_tx_unlocks m_cursors->m_txc_tx_unlocks
#define m_cur_tx_outputs m_cursors->m_txc_tx_outputs
#define m_cur_spent_keys m_cursors->m_txc_spent_keys
#define m_cur_hf_versions m_cursors->m_txc_hf_versions
typedef struct mdb_rflags
{
bool m_rf_txn;
bool m_rf_blocks;
bool m_rf_block_heights;
bool m_rf_block_hashes;
bool m_rf_block_timestamps;
bool m_rf_block_sizes;
bool m_rf_block_diffs;
bool m_rf_block_coins;
bool m_rf_output_txs;
bool m_rf_output_indices;
bool m_rf_output_amounts;
bool m_rf_output_keys;
bool m_rf_txs;
bool m_rf_tx_heights;
bool m_rf_tx_unlocks;
bool m_rf_tx_outputs;
bool m_rf_spent_keys;
bool m_rf_hf_versions;
} mdb_rflags;
typedef struct mdb_threadinfo
{
MDB_txn *m_ti_rtxn; // per-thread read txn
mdb_txn_cursors m_ti_rcursors; // per-thread read cursors
mdb_rflags m_ti_rflags; // per-thread read state
~mdb_threadinfo();
} mdb_threadinfo;
struct mdb_txn_safe struct mdb_txn_safe
{ {
@ -234,9 +269,11 @@ public:
virtual void batch_stop(); virtual void batch_stop();
virtual void batch_abort(); virtual void batch_abort();
virtual void block_txn_start(); virtual void block_txn_start(bool readonly);
virtual void block_txn_stop(); virtual void block_txn_stop();
virtual void block_txn_abort(); virtual void block_txn_abort();
virtual bool block_rtxn_start() const;
virtual void block_rtxn_stop() const;
virtual void pop_block(block& blk, std::vector<transaction>& txs); virtual void pop_block(block& blk, std::vector<transaction>& txs);
@ -355,7 +392,8 @@ private:
bool m_batch_transactions; // support for batch transactions bool m_batch_transactions; // support for batch transactions
bool m_batch_active; // whether batch transaction is in progress bool m_batch_active; // whether batch transaction is in progress
struct mdb_txn_cursors m_cursors; mdb_txn_cursors m_wcursors;
mutable boost::thread_specific_ptr<mdb_threadinfo> m_tinfo;
#if defined(__arm__) #if defined(__arm__)
// force a value so it can compile with 32-bit ARM // force a value so it can compile with 32-bit ARM

View file

@ -1403,6 +1403,7 @@ bool Blockchain::handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NO
{ {
LOG_PRINT_L3("Blockchain::" << __func__); LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock); CRITICAL_REGION_LOCAL(m_blockchain_lock);
m_db->block_txn_start(true);
rsp.current_blockchain_height = get_current_blockchain_height(); rsp.current_blockchain_height = get_current_blockchain_height();
std::list<block> blocks; std::list<block> blocks;
get_blocks(arg.blocks, blocks, rsp.missed_ids); get_blocks(arg.blocks, blocks, rsp.missed_ids);
@ -1424,6 +1425,7 @@ bool Blockchain::handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NO
// as done below if any standalone transactions were requested // as done below if any standalone transactions were requested
// and missed. // and missed.
rsp.missed_ids.splice(rsp.missed_ids.end(), missed_tx_ids); rsp.missed_ids.splice(rsp.missed_ids.end(), missed_tx_ids);
m_db->block_txn_stop();
return false; return false;
} }
@ -1442,6 +1444,7 @@ bool Blockchain::handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NO
for (const auto& tx: txs) for (const auto& tx: txs)
rsp.txs.push_back(t_serializable_object_to_blob(tx)); rsp.txs.push_back(t_serializable_object_to_blob(tx));
m_db->block_txn_stop();
return true; return true;
} }
//------------------------------------------------------------------ //------------------------------------------------------------------
@ -1585,12 +1588,14 @@ bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qbloc
return false; return false;
} }
m_db->block_txn_start(true);
// make sure that the last block in the request's block list matches // make sure that the last block in the request's block list matches
// the genesis block // the genesis block
auto gen_hash = m_db->get_block_hash_from_height(0); auto gen_hash = m_db->get_block_hash_from_height(0);
if(qblock_ids.back() != gen_hash) if(qblock_ids.back() != gen_hash)
{ {
LOG_PRINT_L1("Client sent wrong NOTIFY_REQUEST_CHAIN: genesis block missmatch: " << std::endl << "id: " << qblock_ids.back() << ", " << std::endl << "expected: " << gen_hash << "," << std::endl << " dropping connection"); LOG_PRINT_L1("Client sent wrong NOTIFY_REQUEST_CHAIN: genesis block missmatch: " << std::endl << "id: " << qblock_ids.back() << ", " << std::endl << "expected: " << gen_hash << "," << std::endl << " dropping connection");
m_db->block_txn_abort();
return false; return false;
} }
@ -1612,9 +1617,11 @@ bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qbloc
catch (const std::exception& e) catch (const std::exception& e)
{ {
LOG_PRINT_L1("Non-critical error trying to find block by hash in BlockchainDB, hash: " << *bl_it); LOG_PRINT_L1("Non-critical error trying to find block by hash in BlockchainDB, hash: " << *bl_it);
m_db->block_txn_abort();
return false; return false;
} }
} }
m_db->block_txn_stop();
// this should be impossible, as we checked that we share the genesis block, // this should be impossible, as we checked that we share the genesis block,
// but just in case... // but just in case...
@ -2777,6 +2784,7 @@ void Blockchain::check_against_checkpoints(const checkpoints& points, bool enfor
{ {
const auto& pts = points.get_points(); const auto& pts = points.get_points();
m_db->batch_start();
for (const auto& pt : pts) for (const auto& pt : pts)
{ {
// if the checkpoint is for a block we don't have yet, move on // if the checkpoint is for a block we don't have yet, move on
@ -2800,6 +2808,7 @@ void Blockchain::check_against_checkpoints(const checkpoints& points, bool enfor
} }
} }
} }
m_db->batch_stop();
} }
//------------------------------------------------------------------ //------------------------------------------------------------------
// returns false if any of the checkpoints loading returns false. // returns false if any of the checkpoints loading returns false.

View file

@ -254,8 +254,11 @@ bool HardFork::reorganize_from_chain_height(uint64_t height)
bool HardFork::rescan_from_block_height(uint64_t height) bool HardFork::rescan_from_block_height(uint64_t height)
{ {
CRITICAL_REGION_LOCAL(lock); CRITICAL_REGION_LOCAL(lock);
if (height >= db.height()) db.block_txn_start(true);
if (height >= db.height()) {
db.block_txn_stop();
return false; return false;
}
versions.clear(); versions.clear();
@ -273,6 +276,7 @@ bool HardFork::rescan_from_block_height(uint64_t height)
current_fork_index = 0; current_fork_index = 0;
while (current_fork_index + 1 < heights.size() && heights[current_fork_index].version != lastv) while (current_fork_index + 1 < heights.size() && heights[current_fork_index].version != lastv)
++current_fork_index; ++current_fork_index;
db.block_txn_stop();
return true; return true;
} }