Skip to content

Commit

Permalink
Minor bugfixes, redundancy removal
Browse files Browse the repository at this point in the history
Minor bugfixes in block removal

Storing outputs outside their transactions is largely unnecessary, and
thus has been removed.
  • Loading branch information
tewinget authored and warptangent committed Jan 5, 2015
1 parent 71b18d7 commit ab7951d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 10 deletions.
106 changes: 97 additions & 9 deletions src/cryptonote_core/BlockchainDB_impl/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash)
LOG_PRINT_L1("Failed to add removal of tx block height to db transaction");
throw DB_ERROR("Failed to add removal of tx block height to db transaction");
}

remove_tx_outputs(tx_hash);

if (mdb_del(*m_write_txn, m_tx_outputs, &val_h, NULL))
{
LOG_PRINT_L1("Failed to add removal of tx outputs to db transaction");
Expand Down Expand Up @@ -414,6 +417,8 @@ void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_ou
throw DB_ERROR("Failed to add output amount to db transaction");
}

/****** Uncomment if ever outputs actually need to be stored in this manner
*
blobdata b = output_to_blob(tx_output);
v.mv_size = b.size();
Expand All @@ -428,18 +433,67 @@ void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_ou
LOG_PRINT_L0("Failed to add output global index to db transaction");
throw DB_ERROR("Failed to add output global index to db transaction");
}
************************************************************************/

m_num_outputs++;
}

void BlockchainLMDB::remove_tx_outputs(const crypto::hash& tx_hash)
{

lmdb_cur cur(*m_write_txn, m_tx_outputs);

crypto::hash h_cpy = tx_hash;
MDB_val k;
k.mv_size = sizeof(h_cpy);
k.mv_data = &h_cpy;

MDB_val v;

auto result = mdb_cursor_get(cur, &k, &v, MDB_SET);
if (result == MDB_NOTFOUND)
{
LOG_ERROR("Attempting to remove a tx's outputs, but none found. Continuing, but...be wary, because that's weird.");
}
else if (result)
{
LOG_PRINT_L0("DB error attempting to get an output");
throw DB_ERROR("DB error attempting to get an output");
}
else
{
size_t num_elems = 0;
mdb_cursor_count(cur, &num_elems);

mdb_cursor_get(cur, &k, &v, MDB_FIRST_DUP);

for (uint64_t i = 0; i < num_elems; ++i)
{
remove_output(*(uint64_t*)v.mv_data);
if (i < num_elems - 1)
{
mdb_cursor_get(cur, &k, &v, MDB_NEXT_DUP);
}
}
}

cur.close();
}

void BlockchainLMDB::remove_output(const tx_out& tx_output)
{
return;
}

void BlockchainLMDB::remove_output(const uint64_t& out_index)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();

MDB_val k;
MDB_val v;

/****** Uncomment if ever outputs actually need to be stored in this manner
blobdata b;
t_serializable_object_to_blob(tx_output, b);
k.mv_size = b.size();
Expand All @@ -460,7 +514,15 @@ void BlockchainLMDB::remove_output(const tx_out& tx_output)
throw DB_ERROR("Error adding removal of output global index to db transaction");
}
result = mdb_del(*m_write_txn, m_output_indices, &v, NULL);
result = mdb_del(*m_write_txn, m_outputs, &v, NULL);
if (result != 0 && result != MDB_NOTFOUND)
{
LOG_PRINT_L1("Error adding removal of output to db transaction");
throw DB_ERROR("Error adding removal of output to db transaction");
}
*********************************************************************/

auto result = mdb_del(*m_write_txn, m_output_indices, &v, NULL);
if (result != 0 && result != MDB_NOTFOUND)
{
LOG_PRINT_L1("Error adding removal of output tx index to db transaction");
Expand All @@ -481,13 +543,7 @@ void BlockchainLMDB::remove_output(const tx_out& tx_output)
throw DB_ERROR("Error adding removal of output amount to db transaction");
}

