Complete implementation of transaction removal

Complete method BlockchainLMDB::remove_output()
  - use output index as the key for:
    m_output_indices, m_output_txs, m_output_keys
  - call new method BlockchainLMDB::remove_amount_output_index()

Add method to remove amount output index.
  - BlockchainLMDB::remove_amount_output_index()
  - for m_output_amounts

This also fixes the segfault when blockchain reorganization is
attempted.
This commit is contained in:
warptangent 2015-01-11 18:04:04 -08:00
parent 1701c26750
commit 4d0a94b20c
No known key found for this signature in database
GPG Key ID: 0E490BEBFBE4E92D
4 changed files with 99 additions and 25 deletions

View File

@ -262,7 +262,7 @@ void BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, const tr
throw0(DB_ERROR("Failed to add tx unlock time to db transaction"));
}
void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash)
void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();
@ -279,7 +279,7 @@ void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash)
if (mdb_del(*m_write_txn, m_tx_heights, &val_h, NULL))
throw1(DB_ERROR("Failed to add removal of tx block height to db transaction"));
remove_tx_outputs(tx_hash);
remove_tx_outputs(tx_hash, tx);
if (mdb_del(*m_write_txn, m_tx_outputs, &val_h, NULL))
throw1(DB_ERROR("Failed to add removal of tx outputs to db transaction"));
@ -330,8 +330,9 @@ void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_ou
m_num_outputs++;
}
void BlockchainLMDB::remove_tx_outputs(const crypto::hash& tx_hash)
void BlockchainLMDB::remove_tx_outputs(const crypto::hash& tx_hash, const transaction& tx)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
lmdb_cur cur(*m_write_txn, m_tx_outputs);
@ -356,7 +357,8 @@ void BlockchainLMDB::remove_tx_outputs(const crypto::hash& tx_hash)
for (uint64_t i = 0; i < num_elems; ++i)
{
remove_output(*(const uint64_t*)v.mv_data);
const tx_out tx_output = tx.vout[i];
remove_output(*(const uint64_t*)v.mv_data, tx_output.amount);
if (i < num_elems - 1)
{
mdb_cursor_get(cur, &k, &v, MDB_NEXT_DUP);
@ -367,17 +369,19 @@ void BlockchainLMDB::remove_tx_outputs(const crypto::hash& tx_hash)
cur.close();
}
// TODO: probably remove this function
void BlockchainLMDB::remove_output(const tx_out& tx_output)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__ << " (unused version - does nothing)");
return;
}
void BlockchainLMDB::remove_output(const uint64_t& out_index)
void BlockchainLMDB::remove_output(const uint64_t& out_index, const uint64_t amount)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();
MDB_val k;
MDB_val_copy<uint64_t> k(out_index);
MDB_val v;
/****** Uncomment if ever outputs actually need to be stored in this manner
@ -400,29 +404,94 @@ void BlockchainLMDB::remove_output(const uint64_t& out_index)
throw1(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)
throw1(DB_ERROR("Error adding removal of output tx index to db transaction"));
result = mdb_del(*m_write_txn, m_output_txs, &v, NULL);
if (result != 0 && result != MDB_NOTFOUND)
throw1(DB_ERROR("Error adding removal of output tx hash to db transaction"));
result = mdb_del(*m_write_txn, m_output_amounts, &v, NULL);
if (result != 0 && result != MDB_NOTFOUND)
throw1(DB_ERROR("Error adding removal of output amount to db transaction"));
result = mdb_del(*m_write_txn, m_output_keys, &v, NULL);
auto result = mdb_del(*m_write_txn, m_output_indices, &k, NULL);
if (result == MDB_NOTFOUND)
{
LOG_PRINT_L2("Removing output, no public key found.");
LOG_PRINT_L0("Unexpected: global output index not found in m_output_indices");
}
else if (result)
{
throw1(DB_ERROR("Error adding removal of output tx index to db transaction"));
}
result = mdb_del(*m_write_txn, m_output_txs, &k, NULL);
// if (result != 0 && result != MDB_NOTFOUND)
// throw1(DB_ERROR("Error adding removal of output tx hash to db transaction"));
if (result == MDB_NOTFOUND)
{
LOG_PRINT_L0("Unexpected: global output index not found in m_output_txs");
}
else if (result)
{
throw1(DB_ERROR("Error adding removal of output tx hash to db transaction"));
}
result = mdb_del(*m_write_txn, m_output_keys, &k, NULL);
if (result == MDB_NOTFOUND)
{
LOG_PRINT_L0("Unexpected: global output index not found in m_output_keys");
}
else if (result)
throw1(DB_ERROR("Error adding removal of output pubkey to db transaction"));
remove_amount_output_index(amount, out_index);
m_num_outputs--;
}
void BlockchainLMDB::remove_amount_output_index(const uint64_t amount, const uint64_t global_output_index)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();
lmdb_cur cur(*m_write_txn, m_output_amounts);
MDB_val_copy<uint64_t> k(amount);
MDB_val v;
auto result = mdb_cursor_get(cur, &k, &v, MDB_SET);
if (result == MDB_NOTFOUND)
throw1(OUTPUT_DNE("Attempting to get an output index by amount and amount index, but amount not found"));
else if (result)
throw0(DB_ERROR("DB error attempting to get an output"));
size_t num_elems = 0;
mdb_cursor_count(cur, &num_elems);
mdb_cursor_get(cur, &k, &v, MDB_FIRST_DUP);
uint64_t amount_output_index = 0;
uint64_t goi = 0;
bool found_index = false;
for (uint64_t i = 0; i < num_elems; ++i)
{
mdb_cursor_get(cur, &k, &v, MDB_GET_CURRENT);
goi = *(const uint64_t *)v.mv_data;
if (goi == global_output_index)
{
amount_output_index = i;
found_index = true;
break;
}
mdb_cursor_get(cur, &k, &v, MDB_NEXT_DUP);
}
if (found_index)
{
// found the amount output index
// now delete it
result = mdb_cursor_del(cur, 0);
if (result)
throw0(DB_ERROR(std::string("Error deleting amount output index ").append(boost::lexical_cast<std::string>(amount_output_index)).c_str()));
}
else
{
// not found
cur.close();
throw1(OUTPUT_DNE("Failed to find amount output index"));
}
cur.close();
}
void BlockchainLMDB::add_spent_key(const crypto::key_image& k_image)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);

View File

@ -190,15 +190,16 @@ private:
virtual void add_transaction_data(const crypto::hash& blk_hash, const transaction& tx);
virtual void remove_transaction_data(const crypto::hash& tx_hash);
virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx);
virtual void add_output(const crypto::hash& tx_hash, const tx_out& tx_output, const uint64_t& local_index);
virtual void remove_output(const tx_out& tx_output);
void remove_tx_outputs(const crypto::hash& tx_hash);
void remove_tx_outputs(const crypto::hash& tx_hash, const transaction& tx);
void remove_output(const uint64_t& out_index);
void remove_output(const uint64_t& out_index, const uint64_t amount);
void remove_amount_output_index(const uint64_t amount, const uint64_t global_output_index);
virtual void add_spent_key(const crypto::key_image& k_image);

View File

@ -107,6 +107,9 @@ void BlockchainDB::remove_transaction(const crypto::hash& tx_hash)
{
transaction tx = get_tx(tx_hash);
// TODO: This loop calling remove_output() should be removed. It doesn't do
// anything (function is empty), and the outputs were already intended to be
// removed later as part of remove_transaction_data().
for (const tx_out& tx_output : tx.vout)
{
remove_output(tx_output);
@ -120,7 +123,8 @@ void BlockchainDB::remove_transaction(const crypto::hash& tx_hash)
}
}
remove_transaction_data(tx_hash);
// need tx as tx.vout has the tx outputs, and the output amounts are needed
remove_transaction_data(tx_hash, tx);
}
} // namespace cryptonote

View File

@ -274,7 +274,7 @@ private:
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;
virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) = 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;