From fd46c96dce043cbf9f018f59ac9cedb12f6a4286 Mon Sep 17 00:00:00 2001 From: warptangent Date: Mon, 8 Feb 2016 08:32:36 -0800 Subject: BlockchainDB/LMDB: Refactor block-scope DB txn handling for add block Move block-scope txn start and stop from BlockchainLMDB to BlockchainDB. --- src/blockchain_db/blockchain_db.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/blockchain_db/blockchain_db.cpp') diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp index 3737dfc4f..b504f946a 100644 --- a/src/blockchain_db/blockchain_db.cpp +++ b/src/blockchain_db/blockchain_db.cpp @@ -99,6 +99,8 @@ uint64_t BlockchainDB::add_block( const block& blk , const std::vector& txs ) { + block_txn_start(); + TIME_MEASURE_START(time1); crypto::hash blk_hash = get_block_hash(blk); TIME_MEASURE_FINISH(time1); @@ -125,9 +127,15 @@ uint64_t BlockchainDB::add_block( const block& blk TIME_MEASURE_FINISH(time1); time_add_transaction += time1; + // DB's new height based on this added block is only incremented after this + // function returns, so height() here returns the new previous height. + uint64_t prev_height = height(); + + block_txn_stop(); + ++num_calls; - return height(); + return prev_height; } void BlockchainDB::pop_block(block& blk, std::vector& txs) -- cgit v1.2.3 From 3800875406fecab5123564e58ddb698bce550441 Mon Sep 17 00:00:00 2001 From: warptangent Date: Mon, 8 Feb 2016 07:51:57 -0800 Subject: Make HardFork object available to BlockchainDB and derived DB implementations This will later allow the HardFork object's DB update functions to be called when the DB transaction that persists across block add/remove is open. --- src/blockchain_db/berkeleydb/db_bdb.cpp | 2 ++ src/blockchain_db/blockchain_db.cpp | 5 +++++ src/blockchain_db/blockchain_db.h | 7 +++++++ src/blockchain_db/lmdb/db_lmdb.cpp | 2 ++ src/cryptonote_core/blockchain.cpp | 2 ++ 5 files changed, 18 insertions(+) (limited to 'src/blockchain_db/blockchain_db.cpp') diff --git a/src/blockchain_db/berkeleydb/db_bdb.cpp b/src/blockchain_db/berkeleydb/db_bdb.cpp index f572ddf9d..cdbca52f9 100644 --- a/src/blockchain_db/berkeleydb/db_bdb.cpp +++ b/src/blockchain_db/berkeleydb/db_bdb.cpp @@ -781,6 +781,8 @@ BlockchainBDB::BlockchainBDB(bool batch_transactions) : m_batch_transactions = batch_transactions; m_write_txn = nullptr; m_height = 0; + + m_hardfork = nullptr; } void BlockchainBDB::open(const std::string& filename, const int db_flags) diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp index b504f946a..4b05ddf24 100644 --- a/src/blockchain_db/blockchain_db.cpp +++ b/src/blockchain_db/blockchain_db.cpp @@ -138,6 +138,11 @@ uint64_t BlockchainDB::add_block( const block& blk return prev_height; } +void BlockchainDB::set_hard_fork(HardFork*& hf) +{ + m_hardfork = hf; +} + void BlockchainDB::pop_block(block& blk, std::vector& txs) { blk = get_top_block(); diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h index 31d5e2644..5926f34a5 100644 --- a/src/blockchain_db/blockchain_db.h +++ b/src/blockchain_db/blockchain_db.h @@ -28,12 +28,15 @@ #ifndef BLOCKCHAIN_DB_H #define BLOCKCHAIN_DB_H +#pragma once + #include #include #include #include "crypto/hash.h" #include "cryptonote_core/cryptonote_basic.h" #include "cryptonote_core/difficulty.h" +#include "cryptonote_core/hardfork.h" /* DB Driver Interface * @@ -322,6 +325,8 @@ protected: uint64_t time_commit1 = 0; bool m_auto_remove_logs = true; + HardFork* m_hardfork; + public: // virtual dtor @@ -372,6 +377,8 @@ public: virtual void block_txn_stop() = 0; virtual void block_txn_abort() = 0; + virtual void set_hard_fork(HardFork*& hf); + // adds a block with the given metadata to the top of the blockchain, returns the new height // NOTE: subclass implementations of this (or the functions it calls) need // to handle undoing any partially-added blocks in the event of a failure. diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 8bd64e70d..28e6f5525 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -945,6 +945,8 @@ BlockchainLMDB::BlockchainLMDB(bool batch_transactions) m_write_batch_txn = nullptr; m_batch_active = false; m_height = 0; + + m_hardfork = nullptr; } void BlockchainLMDB::open(const std::string& filename, const int mdb_flags) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index e43875bdc..94ef4b894 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -290,6 +290,8 @@ bool Blockchain::init(BlockchainDB* db, const bool testnet, const bool fakechain } m_hardfork->init(); + m_db->set_hard_fork(m_hardfork); + // if the blockchain is new, add the genesis block // this feels kinda kludgy to do it this way, but can be looked at later. // TODO: add function to create and store genesis block, -- cgit v1.2.3 From e02577f594ecd0a619ef3efaf65de90df9c20289 Mon Sep 17 00:00:00 2001 From: warptangent Date: Mon, 8 Feb 2016 08:09:07 -0800 Subject: Move HardFork DB update to BlockchainDB::add_block() Ensures the database is consistent. Also simplifes blockchain_import in that verify mode off has less to work around. --- src/blockchain_db/blockchain_db.cpp | 1 + src/blockchain_utilities/blockchain_import.cpp | 3 --- src/cryptonote_core/blockchain.cpp | 3 --- 3 files changed, 1 insertion(+), 6 deletions(-) (limited to 'src/blockchain_db/blockchain_db.cpp') diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp index 4b05ddf24..270b5399e 100644 --- a/src/blockchain_db/blockchain_db.cpp +++ b/src/blockchain_db/blockchain_db.cpp @@ -130,6 +130,7 @@ uint64_t BlockchainDB::add_block( const block& blk // DB's new height based on this added block is only incremented after this // function returns, so height() here returns the new previous height. uint64_t prev_height = height(); + m_hardfork->add(blk, prev_height); block_txn_stop(); diff --git a/src/blockchain_utilities/blockchain_import.cpp b/src/blockchain_utilities/blockchain_import.cpp index 7eb493b6d..447d3a21c 100644 --- a/src/blockchain_utilities/blockchain_import.cpp +++ b/src/blockchain_utilities/blockchain_import.cpp @@ -469,9 +469,6 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path, try { simple_core.add_block(b, block_size, cumulative_difficulty, coins_generated, txs); - #if !defined(BLOCKCHAIN_DB) || (BLOCKCHAIN_DB == DB_LMDB) - simple_core.m_hardfork->add(b, h-1); - #endif } catch (const std::exception& e) { diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 94ef4b894..6fe998260 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -2692,9 +2692,6 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash& TIME_MEASURE_FINISH(addblock); - // this will not fail since check succeeded above - m_hardfork->add(bl, new_height - 1); - // do this after updating the hard fork state since the size limit may change due to fork update_next_cumulative_size_limit(); -- cgit v1.2.3