result = mdb_del(*m_write_txn, m_outputs, &v, NULL);
if (result != 0 && result != MDB_NOTFOUND)
{
LOG_PRINT_L1("Error adding removal of output to db transaction");
throw DB_ERROR("Error adding removal of output to db transaction");
}

m_num_outputs--;
}

void BlockchainLMDB::add_spent_key(const crypto::key_image& k_image)
Expand Down Expand Up @@ -674,9 +730,12 @@ void BlockchainLMDB::open(const std::string& filename)

lmdb_db_open(txn, LMDB_OUTPUT_TXS, MDB_INTEGERKEY | MDB_CREATE, m_output_txs, "Failed to open db handle for m_output_txs");
lmdb_db_open(txn, LMDB_OUTPUT_INDICES, MDB_INTEGERKEY | MDB_CREATE, m_output_indices, "Failed to open db handle for m_output_indices");
lmdb_db_open(txn, LMDB_OUTPUT_GINDICES, MDB_CREATE, m_output_gindices, "Failed to open db handle for m_output_gindices");
lmdb_db_open(txn, LMDB_OUTPUT_AMOUNTS, MDB_INTEGERKEY | MDB_DUPSORT | MDB_CREATE, m_output_amounts, "Failed to open db handle for m_output_amounts");

/*************** not used, but kept for posterity
lmdb_db_open(txn, LMDB_OUTPUTS, MDB_INTEGERKEY | MDB_CREATE, m_outputs, "Failed to open db handle for m_outputs");
lmdb_db_open(txn, LMDB_OUTPUT_GINDICES, MDB_CREATE, m_output_gindices, "Failed to open db handle for m_output_gindices");
*************************************************/

lmdb_db_open(txn, LMDB_SPENT_KEYS, MDB_CREATE, m_spent_keys, "Failed to open db handle for m_outputs");

Expand Down Expand Up @@ -1471,8 +1530,11 @@ tx_out BlockchainLMDB::get_output(const crypto::hash& h, const uint64_t& index)
return output_from_blob(b);
}

// As this is not used, its return is now a blank output.
// This will save on space in the db.
tx_out BlockchainLMDB::get_output(const uint64_t& index)
{
return tx_out();
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();

Expand Down Expand Up @@ -1709,4 +1771,30 @@ uint64_t BlockchainLMDB::add_block( const block& blk
return ++m_height;
}

void BlockchainLMDB::pop_block(block& blk, std::vector<transaction>& txs)
{
txn_safe txn;
if (mdb_txn_begin(m_env, NULL, 0, txn))
{
LOG_PRINT_L0("Failed to create a transaction for the db");
throw DB_ERROR("Failed to create a transaction for the db");
}
m_write_txn = &txn;

uint64_t num_outputs = m_num_outputs;
try
{
BlockchainDB::pop_block(blk, txs);

txn.commit();
}
catch (...)
{
m_num_outputs = num_outputs;
throw;
}

--m_height;
}

} // namespace cryptonote
6 changes: 6 additions & 0 deletions src/cryptonote_core/BlockchainDB_impl/db_lmdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ class BlockchainLMDB : public BlockchainDB
, const std::vector<transaction>& txs
);

virtual void pop_block(block& blk, std::vector<transaction>& txs);

private:
virtual void add_block( const block& blk
, const size_t& block_size
Expand All @@ -191,6 +193,10 @@ class BlockchainLMDB : public BlockchainDB

virtual void remove_output(const tx_out& tx_output);

void remove_tx_outputs(const crypto::hash& tx_hash);

void remove_output(const uint64_t& out_index);

virtual void add_spent_key(const crypto::key_image& k_image);

virtual void remove_spent_key(const crypto::key_image& k_image);
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_core/blockchain_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ class BlockchainDB
// 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);
virtual void pop_block(block& blk, std::vector<transaction>& txs);


// return true if a transaction with hash <h> exists
Expand Down

0 comments on commit ab7951d

Please sign in to comment.