aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--contrib/epee/include/console_handler.h2
-rw-r--r--src/blockchain_db/berkeleydb/db_bdb.cpp146
-rw-r--r--src/blockchain_db/berkeleydb/db_bdb.h5
-rw-r--r--src/blockchain_db/blockchain_db.h5
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.cpp177
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.h5
-rw-r--r--src/blockchain_utilities/CMakeLists.txt27
-rw-r--r--src/blockchain_utilities/blockchain_dump.cpp431
-rw-r--r--src/cryptonote_config.h1
-rw-r--r--src/cryptonote_core/blockchain.cpp35
-rw-r--r--src/cryptonote_core/blockchain.h5
-rw-r--r--src/cryptonote_core/blockchain_storage.cpp40
-rw-r--r--src/cryptonote_core/blockchain_storage.h5
-rw-r--r--src/cryptonote_core/cryptonote_basic.h4
-rw-r--r--src/cryptonote_core/hardfork.cpp21
-rw-r--r--src/daemon/command_parser_executor.cpp7
-rw-r--r--src/daemon/command_parser_executor.h2
-rw-r--r--src/daemon/command_server.cpp5
-rw-r--r--src/daemon/rpc_command_executor.cpp55
-rw-r--r--src/daemon/rpc_command_executor.h2
-rw-r--r--src/wallet/wallet2.cpp2
-rw-r--r--src/wallet/wallet2.h1
-rw-r--r--tests/unit_tests/hardfork.cpp7
23 files changed, 950 insertions, 40 deletions
diff --git a/contrib/epee/include/console_handler.h b/contrib/epee/include/console_handler.h
index 5dd3eaba9..0eff095e1 100644
--- a/contrib/epee/include/console_handler.h
+++ b/contrib/epee/include/console_handler.h
@@ -275,7 +275,7 @@ namespace epee
std::string command;
bool get_line_ret = m_stdin_reader.get_line(command);
- if (m_stdin_reader.eos())
+ if (!m_running || m_stdin_reader.eos())
{
break;
}
diff --git a/src/blockchain_db/berkeleydb/db_bdb.cpp b/src/blockchain_db/berkeleydb/db_bdb.cpp
index bd579ed38..6560ce5c2 100644
--- a/src/blockchain_db/berkeleydb/db_bdb.cpp
+++ b/src/blockchain_db/berkeleydb/db_bdb.cpp
@@ -385,7 +385,7 @@ void BlockchainBDB::remove_tx_outputs(const crypto::hash& tx_hash, const transac
auto result = cur->get(&k, &v, DB_SET);
if (result == DB_NOTFOUND)
{
- throw0(DB_ERROR("Attempting to remove a tx's outputs, but none found."));
+ throw0(OUTPUT_DNE("Attempting to remove a tx's outputs, but none found."));
}
else if (result)
{
@@ -533,6 +533,126 @@ void BlockchainBDB::remove_spent_key(const crypto::key_image& k_image)
throw1(DB_ERROR("Error adding removal of key image to db transaction"));
}
+bool BlockchainBDB::for_all_key_images(std::function<bool(const crypto::key_image&)> f) const
+{
+ LOG_PRINT_L3("BlockchainBDB::" << __func__);
+ check_open();
+
+ bdb_cur cur(DB_DEFAULT_TX, m_spent_keys);
+
+ Dbt_copy<crypto::key_image> k;
+ Dbt_copy<char> v;
+ bool ret = true;
+ int result;
+ while ((result = cur->get(&k, &v, DB_NEXT)) == 0)
+ {
+ if (!f(k))
+ {
+ ret = false;
+ break;
+ }
+ }
+ if (result != DB_NOTFOUND)
+ ret = false;
+
+ cur.close();
+ return ret;
+}
+
+bool BlockchainBDB::for_all_blocks(std::function<bool(uint64_t, const crypto::hash&, const cryptonote::block&)> f) const
+{
+ LOG_PRINT_L3("BlockchainBDB::" << __func__);
+ check_open();
+
+ bdb_cur cur(DB_DEFAULT_TX, m_blocks);
+
+ Dbt_copy<uint64_t> k;
+ Dbt_safe v;
+ bool ret = true;
+ int result;
+ while ((result = cur->get(&k, &v, DB_NEXT)) == 0)
+ {
+ uint64_t height = k - 1;
+ blobdata bd;
+ bd.assign(reinterpret_cast<char*>(v.get_data()), v.get_size());
+ block b;
+ if (!parse_and_validate_block_from_blob(bd, b))
+ throw0(DB_ERROR("Failed to parse block from blob retrieved from the db"));
+ crypto::hash hash;
+ if (!get_block_hash(b, hash))
+ throw0(DB_ERROR("Failed to get block hash from blob retrieved from the db"));
+ if (!f(height, hash, b))
+ {
+ ret = false;
+ break;
+ }
+ }
+ if (result != DB_NOTFOUND)
+ ret = false;
+
+ cur.close();
+ return ret;
+}
+
+bool BlockchainBDB::for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)> f) const
+{
+ LOG_PRINT_L3("BlockchainBDB::" << __func__);
+ check_open();
+
+ bdb_cur cur(DB_DEFAULT_TX, m_txs);
+
+ Dbt_copy<crypto::hash> k;
+ Dbt_safe v;
+ bool ret = true;
+ int result;
+ while ((result = cur->get(&k, &v, DB_NEXT)) == 0)
+ {
+ blobdata bd;
+ bd.assign(reinterpret_cast<char*>(v.get_data()), v.get_size());
+ transaction tx;
+ if (!parse_and_validate_tx_from_blob(bd, tx))
+ throw0(DB_ERROR("Failed to parse tx from blob retrieved from the db"));
+ if (!f(k, tx))
+ {
+ ret = false;
+ break;
+ }
+ }
+ if (result != DB_NOTFOUND)
+ ret = false;
+
+ cur.close();
+ return ret;
+}
+
+bool BlockchainBDB::for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const
+{
+ LOG_PRINT_L3("BlockchainBDB::" << __func__);
+ check_open();
+
+ bdb_cur cur(DB_DEFAULT_TX, m_output_amounts);
+
+ Dbt_copy<uint64_t> k;
+ Dbt_copy<uint32_t> v;
+ bool ret = true;
+ int result;
+ while ((result = cur->get(&k, &v, DB_NEXT)) == 0)
+ {
+ uint32_t global_index = v - 1;
+ tx_out_index toi = get_output_tx_and_index_from_global(global_index);
+ if (!f(k, toi.first, toi.second))
+ {
+ ret = false;
+ break;
+ }
+ }
+ if (result != DB_NOTFOUND)
+ ret = false;
+
+ cur.close();
+ return ret;
+}
+
blobdata BlockchainBDB::output_to_blob(const tx_out& output) const
{
LOG_PRINT_L3("BlockchainBDB::" << __func__);
@@ -977,7 +1097,7 @@ block BlockchainBDB::get_block_from_height(const uint64_t& height) const
auto get_result = m_blocks->get(DB_DEFAULT_TX, &key, &result, 0);
if (get_result == DB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get block from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get block from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a block from the db"));
@@ -1002,7 +1122,7 @@ uint64_t BlockchainBDB::get_block_timestamp(const uint64_t& height) const
auto get_result = m_block_timestamps->get(DB_DEFAULT_TX, &key, &result, 0);
if (get_result == DB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get timestamp from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- timestamp not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get timestamp from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- timestamp not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a timestamp from the db"));
@@ -1034,7 +1154,7 @@ size_t BlockchainBDB::get_block_size(const uint64_t& height) const
auto get_result = m_block_sizes->get(DB_DEFAULT_TX, &key, &result, 0);
if (get_result == DB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get block size from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get block size from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a block size from the db"));
@@ -1052,7 +1172,7 @@ difficulty_type BlockchainBDB::get_block_cumulative_difficulty(const uint64_t& h
auto get_result = m_block_diffs->get(DB_DEFAULT_TX, &key, &result, 0);
if (get_result == DB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get cumulative difficulty from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- difficulty not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get cumulative difficulty from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- difficulty not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a cumulative difficulty from the db"));
@@ -1087,7 +1207,7 @@ uint64_t BlockchainBDB::get_block_already_generated_coins(const uint64_t& height
auto get_result = m_block_coins->get(DB_DEFAULT_TX, &key, &result, 0);
if (get_result == DB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get generated coins from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get generated coins from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a total generated coins from the db"));
@@ -1315,11 +1435,11 @@ output_data_t BlockchainBDB::get_output_key(const uint64_t& global_index) const
LOG_PRINT_L3("BlockchainBDB::" << __func__);
check_open();
- Dbt_copy<uint32_t> k(global_index);
+ Dbt_copy<uint32_t> k(global_index + 1);
Dbt_copy<output_data_t> v;
auto get_result = m_output_keys->get(DB_DEFAULT_TX, &k, &v, 0);
if (get_result == DB_NOTFOUND)
- throw0(DB_ERROR("Attempting to get output pubkey by global index, but key does not exist"));
+ throw1(OUTPUT_DNE("Attempting to get output pubkey by global index, but key does not exist"));
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve an output pubkey from the db"));
@@ -1467,7 +1587,7 @@ tx_out_index BlockchainBDB::get_output_tx_and_index_from_global(const uint64_t&
LOG_PRINT_L3("BlockchainBDB::" << __func__);
check_open();
- Dbt_copy<uint32_t> k(index);
+ Dbt_copy<uint32_t> k(index + 1);
Dbt_copy<crypto::hash > v;
auto get_result = m_output_txs->get(DB_DEFAULT_TX, &k, &v, 0);
@@ -1826,7 +1946,7 @@ void BlockchainBDB::get_output_key(const uint64_t &amount, const std::vector<uin
{
for (const uint64_t &index : global_indices)
{
- Dbt_copy<uint32_t> k(index);
+ Dbt_copy<uint32_t> k(index + 1);
Dbt_copy<output_data_t> v;
auto get_result = m_output_keys->get(DB_DEFAULT_TX, &k, &v, 0);
@@ -1880,7 +2000,7 @@ uint64_t BlockchainBDB::get_hard_fork_starting_height(uint8_t version) const
Dbt_copy<uint64_t> result;
auto get_result = m_hf_starting_heights->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
+ if (get_result == DB_NOTFOUND || get_result == DB_KEYEMPTY)
return std::numeric_limits<uint64_t>::max();
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve hard fork starting height from the db"));
@@ -1908,7 +2028,9 @@ uint8_t BlockchainBDB::get_hard_fork_version(uint64_t height) const
Dbt_copy<uint8_t> result;
auto get_result = m_hf_versions->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND || get_result)
+ if (get_result == DB_NOTFOUND || get_result == DB_KEYEMPTY)
+ throw0(OUTPUT_DNE("Error attempting to retrieve hard fork version from the db"));
+ else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve hard fork version from the db"));
return result;
diff --git a/src/blockchain_db/berkeleydb/db_bdb.h b/src/blockchain_db/berkeleydb/db_bdb.h
index 54edcf0ad..b51d745d8 100644
--- a/src/blockchain_db/berkeleydb/db_bdb.h
+++ b/src/blockchain_db/berkeleydb/db_bdb.h
@@ -375,6 +375,11 @@ private:
void get_output_global_indices(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<uint64_t> &global_indices);
+ virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
+ virtual bool for_all_blocks(std::function<bool(uint64_t, const crypto::hash&, const cryptonote::block&)>) const;
+ virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>) const;
+ virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const;
+
// Hard fork related storage
virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height);
virtual uint64_t get_hard_fork_starting_height(uint8_t version) const;
diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h
index 24bf4024d..1666e57c5 100644
--- a/src/blockchain_db/blockchain_db.h
+++ b/src/blockchain_db/blockchain_db.h
@@ -493,6 +493,11 @@ public:
// returns true if key image <img> is present in spent key images storage
virtual bool has_key_image(const crypto::key_image& img) const = 0;
+ virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const = 0;
+ virtual bool for_all_blocks(std::function<bool(uint64_t, const crypto::hash&, const cryptonote::block&)>) const = 0;
+ virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>) const = 0;
+ virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const = 0;
+
// Hard fork related storage
virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height) = 0;
virtual uint64_t get_hard_fork_starting_height(uint8_t version) const = 0;
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp
index 2695d83f8..1e7078c67 100644
--- a/src/blockchain_db/lmdb/db_lmdb.cpp
+++ b/src/blockchain_db/lmdb/db_lmdb.cpp
@@ -747,7 +747,7 @@ void BlockchainLMDB::remove_output(const uint64_t& out_index, const uint64_t amo
auto result = mdb_del(*m_write_txn, m_output_gindices, &k, NULL);
if (result != 0 && result != MDB_NOTFOUND)
- throw1(DB_ERROR("Error adding removal of output global index to db transaction"));
+ throw1(OUTPUT_DNE("Error adding removal of output global index to db transaction"));
result = mdb_del(*m_write_txn, m_outputs, &v, NULL);
if (result != 0 && result != MDB_NOTFOUND)
@@ -1242,7 +1242,7 @@ block BlockchainLMDB::get_block_from_height(const uint64_t& height) const
auto get_result = mdb_get(txn, m_blocks, &key, &result);
if (get_result == MDB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get block from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get block from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a block from the db"));
@@ -1278,7 +1278,7 @@ uint64_t BlockchainLMDB::get_block_timestamp(const uint64_t& height) const
auto get_result = mdb_get(*txn_ptr, m_block_timestamps, &key, &result);
if (get_result == MDB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get timestamp from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- timestamp not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get timestamp from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- timestamp not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a timestamp from the db"));
@@ -1322,7 +1322,7 @@ size_t BlockchainLMDB::get_block_size(const uint64_t& height) const
auto get_result = mdb_get(*txn_ptr, m_block_sizes, &key, &result);
if (get_result == MDB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get block size from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get block size from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a block size from the db"));
@@ -1351,7 +1351,7 @@ difficulty_type BlockchainLMDB::get_block_cumulative_difficulty(const uint64_t&
auto get_result = mdb_get(*txn_ptr, m_block_diffs, &key, &result);
if (get_result == MDB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get cumulative difficulty from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- difficulty not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get cumulative difficulty from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- difficulty not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a cumulative difficulty from the db"));
@@ -1398,7 +1398,7 @@ uint64_t BlockchainLMDB::get_block_already_generated_coins(const uint64_t& heigh
auto get_result = mdb_get(*txn_ptr, m_block_coins, &key, &result);
if (get_result == MDB_NOTFOUND)
{
- throw0(DB_ERROR(std::string("Attempt to get generated coins from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
+ throw0(BLOCK_DNE(std::string("Attempt to get generated coins from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a total generated coins from the db"));
@@ -1696,7 +1696,7 @@ output_data_t BlockchainLMDB::get_output_key(const uint64_t &global_index) const
MDB_val v;
auto get_result = mdb_get(txn, m_output_keys, &k, &v);
if (get_result == MDB_NOTFOUND)
- throw0(DB_ERROR("Attempting to get output pubkey by global index, but key does not exist"));
+ throw1(OUTPUT_DNE("Attempting to get output pubkey by global index, but key does not exist"));
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve an output pubkey from the db"));
txn.commit();
@@ -1968,6 +1968,165 @@ bool BlockchainLMDB::has_key_image(const crypto::key_image& img) const
return false;
}
+bool BlockchainLMDB::for_all_key_images(std::function<bool(const crypto::key_image&)> f) const
+{
+ LOG_PRINT_L3("BlockchainLMDB::" << __func__);
+ check_open();
+
+ mdb_txn_safe txn;
+ if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
+ throw0(DB_ERROR("Failed to create a transaction for the db"));
+
+ MDB_val k;
+ MDB_val v;
+ bool ret = true;
+
+ lmdb_cur cur(txn, m_spent_keys);
+ MDB_cursor_op op = MDB_FIRST;
+ while (1)
+ {
+ int ret = mdb_cursor_get(cur, &k, &v, op);
+ op = MDB_NEXT;
+ if (ret == MDB_NOTFOUND)
+ break;
+ if (ret < 0)
+ throw0(DB_ERROR("Failed to enumerate key images"));
+ const crypto::key_image k_image = *(crypto::key_image*)k.mv_data;
+ if (!f(k_image)) {
+ ret = false;
+ break;
+ }
+ }
+
+ cur.close();
+ txn.commit();
+
+ return ret;
+}
+
+bool BlockchainLMDB::for_all_blocks(std::function<bool(uint64_t, const crypto::hash&, const cryptonote::block&)> f) const
+{
+ LOG_PRINT_L3("BlockchainLMDB::" << __func__);
+ check_open();
+
+ mdb_txn_safe txn;
+ if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
+ throw0(DB_ERROR("Failed to create a transaction for the db"));
+
+ MDB_val k;
+ MDB_val v;
+ bool ret = true;
+
+ lmdb_cur cur(txn, m_blocks);
+ MDB_cursor_op op = MDB_FIRST;
+ while (1)
+ {
+ int ret = mdb_cursor_get(cur, &k, &v, op);
+ op = MDB_NEXT;
+ if (ret == MDB_NOTFOUND)
+ break;
+ if (ret)
+ throw0(DB_ERROR("Failed to enumerate blocks"));
+ uint64_t height = *(uint64_t*)k.mv_data;
+ blobdata bd;
+ bd.assign(reinterpret_cast<char*>(v.mv_data), v.mv_size);
+ block b;
+ if (!parse_and_validate_block_from_blob(bd, b))
+ throw0(DB_ERROR("Failed to parse block from blob retrieved from the db"));
+ crypto::hash hash;
+ if (!get_block_hash(b, hash))
+ throw0(DB_ERROR("Failed to get block hash from blob retrieved from the db"));
+ if (!f(height, hash, b)) {
+ ret = false;
+ break;
+ }
+ }
+
+ cur.close();
+ txn.commit();
+
+ return ret;
+}
+
+bool BlockchainLMDB::for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)> f) const
+{
+ LOG_PRINT_L3("BlockchainLMDB::" << __func__);
+ check_open();
+
+ mdb_txn_safe txn;
+ if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
+ throw0(DB_ERROR("Failed to create a transaction for the db"));
+
+ MDB_val k;
+ MDB_val v;
+ bool ret = true;
+
+ lmdb_cur cur(txn, m_txs);
+ MDB_cursor_op op = MDB_FIRST;
+ while (1)
+ {
+ int ret = mdb_cursor_get(cur, &k, &v, op);
+ op = MDB_NEXT;
+ if (ret == MDB_NOTFOUND)
+ break;
+ if (ret)
+ throw0(DB_ERROR("Failed to enumerate transactions"));
+ const crypto::hash hash = *(crypto::hash*)k.mv_data;
+ blobdata bd;
+ bd.assign(reinterpret_cast<char*>(v.mv_data), v.mv_size);
+ transaction tx;
+ if (!parse_and_validate_tx_from_blob(bd, tx))
+ throw0(DB_ERROR("Failed to parse tx from blob retrieved from the db"));
+ if (!f(hash, tx)) {
+ ret = false;
+ break;
+ }
+ }
+
+ cur.close();
+ txn.commit();
+
+ return ret;
+}
+
+bool BlockchainLMDB::for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const
+{
+ LOG_PRINT_L3("BlockchainLMDB::" << __func__);
+ check_open();
+
+ mdb_txn_safe txn;
+ if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn))
+ throw0(DB_ERROR("Failed to create a transaction for the db"));
+
+ MDB_val k;
+ MDB_val v;
+ bool ret = true;
+
+ lmdb_cur cur(txn, m_output_amounts);
+ MDB_cursor_op op = MDB_FIRST;
+ while (1)
+ {
+ int ret = mdb_cursor_get(cur, &k, &v, op);
+ op = MDB_NEXT;
+ if (ret == MDB_NOTFOUND)
+ break;
+ if (ret)
+ throw0(DB_ERROR("Failed to enumerate outputs"));
+ uint64_t amount = *(uint64_t*)k.mv_data;
+ uint64_t global_index = *(uint64_t*)v.mv_data;
+ tx_out_index toi = get_output_tx_and_index_from_global(global_index);
+ if (!f(amount, toi.first, toi.second)) {
+ ret = false;
+ break;
+ }
+ }
+
+ cur.close();
+ txn.commit();
+
+ return ret;
+}
+
// batch_num_blocks: (optional) Used to check if resize needed before batch transaction starts.
void BlockchainLMDB::batch_start(uint64_t batch_num_blocks)
{
@@ -2329,8 +2488,8 @@ void BlockchainLMDB::get_output_key(const uint64_t &amount, const std::vector<ui
MDB_val v;
auto get_result = mdb_get(*txn_ptr, m_output_keys, &k, &v);
- if (get_result != 0)
- throw0(DB_ERROR("Attempting to get output pubkey by global index, but key does not exist"));
+ if (get_result == MDB_NOTFOUND)
+ throw1(OUTPUT_DNE("Attempting to get output pubkey by global index, but key does not exist"));
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve an output pubkey from the db"));
diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h
index 380954295..6125ef2ef 100644
--- a/src/blockchain_db/lmdb/db_lmdb.h
+++ b/src/blockchain_db/lmdb/db_lmdb.h
@@ -189,6 +189,11 @@ public:
virtual bool has_key_image(const crypto::key_image& img) const;
+ virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
+ virtual bool for_all_blocks(std::function<bool(uint64_t, const crypto::hash&, const cryptonote::block&)>) const;
+ virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>) const;
+ virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const;
+
virtual uint64_t add_block( const block& blk
, const size_t& block_size
, const difficulty_type& cumulative_difficulty
diff --git a/src/blockchain_utilities/CMakeLists.txt b/src/blockchain_utilities/CMakeLists.txt
index 7be9d95f7..f88180686 100644
--- a/src/blockchain_utilities/CMakeLists.txt
+++ b/src/blockchain_utilities/CMakeLists.txt
@@ -67,6 +67,15 @@ bitmonero_private_headers(blockchain_export
${blockchain_export_private_headers})
+set(blockchain_dump_sources
+ blockchain_dump.cpp
+ )
+
+set(blockchain_dump_private_headers)
+
+bitmonero_private_headers(blockchain_dump
+ ${blockchain_dump_private_headers})
+
if (BLOCKCHAIN_DB STREQUAL DB_LMDB)
bitmonero_add_executable(blockchain_converter
@@ -120,3 +129,21 @@ add_dependencies(blockchain_export
set_property(TARGET blockchain_export
PROPERTY
OUTPUT_NAME "blockchain_export")
+
+bitmonero_add_executable(blockchain_dump
+ ${blockchain_dump_sources}
+ ${blockchain_dump_private_headers})
+
+target_link_libraries(blockchain_dump
+ LINK_PRIVATE
+ cryptonote_core
+ blockchain_db
+ p2p
+ ${CMAKE_THREAD_LIBS_INIT})
+
+add_dependencies(blockchain_dump
+ version)
+set_property(TARGET blockchain_dump
+ PROPERTY
+ OUTPUT_NAME "blockchain_dump")
+
diff --git a/src/blockchain_utilities/blockchain_dump.cpp b/src/blockchain_utilities/blockchain_dump.cpp
new file mode 100644
index 000000000..4c3c09c43
--- /dev/null
+++ b/src/blockchain_utilities/blockchain_dump.cpp
@@ -0,0 +1,431 @@
+// Copyright (c) 2014-2015, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "cryptonote_core/cryptonote_basic.h"
+#include "cryptonote_core/blockchain_storage.h"
+#include "cryptonote_core/blockchain.h"
+#include "blockchain_db/blockchain_db.h"
+#include "blockchain_db/lmdb/db_lmdb.h"
+#include "blockchain_db/berkeleydb/db_bdb.h"
+#include "blockchain_utilities.h"
+#include "common/command_line.h"
+#include "version.h"
+
+unsigned int epee::g_test_dbg_lock_sleep = 0;
+
+namespace po = boost::program_options;
+using namespace epee; // log_space
+
+using namespace cryptonote;
+
+struct DumpContext {
+ std::ofstream &f;
+ size_t level;
+ std::vector<std::string> close;
+
+ DumpContext(std::ofstream &f): f(f), level(0) {}
+};
+
+template<typename S> static void start_compound(DumpContext &d, S key, bool array, bool print = false)
+{
+ if (print)
+ LOG_PRINT_L0("Dumping " << key << "...");
+ d.f << std::string(d.level*2, ' ');
+ d.f << "\"" << key << "\": " << (array ? "[" : "{") << " \n";
+ d.close.push_back(array ? "]" : "}");
+ d.level++;
+}
+
+template<typename S> static void start_array(DumpContext &d, S key, bool print = false) { start_compound(d, key, true, print); }
+template<typename S> static void start_struct(DumpContext &d, S key, bool print = false) { start_compound(d, key, false, print); }
+
+static void end_compound(DumpContext &d)
+{
+ d.level--;
+ d.f << std::string(d.level*2, ' ');
+ d.f << d.close.back() << ",\n";
+ d.close.pop_back();
+}
+
+template<typename S, typename T> static void write_pod(DumpContext &d, S key, const T &t)
+{
+ d.f << std::string(d.level*2, ' ');
+ d.f << "\"" << key << "\": ";
+ d.f << t;
+ d.f << ",\n";
+}
+
+template<typename T> static void write_pod(DumpContext &d, const T &t)
+{
+ d.f << std::string(d.level*2, ' ');
+ d.f << t;
+ d.f << ",\n";
+}
+
+int main(int argc, char* argv[])
+{
+ uint32_t log_level = 0;
+ uint64_t block_stop = 0;
+
+ boost::filesystem::path default_data_path {tools::get_default_data_dir()};
+ boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"};
+ boost::filesystem::path output_file_path;
+
+ po::options_description desc_cmd_only("Command line options");
+ po::options_description desc_cmd_sett("Command line options and settings options");
+ const command_line::arg_descriptor<std::string> arg_output_file = {"output-file", "Specify output file", "", true};
+ const command_line::arg_descriptor<uint32_t> arg_log_level = {"log-level", "", log_level};
+ const command_line::arg_descriptor<uint64_t> arg_block_stop = {"block-stop", "Stop at block number", block_stop};
+#if SOURCE_DB != DB_MEMORY
+ const command_line::arg_descriptor<std::string> arg_db_type = {
+ "db-type"
+ , "Specify database type"
+ , DEFAULT_DB_TYPE
+ };
+ const command_line::arg_descriptor<bool> arg_include_db_only_data = {
+ "include-db-only-data"
+ , "Include data that is only in a database version."
+ , false
+ };
+#endif
+ const command_line::arg_descriptor<bool> arg_testnet_on = {
+ "testnet"
+ , "Run on testnet."
+ , false
+ };
+
+
+ command_line::add_arg(desc_cmd_sett, command_line::arg_data_dir, default_data_path.string());
+ command_line::add_arg(desc_cmd_sett, command_line::arg_testnet_data_dir, default_testnet_data_path.string());
+ command_line::add_arg(desc_cmd_sett, arg_output_file);
+#if SOURCE_DB != DB_MEMORY
+ command_line::add_arg(desc_cmd_sett, arg_db_type);
+ command_line::add_arg(desc_cmd_sett, arg_include_db_only_data);
+#endif
+ command_line::add_arg(desc_cmd_sett, arg_testnet_on);
+ command_line::add_arg(desc_cmd_sett, arg_log_level);
+ command_line::add_arg(desc_cmd_sett, arg_block_stop);
+
+ command_line::add_arg(desc_cmd_only, command_line::arg_help);
+
+ po::options_description desc_options("Allowed options");
+ desc_options.add(desc_cmd_only).add(desc_cmd_sett);
+
+ po::variables_map vm;
+ bool r = command_line::handle_error_helper(desc_options, [&]()
+ {
+ po::store(po::parse_command_line(argc, argv, desc_options), vm);
+ po::notify(vm);
+ return true;
+ });
+ if (! r)
+ return 1;
+
+ if (command_line::get_arg(vm, command_line::arg_help))
+ {
+ std::cout << CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL << ENDL << ENDL;
+ std::cout << desc_options << std::endl;
+ return 1;
+ }
+
+ log_level = command_line::get_arg(vm, arg_log_level);
+ block_stop = command_line::get_arg(vm, arg_block_stop);
+
+ log_space::get_set_log_detalisation_level(true, log_level);
+ log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL);
+ LOG_PRINT_L0("Starting...");
+ LOG_PRINT_L0("Setting log level = " << log_level);
+
+ bool opt_testnet = command_line::get_arg(vm, arg_testnet_on);
+ bool opt_include_db_only_data = command_line::get_arg(vm, arg_include_db_only_data);
+
+ std::string m_config_folder;
+
+ auto data_dir_arg = opt_testnet ? command_line::arg_testnet_data_dir : command_line::arg_data_dir;
+ m_config_folder = command_line::get_arg(vm, data_dir_arg);
+
+ if (command_line::has_arg(vm, arg_output_file))
+ output_file_path = boost::filesystem::path(command_line::get_arg(vm, arg_output_file));
+ else
+ output_file_path = boost::filesystem::path(m_config_folder) / "dump" / "blockchain.json";
+ LOG_PRINT_L0("Export output file: " << output_file_path.string());
+
+ const boost::filesystem::path dir_path = output_file_path.parent_path();
+ if (!dir_path.empty())
+ {
+ if (boost::filesystem::exists(dir_path))
+ {
+ if (!boost::filesystem::is_directory(dir_path))
+ {
+ LOG_PRINT_RED_L0("dump directory path is a file: " << dir_path);
+ return false;
+ }
+ }
+ else
+ {
+ if (!boost::filesystem::create_directory(dir_path))
+ {
+ LOG_PRINT_RED_L0("Failed to create directory " << dir_path);
+ return false;
+ }
+ }
+ }
+
+ std::ofstream raw_data_file;
+ raw_data_file.open(output_file_path.string(), std::ios_base::out | std::ios::trunc);
+ if (raw_data_file.fail())
+ return false;
+
+
+ // If we wanted to use the memory pool, we would set up a fake_core.
+
+#if SOURCE_DB == DB_MEMORY
+ // blockchain_storage* core_storage = NULL;
+ // tx_memory_pool m_mempool(*core_storage); // is this fake anyway? just passing in NULL! so m_mempool can't be used anyway, right?
+ // core_storage = new blockchain_storage(&m_mempool);
+
+ blockchain_storage* core_storage = new blockchain_storage(NULL);
+ LOG_PRINT_L0("Initializing source blockchain (in-memory database)");
+ r = core_storage->init(m_config_folder, opt_testnet);
+#else
+ // Use Blockchain instead of lower-level BlockchainDB for two reasons:
+ // 1. Blockchain has the init() method for easy setup
+ // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash()
+ //
+ // cannot match blockchain_storage setup above with just one line,
+ // e.g.
+ // Blockchain* core_storage = new Blockchain(NULL);
+ // because unlike blockchain_storage constructor, which takes a pointer to
+ // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object.
+ LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
+ Blockchain *core_storage = NULL;
+ tx_memory_pool m_mempool(*core_storage);
+ core_storage = new Blockchain(m_mempool);
+
+ BlockchainDB* db;
+ int mdb_flags = 0;
+ std::string db_type = command_line::get_arg(vm, arg_db_type);
+ if (db_type.empty() || db_type == "lmdb")
+ {
+ db = new BlockchainLMDB();
+ mdb_flags |= MDB_RDONLY;
+ }
+ else if (db_type == "berkeley")
+ {
+ db = new BlockchainBDB();
+ // can't open readonly due to the way flags are split in db_bdb.cpp
+ }
+ else
+ {
+ LOG_PRINT_L0("Invalid db type: " << db_type);
+ throw;
+ }
+ boost::filesystem::path folder(m_config_folder);
+ folder /= db->get_db_name();
+ const std::string filename = folder.string();
+
+ LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
+ try
+ {
+ db->open(filename, mdb_flags);
+ }
+ catch (const std::exception& e)
+ {
+ LOG_PRINT_L0("Error opening database: " << e.what());
+ throw;
+ }
+ r = core_storage->init(db, opt_testnet);
+#endif
+
+ CHECK_AND_ASSERT_MES(r, false, "Failed to initialize source blockchain storage");
+ LOG_PRINT_L0("Source blockchain storage initialized OK");
+ LOG_PRINT_L0("Dumping blockchain...");
+
+ DumpContext d(raw_data_file);
+
+ start_struct(d,"blockchain");
+ uint64_t height = core_storage->get_current_blockchain_height();
+ write_pod(d, "height", height);
+goto start;
+ start_array(d,"blockids", true);
+ for (uint64_t h = 0; h < height; ++h)
+ write_pod(d,core_storage->get_block_id_by_height(h));
+ end_compound(d);
+ start_array(d,"txids", true);
+ {
+ std::vector<crypto::hash> txids;
+ core_storage->for_all_transactions([&txids](const crypto::hash &hash, const cryptonote::transaction &tx)->bool{txids.push_back(hash); return true;});
+ std::sort(txids.begin(), txids.end(),
+ [](const crypto::hash &txid0, const crypto::hash &txid1) {return memcmp(txid0.data, txid1.data, sizeof(crypto::hash::data)) < 0;});
+ for (size_t n = 0; n < txids.size(); ++n)
+ write_pod(d, txids[n]);
+ }
+ end_compound(d);
+ start_struct(d,"transactions", true);
+ {
+ for (uint64_t h = 0; h < height; ++h)
+ {
+ start_array(d, boost::lexical_cast<std::string>(h));
+ std::list<cryptonote::block> blocks;
+ std::list<cryptonote::transaction> transactions, miner_tx;
+ core_storage->get_blocks(h, 1, blocks, transactions);
+ if (blocks.size() != 1)
+ throw std::string("Expected 1 block at height ") + boost::lexical_cast<std::string>(h);
+ crypto::hash txid = cryptonote::get_transaction_hash(blocks.front().miner_tx);
+ write_pod(d, string_tools::pod_to_hex(txid).c_str(), obj_to_json_str(blocks.front().miner_tx));
+ std::vector<std::pair<crypto::hash, cryptonote::transaction>> txes;
+ for (std::list<cryptonote::transaction>::iterator i = transactions.begin(); i != transactions.end(); ++i)
+ txes.push_back(std::make_pair(cryptonote::get_transaction_hash(*i), *i));
+ std::sort(txes.begin(), txes.end(),
+ [](const std::pair<crypto::hash, cryptonote::transaction> &tx0, const std::pair<crypto::hash, cryptonote::transaction> &tx1) {return memcmp(tx0.first.data, tx1.first.data, sizeof(crypto::key_image::data)) < 0;});
+ for (std::vector<std::pair<crypto::hash, cryptonote::transaction>>::iterator i = txes.begin(); i != txes.end(); ++i)
+ {
+ write_pod(d, string_tools::pod_to_hex((*i).first).c_str(), obj_to_json_str((*i).second));
+ }
+ end_compound(d);
+ }
+ }
+ start_struct(d,"blocks", true);
+ {
+ std::vector<crypto::hash> blockids;
+ core_storage->for_all_blocks([&](uint64_t height, const crypto::hash &hash, const cryptonote::block &b)->bool{
+ start_struct(d, boost::lexical_cast<std::string>(height));
+ write_pod(d, "hash", string_tools::pod_to_hex(hash));
+ cryptonote::block block = b;
+ write_pod(d, "block", obj_to_json_str(block));
+ end_compound(d);
+ return true;
+ });
+ }
+ end_compound(d);
+ start_array(d,"key_images", true);
+ {
+ std::vector<crypto::key_image> key_images;
+ core_storage->for_all_key_images([&key_images](const crypto::key_image &k_image)->bool{key_images.push_back(k_image); return true;});
+ std::sort(key_images.begin(), key_images.end(),
+ [](const crypto::key_image &k0, const crypto::key_image &k1) {return memcmp(k0.data, k1.data, sizeof(crypto::key_image::data)) < 0;});
+ for (size_t n = 0; n < key_images.size(); ++n)
+ write_pod(d,key_images[n]);
+ }
+ end_compound(d);
+ if (opt_include_db_only_data)
+ {
+ start_struct(d, "block_timestamps", true);
+ for (uint64_t h = 0; h < height; ++h)
+ write_pod(d,boost::lexical_cast<std::string>(h),db->get_block_timestamp(h));
+ end_compound(d);
+ start_struct(d, "block_difficulties", true);
+ for (uint64_t h = 0; h < height; ++h)
+ write_pod(d,boost::lexical_cast<std::string>(h),db->get_block_cumulative_difficulty(h));
+ end_compound(d);
+ start_struct(d, "block_sizes", true);
+ for (uint64_t h = 0; h < height; ++h)
+ write_pod(d,boost::lexical_cast<std::string>(h),db->get_block_size(h));
+ end_compound(d);
+ start_struct(d, "block_coins", true);
+ for (uint64_t h = 0; h < height; ++h)
+ write_pod(d,boost::lexical_cast<std::string>(h),db->get_block_already_generated_coins(h));
+ end_compound(d);
+ start_struct(d, "block_heights", true);
+ for (uint64_t h = 0; h < height; ++h)
+ {
+ const crypto::hash hash = core_storage->get_block_id_by_height(h);
+ write_pod(d,boost::lexical_cast<std::string>(h),db->get_block_height(hash));
+ }
+ end_compound(d);
+ {
+ std::vector<crypto::hash> txids;
+ core_storage->for_all_transactions([&txids](const crypto::hash &hash, const cryptonote::transaction &tx)->bool{txids.push_back(hash); return true;});
+ std::sort(txids.begin(), txids.end(),
+ [](const crypto::hash &txid0, const crypto::hash &txid1) {return memcmp(txid0.data, txid1.data, sizeof(crypto::hash::data)) < 0;});
+ start_struct(d, "transaction_unlock_times", true);
+ for (size_t n = 0; n < txids.size(); ++n)
+ write_pod(d,string_tools::pod_to_hex(txids[n]),db->get_tx_unlock_time(txids[n]));
+ end_compound(d);
+ start_struct(d, "transaction_block_heights", true);
+ for (size_t n = 0; n < txids.size(); ++n)
+ write_pod(d,string_tools::pod_to_hex(txids[n]),db->get_tx_block_height(txids[n]));
+ end_compound(d);
+ }
+ start_array(d, "tx_and_index_from_global", true);
+ for (uint64_t idx = 0; ; ++idx)
+ {
+ try
+ {
+ tx_out_index toi = db->get_output_tx_and_index_from_global(idx);
+ start_struct(d, boost::lexical_cast<std::string>(idx));
+ write_pod(d, "tx_hash", string_tools::pod_to_hex(toi.first));
+ write_pod(d, "tx_index", string_tools::pod_to_hex(toi.second));
+ end_compound(d);
+ }
+ catch (const OUTPUT_DNE &) { break; }
+ }
+ end_compound(d);
+start:
+ start_struct(d, "outputs_amounts", true);
+ for (uint64_t base = 1; base <= (uint64_t)10000000000000000000ul; base *= 10) for (uint64_t digit = 1; digit <= 9; ++digit) {
+ uint64_t amount = digit * base;
+ write_pod(d, boost::lexical_cast<std::string>(amount), db->get_num_outputs(amount));
+ }
+ end_compound(d);
+ start_array(d, "output_keys", true);
+ for (uint64_t idx = 0; ; ++idx)
+ {
+ try
+ {
+ output_data_t od = db->get_output_key(idx);
+ start_struct(d, boost::lexical_cast<std::string>(idx));
+ write_pod(d, "pubkey", string_tools::pod_to_hex(od.pubkey));
+ write_pod(d, "unlock_time", od.unlock_time);
+ write_pod(d, "height", od.height);
+ end_compound(d);
+ }
+ catch (const OUTPUT_DNE &) { break; }
+ }
+ end_compound(d);
+ start_struct(d, "hf_versions", true);
+ for (uint64_t h = 0; h < height; ++h)
+ write_pod(d, boost::lexical_cast<std::string>(h), (unsigned int)db->get_hard_fork_version(h));
+ end_compound(d);
+ start_struct(d, "hf_starting_heights", true);
+ for (unsigned int v = 0; v <= 255; ++v)
+ write_pod(d, boost::lexical_cast<std::string>(v), db->get_hard_fork_starting_height(v));
+ end_compound(d);
+ }
+ end_compound(d);
+
+ CHECK_AND_ASSERT_MES(r, false, "Failed to dump blockchain");
+
+ if (raw_data_file.fail())
+ return false;
+ raw_data_file.flush();
+
+ LOG_PRINT_L0("Blockchain dump OK");
+
+ return 0;
+}
diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h
index b042088bd..303e12b3b 100644
--- a/src/cryptonote_config.h
+++ b/src/cryptonote_config.h
@@ -45,6 +45,7 @@
#define CURRENT_BLOCK_MAJOR_VERSION 1
#define CURRENT_BLOCK_MINOR_VERSION 0
#define CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT 60*60*2
+#define CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE 10
#define BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW 60
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index c38b58841..a594a1b05 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -1033,8 +1033,8 @@ bool Blockchain::create_block_template(block& b, const account_public_address& m
CRITICAL_REGION_BEGIN(m_blockchain_lock);
height = m_db->height();
- b.major_version = m_hardfork->get_ideal_version();
- b.minor_version = 0;
+ b.major_version = m_hardfork->get_current_version();
+ b.minor_version = m_hardfork->get_ideal_version();
b.prev_id = get_tail_id();
b.timestamp = time(NULL);
@@ -1483,6 +1483,17 @@ bool Blockchain::get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUT
for (uint64_t amount : req.amounts)
{
auto num_outs = m_db->get_num_outputs(amount);
+ // ensure we don't include outputs that aren't yet eligible to be used
+ // outpouts are sorted by height
+ while (num_outs > 0)
+ {
+ const tx_out_index toi = m_db->get_output_tx_and_index(amount, num_outs - 1);
+ const uint64_t height = m_db->get_tx_block_height(toi.first);
+ if (height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE <= m_db->height())
+ break;
+ --num_outs;
+ }
+
// create outs_for_amount struct and populate amount field
COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& result_outs = *res.outs.insert(res.outs.end(), COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount());
result_outs.amount = amount;
@@ -3188,3 +3199,23 @@ bool Blockchain::get_hard_fork_voting_info(uint8_t version, uint32_t &window, ui
{
return m_hardfork->get_voting_info(version, window, votes, threshold, voting);
}
+
+bool Blockchain::for_all_key_images(std::function<bool(const crypto::key_image&)> f) const
+{
+ return m_db->for_all_key_images(f);
+}
+
+bool Blockchain::for_all_blocks(std::function<bool(uint64_t, const crypto::hash&, const block&)> f) const
+{
+ return m_db->for_all_blocks(f);
+}
+
+bool Blockchain::for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)> f) const
+{
+ return m_db->for_all_transactions(f);
+}
+
+bool Blockchain::for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const
+{
+ return m_db->for_all_outputs(f);;
+}
diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h
index 3a663a342..21bbfb447 100644
--- a/src/cryptonote_core/blockchain.h
+++ b/src/cryptonote_core/blockchain.h
@@ -165,6 +165,11 @@ namespace cryptonote
uint8_t get_ideal_hard_fork_version() const { return m_hardfork->get_ideal_version(); }
bool get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint8_t &voting) const;
+ bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
+ bool for_all_blocks(std::function<bool(uint64_t, const crypto::hash&, const block&)>) const;
+ bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>) const;
+ bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)>) const;
+
BlockchainDB& get_db()
{
return *m_db;
diff --git a/src/cryptonote_core/blockchain_storage.cpp b/src/cryptonote_core/blockchain_storage.cpp
index 4a4d348ba..6d2b33bda 100644
--- a/src/cryptonote_core/blockchain_storage.cpp
+++ b/src/cryptonote_core/blockchain_storage.cpp
@@ -1042,7 +1042,7 @@ size_t blockchain_storage::find_end_of_allowed_index(const std::vector<std::pair
--i;
transactions_container::const_iterator it = m_transactions.find(amount_outs[i].first);
CHECK_AND_ASSERT_MES(it != m_transactions.end(), 0, "internal error: failed to find transaction from outputs index with tx_id=" << amount_outs[i].first);
- if(it->second.m_keeper_block_height + CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW <= get_current_blockchain_height() )
+ if(it->second.m_keeper_block_height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE <= get_current_blockchain_height() )
return i+1;
} while (i != 0);
return 0;
@@ -1890,3 +1890,41 @@ void blockchain_storage::set_enforce_dns_checkpoints(bool enforce_checkpoints)
{
m_enforce_dns_checkpoints = enforce_checkpoints;
}
+//------------------------------------------------------------------
+bool blockchain_storage::for_all_key_images(std::function<bool(const crypto::key_image&)> f) const
+{
+ for (key_images_container::const_iterator i = m_spent_keys.begin(); i != m_spent_keys.end(); ++i) {
+ if (!f(*i))
+ return false;
+ }
+ return true;
+}
+//------------------------------------------------------------------
+bool blockchain_storage::for_all_blocks(std::function<bool(uint64_t, const block&)> f) const
+{
+ for (blocks_container::const_iterator i = m_blocks.begin(); i != m_blocks.end(); ++i) {
+ if (!f(i->height, i->bl))
+ return false;
+ }
+ return true;
+}
+//------------------------------------------------------------------
+bool blockchain_storage::for_all_transactions(std::function<bool(const transaction&)> f) const
+{
+ for (transactions_container::const_iterator i = m_transactions.begin(); i != m_transactions.end(); ++i) {
+ if (!f(i->second.tx))
+ return false;
+ }
+ return true;
+}
+//------------------------------------------------------------------
+bool blockchain_storage::for_all_outputs(std::function<bool(uint64_t, const crypto::hash&, size_t)> f) const
+{
+ for (outputs_container::const_iterator i = m_outputs.begin(); i != m_outputs.end(); ++i) {
+ for (size_t n = 0; n < i->second.size(); ++n) {
+ if (!f(i->first, i->second[n].first, i->second[n].second))
+ return false;
+ }
+ }
+ return true;
+}
diff --git a/src/cryptonote_core/blockchain_storage.h b/src/cryptonote_core/blockchain_storage.h
index 2a4cfcd49..4a4fc14c2 100644
--- a/src/cryptonote_core/blockchain_storage.h
+++ b/src/cryptonote_core/blockchain_storage.h
@@ -185,6 +185,11 @@ namespace cryptonote
difficulty_type get_block_cumulative_difficulty(uint64_t height) const { return m_blocks[height].cumulative_difficulty; }
uint64_t get_block_coins_generated(uint64_t height) const { return m_blocks[height].already_generated_coins; }
+ bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const;
+ bool for_all_blocks(std::function<bool(uint64_t height, const block&)>) const;
+ bool for_all_transactions(std::function<bool(const transaction&)>) const;
+ bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)>) const;
+
// use for testing only
bool debug_pop_block_from_blockchain() { return pop_block_from_blockchain(); }
diff --git a/src/cryptonote_core/cryptonote_basic.h b/src/cryptonote_core/cryptonote_basic.h
index d3db50068..94887b5a0 100644
--- a/src/cryptonote_core/cryptonote_basic.h
+++ b/src/cryptonote_core/cryptonote_basic.h
@@ -277,8 +277,8 @@ namespace cryptonote
/************************************************************************/
struct block_header
{
- uint8_t major_version; // now used as a voting mechanism, rather than how this particular block is built
- uint8_t minor_version;
+ uint8_t major_version;
+ uint8_t minor_version; // now used as a voting mechanism, rather than how this particular block is built
uint64_t timestamp;
crypto::hash prev_id;
uint32_t nonce;
diff --git a/src/cryptonote_core/hardfork.cpp b/src/cryptonote_core/hardfork.cpp
index 77839678c..6ecaff056 100644
--- a/src/cryptonote_core/hardfork.cpp
+++ b/src/cryptonote_core/hardfork.cpp
@@ -35,6 +35,17 @@
using namespace cryptonote;
+static uint8_t get_block_vote(const cryptonote::block &b)
+{
+ // Pre-hardfork blocks have a minor version hardcoded to 0.
+ // For the purposes of voting, we consider 0 to refer to
+ // version number 1, which is what all blocks from the genesis
+ // block are. It makes things simpler.
+ if (b.minor_version == 0)
+ return 1;
+ return b.minor_version;
+}
+
HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, uint64_t original_version_till_height, time_t forked_time, time_t update_time, uint64_t window_size, int threshold_percent):
db(db),
original_version(original_version),
@@ -87,7 +98,7 @@ bool HardFork::do_check(uint8_t version) const
bool HardFork::check(const cryptonote::block &block) const
{
CRITICAL_REGION_LOCAL(lock);
- return do_check(block.major_version);
+ return do_check(get_block_vote(block));
}
bool HardFork::add(uint8_t block_version, uint64_t height)
@@ -125,7 +136,7 @@ bool HardFork::add(uint8_t block_version, uint64_t height)
bool HardFork::add(const cryptonote::block &block, uint64_t height)
{
- return add(block.major_version, height);
+ return add(get_block_vote(block), height);
}
void HardFork::init()
@@ -168,7 +179,7 @@ uint8_t HardFork::get_block_version(uint64_t height) const
return original_version;
const cryptonote::block &block = db.get_block_from_height(height);
- return block.major_version;
+ return get_block_vote(block);
}
bool HardFork::reorganize_from_block_height(uint64_t height)
@@ -192,7 +203,7 @@ bool HardFork::reorganize_from_block_height(uint64_t height)
}
for (uint64_t h = rescan_height; h <= height; ++h) {
cryptonote::block b = db.get_block_from_height(h);
- const uint8_t v = get_effective_version(b.major_version);
+ const uint8_t v = get_effective_version(get_block_vote(b));
last_versions[v]++;
versions.push_back(v);
}
@@ -236,7 +247,7 @@ bool HardFork::rescan_from_block_height(uint64_t height)
const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size -1) : 0;
for (uint64_t h = rescan_height; h <= height; ++h) {
cryptonote::block b = db.get_block_from_height(h);
- const uint8_t v = get_effective_version(b.major_version);
+ const uint8_t v = get_effective_version(get_block_vote(b));
last_versions[v]++;
versions.push_back(v);
}
diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp
index 7b0f4a66b..51e3231e5 100644
--- a/src/daemon/command_parser_executor.cpp
+++ b/src/daemon/command_parser_executor.cpp
@@ -75,6 +75,13 @@ bool t_command_parser_executor::show_difficulty(const std::vector<std::string>&
return m_executor.show_difficulty();
}
+bool t_command_parser_executor::show_status(const std::vector<std::string>& args)
+{
+ if (!args.empty()) return false;
+
+ return m_executor.show_status();
+}
+
bool t_command_parser_executor::print_connections(const std::vector<std::string>& args)
{
if (!args.empty()) return false;
diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h
index f900c72bd..f00fbd77e 100644
--- a/src/daemon/command_parser_executor.h
+++ b/src/daemon/command_parser_executor.h
@@ -63,6 +63,8 @@ public:
bool show_difficulty(const std::vector<std::string>& args);
+ bool show_status(const std::vector<std::string>& args);
+
bool print_connections(const std::vector<std::string>& args);
bool print_blockchain_info(const std::vector<std::string>& args);
diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp
index 446379558..8714b2569 100644
--- a/src/daemon/command_server.cpp
+++ b/src/daemon/command_server.cpp
@@ -135,6 +135,11 @@ t_command_server::t_command_server(
, "Show difficulty"
);
m_command_lookup.set_handler(
+ "status"
+ , std::bind(&t_command_parser_executor::show_status, &m_parser, p::_1)
+ , "Show status"
+ );
+ m_command_lookup.set_handler(
"stop_daemon"
, std::bind(&t_command_parser_executor::stop_daemon, &m_parser, p::_1)
, "Stop the daemon"
diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp
index 8e0347279..71b2e6f9d 100644
--- a/src/daemon/rpc_command_executor.cpp
+++ b/src/daemon/rpc_command_executor.cpp
@@ -33,6 +33,7 @@
#include "daemon/rpc_command_executor.h"
#include "rpc/core_rpc_server_commands_defs.h"
#include "cryptonote_core/cryptonote_core.h"
+#include "cryptonote_core/hardfork.h"
#include <boost/format.hpp>
#include <ctime>
#include <string>
@@ -246,6 +247,60 @@ bool t_rpc_command_executor::show_difficulty() {
return true;
}
+bool t_rpc_command_executor::show_status() {
+ cryptonote::COMMAND_RPC_GET_INFO::request ireq;
+ cryptonote::COMMAND_RPC_GET_INFO::response ires;
+ cryptonote::COMMAND_RPC_HARD_FORK_INFO::request hfreq;
+ cryptonote::COMMAND_RPC_HARD_FORK_INFO::response hfres;
+ epee::json_rpc::error error_resp;
+
+ std::string fail_message = "Problem fetching info";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->rpc_request(ireq, ires, "/getinfo", fail_message.c_str()))
+ {
+ return true;
+ }
+ if (!m_rpc_client->rpc_request(hfreq, hfres, "/hard_fork_info", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_info(ireq, ires))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ if (!m_rpc_server->on_hard_fork_info(hfreq, hfres, error_resp))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ tools::success_msg_writer() << boost::format("Height: %llu/%llu (%.1f%%) on %s, net hash %s, v%u, %s, %u+%u connections")
+ % (unsigned long long)ires.height
+ % (unsigned long long)(ires.target_height ? ires.target_height : ires.height)
+ % (100.0f * ires.height / (ires.target_height ? ires.target_height < ires.height ? ires.height : ires.target_height : ires.height))
+ % (m_rpc_server->is_testnet() ? "testnet" : "mainnet")
+ % [&ires]()->std::string {
+ float hr = ires.difficulty / 60.0f;
+ if (hr>1e9) return (boost::format("%.2f GH/s") % (hr/1e9)).str();
+ if (hr>1e6) return (boost::format("%.2f MH/s") % (hr/1e6)).str();
+ if (hr>1e3) return (boost::format("%.2f kH/s") % (hr/1e3)).str();
+ return (boost::format("%.0f H/s") % hr).str();
+ }()
+ % (unsigned)hfres.version
+ % (hfres.state == cryptonote::HardFork::Ready ? "up to date" : hfres.state == cryptonote::HardFork::UpdateNeeded ? "update needed" : "out of date, likely forked")
+ % (unsigned)ires.outgoing_connections_count % (unsigned)ires.incoming_connections_count
+ ;
+
+ return true;
+}
+
bool t_rpc_command_executor::print_connections() {
cryptonote::COMMAND_RPC_GET_CONNECTIONS::request req;
cryptonote::COMMAND_RPC_GET_CONNECTIONS::response res;
diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h
index 9ad849434..5df089be2 100644
--- a/src/daemon/rpc_command_executor.h
+++ b/src/daemon/rpc_command_executor.h
@@ -73,6 +73,8 @@ public:
bool show_difficulty();
+ bool show_status();
+
bool print_connections();
bool print_blockchain_info(uint64_t start_block_index, uint64_t end_block_index);
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp
index cca409d9a..92777d16b 100644
--- a/src/wallet/wallet2.cpp
+++ b/src/wallet/wallet2.cpp
@@ -1073,7 +1073,7 @@ bool wallet2::is_transfer_unlocked(const transfer_details& td) const
if(!is_tx_spendtime_unlocked(td.m_tx.unlock_time))
return false;
- if(td.m_block_height + DEFAULT_TX_SPENDABLE_AGE > m_blockchain.size())
+ if(td.m_block_height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE > m_blockchain.size())
return false;
return true;
diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h
index 0bffa7f12..a8c7f9b78 100644
--- a/src/wallet/wallet2.h
+++ b/src/wallet/wallet2.h
@@ -50,7 +50,6 @@
#include "wallet_errors.h"
#include <iostream>
-#define DEFAULT_TX_SPENDABLE_AGE 10
#define WALLET_RCP_CONNECTION_TIMEOUT 200000
namespace tools
diff --git a/tests/unit_tests/hardfork.cpp b/tests/unit_tests/hardfork.cpp
index 25d03b703..210c1959b 100644
--- a/tests/unit_tests/hardfork.cpp
+++ b/tests/unit_tests/hardfork.cpp
@@ -116,11 +116,9 @@ public:
return starting_height[version];
}
virtual void set_hard_fork_version(uint64_t height, uint8_t version) {
- printf("set_hard_fork_version(%lu, %u)\n", (unsigned long)height, version);
if (versions.size() <= height) versions.resize(height+1); versions[height] = version;
}
virtual uint8_t get_hard_fork_version(uint64_t height) const {
- printf("get_hard_fork_version(%lu)\n", (unsigned long)height);
return versions[height];
}
@@ -133,7 +131,7 @@ private:
static cryptonote::block mkblock(uint8_t version)
{
cryptonote::block b;
- b.major_version = version;
+ b.minor_version = version;
return b;
}
@@ -360,7 +358,6 @@ TEST(new_blocks, denied)
ASSERT_TRUE(hf.add(2, 2, 1));
hf.init();
- ASSERT_FALSE(hf.add(mkblock(0), 0));
ASSERT_TRUE(hf.add(mkblock(1), 0));
ASSERT_TRUE(hf.add(mkblock(1), 1));
ASSERT_TRUE(hf.add(mkblock(1), 2));
@@ -386,7 +383,6 @@ TEST(new_version, early)
ASSERT_TRUE(hf.add(2, 4, 1));
hf.init();
- ASSERT_FALSE(hf.add(mkblock(0), 0));
ASSERT_TRUE(hf.add(mkblock(2), 0));
ASSERT_TRUE(hf.add(mkblock(2), 1)); // we have enough votes already
ASSERT_TRUE(hf.add(mkblock(2), 2));
@@ -419,7 +415,6 @@ TEST(reorganize, changed)
#define ADD_TRUE(v, h) ADD(v, h, TRUE)
#define ADD_FALSE(v, h) ADD(v, h, FALSE)
- ADD_FALSE(0, 0);
ADD_TRUE(1, 0);
ADD_TRUE(1, 1);
ADD_TRUE(2, 2);