From 5e673c03fec66024027b94229fa2e317d7767a20 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Fri, 5 Apr 2019 09:28:30 +0000 Subject: blockchain_db: fix db txn ending too early The db txn in add_block ending caused the entire overarching batch txn to stop. Also add a new guard class so a db txn can be stopped in the face of exceptions. Also use a read only db txn in init when the db itself is read only, and do not save the max tx size in that case. --- tests/core_tests/chaingen.h | 19 +++++++++++++++++-- tests/unit_tests/blockchain_db.cpp | 4 ++++ 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/core_tests/chaingen.h b/tests/core_tests/chaingen.h index f2bcb7787..2ecbdc436 100644 --- a/tests/core_tests/chaingen.h +++ b/tests/core_tests/chaingen.h @@ -643,7 +643,15 @@ public: log_event("cryptonote::block"); cryptonote::block_verification_context bvc = AUTO_VAL_INIT(bvc); - m_c.handle_incoming_block(t_serializable_object_to_blob(b), &b, bvc); + cryptonote::blobdata bd = t_serializable_object_to_blob(b); + std::vector pblocks; + if (m_c.prepare_handle_incoming_blocks(std::vector(1, {bd, {}}), pblocks)) + { + m_c.handle_incoming_block(bd, &b, bvc); + m_c.cleanup_handle_incoming_blocks(); + } + else + bvc.m_verifivation_failed = true; bool r = check_block_verification_context(bvc, m_ev_index, b, m_validator); CHECK_AND_NO_ASSERT_MES(r, false, "block verification context check failed"); return r; @@ -666,7 +674,14 @@ public: log_event("serialized_block"); cryptonote::block_verification_context bvc = AUTO_VAL_INIT(bvc); - m_c.handle_incoming_block(sr_block.data, NULL, bvc); + std::vector pblocks; + if (m_c.prepare_handle_incoming_blocks(std::vector(1, {sr_block.data, {}}), pblocks)) + { + m_c.handle_incoming_block(sr_block.data, NULL, bvc); + m_c.cleanup_handle_incoming_blocks(); + } + else + bvc.m_verifivation_failed = true; cryptonote::block blk; std::stringstream ss; diff --git a/tests/unit_tests/blockchain_db.cpp b/tests/unit_tests/blockchain_db.cpp index 4fbc21ddc..f302d7946 100644 --- a/tests/unit_tests/blockchain_db.cpp +++ b/tests/unit_tests/blockchain_db.cpp @@ -271,6 +271,8 @@ TYPED_TEST(BlockchainDBTest, AddBlock) this->get_filenames(); this->init_hard_fork(); + db_wtxn_guard guard(this->m_db); + // adding a block with no parent in the blockchain should throw. // note: this shouldn't be possible, but is a good (and cheap) failsafe. // @@ -317,6 +319,8 @@ TYPED_TEST(BlockchainDBTest, RetrieveBlockData) this->get_filenames(); this->init_hard_fork(); + db_wtxn_guard guard(this->m_db); + ASSERT_NO_THROW(this->m_db->add_block(this->m_blocks[0], t_sizes[0], t_sizes[0], t_diffs[0], t_coins[0], this->m_txs[0])); ASSERT_EQ(t_sizes[0], this->m_db->get_block_weight(0)); -- cgit v1.2.3