aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/blockchain_db/CMakeLists.txt16
-rw-r--r--src/blockchain_db/berkeleydb/db_bdb.cpp2296
-rw-r--r--src/blockchain_db/berkeleydb/db_bdb.h452
-rw-r--r--src/blockchain_db/blockchain_db.cpp52
-rw-r--r--src/blockchain_db/blockchain_db.h6
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.cpp6
-rw-r--r--src/blockchain_utilities/blockchain_ancestry.cpp25
-rw-r--r--src/blockchain_utilities/blockchain_blackball.cpp16
-rw-r--r--src/blockchain_utilities/blockchain_depth.cpp25
-rw-r--r--src/blockchain_utilities/blockchain_export.cpp25
-rw-r--r--src/blockchain_utilities/blockchain_import.cpp87
-rw-r--r--src/blockchain_utilities/blockchain_prune.cpp29
-rw-r--r--src/blockchain_utilities/blockchain_prune_known_spent_data.cpp24
-rw-r--r--src/blockchain_utilities/blockchain_stats.cpp24
-rw-r--r--src/blockchain_utilities/blockchain_usage.cpp25
-rw-r--r--src/blockchain_utilities/blocksdat_file.cpp15
-rw-r--r--src/blockchain_utilities/blocksdat_file.h3
-rw-r--r--src/blocks/checkpoints.datbin234980 -> 234948 bytes
-rw-r--r--src/common/perf_timer.cpp2
-rw-r--r--src/crypto/CMakeLists.txt4
-rw-r--r--src/crypto/c_threads.h58
-rw-r--r--src/crypto/hash-ops.h8
-rw-r--r--src/crypto/hash.h5
-rw-r--r--src/crypto/rx-slow-hash.c359
-rw-r--r--src/crypto/slow-hash.c26
-rw-r--r--src/cryptonote_basic/connection_context.h2
-rw-r--r--src/cryptonote_basic/cryptonote_basic.h74
-rw-r--r--src/cryptonote_basic/cryptonote_format_utils.cpp55
-rw-r--r--src/cryptonote_basic/cryptonote_format_utils.h2
-rw-r--r--src/cryptonote_basic/hardfork.cpp4
-rw-r--r--src/cryptonote_basic/hardfork.h11
-rw-r--r--src/cryptonote_basic/miner.cpp20
-rw-r--r--src/cryptonote_basic/miner.h7
-rw-r--r--src/cryptonote_basic/verification_context.h1
-rw-r--r--src/cryptonote_config.h4
-rw-r--r--src/cryptonote_core/CMakeLists.txt1
-rw-r--r--src/cryptonote_core/blockchain.cpp452
-rw-r--r--src/cryptonote_core/blockchain.h56
-rw-r--r--src/cryptonote_core/cryptonote_core.cpp97
-rw-r--r--src/cryptonote_core/cryptonote_core.h24
-rw-r--r--src/cryptonote_core/cryptonote_tx_utils.cpp55
-rw-r--r--src/cryptonote_core/cryptonote_tx_utils.h7
-rw-r--r--src/cryptonote_core/tx_pool.cpp40
-rw-r--r--src/cryptonote_core/tx_pool.h5
-rw-r--r--src/cryptonote_protocol/block_queue.cpp43
-rw-r--r--src/cryptonote_protocol/block_queue.h2
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_defs.h48
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler.h3
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler.inl230
-rw-r--r--src/cryptonote_protocol/levin_notify.cpp17
-rw-r--r--src/cryptonote_protocol/levin_notify.h2
-rw-r--r--src/daemon/main.cpp11
-rw-r--r--src/hardforks/CMakeLists.txt47
-rw-r--r--src/hardforks/hardforks.cpp110
-rw-r--r--src/hardforks/hardforks.h (renamed from src/blockchain_db/db_types.h)28
-rw-r--r--src/p2p/net_node.cpp2
-rw-r--r--src/p2p/net_node.h10
-rw-r--r--src/p2p/net_node.inl33
-rw-r--r--src/p2p/net_node_common.h10
-rw-r--r--src/rpc/bootstrap_daemon.cpp2
-rw-r--r--src/rpc/bootstrap_daemon.h2
-rw-r--r--src/rpc/core_rpc_server.cpp40
-rw-r--r--src/rpc/core_rpc_server.h2
-rw-r--r--src/rpc/core_rpc_server_commands_defs.h10
-rw-r--r--src/rpc/daemon_handler.cpp4
-rw-r--r--src/rpc/rpc_args.cpp6
-rw-r--r--src/rpc/rpc_args.h2
-rw-r--r--src/serialization/json_object.cpp19
-rw-r--r--src/serialization/json_object.h3
-rw-r--r--src/simplewallet/simplewallet.cpp145
-rw-r--r--src/simplewallet/simplewallet.h4
-rw-r--r--src/wallet/CMakeLists.txt2
-rw-r--r--src/wallet/api/wallet.cpp4
-rw-r--r--src/wallet/message_store.cpp2
-rw-r--r--src/wallet/wallet2.cpp109
-rw-r--r--src/wallet/wallet2.h12
-rw-r--r--src/wallet/wallet_rpc_server.cpp143
-rw-r--r--src/wallet/wallet_rpc_server.h2
-rw-r--r--src/wallet/wallet_rpc_server_commands_defs.h34
80 files changed, 1972 insertions, 3677 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8a21763c8..9bab56200 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -112,6 +112,7 @@ add_subdirectory(cryptonote_core)
add_subdirectory(lmdb)
add_subdirectory(multisig)
add_subdirectory(net)
+add_subdirectory(hardforks)
if(NOT IOS)
add_subdirectory(blockchain_db)
endif()
diff --git a/src/blockchain_db/CMakeLists.txt b/src/blockchain_db/CMakeLists.txt
index db161fa5e..0a21e4920 100644
--- a/src/blockchain_db/CMakeLists.txt
+++ b/src/blockchain_db/CMakeLists.txt
@@ -31,14 +31,6 @@ set(blockchain_db_sources
lmdb/db_lmdb.cpp
)
-if (BERKELEY_DB)
- set(blockchain_db_sources
- ${blockchain_db_sources}
- berkeleydb/db_bdb.cpp
- )
-endif()
-
-
set(blockchain_db_headers)
set(blockchain_db_private_headers
@@ -46,13 +38,6 @@ set(blockchain_db_private_headers
lmdb/db_lmdb.h
)
-if (BERKELEY_DB)
- set(blockchain_db_private_headers
- ${blockchain_db_private_headers}
- berkeleydb/db_bdb.h
- )
-endif()
-
monero_private_headers(blockchain_db
${crypto_private_headers})
monero_add_library(blockchain_db
@@ -65,7 +50,6 @@ target_link_libraries(blockchain_db
cncrypto
ringct
${LMDB_LIBRARY}
- ${BDB_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
PRIVATE
diff --git a/src/blockchain_db/berkeleydb/db_bdb.cpp b/src/blockchain_db/berkeleydb/db_bdb.cpp
deleted file mode 100644
index d138a1e7e..000000000
--- a/src/blockchain_db/berkeleydb/db_bdb.cpp
+++ /dev/null
@@ -1,2296 +0,0 @@
-// Copyright (c) 2014-2019, 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 "db_bdb.h"
-
-#include <boost/filesystem.hpp>
-#include <memory> // std::unique_ptr
-#include <cstring> // memcpy
-
-#include "cryptonote_basic/cryptonote_format_utils.h"
-#include "crypto/crypto.h"
-#include "profile_tools.h"
-
-using epee::string_tools::pod_to_hex;
-#define DB_DEFAULT_TX (m_write_txn != nullptr ? *m_write_txn : (DbTxn*) nullptr)
-
-// Increase when the DB changes in a non backward compatible way, and there
-// is no automatic conversion, so that a full resync is needed.
-#define VERSION 0
-
-namespace
-{
-
-template <typename T>
-inline void throw0(const T &e)
-{
- LOG_PRINT_L0(e.what());
- throw e;
-}
-
-template <typename T>
-inline void throw1(const T &e)
-{
- LOG_PRINT_L1(e.what());
- throw e;
-}
-
-// cursor needs to be closed when it goes out of scope,
-// this helps if the function using it throws
-struct bdb_cur
-{
- bdb_cur(DbTxn* txn, Db* dbi)
- {
- if (dbi->cursor(txn, &m_cur, 0))
- throw0(cryptonote::DB_ERROR("Error opening db cursor"));
- done = false;
- }
-
- ~bdb_cur()
- {
- close();
- }
-
- operator Dbc*()
- {
- return m_cur;
- }
- operator Dbc**()
- {
- return &m_cur;
- }
- Dbc* operator->()
- {
- return m_cur;
- }
-
- void close()
- {
- if (!done)
- {
- m_cur->close();
- done = true;
- }
- }
-
-private:
- Dbc* m_cur;
- bool done;
-};
-
-const char* const BDB_BLOCKS = "blocks";
-const char* const BDB_BLOCK_TIMESTAMPS = "block_timestamps";
-const char* const BDB_BLOCK_HEIGHTS = "block_heights";
-const char* const BDB_BLOCK_HASHES = "block_hashes";
-const char* const BDB_BLOCK_SIZES = "block_sizes";
-const char* const BDB_BLOCK_DIFFS = "block_diffs";
-const char* const BDB_BLOCK_COINS = "block_coins";
-
-const char* const BDB_TXS = "txs";
-const char* const BDB_TX_UNLOCKS = "tx_unlocks";
-const char* const BDB_TX_HEIGHTS = "tx_heights";
-const char* const BDB_TX_OUTPUTS = "tx_outputs";
-
-const char* const BDB_OUTPUT_TXS = "output_txs";
-const char* const BDB_OUTPUT_INDICES = "output_indices";
-const char* const BDB_OUTPUT_AMOUNTS = "output_amounts";
-const char* const BDB_OUTPUT_KEYS = "output_keys";
-
-const char* const BDB_SPENT_KEYS = "spent_keys";
-
-const char* const BDB_HF_STARTING_HEIGHTS = "hf_starting_heights";
-const char* const BDB_HF_VERSIONS = "hf_versions";
-
-const char* const BDB_PROPERTIES = "properties";
-
-const unsigned int MB = 1024 * 1024;
-// ND: FIXME: db keeps running out of locks when doing full syncs. Possible bug??? Set it to 5K for now.
-const unsigned int DB_MAX_LOCKS = 5000;
-const unsigned int DB_BUFFER_LENGTH = 32 * MB;
-// 256MB cache adjust as necessary using DB_CONFIG
-const unsigned int DB_DEF_CACHESIZE = 256 * MB;
-
-#if defined(BDB_BULK_CAN_THREAD)
-const unsigned int DB_BUFFER_COUNT = tools::get_max_concurrency();
-#else
-const unsigned int DB_BUFFER_COUNT = 1;
-#endif
-
-template<typename T>
-struct Dbt_copy: public Dbt
-{
- Dbt_copy(const T &t) :
- t_copy(t)
- {
- init();
- }
-
- Dbt_copy()
- {
- init();
- }
-
- void init()
- {
- set_data(&t_copy);
- set_size(sizeof(T));
- set_ulen(sizeof(T));
- set_flags(DB_DBT_USERMEM);
- }
-
- operator T()
- {
- return t_copy;
- }
-private:
- T t_copy;
-};
-
-template<>
-struct Dbt_copy<cryptonote::blobdata>: public Dbt
-{
- Dbt_copy(const cryptonote::blobdata &bd) :
- m_data(new char[bd.size()])
- {
- memcpy(m_data.get(), bd.data(), bd.size());
- set_data(m_data.get());
- set_size(bd.size());
- set_ulen(bd.size());
- set_flags(DB_DBT_USERMEM);
- }
-private:
- std::unique_ptr<char[]> m_data;
-};
-
-template<>
-struct Dbt_copy<const char*>: public Dbt
-{
- Dbt_copy(const char *s) :
- m_data(strdup(s))
- {
- size_t len = strlen(s) + 1; // include the NUL, makes it easier for compare
- set_data(m_data.get());
- set_size(len);
- set_ulen(len);
- set_flags(DB_DBT_USERMEM);
- }
-private:
- std::unique_ptr<char[]> m_data;
-};
-
-struct Dbt_safe : public Dbt
-{
- Dbt_safe()
- {
- set_data(NULL);
- set_flags(DB_DBT_MALLOC);
- }
- ~Dbt_safe()
- {
- void* buf = get_data();
- if (buf != NULL)
- {
- free(buf);
- }
- }
-};
-
-} // anonymous namespace
-
-namespace cryptonote
-{
-
-void BlockchainBDB::add_block(const block& blk, size_t block_weight, const difficulty_type& cumulative_difficulty, const uint64_t& coins_generated, const crypto::hash& blk_hash)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::hash> val_h(blk_hash);
- if (m_block_heights->exists(DB_DEFAULT_TX, &val_h, 0) == 0)
- throw1(BLOCK_EXISTS("Attempting to add block that's already in the db"));
-
- if (m_height > 0)
- {
- Dbt_copy<crypto::hash> parent_key(blk.prev_id);
- Dbt_copy<uint32_t> parent_h;
- if (m_block_heights->get(DB_DEFAULT_TX, &parent_key, &parent_h, 0))
- {
- LOG_PRINT_L3("m_height: " << m_height);
- LOG_PRINT_L3("parent_key: " << blk.prev_id);
- throw0(DB_ERROR("Failed to get top block hash to check for new block's parent"));
- }
- uint32_t parent_height = parent_h;
- if (parent_height != m_height)
- throw0(BLOCK_PARENT_DNE("Top block is not new block's parent"));
- }
-
- Dbt_copy<uint32_t> key(m_height + 1);
-
- Dbt_copy<blobdata> blob(block_to_blob(blk));
- auto res = m_blocks->put(DB_DEFAULT_TX, &key, &blob, 0);
- if (res)
- throw0(DB_ERROR("Failed to add block blob to db transaction."));
-
- Dbt_copy<size_t> sz(block_weight);
- if (m_block_sizes->put(DB_DEFAULT_TX, &key, &sz, 0))
- throw0(DB_ERROR("Failed to add block size to db transaction."));
-
- Dbt_copy<uint64_t> ts(blk.timestamp);
- if (m_block_timestamps->put(DB_DEFAULT_TX, &key, &ts, 0))
- throw0(DB_ERROR("Failed to add block timestamp to db transaction."));
-
- Dbt_copy<difficulty_type> diff(cumulative_difficulty);
- if (m_block_diffs->put(DB_DEFAULT_TX, &key, &diff, 0))
- throw0(DB_ERROR("Failed to add block cumulative difficulty to db transaction."));
-
- Dbt_copy<uint64_t> coinsgen(coins_generated);
- if (m_block_coins->put(DB_DEFAULT_TX, &key, &coinsgen, 0))
- throw0(DB_ERROR("Failed to add block total generated coins to db transaction."));
-
- if (m_block_heights->put(DB_DEFAULT_TX, &val_h, &key, 0))
- throw0(DB_ERROR("Failed to add block height by hash to db transaction."));
-
- if (m_block_hashes->put(DB_DEFAULT_TX, &key, &val_h, 0))
- throw0(DB_ERROR("Failed to add block hash to db transaction."));
-}
-
-void BlockchainBDB::remove_block()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- if (m_height == 0)
- throw0(BLOCK_DNE ("Attempting to remove block from an empty blockchain"));
-
- Dbt_copy<uint32_t> k(m_height);
- Dbt_copy<crypto::hash> h;
- if (m_block_hashes->get(DB_DEFAULT_TX, &k, &h, 0))
- throw1(BLOCK_DNE("Attempting to remove block that's not in the db"));
-
- if (m_blocks->del(DB_DEFAULT_TX, &k, 0))
- throw1(DB_ERROR("Failed to add removal of block to db transaction"));
-
- if (m_block_sizes->del(DB_DEFAULT_TX, &k, 0))
- throw1(DB_ERROR("Failed to add removal of block size to db transaction"));
-
- if (m_block_diffs->del(DB_DEFAULT_TX, &k, 0))
- throw1(DB_ERROR("Failed to add removal of block cumulative difficulty to db transaction"));
-
- if (m_block_coins->del(DB_DEFAULT_TX, &k, 0))
- throw1(DB_ERROR("Failed to add removal of block total generated coins to db transaction"));
-
- if (m_block_timestamps->del(DB_DEFAULT_TX, &k, 0))
- throw1(DB_ERROR("Failed to add removal of block timestamp to db transaction"));
-
- if (m_block_heights->del(DB_DEFAULT_TX, &h, 0))
- throw1(DB_ERROR("Failed to add removal of block height by hash to db transaction"));
-
- if (m_block_hashes->del(DB_DEFAULT_TX, &k, 0))
- throw1(DB_ERROR("Failed to add removal of block hash to db transaction"));
-}
-
-void BlockchainBDB::add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::hash> val_h(tx_hash);
-
- if (m_txs->exists(DB_DEFAULT_TX, &val_h, 0) == 0)
- throw1(TX_EXISTS("Attempting to add transaction that's already in the db"));
-
- Dbt_copy<blobdata> blob(tx_to_blob(tx));
- if (m_txs->put(DB_DEFAULT_TX, &val_h, &blob, 0))
- throw0(DB_ERROR("Failed to add tx blob to db transaction"));
-
- Dbt_copy<uint64_t> height(m_height + 1);
- if (m_tx_heights->put(DB_DEFAULT_TX, &val_h, &height, 0))
- throw0(DB_ERROR("Failed to add tx block height to db transaction"));
-
- Dbt_copy<uint64_t> unlock_time(tx.unlock_time);
- if (m_tx_unlocks->put(DB_DEFAULT_TX, &val_h, &unlock_time, 0))
- throw0(DB_ERROR("Failed to add tx unlock time to db transaction"));
-}
-
-void BlockchainBDB::remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::hash> val_h(tx_hash);
- if (m_txs->exists(DB_DEFAULT_TX, &val_h, 0))
- throw1(TX_DNE("Attempting to remove transaction that isn't in the db"));
-
- if (m_txs->del(DB_DEFAULT_TX, &val_h, 0))
- throw1(DB_ERROR("Failed to add removal of tx to db transaction"));
- if (m_tx_unlocks->del(DB_DEFAULT_TX, &val_h, 0))
- throw1(DB_ERROR("Failed to add removal of tx unlock time to db transaction"));
- if (m_tx_heights->del(DB_DEFAULT_TX, &val_h, 0))
- throw1(DB_ERROR("Failed to add removal of tx block height to db transaction"));
-
- remove_tx_outputs(tx_hash, tx);
-
- auto result = m_tx_outputs->del(DB_DEFAULT_TX, &val_h, 0);
- if (result == DB_NOTFOUND)
- LOG_PRINT_L1("tx has no outputs to remove: " << tx_hash);
- else if (result)
- throw1(DB_ERROR("Failed to add removal of tx outputs to db transaction"));
-}
-
-void BlockchainBDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_output, const uint64_t& local_index, const uint64_t unlock_time, const rct::key *commitment)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> k(m_num_outputs + 1);
- Dbt_copy<crypto::hash> v(tx_hash);
-
- if (m_output_txs->put(DB_DEFAULT_TX, &k, &v, 0))
- throw0(DB_ERROR("Failed to add output tx hash to db transaction"));
- if (m_tx_outputs->put(DB_DEFAULT_TX, &v, &k, 0))
- throw0(DB_ERROR("Failed to add tx output index to db transaction"));
-
- Dbt_copy<uint64_t> val_local_index(local_index);
- if (m_output_indices->put(DB_DEFAULT_TX, &k, &val_local_index, 0))
- throw0(DB_ERROR("Failed to add tx output index to db transaction"));
-
- Dbt_copy<uint64_t> val_amount(tx_output.amount);
- if (m_output_amounts->put(DB_DEFAULT_TX, &val_amount, &k, 0))
- throw0(DB_ERROR("Failed to add output amount to db transaction."));
-
- if (tx_output.target.type() == typeid(txout_to_key))
- {
- output_data_t od;
- od.pubkey = boost::get < txout_to_key > (tx_output.target).key;
- od.unlock_time = unlock_time;
- od.height = m_height;
-
- Dbt_copy<output_data_t> data(od);
- if (m_output_keys->put(DB_DEFAULT_TX, &k, &data, 0))
- throw0(DB_ERROR("Failed to add output pubkey to db transaction"));
- }
- else
- {
- throw0(DB_ERROR("Wrong output type: expected txout_to_key"));
- }
-
- m_num_outputs++;
-}
-
-void BlockchainBDB::remove_tx_outputs(const crypto::hash& tx_hash, const transaction& tx)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
-
- bdb_cur cur(DB_DEFAULT_TX, m_tx_outputs);
-
- Dbt_copy<crypto::hash> k(tx_hash);
- Dbt_copy<uint32_t> v;
-
- auto result = cur->get(&k, &v, DB_SET);
- if (result == DB_NOTFOUND)
- {
- LOG_PRINT_L2("tx has no outputs, so no global output indices");
- }
- else if (result)
- {
- throw0(DB_ERROR("DB error attempting to get an output"));
- }
- else
- {
- result = cur->get(&k, &v, DB_NEXT_NODUP);
- if (result != 0 && result != DB_NOTFOUND)
- throw0(DB_ERROR("DB error attempting to get next non-duplicate tx hash"));
-
- if (result == 0)
- result = cur->get(&k, &v, DB_PREV);
- else if (result == DB_NOTFOUND)
- result = cur->get(&k, &v, DB_LAST);
-
- db_recno_t num_elems = 0;
- cur->count(&num_elems, 0);
-
- // remove in order: from newest to oldest
- for (uint64_t i = num_elems; i > 0; --i)
- {
- const tx_out tx_output = tx.vout[i-1];
- remove_output(v, tx_output.amount);
- if (i > 1)
- {
- cur->get(&k, &v, DB_PREV_DUP);
- }
- }
- }
-
- cur.close();
-}
-
-// TODO: probably remove this function
-void BlockchainBDB::remove_output(const tx_out& tx_output)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__ << " (unused version - does nothing)");
- return;
-}
-
-void BlockchainBDB::remove_output(const uint64_t& out_index, const uint64_t amount)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> k(out_index);
-
- auto result = m_output_indices->del(DB_DEFAULT_TX, &k, 0);
- if (result == DB_NOTFOUND)
- {
- 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 = m_output_txs->del(DB_DEFAULT_TX, &k, 0);
- // if (result != 0 && result != DB_NOTFOUND)
- // throw1(DB_ERROR("Error adding removal of output tx hash to db transaction"));
- if (result == DB_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 = m_output_keys->del(DB_DEFAULT_TX, &k, 0);
- if (result == DB_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 BlockchainBDB::remove_amount_output_index(const uint64_t amount, const uint64_t global_output_index)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- bdb_cur cur(DB_DEFAULT_TX, m_output_amounts);
-
- Dbt_copy<uint64_t> k(amount);
- Dbt_copy<uint32_t> v;
-
- auto result = cur->get(&k, &v, DB_SET);
- if (result == DB_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"));
-
- db_recno_t num_elems = 0;
- cur->count(&num_elems, 0);
-
- // workaround for Berkeley DB to start at end of k's duplicate list:
- // if next key exists:
- // - move cursor to start of next key's duplicate list, then move back one
- // duplicate element to reach the end of the original key's duplicate
- // list.
- //
- // else if the next key doesn't exist:
- // - that means we're already on the last key.
- // - move cursor to last element in the db, which is the last element of
- // the desired key's duplicate list.
-
- result = cur->get(&k, &v, DB_NEXT_NODUP);
- if (result != 0 && result != DB_NOTFOUND)
- throw0(DB_ERROR("DB error attempting to get next non-duplicate output amount"));
-
- if (result == 0)
- result = cur->get(&k, &v, DB_PREV);
- else if (result == DB_NOTFOUND)
- result = cur->get(&k, &v, DB_LAST);
-
- bool found_index = false;
- uint64_t amount_output_index = 0;
- uint64_t goi = 0;
-
- for (uint64_t i = num_elems; i > 0; --i)
- {
- goi = v;
- if (goi == global_output_index)
- {
- amount_output_index = i-1;
- found_index = true;
- break;
- }
- if (i > 1)
- cur->get(&k, &v, DB_PREV_DUP);
- }
-
- if (found_index)
- {
- // found the amount output index
- // now delete it
- result = cur->del(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
- throw1(OUTPUT_DNE("Failed to find amount output index"));
- }
- cur.close();
-}
-
-void BlockchainBDB::add_spent_key(const crypto::key_image& k_image)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::key_image> val_key(k_image);
- if (m_spent_keys->exists(DB_DEFAULT_TX, &val_key, 0) == 0)
- throw1(KEY_IMAGE_EXISTS("Attempting to add spent key image that's already in the db"));
-
- Dbt_copy<char> val('\0');
- if (m_spent_keys->put(DB_DEFAULT_TX, &val_key, &val, 0))
- throw1(DB_ERROR("Error adding spent key image to db transaction."));
-}
-
-void BlockchainBDB::remove_spent_key(const crypto::key_image& k_image)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::key_image> k(k_image);
- auto result = m_spent_keys->del(DB_DEFAULT_TX, &k, 0);
- if (result != 0 && result != DB_NOTFOUND)
- 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<uint32_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, bool pruned) 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;
-}
-
-uint64_t BlockchainBDB::get_output_global_index(const uint64_t& amount, const uint64_t& index)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- std::vector < uint64_t > offsets;
- std::vector < uint64_t > global_indices;
- offsets.push_back(index);
- get_output_global_indices(amount, offsets, global_indices);
- if (!global_indices.size())
- throw1(OUTPUT_DNE("Attempting to get an output index by amount and amount index, but amount not found"));
-
- return global_indices[0];
-}
-
-void BlockchainBDB::check_open() const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- if (!m_open)
- throw0(DB_ERROR("DB operation attempted on a not-open DB instance"));
-}
-
-BlockchainBDB::~BlockchainBDB()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
-
- if (m_open)
- {
- close();
- }
-}
-
-BlockchainBDB::BlockchainBDB(bool batch_transactions) :
- BlockchainDB(),
- m_buffer(DB_BUFFER_COUNT, DB_BUFFER_LENGTH)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- // initialize folder to something "safe" just in case
- // someone accidentally misuses this class...
- m_folder = "thishsouldnotexistbecauseitisgibberish";
- m_run_checkpoint = 0;
- 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)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
-
- if (m_open)
- throw0(DB_OPEN_FAILURE("Attempted to open db, but it's already open"));
-
- boost::filesystem::path direc(filename);
- if (boost::filesystem::exists(direc))
- {
- if (!boost::filesystem::is_directory(direc))
- throw0(DB_OPEN_FAILURE("DB needs a directory path, but a file was passed"));
- }
- else
- {
- if (!boost::filesystem::create_directories(direc))
- throw0(DB_OPEN_FAILURE(std::string("Failed to create directory ").append(filename).c_str()));
- }
-
- m_folder = filename;
-
- try
- {
-
- //Create BerkeleyDB environment
- m_env = new DbEnv(0); // no flags needed for DbEnv
-
- uint32_t db_env_open_flags = DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER | DB_THREAD;
-
- // Set some default values for these parameters.
- // They can be overridden using the DB_CONFIG file.
- m_env->set_cachesize(0, DB_DEF_CACHESIZE, 1);
- m_env->set_lk_max_locks(DB_MAX_LOCKS);
- m_env->set_lk_max_lockers(DB_MAX_LOCKS);
- m_env->set_lk_max_objects(DB_MAX_LOCKS);
-
- #ifndef __OpenBSD__ //OpenBSD's DB package is too old to support this feature
- if(m_auto_remove_logs)
- m_env->log_set_config(DB_LOG_AUTO_REMOVE, 1);
- #endif
-
- // last parameter left 0, files will be created with default rw access
- m_env->open(filename.c_str(), db_env_open_flags, 0);
- m_env->set_flags(db_flags, 1);
-
- // begin transaction to init dbs
- bdb_txn_safe txn;
- m_env->txn_begin(NULL, txn, 0);
-
- // create Dbs in the environment
- m_blocks = new Db(m_env, 0);
- m_block_heights = new Db(m_env, 0);
- m_block_hashes = new Db(m_env, 0);
- m_block_timestamps = new Db(m_env, 0);
- m_block_sizes = new Db(m_env, 0);
- m_block_diffs = new Db(m_env, 0);
- m_block_coins = new Db(m_env, 0);
-
- m_txs = new Db(m_env, 0);
- m_tx_unlocks = new Db(m_env, 0);
- m_tx_heights = new Db(m_env, 0);
- m_tx_outputs = new Db(m_env, 0);
-
- m_output_txs = new Db(m_env, 0);
- m_output_indices = new Db(m_env, 0);
- m_output_amounts = new Db(m_env, 0);
- m_output_keys = new Db(m_env, 0);
-
- m_spent_keys = new Db(m_env, 0);
-
- m_hf_starting_heights = new Db(m_env, 0);
- m_hf_versions = new Db(m_env, 0);
-
- m_properties = new Db(m_env, 0);
-
- // Tell DB about Dbs that need duplicate support
- // Note: no need to tell about sorting,
- // as the default is insertion order, which we want
- m_tx_outputs->set_flags(DB_DUP);
- m_output_amounts->set_flags(DB_DUP);
-
- // Tell DB about fixed-size values.
- m_block_hashes->set_re_len(sizeof(crypto::hash));
- m_block_timestamps->set_re_len(sizeof(uint64_t));
- m_block_sizes->set_re_len(sizeof(size_t)); // should really store block size as uint64_t...
- m_block_diffs->set_re_len(sizeof(difficulty_type));
- m_block_coins->set_re_len(sizeof(uint64_t));
-
- m_output_txs->set_re_len(sizeof(crypto::hash));
- m_output_indices->set_re_len(sizeof(uint64_t));
- m_output_keys->set_re_len(sizeof(output_data_t));
-
- m_hf_starting_heights->set_re_len(sizeof(uint64_t));
- m_hf_versions->set_re_len(sizeof(uint8_t));
-
- //TODO: Find out if we need to do Db::set_flags(DB_RENUMBER)
- // for the RECNO databases. We shouldn't as we're only
- // inserting/removing from the end, but we'll see.
-
- // open Dbs in the environment
- // m_tx_outputs and m_output_amounts must be DB_HASH or DB_BTREE
- // because they need duplicate entry support. The rest are DB_RECNO,
- // as it seems that will be the most performant choice.
- m_blocks->open(txn, BDB_BLOCKS, NULL, DB_RECNO, DB_CREATE, 0);
-
- m_block_timestamps->open(txn, BDB_BLOCK_TIMESTAMPS, NULL, DB_RECNO, DB_CREATE, 0);
- m_block_heights->open(txn, BDB_BLOCK_HEIGHTS, NULL, DB_HASH, DB_CREATE, 0);
- m_block_hashes->open(txn, BDB_BLOCK_HASHES, NULL, DB_RECNO, DB_CREATE, 0);
- m_block_sizes->open(txn, BDB_BLOCK_SIZES, NULL, DB_RECNO, DB_CREATE, 0);
- m_block_diffs->open(txn, BDB_BLOCK_DIFFS, NULL, DB_RECNO, DB_CREATE, 0);
- m_block_coins->open(txn, BDB_BLOCK_COINS, NULL, DB_RECNO, DB_CREATE, 0);
-
- m_txs->open(txn, BDB_TXS, NULL, DB_HASH, DB_CREATE, 0);
- m_tx_unlocks->open(txn, BDB_TX_UNLOCKS, NULL, DB_HASH, DB_CREATE, 0);
- m_tx_heights->open(txn, BDB_TX_HEIGHTS, NULL, DB_HASH, DB_CREATE, 0);
- m_tx_outputs->open(txn, BDB_TX_OUTPUTS, NULL, DB_HASH, DB_CREATE, 0);
-
- m_output_txs->open(txn, BDB_OUTPUT_TXS, NULL, DB_RECNO, DB_CREATE, 0);
- m_output_indices->open(txn, BDB_OUTPUT_INDICES, NULL, DB_RECNO, DB_CREATE, 0);
- m_output_amounts->open(txn, BDB_OUTPUT_AMOUNTS, NULL, DB_HASH, DB_CREATE, 0);
- m_output_keys->open(txn, BDB_OUTPUT_KEYS, NULL, DB_RECNO, DB_CREATE, 0);
-
- m_spent_keys->open(txn, BDB_SPENT_KEYS, NULL, DB_HASH, DB_CREATE, 0);
-
- m_hf_starting_heights->open(txn, BDB_HF_STARTING_HEIGHTS, NULL, DB_RECNO, DB_CREATE, 0);
- m_hf_versions->open(txn, BDB_HF_VERSIONS, NULL, DB_RECNO, DB_CREATE, 0);
-
- m_properties->open(txn, BDB_PROPERTIES, NULL, DB_HASH, DB_CREATE, 0);
-
- txn.commit();
-
- DB_BTREE_STAT* stats;
-
- // DB_FAST_STAT can apparently cause an incorrect number of records
- // to be returned. The flag should be set to 0 instead if this proves
- // to be the case.
-
- // ND: The bug above can occur when a block is popped and the application
- // exits without pushing a new block to the db. Set txn to NULL and DB_FAST_STAT
- // to zero (0) for reliability.
- m_blocks->stat(NULL, &stats, 0);
- m_height = stats->bt_nkeys;
- free(stats);
-
- // see above comment about DB_FAST_STAT
- m_output_indices->stat(NULL, &stats, 0);
- m_num_outputs = stats->bt_nkeys;
- free(stats);
-
- // checks for compatibility
- bool compatible = true;
-
- Dbt_copy<const char*> key("version");
- Dbt_copy<uint32_t> result;
- auto get_result = m_properties->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == 0)
- {
- if (result > VERSION)
- {
- LOG_PRINT_RED_L0("Existing BerkeleyDB database was made by a later version. We don't know how it will change yet.");
- compatible = false;
- }
-#if VERSION > 0
- else if (result < VERSION)
- {
- compatible = false;
- }
-#endif
- }
- else
- {
- // if not found, but we're on version 0, it's fine. If the DB's empty, it's fine too.
- if (VERSION > 0 && m_height > 0)
- compatible = false;
- }
-
- if (!compatible)
- {
- m_open = false;
- LOG_PRINT_RED_L0("Existing BerkeleyDB database is incompatible with this version.");
- LOG_PRINT_RED_L0("Please delete the existing database and resync.");
- return;
- }
-
- if (1 /* this can't be set readonly atm */)
- {
- // only write version on an empty DB
- if (m_height == 0)
- {
- Dbt_copy<const char*> k("version");
- Dbt_copy<uint32_t> v(VERSION);
- auto put_result = m_properties->put(DB_DEFAULT_TX, &k, &v, 0);
- if (put_result != 0)
- {
- m_open = false;
- LOG_PRINT_RED_L0("Failed to write version to database.");
- return;
- }
- }
- }
-
- // run checkpoint thread
- m_run_checkpoint = true;
- m_checkpoint_thread.reset(new boost::thread(&BlockchainBDB::checkpoint_worker, this));
- }
- catch (const std::exception& e)
- {
- throw0(DB_OPEN_FAILURE(e.what()));
- }
-
- m_open = true;
-}
-
-void BlockchainBDB::close()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- this->sync();
-
- m_run_checkpoint = false;
- m_checkpoint_thread->join();
- m_checkpoint_thread.reset();
-
- // FIXME: not yet thread safe!!! Use with care.
- m_open = false;
-
- // DB_FORCESYNC is only available on newer version of libdb.
- // The libdb doc says using the DB_FORCESYNC flag to DB_ENV->close
- // is "similar to calling the DB->close(0) method to close each
- // database handle". So this is what we do here as a fallback.
-#ifdef DB_FORCESYNC
- m_env->close(DB_FORCESYNC);
-#else
- m_blocks->close(0);
- m_block_heights->close(0);
- m_block_hashes->close(0);
- m_block_timestamps->close(0);
- m_block_sizes->close(0);
- m_block_diffs->close(0);
- m_block_coins->close(0);
-
- m_txs->close(0);
- m_tx_unlocks->close(0);
- m_tx_heights->close(0);
- m_tx_outputs->close(0);
-
- m_output_txs->close(0);
- m_output_indices->close(0);
- m_output_amounts->close(0);
- m_output_keys->close(0);
-
- m_spent_keys->close(0);
-
- m_hf_starting_heights->close(0);
- m_hf_versions->close(0);
-
- m_properties->close(0);
-
- m_env->close(0);
-#endif
-}
-
-void BlockchainBDB::sync()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- try
- {
- m_blocks->sync(0);
- m_block_heights->sync(0);
- m_block_hashes->sync(0);
- m_block_timestamps->sync(0);
- m_block_sizes->sync(0);
- m_block_diffs->sync(0);
- m_block_coins->sync(0);
-
- m_txs->sync(0);
- m_tx_unlocks->sync(0);
- m_tx_heights->sync(0);
- m_tx_outputs->sync(0);
-
- m_output_txs->sync(0);
- m_output_indices->sync(0);
- m_output_amounts->sync(0);
- m_output_keys->sync(0);
-
- m_spent_keys->sync(0);
-
- if (m_hf_starting_heights != nullptr)
- m_hf_starting_heights->sync(0);
- if (m_hf_versions != nullptr)
- m_hf_versions->sync(0);
-
- m_properties->sync(0);
- }
- catch (const std::exception& e)
- {
- throw0(DB_ERROR(std::string("Failed to sync database: ").append(e.what()).c_str()));
- }
-}
-
-void BlockchainBDB::reset()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- bdb_txn_safe txn;
- if (m_env->txn_begin(NULL, txn, 0))
- throw0(DB_ERROR("Failed to create a transaction for the db"));
- m_write_txn = &txn;
- try
- {
- uint32_t count;
-
- m_blocks->truncate(*m_write_txn, &count, 0);
- m_block_heights->truncate(*m_write_txn, &count, 0);
- m_block_hashes->truncate(*m_write_txn, &count, 0);
- m_block_timestamps->truncate(*m_write_txn, &count, 0);
- m_block_sizes->truncate(*m_write_txn, &count, 0);
- m_block_diffs->truncate(*m_write_txn, &count, 0);
- m_block_coins->truncate(*m_write_txn, &count, 0);
-
- m_txs->truncate(*m_write_txn, &count, 0);
- m_tx_unlocks->truncate(*m_write_txn, &count, 0);
- m_tx_heights->truncate(*m_write_txn, &count, 0);
- m_tx_outputs->truncate(*m_write_txn, &count, 0);
-
- m_output_txs->truncate(*m_write_txn, &count, 0);
- m_output_indices->truncate(*m_write_txn, &count, 0);
- m_output_amounts->truncate(*m_write_txn, &count, 0);
- m_output_keys->truncate(*m_write_txn, &count, 0);
-
- m_spent_keys->truncate(*m_write_txn, &count, 0);
-
- m_hf_starting_heights->truncate(*m_write_txn, &count, 0);
- m_hf_versions->truncate(*m_write_txn, &count, 0);
-
- m_properties->truncate(*m_write_txn, &count, 0);
- }
- catch (const std::exception& e)
- {
- throw0(DB_ERROR(std::string("Failed to reset database: ").append(e.what()).c_str()));
- }
- m_write_txn = NULL;
-}
-
-std::vector<std::string> BlockchainBDB::get_filenames() const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- std::vector<std::string> filenames;
-
- char *fname, *dbname;
- const char **pfname, **pdbname;
-
- pfname = (const char **)&fname;
- pdbname = (const char **)&dbname;
-
- m_blocks->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_block_heights->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_block_hashes->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_block_timestamps->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_block_sizes->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_block_diffs->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_block_coins->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_txs->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_tx_unlocks->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_tx_heights->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_tx_outputs->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_output_txs->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_output_indices->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_output_amounts->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_output_keys->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_spent_keys->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_hf_starting_heights->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_hf_versions->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- m_properties->get_dbname(pfname, pdbname);
- filenames.push_back(fname);
-
- std::vector<std::string> full_paths;
-
- for (auto& filename : filenames)
- {
- boost::filesystem::path p(m_folder);
- p /= filename;
- full_paths.push_back(p.string());
- }
-
- return full_paths;
-}
-
-bool BlockchainBDB::remove_data_file(const std::string& folder)
-{
- return true;
-}
-
-std::string BlockchainBDB::get_db_name() const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
-
- return std::string("BerkeleyDB");
-}
-
-// TODO: this?
-bool BlockchainBDB::lock()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
- return false;
-}
-
-// TODO: this?
-void BlockchainBDB::unlock()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-}
-
-bool BlockchainBDB::block_exists(const crypto::hash& h, uint64_t *height) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::hash> key(h);
-
- auto get_result = m_block_heights->exists(DB_DEFAULT_TX, &key, 0);
- if (get_result == DB_NOTFOUND)
- {
- LOG_PRINT_L3("Block with hash " << epee::string_tools::pod_to_hex(h) << " not found in db");
- return false;
- }
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch block index from hash"));
-
- if (height)
- *height = get_result - 1;
-
- return true;
-}
-
-block BlockchainBDB::get_block(const crypto::hash& h) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- return get_block_from_height(get_block_height(h));
-}
-
-uint64_t BlockchainBDB::get_block_height(const crypto::hash& h) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::hash> key(h);
- Dbt_copy<uint32_t> result;
-
- auto get_result = m_block_heights->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- throw1(BLOCK_DNE("Attempted to retrieve non-existent block height"));
- else if (get_result)
- throw0(DB_ERROR("Error attempting to retrieve a block height from the db"));
-
- return result - 1;
-}
-
-block_header BlockchainBDB::get_block_header(const crypto::hash& h) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- // block_header object is automatically cast from block object
- return get_block(h);
-}
-
-block BlockchainBDB::get_block_from_height(const uint64_t& height) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> key(height + 1);
- Dbt_safe result;
- auto get_result = m_blocks->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- {
- 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"));
-
- blobdata bd;
- bd.assign(reinterpret_cast<char*>(result.get_data()), result.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"));
-
- return b;
-}
-
-uint64_t BlockchainBDB::get_block_timestamp(const uint64_t& height) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> key(height + 1);
- Dbt_copy<uint64_t> result;
- auto get_result = m_block_timestamps->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- {
- 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"));
-
- return result;
-}
-
-uint64_t BlockchainBDB::get_top_block_timestamp() const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- // if no blocks, return 0
- if (m_height == 0)
- {
- return 0;
- }
-
- return get_block_timestamp(m_height - 1);
-}
-
-size_t BlockchainBDB::get_block_weight(const uint64_t& height) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> key(height + 1);
- Dbt_copy<size_t> result;
- auto get_result = m_block_sizes->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- {
- 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"));
-
- return result;
-}
-
-difficulty_type BlockchainBDB::get_block_cumulative_difficulty(const uint64_t& height) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__ << " height: " << height);
- check_open();
-
- Dbt_copy<uint32_t> key(height + 1);
- Dbt_copy<difficulty_type> result;
- auto get_result = m_block_diffs->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- {
- 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"));
-
- return result;
-}
-
-difficulty_type BlockchainBDB::get_block_difficulty(const uint64_t& height) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- difficulty_type diff1 = 0;
- difficulty_type diff2 = 0;
-
- diff1 = get_block_cumulative_difficulty(height);
- if (height != 0)
- {
- diff2 = get_block_cumulative_difficulty(height - 1);
- }
-
- return diff1 - diff2;
-}
-
-uint64_t BlockchainBDB::get_block_already_generated_coins(const uint64_t& height) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> key(height + 1);
- Dbt_copy<uint64_t> result;
- auto get_result = m_block_coins->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- {
- 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"));
-
- return result;
-}
-
-crypto::hash BlockchainBDB::get_block_hash_from_height(const uint64_t& height) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> key(height + 1);
- Dbt_copy<crypto::hash> result;
- auto get_result = m_block_hashes->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- {
- throw0(BLOCK_DNE(std::string("Attempt to get hash from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- hash not in db").c_str()));
- }
- else if (get_result)
- throw0(DB_ERROR("Error attempting to retrieve a block hash from the db."));
-
- return result;
-}
-
-std::vector<block> BlockchainBDB::get_blocks_range(const uint64_t& h1, const uint64_t& h2) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
- std::vector<block> v;
-
- for (uint64_t height = h1; height <= h2; ++height)
- {
- v.push_back(get_block_from_height(height));
- }
-
- return v;
-}
-
-std::vector<crypto::hash> BlockchainBDB::get_hashes_range(const uint64_t& h1, const uint64_t& h2) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
- std::vector<crypto::hash> v;
-
- for (uint64_t height = h1; height <= h2; ++height)
- {
- v.push_back(get_block_hash_from_height(height));
- }
-
- return v;
-}
-
-crypto::hash BlockchainBDB::top_block_hash() const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
- if (m_height > 0)
- {
- return get_block_hash_from_height(m_height - 1);
- }
-
- return null_hash;
-}
-
-block BlockchainBDB::get_top_block() const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- if (m_height > 0)
- {
- return get_block_from_height(m_height - 1);
- }
-
- block b;
- return b;
-}
-
-uint64_t BlockchainBDB::height() const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- return m_height;
-}
-
-bool BlockchainBDB::tx_exists(const crypto::hash& h) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::hash> key(h);
-
- TIME_MEASURE_START(time1);
- auto get_result = m_txs->exists(DB_DEFAULT_TX, &key, 0);
- TIME_MEASURE_FINISH(time1);
- time_tx_exists += time1;
- if (get_result == DB_NOTFOUND)
- {
- LOG_PRINT_L1("transaction with hash " << epee::string_tools::pod_to_hex(h) << " not found in db");
- return false;
- }
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch transaction from hash"));
-
- return true;
-}
-
-uint64_t BlockchainBDB::get_tx_unlock_time(const crypto::hash& h) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::hash> key(h);
- Dbt_copy<uint64_t> result;
- auto get_result = m_tx_unlocks->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- throw1(TX_DNE(std::string("tx unlock time with hash ").append(epee::string_tools::pod_to_hex(h)).append(" not found in db").c_str()));
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch tx unlock time from hash"));
-
- return result;
-}
-
-transaction BlockchainBDB::get_tx(const crypto::hash& h) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::hash> key(h);
- Dbt_safe result;
- auto get_result = m_txs->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- throw1(TX_DNE(std::string("tx with hash ").append(epee::string_tools::pod_to_hex(h)).append(" not found in db").c_str()));
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch tx from hash"));
-
- blobdata bd;
- bd.assign(reinterpret_cast<char*>(result.get_data()), result.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"));
-
- return tx;
-}
-
-uint64_t BlockchainBDB::get_tx_count() const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- DB_BTREE_STAT* stats;
-
- // DB_FAST_STAT can apparently cause an incorrect number of records
- // to be returned. The flag should be set to 0 instead if this proves
- // to be the case.
- m_txs->stat(DB_DEFAULT_TX, &stats, DB_FAST_STAT);
- auto num_txs = stats->bt_nkeys;
- delete stats;
-
- return num_txs;
-}
-
-std::vector<transaction> BlockchainBDB::get_tx_list(const std::vector<crypto::hash>& hlist) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
- std::vector<transaction> v;
-
-for (auto& h : hlist)
- {
- v.push_back(get_tx(h));
- }
-
- return v;
-}
-
-uint64_t BlockchainBDB::get_tx_block_height(const crypto::hash& h) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::hash> key(h);
- Dbt_copy<uint64_t> result;
- auto get_result = m_tx_heights->get(DB_DEFAULT_TX, &key, &result, 0);
- if (get_result == DB_NOTFOUND)
- {
- throw1(TX_DNE(std::string("tx height with hash ").append(epee::string_tools::pod_to_hex(h)).append(" not found in db").c_str()));
- }
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch tx height from hash"));
-
- return (uint64_t)result - 1;
-}
-
-uint64_t BlockchainBDB::get_num_outputs(const uint64_t& amount) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- bdb_cur cur(DB_DEFAULT_TX, m_output_amounts);
-
- Dbt_copy<uint64_t> k(amount);
- Dbt_copy<uint32_t> v;
- auto result = cur->get(&k, &v, DB_SET);
- if (result == DB_NOTFOUND)
- {
- return 0;
- }
- else if (result)
- throw0(DB_ERROR("DB error attempting to get number of outputs of an amount"));
-
- db_recno_t num_elems = 0;
- cur->count(&num_elems, 0);
-
- cur.close();
-
- return num_elems;
-}
-
-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<output_data_t> v;
- auto get_result = m_output_keys->get(DB_DEFAULT_TX, &k, &v, 0);
- if (get_result == DB_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"));
-
- return v;
-}
-
-output_data_t BlockchainBDB::get_output_key(const uint64_t& amount, const uint64_t& index) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- uint64_t glob_index = get_output_global_index(amount, index);
- return get_output_key(glob_index);
-}
-
-tx_out_index BlockchainBDB::get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- std::vector < uint64_t > offsets;
- std::vector<tx_out_index> indices;
- offsets.push_back(index);
- get_output_tx_and_index(amount, offsets, indices);
- if (!indices.size())
- throw1(OUTPUT_DNE("Attempting to get an output index by amount and amount index, but amount not found"));
-
- return indices[0];
-}
-
-std::vector<uint64_t> BlockchainBDB::get_tx_amount_output_indices(const crypto::hash& h) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
- std::vector<uint64_t> index_vec;
- std::vector<uint64_t> index_vec2;
-
- // get the transaction's global output indices first
- index_vec = get_tx_output_indices(h);
- // these are next used to obtain the amount output indices
-
- transaction tx = get_tx(h);
-
- uint64_t i = 0;
- uint64_t global_index;
- for (const auto& vout : tx.vout)
- {
- uint64_t amount = vout.amount;
-
- global_index = index_vec[i];
-
- bdb_cur cur(DB_DEFAULT_TX, m_output_amounts);
-
- Dbt_copy<uint64_t> k(amount);
- Dbt_copy<uint32_t> v;
-
- auto result = cur->get(&k, &v, DB_SET);
- if (result == DB_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"));
-
- db_recno_t num_elems = 0;
- cur->count(&num_elems, 0);
-
- uint64_t amount_output_index = 0;
- uint64_t output_index = 0;
- bool found_index = false;
- for (uint64_t j = 0; j < num_elems; ++j)
- {
- output_index = v;
- if (output_index == global_index)
- {
- amount_output_index = j;
- found_index = true;
- break;
- }
- cur->get(&k, &v, DB_NEXT_DUP);
- }
- if (found_index)
- {
- index_vec2.push_back(amount_output_index);
- }
- else
- {
- // not found
- cur.close();
- throw1(OUTPUT_DNE("specified output not found in db"));
- }
-
- cur.close();
- ++i;
- }
-
- return index_vec2;
-}
-
-
-tx_out_index BlockchainBDB::get_output_tx_and_index_from_global(const uint64_t& index) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> k(index);
- Dbt_copy<crypto::hash > v;
-
- auto get_result = m_output_txs->get(DB_DEFAULT_TX, &k, &v, 0);
- if (get_result == DB_NOTFOUND)
- throw1(OUTPUT_DNE("output with given index not in db"));
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch output tx hash"));
-
- crypto::hash tx_hash = v;
-
- Dbt_copy<uint64_t> result;
- get_result = m_output_indices->get(DB_DEFAULT_TX, &k, &result, 0);
- if (get_result == DB_NOTFOUND)
- throw1(OUTPUT_DNE("output with given index not in db"));
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch output tx index"));
-
- return tx_out_index(tx_hash, result);
-}
-
-bool BlockchainBDB::has_key_image(const crypto::key_image& img) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<crypto::key_image> val_key(img);
- if (m_spent_keys->exists(DB_DEFAULT_TX, &val_key, 0) == 0)
- {
- return true;
- }
-
- return false;
-}
-
-// Ostensibly BerkeleyDB has batch transaction support built-in,
-// so the following few functions will be NOP.
-
-bool BlockchainBDB::batch_start(uint64_t batch_num_blocks)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- return false;
-}
-
-void BlockchainBDB::batch_commit()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
-}
-
-void BlockchainBDB::batch_stop()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
-}
-
-void BlockchainBDB::batch_abort()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
-}
-
-void BlockchainBDB::set_batch_transactions(bool batch_transactions)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- m_batch_transactions = batch_transactions;
- LOG_PRINT_L3("batch transactions " << (m_batch_transactions ? "enabled" : "disabled"));
-}
-
-void BlockchainBDB::block_txn_start(bool readonly)
-{
- // TODO
-}
-
-void BlockchainBDB::block_txn_stop()
-{
- // TODO
-}
-
-void BlockchainBDB::block_txn_abort()
-{
- // TODO
-}
-
-uint64_t BlockchainBDB::add_block(const block& blk, size_t block_weight, const difficulty_type& cumulative_difficulty, const uint64_t& coins_generated, const std::vector<transaction>& txs)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- bdb_txn_safe txn;
- if (m_env->txn_begin(NULL, txn, 0))
- throw0(DB_ERROR("Failed to create a transaction for the db"));
- m_write_txn = &txn;
-
- uint64_t num_outputs = m_num_outputs;
- try
- {
- BlockchainDB::add_block(blk, block_weight, cumulative_difficulty, coins_generated, txs);
- m_write_txn = NULL;
-
- TIME_MEASURE_START(time1);
- txn.commit();
- TIME_MEASURE_FINISH(time1);
- time_commit1 += time1;
- }
- catch (const std::exception& e)
- {
- m_num_outputs = num_outputs;
- m_write_txn = NULL;
- throw;
- }
-
- return ++m_height;
-}
-
-void BlockchainBDB::pop_block(block& blk, std::vector<transaction>& txs)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- bdb_txn_safe txn;
- if (m_env->txn_begin(NULL, txn, 0))
- throw0(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);
-
- m_write_txn = NULL;
- txn.commit();
- }
- catch (...)
- {
- m_num_outputs = num_outputs;
- m_write_txn = NULL;
- throw;
- }
-
- --m_height;
-}
-
-void BlockchainBDB::get_output_tx_and_index_from_global(const std::vector<uint64_t> &global_indices, std::vector<tx_out_index> &tx_out_indices) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
- tx_out_indices.clear();
-
- for (const uint64_t &index : global_indices)
- {
- Dbt_copy<uint32_t> k(index);
- Dbt_copy<crypto::hash> v;
-
- auto get_result = m_output_txs->get(DB_DEFAULT_TX, &k, &v, 0);
- if (get_result == DB_NOTFOUND)
- throw1(OUTPUT_DNE("output with given index not in db"));
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch output tx hash"));
-
- crypto::hash tx_hash = v;
-
- Dbt_copy<uint64_t> result;
- get_result = m_output_indices->get(DB_DEFAULT_TX, &k, &result, 0);
- if (get_result == DB_NOTFOUND)
- throw1(OUTPUT_DNE("output with given index not in db"));
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch output tx index"));
- auto hashindex = tx_out_index(tx_hash, result);
- tx_out_indices.push_back(hashindex);
- }
-}
-
-void BlockchainBDB::get_output_global_indices(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<uint64_t> &global_indices)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- TIME_MEASURE_START(txx);
- check_open();
-
- bdb_cur cur(DB_DEFAULT_TX, m_output_amounts);
- uint64_t max = 0;
- for (const uint64_t& index : offsets)
- {
- if (index > max)
- max = index;
- }
-
- // get returned keypairs count
-#define DB_COUNT_RECORDS(dbt, cnt) \
- do { \
- uint32_t *_p = (uint32_t *) ((uint8_t *)(dbt)->data + \
- (dbt)->ulen - sizeof(uint32_t)); \
- cnt = 0; \
- while(*_p != (uint32_t) -1) { \
- _p -= 2; \
- ++cnt; \
- } \
- } while(0); \
-
- Dbt_copy<uint64_t> k(amount);
- Dbt_copy<uint32_t> v;
- uint64_t buflen = 0;
- uint64_t t_dbmul = 0;
- uint64_t t_dbscan = 0;
-
- auto result = cur->get(&k, &v, DB_SET);
- if (result == DB_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"));
-
- db_recno_t num_elems = 0;
- cur->count(&num_elems, 0);
-
- if (max <= 1 && num_elems <= max)
- throw1(OUTPUT_DNE("Attempting to get an output index by amount and amount index, but output not found"));
-
- TIME_MEASURE_START(db2);
- if (max <= 1)
- {
- for (const uint64_t& index : offsets)
- {
- TIME_MEASURE_START(t_seek);
-
- auto result = cur->get(&k, &v, DB_SET);
- if (result == DB_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"));
-
- for (uint64_t i = 0; i < index; ++i)
- cur->get(&k, &v, DB_NEXT_DUP);
-
- uint64_t glob_index = v;
-
- LOG_PRINT_L3("L0->v: " << glob_index);
- global_indices.push_back(glob_index);
-
- TIME_MEASURE_FINISH(t_seek);
- }
- }
- else
- {
- // setup a 256KB minimum buffer size
- uint32_t pagesize = 256 * 1024;
-
- // Retrieve only a suitable portion of the kvp data, up to somewhere near
- // the maximum offset value being retrieved
- buflen = (max + 1) * 4 * sizeof(uint64_t);
- buflen = ((buflen / pagesize) + ((buflen % pagesize) > 0 ? 1 : 0)) * pagesize;
-
- bool nomem = false;
- Dbt data;
-
- bool singlebuff = buflen <= m_buffer.get_buffer_size();
- buflen = buflen < m_buffer.get_buffer_size() ? buflen : m_buffer.get_buffer_size();
- bdb_safe_buffer_t::type buffer = nullptr;
- bdb_safe_buffer_autolock<bdb_safe_buffer_t> lock(m_buffer, buffer);
-
- data.set_data(buffer);
- data.set_ulen(buflen);
- data.set_size(buflen);
- data.set_flags(DB_DBT_USERMEM);
-
- uint32_t curcount = 0;
- uint32_t blockstart = 0;
- for (const uint64_t& index : offsets)
- {
- if (index >= num_elems)
- {
- LOG_PRINT_L1("Index: " << index << " Elems: " << num_elems << " partial results found for get_output_tx_and_index");
- break;
- }
-
- // fixme! for whatever reason, the first call to DB_MULTIPLE | DB_SET does not
- // retrieve the first value.
- if (index <= 1 || nomem)
- {
- auto result = cur->get(&k, &v, DB_SET);
- if (result == DB_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"));
- }
-
- for (uint64_t i = 0; i < index; ++i)
- cur->get(&k, &v, DB_NEXT_DUP);
- }
- else
- {
- while (index >= curcount)
- {
- TIME_MEASURE_START(t_db1);
- try
- {
- cur->get(&k, &data, DB_MULTIPLE | (curcount == 0 ? DB_SET : DB_NEXT_DUP));
- blockstart = curcount;
-
- int count = 0;
- DB_COUNT_RECORDS((DBT * ) &data, count);
- curcount += count;
- }
- catch (const std::exception &e)
- {
- cur.close();
- throw0(DB_ERROR(std::string("Failed on DB_MULTIPLE: ").append(e.what()).c_str()));
- }
-
- TIME_MEASURE_FINISH(t_db1);
- t_dbmul += t_db1;
- if (singlebuff)
- break;
- }
-
- LOG_PRINT_L3("Records returned: " << curcount << " Index: " << index);
- TIME_MEASURE_START(t_db2);
- DBT *pdata = (DBT *) &data;
-
- uint8_t *value;
- uint64_t dlen = 0;
-
- void *pbase = ((uint8_t *) (pdata->data)) + pdata->ulen - sizeof(uint32_t);
- uint32_t *p = (uint32_t *) pbase;
- if (*p == (uint32_t) -1)
- {
- value = NULL;
- }
- else
- {
- p -= (index - blockstart) * 2; // index * 4 + 2; <- if DB_MULTIPLE_KEY
- value = (uint8_t *) pdata->data + *p--;
- dlen = *p--;
- if (value == (uint8_t *) pdata->data)
- value = NULL;
- }
-
- if (value != NULL)
- {
- v = dlen == sizeof(uint64_t) ? *((uint64_t *) value) : *((uint32_t *) value);
- }
- TIME_MEASURE_FINISH(t_db2);
- t_dbscan += t_db2;
- }
-
- uint64_t glob_index = v;
-
- LOG_PRINT_L3("L1->v: " << glob_index);
- global_indices.push_back(glob_index);
- }
- }
- TIME_MEASURE_FINISH(db2);
-
- cur.close();
-
- TIME_MEASURE_FINISH(txx);
-
- LOG_PRINT_L3("blen: " << buflen << " txx: " << txx << " db1: " << t_dbmul << " db2: " << t_dbscan);
-
-}
-
-void BlockchainBDB::get_output_key(const uint64_t &amount, const std::vector<uint64_t> &offsets, std::vector<output_data_t> &outputs)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
- TIME_MEASURE_START(txx);
- outputs.clear();
-
- std::vector < uint64_t > global_indices;
- get_output_global_indices(amount, offsets, global_indices);
-
- TIME_MEASURE_START(db3);
- if (global_indices.size() > 0)
- {
- for (const uint64_t &index : global_indices)
- {
- Dbt_copy<uint32_t> k(index);
- Dbt_copy<output_data_t> v;
-
- auto get_result = m_output_keys->get(DB_DEFAULT_TX, &k, &v, 0);
- if (get_result == DB_NOTFOUND)
- throw1(OUTPUT_DNE("output with given index not in db"));
- else if (get_result)
- throw0(DB_ERROR("DB error attempting to fetch output tx hash"));
-
- output_data_t data = *(output_data_t *) v.get_data();
- outputs.push_back(data);
- }
- }
-
- TIME_MEASURE_FINISH(txx);
- LOG_PRINT_L3("db3: " << db3);
-}
-
-void BlockchainBDB::get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- std::vector < uint64_t > global_indices;
- get_output_global_indices(amount, offsets, global_indices);
-
- TIME_MEASURE_START(db3);
- if (global_indices.size() > 0)
- get_output_tx_and_index_from_global(global_indices, indices);
- TIME_MEASURE_FINISH(db3);
-
- LOG_PRINT_L3("db3: " << db3);
-}
-
-std::map<uint64_t, uint64_t>::BlockchainBDB::get_output_histogram(const std::vector<uint64_t> &amounts) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- throw1(DB_ERROR("Not implemented."));
-}
-
-void BlockchainBDB::check_hard_fork_info()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- if (m_hf_versions == nullptr)
- {
- LOG_PRINT_L0("hf versions DB not open, so not checking");
- return;
- }
-
- DB_BTREE_STAT* db_stat1, * db_stat2;
-
- // DB_FAST_STAT can apparently cause an incorrect number of records
- // to be returned. The flag should be set to 0 instead if this proves
- // to be the case.
-
- // Set txn to NULL and DB_FAST_STAT to zero (0) for reliability.
- m_blocks->stat(NULL, &db_stat1, 0);
- m_hf_versions->stat(NULL, &db_stat2, 0);
- if (db_stat1->bt_nkeys != db_stat2->bt_nkeys)
- {
- LOG_PRINT_L0("num blocks " << db_stat1->bt_nkeys << " != " << "num hf_versions " << db_stat2->bt_nkeys << " - will clear the two hard fork DBs");
-
- bdb_txn_safe txn;
- bdb_txn_safe* txn_ptr = &txn;
- if (m_write_txn)
- txn_ptr = m_write_txn;
- else
- {
- if (m_env->txn_begin(NULL, txn, 0))
- throw0(DB_ERROR("Failed to create a transaction for the db"));
- }
-
- try
- {
- uint32_t count;
- m_hf_starting_heights->truncate(*txn_ptr, &count, 0);
- LOG_PRINT_L0("hf_starting_heights count: " << count);
- m_hf_versions->truncate(*txn_ptr, &count, 0);
- LOG_PRINT_L0("hf_versions count: " << count);
-
- if (!m_write_txn)
- txn.commit();
- }
- catch (const std::exception& e)
- {
- throw0(DB_ERROR(std::string("Failed to clear two hard fork DBs: ").append(e.what()).c_str()));
- }
- }
- delete db_stat1;
- delete db_stat2;
-}
-
-void BlockchainBDB::drop_hard_fork_info()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- bdb_txn_safe txn;
- bdb_txn_safe* txn_ptr = &txn;
- if (m_write_txn)
- txn_ptr = m_write_txn;
- else
- {
- if (m_env->txn_begin(NULL, txn, 0))
- throw0(DB_ERROR("Failed to create a transaction for the db"));
- }
-
- try
- {
- m_hf_starting_heights->close(0);
- m_hf_versions->close(0);
- m_hf_starting_heights = nullptr;
- m_hf_versions = nullptr;
- if (m_env->dbremove(*txn_ptr, BDB_HF_STARTING_HEIGHTS, NULL, 0) != 0)
- LOG_ERROR("Error removing hf_starting_heights");
- if (m_env->dbremove(*txn_ptr, BDB_HF_VERSIONS, NULL, 0) != 0)
- LOG_ERROR("Error removing hf_versions");
-
- if (!m_write_txn)
- txn.commit();
- }
- catch (const std::exception& e)
- {
- throw0(DB_ERROR(std::string("Failed to drop hard fork info: ").append(e.what()).c_str()));
- }
-}
-
-void BlockchainBDB::set_hard_fork_version(uint64_t height, uint8_t version)
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> val_key(height + 1);
- Dbt_copy<uint8_t> val(version);
- if (m_hf_versions->put(DB_DEFAULT_TX, &val_key, &val, 0))
- throw1(DB_ERROR("Error adding hard fork version to db transaction."));
-}
-
-uint8_t BlockchainBDB::get_hard_fork_version(uint64_t height) const
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- check_open();
-
- Dbt_copy<uint32_t> key(height + 1);
- 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 == 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;
-}
-
-void BlockchainBDB::checkpoint_worker() const
-{
- LOG_PRINT_L0("Entering BDB checkpoint thread.");
- int count = 0;
- while(m_run_checkpoint && m_open)
- {
- // sleep every second, so we don't delay exit condition m_run_checkpoint = false
- sleep(1);
- // checkpoint every 5 minutes
- if(count++ >= 300)
- {
- count = 0;
- if(m_env->txn_checkpoint(0, 0, 0) != 0)
- {
- LOG_PRINT_L0("BDB txn_checkpoint failed.");
- break;
- }
- }
- }
- LOG_PRINT_L0("Leaving BDB checkpoint thread.");
-}
-
-bool BlockchainBDB::is_read_only() const
-{
- return false;
-}
-
-void BlockchainBDB::fixup()
-{
- LOG_PRINT_L3("BlockchainBDB::" << __func__);
- // Always call parent as well
- BlockchainDB::fixup();
-}
-
-} // namespace cryptonote
diff --git a/src/blockchain_db/berkeleydb/db_bdb.h b/src/blockchain_db/berkeleydb/db_bdb.h
deleted file mode 100644
index 3ae90efe1..000000000
--- a/src/blockchain_db/berkeleydb/db_bdb.h
+++ /dev/null
@@ -1,452 +0,0 @@
-// Copyright (c) 2014-2019, 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 <db_cxx.h>
-
-#include "blockchain_db/blockchain_db.h"
-#include "cryptonote_basic/blobdatatype.h" // for type blobdata
-
-#include <unordered_map>
-#include <condition_variable>
-
-// ND: Enables multi-threaded bulk reads for when getting indices.
-// TODO: Disabled for now, as it doesn't seem to provide noticeable improvements (??. Reason: TBD.
-// #define BDB_BULK_CAN_THREAD
-namespace cryptonote
-{
-
-struct bdb_txn_safe
-{
- bdb_txn_safe() : m_txn(NULL) { }
- ~bdb_txn_safe()
- {
- LOG_PRINT_L3("bdb_txn_safe: destructor");
-
- if (m_txn != NULL)
- abort();
- }
-
- void commit(std::string message = "")
- {
- if (message.size() == 0)
- {
- message = "Failed to commit a transaction to the db";
- }
-
- if (m_txn->commit(0))
- {
- m_txn = NULL;
- LOG_PRINT_L0(message);
- throw DB_ERROR(message.c_str());
- }
- m_txn = NULL;
- }
-
- void abort()
- {
- LOG_PRINT_L3("bdb_txn_safe: abort()");
- if(m_txn != NULL)
- {
- m_txn->abort();
- m_txn = NULL;
- }
- else
- {
- LOG_PRINT_L0("WARNING: bdb_txn_safe: abort() called, but m_txn is NULL");
- }
- }
-
- operator DbTxn*()
- {
- return m_txn;
- }
-
- operator DbTxn**()
- {
- return &m_txn;
- }
-private:
- DbTxn* m_txn;
-};
-
-// ND: Class to handle buffer management when doing bulk queries
-// (DB_MULTIPLE). Allocates buffers then handles thread queuing
-// so a fixed set of buffers can be used (instead of allocating
-// every time a bulk query is needed).
-template <typename T>
-class bdb_safe_buffer
-{
- // limit the number of buffers to 8
- const size_t MaxAllowedBuffers = 8;
-public:
- bdb_safe_buffer(size_t num_buffers, size_t count)
- {
- if(num_buffers > MaxAllowedBuffers)
- num_buffers = MaxAllowedBuffers;
-
- set_count(num_buffers);
- for (size_t i = 0; i < num_buffers; i++)
- m_buffers.push_back((T) malloc(sizeof(T) * count));
- m_buffer_count = count;
- }
-
- ~bdb_safe_buffer()
- {
- for (size_t i = 0; i < m_buffers.size(); i++)
- {
- if (m_buffers[i])
- {
- free(m_buffers[i]);
- m_buffers[i] = nullptr;
- }
- }
-
- m_buffers.resize(0);
- }
-
- T acquire_buffer()
- {
- boost::unique_lock<boost::mutex> lock(m_lock);
- m_cv.wait(lock, [&]{ return m_count > 0; });
-
- --m_count;
- size_t index = -1;
- for (size_t i = 0; i < m_open_slot.size(); i++)
- {
- if (m_open_slot[i])
- {
- m_open_slot[i] = false;
- index = i;
- break;
- }
- }
-
- assert(index >= 0);
-
- T buffer = m_buffers[index];
- m_buffer_map.emplace(buffer, index);
- return buffer;
- }
-
- void release_buffer(T buffer)
- {
- boost::unique_lock<boost::mutex> lock(m_lock);
-
- assert(buffer != nullptr);
- auto it = m_buffer_map.find(buffer);
- if (it != m_buffer_map.end())
- {
- auto index = it->second;
-
- assert(index < m_open_slot.size());
- assert(m_open_slot[index] == false);
- assert(m_count < m_open_slot.size());
-
- ++m_count;
- m_open_slot[index] = true;
- m_buffer_map.erase(it);
- m_cv.notify_one();
- }
- }
-
- size_t get_buffer_size() const
- {
- return m_buffer_count * sizeof(T);
- }
-
- size_t get_buffer_count() const
- {
- return m_buffer_count;
- }
-
- typedef T type;
-
-private:
- void set_count(size_t count)
- {
- assert(count > 0);
- m_open_slot.resize(count, true);
- m_count = count;
- }
-
- std::vector<T> m_buffers;
- std::unordered_map<T, size_t> m_buffer_map;
-
- boost::condition_variable m_cv;
- std::vector<bool> m_open_slot;
- size_t m_count;
- boost::mutex m_lock;
-
- size_t m_buffer_count;
-};
-
-template <typename T>
-class bdb_safe_buffer_autolock
-{
-public:
- bdb_safe_buffer_autolock(T &safe_buffer, typename T::type &buffer) :
- m_safe_buffer(safe_buffer), m_buffer(nullptr)
- {
- m_buffer = m_safe_buffer.acquire_buffer();
- buffer = m_buffer;
- }
-
- ~bdb_safe_buffer_autolock()
- {
- if (m_buffer != nullptr)
- {
- m_safe_buffer.release_buffer(m_buffer);
- m_buffer = nullptr;
- }
- }
-private:
- T &m_safe_buffer;
- typename T::type m_buffer;
-};
-
-class BlockchainBDB : public BlockchainDB
-{
-public:
- BlockchainBDB(bool batch_transactions=false);
- ~BlockchainBDB();
-
- virtual void open(const std::string& filename, const int db_flags);
-
- virtual void close();
-
- virtual void sync();
-
- virtual void reset();
-
- virtual std::vector<std::string> get_filenames() const;
-
- virtual bool remove_data_file(const std::string& folder);
-
- virtual std::string get_db_name() const;
-
- virtual bool lock();
-
- virtual void unlock();
-
- virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const;
-
- virtual block get_block(const crypto::hash& h) const;
-
- virtual uint64_t get_block_height(const crypto::hash& h) const;
-
- virtual block_header get_block_header(const crypto::hash& h) const;
-
- virtual block get_block_from_height(const uint64_t& height) const;
-
- virtual uint64_t get_block_timestamp(const uint64_t& height) const;
-
- virtual uint64_t get_top_block_timestamp() const;
-
- virtual size_t get_block_weight(const uint64_t& height) const;
-
- virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const;
-
- virtual difficulty_type get_block_difficulty(const uint64_t& height) const;
-
- virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const;
-
- virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const;
-
- virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const;
-
- virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const;
-
- virtual crypto::hash top_block_hash() const;
-
- virtual block get_top_block() const;
-
- virtual uint64_t height() const;
-
- virtual bool tx_exists(const crypto::hash& h) const;
-
- virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const;
-
- virtual transaction get_tx(const crypto::hash& h) const;
-
- virtual uint64_t get_tx_count() const;
-
- virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const;
-
- virtual uint64_t get_tx_block_height(const crypto::hash& h) const;
-
- virtual uint64_t get_num_outputs(const uint64_t& amount) const;
-
- virtual uint64_t get_indexing_base() const { return 1; }
-
- virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index);
- virtual void get_output_key(const uint64_t &amount, const std::vector<uint64_t> &offsets, std::vector<output_data_t> &outputs);
-
- virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const;
- virtual void get_output_tx_and_index_from_global(const std::vector<uint64_t> &global_indices,
- std::vector<tx_out_index> &tx_out_indices) const;
-
- virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index);
- virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices);
-
- virtual std::vector<uint64_t> get_tx_amount_output_indices(const crypto::hash& h) const;
-
- virtual bool has_key_image(const crypto::key_image& img) const;
-
- virtual uint64_t add_block( const block& blk
- , size_t block_weight
- , const difficulty_type& cumulative_difficulty
- , const uint64_t& coins_generated
- , const std::vector<transaction>& txs
- );
-
- virtual void set_batch_transactions(bool batch_transactions);
- virtual bool batch_start(uint64_t batch_num_blocks=0);
- virtual void batch_commit();
- virtual void batch_stop();
- virtual void batch_abort();
-
- virtual void block_txn_start(bool readonly);
- virtual void block_txn_stop();
- virtual void block_txn_abort();
-
- virtual void pop_block(block& blk, std::vector<transaction>& txs);
-
-#if defined(BDB_BULK_CAN_THREAD)
- virtual bool can_thread_bulk_indices() const { return true; }
-#else
- virtual bool can_thread_bulk_indices() const { return false; }
-#endif
-
- /**
- * @brief return a histogram of outputs on the blockchain
- *
- * @param amounts optional set of amounts to lookup
- *
- * @return a set of amount/instances
- */
- std::map<uint64_t, uint64_t> get_output_histogram(const std::vector<uint64_t> &amounts) const;
-
-private:
- virtual void add_block( const block& blk
- , size_t block_weight
- , const difficulty_type& cumulative_difficulty
- , const uint64_t& coins_generated
- , const crypto::hash& block_hash
- );
-
- virtual void remove_block();
-
- virtual void add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_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, const uint64_t unlock_time, const rct::key *commitment);
-
- virtual void remove_output(const tx_out& tx_output);
-
- void remove_tx_outputs(const crypto::hash& tx_hash, const transaction& tx);
-
- 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);
-
- virtual void remove_spent_key(const crypto::key_image& k_image);
-
- 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&)>, bool pruned) 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_version(uint64_t height, uint8_t version);
- virtual uint8_t get_hard_fork_version(uint64_t height) const;
- virtual void check_hard_fork_info();
- virtual void drop_hard_fork_info();
-
- /**
- * @brief get the global index of the index-th output of the given amount
- *
- * @param amount the output amount
- * @param index the index into the set of outputs of that amount
- *
- * @return the global index of the desired output
- */
- uint64_t get_output_global_index(const uint64_t& amount, const uint64_t& index);
- output_data_t get_output_key(const uint64_t& global_index) const;
- void checkpoint_worker() const;
- void check_open() const;
-
- virtual bool is_read_only() const;
-
- //
- // fix up anything that may be wrong due to past bugs
- virtual void fixup();
-
- bool m_run_checkpoint;
- std::unique_ptr<boost::thread> m_checkpoint_thread;
- typedef bdb_safe_buffer<void *> bdb_safe_buffer_t;
- bdb_safe_buffer_t m_buffer;
-
- DbEnv* m_env;
-
- Db* m_blocks;
- Db* m_block_heights;
- Db* m_block_hashes;
- Db* m_block_timestamps;
- Db* m_block_sizes;
- Db* m_block_diffs;
- Db* m_block_coins;
-
- Db* m_txs;
- Db* m_tx_unlocks;
- Db* m_tx_heights;
- Db* m_tx_outputs;
-
- Db* m_output_txs;
- Db* m_output_indices;
- Db* m_output_amounts;
- Db* m_output_keys;
-
- Db* m_spent_keys;
-
- Db* m_hf_starting_heights;
- Db* m_hf_versions;
-
- Db* m_properties;
-
- uint64_t m_height;
- uint64_t m_num_outputs;
- std::string m_folder;
- bdb_txn_safe *m_write_txn;
-
- bool m_batch_transactions; // support for batch transactions
-};
-
-} // namespace cryptonote
diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp
index 2b039f557..63ac38a88 100644
--- a/src/blockchain_db/blockchain_db.cpp
+++ b/src/blockchain_db/blockchain_db.cpp
@@ -35,17 +35,6 @@
#include "ringct/rctOps.h"
#include "lmdb/db_lmdb.h"
-#ifdef BERKELEY_DB
-#include "berkeleydb/db_bdb.h"
-#endif
-
-static const char *db_types[] = {
- "lmdb",
-#ifdef BERKELEY_DB
- "berkeley",
-#endif
- NULL
-};
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "blockchain.db"
@@ -55,36 +44,6 @@ using epee::string_tools::pod_to_hex;
namespace cryptonote
{
-bool blockchain_valid_db_type(const std::string& db_type)
-{
- int i;
- for (i=0; db_types[i]; i++)
- {
- if (db_types[i] == db_type)
- return true;
- }
- return false;
-}
-
-std::string blockchain_db_types(const std::string& sep)
-{
- int i;
- std::string ret = "";
- for (i=0; db_types[i]; i++)
- {
- if (i)
- ret += sep;
- ret += db_types[i];
- }
- return ret;
-}
-
-std::string arg_db_type_description = "Specify database type, available: " + cryptonote::blockchain_db_types(", ");
-const command_line::arg_descriptor<std::string> arg_db_type = {
- "db-type"
-, arg_db_type_description.c_str()
-, DEFAULT_DB_TYPE
-};
const command_line::arg_descriptor<std::string> arg_db_sync_mode = {
"db-sync-mode"
, "Specify sync option, using format [safe|fast|fastest]:[sync|async]:[<nblocks_per_sync>[blocks]|<nbytes_per_sync>[bytes]]."
@@ -96,20 +55,13 @@ const command_line::arg_descriptor<bool> arg_db_salvage = {
, false
};
-BlockchainDB *new_db(const std::string& db_type)
+BlockchainDB *new_db()
{
- if (db_type == "lmdb")
- return new BlockchainLMDB();
-#if defined(BERKELEY_DB)
- if (db_type == "berkeley")
- return new BlockchainBDB();
-#endif
- return NULL;
+ return new BlockchainLMDB();
}
void BlockchainDB::init_options(boost::program_options::options_description& desc)
{
- command_line::add_arg(desc, arg_db_type);
command_line::add_arg(desc, arg_db_sync_mode);
command_line::add_arg(desc, arg_db_salvage);
}
diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h
index bb4de3ce6..d1e4919be 100644
--- a/src/blockchain_db/blockchain_db.h
+++ b/src/blockchain_db/blockchain_db.h
@@ -102,7 +102,6 @@ namespace cryptonote
/** a pair of <transaction hash, output index>, typedef for convenience */
typedef std::pair<crypto::hash, uint64_t> tx_out_index;
-extern const command_line::arg_descriptor<std::string> arg_db_type;
extern const command_line::arg_descriptor<std::string> arg_db_sync_mode;
extern const command_line::arg_descriptor<bool, false> arg_db_salvage;
@@ -156,7 +155,8 @@ struct txpool_tx_meta_t
uint8_t relayed;
uint8_t do_not_relay;
uint8_t double_spend_seen: 1;
- uint8_t bf_padding: 7;
+ uint8_t pruned: 1;
+ uint8_t bf_padding: 6;
uint8_t padding[76]; // till 192 bytes
};
@@ -1826,7 +1826,7 @@ private:
class db_rtxn_guard: public db_txn_guard { public: db_rtxn_guard(BlockchainDB *db): db_txn_guard(db, true) {} };
class db_wtxn_guard: public db_txn_guard { public: db_wtxn_guard(BlockchainDB *db): db_txn_guard(db, false) {} };
-BlockchainDB *new_db(const std::string& db_type);
+BlockchainDB *new_db();
} // namespace cryptonote
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp
index 760e380a9..8e2b5bebf 100644
--- a/src/blockchain_db/lmdb/db_lmdb.cpp
+++ b/src/blockchain_db/lmdb/db_lmdb.cpp
@@ -2056,7 +2056,7 @@ bool BlockchainLMDB::prune_worker(int mode, uint32_t pruning_seed)
++n_prunable_records;
result = mdb_cursor_get(c_txs_prunable, &k, &v, MDB_SET);
if (result == MDB_NOTFOUND)
- MWARNING("Already pruned at height " << block_height << "/" << blockchain_height);
+ MDEBUG("Already pruned at height " << block_height << "/" << blockchain_height);
else if (result)
throw0(DB_ERROR(lmdb_error("Failed to find transaction prunable data: ", result).c_str()));
else
@@ -2152,7 +2152,7 @@ bool BlockchainLMDB::prune_worker(int mode, uint32_t pruning_seed)
{
++n_prunable_records;
if (result == MDB_NOTFOUND)
- MWARNING("Already pruned at height " << block_height << "/" << blockchain_height);
+ MDEBUG("Already pruned at height " << block_height << "/" << blockchain_height);
else
{
MDEBUG("Pruning at height " << block_height << "/" << blockchain_height);
@@ -2994,6 +2994,8 @@ bool BlockchainLMDB::get_tx_blob(const crypto::hash& h, cryptonote::blobdata &bd
return false;
else if (get_result)
throw0(DB_ERROR(lmdb_error("DB error attempting to fetch tx from hash", get_result).c_str()));
+ else if (result1.mv_size == 0)
+ return false;
bd.assign(reinterpret_cast<char*>(result0.mv_data), result0.mv_size);
bd.append(reinterpret_cast<char*>(result1.mv_data), result1.mv_size);
diff --git a/src/blockchain_utilities/blockchain_ancestry.cpp b/src/blockchain_utilities/blockchain_ancestry.cpp
index a6ee0573f..db6d6f7d7 100644
--- a/src/blockchain_utilities/blockchain_ancestry.cpp
+++ b/src/blockchain_utilities/blockchain_ancestry.cpp
@@ -40,7 +40,6 @@
#include "cryptonote_core/cryptonote_core.h"
#include "cryptonote_core/blockchain.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/db_types.h"
#include "version.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -336,11 +335,6 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
- std::string default_db_type = "lmdb";
-
- std::string available_dbs = cryptonote::blockchain_db_types(", ");
- available_dbs = "available: " + available_dbs;
-
uint32_t log_level = 0;
tools::on_startup();
@@ -350,9 +344,6 @@ int main(int argc, char* argv[])
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_log_level = {"log-level", "0-4 or categories", ""};
- const command_line::arg_descriptor<std::string> arg_database = {
- "database", available_dbs.c_str(), default_db_type
- };
const command_line::arg_descriptor<std::string> arg_txid = {"txid", "Get ancestry for this txid", ""};
const command_line::arg_descriptor<std::string> arg_output = {"output", "Get ancestry for this output (amount/offset format)", ""};
const command_line::arg_descriptor<uint64_t> arg_height = {"height", "Get ancestry for all txes at this height", 0};
@@ -367,7 +358,6 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_txid);
command_line::add_arg(desc_cmd_sett, arg_output);
command_line::add_arg(desc_cmd_sett, arg_height);
@@ -446,13 +436,6 @@ int main(int argc, char* argv[])
}
}
- std::string db_type = command_line::get_arg(vm, arg_database);
- if (!cryptonote::blockchain_valid_db_type(db_type))
- {
- std::cerr << "Invalid database type: " << db_type << std::endl;
- return 1;
- }
-
// If we wanted to use the memory pool, we would set up a fake_core.
// Use Blockchain instead of lower-level BlockchainDB for two reasons:
@@ -468,13 +451,13 @@ int main(int argc, char* argv[])
std::unique_ptr<Blockchain> core_storage;
tx_memory_pool m_mempool(*core_storage);
core_storage.reset(new Blockchain(m_mempool));
- BlockchainDB *db = new_db(db_type);
+ BlockchainDB *db = new_db();
if (db == NULL)
{
- LOG_ERROR("Attempted to use non-existent database type: " << db_type);
- throw std::runtime_error("Attempting to use non-existent database type");
+ LOG_ERROR("Failed to initialize a database");
+ throw std::runtime_error("Failed to initialize a database");
}
- LOG_PRINT_L0("database: " << db_type);
+ LOG_PRINT_L0("database: LMDB");
const std::string filename = (boost::filesystem::path(opt_data_dir) / db->get_db_name()).string();
LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
diff --git a/src/blockchain_utilities/blockchain_blackball.cpp b/src/blockchain_utilities/blockchain_blackball.cpp
index 857e97afd..4a5712921 100644
--- a/src/blockchain_utilities/blockchain_blackball.cpp
+++ b/src/blockchain_utilities/blockchain_blackball.cpp
@@ -39,7 +39,6 @@
#include "cryptonote_core/cryptonote_core.h"
#include "cryptonote_core/blockchain.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/db_types.h"
#include "wallet/ringdb.h"
#include "version.h"
@@ -1170,11 +1169,6 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
- std::string default_db_type = "lmdb";
-
- std::string available_dbs = cryptonote::blockchain_db_types(", ");
- available_dbs = "available: " + available_dbs;
-
uint32_t log_level = 0;
tools::on_startup();
@@ -1188,9 +1182,6 @@ int main(int argc, char* argv[])
get_default_db_path(),
};
const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
- const command_line::arg_descriptor<std::string> arg_database = {
- "database", available_dbs.c_str(), default_db_type
- };
const command_line::arg_descriptor<bool> arg_rct_only = {"rct-only", "Only work on ringCT outputs", false};
const command_line::arg_descriptor<bool> arg_check_subsets = {"check-subsets", "Check ring subsets (very expensive)", false};
const command_line::arg_descriptor<bool> arg_verbose = {"verbose", "Verbose output)", false};
@@ -1207,7 +1198,6 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, arg_blackball_db_dir);
command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_rct_only);
command_line::add_arg(desc_cmd_sett, arg_check_subsets);
command_line::add_arg(desc_cmd_sett, arg_verbose);
@@ -1261,12 +1251,6 @@ int main(int argc, char* argv[])
std::string extra_spent_list = command_line::get_arg(vm, arg_extra_spent_list);
std::vector<std::pair<uint64_t, uint64_t>> extra_spent_outputs = extra_spent_list.empty() ? std::vector<std::pair<uint64_t, uint64_t>>() : load_outputs(extra_spent_list);
- std::string db_type = command_line::get_arg(vm, arg_database);
- if (!cryptonote::blockchain_valid_db_type(db_type))
- {
- std::cerr << "Invalid database type: " << db_type << std::endl;
- return 1;
- }
std::string db_sync_mode = command_line::get_arg(vm, arg_db_sync_mode);
if (!parse_db_sync_mode(db_sync_mode))
diff --git a/src/blockchain_utilities/blockchain_depth.cpp b/src/blockchain_utilities/blockchain_depth.cpp
index 8be83ee67..153f5f7c6 100644
--- a/src/blockchain_utilities/blockchain_depth.cpp
+++ b/src/blockchain_utilities/blockchain_depth.cpp
@@ -34,7 +34,6 @@
#include "cryptonote_core/cryptonote_core.h"
#include "cryptonote_core/blockchain.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/db_types.h"
#include "version.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -50,11 +49,6 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
- std::string default_db_type = "lmdb";
-
- std::string available_dbs = cryptonote::blockchain_db_types(", ");
- available_dbs = "available: " + available_dbs;
-
uint32_t log_level = 0;
tools::on_startup();
@@ -64,9 +58,6 @@ int main(int argc, char* argv[])
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_log_level = {"log-level", "0-4 or categories", ""};
- const command_line::arg_descriptor<std::string> arg_database = {
- "database", available_dbs.c_str(), default_db_type
- };
const command_line::arg_descriptor<std::string> arg_txid = {"txid", "Get min depth for this txid", ""};
const command_line::arg_descriptor<uint64_t> arg_height = {"height", "Get min depth for all txes at this height", 0};
const command_line::arg_descriptor<bool> arg_include_coinbase = {"include-coinbase", "Include coinbase in the average", false};
@@ -75,7 +66,6 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_txid);
command_line::add_arg(desc_cmd_sett, arg_height);
command_line::add_arg(desc_cmd_sett, arg_include_coinbase);
@@ -133,13 +123,6 @@ int main(int argc, char* argv[])
}
}
- std::string db_type = command_line::get_arg(vm, arg_database);
- if (!cryptonote::blockchain_valid_db_type(db_type))
- {
- std::cerr << "Invalid database type: " << db_type << std::endl;
- return 1;
- }
-
// If we wanted to use the memory pool, we would set up a fake_core.
// Use Blockchain instead of lower-level BlockchainDB for two reasons:
@@ -155,13 +138,13 @@ int main(int argc, char* argv[])
std::unique_ptr<Blockchain> core_storage;
tx_memory_pool m_mempool(*core_storage);
core_storage.reset(new Blockchain(m_mempool));
- BlockchainDB *db = new_db(db_type);
+ BlockchainDB *db = new_db();
if (db == NULL)
{
- LOG_ERROR("Attempted to use non-existent database type: " << db_type);
- throw std::runtime_error("Attempting to use non-existent database type");
+ LOG_ERROR("Failed to initialize a database");
+ throw std::runtime_error("Failed to initialize a database");
}
- LOG_PRINT_L0("database: " << db_type);
+ LOG_PRINT_L0("database: LMDB");
const std::string filename = (boost::filesystem::path(opt_data_dir) / db->get_db_name()).string();
LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
diff --git a/src/blockchain_utilities/blockchain_export.cpp b/src/blockchain_utilities/blockchain_export.cpp
index 85566efca..b180f88de 100644
--- a/src/blockchain_utilities/blockchain_export.cpp
+++ b/src/blockchain_utilities/blockchain_export.cpp
@@ -32,7 +32,6 @@
#include "cryptonote_core/tx_pool.h"
#include "cryptonote_core/cryptonote_core.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/db_types.h"
#include "version.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -47,11 +46,6 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
- std::string default_db_type = "lmdb";
-
- std::string available_dbs = cryptonote::blockchain_db_types(", ");
- available_dbs = "available: " + available_dbs;
-
uint32_t log_level = 0;
uint64_t block_stop = 0;
bool blocks_dat = false;
@@ -65,9 +59,6 @@ int main(int argc, char* argv[])
const command_line::arg_descriptor<std::string> arg_output_file = {"output-file", "Specify output file", "", true};
const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
const command_line::arg_descriptor<uint64_t> arg_block_stop = {"block-stop", "Stop at block number", block_stop};
- const command_line::arg_descriptor<std::string> arg_database = {
- "database", available_dbs.c_str(), default_db_type
- };
const command_line::arg_descriptor<bool> arg_blocks_dat = {"blocksdat", "Output in blocks.dat format", blocks_dat};
@@ -76,7 +67,6 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_block_stop);
command_line::add_arg(desc_cmd_sett, arg_blocks_dat);
@@ -124,13 +114,6 @@ int main(int argc, char* argv[])
m_config_folder = command_line::get_arg(vm, cryptonote::arg_data_dir);
- std::string db_type = command_line::get_arg(vm, arg_database);
- if (!cryptonote::blockchain_valid_db_type(db_type))
- {
- std::cerr << "Invalid database type: " << db_type << std::endl;
- return 1;
- }
-
if (command_line::has_arg(vm, arg_output_file))
output_file_path = boost::filesystem::path(command_line::get_arg(vm, arg_output_file));
else
@@ -153,13 +136,13 @@ int main(int argc, char* argv[])
tx_memory_pool m_mempool(*core_storage);
core_storage = new Blockchain(m_mempool);
- BlockchainDB* db = new_db(db_type);
+ BlockchainDB* db = new_db();
if (db == NULL)
{
- LOG_ERROR("Attempted to use non-existent database type: " << db_type);
- throw std::runtime_error("Attempting to use non-existent database type");
+ LOG_ERROR("Failed to initialize a database");
+ throw std::runtime_error("Failed to initialize a database");
}
- LOG_PRINT_L0("database: " << db_type);
+ LOG_PRINT_L0("database: LMDB");
boost::filesystem::path folder(m_config_folder);
folder /= db->get_db_name();
diff --git a/src/blockchain_utilities/blockchain_import.cpp b/src/blockchain_utilities/blockchain_import.cpp
index cb9154f29..5d039d7f4 100644
--- a/src/blockchain_utilities/blockchain_import.cpp
+++ b/src/blockchain_utilities/blockchain_import.cpp
@@ -42,7 +42,6 @@
#include "serialization/binary_utils.h" // dump_binary(), parse_binary()
#include "serialization/json_utils.h" // dump_json()
#include "include_base_utils.h"
-#include "blockchain_db/db_types.h"
#include "cryptonote_core/cryptonote_core.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -93,44 +92,6 @@ int get_db_flags_from_mode(const std::string& db_mode)
return db_flags;
}
-int parse_db_arguments(const std::string& db_arg_str, std::string& db_type, int& db_flags)
-{
- std::vector<std::string> db_args;
- boost::split(db_args, db_arg_str, boost::is_any_of("#"));
- db_type = db_args.front();
- boost::algorithm::trim(db_type);
-
- if (db_args.size() == 1)
- {
- return 0;
- }
- else if (db_args.size() > 2)
- {
- std::cerr << "unrecognized database argument format: " << db_arg_str << ENDL;
- return 1;
- }
-
- std::string db_arg_str2 = db_args[1];
- boost::split(db_args, db_arg_str2, boost::is_any_of(","));
-
- // optionally use a composite mode instead of individual flags
- const std::unordered_set<std::string> db_modes {"safe", "fast", "fastest"};
- std::string db_mode;
- if (db_args.size() == 1)
- {
- if (db_modes.count(db_args[0]) > 0)
- {
- db_mode = db_args[0];
- }
- }
- if (! db_mode.empty())
- {
- db_flags = get_db_flags_from_mode(db_mode);
- }
- return 0;
-}
-
-
int pop_blocks(cryptonote::core& core, int num_blocks)
{
bool use_batch = opt_batch;
@@ -191,7 +152,7 @@ int check_flush(cryptonote::core &core, std::vector<block_complete_entry> &block
}
hashes.push_back(cryptonote::get_block_hash(block));
}
- core.prevalidate_block_hashes(core.get_blockchain_storage().get_db().height(), hashes);
+ core.prevalidate_block_hashes(core.get_blockchain_storage().get_db().height(), hashes, {});
std::vector<block> pblocks;
if (!core.prepare_handle_incoming_blocks(blocks, pblocks))
@@ -217,7 +178,7 @@ int check_flush(cryptonote::core &core, std::vector<block_complete_entry> &block
if(tvc.m_verifivation_failed)
{
MERROR("transaction verification failed, tx_id = "
- << epee::string_tools::pod_to_hex(get_blob_hash(tx_blob)));
+ << epee::string_tools::pod_to_hex(get_blob_hash(tx_blob.blob)));
core.cleanup_handle_incoming_blocks();
return 1;
}
@@ -225,7 +186,7 @@ int check_flush(cryptonote::core &core, std::vector<block_complete_entry> &block
// process block
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
core.handle_incoming_block(block_entry.block, pblocks.empty() ? NULL : &pblocks[blockidx++], bvc, false); // <--- process block
@@ -468,13 +429,17 @@ int import_from_file(cryptonote::core& core, const std::string& import_file_path
{
cryptonote::blobdata block;
cryptonote::block_to_blob(bp.block, block);
- std::vector<cryptonote::blobdata> txs;
+ std::vector<tx_blob_entry> txs;
for (const auto &tx: bp.txs)
{
- txs.push_back(cryptonote::blobdata());
- cryptonote::tx_to_blob(tx, txs.back());
+ txs.push_back({cryptonote::blobdata(), crypto::null_hash});
+ cryptonote::tx_to_blob(tx, txs.back().blob);
}
- blocks.push_back({block, txs});
+ block_complete_entry bce;
+ bce.pruned = false;
+ bce.block = std::move(block);
+ bce.txs = std::move(txs);
+ blocks.push_back(bce);
int ret = check_flush(core, blocks, false);
if (ret)
{
@@ -594,11 +559,6 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
- std::string default_db_type = "lmdb";
-
- std::string available_dbs = cryptonote::blockchain_db_types(", ");
- available_dbs = "available: " + available_dbs;
-
uint32_t log_level = 0;
uint64_t num_blocks = 0;
uint64_t block_stop = 0;
@@ -622,9 +582,6 @@ int main(int argc, char* argv[])
, "Count blocks in bootstrap file and exit"
, false
};
- const command_line::arg_descriptor<std::string> arg_database = {
- "database", available_dbs.c_str(), default_db_type
- };
const command_line::arg_descriptor<bool> arg_noverify = {"dangerous-unverified-import",
"Blindly trust the import file and use potentially malicious blocks and transactions during import (only enable if you exported the file yourself)", false};
const command_line::arg_descriptor<bool> arg_batch = {"batch",
@@ -634,7 +591,6 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, arg_input_file);
command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_batch_size);
command_line::add_arg(desc_cmd_sett, arg_block_stop);
@@ -709,7 +665,6 @@ int main(int argc, char* argv[])
return 1;
}
m_config_folder = command_line::get_arg(vm, cryptonote::arg_data_dir);
- db_arg_str = command_line::get_arg(vm, arg_database);
mlog_configure(mlog_get_default_log_path("monero-blockchain-import.log"), true);
if (!command_line::is_arg_defaulted(vm, arg_log_level))
@@ -735,25 +690,7 @@ int main(int argc, char* argv[])
return 0;
}
-
- std::string db_type;
- int db_flags = 0;
- int res = 0;
- res = parse_db_arguments(db_arg_str, db_type, db_flags);
- if (res)
- {
- std::cerr << "Error parsing database argument(s)" << ENDL;
- return 1;
- }
-
- if (!cryptonote::blockchain_valid_db_type(db_type))
- {
- std::cerr << "Invalid database type: " << db_type << std::endl;
- return 1;
- }
-
- MINFO("database: " << db_type);
- MINFO("database flags: " << db_flags);
+ MINFO("database: LMDB");
MINFO("verify: " << std::boolalpha << opt_verify << std::noboolalpha);
if (opt_batch)
{
diff --git a/src/blockchain_utilities/blockchain_prune.cpp b/src/blockchain_utilities/blockchain_prune.cpp
index 8e13f2c04..9a9d58c46 100644
--- a/src/blockchain_utilities/blockchain_prune.cpp
+++ b/src/blockchain_utilities/blockchain_prune.cpp
@@ -35,7 +35,6 @@
#include "cryptonote_core/blockchain.h"
#include "blockchain_db/blockchain_db.h"
#include "blockchain_db/lmdb/db_lmdb.h"
-#include "blockchain_db/db_types.h"
#include "version.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -441,11 +440,6 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
- std::string default_db_type = "lmdb";
-
- std::string available_dbs = cryptonote::blockchain_db_types(", ");
- available_dbs = "available: " + available_dbs;
-
uint32_t log_level = 0;
tools::on_startup();
@@ -455,9 +449,6 @@ int main(int argc, char* argv[])
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_log_level = {"log-level", "0-4 or categories", ""};
- const command_line::arg_descriptor<std::string> arg_database = {
- "database", available_dbs.c_str(), default_db_type
- };
const command_line::arg_descriptor<std::string> arg_db_sync_mode = {
"db-sync-mode"
, "Specify sync option, using format [safe|fast|fastest]:[nrecords_per_sync]."
@@ -469,7 +460,6 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_db_sync_mode);
command_line::add_arg(desc_cmd_sett, arg_copy_pruned_database);
command_line::add_arg(desc_cmd_only, command_line::arg_help);
@@ -511,18 +501,6 @@ int main(int argc, char* argv[])
while (boost::ends_with(data_dir, "/") || boost::ends_with(data_dir, "\\"))
data_dir.pop_back();
- std::string db_type = command_line::get_arg(vm, arg_database);
- if (!cryptonote::blockchain_valid_db_type(db_type))
- {
- MERROR("Invalid database type: " << db_type);
- return 1;
- }
- if (db_type != "lmdb")
- {
- MERROR("Unsupported database type: " << db_type << ". Only lmdb is supported");
- return 1;
- }
-
std::string db_sync_mode = command_line::get_arg(vm, arg_db_sync_mode);
uint64_t db_flags = 0;
if (!parse_db_sync_mode(db_sync_mode, db_flags))
@@ -552,13 +530,12 @@ int main(int argc, char* argv[])
{
core_storage[n].reset(new Blockchain(m_mempool));
- BlockchainDB* db = new_db(db_type);
+ BlockchainDB* db = new_db();
if (db == NULL)
{
- MERROR("Attempted to use non-existent database type: " << db_type);
- throw std::runtime_error("Attempting to use non-existent database type");
+ MERROR("Failed to initialize a database");
+ throw std::runtime_error("Failed to initialize a database");
}
- MDEBUG("database: " << db_type);
if (n == 1)
{
diff --git a/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp b/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp
index 2d49b6ecd..cee24d4da 100644
--- a/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp
+++ b/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp
@@ -33,7 +33,6 @@
#include "cryptonote_core/cryptonote_core.h"
#include "cryptonote_core/blockchain.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/db_types.h"
#include "version.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -102,11 +101,6 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
- std::string default_db_type = "lmdb";
-
- std::string available_dbs = cryptonote::blockchain_db_types(", ");
- available_dbs = "available: " + available_dbs;
-
uint32_t log_level = 0;
tools::on_startup();
@@ -114,9 +108,6 @@ int main(int argc, char* argv[])
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_log_level = {"log-level", "0-4 or categories", ""};
- const command_line::arg_descriptor<std::string> arg_database = {
- "database", available_dbs.c_str(), default_db_type
- };
const command_line::arg_descriptor<bool> arg_verbose = {"verbose", "Verbose output", false};
const command_line::arg_descriptor<bool> arg_dry_run = {"dry-run", "Do not actually prune", false};
const command_line::arg_descriptor<std::string> arg_input = {"input", "Path to the known spent outputs file"};
@@ -125,7 +116,6 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_verbose);
command_line::add_arg(desc_cmd_sett, arg_dry_run);
command_line::add_arg(desc_cmd_sett, arg_input);
@@ -167,26 +157,18 @@ int main(int argc, char* argv[])
bool opt_verbose = command_line::get_arg(vm, arg_verbose);
bool opt_dry_run = command_line::get_arg(vm, arg_dry_run);
- std::string db_type = command_line::get_arg(vm, arg_database);
- if (!cryptonote::blockchain_valid_db_type(db_type))
- {
- std::cerr << "Invalid database type: " << db_type << std::endl;
- return 1;
- }
-
const std::string input = command_line::get_arg(vm, arg_input);
LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
std::unique_ptr<Blockchain> core_storage;
tx_memory_pool m_mempool(*core_storage);
core_storage.reset(new Blockchain(m_mempool));
- BlockchainDB *db = new_db(db_type);
+ BlockchainDB *db = new_db();
if (db == NULL)
{
- LOG_ERROR("Attempted to use non-existent database type: " << db_type);
- throw std::runtime_error("Attempting to use non-existent database type");
+ LOG_ERROR("Failed to initialize a database");
+ throw std::runtime_error("Failed to initialize a database");
}
- LOG_PRINT_L0("database: " << db_type);
const std::string filename = (boost::filesystem::path(opt_data_dir) / db->get_db_name()).string();
LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
diff --git a/src/blockchain_utilities/blockchain_stats.cpp b/src/blockchain_utilities/blockchain_stats.cpp
index 33c26277e..2f66d54aa 100644
--- a/src/blockchain_utilities/blockchain_stats.cpp
+++ b/src/blockchain_utilities/blockchain_stats.cpp
@@ -34,7 +34,6 @@
#include "cryptonote_core/cryptonote_core.h"
#include "cryptonote_core/blockchain.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/db_types.h"
#include "version.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -52,11 +51,6 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
- std::string default_db_type = "lmdb";
-
- std::string available_dbs = cryptonote::blockchain_db_types(", ");
- available_dbs = "available: " + available_dbs;
-
uint32_t log_level = 0;
uint64_t block_start = 0;
uint64_t block_stop = 0;
@@ -68,9 +62,6 @@ int main(int argc, char* argv[])
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_log_level = {"log-level", "0-4 or categories", ""};
- const command_line::arg_descriptor<std::string> arg_database = {
- "database", available_dbs.c_str(), default_db_type
- };
const command_line::arg_descriptor<uint64_t> arg_block_start = {"block-start", "start at block number", block_start};
const command_line::arg_descriptor<uint64_t> arg_block_stop = {"block-stop", "Stop at block number", block_stop};
const command_line::arg_descriptor<bool> arg_inputs = {"with-inputs", "with input stats", false};
@@ -82,7 +73,6 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_block_start);
command_line::add_arg(desc_cmd_sett, arg_block_stop);
command_line::add_arg(desc_cmd_sett, arg_inputs);
@@ -131,24 +121,16 @@ int main(int argc, char* argv[])
bool do_ringsize = command_line::get_arg(vm, arg_ringsize);
bool do_hours = command_line::get_arg(vm, arg_hours);
- std::string db_type = command_line::get_arg(vm, arg_database);
- if (!cryptonote::blockchain_valid_db_type(db_type))
- {
- std::cerr << "Invalid database type: " << db_type << std::endl;
- return 1;
- }
-
LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
std::unique_ptr<Blockchain> core_storage;
tx_memory_pool m_mempool(*core_storage);
core_storage.reset(new Blockchain(m_mempool));
- BlockchainDB *db = new_db(db_type);
+ BlockchainDB *db = new_db();
if (db == NULL)
{
- LOG_ERROR("Attempted to use non-existent database type: " << db_type);
- throw std::runtime_error("Attempting to use non-existent database type");
+ LOG_ERROR("Failed to initialize a database");
+ throw std::runtime_error("Failed to initialize a database");
}
- LOG_PRINT_L0("database: " << db_type);
const std::string filename = (boost::filesystem::path(opt_data_dir) / db->get_db_name()).string();
LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
diff --git a/src/blockchain_utilities/blockchain_usage.cpp b/src/blockchain_utilities/blockchain_usage.cpp
index bd73350b3..2fa56452b 100644
--- a/src/blockchain_utilities/blockchain_usage.cpp
+++ b/src/blockchain_utilities/blockchain_usage.cpp
@@ -34,7 +34,6 @@
#include "cryptonote_core/cryptonote_core.h"
#include "cryptonote_core/blockchain.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/db_types.h"
#include "version.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -82,11 +81,6 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
- std::string default_db_type = "lmdb";
-
- std::string available_dbs = cryptonote::blockchain_db_types(", ");
- available_dbs = "available: " + available_dbs;
-
uint32_t log_level = 0;
tools::on_startup();
@@ -96,16 +90,12 @@ int main(int argc, char* argv[])
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_log_level = {"log-level", "0-4 or categories", ""};
- const command_line::arg_descriptor<std::string> arg_database = {
- "database", available_dbs.c_str(), default_db_type
- };
const command_line::arg_descriptor<bool> arg_rct_only = {"rct-only", "Only work on ringCT outputs", false};
const command_line::arg_descriptor<std::string> arg_input = {"input", ""};
command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
command_line::add_arg(desc_cmd_sett, arg_log_level);
- command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_rct_only);
command_line::add_arg(desc_cmd_sett, arg_input);
command_line::add_arg(desc_cmd_only, command_line::arg_help);
@@ -147,13 +137,6 @@ int main(int argc, char* argv[])
network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET;
bool opt_rct_only = command_line::get_arg(vm, arg_rct_only);
- std::string db_type = command_line::get_arg(vm, arg_database);
- if (!cryptonote::blockchain_valid_db_type(db_type))
- {
- std::cerr << "Invalid database type: " << db_type << std::endl;
- return 1;
- }
-
// If we wanted to use the memory pool, we would set up a fake_core.
// Use Blockchain instead of lower-level BlockchainDB for two reasons:
@@ -170,13 +153,13 @@ int main(int argc, char* argv[])
std::unique_ptr<Blockchain> core_storage;
tx_memory_pool m_mempool(*core_storage);
core_storage.reset(new Blockchain(m_mempool));
- BlockchainDB* db = new_db(db_type);
+ BlockchainDB* db = new_db();
if (db == NULL)
{
- LOG_ERROR("Attempted to use non-existent database type: " << db_type);
- throw std::runtime_error("Attempting to use non-existent database type");
+ LOG_ERROR("Failed to initialize a database");
+ throw std::runtime_error("Failed to initialize a database");
}
- LOG_PRINT_L0("database: " << db_type);
+ LOG_PRINT_L0("database: LMDB");
const std::string filename = input;
LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
diff --git a/src/blockchain_utilities/blocksdat_file.cpp b/src/blockchain_utilities/blocksdat_file.cpp
index f56ff5f94..df3c6cafc 100644
--- a/src/blockchain_utilities/blocksdat_file.cpp
+++ b/src/blockchain_utilities/blocksdat_file.cpp
@@ -99,17 +99,23 @@ bool BlocksdatFile::initialize_file(uint64_t block_stop)
return true;
}
-void BlocksdatFile::write_block(const crypto::hash& block_hash)
+void BlocksdatFile::write_block(const crypto::hash& block_hash, uint64_t weight)
{
m_hashes.push_back(block_hash);
+ m_weights.push_back(weight);
while (m_hashes.size() >= HASH_OF_HASHES_STEP)
{
crypto::hash hash;
crypto::cn_fast_hash(m_hashes.data(), HASH_OF_HASHES_STEP * sizeof(crypto::hash), hash);
memmove(m_hashes.data(), m_hashes.data() + HASH_OF_HASHES_STEP, (m_hashes.size() - HASH_OF_HASHES_STEP) * sizeof(crypto::hash));
m_hashes.resize(m_hashes.size() - HASH_OF_HASHES_STEP);
- const std::string data(hash.data, sizeof(hash));
- *m_raw_data_file << data;
+ const std::string data_hashes(hash.data, sizeof(hash));
+ *m_raw_data_file << data_hashes;
+ crypto::cn_fast_hash(m_weights.data(), HASH_OF_HASHES_STEP * sizeof(uint64_t), hash);
+ memmove(m_weights.data(), m_weights.data() + HASH_OF_HASHES_STEP, (m_weights.size() - HASH_OF_HASHES_STEP) * sizeof(uint64_t));
+ m_weights.resize(m_weights.size() - HASH_OF_HASHES_STEP);
+ const std::string data_weights(hash.data, sizeof(hash));
+ *m_raw_data_file << data_weights;
}
}
@@ -154,7 +160,8 @@ bool BlocksdatFile::store_blockchain_raw(Blockchain* _blockchain_storage, tx_mem
{
// this method's height refers to 0-based height (genesis block = height 0)
crypto::hash hash = m_blockchain_storage->get_block_id_by_height(m_cur_height);
- write_block(hash);
+ uint64_t weight = m_blockchain_storage->get_db().get_block_weight(m_cur_height);
+ write_block(hash, weight);
if (m_cur_height % NUM_BLOCKS_PER_CHUNK == 0) {
num_blocks_written += NUM_BLOCKS_PER_CHUNK;
}
diff --git a/src/blockchain_utilities/blocksdat_file.h b/src/blockchain_utilities/blocksdat_file.h
index 315713424..72b7afc17 100644
--- a/src/blockchain_utilities/blocksdat_file.h
+++ b/src/blockchain_utilities/blocksdat_file.h
@@ -72,10 +72,11 @@ protected:
bool open_writer(const boost::filesystem::path& file_path, uint64_t block_stop);
bool initialize_file(uint64_t block_stop);
bool close();
- void write_block(const crypto::hash &block_hash);
+ void write_block(const crypto::hash &block_hash, uint64_t weight);
private:
uint64_t m_cur_height; // tracks current height during export
std::vector<crypto::hash> m_hashes;
+ std::vector<uint64_t> m_weights;
};
diff --git a/src/blocks/checkpoints.dat b/src/blocks/checkpoints.dat
index a7d309753..eb614d58d 100644
--- a/src/blocks/checkpoints.dat
+++ b/src/blocks/checkpoints.dat
Binary files differ
diff --git a/src/common/perf_timer.cpp b/src/common/perf_timer.cpp
index 189eb85eb..4408170d1 100644
--- a/src/common/perf_timer.cpp
+++ b/src/common/perf_timer.cpp
@@ -34,7 +34,7 @@
#define MONERO_DEFAULT_LOG_CATEGORY "perf"
#define PERF_LOG_ALWAYS(level, cat, x) \
- el::base::Writer(level, __FILE__, __LINE__, ELPP_FUNC, el::base::DispatchAction::FileOnlyLog).construct(cat) << x
+ el::base::Writer(level, el::Color::Default, __FILE__, __LINE__, ELPP_FUNC, el::base::DispatchAction::FileOnlyLog).construct(cat) << x
#define PERF_LOG(level, cat, x) \
do { \
if (ELPP->vRegistry()->allowed(level, cat)) PERF_LOG_ALWAYS(level, cat, x); \
diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt
index 3a4d09fd8..80f1e5d7e 100644
--- a/src/crypto/CMakeLists.txt
+++ b/src/crypto/CMakeLists.txt
@@ -46,6 +46,7 @@ set(crypto_sources
random.c
skein.c
slow-hash.c
+ rx-slow-hash.c
CryptonightR_JIT.c
tree-hash.c)
@@ -53,6 +54,8 @@ if(ARCH_ID STREQUAL "i386" OR ARCH_ID STREQUAL "x86_64" OR ARCH_ID STREQUAL "x86
list(APPEND crypto_sources CryptonightR_template.S)
endif()
+include_directories(${RANDOMX_INCLUDE})
+
set(crypto_headers)
set(crypto_private_headers
@@ -86,6 +89,7 @@ monero_add_library(cncrypto
target_link_libraries(cncrypto
PUBLIC
epee
+ randomx
${Boost_SYSTEM_LIBRARY}
${SODIUM_LIBRARY}
PRIVATE
diff --git a/src/crypto/c_threads.h b/src/crypto/c_threads.h
new file mode 100644
index 000000000..56d680ff8
--- /dev/null
+++ b/src/crypto/c_threads.h
@@ -0,0 +1,58 @@
+// Copyright (c) 2019, 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.
+
+/* Brain-dead simple portability wrapper over thread APIs for C */
+#pragma once
+
+#ifdef _WIN32
+#include <windows.h>
+#define CTHR_MUTEX_TYPE HANDLE
+#define CTHR_MUTEX_INIT NULL
+#define CTHR_MUTEX_LOCK(x) do { if (x == NULL) { \
+ HANDLE p = CreateMutex(NULL, FALSE, NULL); \
+ if (InterlockedCompareExchangePointer((PVOID*)&x, (PVOID)p, NULL) != NULL) \
+ CloseHandle(p); \
+ } WaitForSingleObject(x, INFINITE); } while(0)
+#define CTHR_MUTEX_UNLOCK(x) ReleaseMutex(x)
+#define CTHR_THREAD_TYPE HANDLE
+#define CTHR_THREAD_RTYPE void
+#define CTHR_THREAD_RETURN return
+#define CTHR_THREAD_CREATE(thr, func, arg) thr = (HANDLE)_beginthread(func, 0, arg)
+#define CTHR_THREAD_JOIN(thr) WaitForSingleObject(thr, INFINITE)
+#else
+#include <pthread.h>
+#define CTHR_MUTEX_TYPE pthread_mutex_t
+#define CTHR_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
+#define CTHR_MUTEX_LOCK(x) pthread_mutex_lock(&x)
+#define CTHR_MUTEX_UNLOCK(x) pthread_mutex_unlock(&x)
+#define CTHR_THREAD_TYPE pthread_t
+#define CTHR_THREAD_RTYPE void *
+#define CTHR_THREAD_RETURN return NULL
+#define CTHR_THREAD_CREATE(thr, func, arg) pthread_create(&thr, NULL, func, arg)
+#define CTHR_THREAD_JOIN(thr) pthread_join(thr, NULL)
+#endif
diff --git a/src/crypto/hash-ops.h b/src/crypto/hash-ops.h
index 859c810bd..d117bb640 100644
--- a/src/crypto/hash-ops.h
+++ b/src/crypto/hash-ops.h
@@ -87,3 +87,11 @@ void hash_extra_jh(const void *data, size_t length, char *hash);
void hash_extra_skein(const void *data, size_t length, char *hash);
void tree_hash(const char (*hashes)[HASH_SIZE], size_t count, char *root_hash);
+
+#define RX_BLOCK_VERSION 12
+void rx_slow_hash_allocate_state(void);
+void rx_slow_hash_free_state(void);
+uint64_t rx_seedheight(const uint64_t height);
+void rx_seedheights(const uint64_t height, uint64_t *seed_height, uint64_t *next_height);
+void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const char *seedhash, const void *data, size_t length, char *hash, int miners, int is_alt);
+void rx_reorg(const uint64_t split_height);
diff --git a/src/crypto/hash.h b/src/crypto/hash.h
index 17071923d..27184fa53 100644
--- a/src/crypto/hash.h
+++ b/src/crypto/hash.h
@@ -32,7 +32,6 @@
#include <stddef.h>
#include <iostream>
-#include <boost/utility/value_init.hpp>
#include "common/pod-class.h"
#include "generic-ops.h"
@@ -90,8 +89,8 @@ namespace crypto {
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
}
- const static crypto::hash null_hash = boost::value_initialized<crypto::hash>();
- const static crypto::hash8 null_hash8 = boost::value_initialized<crypto::hash8>();
+ constexpr static crypto::hash null_hash = {};
+ constexpr static crypto::hash8 null_hash8 = {};
}
CRYPTO_MAKE_HASHABLE(hash)
diff --git a/src/crypto/rx-slow-hash.c b/src/crypto/rx-slow-hash.c
new file mode 100644
index 000000000..59bd89d13
--- /dev/null
+++ b/src/crypto/rx-slow-hash.c
@@ -0,0 +1,359 @@
+// Copyright (c) 2019, 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 <assert.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "randomx.h"
+#include "c_threads.h"
+#include "hash-ops.h"
+#include "misc_log_ex.h"
+
+#define RX_LOGCAT "randomx"
+
+#if defined(_MSC_VER)
+#define THREADV __declspec(thread)
+#else
+#define THREADV __thread
+#endif
+
+typedef struct rx_state {
+ CTHR_MUTEX_TYPE rs_mutex;
+ char rs_hash[HASH_SIZE];
+ uint64_t rs_height;
+ randomx_cache *rs_cache;
+} rx_state;
+
+static CTHR_MUTEX_TYPE rx_mutex = CTHR_MUTEX_INIT;
+static CTHR_MUTEX_TYPE rx_dataset_mutex = CTHR_MUTEX_INIT;
+
+static rx_state rx_s[2] = {{CTHR_MUTEX_INIT,{0},0,0},{CTHR_MUTEX_INIT,{0},0,0}};
+
+static randomx_dataset *rx_dataset;
+static uint64_t rx_dataset_height;
+static THREADV randomx_vm *rx_vm = NULL;
+
+static void local_abort(const char *msg)
+{
+ fprintf(stderr, "%s\n", msg);
+#ifdef NDEBUG
+ _exit(1);
+#else
+ abort();
+#endif
+}
+
+/**
+ * @brief uses cpuid to determine if the CPU supports the AES instructions
+ * @return true if the CPU supports AES, false otherwise
+ */
+
+static inline int force_software_aes(void)
+{
+ static int use = -1;
+
+ if (use != -1)
+ return use;
+
+ const char *env = getenv("MONERO_USE_SOFTWARE_AES");
+ if (!env) {
+ use = 0;
+ }
+ else if (!strcmp(env, "0") || !strcmp(env, "no")) {
+ use = 0;
+ }
+ else {
+ use = 1;
+ }
+ return use;
+}
+
+static void cpuid(int CPUInfo[4], int InfoType)
+{
+#if defined(__x86_64__)
+ __asm __volatile__
+ (
+ "cpuid":
+ "=a" (CPUInfo[0]),
+ "=b" (CPUInfo[1]),
+ "=c" (CPUInfo[2]),
+ "=d" (CPUInfo[3]) :
+ "a" (InfoType), "c" (0)
+ );
+#endif
+}
+static inline int check_aes_hw(void)
+{
+#if defined(__x86_64__)
+ int cpuid_results[4];
+ static int supported = -1;
+
+ if(supported >= 0)
+ return supported;
+
+ cpuid(cpuid_results,1);
+ return supported = cpuid_results[2] & (1 << 25);
+#else
+ return 0;
+#endif
+}
+
+static volatile int use_rx_jit_flag = -1;
+
+static inline int use_rx_jit(void)
+{
+#if defined(__x86_64__)
+
+ if (use_rx_jit_flag != -1)
+ return use_rx_jit_flag;
+
+ const char *env = getenv("MONERO_USE_RX_JIT");
+ if (!env) {
+ use_rx_jit_flag = 1;
+ }
+ else if (!strcmp(env, "0") || !strcmp(env, "no")) {
+ use_rx_jit_flag = 0;
+ }
+ else {
+ use_rx_jit_flag = 1;
+ }
+ return use_rx_jit_flag;
+#else
+ return 0;
+#endif
+}
+
+#define SEEDHASH_EPOCH_BLOCKS 2048 /* Must be same as BLOCKS_SYNCHRONIZING_MAX_COUNT in cryptonote_config.h */
+#define SEEDHASH_EPOCH_LAG 64
+
+void rx_reorg(const uint64_t split_height) {
+ int i;
+ CTHR_MUTEX_LOCK(rx_mutex);
+ for (i=0; i<2; i++) {
+ if (split_height <= rx_s[i].rs_height) {
+ if (rx_s[i].rs_height == rx_dataset_height)
+ rx_dataset_height = 1;
+ rx_s[i].rs_height = 1; /* set to an invalid seed height */
+ }
+ }
+ CTHR_MUTEX_UNLOCK(rx_mutex);
+}
+
+uint64_t rx_seedheight(const uint64_t height) {
+ uint64_t s_height = (height <= SEEDHASH_EPOCH_BLOCKS+SEEDHASH_EPOCH_LAG) ? 0 :
+ (height - SEEDHASH_EPOCH_LAG - 1) & ~(SEEDHASH_EPOCH_BLOCKS-1);
+ return s_height;
+}
+
+void rx_seedheights(const uint64_t height, uint64_t *seedheight, uint64_t *nextheight) {
+ *seedheight = rx_seedheight(height);
+ *nextheight = rx_seedheight(height + SEEDHASH_EPOCH_LAG);
+}
+
+typedef struct seedinfo {
+ randomx_cache *si_cache;
+ unsigned long si_start;
+ unsigned long si_count;
+} seedinfo;
+
+static CTHR_THREAD_RTYPE rx_seedthread(void *arg) {
+ seedinfo *si = arg;
+ randomx_init_dataset(rx_dataset, si->si_cache, si->si_start, si->si_count);
+ CTHR_THREAD_RETURN;
+}
+
+static void rx_initdata(randomx_cache *rs_cache, const int miners, const uint64_t seedheight) {
+ if (miners > 1) {
+ unsigned long delta = randomx_dataset_item_count() / miners;
+ unsigned long start = 0;
+ int i;
+ seedinfo *si;
+ CTHR_THREAD_TYPE *st;
+ si = malloc(miners * sizeof(seedinfo));
+ if (si == NULL)
+ local_abort("Couldn't allocate RandomX mining threadinfo");
+ st = malloc(miners * sizeof(CTHR_THREAD_TYPE));
+ if (st == NULL) {
+ free(si);
+ local_abort("Couldn't allocate RandomX mining threadlist");
+ }
+ for (i=0; i<miners-1; i++) {
+ si[i].si_cache = rs_cache;
+ si[i].si_start = start;
+ si[i].si_count = delta;
+ start += delta;
+ }
+ si[i].si_cache = rs_cache;
+ si[i].si_start = start;
+ si[i].si_count = randomx_dataset_item_count() - start;
+ for (i=1; i<miners; i++) {
+ CTHR_THREAD_CREATE(st[i], rx_seedthread, &si[i]);
+ }
+ randomx_init_dataset(rx_dataset, rs_cache, 0, si[0].si_count);
+ for (i=1; i<miners; i++) {
+ CTHR_THREAD_JOIN(st[i]);
+ }
+ free(st);
+ free(si);
+ } else {
+ randomx_init_dataset(rx_dataset, rs_cache, 0, randomx_dataset_item_count());
+ }
+ rx_dataset_height = seedheight;
+}
+
+void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const char *seedhash, const void *data, size_t length,
+ char *hash, int miners, int is_alt) {
+ uint64_t s_height = rx_seedheight(mainheight);
+ int toggle = (s_height & SEEDHASH_EPOCH_BLOCKS) != 0;
+ randomx_flags flags = RANDOMX_FLAG_DEFAULT;
+ rx_state *rx_sp;
+ randomx_cache *cache;
+
+ CTHR_MUTEX_LOCK(rx_mutex);
+
+ /* if alt block but with same seed as mainchain, no need for alt cache */
+ if (is_alt) {
+ if (s_height == seedheight && !memcmp(rx_s[toggle].rs_hash, seedhash, HASH_SIZE))
+ is_alt = 0;
+ } else {
+ /* RPC could request an earlier block on mainchain */
+ if (s_height > seedheight)
+ is_alt = 1;
+ /* miner can be ahead of mainchain */
+ else if (s_height < seedheight)
+ toggle ^= 1;
+ }
+
+ toggle ^= (is_alt != 0);
+
+ rx_sp = &rx_s[toggle];
+ CTHR_MUTEX_LOCK(rx_sp->rs_mutex);
+ CTHR_MUTEX_UNLOCK(rx_mutex);
+
+ cache = rx_sp->rs_cache;
+ if (cache == NULL) {
+ if (use_rx_jit())
+ flags |= RANDOMX_FLAG_JIT;
+ if (cache == NULL) {
+ cache = randomx_alloc_cache(flags | RANDOMX_FLAG_LARGE_PAGES);
+ if (cache == NULL) {
+ mdebug(RX_LOGCAT, "Couldn't use largePages for RandomX cache");
+ cache = randomx_alloc_cache(flags);
+ }
+ if (cache == NULL)
+ local_abort("Couldn't allocate RandomX cache");
+ }
+ }
+ if (rx_sp->rs_height != seedheight || rx_sp->rs_cache == NULL || memcmp(seedhash, rx_sp->rs_hash, HASH_SIZE)) {
+ randomx_init_cache(cache, seedhash, HASH_SIZE);
+ rx_sp->rs_cache = cache;
+ rx_sp->rs_height = seedheight;
+ memcpy(rx_sp->rs_hash, seedhash, HASH_SIZE);
+ }
+ if (rx_vm == NULL) {
+ randomx_flags flags = RANDOMX_FLAG_DEFAULT;
+ if (use_rx_jit()) {
+ flags |= RANDOMX_FLAG_JIT;
+ if (!miners)
+ flags |= RANDOMX_FLAG_SECURE;
+ }
+ if(!force_software_aes() && check_aes_hw())
+ flags |= RANDOMX_FLAG_HARD_AES;
+ if (miners) {
+ CTHR_MUTEX_LOCK(rx_dataset_mutex);
+ if (rx_dataset == NULL) {
+ rx_dataset = randomx_alloc_dataset(RANDOMX_FLAG_LARGE_PAGES);
+ if (rx_dataset == NULL) {
+ mdebug(RX_LOGCAT, "Couldn't use largePages for RandomX dataset");
+ rx_dataset = randomx_alloc_dataset(RANDOMX_FLAG_DEFAULT);
+ }
+ if (rx_dataset != NULL)
+ rx_initdata(rx_sp->rs_cache, miners, seedheight);
+ }
+ if (rx_dataset != NULL)
+ flags |= RANDOMX_FLAG_FULL_MEM;
+ else {
+ miners = 0;
+ mwarning(RX_LOGCAT, "Couldn't allocate RandomX dataset for miner");
+ }
+ CTHR_MUTEX_UNLOCK(rx_dataset_mutex);
+ }
+ rx_vm = randomx_create_vm(flags | RANDOMX_FLAG_LARGE_PAGES, rx_sp->rs_cache, rx_dataset);
+ if(rx_vm == NULL) { //large pages failed
+ mdebug(RX_LOGCAT, "Couldn't use largePages for RandomX VM");
+ rx_vm = randomx_create_vm(flags, rx_sp->rs_cache, rx_dataset);
+ }
+ if(rx_vm == NULL) {//fallback if everything fails
+ flags = RANDOMX_FLAG_DEFAULT | (miners ? RANDOMX_FLAG_FULL_MEM : 0);
+ rx_vm = randomx_create_vm(flags, rx_sp->rs_cache, rx_dataset);
+ }
+ if (rx_vm == NULL)
+ local_abort("Couldn't allocate RandomX VM");
+ } else if (miners) {
+ CTHR_MUTEX_LOCK(rx_dataset_mutex);
+ if (rx_dataset != NULL && rx_dataset_height != seedheight)
+ rx_initdata(cache, miners, seedheight);
+ CTHR_MUTEX_UNLOCK(rx_dataset_mutex);
+ } else {
+ /* this is a no-op if the cache hasn't changed */
+ randomx_vm_set_cache(rx_vm, rx_sp->rs_cache);
+ }
+ /* mainchain users can run in parallel */
+ if (!is_alt)
+ CTHR_MUTEX_UNLOCK(rx_sp->rs_mutex);
+ randomx_calculate_hash(rx_vm, data, length, hash);
+ /* altchain slot users always get fully serialized */
+ if (is_alt)
+ CTHR_MUTEX_UNLOCK(rx_sp->rs_mutex);
+}
+
+void rx_slow_hash_allocate_state(void) {
+}
+
+void rx_slow_hash_free_state(void) {
+ if (rx_vm != NULL) {
+ randomx_destroy_vm(rx_vm);
+ rx_vm = NULL;
+ }
+}
+
+void rx_stop_mining(void) {
+ CTHR_MUTEX_LOCK(rx_dataset_mutex);
+ if (rx_dataset != NULL) {
+ randomx_dataset *rd = rx_dataset;
+ rx_dataset = NULL;
+ randomx_release_dataset(rd);
+ }
+ CTHR_MUTEX_UNLOCK(rx_dataset_mutex);
+}
diff --git a/src/crypto/slow-hash.c b/src/crypto/slow-hash.c
index 647471513..88eb751a6 100644
--- a/src/crypto/slow-hash.c
+++ b/src/crypto/slow-hash.c
@@ -742,7 +742,7 @@ BOOL SetLockPagesPrivilege(HANDLE hProcess, BOOL bEnable)
* the allocated buffer.
*/
-void slow_hash_allocate_state(void)
+void cn_slow_hash_allocate_state(void)
{
if(hp_state != NULL)
return;
@@ -804,7 +804,7 @@ void slow_hash_allocate_state(void)
*@brief frees the state allocated by slow_hash_allocate_state
*/
-void slow_hash_free_state(void)
+void cn_slow_hash_free_state(void)
{
if(hp_state == NULL)
return;
@@ -892,7 +892,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int
// this isn't supposed to happen, but guard against it for now.
if(hp_state == NULL)
- slow_hash_allocate_state();
+ cn_slow_hash_allocate_state();
// locals to avoid constant TLS dereferencing
uint8_t *local_hp_state = hp_state;
@@ -1009,13 +1009,13 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int
}
#elif !defined NO_AES && (defined(__arm__) || defined(__aarch64__))
-void slow_hash_allocate_state(void)
+void cn_slow_hash_allocate_state(void)
{
// Do nothing, this is just to maintain compatibility with the upgraded slow-hash.c
return;
}
-void slow_hash_free_state(void)
+void cn_slow_hash_free_state(void)
{
// As above
return;
@@ -1582,13 +1582,13 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int
#define hp_jitfunc ((v4_random_math_JIT_func)NULL)
-void slow_hash_allocate_state(void)
+void cn_slow_hash_allocate_state(void)
{
// Do nothing, this is just to maintain compatibility with the upgraded slow-hash.c
return;
}
-void slow_hash_free_state(void)
+void cn_slow_hash_free_state(void)
{
// As above
return;
@@ -1765,3 +1765,15 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int
}
#endif
+
+void slow_hash_allocate_state(void)
+{
+ cn_slow_hash_allocate_state();
+ rx_slow_hash_allocate_state();
+}
+
+void slow_hash_free_state(void)
+{
+ cn_slow_hash_free_state();
+ rx_slow_hash_free_state();
+}
diff --git a/src/cryptonote_basic/connection_context.h b/src/cryptonote_basic/connection_context.h
index 51076e8c0..5e8f6685d 100644
--- a/src/cryptonote_basic/connection_context.h
+++ b/src/cryptonote_basic/connection_context.h
@@ -55,7 +55,7 @@ namespace cryptonote
};
state m_state;
- std::vector<crypto::hash> m_needed_objects;
+ std::vector<std::pair<crypto::hash, uint64_t>> m_needed_objects;
std::unordered_set<crypto::hash> m_requested_objects;
uint64_t m_remote_blockchain_height;
uint64_t m_last_response_height;
diff --git a/src/cryptonote_basic/cryptonote_basic.h b/src/cryptonote_basic/cryptonote_basic.h
index 055c4a22b..e2286ae8c 100644
--- a/src/cryptonote_basic/cryptonote_basic.h
+++ b/src/cryptonote_basic/cryptonote_basic.h
@@ -195,6 +195,7 @@ namespace cryptonote
private:
// hash cash
mutable std::atomic<bool> hash_valid;
+ mutable std::atomic<bool> prunable_hash_valid;
mutable std::atomic<bool> blob_size_valid;
public:
@@ -203,6 +204,7 @@ namespace cryptonote
// hash cash
mutable crypto::hash hash;
+ mutable crypto::hash prunable_hash;
mutable size_t blob_size;
bool pruned;
@@ -211,22 +213,26 @@ namespace cryptonote
std::atomic<unsigned int> prefix_size;
transaction();
- transaction(const transaction &t): transaction_prefix(t), hash_valid(false), blob_size_valid(false), signatures(t.signatures), rct_signatures(t.rct_signatures), pruned(t.pruned), unprunable_size(t.unprunable_size.load()), prefix_size(t.prefix_size.load()) { if (t.is_hash_valid()) { hash = t.hash; set_hash_valid(true); } if (t.is_blob_size_valid()) { blob_size = t.blob_size; set_blob_size_valid(true); } }
- transaction &operator=(const transaction &t) { transaction_prefix::operator=(t); set_hash_valid(false); set_blob_size_valid(false); signatures = t.signatures; rct_signatures = t.rct_signatures; if (t.is_hash_valid()) { hash = t.hash; set_hash_valid(true); } if (t.is_blob_size_valid()) { blob_size = t.blob_size; set_blob_size_valid(true); } pruned = t.pruned; unprunable_size = t.unprunable_size.load(); prefix_size = t.prefix_size.load(); return *this; }
+ transaction(const transaction &t);
+ transaction &operator=(const transaction &t);
virtual ~transaction();
void set_null();
void invalidate_hashes();
bool is_hash_valid() const { return hash_valid.load(std::memory_order_acquire); }
void set_hash_valid(bool v) const { hash_valid.store(v,std::memory_order_release); }
+ bool is_prunable_hash_valid() const { return prunable_hash_valid.load(std::memory_order_acquire); }
+ void set_prunable_hash_valid(bool v) const { prunable_hash_valid.store(v,std::memory_order_release); }
bool is_blob_size_valid() const { return blob_size_valid.load(std::memory_order_acquire); }
void set_blob_size_valid(bool v) const { blob_size_valid.store(v,std::memory_order_release); }
- void set_hash(const crypto::hash &h) { hash = h; set_hash_valid(true); }
- void set_blob_size(size_t sz) { blob_size = sz; set_blob_size_valid(true); }
+ void set_hash(const crypto::hash &h) const { hash = h; set_hash_valid(true); }
+ void set_prunable_hash(const crypto::hash &h) const { prunable_hash = h; set_prunable_hash_valid(true); }
+ void set_blob_size(size_t sz) const { blob_size = sz; set_blob_size_valid(true); }
BEGIN_SERIALIZE_OBJECT()
if (!typename Archive<W>::is_saving())
{
set_hash_valid(false);
+ set_prunable_hash_valid(false);
set_blob_size_valid(false);
}
@@ -327,6 +333,63 @@ namespace cryptonote
static size_t get_signature_size(const txin_v& tx_in);
};
+ inline transaction::transaction(const transaction &t):
+ transaction_prefix(t),
+ hash_valid(false),
+ prunable_hash_valid(false),
+ blob_size_valid(false),
+ signatures(t.signatures),
+ rct_signatures(t.rct_signatures),
+ pruned(t.pruned),
+ unprunable_size(t.unprunable_size.load()),
+ prefix_size(t.prefix_size.load())
+ {
+ if (t.is_hash_valid())
+ {
+ hash = t.hash;
+ set_hash_valid(true);
+ }
+ if (t.is_blob_size_valid())
+ {
+ blob_size = t.blob_size;
+ set_blob_size_valid(true);
+ }
+ if (t.is_prunable_hash_valid())
+ {
+ prunable_hash = t.prunable_hash;
+ set_prunable_hash_valid(true);
+ }
+ }
+
+ inline transaction &transaction::operator=(const transaction &t)
+ {
+ transaction_prefix::operator=(t);
+
+ set_hash_valid(false);
+ set_prunable_hash_valid(false);
+ set_blob_size_valid(false);
+ signatures = t.signatures;
+ rct_signatures = t.rct_signatures;
+ if (t.is_hash_valid())
+ {
+ hash = t.hash;
+ set_hash_valid(true);
+ }
+ if (t.is_prunable_hash_valid())
+ {
+ prunable_hash = t.prunable_hash;
+ set_prunable_hash_valid(true);
+ }
+ if (t.is_blob_size_valid())
+ {
+ blob_size = t.blob_size;
+ set_blob_size_valid(true);
+ }
+ pruned = t.pruned;
+ unprunable_size = t.unprunable_size.load();
+ prefix_size = t.prefix_size.load();
+ return *this;
+ }
inline
transaction::transaction()
@@ -346,6 +409,7 @@ namespace cryptonote
signatures.clear();
rct_signatures.type = rct::RCTTypeNull;
set_hash_valid(false);
+ set_prunable_hash_valid(false);
set_blob_size_valid(false);
pruned = false;
unprunable_size = 0;
@@ -356,6 +420,7 @@ namespace cryptonote
void transaction::invalidate_hashes()
{
set_hash_valid(false);
+ set_prunable_hash_valid(false);
set_blob_size_valid(false);
}
@@ -408,6 +473,7 @@ namespace cryptonote
void invalidate_hashes() { set_hash_valid(false); }
bool is_hash_valid() const { return hash_valid.load(std::memory_order_acquire); }
void set_hash_valid(bool v) const { hash_valid.store(v,std::memory_order_release); }
+ void set_hash(const crypto::hash &h) const { hash = h; set_hash_valid(true); }
transaction miner_tx;
std::vector<crypto::hash> tx_hashes;
diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp
index 7d7de416d..3501c66c8 100644
--- a/src/cryptonote_basic/cryptonote_format_utils.cpp
+++ b/src/cryptonote_basic/cryptonote_format_utils.cpp
@@ -1011,7 +1011,19 @@ namespace cryptonote
crypto::hash get_transaction_prunable_hash(const transaction& t, const cryptonote::blobdata *blobdata)
{
crypto::hash res;
+ if (t.is_prunable_hash_valid())
+ {
+#ifdef ENABLE_HASH_CASH_INTEGRITY_CHECK
+ CHECK_AND_ASSERT_THROW_MES(!calculate_transaction_prunable_hash(t, blobdata, res) || t.hash == res, "tx hash cash integrity failure");
+#endif
+ res = t.prunable_hash;
+ ++tx_hashes_cached_count;
+ return res;
+ }
+
+ ++tx_hashes_calculated_count;
CHECK_AND_ASSERT_THROW_MES(calculate_transaction_prunable_hash(t, blobdata, res), "Failed to calculate tx prunable hash");
+ t.set_prunable_hash(res);
return res;
}
//---------------------------------------------------------------
@@ -1047,11 +1059,14 @@ namespace cryptonote
// the tx hash is the hash of the 3 hashes
crypto::hash res = cn_fast_hash(hashes, sizeof(hashes));
+ t.set_hash(res);
return res;
}
//---------------------------------------------------------------
bool calculate_transaction_hash(const transaction& t, crypto::hash& res, size_t* blob_size)
{
+ CHECK_AND_ASSERT_MES(!t.pruned, false, "Cannot calculate the hash of a pruned transaction");
+
// v1 transactions hash the entire blob
if (t.version == 1)
{
@@ -1091,8 +1106,7 @@ namespace cryptonote
{
if (!t.is_blob_size_valid())
{
- t.blob_size = blob.size();
- t.set_blob_size_valid(true);
+ t.set_blob_size(blob.size());
}
*blob_size = t.blob_size;
}
@@ -1112,8 +1126,7 @@ namespace cryptonote
{
if (!t.is_blob_size_valid())
{
- t.blob_size = get_object_blobsize(t);
- t.set_blob_size_valid(true);
+ t.set_blob_size(get_object_blobsize(t));
}
*blob_size = t.blob_size;
}
@@ -1124,12 +1137,10 @@ namespace cryptonote
bool ret = calculate_transaction_hash(t, res, blob_size);
if (!ret)
return false;
- t.hash = res;
- t.set_hash_valid(true);
+ t.set_hash(res);
if (blob_size)
{
- t.blob_size = *blob_size;
- t.set_blob_size_valid(true);
+ t.set_blob_size(*blob_size);
}
return true;
}
@@ -1206,8 +1217,7 @@ namespace cryptonote
bool ret = calculate_block_hash(b, res);
if (!ret)
return false;
- b.hash = res;
- b.set_hash_valid(true);
+ b.set_hash(res);
return true;
}
//---------------------------------------------------------------
@@ -1218,21 +1228,6 @@ namespace cryptonote
return p;
}
//---------------------------------------------------------------
- bool get_block_longhash(const block& b, crypto::hash& res, uint64_t height)
- {
- // block 202612 bug workaround
- if (height == 202612)
- {
- static const std::string longhash_202612 = "84f64766475d51837ac9efbef1926486e58563c95a19fef4aec3254f03000000";
- string_tools::hex_to_pod(longhash_202612, res);
- return true;
- }
- blobdata bd = get_block_hashing_blob(b);
- const int cn_variant = b.major_version >= 7 ? b.major_version - 6 : 0;
- crypto::cn_slow_hash(bd.data(), bd.size(), res, cn_variant, height);
- return true;
- }
- //---------------------------------------------------------------
std::vector<uint64_t> relative_output_offsets_to_absolute(const std::vector<uint64_t>& off)
{
std::vector<uint64_t> res = off;
@@ -1253,13 +1248,6 @@ namespace cryptonote
return res;
}
//---------------------------------------------------------------
- crypto::hash get_block_longhash(const block& b, uint64_t height)
- {
- crypto::hash p = null_hash;
- get_block_longhash(b, p, height);
- return p;
- }
- //---------------------------------------------------------------
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b, crypto::hash *block_hash)
{
std::stringstream ss;
@@ -1273,8 +1261,7 @@ namespace cryptonote
{
calculate_block_hash(b, *block_hash, &b_blob);
++block_hashes_calculated_count;
- b.hash = *block_hash;
- b.set_hash_valid(true);
+ b.set_hash(*block_hash);
}
return true;
}
diff --git a/src/cryptonote_basic/cryptonote_format_utils.h b/src/cryptonote_basic/cryptonote_format_utils.h
index c9de2a56e..284494299 100644
--- a/src/cryptonote_basic/cryptonote_format_utils.h
+++ b/src/cryptonote_basic/cryptonote_format_utils.h
@@ -117,8 +117,6 @@ namespace cryptonote
bool calculate_block_hash(const block& b, crypto::hash& res, const blobdata *blob = NULL);
bool get_block_hash(const block& b, crypto::hash& res);
crypto::hash get_block_hash(const block& b);
- bool get_block_longhash(const block& b, crypto::hash& res, uint64_t height);
- crypto::hash get_block_longhash(const block& b, uint64_t height);
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b, crypto::hash *block_hash);
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b);
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b, crypto::hash &block_hash);
diff --git a/src/cryptonote_basic/hardfork.cpp b/src/cryptonote_basic/hardfork.cpp
index 98158a513..dfeca27b4 100644
--- a/src/cryptonote_basic/hardfork.cpp
+++ b/src/cryptonote_basic/hardfork.cpp
@@ -87,7 +87,7 @@ bool HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold, tim
}
if (threshold > 100)
return false;
- heights.push_back(Params(version, height, threshold, time));
+ heights.push_back(hardfork_t(version, height, threshold, time));
return true;
}
@@ -171,7 +171,7 @@ void HardFork::init()
// add a placeholder for the default version, to avoid special cases
if (heights.empty())
- heights.push_back(Params(original_version, 0, 0, 0));
+ heights.push_back(hardfork_t(original_version, 0, 0, 0));
versions.clear();
for (size_t n = 0; n < 256; ++n)
diff --git a/src/cryptonote_basic/hardfork.h b/src/cryptonote_basic/hardfork.h
index 123978b12..987dcc75a 100644
--- a/src/cryptonote_basic/hardfork.h
+++ b/src/cryptonote_basic/hardfork.h
@@ -29,6 +29,7 @@
#pragma once
#include "syncobj.h"
+#include "hardforks/hardforks.h"
#include "cryptonote_basic/cryptonote_basic.h"
namespace cryptonote
@@ -230,14 +231,6 @@ namespace cryptonote
*/
uint64_t get_window_size() const { return window_size; }
- struct Params {
- uint8_t version;
- uint8_t threshold;
- uint64_t height;
- time_t time;
- Params(uint8_t version, uint64_t height, uint8_t threshold, time_t time): version(version), threshold(threshold), height(height), time(time) {}
- };
-
private:
uint8_t get_block_version(uint64_t height) const;
@@ -262,7 +255,7 @@ namespace cryptonote
uint8_t original_version;
uint64_t original_version_till_height;
- std::vector<Params> heights;
+ std::vector<hardfork_t> heights;
std::deque<uint8_t> versions; /* rolling window of the last N blocks' versions */
unsigned int last_versions[256]; /* count of the block versions in the last N blocks */
diff --git a/src/cryptonote_basic/miner.cpp b/src/cryptonote_basic/miner.cpp
index 2dad2795e..c4b5c8455 100644
--- a/src/cryptonote_basic/miner.cpp
+++ b/src/cryptonote_basic/miner.cpp
@@ -30,15 +30,16 @@
#include <sstream>
#include <numeric>
-#include <boost/utility/value_init.hpp>
#include <boost/interprocess/detail/atomic.hpp>
#include <boost/algorithm/string.hpp>
#include "misc_language.h"
#include "syncobj.h"
#include "cryptonote_basic_impl.h"
#include "cryptonote_format_utils.h"
+#include "cryptonote_core/cryptonote_tx_utils.h"
#include "file_io_utils.h"
#include "common/command_line.h"
+#include "common/util.h"
#include "string_coding.h"
#include "string_tools.h"
#include "storages/portable_storage_template_helper.h"
@@ -61,7 +62,9 @@
#include <devstat.h>
#include <errno.h>
#include <fcntl.h>
+#if defined(__amd64__) || defined(__i386__) || defined(__x86_64__)
#include <machine/apm_bios.h>
+#endif
#include <stdio.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
@@ -99,12 +102,13 @@ namespace cryptonote
}
- miner::miner(i_miner_handler* phandler):m_stop(1),
- m_template(boost::value_initialized<block>()),
+ miner::miner(i_miner_handler* phandler, Blockchain* pbc):m_stop(1),
+ m_template{},
m_template_no(0),
m_diffic(0),
m_thread_index(0),
m_phandler(phandler),
+ m_pbc(pbc),
m_height(0),
m_threads_active(0),
m_pausers_count(0),
@@ -430,6 +434,7 @@ namespace cryptonote
{
boost::interprocess::ipcdetail::atomic_write32(&m_stop, 1);
}
+ extern "C" void rx_stop_mining(void);
//-----------------------------------------------------------------------------------------------------
bool miner::stop()
{
@@ -462,15 +467,16 @@ namespace cryptonote
MINFO("Mining has been stopped, " << m_threads.size() << " finished" );
m_threads.clear();
m_threads_autodetect.clear();
+ rx_stop_mining();
return true;
}
//-----------------------------------------------------------------------------------------------------
- bool miner::find_nonce_for_given_block(block& bl, const difficulty_type& diffic, uint64_t height)
+ bool miner::find_nonce_for_given_block(const Blockchain *pbc, block& bl, const difficulty_type& diffic, uint64_t height)
{
for(; bl.nonce != std::numeric_limits<uint32_t>::max(); bl.nonce++)
{
crypto::hash h;
- get_block_longhash(bl, h, height);
+ get_block_longhash(pbc, bl, h, height, tools::get_max_concurrency());
if(check_hash(h, diffic))
{
@@ -566,7 +572,7 @@ namespace cryptonote
b.nonce = nonce;
crypto::hash h;
- get_block_longhash(b, h, height);
+ get_block_longhash(m_pbc, b, h, height, tools::get_max_concurrency());
if(check_hash(h, local_diff))
{
@@ -1082,6 +1088,7 @@ namespace cryptonote
return boost::logic::tribool(boost::logic::indeterminate);
}
+#if defined(__amd64__) || defined(__i386__) || defined(__x86_64__)
apm_info info;
if( ioctl(fd, APMIO_GETINFO, &info) == -1 ) {
close(fd);
@@ -1122,6 +1129,7 @@ namespace cryptonote
LOG_ERROR("sysctlbyname(\"hw.acpi.acline\") output is unexpectedly "
<< n << " bytes instead of the expected " << sizeof(ac) << " bytes.");
return boost::logic::tribool(boost::logic::indeterminate);
+#endif
}
return boost::logic::tribool(ac == 0);
#endif
diff --git a/src/cryptonote_basic/miner.h b/src/cryptonote_basic/miner.h
index ac7a0381c..4efbcbec3 100644
--- a/src/cryptonote_basic/miner.h
+++ b/src/cryptonote_basic/miner.h
@@ -52,13 +52,15 @@ namespace cryptonote
~i_miner_handler(){};
};
+ class Blockchain;
+
/************************************************************************/
/* */
/************************************************************************/
class miner
{
public:
- miner(i_miner_handler* phandler);
+ miner(i_miner_handler* phandler, Blockchain* pbc);
~miner();
bool init(const boost::program_options::variables_map& vm, network_type nettype);
static void init_options(boost::program_options::options_description& desc);
@@ -74,7 +76,7 @@ namespace cryptonote
bool on_idle();
void on_synchronized();
//synchronous analog (for fast calls)
- static bool find_nonce_for_given_block(block& bl, const difficulty_type& diffic, uint64_t height);
+ static bool find_nonce_for_given_block(const Blockchain *pbc, block& bl, const difficulty_type& diffic, uint64_t height);
void pause();
void resume();
void do_print_hashrate(bool do_hr);
@@ -133,6 +135,7 @@ namespace cryptonote
std::list<boost::thread> m_threads;
epee::critical_section m_threads_lock;
i_miner_handler* m_phandler;
+ Blockchain* m_pbc;
account_public_address m_mine_address;
epee::math_helper::once_a_time_seconds<5> m_update_block_template_interval;
epee::math_helper::once_a_time_seconds<2> m_update_merge_hr_interval;
diff --git a/src/cryptonote_basic/verification_context.h b/src/cryptonote_basic/verification_context.h
index 3d7200fae..f5f663464 100644
--- a/src/cryptonote_basic/verification_context.h
+++ b/src/cryptonote_basic/verification_context.h
@@ -58,5 +58,6 @@ namespace cryptonote
bool m_marked_as_orphaned;
bool m_already_exists;
bool m_partial_block_reward;
+ bool m_bad_pow; // if bad pow, bad peer outright for DoS protection
};
}
diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h
index 4147b48ee..69551934a 100644
--- a/src/cryptonote_config.h
+++ b/src/cryptonote_config.h
@@ -96,6 +96,7 @@
#define BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT 10000 //by default, blocks ids count in synchronizing
#define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4 100 //by default, blocks count in blocks downloading
#define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT 20 //by default, blocks count in blocks downloading
+#define BLOCKS_SYNCHRONIZING_MAX_COUNT 2048 //must be a power of 2, greater than 128, equal to SEEDHASH_EPOCH_BLOCKS
#define CRYPTONOTE_MEMPOOL_TX_LIVETIME (86400*3) //seconds, three days
#define CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME 604800 //seconds, one week
@@ -163,10 +164,11 @@
#define HF_VERSION_MIN_V2_COINBASE_TX 12
#define HF_VERSION_SAME_MIXIN 12
#define HF_VERSION_REJECT_SIGS_IN_COINBASE 12
+#define HF_VERSION_ENFORCE_MIN_AGE 12
#define PER_KB_FEE_QUANTIZATION_DECIMALS 8
-#define HASH_OF_HASHES_STEP 256
+#define HASH_OF_HASHES_STEP 512
#define DEFAULT_TXPOOL_MAX_WEIGHT 648000000ull // 3 days at 300000, in bytes
diff --git a/src/cryptonote_core/CMakeLists.txt b/src/cryptonote_core/CMakeLists.txt
index 2cbe89b01..cb3875878 100644
--- a/src/cryptonote_core/CMakeLists.txt
+++ b/src/cryptonote_core/CMakeLists.txt
@@ -58,6 +58,7 @@ target_link_libraries(cryptonote_core
multisig
ringct
device
+ hardforks
${Boost_DATE_TIME_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_SERIALIZATION_LIBRARY}
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index 833e8120c..b4942d03a 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -41,6 +41,7 @@
#include "cryptonote_basic/cryptonote_boost_serialization.h"
#include "cryptonote_config.h"
#include "cryptonote_basic/miner.h"
+#include "hardforks/hardforks.h"
#include "misc_language.h"
#include "profile_tools.h"
#include "file_io_utils.h"
@@ -83,95 +84,6 @@ DISABLE_VS_WARNINGS(4267)
// used to overestimate the block reward when estimating a per kB to use
#define BLOCK_REWARD_OVERESTIMATE (10 * 1000000000000)
-static const struct {
- uint8_t version;
- uint64_t height;
- uint8_t threshold;
- time_t time;
-} mainnet_hard_forks[] = {
- // version 1 from the start of the blockchain
- { 1, 1, 0, 1341378000 },
-
- // version 2 starts from block 1009827, which is on or around the 20th of March, 2016. Fork time finalised on 2015-09-20. No fork voting occurs for the v2 fork.
- { 2, 1009827, 0, 1442763710 },
-
- // version 3 starts from block 1141317, which is on or around the 24th of September, 2016. Fork time finalised on 2016-03-21.
- { 3, 1141317, 0, 1458558528 },
-
- // version 4 starts from block 1220516, which is on or around the 5th of January, 2017. Fork time finalised on 2016-09-18.
- { 4, 1220516, 0, 1483574400 },
-
- // version 5 starts from block 1288616, which is on or around the 15th of April, 2017. Fork time finalised on 2017-03-14.
- { 5, 1288616, 0, 1489520158 },
-
- // version 6 starts from block 1400000, which is on or around the 16th of September, 2017. Fork time finalised on 2017-08-18.
- { 6, 1400000, 0, 1503046577 },
-
- // version 7 starts from block 1546000, which is on or around the 6th of April, 2018. Fork time finalised on 2018-03-17.
- { 7, 1546000, 0, 1521303150 },
-
- // version 8 starts from block 1685555, which is on or around the 18th of October, 2018. Fork time finalised on 2018-09-02.
- { 8, 1685555, 0, 1535889547 },
-
- // version 9 starts from block 1686275, which is on or around the 19th of October, 2018. Fork time finalised on 2018-09-02.
- { 9, 1686275, 0, 1535889548 },
-
- // version 10 starts from block 1788000, which is on or around the 9th of March, 2019. Fork time finalised on 2019-02-10.
- { 10, 1788000, 0, 1549792439 },
-
- // version 11 starts from block 1788720, which is on or around the 10th of March, 2019. Fork time finalised on 2019-02-15.
- { 11, 1788720, 0, 1550225678 },
-};
-static const uint64_t mainnet_hard_fork_version_1_till = 1009826;
-
-static const struct {
- uint8_t version;
- uint64_t height;
- uint8_t threshold;
- time_t time;
-} testnet_hard_forks[] = {
- // version 1 from the start of the blockchain
- { 1, 1, 0, 1341378000 },
-
- // version 2 starts from block 624634, which is on or around the 23rd of November, 2015. Fork time finalised on 2015-11-20. No fork voting occurs for the v2 fork.
- { 2, 624634, 0, 1445355000 },
-
- // versions 3-5 were passed in rapid succession from September 18th, 2016
- { 3, 800500, 0, 1472415034 },
- { 4, 801219, 0, 1472415035 },
- { 5, 802660, 0, 1472415036 + 86400*180 }, // add 5 months on testnet to shut the update warning up since there's a large gap to v6
-
- { 6, 971400, 0, 1501709789 },
- { 7, 1057027, 0, 1512211236 },
- { 8, 1057058, 0, 1533211200 },
- { 9, 1057778, 0, 1533297600 },
- { 10, 1154318, 0, 1550153694 },
- { 11, 1155038, 0, 1550225678 },
-};
-static const uint64_t testnet_hard_fork_version_1_till = 624633;
-
-static const struct {
- uint8_t version;
- uint64_t height;
- uint8_t threshold;
- time_t time;
-} stagenet_hard_forks[] = {
- // version 1 from the start of the blockchain
- { 1, 1, 0, 1341378000 },
-
- // versions 2-7 in rapid succession from March 13th, 2018
- { 2, 32000, 0, 1521000000 },
- { 3, 33000, 0, 1521120000 },
- { 4, 34000, 0, 1521240000 },
- { 5, 35000, 0, 1521360000 },
- { 6, 36000, 0, 1521480000 },
- { 7, 37000, 0, 1521600000 },
- { 8, 176456, 0, 1537821770 },
- { 9, 177176, 0, 1537821771 },
- { 10, 269000, 0, 1550153694 },
- { 11, 269720, 0, 1550225678 },
-};
-
//------------------------------------------------------------------
Blockchain::Blockchain(tx_memory_pool& tx_pool) :
m_db(), m_tx_pool(tx_pool), m_hardfork(NULL), m_timestamps_and_difficulties_height(0), m_current_block_cumul_weight_limit(0), m_current_block_cumul_weight_median(0),
@@ -183,7 +95,8 @@ Blockchain::Blockchain(tx_memory_pool& tx_pool) :
m_difficulty_for_next_block_top_hash(crypto::null_hash),
m_difficulty_for_next_block(1),
m_btc_valid(false),
- m_batch_success(true)
+ m_batch_success(true),
+ m_prepare_height(0)
{
LOG_PRINT_L3("Blockchain::" << __func__);
}
@@ -403,17 +316,17 @@ bool Blockchain::init(BlockchainDB* db, const network_type nettype, bool offline
}
else if (m_nettype == TESTNET)
{
- for (size_t n = 0; n < sizeof(testnet_hard_forks) / sizeof(testnet_hard_forks[0]); ++n)
+ for (size_t n = 0; n < num_testnet_hard_forks; ++n)
m_hardfork->add_fork(testnet_hard_forks[n].version, testnet_hard_forks[n].height, testnet_hard_forks[n].threshold, testnet_hard_forks[n].time);
}
else if (m_nettype == STAGENET)
{
- for (size_t n = 0; n < sizeof(stagenet_hard_forks) / sizeof(stagenet_hard_forks[0]); ++n)
+ for (size_t n = 0; n < num_stagenet_hard_forks; ++n)
m_hardfork->add_fork(stagenet_hard_forks[n].version, stagenet_hard_forks[n].height, stagenet_hard_forks[n].threshold, stagenet_hard_forks[n].time);
}
else
{
- for (size_t n = 0; n < sizeof(mainnet_hard_forks) / sizeof(mainnet_hard_forks[0]); ++n)
+ for (size_t n = 0; n < num_mainnet_hard_forks; ++n)
m_hardfork->add_fork(mainnet_hard_forks[n].version, mainnet_hard_forks[n].height, mainnet_hard_forks[n].threshold, mainnet_hard_forks[n].time);
}
m_hardfork->init();
@@ -428,7 +341,7 @@ bool Blockchain::init(BlockchainDB* db, const network_type nettype, bool offline
{
MINFO("Blockchain not loaded, generating genesis block.");
block bl;
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
generate_genesis_block(bl, get_config(m_nettype).GENESIS_TX, get_config(m_nettype).GENESIS_NONCE);
db_wtxn_guard wtxn_guard(m_db);
add_new_block(bl, bvc);
@@ -737,7 +650,7 @@ bool Blockchain::reset_and_set_genesis_block(const block& b)
m_hardfork->init();
db_wtxn_guard wtxn_guard(m_db);
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
add_new_block(b, bvc);
if (!update_next_cumulative_weight_limit())
return false;
@@ -842,6 +755,13 @@ crypto::hash Blockchain::get_block_id_by_height(uint64_t height) const
return null_hash;
}
//------------------------------------------------------------------
+crypto::hash Blockchain::get_pending_block_id_by_height(uint64_t height) const
+{
+ if (m_prepare_height && height >= m_prepare_height && height - m_prepare_height < m_prepare_nblocks)
+ return (*m_prepare_blocks)[height - m_prepare_height].hash;
+ return get_block_id_by_height(height);
+}
+//------------------------------------------------------------------
bool Blockchain::get_block_by_hash(const crypto::hash &h, block &blk, bool *orphan) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
@@ -1007,7 +927,7 @@ bool Blockchain::rollback_blockchain_switching(std::list<block>& original_chain,
//return back original chain
for (auto& bl : original_chain)
{
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
bool r = handle_block_to_main_chain(bl, bvc);
CHECK_AND_ASSERT_MES(r && bvc.m_added_to_main_chain, false, "PANIC! failed to add (again) block while chain switching during the rollback!");
}
@@ -1056,7 +976,7 @@ bool Blockchain::switch_to_alternative_blockchain(std::list<block_extended_info>
for(auto alt_ch_iter = alt_chain.begin(); alt_ch_iter != alt_chain.end(); alt_ch_iter++)
{
const auto &bei = *alt_ch_iter;
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
// add block to main chain
bool r = handle_block_to_main_chain(bei.bl, bvc);
@@ -1099,7 +1019,7 @@ bool Blockchain::switch_to_alternative_blockchain(std::list<block_extended_info>
//pushing old chain as alternative chain
for (auto& old_ch_ent : disconnected_chain)
{
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
bool r = handle_alternative_block(old_ch_ent, get_block_hash(old_ch_ent), bvc);
if(!r)
{
@@ -1117,6 +1037,7 @@ bool Blockchain::switch_to_alternative_blockchain(std::list<block_extended_info>
}
m_hardfork->reorganize_from_chain_height(split_height);
+ get_block_longhash_reorg(split_height);
std::shared_ptr<tools::Notify> reorg_notify = m_reorg_notify;
if (reorg_notify)
@@ -1264,7 +1185,7 @@ bool Blockchain::validate_miner_transaction(const block& b, size_t cumulative_bl
}
if(base_reward + fee < money_in_use)
{
- MERROR_VER("coinbase transaction spend too much money (" << print_money(money_in_use) << "). Block reward is " << print_money(base_reward + fee) << "(" << print_money(base_reward) << "+" << print_money(fee) << ")");
+ MERROR_VER("coinbase transaction spend too much money (" << print_money(money_in_use) << "). Block reward is " << print_money(base_reward + fee) << "(" << print_money(base_reward) << "+" << print_money(fee) << "), cumulative_block_weight " << cumulative_block_weight);
return false;
}
// From hard fork 2, we allow a miner to claim less block reward than is allowed, in case a miner wants less dust
@@ -1394,7 +1315,9 @@ bool Blockchain::create_block_template(block& b, const crypto::hash *from_block,
if (!memcmp(&miner_address, &m_btc_address, sizeof(cryptonote::account_public_address)) && m_btc_nonce == ex_nonce
&& m_btc_pool_cookie == m_tx_pool.cookie() && m_btc.prev_id == get_tail_id()) {
MDEBUG("Using cached template");
- m_btc.timestamp = time(NULL); // update timestamp unconditionally
+ const uint64_t now = time(NULL);
+ if (m_btc.timestamp < now) // ensures it can't get below the median of the last few blocks
+ m_btc.timestamp = now;
b = m_btc;
diffic = m_btc_difficulty;
height = m_btc_height;
@@ -1421,7 +1344,7 @@ bool Blockchain::create_block_template(block& b, const crypto::hash *from_block,
//we have new block in alternative chain
std::list<block_extended_info> alt_chain;
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
std::vector<uint64_t> timestamps;
if (!build_alt_chain(*from_block, alt_chain, timestamps, bvc))
return false;
@@ -1455,7 +1378,7 @@ bool Blockchain::create_block_template(block& b, const crypto::hash *from_block,
}
// FIXME: consider moving away from block_extended_info at some point
- block_extended_info bei = boost::value_initialized<block_extended_info>();
+ block_extended_info bei = {};
bei.bl = b;
bei.height = alt_chain.size() ? prev_data.height + 1 : m_db->get_block_height(*from_block) + 1;
@@ -1743,7 +1666,7 @@ bool Blockchain::handle_alternative_block(const block& b, const crypto::hash& id
return false;
// FIXME: consider moving away from block_extended_info at some point
- block_extended_info bei = boost::value_initialized<block_extended_info>();
+ block_extended_info bei = {};
bei.bl = b;
const uint64_t prev_height = alt_chain.size() ? prev_data.height : m_db->get_block_height(b.prev_id);
bei.height = prev_height + 1;
@@ -1772,11 +1695,35 @@ bool Blockchain::handle_alternative_block(const block& b, const crypto::hash& id
difficulty_type current_diff = get_next_difficulty_for_alternative_chain(alt_chain, bei);
CHECK_AND_ASSERT_MES(current_diff, false, "!!!!!!! DIFFICULTY OVERHEAD !!!!!!!");
crypto::hash proof_of_work = null_hash;
- get_block_longhash(bei.bl, proof_of_work, bei.height);
+ if (b.major_version >= RX_BLOCK_VERSION)
+ {
+ crypto::hash seedhash = null_hash;
+ uint64_t seedheight = rx_seedheight(bei.height);
+ // seedblock is on the alt chain somewhere
+ if (alt_chain.size() && alt_chain.front().height <= seedheight)
+ {
+ for (auto it=alt_chain.begin(); it != alt_chain.end(); it++)
+ {
+ if (it->height == seedheight+1)
+ {
+ seedhash = it->bl.prev_id;
+ break;
+ }
+ }
+ } else
+ {
+ seedhash = get_block_id_by_height(seedheight);
+ }
+ get_altblock_longhash(bei.bl, proof_of_work, get_current_blockchain_height(), bei.height, seedheight, seedhash);
+ } else
+ {
+ get_block_longhash(this, bei.bl, proof_of_work, bei.height, 0);
+ }
if(!check_hash(proof_of_work, current_diff))
{
MERROR_VER("Block with id: " << id << std::endl << " for alternative chain, does not have enough proof of work: " << proof_of_work << std::endl << " expected difficulty: " << current_diff);
bvc.m_verifivation_failed = true;
+ bvc.m_bad_pow = true;
return false;
}
@@ -1945,8 +1892,9 @@ bool Blockchain::handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NO
std::vector<std::pair<cryptonote::blobdata,block>> blocks;
get_blocks(arg.blocks, blocks, rsp.missed_ids);
- for (auto& bl: blocks)
+ for (size_t i = 0; i < blocks.size(); ++i)
{
+ auto& bl = blocks[i];
std::vector<crypto::hash> missed_tx_ids;
rsp.blocks.push_back(block_complete_entry());
@@ -1954,8 +1902,8 @@ bool Blockchain::handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NO
// FIXME: s/rsp.missed_ids/missed_tx_id/ ? Seems like rsp.missed_ids
// is for missed blocks, not missed transactions as well.
- get_transactions_blobs(bl.second.tx_hashes, e.txs, missed_tx_ids);
-
+ e.pruned = arg.prune;
+ get_transactions_blobs(bl.second.tx_hashes, e.txs, missed_tx_ids, arg.prune);
if (missed_tx_ids.size() != 0)
{
// do not display an error if the peer asked for an unpruned block which we are not meant to have
@@ -1976,6 +1924,9 @@ bool Blockchain::handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NO
//pack block
e.block = std::move(bl.first);
+ e.block_weight = 0;
+ if (arg.prune && m_db->block_exists(arg.blocks[i]))
+ e.block_weight = m_db->get_block_weight(m_db->get_block_height(arg.blocks[i]));
}
return true;
@@ -2254,23 +2205,95 @@ bool Blockchain::get_blocks(const t_ids_container& block_ids, t_blocks_container
return true;
}
//------------------------------------------------------------------
+static bool fill(BlockchainDB *db, const crypto::hash &tx_hash, cryptonote::blobdata &tx, bool pruned)
+{
+ if (pruned)
+ {
+ if (!db->get_pruned_tx_blob(tx_hash, tx))
+ {
+ MDEBUG("Pruned transaction blob not found for " << tx_hash);
+ return false;
+ }
+ }
+ else
+ {
+ if (!db->get_tx_blob(tx_hash, tx))
+ {
+ MDEBUG("Transaction blob not found for " << tx_hash);
+ return false;
+ }
+ }
+ return true;
+}
+//------------------------------------------------------------------
+static bool fill(BlockchainDB *db, const crypto::hash &tx_hash, tx_blob_entry &tx, bool pruned)
+{
+ if (!fill(db, tx_hash, tx.blob, pruned))
+ return false;
+ if (pruned)
+ {
+ if (is_v1_tx(tx.blob))
+ {
+ // v1 txes aren't pruned, so fetch the whole thing
+ cryptonote::blobdata prunable_blob;
+ if (!db->get_prunable_tx_blob(tx_hash, prunable_blob))
+ {
+ MDEBUG("Prunable transaction blob not found for " << tx_hash);
+ return false;
+ }
+ tx.blob.append(prunable_blob);
+ tx.prunable_hash = crypto::null_hash;
+ }
+ else
+ {
+ if (!db->get_prunable_tx_hash(tx_hash, tx.prunable_hash))
+ {
+ MDEBUG("Prunable transaction data hash not found for " << tx_hash);
+ return false;
+ }
+ }
+ }
+ return true;
+}
+//------------------------------------------------------------------
//TODO: return type should be void, throw on exception
// alternatively, return true only if no transactions missed
-template<class t_ids_container, class t_tx_container, class t_missed_container>
-bool Blockchain::get_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs, bool pruned) const
+bool Blockchain::get_transactions_blobs(const std::vector<crypto::hash>& txs_ids, std::vector<cryptonote::blobdata>& txs, std::vector<crypto::hash>& missed_txs, bool pruned) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);
- reserve_container(txs, txs_ids.size());
+ txs.reserve(txs_ids.size());
for (const auto& tx_hash : txs_ids)
{
try
{
cryptonote::blobdata tx;
- if (pruned && m_db->get_pruned_tx_blob(tx_hash, tx))
+ if (fill(m_db, tx_hash, tx, pruned))
txs.push_back(std::move(tx));
- else if (!pruned && m_db->get_tx_blob(tx_hash, tx))
+ else
+ missed_txs.push_back(tx_hash);
+ }
+ catch (const std::exception& e)
+ {
+ return false;
+ }
+ }
+ return true;
+}
+//------------------------------------------------------------------
+bool Blockchain::get_transactions_blobs(const std::vector<crypto::hash>& txs_ids, std::vector<tx_blob_entry>& txs, std::vector<crypto::hash>& missed_txs, bool pruned) const
+{
+ LOG_PRINT_L3("Blockchain::" << __func__);
+ CRITICAL_REGION_LOCAL(m_blockchain_lock);
+
+ txs.reserve(txs_ids.size());
+ for (const auto& tx_hash : txs_ids)
+ {
+ try
+ {
+ tx_blob_entry tx;
+ if (fill(m_db, tx_hash, tx, pruned))
txs.push_back(std::move(tx));
else
missed_txs.push_back(tx_hash);
@@ -2363,7 +2386,7 @@ bool Blockchain::get_transactions(const t_ids_container& txs_ids, t_tx_container
// Find the split point between us and foreign blockchain and return
// (by reference) the most recent common block hash along with up to
// BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT additional (more recent) hashes.
-bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::vector<crypto::hash>& hashes, uint64_t& start_height, uint64_t& current_height, bool clip_pruned) const
+bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::vector<crypto::hash>& hashes, std::vector<uint64_t>* weights, uint64_t& start_height, uint64_t& current_height, bool clip_pruned) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);
@@ -2380,25 +2403,34 @@ bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qbloc
if (clip_pruned)
{
const uint32_t pruning_seed = get_blockchain_pruning_seed();
- start_height = tools::get_next_unpruned_block_height(start_height, current_height, pruning_seed);
+ if (start_height < tools::get_next_unpruned_block_height(start_height, current_height, pruning_seed))
+ {
+ MDEBUG("We only have a pruned version of the common ancestor");
+ return false;
+ }
stop_height = tools::get_next_pruned_block_height(start_height, current_height, pruning_seed);
}
size_t count = 0;
- hashes.reserve(std::min((size_t)(stop_height - start_height), (size_t)BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT));
+ const size_t reserve = std::min((size_t)(stop_height - start_height), (size_t)BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT);
+ hashes.reserve(reserve);
+ if (weights)
+ weights->reserve(reserve);
for(size_t i = start_height; i < stop_height && count < BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT; i++, count++)
{
hashes.push_back(m_db->get_block_hash_from_height(i));
+ if (weights)
+ weights->push_back(m_db->get_block_weight(i));
}
return true;
}
-bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const
+bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, bool clip_pruned, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);
- bool result = find_blockchain_supplement(qblock_ids, resp.m_block_ids, resp.start_height, resp.total_height, true);
+ bool result = find_blockchain_supplement(qblock_ids, resp.m_block_ids, &resp.m_block_weights, resp.start_height, resp.total_height, clip_pruned);
if (result)
{
cryptonote::difficulty_type wide_cumulative_difficulty = m_db->get_block_cumulative_difficulty(resp.total_height - 1);
@@ -2837,18 +2869,24 @@ bool Blockchain::expand_transaction_2(transaction &tx, const crypto::hash &tx_pr
// II
if (rv.type == rct::RCTTypeFull)
{
- rv.p.MGs.resize(1);
- rv.p.MGs[0].II.resize(tx.vin.size());
- for (size_t n = 0; n < tx.vin.size(); ++n)
- rv.p.MGs[0].II[n] = rct::ki2rct(boost::get<txin_to_key>(tx.vin[n]).k_image);
+ if (!tx.pruned)
+ {
+ rv.p.MGs.resize(1);
+ rv.p.MGs[0].II.resize(tx.vin.size());
+ for (size_t n = 0; n < tx.vin.size(); ++n)
+ rv.p.MGs[0].II[n] = rct::ki2rct(boost::get<txin_to_key>(tx.vin[n]).k_image);
+ }
}
else if (rv.type == rct::RCTTypeSimple || rv.type == rct::RCTTypeBulletproof || rv.type == rct::RCTTypeBulletproof2)
{
- CHECK_AND_ASSERT_MES(rv.p.MGs.size() == tx.vin.size(), false, "Bad MGs size");
- for (size_t n = 0; n < tx.vin.size(); ++n)
+ if (!tx.pruned)
{
- rv.p.MGs[n].II.resize(1);
- rv.p.MGs[n].II[0] = rct::ki2rct(boost::get<txin_to_key>(tx.vin[n]).k_image);
+ CHECK_AND_ASSERT_MES(rv.p.MGs.size() == tx.vin.size(), false, "Bad MGs size");
+ for (size_t n = 0; n < tx.vin.size(); ++n)
+ {
+ rv.p.MGs[n].II.resize(1);
+ rv.p.MGs[n].II[0] = rct::ki2rct(boost::get<txin_to_key>(tx.vin[n]).k_image);
+ }
}
}
else
@@ -2874,6 +2912,10 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
if(pmax_used_block_height)
*pmax_used_block_height = 0;
+ // pruned txes are skipped, as they're only allowed in sync-pruned-blocks mode, which is within the builtin hashes
+ if (tx.pruned)
+ return true;
+
crypto::hash tx_prefix_hash = get_transaction_prefix_hash(tx);
const uint8_t hf_version = m_hardfork->get_current_version();
@@ -3010,6 +3052,9 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
const auto waiter_guard = epee::misc_utils::create_scope_leave_handler([&]() { waiter.wait(&tpool); });
int threads = tpool.get_max_concurrency();
+ uint64_t max_used_block_height = 0;
+ if (!pmax_used_block_height)
+ pmax_used_block_height = &max_used_block_height;
for (const auto& txin : tx.vin)
{
// make sure output being spent is of type txin_to_key, rather than
@@ -3076,6 +3121,13 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
if (tx.version == 1 && threads > 1)
waiter.wait(&tpool);
+ // enforce min output age
+ if (hf_version >= HF_VERSION_ENFORCE_MIN_AGE)
+ {
+ CHECK_AND_ASSERT_MES(*pmax_used_block_height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE <= m_db->height(),
+ false, "Transaction spends at least one output which is too young");
+ }
+
if (tx.version == 1)
{
if (threads > 1)
@@ -3408,7 +3460,8 @@ uint64_t Blockchain::get_dynamic_base_fee_estimate(uint64_t grace_blocks) const
}
const bool use_long_term_median_in_fee = version >= HF_VERSION_LONG_TERM_BLOCK_WEIGHT;
- uint64_t fee = get_dynamic_base_fee(base_reward, use_long_term_median_in_fee ? m_long_term_effective_median_block_weight : median, version);
+ const uint64_t use_median_value = use_long_term_median_in_fee ? std::min<uint64_t>(median, m_long_term_effective_median_block_weight) : median;
+ const uint64_t fee = get_dynamic_base_fee(base_reward, use_median_value, version);
const bool per_byte = version < HF_VERSION_PER_BYTE_FEE;
MDEBUG("Estimating " << grace_blocks << "-block fee at " << print_money(fee) << "/" << (per_byte ? "byte" : "kB"));
return fee;
@@ -3592,9 +3645,9 @@ bool Blockchain::flush_txes_from_pool(const std::vector<crypto::hash> &txids)
cryptonote::blobdata txblob;
size_t tx_weight;
uint64_t fee;
- bool relayed, do_not_relay, double_spend_seen;
+ bool relayed, do_not_relay, double_spend_seen, pruned;
MINFO("Removing txid " << txid << " from the pool");
- if(m_tx_pool.have_tx(txid) && !m_tx_pool.take_tx(txid, tx, txblob, tx_weight, fee, relayed, do_not_relay, double_spend_seen))
+ if(m_tx_pool.have_tx(txid) && !m_tx_pool.take_tx(txid, tx, txblob, tx_weight, fee, relayed, do_not_relay, double_spend_seen, pruned))
{
MERROR("Failed to remove txid " << txid << " from the pool");
res = false;
@@ -3693,7 +3746,7 @@ leave:
#if defined(PER_BLOCK_CHECKPOINT)
if (blockchain_height < m_blocks_hash_check.size())
{
- const auto &expected_hash = m_blocks_hash_check[blockchain_height];
+ const auto &expected_hash = m_blocks_hash_check[blockchain_height].first;
if (expected_hash != crypto::null_hash)
{
if (memcmp(&id, &expected_hash, sizeof(hash)) != 0)
@@ -3719,13 +3772,14 @@ leave:
proof_of_work = it->second;
}
else
- proof_of_work = get_block_longhash(bl, blockchain_height);
+ proof_of_work = get_block_longhash(this, bl, blockchain_height, 0);
// validate proof_of_work versus difficulty target
if(!check_hash(proof_of_work, current_diffic))
{
MERROR_VER("Block with id: " << id << std::endl << "does not have enough proof of work: " << proof_of_work << " at height " << blockchain_height << ", unexpected difficulty: " << current_diffic);
bvc.m_verifivation_failed = true;
+ bvc.m_bad_pow = true;
goto leave;
}
}
@@ -3767,6 +3821,7 @@ leave:
uint64_t t_exists = 0;
uint64_t t_pool = 0;
uint64_t t_dblspnd = 0;
+ uint64_t n_pruned = 0;
TIME_MEASURE_FINISH(t3);
// XXX old code adds miner tx here
@@ -3782,7 +3837,7 @@ leave:
blobdata txblob;
size_t tx_weight = 0;
uint64_t fee = 0;
- bool relayed = false, do_not_relay = false, double_spend_seen = false;
+ bool relayed = false, do_not_relay = false, double_spend_seen = false, pruned = false;
TIME_MEASURE_START(aa);
// XXX old code does not check whether tx exists
@@ -3799,13 +3854,15 @@ leave:
TIME_MEASURE_START(bb);
// get transaction with hash <tx_id> from tx_pool
- if(!m_tx_pool.take_tx(tx_id, tx_tmp, txblob, tx_weight, fee, relayed, do_not_relay, double_spend_seen))
+ if(!m_tx_pool.take_tx(tx_id, tx_tmp, txblob, tx_weight, fee, relayed, do_not_relay, double_spend_seen, pruned))
{
MERROR_VER("Block with id: " << id << " has at least one unknown transaction with id: " << tx_id);
bvc.m_verifivation_failed = true;
return_tx_to_pool(txs);
goto leave;
}
+ if (pruned)
+ ++n_pruned;
TIME_MEASURE_FINISH(bb);
t_pool += bb;
@@ -3876,6 +3933,17 @@ leave:
cumulative_block_weight += tx_weight;
}
+ // if we were syncing pruned blocks
+ if (n_pruned > 0)
+ {
+ if (blockchain_height >= m_blocks_hash_check.size() || m_blocks_hash_check[blockchain_height].second == 0)
+ {
+ MERROR("Block at " << blockchain_height << " is pruned, but we do not have a weight for it");
+ goto leave;
+ }
+ cumulative_block_weight = m_blocks_hash_check[blockchain_height].second;
+ }
+
m_blocks_txs_check.clear();
TIME_MEASURE_START(vmt);
@@ -4220,7 +4288,7 @@ void Blockchain::block_longhash_worker(uint64_t height, const epee::span<const b
if (m_cancel)
break;
crypto::hash id = get_block_hash(block);
- crypto::hash pow = get_block_longhash(block, height++);
+ crypto::hash pow = get_block_longhash(this, block, height++, 0);
map.emplace(id, pow);
}
@@ -4316,11 +4384,13 @@ void Blockchain::output_scan_worker(const uint64_t amount, const std::vector<uin
}
}
-uint64_t Blockchain::prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes)
+uint64_t Blockchain::prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes, const std::vector<uint64_t> &weights)
{
// new: . . . . . X X X X X . . . . . .
// pre: A A A A B B B B C C C C D D D D
+ CHECK_AND_ASSERT_MES(weights.empty() || weights.size() == hashes.size(), 0, "Unexpected weights size");
+
// easy case: height >= hashes
if (height >= m_blocks_hash_of_hashes.size() * HASH_OF_HASHES_STEP)
return hashes.size();
@@ -4339,8 +4409,11 @@ uint64_t Blockchain::prevalidate_block_hashes(uint64_t height, const std::vector
return hashes.size();
// build hashes vector to hash hashes together
- std::vector<crypto::hash> data;
- data.reserve(hashes.size() + HASH_OF_HASHES_STEP - 1); // may be a bit too much
+ std::vector<crypto::hash> data_hashes;
+ std::vector<uint64_t> data_weights;
+ data_hashes.reserve(hashes.size() + HASH_OF_HASHES_STEP - 1); // may be a bit too much
+ if (!weights.empty())
+ data_weights.reserve(data_hashes.size());
// we expect height to be either equal or a bit below db height
bool disconnected = (height > m_db->height());
@@ -4355,18 +4428,24 @@ uint64_t Blockchain::prevalidate_block_hashes(uint64_t height, const std::vector
// we might need some already in the chain for the first part of the first hash
for (uint64_t h = first_index * HASH_OF_HASHES_STEP; h < height; ++h)
{
- data.push_back(m_db->get_block_hash_from_height(h));
+ data_hashes.push_back(m_db->get_block_hash_from_height(h));
+ if (!weights.empty())
+ data_weights.push_back(m_db->get_block_weight(h));
}
pop = 0;
}
// push the data to check
- for (const auto &h: hashes)
+ for (size_t i = 0; i < hashes.size(); ++i)
{
if (pop)
--pop;
else
- data.push_back(h);
+ {
+ data_hashes.push_back(hashes[i]);
+ if (!weights.empty())
+ data_weights.push_back(weights[i]);
+ }
}
// hash and check
@@ -4376,12 +4455,17 @@ uint64_t Blockchain::prevalidate_block_hashes(uint64_t height, const std::vector
if (n < m_blocks_hash_of_hashes.size())
{
// if the last index isn't fully filled, we can't tell if valid
- if (data.size() < (n - first_index) * HASH_OF_HASHES_STEP + HASH_OF_HASHES_STEP)
+ if (data_hashes.size() < (n - first_index) * HASH_OF_HASHES_STEP + HASH_OF_HASHES_STEP)
break;
crypto::hash hash;
- cn_fast_hash(data.data() + (n - first_index) * HASH_OF_HASHES_STEP, HASH_OF_HASHES_STEP * sizeof(crypto::hash), hash);
- bool valid = hash == m_blocks_hash_of_hashes[n];
+ cn_fast_hash(data_hashes.data() + (n - first_index) * HASH_OF_HASHES_STEP, HASH_OF_HASHES_STEP * sizeof(crypto::hash), hash);
+ bool valid = hash == m_blocks_hash_of_hashes[n].first;
+ if (valid && !weights.empty())
+ {
+ cn_fast_hash(data_weights.data() + (n - first_index) * HASH_OF_HASHES_STEP, HASH_OF_HASHES_STEP * sizeof(uint64_t), hash);
+ valid &= hash == m_blocks_hash_of_hashes[n].second;
+ }
// add to the known hashes array
if (!valid)
@@ -4393,9 +4477,15 @@ uint64_t Blockchain::prevalidate_block_hashes(uint64_t height, const std::vector
size_t end = n * HASH_OF_HASHES_STEP + HASH_OF_HASHES_STEP;
for (size_t i = n * HASH_OF_HASHES_STEP; i < end; ++i)
{
- CHECK_AND_ASSERT_MES(m_blocks_hash_check[i] == crypto::null_hash || m_blocks_hash_check[i] == data[i - first_index * HASH_OF_HASHES_STEP],
+ CHECK_AND_ASSERT_MES(m_blocks_hash_check[i].first == crypto::null_hash || m_blocks_hash_check[i].first == data_hashes[i - first_index * HASH_OF_HASHES_STEP],
0, "Consistency failure in m_blocks_hash_check construction");
- m_blocks_hash_check[i] = data[i - first_index * HASH_OF_HASHES_STEP];
+ m_blocks_hash_check[i].first = data_hashes[i - first_index * HASH_OF_HASHES_STEP];
+ if (!weights.empty())
+ {
+ CHECK_AND_ASSERT_MES(m_blocks_hash_check[i].second == 0 || m_blocks_hash_check[i].second == data_weights[i - first_index * HASH_OF_HASHES_STEP],
+ 0, "Consistency failure in m_blocks_hash_check construction");
+ m_blocks_hash_check[i].second = data_weights[i - first_index * HASH_OF_HASHES_STEP];
+ }
}
usable += HASH_OF_HASHES_STEP;
}
@@ -4412,6 +4502,18 @@ uint64_t Blockchain::prevalidate_block_hashes(uint64_t height, const std::vector
return usable;
}
+bool Blockchain::has_block_weights(uint64_t height, uint64_t nblocks) const
+{
+ CHECK_AND_ASSERT_MES(nblocks > 0, false, "nblocks is 0");
+ uint64_t last_block_height = height + nblocks - 1;
+ if (last_block_height >= m_blocks_hash_check.size())
+ return false;
+ for (uint64_t h = height; h <= last_block_height; ++h)
+ if (m_blocks_hash_check[h].second == 0)
+ return false;
+ return true;
+}
+
//------------------------------------------------------------------
// ND: Speedups:
// 1. Thread long_hash computations if possible (m_max_prepare_blocks_threads = nthreads, default = 4)
@@ -4453,7 +4555,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
bytes += entry.block.size();
for (const auto &tx_blob : entry.txs)
{
- bytes += tx_blob.size();
+ bytes += tx_blob.blob.size();
}
total_txs += entry.txs.size();
}
@@ -4536,6 +4638,9 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
m_blocks_longhash_table.clear();
uint64_t thread_height = height;
tools::threadpool::waiter waiter;
+ m_prepare_height = height;
+ m_prepare_nblocks = blocks_entry.size();
+ m_prepare_blocks = &blocks;
for (unsigned int i = 0; i < threads; i++)
{
unsigned nblocks = batches;
@@ -4546,6 +4651,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
}
waiter.wait(&tpool);
+ m_prepare_height = 0;
if (m_cancel)
return false;
@@ -4609,7 +4715,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
crypto::hash &tx_prefix_hash = txes[tx_index].second;
++tx_index;
- if (!parse_and_validate_tx_base_from_blob(tx_blob, tx))
+ if (!parse_and_validate_tx_base_from_blob(tx_blob.blob, tx))
SCAN_TABLE_QUIT("Could not parse tx from incoming blocks.");
cryptonote::get_transaction_prefix_hash(tx, tx_prefix_hash);
@@ -4830,39 +4936,6 @@ HardFork::State Blockchain::get_hard_fork_state() const
return m_hardfork->get_state();
}
-const std::vector<HardFork::Params>& Blockchain::get_hard_fork_heights(network_type nettype)
-{
- static const std::vector<HardFork::Params> mainnet_heights = []()
- {
- std::vector<HardFork::Params> heights;
- for (const auto& i : mainnet_hard_forks)
- heights.emplace_back(i.version, i.height, i.threshold, i.time);
- return heights;
- }();
- static const std::vector<HardFork::Params> testnet_heights = []()
- {
- std::vector<HardFork::Params> heights;
- for (const auto& i : testnet_hard_forks)
- heights.emplace_back(i.version, i.height, i.threshold, i.time);
- return heights;
- }();
- static const std::vector<HardFork::Params> stagenet_heights = []()
- {
- std::vector<HardFork::Params> heights;
- for (const auto& i : stagenet_hard_forks)
- heights.emplace_back(i.version, i.height, i.threshold, i.time);
- return heights;
- }();
- static const std::vector<HardFork::Params> dummy;
- switch (nettype)
- {
- case MAINNET: return mainnet_heights;
- case TESTNET: return testnet_heights;
- case STAGENET: return stagenet_heights;
- default: return dummy;
- }
-}
-
bool Blockchain::get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const
{
return m_hardfork->get_voting_info(version, window, votes, threshold, earliest_height, voting);
@@ -4941,7 +5014,7 @@ void Blockchain::cancel()
}
#if defined(PER_BLOCK_CHECKPOINT)
-static const char expected_block_hashes_hash[] = "7dafb40b414a0e59bfced6682ef519f0b416bc914dd3d622b72e0dd1a47117c2";
+static const char expected_block_hashes_hash[] = "95e60612c1a16f4cd992c335b66daabd98e2d351c2b02b66e43ced0296848d33";
void Blockchain::load_compiled_in_block_hashes(const GetCheckpointsCallback& get_checkpoints)
{
if (get_checkpoints == nullptr || !m_fast_sync)
@@ -4985,19 +5058,21 @@ void Blockchain::load_compiled_in_block_hashes(const GetCheckpointsCallback& get
MERROR("Block hash data is too large");
return;
}
- const size_t size_needed = 4 + nblocks * sizeof(crypto::hash);
+ const size_t size_needed = 4 + nblocks * (sizeof(crypto::hash) * 2);
if(nblocks > 0 && nblocks > (m_db->height() + HASH_OF_HASHES_STEP - 1) / HASH_OF_HASHES_STEP && checkpoints.size() >= size_needed)
{
p += sizeof(uint32_t);
m_blocks_hash_of_hashes.reserve(nblocks);
for (uint32_t i = 0; i < nblocks; i++)
{
- crypto::hash hash;
- memcpy(hash.data, p, sizeof(hash.data));
- p += sizeof(hash.data);
- m_blocks_hash_of_hashes.push_back(hash);
+ crypto::hash hash_hashes, hash_weights;
+ memcpy(hash_hashes.data, p, sizeof(hash_hashes.data));
+ p += sizeof(hash_hashes.data);
+ memcpy(hash_weights.data, p, sizeof(hash_weights.data));
+ p += sizeof(hash_weights.data);
+ m_blocks_hash_of_hashes.push_back(std::make_pair(hash_hashes, hash_weights));
}
- m_blocks_hash_check.resize(m_blocks_hash_of_hashes.size() * HASH_OF_HASHES_STEP, crypto::null_hash);
+ m_blocks_hash_check.resize(m_blocks_hash_of_hashes.size() * HASH_OF_HASHES_STEP, std::make_pair(crypto::null_hash, 0));
MINFO(nblocks << " block hashes loaded");
// FIXME: clear tx_pool because the process might have been
@@ -5012,13 +5087,13 @@ void Blockchain::load_compiled_in_block_hashes(const GetCheckpointsCallback& get
size_t tx_weight;
uint64_t fee;
- bool relayed, do_not_relay, double_spend_seen;
+ bool relayed, do_not_relay, double_spend_seen, pruned;
transaction pool_tx;
blobdata txblob;
for(const transaction &tx : txs)
{
crypto::hash tx_hash = get_transaction_hash(tx);
- m_tx_pool.take_tx(tx_hash, pool_tx, txblob, tx_weight, fee, relayed, do_not_relay, double_spend_seen);
+ m_tx_pool.take_tx(tx_hash, pool_tx, txblob, tx_weight, fee, relayed, do_not_relay, double_spend_seen, pruned);
}
}
}
@@ -5091,6 +5166,5 @@ void Blockchain::cache_block_template(const block &b, const cryptonote::account_
namespace cryptonote {
template bool Blockchain::get_transactions(const std::vector<crypto::hash>&, std::vector<transaction>&, std::vector<crypto::hash>&) const;
-template bool Blockchain::get_transactions_blobs(const std::vector<crypto::hash>&, std::vector<cryptonote::blobdata>&, std::vector<crypto::hash>&, bool) const;
template bool Blockchain::get_split_transactions_blobs(const std::vector<crypto::hash>&, std::vector<std::tuple<crypto::hash, cryptonote::blobdata, crypto::hash, cryptonote::blobdata>>&, std::vector<crypto::hash>&) const;
}
diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h
index f32645949..6467031c2 100644
--- a/src/cryptonote_core/blockchain.h
+++ b/src/cryptonote_core/blockchain.h
@@ -206,6 +206,18 @@ namespace cryptonote
crypto::hash get_block_id_by_height(uint64_t height) const;
/**
+ * @brief gets a block's hash given a height
+ *
+ * Used only by prepare_handle_incoming_blocks. Will look in the list of incoming blocks
+ * if the height is contained there.
+ *
+ * @param height the height of the block
+ *
+ * @return the hash of the block at the requested height, or a zeroed hash if there is no such block
+ */
+ crypto::hash get_pending_block_id_by_height(uint64_t height) const;
+
+ /**
* @brief gets the block with a given hash
*
* @param h the hash to look for
@@ -381,13 +393,14 @@ namespace cryptonote
*
* @param qblock_ids the foreign chain's "short history" (see get_short_chain_history)
* @param hashes the hashes to be returned, return-by-reference
+ * @param weights the block weights to be returned, return-by-reference
* @param start_height the start height, return-by-reference
* @param current_height the current blockchain height, return-by-reference
* @param clip_pruned whether to constrain results to unpruned data
*
* @return true if a block found in common, else false
*/
- bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::vector<crypto::hash>& hashes, uint64_t& start_height, uint64_t& current_height, bool clip_pruned) const;
+ bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::vector<crypto::hash>& hashes, std::vector<uint64_t>* weights, uint64_t& start_height, uint64_t& current_height, bool clip_pruned) const;
/**
* @brief get recent block hashes for a foreign chain
@@ -397,11 +410,12 @@ namespace cryptonote
* BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT additional (more recent) hashes.
*
* @param qblock_ids the foreign chain's "short history" (see get_short_chain_history)
+ * @param clip_pruned clip pruned blocks if true, include them otherwise
* @param resp return-by-reference the split height and subsequent blocks' hashes
*
* @return true if a block found in common, else false
*/
- bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
+ bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, bool clip_pruned, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
/**
* @brief find the most recent common point between ours and a foreign chain
@@ -675,8 +689,8 @@ namespace cryptonote
*
* @return false if an unexpected exception occurs, else true
*/
- template<class t_ids_container, class t_tx_container, class t_missed_container>
- bool get_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs, bool pruned = false) const;
+ bool get_transactions_blobs(const std::vector<crypto::hash>& txs_ids, std::vector<cryptonote::blobdata>& txs, std::vector<crypto::hash>& missed_txs, bool pruned = false) const;
+ bool get_transactions_blobs(const std::vector<crypto::hash>& txs_ids, std::vector<tx_blob_entry>& txs, std::vector<crypto::hash>& missed_txs, bool pruned = false) const;
template<class t_ids_container, class t_tx_container, class t_missed_container>
bool get_split_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const;
template<class t_ids_container, class t_tx_container, class t_missed_container>
@@ -763,13 +777,6 @@ namespace cryptonote
HardFork::State get_hard_fork_state() const;
/**
- * @brief gets the hardfork heights of given network
- *
- * @return the HardFork object
- */
- static const std::vector<HardFork::Params>& get_hard_fork_heights(network_type nettype);
-
- /**
* @brief gets the current hardfork version in use/voted for
*
* @return the version
@@ -963,9 +970,8 @@ namespace cryptonote
cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const;
bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false, bool include_unrelayed_txes = true) const;
- bool is_within_compiled_block_hash_area(uint64_t height) const;
bool is_within_compiled_block_hash_area() const { return is_within_compiled_block_hash_area(m_db->height()); }
- uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes);
+ uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes, const std::vector<uint64_t> &weights);
uint32_t get_blockchain_pruning_seed() const { return m_db->get_blockchain_pruning_seed(); }
bool prune_blockchain(uint32_t pruning_seed = 0);
bool update_blockchain_pruning();
@@ -995,6 +1001,21 @@ namespace cryptonote
*/
void pop_blocks(uint64_t nblocks);
+ /**
+ * @brief checks whether a given block height is included in the precompiled block hash area
+ *
+ * @param height the height to check for
+ */
+ bool is_within_compiled_block_hash_area(uint64_t height) const;
+
+ /**
+ * @brief checks whether we have known weights for the given block heights
+ *
+ * @param height the start height to check for
+ * @param nblocks how many blocks to check from that height
+ */
+ bool has_block_weights(uint64_t height, uint64_t nblocks) const;
+
#ifndef IN_UNIT_TESTS
private:
#endif
@@ -1022,8 +1043,8 @@ namespace cryptonote
std::unordered_map<crypto::hash, crypto::hash> m_blocks_longhash_table;
// SHA-3 hashes for each block and for fast pow checking
- std::vector<crypto::hash> m_blocks_hash_of_hashes;
- std::vector<crypto::hash> m_blocks_hash_check;
+ std::vector<std::pair<crypto::hash, crypto::hash>> m_blocks_hash_of_hashes;
+ std::vector<std::pair<crypto::hash, uint64_t>> m_blocks_hash_check;
std::vector<crypto::hash> m_blocks_txs_check;
blockchain_db_sync_mode m_db_sync_mode;
@@ -1084,6 +1105,11 @@ namespace cryptonote
std::shared_ptr<tools::Notify> m_block_notify;
std::shared_ptr<tools::Notify> m_reorg_notify;
+ // for prepare_handle_incoming_blocks
+ uint64_t m_prepare_height;
+ uint64_t m_prepare_nblocks;
+ std::vector<block> *m_prepare_blocks;
+
/**
* @brief collects the keys for all outputs being "spent" as an input
*
diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp
index 0147bde23..b831cc9ff 100644
--- a/src/cryptonote_core/cryptonote_core.cpp
+++ b/src/cryptonote_core/cryptonote_core.cpp
@@ -51,6 +51,7 @@ using namespace epee;
#include "blockchain_db/blockchain_db.h"
#include "ringct/rctSigs.h"
#include "common/notify.h"
+#include "hardforks/hardforks.h"
#include "version.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -113,6 +114,10 @@ namespace cryptonote
, "Set maximum size of block download queue in bytes (0 for default)"
, 0
};
+ const command_line::arg_descriptor<bool> arg_sync_pruned_blocks = {
+ "sync-pruned-blocks"
+ , "Allow syncing from nodes with only pruned blocks"
+ };
static const command_line::arg_descriptor<bool> arg_test_drop_download = {
"test-drop-download"
@@ -218,7 +223,7 @@ namespace cryptonote
core::core(i_cryptonote_protocol* pprotocol):
m_mempool(m_blockchain_storage),
m_blockchain_storage(m_mempool),
- m_miner(this),
+ m_miner(this, &m_blockchain_storage),
m_starter_message_showed(false),
m_target_blockchain_height(0),
m_checkpoints_path(""),
@@ -323,6 +328,7 @@ namespace cryptonote
command_line::add_arg(desc, arg_offline);
command_line::add_arg(desc, arg_disable_dns_checkpoints);
command_line::add_arg(desc, arg_block_download_max_size);
+ command_line::add_arg(desc, arg_sync_pruned_blocks);
command_line::add_arg(desc, arg_max_txpool_weight);
command_line::add_arg(desc, arg_pad_transactions);
command_line::add_arg(desc, arg_block_notify);
@@ -454,7 +460,6 @@ namespace cryptonote
bool r = handle_command_line(vm);
CHECK_AND_ASSERT_MES(r, false, "Failed to handle command line");
- std::string db_type = command_line::get_arg(vm, cryptonote::arg_db_type);
std::string db_sync_mode = command_line::get_arg(vm, cryptonote::arg_db_sync_mode);
bool db_salvage = command_line::get_arg(vm, cryptonote::arg_db_salvage) != 0;
bool fast_sync = command_line::get_arg(vm, arg_fast_block_sync) != 0;
@@ -488,10 +493,10 @@ namespace cryptonote
// folder might not be a directory, etc, etc
catch (...) { }
- std::unique_ptr<BlockchainDB> db(new_db(db_type));
+ std::unique_ptr<BlockchainDB> db(new_db());
if (db == NULL)
{
- LOG_ERROR("Attempted to use non-existent database type");
+ LOG_ERROR("Failed to initialize a database");
return false;
}
@@ -634,7 +639,7 @@ namespace cryptonote
MERROR("Failed to parse block rate notify spec: " << e.what());
}
- const std::pair<uint8_t, uint64_t> regtest_hard_forks[3] = {std::make_pair(1, 0), std::make_pair(Blockchain::get_hard_fork_heights(MAINNET).back().version, 1), std::make_pair(0, 0)};
+ const std::pair<uint8_t, uint64_t> regtest_hard_forks[3] = {std::make_pair(1, 0), std::make_pair(mainnet_hard_forks[num_mainnet_hard_forks-1].version, 1), std::make_pair(0, 0)};
const cryptonote::test_options regtest_test_options = {
regtest_hard_forks,
0
@@ -655,6 +660,8 @@ namespace cryptonote
CHECK_AND_ASSERT_MES(r, false, "Failed to initialize blockchain storage");
block_sync_size = command_line::get_arg(vm, arg_block_sync_size);
+ if (block_sync_size > BLOCKS_SYNCHRONIZING_MAX_COUNT)
+ MERROR("Error --block-sync-size cannot be greater than " << BLOCKS_SYNCHRONIZING_MAX_COUNT);
MGINFO("Loading checkpoints");
@@ -744,13 +751,13 @@ namespace cryptonote
return false;
}
//-----------------------------------------------------------------------------------------------
- bool core::handle_incoming_tx_pre(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay)
+ bool core::handle_incoming_tx_pre(const tx_blob_entry& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay)
{
- tvc = boost::value_initialized<tx_verification_context>();
+ tvc = {};
- if(tx_blob.size() > get_max_tx_size())
+ if(tx_blob.blob.size() > get_max_tx_size())
{
- LOG_PRINT_L1("WRONG TRANSACTION BLOB, too big size " << tx_blob.size() << ", rejected");
+ LOG_PRINT_L1("WRONG TRANSACTION BLOB, too big size " << tx_blob.blob.size() << ", rejected");
tvc.m_verifivation_failed = true;
tvc.m_too_big = true;
return false;
@@ -758,7 +765,23 @@ namespace cryptonote
tx_hash = crypto::null_hash;
- if(!parse_tx_from_blob(tx, tx_hash, tx_blob))
+ bool r;
+ if (tx_blob.prunable_hash == crypto::null_hash)
+ {
+ r = parse_tx_from_blob(tx, tx_hash, tx_blob.blob);
+ }
+ else
+ {
+ r = parse_and_validate_tx_base_from_blob(tx_blob.blob, tx);
+ if (r)
+ {
+ tx.set_prunable_hash(tx_blob.prunable_hash);
+ tx_hash = cryptonote::get_pruned_transaction_hash(tx, tx_blob.prunable_hash);
+ tx.set_hash(tx_hash);
+ }
+ }
+
+ if (!r)
{
LOG_PRINT_L1("WRONG TRANSACTION BLOB, Failed to parse, rejected");
tvc.m_verifivation_failed = true;
@@ -784,6 +807,7 @@ namespace cryptonote
if (tx.version == 0 || tx.version > max_tx_version)
{
// v2 is the latest one we know
+ MERROR_VER("Bad tx version (" << tx.version << ", max is " << max_tx_version << ")");
tvc.m_verifivation_failed = true;
return false;
}
@@ -791,7 +815,7 @@ namespace cryptonote
return true;
}
//-----------------------------------------------------------------------------------------------
- bool core::handle_incoming_tx_post(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay)
+ bool core::handle_incoming_tx_post(const tx_blob_entry& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay)
{
if(!check_tx_syntax(tx))
{
@@ -920,7 +944,7 @@ namespace cryptonote
return ret;
}
//-----------------------------------------------------------------------------------------------
- bool core::handle_incoming_txs(const std::vector<blobdata>& tx_blobs, std::vector<tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
+ bool core::handle_incoming_txs(const std::vector<tx_blob_entry>& tx_blobs, std::vector<tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
{
TRY_ENTRY();
CRITICAL_REGION_LOCAL(m_incoming_tx_lock);
@@ -931,7 +955,7 @@ namespace cryptonote
tvc.resize(tx_blobs.size());
tools::threadpool& tpool = tools::threadpool::getInstance();
tools::threadpool::waiter waiter;
- std::vector<blobdata>::const_iterator it = tx_blobs.begin();
+ std::vector<tx_blob_entry>::const_iterator it = tx_blobs.begin();
for (size_t i = 0; i < tx_blobs.size(); i++, ++it) {
tpool.submit(&waiter, [&, i, it] {
try
@@ -1003,8 +1027,12 @@ namespace cryptonote
if (already_have[i])
continue;
- const size_t weight = get_transaction_weight(results[i].tx, it->size());
- ok &= add_new_tx(results[i].tx, results[i].hash, tx_blobs[i], weight, tvc[i], keeped_by_block, relayed, do_not_relay);
+ // if it's a pruned tx from an incoming block, we'll get a weight that's technically
+ // different from the actual transaction weight, but it's OK for our use. Those txes
+ // will be ignored when mining, and using that "pruned" weight seems appropriate for
+ // keeping the txpool size constrained
+ const uint64_t weight = results[i].tx.pruned ? 0 : get_transaction_weight(results[i].tx, it->blob.size());
+ ok &= add_new_tx(results[i].tx, results[i].hash, tx_blobs[i].blob, weight, tvc[i], keeped_by_block, relayed, do_not_relay);
if(tvc[i].m_verifivation_failed)
{MERROR_VER("Transaction verification failed: " << results[i].hash);}
else if(tvc[i].m_verifivation_impossible)
@@ -1018,9 +1046,9 @@ namespace cryptonote
CATCH_ENTRY_L0("core::handle_incoming_txs()", false);
}
//-----------------------------------------------------------------------------------------------
- bool core::handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
+ bool core::handle_incoming_tx(const tx_blob_entry& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
{
- std::vector<cryptonote::blobdata> tx_blobs;
+ std::vector<tx_blob_entry> tx_blobs;
tx_blobs.push_back(tx_blob);
std::vector<tx_verification_context> tvcv(1);
bool r = handle_incoming_txs(tx_blobs, tvcv, keeped_by_block, relayed, do_not_relay);
@@ -1028,6 +1056,11 @@ namespace cryptonote
return r;
}
//-----------------------------------------------------------------------------------------------
+ bool core::handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
+ {
+ return handle_incoming_tx({tx_blob, crypto::null_hash}, tvc, keeped_by_block, relayed, do_not_relay);
+ }
+ //-----------------------------------------------------------------------------------------------
bool core::get_stat_info(core_stat_info& st_inf) const
{
st_inf.mining_speed = m_miner.get_speed();
@@ -1290,9 +1323,9 @@ namespace cryptonote
return m_blockchain_storage.create_block_template(b, prev_block, adr, diffic, height, expected_reward, ex_nonce);
}
//-----------------------------------------------------------------------------------------------
- bool core::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const
+ bool core::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, bool clip_pruned, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const
{
- return m_blockchain_storage.find_blockchain_supplement(qblock_ids, resp);
+ return m_blockchain_storage.find_blockchain_supplement(qblock_ids, clip_pruned, resp);
}
//-----------------------------------------------------------------------------------------------
bool core::find_blockchain_supplement(const uint64_t req_start_block, const std::list<crypto::hash>& qblock_ids, std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata> > > >& blocks, uint64_t& total_height, uint64_t& start_height, bool pruned, bool get_miner_tx_hash, size_t max_count) const
@@ -1338,14 +1371,14 @@ namespace cryptonote
{
cryptonote::blobdata txblob;
CHECK_AND_ASSERT_THROW_MES(pool.get_transaction(tx_hash, txblob), "Transaction not found in pool");
- bce.txs.push_back(txblob);
+ bce.txs.push_back({txblob, crypto::null_hash});
}
return bce;
}
//-----------------------------------------------------------------------------------------------
bool core::handle_block_found(block& b, block_verification_context &bvc)
{
- bvc = boost::value_initialized<block_verification_context>();
+ bvc = {};
m_miner.pause();
std::vector<block_complete_entry> blocks;
try
@@ -1374,7 +1407,7 @@ namespace cryptonote
CHECK_AND_ASSERT_MES(!bvc.m_verifivation_failed, false, "mined block failed verification");
if(bvc.m_added_to_main_chain)
{
- cryptonote_connection_context exclude_context = boost::value_initialized<cryptonote_connection_context>();
+ cryptonote_connection_context exclude_context = {};
NOTIFY_NEW_BLOCK::request arg = AUTO_VAL_INIT(arg);
arg.current_blockchain_height = m_blockchain_storage.get_current_blockchain_height();
std::vector<crypto::hash> missed_txs;
@@ -1391,7 +1424,7 @@ namespace cryptonote
block_to_blob(b, arg.b.block);
//pack transactions
for(auto& tx: txs)
- arg.b.txs.push_back(tx);
+ arg.b.txs.push_back({tx, crypto::null_hash});
m_pprotocol->relay_block(arg, exclude_context);
}
@@ -1442,7 +1475,7 @@ namespace cryptonote
{
TRY_ENTRY();
- bvc = boost::value_initialized<block_verification_context>();
+ bvc = {};
if (!check_incoming_block_size(block_blob))
{
@@ -1641,7 +1674,7 @@ namespace cryptonote
break;
case HardFork::UpdateNeeded:
level = el::Level::Info;
- MCLOG(level, "global", "Last scheduled hard fork time suggests a daemon update will be released within the next couple months.");
+ MCLOG(level, "global", el::Color::Default, "Last scheduled hard fork time suggests a daemon update will be released within the next couple months.");
break;
default:
break;
@@ -1901,9 +1934,9 @@ namespace cryptonote
return m_target_blockchain_height;
}
//-----------------------------------------------------------------------------------------------
- uint64_t core::prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes)
+ uint64_t core::prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes, const std::vector<uint64_t> &weights)
{
- return get_blockchain_storage().prevalidate_block_hashes(height, hashes);
+ return get_blockchain_storage().prevalidate_block_hashes(height, hashes, weights);
}
//-----------------------------------------------------------------------------------------------
uint64_t core::get_free_space() const
@@ -1923,6 +1956,16 @@ namespace cryptonote
return get_blockchain_storage().prune_blockchain(pruning_seed);
}
//-----------------------------------------------------------------------------------------------
+ bool core::is_within_compiled_block_hash_area(uint64_t height) const
+ {
+ return get_blockchain_storage().is_within_compiled_block_hash_area(height);
+ }
+ //-----------------------------------------------------------------------------------------------
+ bool core::has_block_weights(uint64_t height, uint64_t nblocks) const
+ {
+ return get_blockchain_storage().has_block_weights(height, nblocks);
+ }
+ //-----------------------------------------------------------------------------------------------
std::time_t core::get_start_time() const
{
return start_time;
diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h
index badbaf936..0db6350be 100644
--- a/src/cryptonote_core/cryptonote_core.h
+++ b/src/cryptonote_core/cryptonote_core.h
@@ -64,6 +64,7 @@ namespace cryptonote
extern const command_line::arg_descriptor<difficulty_type> arg_fixed_difficulty;
extern const command_line::arg_descriptor<bool> arg_offline;
extern const command_line::arg_descriptor<size_t> arg_block_download_max_size;
+ extern const command_line::arg_descriptor<bool> arg_sync_pruned_blocks;
/************************************************************************/
/* */
@@ -120,6 +121,7 @@ namespace cryptonote
*
* @return true if the transaction was accepted, false otherwise
*/
+ bool handle_incoming_tx(const tx_blob_entry& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
bool handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
/**
@@ -136,7 +138,7 @@ namespace cryptonote
*
* @return true if the transactions were accepted, false otherwise
*/
- bool handle_incoming_txs(const std::vector<blobdata>& tx_blobs, std::vector<tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
+ bool handle_incoming_txs(const std::vector<tx_blob_entry>& tx_blobs, std::vector<tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
/**
* @brief handles an incoming block
@@ -522,7 +524,7 @@ namespace cryptonote
*
* @note see Blockchain::find_blockchain_supplement(const std::list<crypto::hash>&, NOTIFY_RESPONSE_CHAIN_ENTRY::request&) const
*/
- bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
+ bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, bool clip_pruned, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const;
/**
* @copydoc Blockchain::find_blockchain_supplement(const uint64_t, const std::list<crypto::hash>&, std::vector<std::pair<cryptonote::blobdata, std::vector<cryptonote::blobdata> > >&, uint64_t&, uint64_t&, size_t) const
@@ -779,7 +781,7 @@ namespace cryptonote
*
* @return number of usable blocks
*/
- uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes);
+ uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes, const std::vector<uint64_t> &weights);
/**
* @brief get free disk space on the blockchain partition
@@ -825,6 +827,18 @@ namespace cryptonote
*/
bool check_blockchain_pruning();
+ /**
+ * @brief checks whether a given block height is included in the precompiled block hash area
+ *
+ * @param height the height to check for
+ */
+ bool is_within_compiled_block_hash_area(uint64_t height) const;
+
+ /**
+ * @brief checks whether block weights are known for the given range
+ */
+ bool has_block_weights(uint64_t height, uint64_t nblocks) const;
+
private:
/**
@@ -910,8 +924,8 @@ namespace cryptonote
bool check_tx_semantic(const transaction& tx, bool keeped_by_block) const;
void set_semantics_failed(const crypto::hash &tx_hash);
- bool handle_incoming_tx_pre(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
- bool handle_incoming_tx_post(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
+ bool handle_incoming_tx_pre(const tx_blob_entry& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
+ bool handle_incoming_tx_post(const tx_blob_entry &tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
struct tx_verification_batch_info { const cryptonote::transaction *tx; crypto::hash tx_hash; tx_verification_context &tvc; bool &result; };
bool handle_incoming_tx_accumulated_batch(std::vector<tx_verification_batch_info> &tx_info, bool keeped_by_block);
diff --git a/src/cryptonote_core/cryptonote_tx_utils.cpp b/src/cryptonote_core/cryptonote_tx_utils.cpp
index 4cf71e558..d2e022347 100644
--- a/src/cryptonote_core/cryptonote_tx_utils.cpp
+++ b/src/cryptonote_core/cryptonote_tx_utils.cpp
@@ -37,6 +37,7 @@ using namespace epee;
#include "common/apply_permutation.h"
#include "cryptonote_tx_utils.h"
#include "cryptonote_config.h"
+#include "blockchain.h"
#include "cryptonote_basic/miner.h"
#include "cryptonote_basic/tx_extra.h"
#include "crypto/crypto.h"
@@ -647,7 +648,7 @@ namespace cryptonote
)
{
//genesis block
- bl = boost::value_initialized<block>();
+ bl = {};
blobdata tx_bl;
bool r = string_tools::parse_hexstr_to_binbuff(genesis_tx, tx_bl);
@@ -658,9 +659,59 @@ namespace cryptonote
bl.minor_version = CURRENT_BLOCK_MINOR_VERSION;
bl.timestamp = 0;
bl.nonce = nonce;
- miner::find_nonce_for_given_block(bl, 1, 0);
+ miner::find_nonce_for_given_block(NULL, bl, 1, 0);
bl.invalidate_hashes();
return true;
}
//---------------------------------------------------------------
+ void get_altblock_longhash(const block& b, crypto::hash& res, const uint64_t main_height, const uint64_t height, const uint64_t seed_height, const crypto::hash& seed_hash)
+ {
+ blobdata bd = get_block_hashing_blob(b);
+ rx_slow_hash(main_height, seed_height, seed_hash.data, bd.data(), bd.size(), res.data, 0, 1);
+ }
+
+ bool get_block_longhash(const Blockchain *pbc, const block& b, crypto::hash& res, const uint64_t height, const int miners)
+ {
+ // block 202612 bug workaround
+ if (height == 202612)
+ {
+ static const std::string longhash_202612 = "84f64766475d51837ac9efbef1926486e58563c95a19fef4aec3254f03000000";
+ epee::string_tools::hex_to_pod(longhash_202612, res);
+ return true;
+ }
+ blobdata bd = get_block_hashing_blob(b);
+ if (b.major_version >= RX_BLOCK_VERSION)
+ {
+ uint64_t seed_height, main_height;
+ crypto::hash hash;
+ if (pbc != NULL)
+ {
+ seed_height = rx_seedheight(height);
+ hash = pbc->get_pending_block_id_by_height(seed_height);
+ main_height = pbc->get_current_blockchain_height();
+ } else
+ {
+ memset(&hash, 0, sizeof(hash)); // only happens when generating genesis block
+ seed_height = 0;
+ main_height = 0;
+ }
+ rx_slow_hash(main_height, seed_height, hash.data, bd.data(), bd.size(), res.data, miners, 0);
+ } else {
+ const int pow_variant = b.major_version >= 7 ? b.major_version - 6 : 0;
+ crypto::cn_slow_hash(bd.data(), bd.size(), res, pow_variant, height);
+ }
+ return true;
+ }
+
+ crypto::hash get_block_longhash(const Blockchain *pbc, const block& b, const uint64_t height, const int miners)
+ {
+ crypto::hash p = crypto::null_hash;
+ get_block_longhash(pbc, b, p, height, miners);
+ return p;
+ }
+
+ void get_block_longhash_reorg(const uint64_t split_height)
+ {
+ rx_reorg(split_height);
+ }
}
diff --git a/src/cryptonote_core/cryptonote_tx_utils.h b/src/cryptonote_core/cryptonote_tx_utils.h
index b03eb6e70..309e4177f 100644
--- a/src/cryptonote_core/cryptonote_tx_utils.h
+++ b/src/cryptonote_core/cryptonote_tx_utils.h
@@ -132,6 +132,13 @@ namespace cryptonote
, uint32_t nonce
);
+ class Blockchain;
+ bool get_block_longhash(const Blockchain *pb, const block& b, crypto::hash& res, const uint64_t height, const int miners);
+ void get_altblock_longhash(const block& b, crypto::hash& res, const uint64_t main_height, const uint64_t height,
+ const uint64_t seed_height, const crypto::hash& seed_hash);
+ crypto::hash get_block_longhash(const Blockchain *pb, const block& b, const uint64_t height, const int miners);
+ void get_block_longhash_reorg(const uint64_t split_height);
+
}
BOOST_CLASS_VERSION(cryptonote::tx_source_entry, 1)
diff --git a/src/cryptonote_core/tx_pool.cpp b/src/cryptonote_core/tx_pool.cpp
index 71c32c133..648e691d0 100644
--- a/src/cryptonote_core/tx_pool.cpp
+++ b/src/cryptonote_core/tx_pool.cpp
@@ -247,6 +247,7 @@ namespace cryptonote
meta.relayed = relayed;
meta.do_not_relay = do_not_relay;
meta.double_spend_seen = have_tx_keyimges_as_spent(tx);
+ meta.pruned = tx.pruned;
meta.bf_padding = 0;
memset(meta.padding, 0, sizeof(meta.padding));
try
@@ -290,6 +291,7 @@ namespace cryptonote
meta.relayed = relayed;
meta.do_not_relay = do_not_relay;
meta.double_spend_seen = false;
+ meta.pruned = tx.pruned;
meta.bf_padding = 0;
memset(meta.padding, 0, sizeof(meta.padding));
@@ -460,7 +462,7 @@ namespace cryptonote
return true;
}
//---------------------------------------------------------------------------------
- bool tx_memory_pool::take_tx(const crypto::hash &id, transaction &tx, cryptonote::blobdata &txblob, size_t& tx_weight, uint64_t& fee, bool &relayed, bool &do_not_relay, bool &double_spend_seen)
+ bool tx_memory_pool::take_tx(const crypto::hash &id, transaction &tx, cryptonote::blobdata &txblob, size_t& tx_weight, uint64_t& fee, bool &relayed, bool &do_not_relay, bool &double_spend_seen, bool &pruned)
{
CRITICAL_REGION_LOCAL(m_transactions_lock);
CRITICAL_REGION_LOCAL1(m_blockchain);
@@ -482,7 +484,7 @@ namespace cryptonote
{
tx = ci->second;
}
- else if (!parse_and_validate_tx_from_blob(txblob, tx))
+ else if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(txblob, tx) : parse_and_validate_tx_from_blob(txblob, tx)))
{
MERROR("Failed to parse tx from txpool");
return false;
@@ -496,6 +498,7 @@ namespace cryptonote
relayed = meta.relayed;
do_not_relay = meta.do_not_relay;
double_spend_seen = meta.double_spend_seen;
+ pruned = meta.pruned;
// remove first, in case this throws, so key images aren't removed
m_blockchain.remove_txpool_tx(id);
@@ -654,7 +657,7 @@ namespace cryptonote
txs.reserve(m_blockchain.get_txpool_tx_count());
m_blockchain.for_all_txpool_txes([this, now, &txs](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *){
// 0 fee transactions are never relayed
- if(meta.fee > 0 && !meta.do_not_relay && now - meta.last_relayed_time > get_relay_delay(now, meta.receive_time))
+ if(!meta.pruned && meta.fee > 0 && !meta.do_not_relay && now - meta.last_relayed_time > get_relay_delay(now, meta.receive_time))
{
// if the tx is older than half the max lifetime, we don't re-relay it, to avoid a problem
// mentioned by smooth where nodes would flush txes at slightly different times, causing
@@ -720,7 +723,7 @@ namespace cryptonote
txs.reserve(m_blockchain.get_txpool_tx_count(include_unrelayed_txes));
m_blockchain.for_all_txpool_txes([&txs](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
transaction tx;
- if (!parse_and_validate_tx_from_blob(*bd, tx))
+ if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(*bd, tx) : parse_and_validate_tx_from_blob(*bd, tx)))
{
MERROR("Failed to parse tx from txpool");
// continue
@@ -786,7 +789,7 @@ namespace cryptonote
if (meta.double_spend_seen)
++stats.num_double_spends;
return true;
- }, false, include_unrelayed_txes);
+ }, false, include_unrelayed_txes);
stats.bytes_med = epee::misc_utils::median(weights);
if (stats.txs_total > 1)
{
@@ -799,8 +802,15 @@ namespace cryptonote
/* If enough txs, spread the first 98% of results across
* the first 9 bins, drop final 2% in last bin.
*/
- it=agebytes.end();
- for (size_t n=0; n <= end; n++, it--);
+ it = agebytes.end();
+ size_t cumulative_num = 0;
+ /* Since agebytes is not empty and end is nonzero, the
+ * below loop can always run at least once.
+ */
+ do {
+ --it;
+ cumulative_num += it->second.txs;
+ } while (it != agebytes.begin() && cumulative_num < end);
stats.histo_98pc = it->first;
factor = 9;
delta = it->first;
@@ -844,7 +854,7 @@ namespace cryptonote
txi.id_hash = epee::string_tools::pod_to_hex(txid);
txi.tx_blob = *bd;
transaction tx;
- if (!parse_and_validate_tx_from_blob(*bd, tx))
+ if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(*bd, tx) : parse_and_validate_tx_from_blob(*bd, tx)))
{
MERROR("Failed to parse tx from txpool");
// continue
@@ -916,7 +926,7 @@ namespace cryptonote
m_blockchain.for_all_txpool_txes([&tx_infos, key_image_infos](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
cryptonote::rpc::tx_in_pool txi;
txi.tx_hash = txid;
- if (!parse_and_validate_tx_from_blob(*bd, txi.tx))
+ if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(*bd, txi.tx) : parse_and_validate_tx_from_blob(*bd, txi.tx)))
{
MERROR("Failed to parse tx from txpool");
// continue
@@ -954,7 +964,7 @@ namespace cryptonote
return true;
}
//---------------------------------------------------------------------------------
- bool tx_memory_pool::check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool> spent) const
+ bool tx_memory_pool::check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool>& spent) const
{
CRITICAL_REGION_LOCAL(m_transactions_lock);
CRITICAL_REGION_LOCAL1(m_blockchain);
@@ -1196,7 +1206,7 @@ namespace cryptonote
ss << "id: " << txid << std::endl;
if (!short_format) {
cryptonote::transaction tx;
- if (!parse_and_validate_tx_from_blob(*txblob, tx))
+ if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(*txblob, tx) : parse_and_validate_tx_from_blob(*txblob, tx)))
{
MERROR("Failed to parse tx from txpool");
return true; // continue
@@ -1252,6 +1262,12 @@ namespace cryptonote
}
LOG_PRINT_L2("Considering " << sorted_it->second << ", weight " << meta.weight << ", current block weight " << total_weight << "/" << max_total_weight << ", current coinbase " << print_money(best_coinbase));
+ if (meta.pruned)
+ {
+ LOG_PRINT_L2(" tx is pruned");
+ continue;
+ }
+
// Can not exceed maximum block weight
if (max_total_weight < total_weight + meta.weight)
{
@@ -1375,7 +1391,7 @@ namespace cryptonote
{
cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(txid);
cryptonote::transaction tx;
- if (!parse_and_validate_tx_from_blob(txblob, tx))
+ if (!parse_and_validate_tx_from_blob(txblob, tx)) // remove pruned ones on startup, they're meant to be temporary
{
MERROR("Failed to parse tx from txpool");
continue;
diff --git a/src/cryptonote_core/tx_pool.h b/src/cryptonote_core/tx_pool.h
index eade2493a..dec7e3cd9 100644
--- a/src/cryptonote_core/tx_pool.h
+++ b/src/cryptonote_core/tx_pool.h
@@ -139,10 +139,11 @@ namespace cryptonote
* @param relayed return-by-reference was transaction relayed to us by the network?
* @param do_not_relay return-by-reference is transaction not to be relayed to the network?
* @param double_spend_seen return-by-reference was a double spend seen for that transaction?
+ * @param pruned return-by-reference is the tx pruned
*
* @return true unless the transaction cannot be found in the pool
*/
- bool take_tx(const crypto::hash &id, transaction &tx, cryptonote::blobdata &txblob, size_t& tx_weight, uint64_t& fee, bool &relayed, bool &do_not_relay, bool &double_spend_seen);
+ bool take_tx(const crypto::hash &id, transaction &tx, cryptonote::blobdata &txblob, size_t& tx_weight, uint64_t& fee, bool &relayed, bool &do_not_relay, bool &double_spend_seen, bool &pruned);
/**
* @brief checks if the pool has a transaction with the given hash
@@ -300,7 +301,7 @@ namespace cryptonote
*
* @return true
*/
- bool check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool> spent) const;
+ bool check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool>& spent) const;
/**
* @brief get a specific transaction from the pool
diff --git a/src/cryptonote_protocol/block_queue.cpp b/src/cryptonote_protocol/block_queue.cpp
index b4f9daa74..67f0b3e5d 100644
--- a/src/cryptonote_protocol/block_queue.cpp
+++ b/src/cryptonote_protocol/block_queue.cpp
@@ -228,13 +228,14 @@ bool block_queue::have(const crypto::hash &hash) const
return have_blocks.find(hash) != have_blocks.end();
}
-std::pair<uint64_t, uint64_t> block_queue::reserve_span(uint64_t first_block_height, uint64_t last_block_height, uint64_t max_blocks, const boost::uuids::uuid &connection_id, uint32_t pruning_seed, uint64_t blockchain_height, const std::vector<crypto::hash> &block_hashes, boost::posix_time::ptime time)
+std::pair<uint64_t, uint64_t> block_queue::reserve_span(uint64_t first_block_height, uint64_t last_block_height, uint64_t max_blocks, const boost::uuids::uuid &connection_id, bool sync_pruned_blocks, uint32_t local_pruning_seed, uint32_t pruning_seed, uint64_t blockchain_height, const std::vector<std::pair<crypto::hash, uint64_t>> &block_hashes, boost::posix_time::ptime time)
{
boost::unique_lock<boost::recursive_mutex> lock(mutex);
MDEBUG("reserve_span: first_block_height " << first_block_height << ", last_block_height " << last_block_height
- << ", max " << max_blocks << ", seed " << epee::string_tools::to_string_hex(pruning_seed) << ", blockchain_height " <<
- blockchain_height << ", block hashes size " << block_hashes.size());
+ << ", max " << max_blocks << ", peer seed " << epee::string_tools::to_string_hex(pruning_seed) << ", blockchain_height " <<
+ blockchain_height << ", block hashes size " << block_hashes.size() << ", local seed " << epee::string_tools::to_string_hex(local_pruning_seed)
+ << ", sync_pruned_blocks " << sync_pruned_blocks);
if (last_block_height < first_block_height || max_blocks == 0)
{
MDEBUG("reserve_span: early out: first_block_height " << first_block_height << ", last_block_height " << last_block_height << ", max_blocks " << max_blocks);
@@ -248,22 +249,25 @@ std::pair<uint64_t, uint64_t> block_queue::reserve_span(uint64_t first_block_hei
// skip everything we've already requested
uint64_t span_start_height = last_block_height - block_hashes.size() + 1;
- std::vector<crypto::hash>::const_iterator i = block_hashes.begin();
- while (i != block_hashes.end() && requested_internal(*i))
+ std::vector<std::pair<crypto::hash, uint64_t>>::const_iterator i = block_hashes.begin();
+ while (i != block_hashes.end() && requested_internal((*i).first))
{
++i;
++span_start_height;
}
- // if the peer's pruned for the starting block and its unpruned stripe comes next, start downloading from there
- const uint32_t next_unpruned_height = tools::get_next_unpruned_block_height(span_start_height, blockchain_height, pruning_seed);
- MDEBUG("reserve_span: next_unpruned_height " << next_unpruned_height << " from " << span_start_height << " and seed "
- << epee::string_tools::to_string_hex(pruning_seed) << ", limit " << span_start_height + CRYPTONOTE_PRUNING_STRIPE_SIZE);
- if (next_unpruned_height > span_start_height && next_unpruned_height < span_start_height + CRYPTONOTE_PRUNING_STRIPE_SIZE)
+ if (!sync_pruned_blocks)
{
- MDEBUG("We can download from next span: ideal height " << span_start_height << ", next unpruned height " << next_unpruned_height <<
- "(+" << next_unpruned_height - span_start_height << "), current seed " << pruning_seed);
- span_start_height = next_unpruned_height;
+ // if the peer's pruned for the starting block and its unpruned stripe comes next, start downloading from there
+ const uint32_t next_unpruned_height = tools::get_next_unpruned_block_height(span_start_height, blockchain_height, pruning_seed);
+ MDEBUG("reserve_span: next_unpruned_height " << next_unpruned_height << " from " << span_start_height << " and seed "
+ << epee::string_tools::to_string_hex(pruning_seed) << ", limit " << span_start_height + CRYPTONOTE_PRUNING_STRIPE_SIZE);
+ if (next_unpruned_height > span_start_height && next_unpruned_height < span_start_height + CRYPTONOTE_PRUNING_STRIPE_SIZE)
+ {
+ MDEBUG("We can download from next span: ideal height " << span_start_height << ", next unpruned height " << next_unpruned_height <<
+ "(+" << next_unpruned_height - span_start_height << "), current seed " << pruning_seed);
+ span_start_height = next_unpruned_height;
+ }
}
MDEBUG("span_start_height: " <<span_start_height);
const uint64_t block_hashes_start_height = last_block_height - block_hashes.size() + 1;
@@ -274,7 +278,7 @@ std::pair<uint64_t, uint64_t> block_queue::reserve_span(uint64_t first_block_hei
}
i = block_hashes.begin() + span_start_height - block_hashes_start_height;
- while (i != block_hashes.end() && requested_internal(*i))
+ while (i != block_hashes.end() && requested_internal((*i).first))
{
++i;
++span_start_height;
@@ -282,9 +286,16 @@ std::pair<uint64_t, uint64_t> block_queue::reserve_span(uint64_t first_block_hei
uint64_t span_length = 0;
std::vector<crypto::hash> hashes;
- while (i != block_hashes.end() && span_length < max_blocks && tools::has_unpruned_block(span_start_height + span_length, blockchain_height, pruning_seed))
+ bool first_is_pruned = sync_pruned_blocks && !tools::has_unpruned_block(span_start_height + span_length, blockchain_height, local_pruning_seed);
+ while (i != block_hashes.end() && span_length < max_blocks && (sync_pruned_blocks || tools::has_unpruned_block(span_start_height + span_length, blockchain_height, pruning_seed)))
{
- hashes.push_back(*i);
+ // if we want to sync pruned blocks, stop at the first block for which we need full data
+ if (sync_pruned_blocks && first_is_pruned == tools::has_unpruned_block(span_start_height + span_length, blockchain_height, local_pruning_seed))
+ {
+ MDEBUG("Stopping at " << span_start_height + span_length << " for peer on stripe " << tools::get_pruning_stripe(pruning_seed) << " as we need full data for " << tools::get_pruning_stripe(local_pruning_seed));
+ break;
+ }
+ hashes.push_back((*i).first);
++i;
++span_length;
}
diff --git a/src/cryptonote_protocol/block_queue.h b/src/cryptonote_protocol/block_queue.h
index 1bef01d67..93c6532e7 100644
--- a/src/cryptonote_protocol/block_queue.h
+++ b/src/cryptonote_protocol/block_queue.h
@@ -78,7 +78,7 @@ namespace cryptonote
void print() const;
std::string get_overview(uint64_t blockchain_height) const;
bool has_unpruned_height(uint64_t block_height, uint64_t blockchain_height, uint32_t pruning_seed) const;
- std::pair<uint64_t, uint64_t> reserve_span(uint64_t first_block_height, uint64_t last_block_height, uint64_t max_blocks, const boost::uuids::uuid &connection_id, uint32_t pruning_seed, uint64_t blockchain_height, const std::vector<crypto::hash> &block_hashes, boost::posix_time::ptime time = boost::posix_time::microsec_clock::universal_time());
+ std::pair<uint64_t, uint64_t> reserve_span(uint64_t first_block_height, uint64_t last_block_height, uint64_t max_blocks, const boost::uuids::uuid &connection_id, bool sync_pruned_blocks, uint32_t local_pruning_seed, uint32_t pruning_seed, uint64_t blockchain_height, const std::vector<std::pair<crypto::hash, uint64_t>> &block_hashes, boost::posix_time::ptime time = boost::posix_time::microsec_clock::universal_time());
uint64_t get_next_needed_height(uint64_t blockchain_height) const;
std::pair<uint64_t, uint64_t> get_next_span_if_scheduled(std::vector<crypto::hash> &hashes, boost::uuids::uuid &connection_id, boost::posix_time::ptime &time) const;
void reset_next_span_time(boost::posix_time::ptime t = boost::posix_time::microsec_clock::universal_time());
diff --git a/src/cryptonote_protocol/cryptonote_protocol_defs.h b/src/cryptonote_protocol/cryptonote_protocol_defs.h
index b2f8da399..3d594bf83 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_defs.h
+++ b/src/cryptonote_protocol/cryptonote_protocol_defs.h
@@ -116,14 +116,51 @@ namespace cryptonote
/************************************************************************/
/* */
/************************************************************************/
+ struct tx_blob_entry
+ {
+ blobdata blob;
+ crypto::hash prunable_hash;
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(blob)
+ KV_SERIALIZE_VAL_POD_AS_BLOB(prunable_hash)
+ END_KV_SERIALIZE_MAP()
+
+ tx_blob_entry(const blobdata &bd = {}, const crypto::hash &h = crypto::null_hash): blob(bd), prunable_hash(h) {}
+ };
struct block_complete_entry
{
+ bool pruned;
blobdata block;
- std::vector<blobdata> txs;
+ uint64_t block_weight;
+ std::vector<tx_blob_entry> txs;
BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE_OPT(pruned, false)
KV_SERIALIZE(block)
- KV_SERIALIZE(txs)
+ KV_SERIALIZE_OPT(block_weight, (uint64_t)0)
+ if (this_ref.pruned)
+ {
+ KV_SERIALIZE(txs)
+ }
+ else
+ {
+ std::vector<blobdata> txs;
+ if (is_store)
+ {
+ txs.reserve(this_ref.txs.size());
+ for (const auto &e: this_ref.txs) txs.push_back(e.blob);
+ }
+ epee::serialization::selector<is_store>::serialize(txs, stg, hparent_section, "txs");
+ if (!is_store)
+ {
+ block_complete_entry &self = const_cast<block_complete_entry&>(this_ref);
+ self.txs.clear();
+ self.txs.reserve(txs.size());
+ for (auto &e: txs) self.txs.push_back({std::move(e), crypto::null_hash});
+ }
+ }
END_KV_SERIALIZE_MAP()
+
+ block_complete_entry(): pruned(false) {}
};
@@ -176,8 +213,11 @@ namespace cryptonote
struct request_t
{
std::vector<crypto::hash> blocks;
+ bool prune;
+
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE_CONTAINER_POD_AS_BLOB(blocks)
+ KV_SERIALIZE_OPT(prune, false)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;
@@ -229,9 +269,11 @@ namespace cryptonote
struct request_t
{
std::list<crypto::hash> block_ids; /*IDs of the first 10 blocks are sequential, next goes with pow(2,n) offset, like 2, 4, 8, 16, 32, 64 and so on, and the last one is always genesis block */
+ bool prune;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE_CONTAINER_POD_AS_BLOB(block_ids)
+ KV_SERIALIZE_OPT(prune, false)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;
@@ -248,6 +290,7 @@ namespace cryptonote
uint64_t cumulative_difficulty;
uint64_t cumulative_difficulty_top64;
std::vector<crypto::hash> m_block_ids;
+ std::vector<uint64_t> m_block_weights;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(start_height)
@@ -255,6 +298,7 @@ namespace cryptonote
KV_SERIALIZE(cumulative_difficulty)
KV_SERIALIZE(cumulative_difficulty_top64)
KV_SERIALIZE_CONTAINER_POD_AS_BLOB(m_block_ids)
+ KV_SERIALIZE_CONTAINER_POD_AS_BLOB(m_block_weights)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h
index dcc5ec6ed..b16b42564 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler.h
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h
@@ -137,7 +137,9 @@ namespace cryptonote
size_t get_synchronizing_connections_count();
bool on_connection_synchronized();
bool should_download_next_span(cryptonote_connection_context& context, bool standby);
+ bool should_ask_for_pruned_data(cryptonote_connection_context& context, uint64_t first_block_height, uint64_t nblocks, bool check_block_weights) const;
void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans);
+ void drop_connection_with_score(cryptonote_connection_context &context, unsigned int score, bool flush_all_spans);
bool kick_idle_peers();
bool check_standby_peers();
bool update_sync_search();
@@ -164,6 +166,7 @@ namespace cryptonote
uint64_t m_sync_spans_downloaded, m_sync_old_spans_downloaded, m_sync_bad_spans_downloaded;
uint64_t m_sync_download_chain_size, m_sync_download_objects_size;
size_t m_block_download_max_size;
+ bool m_sync_pruned_blocks;
boost::mutex m_buffer_mutex;
double get_avg_block_size();
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl
index 65115ee72..bc5c8d6de 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl
@@ -55,7 +55,7 @@
if (ELPP->vRegistry()->allowed(level, cat)) { \
init; \
if (test) \
- el::base::Writer(level, __FILE__, __LINE__, ELPP_FUNC, el::base::DispatchAction::NormalLog).construct(cat) << x; \
+ el::base::Writer(level, el::Color::Default, __FILE__, __LINE__, ELPP_FUNC, el::base::DispatchAction::NormalLog).construct(cat) << x; \
} \
} while(0)
@@ -106,6 +106,7 @@ namespace cryptonote
m_sync_download_objects_size = 0;
m_block_download_max_size = command_line::get_arg(vm, cryptonote::arg_block_download_max_size);
+ m_sync_pruned_blocks = command_line::get_arg(vm, cryptonote::arg_sync_pruned_blocks);
return true;
}
@@ -134,10 +135,11 @@ namespace cryptonote
if(context.m_state == cryptonote_connection_context::state_synchronizing)
{
- NOTIFY_REQUEST_CHAIN::request r = boost::value_initialized<NOTIFY_REQUEST_CHAIN::request>();
+ NOTIFY_REQUEST_CHAIN::request r = {};
context.m_needed_objects.clear();
m_core.get_short_chain_history(r.block_ids);
handler_request_blocks_history( r.block_ids ); // change the limit(?), sleep(?)
+ r.prune = m_sync_pruned_blocks;
MLOG_P2P_MESSAGE("-->>NOTIFY_REQUEST_CHAIN: m_block_ids.size()=" << r.block_ids.size() );
post_notify<NOTIFY_REQUEST_CHAIN>(r, context);
MLOG_PEER_STATE("requesting chain");
@@ -342,11 +344,6 @@ namespace cryptonote
if(m_core.have_block(hshd.top_id))
{
- if (target > m_core.get_current_blockchain_height())
- {
- MINFO(context << "peer is not ahead of us and we're syncing, disconnecting");
- return false;
- }
context.m_state = cryptonote_connection_context::state_normal;
if(is_inital && target == m_core.get_current_blockchain_height())
on_connection_synchronized();
@@ -370,7 +367,7 @@ namespace cryptonote
uint64_t max_block_height = std::max(hshd.current_height,m_core.get_current_blockchain_height());
uint64_t last_block_v1 = m_core.get_nettype() == TESTNET ? 624633 : m_core.get_nettype() == MAINNET ? 1009826 : (uint64_t)-1;
uint64_t diff_v2 = max_block_height > last_block_v1 ? std::min(abs_diff, max_block_height - last_block_v1) : 0;
- MCLOG(is_inital ? el::Level::Info : el::Level::Debug, "global", context << "Sync data returned a new top block candidate: " << m_core.get_current_blockchain_height() << " -> " << hshd.current_height
+ MCLOG(is_inital ? el::Level::Info : el::Level::Debug, "global", el::Color::Yellow, context << "Sync data returned a new top block candidate: " << m_core.get_current_blockchain_height() << " -> " << hshd.current_height
<< " [Your node is " << abs_diff << " blocks (" << ((abs_diff - diff_v2) / (24 * 60 * 60 / DIFFICULTY_TARGET_V1)) + (diff_v2 / (24 * 60 * 60 / DIFFICULTY_TARGET_V2)) << " days) "
<< (0 <= diff ? std::string("behind") : std::string("ahead"))
<< "] " << ENDL << "SYNCHRONIZATION started");
@@ -426,7 +423,7 @@ namespace cryptonote
template<class t_core>
bool t_cryptonote_protocol_handler<t_core>::get_payload_sync_data(blobdata& data)
{
- CORE_SYNC_DATA hsd = boost::value_initialized<CORE_SYNC_DATA>();
+ CORE_SYNC_DATA hsd = {};
get_payload_sync_data(hsd);
epee::serialization::store_t_to_binary(hsd, data);
return true;
@@ -468,7 +465,7 @@ namespace cryptonote
}
}
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
m_core.handle_incoming_block(arg.b.block, pblocks.empty() ? NULL : &pblocks[0], bvc); // got block from handle_notify_new_block
if (!m_core.cleanup_handle_incoming_blocks(true))
{
@@ -480,7 +477,7 @@ namespace cryptonote
if(bvc.m_verifivation_failed)
{
LOG_PRINT_CCONTEXT_L0("Block verification failed, dropping connection");
- drop_connection(context, true, false);
+ drop_connection_with_score(context, bvc.m_bad_pow ? P2P_IP_FAILS_BEFORE_BLOCK : 1, false);
return 1;
}
if(bvc.m_added_to_main_chain)
@@ -491,8 +488,9 @@ namespace cryptonote
{
context.m_needed_objects.clear();
context.m_state = cryptonote_connection_context::state_synchronizing;
- NOTIFY_REQUEST_CHAIN::request r = boost::value_initialized<NOTIFY_REQUEST_CHAIN::request>();
+ NOTIFY_REQUEST_CHAIN::request r = {};
m_core.get_short_chain_history(r.block_ids);
+ r.prune = m_sync_pruned_blocks;
handler_request_blocks_history( r.block_ids ); // change the limit(?), sleep(?)
MLOG_P2P_MESSAGE("-->>NOTIFY_REQUEST_CHAIN: m_block_ids.size()=" << r.block_ids.size() );
post_notify<NOTIFY_REQUEST_CHAIN>(r, context);
@@ -540,9 +538,9 @@ namespace cryptonote
return 1;
}
}
-
- std::vector<blobdata> have_tx;
-
+
+ std::vector<tx_blob_entry> have_tx;
+
// Instead of requesting missing transactions by hash like BTC,
// we do it by index (thanks to a suggestion from moneromooo) because
// we're way cooler .. and also because they're smaller than hashes.
@@ -556,7 +554,7 @@ namespace cryptonote
for(auto& tx_blob: arg.b.txs)
{
- if(parse_and_validate_tx_from_blob(tx_blob, tx))
+ if(parse_and_validate_tx_from_blob(tx_blob.blob, tx))
{
try
{
@@ -641,7 +639,7 @@ namespace cryptonote
LOG_ERROR_CCONTEXT
(
"sent wrong tx: failed to parse and validate transaction: "
- << epee::string_tools::buff_to_hex_nodelimer(tx_blob)
+ << epee::string_tools::buff_to_hex_nodelimer(tx_blob.blob)
<< ", dropping connection"
);
@@ -676,7 +674,7 @@ namespace cryptonote
cryptonote::blobdata txblob;
if(m_core.get_pool_transaction(tx_hash, txblob))
{
- have_tx.push_back(txblob);
+ have_tx.push_back({txblob, crypto::null_hash});
}
else
{
@@ -688,7 +686,7 @@ namespace cryptonote
{
if (txes.size() == 1)
{
- have_tx.push_back(tx_to_blob(txes.front()));
+ have_tx.push_back({tx_to_blob(txes.front()), crypto::null_hash});
}
else
{
@@ -740,7 +738,7 @@ namespace cryptonote
return 1;
}
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
m_core.handle_incoming_block(arg.b.block, pblocks.empty() ? NULL : &pblocks[0], bvc); // got block from handle_notify_new_block
if (!m_core.cleanup_handle_incoming_blocks(true))
{
@@ -753,7 +751,7 @@ namespace cryptonote
if( bvc.m_verifivation_failed )
{
LOG_PRINT_CCONTEXT_L0("Block verification failed, dropping connection");
- drop_connection(context, true, false);
+ drop_connection_with_score(context, bvc.m_bad_pow ? P2P_IP_FAILS_BEFORE_BLOCK : 1, false);
return 1;
}
if( bvc.m_added_to_main_chain )
@@ -768,9 +766,10 @@ namespace cryptonote
{
context.m_needed_objects.clear();
context.m_state = cryptonote_connection_context::state_synchronizing;
- NOTIFY_REQUEST_CHAIN::request r = boost::value_initialized<NOTIFY_REQUEST_CHAIN::request>();
+ NOTIFY_REQUEST_CHAIN::request r = {};
m_core.get_short_chain_history(r.block_ids);
handler_request_blocks_history( r.block_ids ); // change the limit(?), sleep(?)
+ r.prune = m_sync_pruned_blocks;
MLOG_P2P_MESSAGE("-->>NOTIFY_REQUEST_CHAIN: m_block_ids.size()=" << r.block_ids.size() );
post_notify<NOTIFY_REQUEST_CHAIN>(r, context);
MLOG_PEER_STATE("requesting chain");
@@ -872,7 +871,7 @@ namespace cryptonote
for(auto& tx: txs)
{
- fluffy_response.b.txs.push_back(t_serializable_object_to_blob(tx));
+ fluffy_response.b.txs.push_back({t_serializable_object_to_blob(tx), crypto::null_hash});
}
MLOG_P2P_MESSAGE
@@ -910,7 +909,7 @@ namespace cryptonote
for (size_t i = 0; i < arg.txs.size(); ++i)
{
cryptonote::tx_verification_context tvc = AUTO_VAL_INIT(tvc);
- m_core.handle_incoming_tx(arg.txs[i], tvc, false, true, false);
+ m_core.handle_incoming_tx({arg.txs[i], crypto::null_hash}, tvc, false, true, false);
if(tvc.m_verifivation_failed)
{
LOG_PRINT_CCONTEXT_L1("Tx verification failed, dropping connection");
@@ -991,7 +990,7 @@ namespace cryptonote
for (const auto &element : arg.blocks) {
blocks_size += element.block.size();
for (const auto &tx : element.txs)
- blocks_size += tx.size();
+ blocks_size += tx.blob.size();
}
size += blocks_size;
@@ -1090,6 +1089,53 @@ namespace cryptonote
return 1;
}
+ const bool pruned_ok = should_ask_for_pruned_data(context, start_height, arg.blocks.size(), true);
+ if (!pruned_ok)
+ {
+ // if we don't want pruned data, check we did not get any
+ for (block_complete_entry& block_entry: arg.blocks)
+ {
+ if (block_entry.pruned)
+ {
+ MERROR(context << "returned a pruned block, dropping connection");
+ drop_connection(context, false, false);
+ ++m_sync_bad_spans_downloaded;
+ return 1;
+ }
+ if (block_entry.block_weight)
+ {
+ MERROR(context << "returned a block weight for a non pruned block, dropping connection");
+ drop_connection(context, false, false);
+ ++m_sync_bad_spans_downloaded;
+ return 1;
+ }
+ for (const tx_blob_entry &tx_entry: block_entry.txs)
+ {
+ if (tx_entry.prunable_hash != crypto::null_hash)
+ {
+ MERROR(context << "returned at least one pruned object which we did not expect, dropping connection");
+ drop_connection(context, false, false);
+ ++m_sync_bad_spans_downloaded;
+ return 1;
+ }
+ }
+ }
+ }
+ else
+ {
+ // we accept pruned data, check that if we got some, then no weights are zero
+ for (block_complete_entry& block_entry: arg.blocks)
+ {
+ if (block_entry.block_weight == 0 && block_entry.pruned)
+ {
+ MERROR(context << "returned at least one pruned block with 0 weight, dropping connection");
+ drop_connection(context, false, false);
+ ++m_sync_bad_spans_downloaded;
+ return 1;
+ }
+ }
+ }
+
{
MLOG_YELLOW(el::Level::Debug, context << " Got NEW BLOCKS inside of " << __FUNCTION__ << ": size: " << arg.blocks.size()
<< ", blocks: " << start_height << " - " << (start_height + arg.blocks.size() - 1) <<
@@ -1273,18 +1319,32 @@ namespace cryptonote
if (tvc.size() != block_entry.txs.size())
{
LOG_ERROR_CCONTEXT("Internal error: tvc.size() != block_entry.txs.size()");
+ if (!m_core.cleanup_handle_incoming_blocks())
+ {
+ LOG_PRINT_CCONTEXT_L0("Failure in cleanup_handle_incoming_blocks");
+ return 1;
+ }
return 1;
}
- std::vector<blobdata>::const_iterator it = block_entry.txs.begin();
+ std::vector<tx_blob_entry>::const_iterator it = block_entry.txs.begin();
for (size_t i = 0; i < tvc.size(); ++i, ++it)
{
if(tvc[i].m_verifivation_failed)
{
if (!m_p2p->for_connection(span_connection_id, [&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t f)->bool{
cryptonote::transaction tx;
- parse_and_validate_tx_from_blob(*it, tx); // must succeed if we got here
+ crypto::hash txid;
+ if (it->prunable_hash == crypto::null_hash)
+ {
+ parse_and_validate_tx_from_blob(it->blob, tx, txid); // must succeed if we got here
+ }
+ else
+ {
+ parse_and_validate_tx_base_from_blob(it->blob, tx); // must succeed if we got here
+ txid = get_pruned_transaction_hash(tx, it->prunable_hash);
+ }
LOG_ERROR_CCONTEXT("transaction verification failed on NOTIFY_RESPONSE_GET_OBJECTS, tx_id = "
- << epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(tx)) << ", dropping connection");
+ << epee::string_tools::pod_to_hex(txid) << ", dropping connection");
drop_connection(context, false, true);
return 1;
}))
@@ -1306,7 +1366,7 @@ namespace cryptonote
// process block
TIME_MEASURE_START(block_process_time);
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ block_verification_context bvc = {};
m_core.handle_incoming_block(block_entry.block, pblocks.empty() ? NULL : &pblocks[blockidx], bvc, false); // <--- process block
@@ -1314,7 +1374,7 @@ namespace cryptonote
{
if (!m_p2p->for_connection(span_connection_id, [&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t f)->bool{
LOG_PRINT_CCONTEXT_L1("Block verification failed, dropping connection");
- drop_connection(context, true, true);
+ drop_connection_with_score(context, bvc.m_bad_pow ? P2P_IP_FAILS_BEFORE_BLOCK : 1, true);
return 1;
}))
LOG_ERROR_CCONTEXT("span connection id not found");
@@ -1543,7 +1603,7 @@ skip:
{
MLOG_P2P_MESSAGE("Received NOTIFY_REQUEST_CHAIN (" << arg.block_ids.size() << " blocks");
NOTIFY_RESPONSE_CHAIN_ENTRY::request r;
- if(!m_core.find_blockchain_supplement(arg.block_ids, r))
+ if(!m_core.find_blockchain_supplement(arg.block_ids, !arg.prune, r))
{
LOG_ERROR_CCONTEXT("Failed to handle NOTIFY_REQUEST_CHAIN.");
drop_connection(context, false, false);
@@ -1662,6 +1722,12 @@ skip:
MDEBUG(context << "This peer has needed stripe " << peer_stripe << ", not dropping");
return false;
}
+ const uint32_t local_stripe = tools::get_pruning_stripe(m_core.get_blockchain_pruning_seed());
+ if (m_sync_pruned_blocks && peer_stripe == local_stripe)
+ {
+ MDEBUG(context << "We can sync pruned blocks off this peer, not dropping");
+ return false;
+ }
if (!context.m_needed_objects.empty())
{
@@ -1701,22 +1767,42 @@ skip:
{
// take out blocks we already have
size_t skip = 0;
- while (skip < context.m_needed_objects.size() && (m_core.have_block(context.m_needed_objects[skip]) || (check_block_queue && m_block_queue.have(context.m_needed_objects[skip]))))
+ while (skip < context.m_needed_objects.size() && (m_core.have_block(context.m_needed_objects[skip].first) || (check_block_queue && m_block_queue.have(context.m_needed_objects[skip].first))))
{
// if we're popping the last hash, record it so we can ask again from that hash,
// this prevents never being able to progress on peers we get old hash lists from
if (skip + 1 == context.m_needed_objects.size())
- context.m_last_known_hash = context.m_needed_objects[skip];
+ context.m_last_known_hash = context.m_needed_objects[skip].first;
++skip;
}
if (skip > 0)
{
MDEBUG(context << "skipping " << skip << "/" << context.m_needed_objects.size() << " blocks");
- context.m_needed_objects = std::vector<crypto::hash>(context.m_needed_objects.begin() + skip, context.m_needed_objects.end());
+ context.m_needed_objects = std::vector<std::pair<crypto::hash, uint64_t>>(context.m_needed_objects.begin() + skip, context.m_needed_objects.end());
}
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
+ bool t_cryptonote_protocol_handler<t_core>::should_ask_for_pruned_data(cryptonote_connection_context& context, uint64_t first_block_height, uint64_t nblocks, bool check_block_weights) const
+ {
+ if (!m_sync_pruned_blocks)
+ return false;
+ if (!m_core.is_within_compiled_block_hash_area(first_block_height + nblocks - 1))
+ return false;
+ const uint32_t local_stripe = tools::get_pruning_stripe(m_core.get_blockchain_pruning_seed());
+ if (local_stripe == 0)
+ return false;
+ // assumes the span size is less or equal to the stripe size
+ bool full_data_needed = tools::get_pruning_stripe(first_block_height, context.m_remote_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES) == local_stripe
+ || tools::get_pruning_stripe(first_block_height + nblocks - 1, context.m_remote_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES) == local_stripe;
+ if (full_data_needed)
+ return false;
+ if (check_block_weights && !m_core.has_block_weights(first_block_height, nblocks))
+ return false;
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------
+ template<class t_core>
bool t_cryptonote_protocol_handler<t_core>::request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks, bool force_next_span)
{
// flush stale spans
@@ -1739,6 +1825,7 @@ skip:
const auto next_needed_pruning_stripe = get_next_needed_pruning_stripe();
const uint32_t add_stripe = tools::get_pruning_stripe(bc_height, context.m_remote_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES);
const uint32_t peer_stripe = tools::get_pruning_stripe(context.m_pruning_seed);
+ const uint32_t local_stripe = tools::get_pruning_stripe(m_core.get_blockchain_pruning_seed());
const size_t block_queue_size_threshold = m_block_download_max_size ? m_block_download_max_size : BLOCK_QUEUE_SIZE_THRESHOLD;
bool queue_proceed = nspans < BLOCK_QUEUE_NSPANS_THRESHOLD || size < block_queue_size_threshold;
// get rid of blocks we already requested, or already have
@@ -1749,7 +1836,7 @@ skip:
next_block_height = next_needed_height;
else
next_block_height = context.m_last_response_height - context.m_needed_objects.size() + 1;
- bool stripe_proceed_main = (add_stripe == 0 || peer_stripe == 0 || add_stripe == peer_stripe) && (next_block_height < bc_height + BLOCK_QUEUE_FORCE_DOWNLOAD_NEAR_BLOCKS || next_needed_height < bc_height + BLOCK_QUEUE_FORCE_DOWNLOAD_NEAR_BLOCKS);
+ bool stripe_proceed_main = ((m_sync_pruned_blocks && peer_stripe == local_stripe) || add_stripe == 0 || peer_stripe == 0 || add_stripe == peer_stripe) && (next_block_height < bc_height + BLOCK_QUEUE_FORCE_DOWNLOAD_NEAR_BLOCKS || next_needed_height < bc_height + BLOCK_QUEUE_FORCE_DOWNLOAD_NEAR_BLOCKS);
bool stripe_proceed_secondary = tools::has_unpruned_block(next_block_height, context.m_remote_blockchain_height, context.m_pruning_seed);
bool proceed = stripe_proceed_main || (queue_proceed && stripe_proceed_secondary);
if (!stripe_proceed_main && !stripe_proceed_secondary && should_drop_connection(context, tools::get_pruning_stripe(next_block_height, context.m_remote_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES)))
@@ -1812,8 +1899,9 @@ skip:
{
const uint64_t now = tools::get_tick_count();
const uint64_t dt = now - m_last_add_end_time;
- if (tools::ticks_to_ns(dt) >= DROP_ON_SYNC_WEDGE_THRESHOLD)
+ if (m_last_add_end_time && tools::ticks_to_ns(dt) >= DROP_ON_SYNC_WEDGE_THRESHOLD)
{
+ MDEBUG(context << "ns " << tools::ticks_to_ns(dt) << " from " << m_last_add_end_time << " and " << now);
MDEBUG(context << "Block addition seems to have wedged, dropping connection");
return false;
}
@@ -1880,7 +1968,8 @@ skip:
skip_unneeded_hashes(context, false);
const uint64_t first_block_height = context.m_last_response_height - context.m_needed_objects.size() + 1;
- span = m_block_queue.reserve_span(first_block_height, context.m_last_response_height, count_limit, context.m_connection_id, context.m_pruning_seed, context.m_remote_blockchain_height, context.m_needed_objects);
+ bool sync_pruned_blocks = m_sync_pruned_blocks && m_core.get_blockchain_pruning_seed();
+ span = m_block_queue.reserve_span(first_block_height, context.m_last_response_height, count_limit, context.m_connection_id, sync_pruned_blocks, m_core.get_blockchain_pruning_seed(), context.m_pruning_seed, context.m_remote_blockchain_height, context.m_needed_objects);
MDEBUG(context << " span from " << first_block_height << ": " << span.first << "/" << span.second);
if (span.second > 0)
{
@@ -1910,7 +1999,8 @@ skip:
++count;
context.m_requested_objects.insert(hash);
// that's atrocious O(n) wise, but this is rare
- auto i = std::find(context.m_needed_objects.begin(), context.m_needed_objects.end(), hash);
+ auto i = std::find_if(context.m_needed_objects.begin(), context.m_needed_objects.end(),
+ [&hash](const std::pair<crypto::hash, uint64_t> &o) { return o.first == hash; });
if (i != context.m_needed_objects.end())
context.m_needed_objects.erase(i);
}
@@ -1929,7 +2019,7 @@ skip:
return false;
}
if (skip > 0)
- context.m_needed_objects = std::vector<crypto::hash>(context.m_needed_objects.begin() + skip, context.m_needed_objects.end());
+ context.m_needed_objects = std::vector<std::pair<crypto::hash, uint64_t>>(context.m_needed_objects.begin() + skip, context.m_needed_objects.end());
if (context.m_needed_objects.size() < span.second)
{
MERROR("ERROR: span " << span.first << "/" << span.second << ", m_needed_objects " << context.m_needed_objects.size());
@@ -1938,18 +2028,37 @@ skip:
for (size_t n = 0; n < span.second; ++n)
{
- req.blocks.push_back(context.m_needed_objects[n]);
+ req.blocks.push_back(context.m_needed_objects[n].first);
++count;
- context.m_requested_objects.insert(context.m_needed_objects[n]);
+ context.m_requested_objects.insert(context.m_needed_objects[n].first);
}
- context.m_needed_objects = std::vector<crypto::hash>(context.m_needed_objects.begin() + span.second, context.m_needed_objects.end());
+ context.m_needed_objects = std::vector<std::pair<crypto::hash, uint64_t>>(context.m_needed_objects.begin() + span.second, context.m_needed_objects.end());
}
+ req.prune = should_ask_for_pruned_data(context, span.first, span.second, true);
+
+ // if we need to ask for full data and that peer does not have the right stripe, we can't ask it
+ if (!req.prune && context.m_pruning_seed)
+ {
+ const uint32_t peer_stripe = tools::get_pruning_stripe(context.m_pruning_seed);
+ const uint32_t first_stripe = tools::get_pruning_stripe(span.first, context.m_remote_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES);
+ const uint32_t last_stripe = tools::get_pruning_stripe(span.first + span.second - 1, context.m_remote_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES);
+ if ((first_stripe && peer_stripe != first_stripe) || (last_stripe && peer_stripe != last_stripe))
+ {
+ MDEBUG(context << "We need full data, but the peer does not have it, dropping peer");
+ return false;
+ }
+ }
context.m_last_request_time = boost::posix_time::microsec_clock::universal_time();
MLOG_P2P_MESSAGE("-->>NOTIFY_REQUEST_GET_OBJECTS: blocks.size()=" << req.blocks.size()
<< "requested blocks count=" << count << " / " << count_limit << " from " << span.first << ", first hash " << req.blocks.front());
//epee::net_utils::network_throttle_manager::get_global_throttle_inreq().logger_handle_net("log/dr-monero/net/req-all.data", sec, get_avg_block_size());
+ MDEBUG("Asking for " << (req.prune ? "pruned" : "full") << " data, start/end "
+ << tools::get_pruning_stripe(span.first, context.m_remote_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES)
+ << "/" << tools::get_pruning_stripe(span.first + span.second - 1, context.m_remote_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES)
+ << ", ours " << tools::get_pruning_stripe(m_core.get_blockchain_pruning_seed()) << ", peer stripe " << tools::get_pruning_stripe(context.m_pruning_seed));
+
post_notify<NOTIFY_REQUEST_GET_OBJECTS>(req, context);
MLOG_PEER_STATE("requesting objects");
return true;
@@ -1959,7 +2068,8 @@ skip:
// drop it to make space for other peers, or ask for a span further down the line
const uint32_t next_stripe = get_next_needed_pruning_stripe().first;
const uint32_t peer_stripe = tools::get_pruning_stripe(context.m_pruning_seed);
- if (next_stripe && peer_stripe && next_stripe != peer_stripe)
+ const uint32_t local_stripe = tools::get_pruning_stripe(m_core.get_blockchain_pruning_seed());
+ if (!(m_sync_pruned_blocks && peer_stripe == local_stripe) && next_stripe && peer_stripe && next_stripe != peer_stripe)
{
// at this point, we have to either close the connection, or start getting blocks past the
// current point, or become dormant
@@ -2010,7 +2120,7 @@ skip:
if(context.m_last_response_height < context.m_remote_blockchain_height-1)
{//we have to fetch more objects ids, request blockchain entry
- NOTIFY_REQUEST_CHAIN::request r = boost::value_initialized<NOTIFY_REQUEST_CHAIN::request>();
+ NOTIFY_REQUEST_CHAIN::request r = {};
m_core.get_short_chain_history(r.block_ids);
CHECK_AND_ASSERT_MES(!r.block_ids.empty(), false, "Short chain history is empty");
@@ -2022,6 +2132,7 @@ skip:
}
handler_request_blocks_history( r.block_ids ); // change the limit(?), sleep(?)
+ r.prune = m_sync_pruned_blocks;
//std::string blob; // for calculate size of request
//epee::serialization::store_t_to_binary(r, blob);
@@ -2064,7 +2175,7 @@ skip:
bool t_cryptonote_protocol_handler<t_core>::on_connection_synchronized()
{
bool val_expected = false;
- if(m_synchronized.compare_exchange_strong(val_expected, true))
+ if(!m_core.is_within_compiled_block_hash_area(m_core.get_current_blockchain_height()) && m_synchronized.compare_exchange_strong(val_expected, true))
{
MGINFO_YELLOW(ENDL << "**********************************************************************" << ENDL
<< "You are now synchronized with the network. You may now start monero-wallet-cli." << ENDL
@@ -2128,6 +2239,12 @@ skip:
drop_connection(context, true, false);
return 1;
}
+ if (!arg.m_block_weights.empty() && arg.m_block_weights.size() != arg.m_block_ids.size())
+ {
+ LOG_ERROR_CCONTEXT("sent invalid block weight array, dropping connection");
+ drop_connection(context, true, false);
+ return 1;
+ }
MDEBUG(context << "first block hash " << arg.m_block_ids.front() << ", last " << arg.m_block_ids.back());
if (arg.total_height >= CRYPTONOTE_MAX_BLOCK_NUMBER || arg.m_block_ids.size() >= CRYPTONOTE_MAX_BLOCK_NUMBER)
@@ -2147,7 +2264,7 @@ skip:
return 1;
}
- uint64_t n_use_blocks = m_core.prevalidate_block_hashes(arg.start_height, arg.m_block_ids);
+ uint64_t n_use_blocks = m_core.prevalidate_block_hashes(arg.start_height, arg.m_block_ids, arg.m_block_weights);
if (n_use_blocks + HASH_OF_HASHES_STEP <= arg.m_block_ids.size())
{
LOG_ERROR_CCONTEXT("Most blocks are invalid, dropping connection");
@@ -2157,9 +2274,10 @@ skip:
context.m_needed_objects.clear();
uint64_t added = 0;
- for(auto& bl_id: arg.m_block_ids)
+ for (size_t i = 0; i < arg.m_block_ids.size(); ++i)
{
- context.m_needed_objects.push_back(bl_id);
+ const uint64_t block_weight = arg.m_block_weights.empty() ? 0 : arg.m_block_weights[i];
+ context.m_needed_objects.push_back(std::make_pair(arg.m_block_ids[i], block_weight));
if (++added == n_use_blocks)
break;
}
@@ -2183,7 +2301,7 @@ skip:
{
NOTIFY_NEW_FLUFFY_BLOCK::request fluffy_arg = AUTO_VAL_INIT(fluffy_arg);
fluffy_arg.current_blockchain_height = arg.current_blockchain_height;
- std::vector<blobdata> fluffy_txs;
+ std::vector<tx_blob_entry> fluffy_txs;
fluffy_arg.b = arg.b;
fluffy_arg.b.txs = fluffy_txs;
@@ -2310,14 +2428,14 @@ skip:
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
- void t_cryptonote_protocol_handler<t_core>::drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans)
+ void t_cryptonote_protocol_handler<t_core>::drop_connection_with_score(cryptonote_connection_context &context, unsigned score, bool flush_all_spans)
{
LOG_DEBUG_CC(context, "dropping connection id " << context.m_connection_id << " (pruning seed " <<
epee::string_tools::to_string_hex(context.m_pruning_seed) <<
- "), add_fail " << add_fail << ", flush_all_spans " << flush_all_spans);
+ "), score " << score << ", flush_all_spans " << flush_all_spans);
- if (add_fail)
- m_p2p->add_host_fail(context.m_remote_address);
+ if (score > 0)
+ m_p2p->add_host_fail(context.m_remote_address, score);
m_block_queue.flush_spans(context.m_connection_id, flush_all_spans);
@@ -2325,6 +2443,12 @@ skip:
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
+ void t_cryptonote_protocol_handler<t_core>::drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans)
+ {
+ return drop_connection_with_score(context, add_fail ? 1 : 0, flush_all_spans);
+ }
+ //------------------------------------------------------------------------------------------------------------------------
+ template<class t_core>
void t_cryptonote_protocol_handler<t_core>::on_connection_close(cryptonote_connection_context &context)
{
uint64_t target = 0;
diff --git a/src/cryptonote_protocol/levin_notify.cpp b/src/cryptonote_protocol/levin_notify.cpp
index 26cd93b5a..4b41b5bfc 100644
--- a/src/cryptonote_protocol/levin_notify.cpp
+++ b/src/cryptonote_protocol/levin_notify.cpp
@@ -187,14 +187,15 @@ namespace levin
{
struct zone
{
- explicit zone(boost::asio::io_service& io_service, std::shared_ptr<connections> p2p, epee::byte_slice noise_in)
+ explicit zone(boost::asio::io_service& io_service, std::shared_ptr<connections> p2p, epee::byte_slice noise_in, bool is_public)
: p2p(std::move(p2p)),
noise(std::move(noise_in)),
next_epoch(io_service),
strand(io_service),
map(),
channels(),
- connection_count(0)
+ connection_count(0),
+ is_public(is_public)
{
for (std::size_t count = 0; !noise.empty() && count < CRYPTONOTE_NOISE_CHANNELS; ++count)
channels.emplace_back(io_service);
@@ -207,6 +208,7 @@ namespace levin
net::dandelionpp::connection_map map;//!< Tracks outgoing uuid's for noise channels or Dandelion++ stems
std::deque<noise_channel> channels; //!< Never touch after init; only update elements on `noise_channel.strand`
std::atomic<std::size_t> connection_count; //!< Only update in strand, can be read at any time
+ const bool is_public; //!< Zone is public ipv4/ipv6 connections
};
} // detail
@@ -276,7 +278,10 @@ namespace levin
std::vector<boost::uuids::uuid> connections;
connections.reserve(connection_id_reserve_size);
zone_->p2p->foreach_connection([this, &connections] (detail::p2p_context& context) {
- if (this->source_ != context.m_connection_id)
+ /* Only send to outgoing connections when "flooding" over i2p/tor.
+ Otherwise this makes the tx linkable to a hidden service address,
+ making things linkable across connections. */
+ if (this->source_ != context.m_connection_id && (this->zone_->is_public || !context.m_is_income))
connections.emplace_back(context.m_connection_id);
return true;
});
@@ -476,8 +481,8 @@ namespace levin
};
} // anonymous
- notify::notify(boost::asio::io_service& service, std::shared_ptr<connections> p2p, epee::byte_slice noise)
- : zone_(std::make_shared<detail::zone>(service, std::move(p2p), std::move(noise)))
+ notify::notify(boost::asio::io_service& service, std::shared_ptr<connections> p2p, epee::byte_slice noise, bool is_public)
+ : zone_(std::make_shared<detail::zone>(service, std::move(p2p), std::move(noise), is_public))
{
if (!zone_->p2p)
throw std::logic_error{"cryptonote::levin::notify cannot have nullptr p2p argument"};
@@ -528,7 +533,7 @@ namespace levin
channel.next_noise.cancel();
}
- bool notify::send_txs(std::vector<cryptonote::blobdata> txs, const boost::uuids::uuid& source, const bool pad_txs)
+ bool notify::send_txs(std::vector<blobdata> txs, const boost::uuids::uuid& source, const bool pad_txs)
{
if (!zone_)
return false;
diff --git a/src/cryptonote_protocol/levin_notify.h b/src/cryptonote_protocol/levin_notify.h
index 82d22680a..484243af5 100644
--- a/src/cryptonote_protocol/levin_notify.h
+++ b/src/cryptonote_protocol/levin_notify.h
@@ -86,7 +86,7 @@ namespace levin
{}
//! Construct an instance with available notification `zones`.
- explicit notify(boost::asio::io_service& service, std::shared_ptr<connections> p2p, epee::byte_slice noise);
+ explicit notify(boost::asio::io_service& service, std::shared_ptr<connections> p2p, epee::byte_slice noise, bool is_public);
notify(const notify&) = delete;
notify(notify&&) = default;
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index cb288071e..8fa983fe5 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -44,7 +44,6 @@
#include "rpc/core_rpc_server.h"
#include "rpc/rpc_args.h"
#include "daemon/command_line_args.h"
-#include "blockchain_db/db_types.h"
#include "version.h"
#ifdef STACK_TRACE
@@ -224,16 +223,6 @@ int main(int argc, char const * argv[])
return 1;
}
- std::string db_type = command_line::get_arg(vm, cryptonote::arg_db_type);
-
- // verify that blockchaindb type is valid
- if(!cryptonote::blockchain_valid_db_type(db_type))
- {
- std::cout << "Invalid database type (" << db_type << "), available types are: " <<
- cryptonote::blockchain_db_types(", ") << std::endl;
- return 0;
- }
-
// data_dir
// default: e.g. ~/.bitmonero/ or ~/.bitmonero/testnet
// if data-dir argument given:
diff --git a/src/hardforks/CMakeLists.txt b/src/hardforks/CMakeLists.txt
new file mode 100644
index 000000000..bd2d14ceb
--- /dev/null
+++ b/src/hardforks/CMakeLists.txt
@@ -0,0 +1,47 @@
+# Copyright (c) 2014-2019, 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.
+
+set(hardforks_sources
+ hardforks.cpp)
+
+set(hardforks_headers
+ hardforks.h)
+
+set(hardforks_private_headers)
+
+monero_private_headers(hardforks
+ ${hardforks_private_headers})
+monero_add_library(hardforks
+ ${hardforks_sources}
+ ${hardforks_headers}
+ ${hardforks_private_headers})
+target_link_libraries(hardforks
+ PUBLIC
+ version
+ PRIVATE
+ ${EXTRA_LIBRARIES})
diff --git a/src/hardforks/hardforks.cpp b/src/hardforks/hardforks.cpp
new file mode 100644
index 000000000..41aa3e9cb
--- /dev/null
+++ b/src/hardforks/hardforks.cpp
@@ -0,0 +1,110 @@
+// Copyright (c) 2014-2019, 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 "hardforks.h"
+
+#undef MONERO_DEFAULT_LOG_CATEGORY
+#define MONERO_DEFAULT_LOG_CATEGORY "blockchain.hardforks"
+
+const hardfork_t mainnet_hard_forks[] = {
+ // version 1 from the start of the blockchain
+ { 1, 1, 0, 1341378000 },
+
+ // version 2 starts from block 1009827, which is on or around the 20th of March, 2016. Fork time finalised on 2015-09-20. No fork voting occurs for the v2 fork.
+ { 2, 1009827, 0, 1442763710 },
+
+ // version 3 starts from block 1141317, which is on or around the 24th of September, 2016. Fork time finalised on 2016-03-21.
+ { 3, 1141317, 0, 1458558528 },
+
+ // version 4 starts from block 1220516, which is on or around the 5th of January, 2017. Fork time finalised on 2016-09-18.
+ { 4, 1220516, 0, 1483574400 },
+
+ // version 5 starts from block 1288616, which is on or around the 15th of April, 2017. Fork time finalised on 2017-03-14.
+ { 5, 1288616, 0, 1489520158 },
+
+ // version 6 starts from block 1400000, which is on or around the 16th of September, 2017. Fork time finalised on 2017-08-18.
+ { 6, 1400000, 0, 1503046577 },
+
+ // version 7 starts from block 1546000, which is on or around the 6th of April, 2018. Fork time finalised on 2018-03-17.
+ { 7, 1546000, 0, 1521303150 },
+
+ // version 8 starts from block 1685555, which is on or around the 18th of October, 2018. Fork time finalised on 2018-09-02.
+ { 8, 1685555, 0, 1535889547 },
+
+ // version 9 starts from block 1686275, which is on or around the 19th of October, 2018. Fork time finalised on 2018-09-02.
+ { 9, 1686275, 0, 1535889548 },
+
+ // version 10 starts from block 1788000, which is on or around the 9th of March, 2019. Fork time finalised on 2019-02-10.
+ { 10, 1788000, 0, 1549792439 },
+
+ // version 11 starts from block 1788720, which is on or around the 10th of March, 2019. Fork time finalised on 2019-02-15.
+ { 11, 1788720, 0, 1550225678 },
+};
+const size_t num_mainnet_hard_forks = sizeof(mainnet_hard_forks) / sizeof(mainnet_hard_forks[0]);
+const uint64_t mainnet_hard_fork_version_1_till = 1009826;
+
+const hardfork_t testnet_hard_forks[] = {
+ // version 1 from the start of the blockchain
+ { 1, 1, 0, 1341378000 },
+
+ // version 2 starts from block 624634, which is on or around the 23rd of November, 2015. Fork time finalised on 2015-11-20. No fork voting occurs for the v2 fork.
+ { 2, 624634, 0, 1445355000 },
+
+ // versions 3-5 were passed in rapid succession from September 18th, 2016
+ { 3, 800500, 0, 1472415034 },
+ { 4, 801219, 0, 1472415035 },
+ { 5, 802660, 0, 1472415036 + 86400*180 }, // add 5 months on testnet to shut the update warning up since there's a large gap to v6
+
+ { 6, 971400, 0, 1501709789 },
+ { 7, 1057027, 0, 1512211236 },
+ { 8, 1057058, 0, 1533211200 },
+ { 9, 1057778, 0, 1533297600 },
+ { 10, 1154318, 0, 1550153694 },
+ { 11, 1155038, 0, 1550225678 },
+ { 12, 1308737, 0, 1569582000 },
+};
+const size_t num_testnet_hard_forks = sizeof(testnet_hard_forks) / sizeof(testnet_hard_forks[0]);
+const uint64_t testnet_hard_fork_version_1_till = 624633;
+
+const hardfork_t stagenet_hard_forks[] = {
+ // version 1 from the start of the blockchain
+ { 1, 1, 0, 1341378000 },
+
+ // versions 2-7 in rapid succession from March 13th, 2018
+ { 2, 32000, 0, 1521000000 },
+ { 3, 33000, 0, 1521120000 },
+ { 4, 34000, 0, 1521240000 },
+ { 5, 35000, 0, 1521360000 },
+ { 6, 36000, 0, 1521480000 },
+ { 7, 37000, 0, 1521600000 },
+ { 8, 176456, 0, 1537821770 },
+ { 9, 177176, 0, 1537821771 },
+ { 10, 269000, 0, 1550153694 },
+ { 11, 269720, 0, 1550225678 },
+};
+const size_t num_stagenet_hard_forks = sizeof(stagenet_hard_forks) / sizeof(stagenet_hard_forks[0]);
diff --git a/src/blockchain_db/db_types.h b/src/hardforks/hardforks.h
index 04cadbb10..e7bceca42 100644
--- a/src/blockchain_db/db_types.h
+++ b/src/hardforks/hardforks.h
@@ -25,12 +25,28 @@
// 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.
-//
-// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
#pragma once
-namespace cryptonote
+#include <stdint.h>
+#include <time.h>
+
+struct hardfork_t
{
- bool blockchain_valid_db_type(const std::string& db_type);
- std::string blockchain_db_types(const std::string& sep);
-} // namespace cryptonote
+ uint8_t version;
+ uint64_t height;
+ uint8_t threshold;
+ time_t time;
+ hardfork_t(uint8_t version, uint64_t height, uint8_t threshold, time_t time): version(version), height(height), threshold(threshold), time(time) {}
+};
+
+extern const hardfork_t mainnet_hard_forks[];
+extern const uint64_t mainnet_hard_fork_version_1_till;
+extern const size_t num_mainnet_hard_forks;
+
+extern const hardfork_t testnet_hard_forks[];
+extern const uint64_t testnet_hard_fork_version_1_till;
+extern const size_t num_testnet_hard_forks;
+
+extern const hardfork_t stagenet_hard_forks[];
+extern const size_t num_stagenet_hard_forks;
diff --git a/src/p2p/net_node.cpp b/src/p2p/net_node.cpp
index c7fc058ca..ae23bb7fd 100644
--- a/src/p2p/net_node.cpp
+++ b/src/p2p/net_node.cpp
@@ -152,7 +152,7 @@ namespace nodetool
const command_line::arg_descriptor<bool> arg_no_igd = {"no-igd", "Disable UPnP port mapping"};
const command_line::arg_descriptor<std::string> arg_igd = {"igd", "UPnP port mapping (disabled, enabled, delayed)", "delayed"};
const command_line::arg_descriptor<bool> arg_p2p_use_ipv6 = {"p2p-use-ipv6", "Enable IPv6 for p2p", false};
- const command_line::arg_descriptor<bool> arg_p2p_require_ipv4 = {"p2p-require-ipv4", "Require successful IPv4 bind for p2p", true};
+ const command_line::arg_descriptor<bool> arg_p2p_ignore_ipv4 = {"p2p-ignore-ipv4", "Ignore unsuccessful IPv4 bind for p2p", false};
const command_line::arg_descriptor<int64_t> arg_out_peers = {"out-peers", "set max number of out peers", -1};
const command_line::arg_descriptor<int64_t> arg_in_peers = {"in-peers", "set max number of in peers", -1};
const command_line::arg_descriptor<int> arg_tos_flag = {"tos-flag", "set TOS flag", -1};
diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h
index 255a1fc1f..0c9c285e8 100644
--- a/src/p2p/net_node.h
+++ b/src/p2p/net_node.h
@@ -271,7 +271,7 @@ namespace nodetool
virtual bool block_subnet(const epee::net_utils::ipv4_network_subnet &subnet, time_t seconds = P2P_IP_BLOCKTIME);
virtual bool unblock_subnet(const epee::net_utils::ipv4_network_subnet &subnet);
virtual bool is_host_blocked(const epee::net_utils::network_address &address, time_t *seconds) { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock); return !is_remote_host_allowed(address, seconds); }
- virtual std::map<epee::net_utils::network_address, time_t> get_blocked_hosts() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock); return m_blocked_hosts; }
+ virtual std::map<std::string, time_t> get_blocked_hosts() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock); return m_blocked_hosts; }
virtual std::map<epee::net_utils::ipv4_network_subnet, time_t> get_blocked_subnets() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock); return m_blocked_subnets; }
virtual void add_used_stripe_peer(const typename t_payload_net_handler::connection_context &context);
@@ -342,7 +342,7 @@ namespace nodetool
virtual void request_callback(const epee::net_utils::connection_context_base& context);
virtual void for_each_connection(std::function<bool(typename t_payload_net_handler::connection_context&, peerid_type, uint32_t)> f);
virtual bool for_connection(const boost::uuids::uuid&, std::function<bool(typename t_payload_net_handler::connection_context&, peerid_type, uint32_t)> f);
- virtual bool add_host_fail(const epee::net_utils::network_address &address);
+ virtual bool add_host_fail(const epee::net_utils::network_address &address, unsigned int score = 1);
//----------------- i_connection_filter --------------------------------------------------------
virtual bool is_remote_host_allowed(const epee::net_utils::network_address &address, time_t *t = NULL);
//-----------------------------------------------------------------------------------------------
@@ -484,11 +484,11 @@ namespace nodetool
std::map<epee::net_utils::zone, network_zone> m_network_zones;
- std::map<epee::net_utils::network_address, time_t> m_conn_fails_cache;
+ std::map<std::string, time_t> m_conn_fails_cache;
epee::critical_section m_conn_fails_cache_lock;
epee::critical_section m_blocked_hosts_lock; // for both hosts and subnets
- std::map<epee::net_utils::network_address, time_t> m_blocked_hosts;
+ std::map<std::string, time_t> m_blocked_hosts;
std::map<epee::net_utils::ipv4_network_subnet, time_t> m_blocked_subnets;
epee::critical_section m_host_fails_score_lock;
@@ -510,7 +510,7 @@ namespace nodetool
extern const command_line::arg_descriptor<std::string, false, true, 2> arg_p2p_bind_port;
extern const command_line::arg_descriptor<std::string, false, true, 2> arg_p2p_bind_port_ipv6;
extern const command_line::arg_descriptor<bool> arg_p2p_use_ipv6;
- extern const command_line::arg_descriptor<bool> arg_p2p_require_ipv4;
+ extern const command_line::arg_descriptor<bool> arg_p2p_ignore_ipv4;
extern const command_line::arg_descriptor<uint32_t> arg_p2p_external_port;
extern const command_line::arg_descriptor<bool> arg_p2p_allow_local_ip;
extern const command_line::arg_descriptor<std::vector<std::string> > arg_p2p_add_peer;
diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl
index 97a18b519..5fccdca5a 100644
--- a/src/p2p/net_node.inl
+++ b/src/p2p/net_node.inl
@@ -97,7 +97,7 @@ namespace nodetool
command_line::add_arg(desc, arg_p2p_bind_port, false);
command_line::add_arg(desc, arg_p2p_bind_port_ipv6, false);
command_line::add_arg(desc, arg_p2p_use_ipv6);
- command_line::add_arg(desc, arg_p2p_require_ipv4);
+ command_line::add_arg(desc, arg_p2p_ignore_ipv4);
command_line::add_arg(desc, arg_p2p_external_port);
command_line::add_arg(desc, arg_p2p_allow_local_ip);
command_line::add_arg(desc, arg_p2p_add_peer);
@@ -166,7 +166,7 @@ namespace nodetool
const time_t now = time(nullptr);
// look in the hosts list
- auto it = m_blocked_hosts.find(address);
+ auto it = m_blocked_hosts.find(address.host_str());
if (it != m_blocked_hosts.end())
{
if (now >= it->second)
@@ -224,7 +224,7 @@ namespace nodetool
limit = std::numeric_limits<time_t>::max();
else
limit = now + seconds;
- m_blocked_hosts[addr] = limit;
+ m_blocked_hosts[addr.host_str()] = limit;
// drop any connection to that address. This should only have to look into
// the zone related to the connection, but really make sure everything is
@@ -254,7 +254,7 @@ namespace nodetool
bool node_server<t_payload_net_handler>::unblock_host(const epee::net_utils::network_address &address)
{
CRITICAL_REGION_LOCAL(m_blocked_hosts_lock);
- auto i = m_blocked_hosts.find(address);
+ auto i = m_blocked_hosts.find(address.host_str());
if (i == m_blocked_hosts.end())
return false;
m_blocked_hosts.erase(i);
@@ -315,13 +315,13 @@ namespace nodetool
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
- bool node_server<t_payload_net_handler>::add_host_fail(const epee::net_utils::network_address &address)
+ bool node_server<t_payload_net_handler>::add_host_fail(const epee::net_utils::network_address &address, unsigned int score)
{
if(!address.is_blockable())
return false;
CRITICAL_REGION_LOCAL(m_host_fails_score_lock);
- uint64_t fails = ++m_host_fails_score[address.host_str()];
+ uint64_t fails = m_host_fails_score[address.host_str()] += score;
MDEBUG("Host " << address.host_str() << " fail score=" << fails);
if(fails > P2P_IP_FAILS_BEFORE_BLOCK)
{
@@ -382,9 +382,9 @@ namespace nodetool
}
m_offline = command_line::get_arg(vm, cryptonote::arg_offline);
m_use_ipv6 = command_line::get_arg(vm, arg_p2p_use_ipv6);
- m_require_ipv4 = command_line::get_arg(vm, arg_p2p_require_ipv4);
+ m_require_ipv4 = !command_line::get_arg(vm, arg_p2p_ignore_ipv4);
public_zone.m_notifier = cryptonote::levin::notify{
- public_zone.m_net_server.get_io_service(), public_zone.m_net_server.get_config_shared(), nullptr
+ public_zone.m_net_server.get_io_service(), public_zone.m_net_server.get_config_shared(), nullptr, true
};
if (command_line::has_arg(vm, arg_p2p_add_peer))
@@ -495,7 +495,7 @@ namespace nodetool
}
zone.m_notifier = cryptonote::levin::notify{
- zone.m_net_server.get_io_service(), zone.m_net_server.get_config_shared(), std::move(this_noise)
+ zone.m_net_server.get_io_service(), zone.m_net_server.get_config_shared(), std::move(this_noise), false
};
}
@@ -670,11 +670,18 @@ namespace nodetool
std::vector<std::vector<std::string>> dns_results;
dns_results.resize(m_seed_nodes_list.size());
+ // some libc implementation provide only a very small stack
+ // for threads, e.g. musl only gives +- 80kb, which is not
+ // enough to do a resolve with unbound. we request a stack
+ // of 1 mb, which should be plenty
+ boost::thread::attributes thread_attributes;
+ thread_attributes.set_stack_size(1024*1024);
+
std::list<boost::thread> dns_threads;
uint64_t result_index = 0;
for (const std::string& addr_str : m_seed_nodes_list)
{
- boost::thread th = boost::thread([=, &dns_results, &addr_str]
+ boost::thread th = boost::thread(thread_attributes, [=, &dns_results, &addr_str]
{
MDEBUG("dns_threads[" << result_index << "] created for: " << addr_str);
// TODO: care about dnssec avail/valid
@@ -1342,7 +1349,7 @@ namespace nodetool
bool node_server<t_payload_net_handler>::is_addr_recently_failed(const epee::net_utils::network_address& addr)
{
CRITICAL_REGION_LOCAL(m_conn_fails_cache_lock);
- auto it = m_conn_fails_cache.find(addr);
+ auto it = m_conn_fails_cache.find(addr.host_str());
if(it == m_conn_fails_cache.end())
return false;
@@ -1857,7 +1864,11 @@ namespace nodetool
const epee::net_utils::ipv4_network_address &ipv4 = na.as<const epee::net_utils::ipv4_network_address>();
if (ipv4.ip() == 0)
ignore = true;
+ else if (ipv4.port() == be.rpc_port)
+ ignore = true;
}
+ if (be.pruning_seed && (be.pruning_seed < tools::make_pruning_seed(1, CRYPTONOTE_PRUNING_LOG_STRIPES) || be.pruning_seed > tools::make_pruning_seed(1ul << CRYPTONOTE_PRUNING_LOG_STRIPES, CRYPTONOTE_PRUNING_LOG_STRIPES)))
+ ignore = true;
if (ignore)
{
MDEBUG("Ignoring " << be.adr.str());
diff --git a/src/p2p/net_node_common.h b/src/p2p/net_node_common.h
index 239814c2c..752873666 100644
--- a/src/p2p/net_node_common.h
+++ b/src/p2p/net_node_common.h
@@ -58,9 +58,9 @@ namespace nodetool
virtual bool for_connection(const boost::uuids::uuid&, std::function<bool(t_connection_context&, peerid_type, uint32_t)> f)=0;
virtual bool block_host(const epee::net_utils::network_address &address, time_t seconds = 0)=0;
virtual bool unblock_host(const epee::net_utils::network_address &address)=0;
- virtual std::map<epee::net_utils::network_address, time_t> get_blocked_hosts()=0;
+ virtual std::map<std::string, time_t> get_blocked_hosts()=0;
virtual std::map<epee::net_utils::ipv4_network_subnet, time_t> get_blocked_subnets()=0;
- virtual bool add_host_fail(const epee::net_utils::network_address &address)=0;
+ virtual bool add_host_fail(const epee::net_utils::network_address &address, unsigned int score = 1)=0;
virtual void add_used_stripe_peer(const t_connection_context &context)=0;
virtual void remove_used_stripe_peer(const t_connection_context &context)=0;
virtual void clear_used_stripe_peers()=0;
@@ -114,15 +114,15 @@ namespace nodetool
{
return true;
}
- virtual std::map<epee::net_utils::network_address, time_t> get_blocked_hosts()
+ virtual std::map<std::string, time_t> get_blocked_hosts()
{
- return std::map<epee::net_utils::network_address, time_t>();
+ return std::map<std::string, time_t>();
}
virtual std::map<epee::net_utils::ipv4_network_subnet, time_t> get_blocked_subnets()
{
return std::map<epee::net_utils::ipv4_network_subnet, time_t>();
}
- virtual bool add_host_fail(const epee::net_utils::network_address &address)
+ virtual bool add_host_fail(const epee::net_utils::network_address &address, unsigned int score)
{
return true;
}
diff --git a/src/rpc/bootstrap_daemon.cpp b/src/rpc/bootstrap_daemon.cpp
index 6f8426ee7..c97b2c95a 100644
--- a/src/rpc/bootstrap_daemon.cpp
+++ b/src/rpc/bootstrap_daemon.cpp
@@ -12,7 +12,7 @@
namespace cryptonote
{
- bootstrap_daemon::bootstrap_daemon(std::function<boost::optional<std::string>()> get_next_public_node) noexcept
+ bootstrap_daemon::bootstrap_daemon(std::function<boost::optional<std::string>()> get_next_public_node)
: m_get_next_public_node(get_next_public_node)
{
}
diff --git a/src/rpc/bootstrap_daemon.h b/src/rpc/bootstrap_daemon.h
index 130a6458d..6276b1b21 100644
--- a/src/rpc/bootstrap_daemon.h
+++ b/src/rpc/bootstrap_daemon.h
@@ -15,7 +15,7 @@ namespace cryptonote
class bootstrap_daemon
{
public:
- bootstrap_daemon(std::function<boost::optional<std::string>()> get_next_public_node) noexcept;
+ bootstrap_daemon(std::function<boost::optional<std::string>()> get_next_public_node);
bootstrap_daemon(const std::string &address, const boost::optional<epee::net_utils::http::login> &credentials);
std::string address() const noexcept;
diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp
index 529cdbf2d..6f5f912cd 100644
--- a/src/rpc/core_rpc_server.cpp
+++ b/src/rpc/core_rpc_server.cpp
@@ -217,13 +217,13 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
- bool core_rpc_server::add_host_fail(const connection_context *ctx)
+ bool core_rpc_server::add_host_fail(const connection_context *ctx, unsigned int score)
{
if(!ctx || !ctx->m_remote_address.is_blockable())
return false;
CRITICAL_REGION_LOCAL(m_host_fails_score_lock);
- uint64_t fails = ++m_host_fails_score[ctx->m_remote_address.host_str()];
+ uint64_t fails = m_host_fails_score[ctx->m_remote_address.host_str()] += score;
MDEBUG("Host " << ctx->m_remote_address.host_str() << " fail score=" << fails);
if(fails > RPC_IP_FAILS_BEFORE_BLOCK)
{
@@ -377,6 +377,7 @@ namespace cryptonote
for(auto& bd: bs)
{
res.blocks.resize(res.blocks.size()+1);
+ res.blocks.back().pruned = req.prune;
res.blocks.back().block = bd.first.first;
pruned_size += bd.first.first.size();
unpruned_size += bd.first.first.size();
@@ -389,10 +390,10 @@ namespace cryptonote
for (std::vector<std::pair<crypto::hash, cryptonote::blobdata>>::iterator i = bd.second.begin(); i != bd.second.end(); ++i)
{
unpruned_size += i->second.size();
- res.blocks.back().txs.push_back(std::move(i->second));
+ res.blocks.back().txs.push_back({std::move(i->second), crypto::null_hash});
i->second.clear();
i->second.shrink_to_fit();
- pruned_size += res.blocks.back().txs.back().size();
+ pruned_size += res.blocks.back().txs.back().blob.size();
}
const size_t n_txes_to_lookup = bd.second.size() + (req.no_miner_tx ? 0 : 1);
@@ -474,7 +475,7 @@ namespace cryptonote
res.blocks.resize(res.blocks.size() + 1);
res.blocks.back().block = block_to_blob(blk);
for (auto& tx : txs)
- res.blocks.back().txs.push_back(tx_to_blob(tx));
+ res.blocks.back().txs.push_back({tx_to_blob(tx), crypto::null_hash});
}
res.status = CORE_RPC_STATUS_OK;
return true;
@@ -488,7 +489,7 @@ namespace cryptonote
return r;
res.start_height = req.start_height;
- if(!m_core.get_blockchain_storage().find_blockchain_supplement(req.block_ids, res.m_block_ids, res.start_height, res.current_height, false))
+ if(!m_core.get_blockchain_storage().find_blockchain_supplement(req.block_ids, res.m_block_ids, NULL, res.start_height, res.current_height, false))
{
res.status = "Failed";
add_host_fail(ctx);
@@ -912,7 +913,7 @@ namespace cryptonote
cryptonote_connection_context fake_context = AUTO_VAL_INIT(fake_context);
tx_verification_context tvc = AUTO_VAL_INIT(tvc);
- if(!m_core.handle_incoming_tx(tx_blob, tvc, false, false, req.do_not_relay) || tvc.m_verifivation_failed)
+ if(!m_core.handle_incoming_tx({tx_blob, crypto::null_hash}, tvc, false, false, req.do_not_relay) || tvc.m_verifivation_failed)
{
res.status = "Failed";
std::string reason = "";
@@ -1060,6 +1061,7 @@ namespace cryptonote
case 1: res.pow_algorithm = "CNv1 (Cryptonight variant 1)"; break;
case 2: case 3: res.pow_algorithm = "CNv2 (Cryptonight variant 2)"; break;
case 4: case 5: res.pow_algorithm = "CNv4 (Cryptonight variant 4)"; break;
+ case 6: res.pow_algorithm = "RandomX"; break;
default: res.pow_algorithm = "I'm not sure actually"; break;
}
if (res.is_background_mining_enabled)
@@ -1440,6 +1442,18 @@ namespace cryptonote
LOG_ERROR("Failed to create block template");
return false;
}
+ if (b.major_version >= RX_BLOCK_VERSION)
+ {
+ uint64_t seed_height, next_height;
+ crypto::hash seed_hash;
+ crypto::rx_seedheights(res.height, &seed_height, &next_height);
+ seed_hash = m_core.get_block_id_by_height(seed_height);
+ res.seed_hash = string_tools::pod_to_hex(seed_hash);
+ if (next_height != seed_height) {
+ seed_hash = m_core.get_block_id_by_height(next_height);
+ res.next_seed_hash = string_tools::pod_to_hex(seed_hash);
+ }
+ }
store_difficulty(wdiff, res.difficulty, res.wide_difficulty, res.difficulty_top64);
blobdata block_blob = t_serializable_object_to_blob(b);
crypto::public_key tx_pub_key = cryptonote::get_tx_pub_key_from_extra(b.miner_tx);
@@ -1556,7 +1570,7 @@ namespace cryptonote
template_req.reserve_size = 1;
template_req.wallet_address = req.wallet_address;
template_req.prev_block = req.prev_block;
- submit_req.push_back(boost::value_initialized<std::string>());
+ submit_req.push_back(std::string{});
res.height = m_core.get_blockchain_storage().get_current_blockchain_height();
for(size_t i = 0; i < req.amount_of_blocks; i++)
@@ -1582,7 +1596,7 @@ namespace cryptonote
return false;
}
b.nonce = req.starting_nonce;
- miner::find_nonce_for_given_block(b, template_res.difficulty, template_res.height);
+ miner::find_nonce_for_given_block(&(m_core.get_blockchain_storage()), b, template_res.difficulty, template_res.height);
submit_req.front() = string_tools::buff_to_hex_nodelimer(block_to_blob(b));
r = on_submitblock(submit_req, submit_res, error_resp, ctx);
@@ -1627,7 +1641,7 @@ namespace cryptonote
response.reward = get_block_reward(blk);
response.block_size = response.block_weight = m_core.get_blockchain_storage().get_db().get_block_weight(height);
response.num_txes = blk.tx_hashes.size();
- response.pow_hash = fill_pow_hash ? string_tools::pod_to_hex(get_block_longhash(blk, height)) : "";
+ response.pow_hash = fill_pow_hash ? string_tools::pod_to_hex(get_block_longhash(&(m_core.get_blockchain_storage()), blk, height, 0)) : "";
response.long_term_weight = m_core.get_blockchain_storage().get_db().get_block_long_term_weight(height);
response.miner_tx_hash = string_tools::pod_to_hex(cryptonote::get_transaction_hash(blk.miner_tx));
return true;
@@ -1985,12 +1999,12 @@ namespace cryptonote
PERF_TIMER(on_get_bans);
auto now = time(nullptr);
- std::map<epee::net_utils::network_address, time_t> blocked_hosts = m_p2p.get_blocked_hosts();
- for (std::map<epee::net_utils::network_address, time_t>::const_iterator i = blocked_hosts.begin(); i != blocked_hosts.end(); ++i)
+ std::map<std::string, time_t> blocked_hosts = m_p2p.get_blocked_hosts();
+ for (std::map<std::string, time_t>::const_iterator i = blocked_hosts.begin(); i != blocked_hosts.end(); ++i)
{
if (i->second > now) {
COMMAND_RPC_GETBANS::ban b;
- b.host = i->first.host_str();
+ b.host = i->first;
b.ip = 0;
uint32_t ip;
if (epee::string_tools::get_ip_int32_from_string(ip, b.host))
diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h
index 379f6ed28..fe03012b7 100644
--- a/src/rpc/core_rpc_server.h
+++ b/src/rpc/core_rpc_server.h
@@ -241,7 +241,7 @@ namespace cryptonote
private:
bool check_core_busy();
bool check_core_ready();
- bool add_host_fail(const connection_context *ctx);
+ bool add_host_fail(const connection_context *ctx, unsigned int score = 1);
//utils
uint64_t get_block_reward(const block& blk);
diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h
index 2f7f22293..2760260f6 100644
--- a/src/rpc/core_rpc_server_commands_defs.h
+++ b/src/rpc/core_rpc_server_commands_defs.h
@@ -86,8 +86,8 @@ namespace cryptonote
// whether they can talk to a given daemon without having to know in
// advance which version they will stop working with
// Don't go over 32767 for any of these
-#define CORE_RPC_VERSION_MAJOR 2
-#define CORE_RPC_VERSION_MINOR 10
+#define CORE_RPC_VERSION_MAJOR 3
+#define CORE_RPC_VERSION_MINOR 0
#define MAKE_CORE_RPC_VERSION(major,minor) (((major)<<16)|(minor))
#define CORE_RPC_VERSION MAKE_CORE_RPC_VERSION(CORE_RPC_VERSION_MAJOR, CORE_RPC_VERSION_MINOR)
@@ -894,6 +894,8 @@ namespace cryptonote
uint64_t reserved_offset;
uint64_t expected_reward;
std::string prev_hash;
+ std::string seed_hash;
+ std::string next_seed_hash;
blobdata blocktemplate_blob;
blobdata blockhashing_blob;
std::string status;
@@ -911,6 +913,8 @@ namespace cryptonote
KV_SERIALIZE(blockhashing_blob)
KV_SERIALIZE(status)
KV_SERIALIZE(untrusted)
+ KV_SERIALIZE(seed_hash)
+ KV_SERIALIZE(next_seed_hash)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<response_t> response;
@@ -1516,7 +1520,7 @@ namespace cryptonote
KV_SERIALIZE(num_10m)
KV_SERIALIZE(num_not_relayed)
KV_SERIALIZE(histo_98pc)
- KV_SERIALIZE_CONTAINER_POD_AS_BLOB(histo)
+ KV_SERIALIZE(histo)
KV_SERIALIZE(num_double_spends)
END_KV_SERIALIZE_MAP()
};
diff --git a/src/rpc/daemon_handler.cpp b/src/rpc/daemon_handler.cpp
index 890380dc8..d7e081af3 100644
--- a/src/rpc/daemon_handler.cpp
+++ b/src/rpc/daemon_handler.cpp
@@ -141,7 +141,7 @@ namespace rpc
auto& chain = m_core.get_blockchain_storage();
- if (!chain.find_blockchain_supplement(req.known_hashes, res.hashes, res.start_height, res.current_height, false))
+ if (!chain.find_blockchain_supplement(req.known_hashes, res.hashes, NULL, res.start_height, res.current_height, false))
{
res.status = Message::STATUS_FAILED;
res.error_details = "Blockchain::find_blockchain_supplement() returned false";
@@ -291,7 +291,7 @@ namespace rpc
cryptonote_connection_context fake_context = AUTO_VAL_INIT(fake_context);
tx_verification_context tvc = AUTO_VAL_INIT(tvc);
- if(!m_core.handle_incoming_tx(tx_blob, tvc, false, false, !relay) || tvc.m_verifivation_failed)
+ if(!m_core.handle_incoming_tx({tx_blob, crypto::null_hash}, tvc, false, false, !relay) || tvc.m_verifivation_failed)
{
if (tvc.m_verifivation_failed)
{
diff --git a/src/rpc/rpc_args.cpp b/src/rpc/rpc_args.cpp
index 68b33cb8c..0eaa0ef0e 100644
--- a/src/rpc/rpc_args.cpp
+++ b/src/rpc/rpc_args.cpp
@@ -92,7 +92,7 @@ namespace cryptonote
: rpc_bind_ip({"rpc-bind-ip", rpc_args::tr("Specify IP to bind RPC server"), "127.0.0.1"})
, rpc_bind_ipv6_address({"rpc-bind-ipv6-address", rpc_args::tr("Specify IPv6 address to bind RPC server"), "::1"})
, rpc_use_ipv6({"rpc-use-ipv6", rpc_args::tr("Allow IPv6 for RPC"), false})
- , rpc_require_ipv4({"rpc-require-ipv4", rpc_args::tr("Require successful IPv4 bind for RPC"), true})
+ , rpc_ignore_ipv4({"rpc-ignore-ipv4", rpc_args::tr("Ignore unsuccessful IPv4 bind for RPC"), false})
, rpc_login({"rpc-login", rpc_args::tr("Specify username[:password] required for RPC server"), "", true})
, confirm_external_bind({"confirm-external-bind", rpc_args::tr("Confirm rpc-bind-ip value is NOT a loopback (local) IP")})
, rpc_access_control_origins({"rpc-access-control-origins", rpc_args::tr("Specify a comma separated list of origins to allow cross origin resource sharing"), ""})
@@ -113,7 +113,7 @@ namespace cryptonote
command_line::add_arg(desc, arg.rpc_bind_ip);
command_line::add_arg(desc, arg.rpc_bind_ipv6_address);
command_line::add_arg(desc, arg.rpc_use_ipv6);
- command_line::add_arg(desc, arg.rpc_require_ipv4);
+ command_line::add_arg(desc, arg.rpc_ignore_ipv4);
command_line::add_arg(desc, arg.rpc_login);
command_line::add_arg(desc, arg.confirm_external_bind);
command_line::add_arg(desc, arg.rpc_access_control_origins);
@@ -135,7 +135,7 @@ namespace cryptonote
config.bind_ip = command_line::get_arg(vm, arg.rpc_bind_ip);
config.bind_ipv6_address = command_line::get_arg(vm, arg.rpc_bind_ipv6_address);
config.use_ipv6 = command_line::get_arg(vm, arg.rpc_use_ipv6);
- config.require_ipv4 = command_line::get_arg(vm, arg.rpc_require_ipv4);
+ config.require_ipv4 = !command_line::get_arg(vm, arg.rpc_ignore_ipv4);
if (!config.bind_ip.empty())
{
// always parse IP here for error consistency
diff --git a/src/rpc/rpc_args.h b/src/rpc/rpc_args.h
index cd154a4d0..bdb9c70d5 100644
--- a/src/rpc/rpc_args.h
+++ b/src/rpc/rpc_args.h
@@ -54,7 +54,7 @@ namespace cryptonote
const command_line::arg_descriptor<std::string> rpc_bind_ip;
const command_line::arg_descriptor<std::string> rpc_bind_ipv6_address;
const command_line::arg_descriptor<bool> rpc_use_ipv6;
- const command_line::arg_descriptor<bool> rpc_require_ipv4;
+ const command_line::arg_descriptor<bool> rpc_ignore_ipv4;
const command_line::arg_descriptor<std::string> rpc_login;
const command_line::arg_descriptor<bool> confirm_external_bind;
const command_line::arg_descriptor<std::string> rpc_access_control_origins;
diff --git a/src/serialization/json_object.cpp b/src/serialization/json_object.cpp
index cc52bde58..42d09aa5f 100644
--- a/src/serialization/json_object.cpp
+++ b/src/serialization/json_object.cpp
@@ -627,6 +627,25 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::connection_info& inf
GET_FROM_JSON_OBJECT(val, info.current_upload, current_upload);
}
+void toJsonValue(rapidjson::Document& doc, const cryptonote::tx_blob_entry& tx, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, blob, tx.blob);
+ INSERT_INTO_JSON_OBJECT(val, doc, prunable_hash, tx.prunable_hash);
+}
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_blob_entry& tx)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, tx.blob, blob);
+ GET_FROM_JSON_OBJECT(val, tx.prunable_hash, prunable_hash);
+}
+
void toJsonValue(rapidjson::Document& doc, const cryptonote::block_complete_entry& blk, rapidjson::Value& val)
{
val.SetObject();
diff --git a/src/serialization/json_object.h b/src/serialization/json_object.h
index c804d148b..5ef75b863 100644
--- a/src/serialization/json_object.h
+++ b/src/serialization/json_object.h
@@ -221,6 +221,9 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout);
void toJsonValue(rapidjson::Document& doc, const cryptonote::connection_info& info, rapidjson::Value& val);
void fromJsonValue(const rapidjson::Value& val, cryptonote::connection_info& info);
+void toJsonValue(rapidjson::Document& doc, const cryptonote::tx_blob_entry& tx, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_blob_entry& tx);
+
void toJsonValue(rapidjson::Document& doc, const cryptonote::block_complete_entry& blk, rapidjson::Value& val);
void fromJsonValue(const rapidjson::Value& val, cryptonote::block_complete_entry& blk);
diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp
index fe384e529..0174104be 100644
--- a/src/simplewallet/simplewallet.cpp
+++ b/src/simplewallet/simplewallet.cpp
@@ -119,12 +119,10 @@ typedef cryptonote::simple_wallet sw;
#define LONG_PAYMENT_ID_SUPPORT_CHECK() \
do { \
- if (!m_long_payment_id_support) { \
- fail_msg_writer() << tr("Warning: Long payment IDs are obsolete."); \
- fail_msg_writer() << tr("Long payment IDs are not encrypted on the blockchain, and will harm your privacy."); \
- fail_msg_writer() << tr("Use --long-payment-id-support-bad-for-privacy if you really must use one, and warn the recipient they are using an obsolete feature that will disappear in the future."); \
- return true; \
- } \
+ fail_msg_writer() << tr("Error: Long payment IDs are obsolete."); \
+ fail_msg_writer() << tr("Long payment IDs were not encrypted on the blockchain and would harm your privacy."); \
+ fail_msg_writer() << tr("If the party you're sending to still requires a long payment ID, please notify them."); \
+ return true; \
} while(0)
enum TransferType {
@@ -155,7 +153,6 @@ namespace
const command_line::arg_descriptor<bool> arg_create_address_file = {"create-address-file", sw::tr("Create an address file for new wallets"), false};
const command_line::arg_descriptor<std::string> arg_subaddress_lookahead = {"subaddress-lookahead", tools::wallet2::tr("Set subaddress lookahead sizes to <major>:<minor>"), ""};
const command_line::arg_descriptor<bool> arg_use_english_language_names = {"use-english-language-names", sw::tr("Display English language names"), false};
- const command_line::arg_descriptor<bool> arg_long_payment_id_support = {"long-payment-id-support-bad-for-privacy", sw::tr("Support obsolete long (unencrypted) payment ids (using them harms your privacy)"), false};
const command_line::arg_descriptor< std::vector<std::string> > arg_command = {"command", ""};
@@ -2157,8 +2154,8 @@ bool simple_wallet::welcome(const std::vector<std::string> &args)
message_writer() << tr("Welcome to Monero, the private cryptocurrency.");
message_writer() << "";
message_writer() << tr("Monero, like Bitcoin, is a cryptocurrency. That is, it is digital money.");
- message_writer() << tr("Unlike Bitcoin, your Monero transactions and balance stay private, and not visible to the world by default.");
- message_writer() << tr("However, you have the option of making those available to select parties, if you choose to.");
+ message_writer() << tr("Unlike Bitcoin, your Monero transactions and balance stay private and are not visible to the world by default.");
+ message_writer() << tr("However, you have the option of making those available to select parties if you choose to.");
message_writer() << "";
message_writer() << tr("Monero protects your privacy on the blockchain, and while Monero strives to improve all the time,");
message_writer() << tr("no privacy technology can be 100% perfect, Monero included.");
@@ -2166,7 +2163,7 @@ bool simple_wallet::welcome(const std::vector<std::string> &args)
message_writer() << tr("Flaws in Monero may be discovered in the future, and attacks may be developed to peek under some");
message_writer() << tr("of the layers of privacy Monero provides. Be safe and practice defense in depth.");
message_writer() << "";
- message_writer() << tr("Welcome to Monero and financial privacy. For more information, see https://getmonero.org/");
+ message_writer() << tr("Welcome to Monero and financial privacy. For more information see https://GetMonero.org");
return true;
}
@@ -2406,21 +2403,6 @@ bool simple_wallet::set_refresh_type(const std::vector<std::string> &args/* = st
return true;
}
-bool simple_wallet::set_confirm_missing_payment_id(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
-{
- LONG_PAYMENT_ID_SUPPORT_CHECK();
-
- const auto pwd_container = get_and_verify_password();
- if (pwd_container)
- {
- parse_bool_and_use(args[1], [&](bool r) {
- m_wallet->confirm_missing_payment_id(r);
- m_wallet->rewrite(m_wallet_file, pwd_container->password());
- });
- }
- return true;
-}
-
bool simple_wallet::set_ask_password(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{
const auto pwd_container = get_and_verify_password();
@@ -2677,6 +2659,43 @@ bool simple_wallet::set_ignore_fractional_outputs(const std::vector<std::string>
return true;
}
+
+bool simple_wallet::set_ignore_outputs_above(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
+{
+ const auto pwd_container = get_and_verify_password();
+ if (pwd_container)
+ {
+ uint64_t amount;
+ if (!cryptonote::parse_amount(amount, args[1]))
+ {
+ fail_msg_writer() << tr("Invalid amount");
+ return true;
+ }
+ if (amount == 0)
+ amount = MONEY_SUPPLY;
+ m_wallet->ignore_outputs_above(amount);
+ m_wallet->rewrite(m_wallet_file, pwd_container->password());
+ }
+ return true;
+}
+
+bool simple_wallet::set_ignore_outputs_below(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
+{
+ const auto pwd_container = get_and_verify_password();
+ if (pwd_container)
+ {
+ uint64_t amount;
+ if (!cryptonote::parse_amount(amount, args[1]))
+ {
+ fail_msg_writer() << tr("Invalid amount");
+ return true;
+ }
+ m_wallet->ignore_outputs_below(amount);
+ m_wallet->rewrite(m_wallet_file, pwd_container->password());
+ }
+ return true;
+}
+
bool simple_wallet::set_track_uses(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{
const auto pwd_container = get_and_verify_password();
@@ -2987,11 +3006,14 @@ simple_wallet::simple_wallet()
" Set this if you are not sure whether you will spend on a key reusing Monero fork later.\n "
"subaddress-lookahead <major>:<minor>\n "
" Set the lookahead sizes for the subaddress hash table.\n "
- " Set this if you are not sure whether you will spend on a key reusing Monero fork later.\n "
"segregation-height <n>\n "
" Set to the height of a key reusing fork you want to use, 0 to use default.\n "
"ignore-fractional-outputs <1|0>\n "
" Whether to ignore fractional outputs that result in net loss when spending due to fee.\n "
+ "ignore-outputs-above <amount>\n "
+ " Ignore outputs of amount above this threshold when spending. Value 0 is translated to the maximum value (18 million) which disables this filter.\n "
+ "ignore-outputs-below <amount>\n "
+ " Ignore outputs of amount below this threshold when spending.\n "
"track-uses <1|0>\n "
" Whether to keep track of owned outputs uses.\n "
"setup-background-mining <1|0>\n "
@@ -3352,7 +3374,6 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
success_msg_writer() << "auto-refresh = " << m_wallet->auto_refresh();
success_msg_writer() << "refresh-type = " << get_refresh_type_name(m_wallet->get_refresh_type());
success_msg_writer() << "priority = " << priority<< " (" << priority_string << ")";
- success_msg_writer() << "confirm-missing-payment-id = " << m_wallet->confirm_missing_payment_id();
success_msg_writer() << "ask-password = " << m_wallet->ask_password() << " (" << ask_password_string << ")";
success_msg_writer() << "unit = " << cryptonote::get_unit(cryptonote::get_default_decimal_point());
success_msg_writer() << "min-outputs-count = " << m_wallet->get_min_output_count();
@@ -3369,6 +3390,8 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
success_msg_writer() << "subaddress-lookahead = " << lookahead.first << ":" << lookahead.second;
success_msg_writer() << "segregation-height = " << m_wallet->segregation_height();
success_msg_writer() << "ignore-fractional-outputs = " << m_wallet->ignore_fractional_outputs();
+ success_msg_writer() << "ignore-outputs-above = " << cryptonote::print_money(m_wallet->ignore_outputs_above());
+ success_msg_writer() << "ignore-outputs-below = " << cryptonote::print_money(m_wallet->ignore_outputs_below());
success_msg_writer() << "track-uses = " << m_wallet->track_uses();
success_msg_writer() << "setup-background-mining = " << setup_background_mining_string;
success_msg_writer() << "device-name = " << m_wallet->device_name();
@@ -3417,7 +3440,6 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
CHECK_SIMPLE_VARIABLE("auto-refresh", set_auto_refresh, tr("0 or 1"));
CHECK_SIMPLE_VARIABLE("refresh-type", set_refresh_type, tr("full (slowest, no assumptions); optimize-coinbase (fast, assumes the whole coinbase is paid to a single address); no-coinbase (fastest, assumes we receive no coinbase transaction), default (same as optimize-coinbase)"));
CHECK_SIMPLE_VARIABLE("priority", set_default_priority, tr("0, 1, 2, 3, or 4, or one of ") << join_priority_strings(", "));
- CHECK_SIMPLE_VARIABLE("confirm-missing-payment-id", set_confirm_missing_payment_id, tr("0 or 1"));
CHECK_SIMPLE_VARIABLE("ask-password", set_ask_password, tr("0|1|2 (or never|action|decrypt)"));
CHECK_SIMPLE_VARIABLE("unit", set_unit, tr("monero, millinero, micronero, nanonero, piconero"));
CHECK_SIMPLE_VARIABLE("min-outputs-count", set_min_output_count, tr("unsigned integer"));
@@ -3433,6 +3455,8 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
CHECK_SIMPLE_VARIABLE("subaddress-lookahead", set_subaddress_lookahead, tr("<major>:<minor>"));
CHECK_SIMPLE_VARIABLE("segregation-height", set_segregation_height, tr("unsigned integer"));
CHECK_SIMPLE_VARIABLE("ignore-fractional-outputs", set_ignore_fractional_outputs, tr("0 or 1"));
+ CHECK_SIMPLE_VARIABLE("ignore-outputs-above", set_ignore_outputs_above, tr("amount"));
+ CHECK_SIMPLE_VARIABLE("ignore-outputs-below", set_ignore_outputs_below, tr("amount"));
CHECK_SIMPLE_VARIABLE("track-uses", set_track_uses, tr("0 or 1"));
CHECK_SIMPLE_VARIABLE("inactivity-lock-timeout", set_inactivity_lock_timeout, tr("unsigned integer (seconds, 0 to disable)"));
CHECK_SIMPLE_VARIABLE("setup-background-mining", set_setup_background_mining, tr("1/yes or 0/no"));
@@ -4232,14 +4256,6 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
if (welcome)
message_writer(console_color_yellow, true) << tr("If you are new to Monero, type \"welcome\" for a brief overview.");
- if (m_long_payment_id_support)
- {
- message_writer(console_color_red, false) <<
- tr("WARNING: obsolete long payment IDs are enabled. Sending transactions with those payment IDs are bad for your privacy.");
- message_writer(console_color_red, false) <<
- tr("It is recommended that you do not use them, and ask recipients who ask for one to not endanger your privacy.");
- }
-
m_last_activity_time = time(NULL);
return true;
}
@@ -4273,7 +4289,6 @@ bool simple_wallet::handle_command_line(const boost::program_options::variables_
m_do_not_relay = command_line::get_arg(vm, arg_do_not_relay);
m_subaddress_lookahead = command_line::get_arg(vm, arg_subaddress_lookahead);
m_use_english_language_names = command_line::get_arg(vm, arg_use_english_language_names);
- m_long_payment_id_support = command_line::get_arg(vm, arg_long_payment_id_support);
m_restoring = !m_generate_from_view_key.empty() ||
!m_generate_from_spend_key.empty() ||
!m_generate_from_keys.empty() ||
@@ -5104,9 +5119,16 @@ void simple_wallet::on_money_received(uint64_t height, const crypto::hash &txid,
if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce))
{
crypto::hash payment_id = crypto::null_hash;
- if (get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id))
+ crypto::hash8 payment_id8 = crypto::null_hash8;
+ if (get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8))
+ {
+ if (payment_id8 != crypto::null_hash8)
+ message_writer() <<
+ tr("NOTE: this transaction uses an encrypted payment ID: consider using subaddresses instead");
+ }
+ else if (get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id))
message_writer(console_color_red, false) <<
- (m_long_payment_id_support ? tr("WARNING: this transaction uses an unencrypted payment ID: consider using subaddresses instead.") : tr("WARNING: this transaction uses an unencrypted payment ID: these are obsolete. Support will be withdrawn in the future. Use subaddresses instead."));
+ tr("WARNING: this transaction uses an unencrypted payment ID: these are obsolete and ignored. Use subaddresses instead.");
}
}
if (unlock_time)
@@ -6062,20 +6084,6 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
dsts.push_back(de);
}
- // prompt is there is no payment id and confirmation is required
- if (m_long_payment_id_support && !payment_id_seen && m_wallet->confirm_missing_payment_id() && dsts.size() > num_subaddresses)
- {
- std::string accepted = input_line(tr("No payment id is included with this transaction. Is this okay?"), true);
- if (std::cin.eof())
- return false;
- if (!command_line::is_yes(accepted))
- {
- fail_msg_writer() << tr("transaction cancelled.");
-
- return false;
- }
- }
-
SCOPED_WALLET_UNLOCK_ON_BAD_PASSWORD(return false;);
try
@@ -6637,20 +6645,6 @@ bool simple_wallet::sweep_main(uint64_t below, bool locked, const std::vector<st
payment_id_seen = true;
}
- // prompt is there is no payment id and confirmation is required
- if (m_long_payment_id_support && !payment_id_seen && m_wallet->confirm_missing_payment_id() && !info.is_subaddress)
- {
- std::string accepted = input_line(tr("No payment id is included with this transaction. Is this okay?"), true);
- if (std::cin.eof())
- return true;
- if (!command_line::is_yes(accepted))
- {
- fail_msg_writer() << tr("transaction cancelled.");
-
- return true;
- }
- }
-
SCOPED_WALLET_UNLOCK();
try
@@ -6909,22 +6903,6 @@ bool simple_wallet::sweep_single(const std::vector<std::string> &args_)
payment_id_seen = true;
}
- // prompt if there is no payment id and confirmation is required
- if (m_long_payment_id_support && !payment_id_seen && m_wallet->confirm_missing_payment_id() && !info.is_subaddress)
- {
- std::string accepted = input_line(tr("No payment id is included with this transaction. Is this okay?"), true);
- if (std::cin.eof())
- return true;
- if (!command_line::is_yes(accepted))
- {
- fail_msg_writer() << tr("transaction cancelled.");
-
- // would like to return false, because no tx made, but everything else returns true
- // and I don't know what returning false might adversely affect. *sigh*
- return true;
- }
- }
-
SCOPED_WALLET_UNLOCK();
try
@@ -9745,7 +9723,6 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_params, arg_create_address_file);
command_line::add_arg(desc_params, arg_subaddress_lookahead);
command_line::add_arg(desc_params, arg_use_english_language_names);
- command_line::add_arg(desc_params, arg_long_payment_id_support);
po::positional_options_description positional_options;
positional_options.add(arg_command.name, -1);
diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h
index 22659e99e..47e08ca87 100644
--- a/src/simplewallet/simplewallet.h
+++ b/src/simplewallet/simplewallet.h
@@ -144,6 +144,8 @@ namespace cryptonote
bool set_subaddress_lookahead(const std::vector<std::string> &args = std::vector<std::string>());
bool set_segregation_height(const std::vector<std::string> &args = std::vector<std::string>());
bool set_ignore_fractional_outputs(const std::vector<std::string> &args = std::vector<std::string>());
+ bool set_ignore_outputs_above(const std::vector<std::string> &args = std::vector<std::string>());
+ bool set_ignore_outputs_below(const std::vector<std::string> &args = std::vector<std::string>());
bool set_track_uses(const std::vector<std::string> &args = std::vector<std::string>());
bool set_inactivity_lock_timeout(const std::vector<std::string> &args = std::vector<std::string>());
bool set_setup_background_mining(const std::vector<std::string> &args = std::vector<std::string>());
@@ -430,8 +432,6 @@ namespace cryptonote
std::atomic<bool> m_in_manual_refresh;
uint32_t m_current_subaddress_account;
- bool m_long_payment_id_support;
-
std::atomic<time_t> m_last_activity_time;
std::atomic<bool> m_locked;
std::atomic<bool> m_in_command;
diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt
index d0fc21f51..553445a39 100644
--- a/src/wallet/CMakeLists.txt
+++ b/src/wallet/CMakeLists.txt
@@ -117,12 +117,14 @@ if (BUILD_GUI_DEPS)
wallet_api
wallet
multisig
+ blockchain_db
cryptonote_core
cryptonote_basic
mnemonics
common
cncrypto
device
+ hardforks
ringct
ringct_basic
checkpoints
diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp
index 7120485d5..6200c7a1f 100644
--- a/src/wallet/api/wallet.cpp
+++ b/src/wallet/api/wallet.cpp
@@ -1123,6 +1123,10 @@ UnsignedTransaction *WalletImpl::loadUnsignedTx(const std::string &unsigned_file
UnsignedTransactionImpl * transaction = new UnsignedTransactionImpl(*this);
if (!m_wallet->load_unsigned_tx(unsigned_filename, transaction->m_unsigned_tx_set)){
setStatusError(tr("Failed to load unsigned transactions"));
+ transaction->m_status = UnsignedTransaction::Status::Status_Error;
+ transaction->m_errorString = errorString();
+
+ return transaction;
}
// Check tx data and construct confirmation message
diff --git a/src/wallet/message_store.cpp b/src/wallet/message_store.cpp
index 96d4ef3ce..6e2cb933f 100644
--- a/src/wallet/message_store.cpp
+++ b/src/wallet/message_store.cpp
@@ -699,7 +699,7 @@ void message_store::write_to_file(const multisig_wallet_state &state, const std:
crypto::chacha_key key;
crypto::generate_chacha_key(&state.view_secret_key, sizeof(crypto::secret_key), key, 1);
- file_data write_file_data = boost::value_initialized<file_data>();
+ file_data write_file_data = {};
write_file_data.magic_string = "MMS";
write_file_data.file_version = 0;
write_file_data.iv = crypto::rand<crypto::chacha_iv>();
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp
index 86a606fb1..c7374b896 100644
--- a/src/wallet/wallet2.cpp
+++ b/src/wallet/wallet2.cpp
@@ -32,7 +32,6 @@
#include <tuple>
#include <boost/format.hpp>
#include <boost/optional/optional.hpp>
-#include <boost/utility/value_init.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/split.hpp>
@@ -138,6 +137,8 @@ using namespace cryptonote;
#define DEFAULT_INACTIVITY_LOCK_TIMEOUT 90 // a minute and a half
+#define IGNORE_LONG_PAYMENT_ID_FROM_BLOCK_VERSION 12
+
static const std::string MULTISIG_SIGNATURE_MAGIC = "SigMultisigPkV1";
static const std::string MULTISIG_EXTRA_INFO_MAGIC = "MultisigxV1";
@@ -1114,7 +1115,6 @@ wallet2::wallet2(network_type nettype, uint64_t kdf_rounds, bool unattended):
m_first_refresh_done(false),
m_refresh_from_block_height(0),
m_explicit_refresh_from_block_height(true),
- m_confirm_missing_payment_id(true),
m_confirm_non_default_ring_size(true),
m_ask_password(AskPasswordToDecrypt),
m_min_output_count(0),
@@ -1128,6 +1128,8 @@ wallet2::wallet2(network_type nettype, uint64_t kdf_rounds, bool unattended):
m_key_reuse_mitigation2(true),
m_segregation_height(0),
m_ignore_fractional_outputs(true),
+ m_ignore_outputs_above(MONEY_SUPPLY),
+ m_ignore_outputs_below(0),
m_track_uses(false),
m_inactivity_lock_timeout(DEFAULT_INACTIVITY_LOCK_TIMEOUT),
m_setup_background_mining(BackgroundMiningMaybe),
@@ -1794,7 +1796,7 @@ void wallet2::cache_tx_data(const cryptonote::transaction& tx, const crypto::has
}
}
//----------------------------------------------------------------------------------------------------
-void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen, const tx_cache_data &tx_cache_data, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache)
+void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint8_t block_version, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen, const tx_cache_data &tx_cache_data, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache)
{
PERF_TIMER(process_new_transaction);
// In this function, tx (probably) only contains the base information
@@ -1999,7 +2001,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote
uint64_t amount = tx.vout[o].amount ? tx.vout[o].amount : tx_scan_info[o].amount;
if (!pool)
{
- m_transfers.push_back(boost::value_initialized<transfer_details>());
+ m_transfers.push_back(transfer_details{});
transfer_details& td = m_transfers.back();
td.m_block_height = height;
td.m_internal_output_index = o;
@@ -2286,8 +2288,18 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote
}
else if (get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id))
{
- LOG_PRINT_L2("Found unencrypted payment ID: " << payment_id);
- MWARNING("Found unencrypted payment ID: these are bad for privacy, consider using subaddresses instead");
+ bool ignore = block_version >= IGNORE_LONG_PAYMENT_ID_FROM_BLOCK_VERSION;
+ if (ignore)
+ {
+ LOG_PRINT_L2("Found unencrypted payment ID in tx " << txid << " (ignored)");
+ MWARNING("Found OBSOLETE AND IGNORED unencrypted payment ID: these are bad for privacy, use subaddresses instead");
+ payment_id = crypto::null_hash;
+ }
+ else
+ {
+ LOG_PRINT_L2("Found unencrypted payment ID: " << payment_id);
+ MWARNING("Found unencrypted payment ID: these are bad for privacy, consider using subaddresses instead");
+ }
}
}
@@ -2423,7 +2435,7 @@ void wallet2::process_new_blockchain_entry(const cryptonote::block& b, const cry
{
TIME_MEASURE_START(miner_tx_handle_time);
if (m_refresh_type != RefreshNoCoinbase)
- process_new_transaction(get_transaction_hash(b.miner_tx), b.miner_tx, parsed_block.o_indices.indices[0].indices, height, b.timestamp, true, false, false, tx_cache_data[tx_cache_data_offset], output_tracker_cache);
+ process_new_transaction(get_transaction_hash(b.miner_tx), b.miner_tx, parsed_block.o_indices.indices[0].indices, height, b.major_version, b.timestamp, true, false, false, tx_cache_data[tx_cache_data_offset], output_tracker_cache);
++tx_cache_data_offset;
TIME_MEASURE_FINISH(miner_tx_handle_time);
@@ -2432,7 +2444,7 @@ void wallet2::process_new_blockchain_entry(const cryptonote::block& b, const cry
THROW_WALLET_EXCEPTION_IF(bche.txs.size() != parsed_block.txes.size(), error::wallet_internal_error, "Wrong amount of transactions for block");
for (size_t idx = 0; idx < b.tx_hashes.size(); ++idx)
{
- process_new_transaction(b.tx_hashes[idx], parsed_block.txes[idx], parsed_block.o_indices.indices[idx+1].indices, height, b.timestamp, false, false, false, tx_cache_data[tx_cache_data_offset++], output_tracker_cache);
+ process_new_transaction(b.tx_hashes[idx], parsed_block.txes[idx], parsed_block.o_indices.indices[idx+1].indices, height, b.major_version, b.timestamp, false, false, false, tx_cache_data[tx_cache_data_offset++], output_tracker_cache);
}
TIME_MEASURE_FINISH(txs_handle_time);
m_last_block_reward = cryptonote::get_outs_money_amount(b.miner_tx);
@@ -2735,7 +2747,7 @@ void wallet2::pull_and_parse_next_blocks(uint64_t start_height, uint64_t &blocks
for (size_t j = 0; j < blocks[i].txs.size(); ++j)
{
tpool.submit(&waiter, [&, i, j](){
- if (!parse_and_validate_tx_base_from_blob(blocks[i].txs[j], parsed_blocks[i].txes[j]))
+ if (!parse_and_validate_tx_base_from_blob(blocks[i].txs[j].blob, parsed_blocks[i].txes[j]))
{
boost::unique_lock<boost::mutex> lock(error_lock);
error = true;
@@ -2963,7 +2975,7 @@ void wallet2::update_pool_state(bool refreshed)
[tx_hash](const std::pair<crypto::hash, bool> &e) { return e.first == tx_hash; });
if (i != txids.end())
{
- process_new_transaction(tx_hash, tx, std::vector<uint64_t>(), 0, time(NULL), false, true, tx_entry.double_spend_seen, {});
+ process_new_transaction(tx_hash, tx, std::vector<uint64_t>(), 0, 0, time(NULL), false, true, tx_entry.double_spend_seen, {});
m_scanned_pool_txs[0].insert(tx_hash);
if (m_scanned_pool_txs[0].size() > 5000)
{
@@ -3080,6 +3092,21 @@ bool wallet2::add_address_book_row(const cryptonote::account_public_address &add
return false;
}
+bool wallet2::set_address_book_row(size_t row_id, const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description, bool is_subaddress)
+{
+ wallet2::address_book_row a;
+ a.m_address = address;
+ a.m_payment_id = payment_id;
+ a.m_description = description;
+ a.m_is_subaddress = is_subaddress;
+
+ const auto size = m_address_book.size();
+ if (row_id >= size)
+ return false;
+ m_address_book[row_id] = a;
+ return true;
+}
+
bool wallet2::delete_address_book_row(std::size_t row_id) {
if(m_address_book.size() <= row_id)
return false;
@@ -3557,7 +3584,7 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable
bool r = epee::serialization::store_t_to_binary(account, account_data);
CHECK_AND_ASSERT_MES(r, false, "failed to serialize wallet keys");
- wallet2::keys_file_data keys_file_data = boost::value_initialized<wallet2::keys_file_data>();
+ wallet2::keys_file_data keys_file_data = {};
// Create a JSON object with "key_data" and "seed_language" as keys.
rapidjson::Document json;
@@ -3625,9 +3652,6 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable
value2.SetUint64(m_refresh_from_block_height);
json.AddMember("refresh_height", value2, json.GetAllocator());
- value2.SetInt(m_confirm_missing_payment_id ? 1 :0);
- json.AddMember("confirm_missing_payment_id", value2, json.GetAllocator());
-
value2.SetInt(m_confirm_non_default_ring_size ? 1 :0);
json.AddMember("confirm_non_default_ring_size", value2, json.GetAllocator());
@@ -3673,6 +3697,12 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable
value2.SetInt(m_ignore_fractional_outputs ? 1 : 0);
json.AddMember("ignore_fractional_outputs", value2, json.GetAllocator());
+ value2.SetUint64(m_ignore_outputs_above);
+ json.AddMember("ignore_outputs_above", value2, json.GetAllocator());
+
+ value2.SetUint64(m_ignore_outputs_below);
+ json.AddMember("ignore_outputs_below", value2, json.GetAllocator());
+
value2.SetInt(m_track_uses ? 1 : 0);
json.AddMember("track_uses", value2, json.GetAllocator());
@@ -3819,7 +3849,6 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
m_auto_refresh = true;
m_refresh_type = RefreshType::RefreshDefault;
m_refresh_from_block_height = 0;
- m_confirm_missing_payment_id = true;
m_confirm_non_default_ring_size = true;
m_ask_password = AskPasswordToDecrypt;
cryptonote::set_default_decimal_point(CRYPTONOTE_DISPLAY_DECIMAL_POINT);
@@ -3834,6 +3863,8 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
m_key_reuse_mitigation2 = true;
m_segregation_height = 0;
m_ignore_fractional_outputs = true;
+ m_ignore_outputs_above = MONEY_SUPPLY;
+ m_ignore_outputs_below = 0;
m_track_uses = false;
m_inactivity_lock_timeout = DEFAULT_INACTIVITY_LOCK_TIMEOUT;
m_setup_background_mining = BackgroundMiningMaybe;
@@ -3954,8 +3985,6 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
}
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, refresh_height, uint64_t, Uint64, false, 0);
m_refresh_from_block_height = field_refresh_height;
- GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, confirm_missing_payment_id, int, Int, false, true);
- m_confirm_missing_payment_id = field_confirm_missing_payment_id;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, confirm_non_default_ring_size, int, Int, false, true);
m_confirm_non_default_ring_size = field_confirm_non_default_ring_size;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, ask_password, AskPasswordType, Int, false, AskPasswordToDecrypt);
@@ -3990,6 +4019,10 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
m_segregation_height = field_segregation_height;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, ignore_fractional_outputs, int, Int, false, true);
m_ignore_fractional_outputs = field_ignore_fractional_outputs;
+ GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, ignore_outputs_above, uint64_t, Uint64, false, MONEY_SUPPLY);
+ m_ignore_outputs_above = field_ignore_outputs_above;
+ GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, ignore_outputs_below, uint64_t, Uint64, false, 0);
+ m_ignore_outputs_below = field_ignore_outputs_below;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, track_uses, int, Int, false, false);
m_track_uses = field_track_uses;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, inactivity_lock_timeout, uint32_t, Uint, false, DEFAULT_INACTIVITY_LOCK_TIMEOUT);
@@ -5530,7 +5563,7 @@ void wallet2::store_to(const std::string &path, const epee::wipeable_string &pas
boost::archive::portable_binary_oarchive ar(oss);
ar << *this;
- wallet2::cache_file_data cache_file_data = boost::value_initialized<wallet2::cache_file_data>();
+ wallet2::cache_file_data cache_file_data = {};
cache_file_data.cache_data = oss.str();
std::string cipher;
cipher.resize(cache_file_data.cache_data.size());
@@ -5556,6 +5589,11 @@ void wallet2::store_to(const std::string &path, const epee::wipeable_string &pas
const std::string address_file = m_wallet_file + ".address.txt";
r = save_to_file(address_file, m_account.get_public_address_str(m_nettype), true);
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_wallet_file);
+ // remove old address file
+ r = boost::filesystem::remove(old_address_file);
+ if (!r) {
+ LOG_ERROR("error removing file: " << old_address_file);
+ }
}
// remove old wallet file
r = boost::filesystem::remove(old_file);
@@ -5567,11 +5605,6 @@ void wallet2::store_to(const std::string &path, const epee::wipeable_string &pas
if (!r) {
LOG_ERROR("error removing file: " << old_keys_file);
}
- // remove old address file
- r = boost::filesystem::remove(old_address_file);
- if (!r) {
- LOG_ERROR("error removing file: " << old_address_file);
- }
// remove old message store file
if (boost::filesystem::exists(old_mms_file))
{
@@ -8585,6 +8618,11 @@ std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money, ui
const transfer_details& td = m_transfers[i];
if (!is_spent(td, false) && !td.m_frozen && td.is_rct() && td.amount() >= needed_money && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1)
{
+ if (td.amount() > m_ignore_outputs_above || td.amount() < m_ignore_outputs_below)
+ {
+ MDEBUG("Ignoring output " << i << " of amount " << print_money(td.amount()) << " which is outside prescribed range [" << print_money(m_ignore_outputs_below) << ", " << print_money(m_ignore_outputs_above) << "]");
+ continue;
+ }
LOG_PRINT_L2("We can use " << i << " alone: " << print_money(td.amount()));
picks.push_back(i);
return picks;
@@ -8600,10 +8638,20 @@ std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money, ui
const transfer_details& td = m_transfers[i];
if (!is_spent(td, false) && !td.m_frozen && !td.m_key_image_partial && td.is_rct() && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1)
{
+ if (td.amount() > m_ignore_outputs_above || td.amount() < m_ignore_outputs_below)
+ {
+ MDEBUG("Ignoring output " << i << " of amount " << print_money(td.amount()) << " which is outside prescribed range [" << print_money(m_ignore_outputs_below) << ", " << print_money(m_ignore_outputs_above) << "]");
+ continue;
+ }
LOG_PRINT_L2("Considering input " << i << ", " << print_money(td.amount()));
for (size_t j = i + 1; j < m_transfers.size(); ++j)
{
const transfer_details& td2 = m_transfers[j];
+ if (td2.amount() > m_ignore_outputs_above || td2.amount() < m_ignore_outputs_below)
+ {
+ MDEBUG("Ignoring output " << j << " of amount " << print_money(td2.amount()) << " which is outside prescribed range [" << print_money(m_ignore_outputs_below) << ", " << print_money(m_ignore_outputs_above) << "]");
+ continue;
+ }
if (!is_spent(td2, false) && !td2.m_frozen && !td.m_key_image_partial && td2.is_rct() && td.amount() + td2.amount() >= needed_money && is_transfer_unlocked(td2) && td2.m_subaddr_index == td.m_subaddr_index)
{
// update our picks if those outputs are less related than any we
@@ -8813,7 +8861,7 @@ void wallet2::light_wallet_get_unspent_outs()
if(!add_transfer)
continue;
- m_transfers.push_back(boost::value_initialized<transfer_details>());
+ m_transfers.push_back(transfer_details{});
transfer_details& td = m_transfers.back();
td.m_block_height = o.height;
@@ -9303,11 +9351,16 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp
const transfer_details& td = m_transfers[i];
if (m_ignore_fractional_outputs && td.amount() < fractional_threshold)
{
- MDEBUG("Ignoring output " << i << " of amount " << print_money(td.amount()) << " which is below threshold " << print_money(fractional_threshold));
+ MDEBUG("Ignoring output " << i << " of amount " << print_money(td.amount()) << " which is below fractional threshold " << print_money(fractional_threshold));
continue;
}
if (!is_spent(td, false) && !td.m_frozen && !td.m_key_image_partial && (use_rct ? true : !td.is_rct()) && is_transfer_unlocked(td) && td.m_subaddr_index.major == subaddr_account && subaddr_indices.count(td.m_subaddr_index.minor) == 1)
{
+ if (td.amount() > m_ignore_outputs_above || td.amount() < m_ignore_outputs_below)
+ {
+ MDEBUG("Ignoring output " << i << " of amount " << print_money(td.amount()) << " which is outside prescribed range [" << print_money(m_ignore_outputs_below) << ", " << print_money(m_ignore_outputs_above) << "]");
+ continue;
+ }
const uint32_t index_minor = td.m_subaddr_index.minor;
auto find_predicate = [&index_minor](const std::pair<uint32_t, std::vector<size_t>>& x) { return x.first == index_minor; };
if ((td.is_rct()) || is_valid_decomposed_amount(td.amount()))
@@ -13181,6 +13234,12 @@ bool wallet2::save_to_file(const std::string& path_to_file, const std::string& r
}
FILE *fp = fopen(path_to_file.c_str(), "w+");
+ if (!fp)
+ {
+ MERROR("Failed to open wallet file for writing: " << path_to_file << ": " << strerror(errno));
+ return false;
+ }
+
// Save the result b/c we need to close the fp before returning success/failure.
int write_result = PEM_write(fp, ASCII_OUTPUT_MAGIC.c_str(), "", (const unsigned char *) raw.c_str(), raw.length());
fclose(fp);
diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h
index 95f6f507a..52118c426 100644
--- a/src/wallet/wallet2.h
+++ b/src/wallet/wallet2.h
@@ -1019,8 +1019,6 @@ private:
void set_default_priority(uint32_t p) { m_default_priority = p; }
bool auto_refresh() const { return m_auto_refresh; }
void auto_refresh(bool r) { m_auto_refresh = r; }
- bool confirm_missing_payment_id() const { return m_confirm_missing_payment_id; }
- void confirm_missing_payment_id(bool always) { m_confirm_missing_payment_id = always; }
AskPasswordType ask_password() const { return m_ask_password; }
void ask_password(AskPasswordType ask) { m_ask_password = ask; }
void set_min_output_count(uint32_t count) { m_min_output_count = count; }
@@ -1047,6 +1045,10 @@ private:
void ignore_fractional_outputs(bool value) { m_ignore_fractional_outputs = value; }
bool confirm_non_default_ring_size() const { return m_confirm_non_default_ring_size; }
void confirm_non_default_ring_size(bool always) { m_confirm_non_default_ring_size = always; }
+ uint64_t ignore_outputs_above() const { return m_ignore_outputs_above; }
+ void ignore_outputs_above(uint64_t value) { m_ignore_outputs_above = value; }
+ uint64_t ignore_outputs_below() const { return m_ignore_outputs_below; }
+ void ignore_outputs_below(uint64_t value) { m_ignore_outputs_below = value; }
bool track_uses() const { return m_track_uses; }
void track_uses(bool value) { m_track_uses = value; }
BackgroundMiningSetupType setup_background_mining() const { return m_setup_background_mining; }
@@ -1098,6 +1100,7 @@ private:
*/
std::vector<address_book_row> get_address_book() const { return m_address_book; }
bool add_address_book_row(const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description, bool is_subaddress);
+ bool set_address_book_row(size_t row_id, const cryptonote::account_public_address &address, const crypto::hash &payment_id, const std::string &description, bool is_subaddress);
bool delete_address_book_row(std::size_t row_id);
uint64_t get_num_rct_outputs();
@@ -1350,7 +1353,7 @@ private:
* \param password Password of wallet file
*/
bool load_keys(const std::string& keys_file_name, const epee::wipeable_string& password);
- void process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen, const tx_cache_data &tx_cache_data, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL);
+ void process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint8_t block_version, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen, const tx_cache_data &tx_cache_data, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL);
bool should_skip_block(const cryptonote::block &b, uint64_t height) const;
void process_new_blockchain_entry(const cryptonote::block& b, const cryptonote::block_complete_entry& bche, const parsed_block &parsed_block, const crypto::hash& bl_id, uint64_t height, const std::vector<tx_cache_data> &tx_cache_data, size_t tx_cache_data_offset, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL);
void detach_blockchain(uint64_t height, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL);
@@ -1495,7 +1498,6 @@ private:
// If m_refresh_from_block_height is explicitly set to zero we need this to differentiate it from the case that
// m_refresh_from_block_height was defaulted to zero.*/
bool m_explicit_refresh_from_block_height;
- bool m_confirm_missing_payment_id;
bool m_confirm_non_default_ring_size;
AskPasswordType m_ask_password;
uint32_t m_min_output_count;
@@ -1509,6 +1511,8 @@ private:
bool m_key_reuse_mitigation2;
uint64_t m_segregation_height;
bool m_ignore_fractional_outputs;
+ uint64_t m_ignore_outputs_above;
+ uint64_t m_ignore_outputs_below;
bool m_track_uses;
uint32_t m_inactivity_lock_timeout;
BackgroundMiningSetupType m_setup_background_mining;
diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp
index 765a6c24e..dbd42ab81 100644
--- a/src/wallet/wallet_rpc_server.cpp
+++ b/src/wallet/wallet_rpc_server.cpp
@@ -793,30 +793,9 @@ namespace tools
if (!payment_id.empty())
{
-
- /* Just to clarify */
- const std::string& payment_id_str = payment_id;
-
- crypto::hash long_payment_id;
- crypto::hash8 short_payment_id;
-
- /* Parse payment ID */
- if (wallet2::parse_long_payment_id(payment_id_str, long_payment_id)) {
- cryptonote::set_payment_id_to_tx_extra_nonce(extra_nonce, long_payment_id);
- }
- else {
- er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID;
- er.message = "Payment id has invalid format: \"" + payment_id_str + "\", expected 64 character string";
- return false;
- }
-
- /* Append Payment ID data into extra */
- if (!cryptonote::add_extra_nonce_to_tx_extra(extra, extra_nonce)) {
- er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID;
- er.message = "Something went wrong with payment_id. Please check its format: \"" + payment_id_str + "\", expected 64-character string";
- return false;
- }
-
+ er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID;
+ er.message = "Standalone payment IDs are obsolete. Use subaddresses or integrated addresses instead";
+ return false;
}
return true;
}
@@ -1003,6 +982,13 @@ namespace tools
std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, priority, extra, req.account_index, req.subaddr_indices);
LOG_PRINT_L2("on_transfer_split called create_transactions_2");
+ if (ptx_vector.empty())
+ {
+ er.code = WALLET_RPC_ERROR_CODE_TX_NOT_POSSIBLE;
+ er.message = "No transaction created";
+ return false;
+ }
+
return fill_response(ptx_vector, req.get_tx_keys, res.tx_key_list, res.amount_list, res.fee_list, res.multisig_txset, res.unsigned_txset, req.do_not_relay,
res.tx_hash_list, req.get_tx_hex, res.tx_blob_list, req.get_tx_metadata, res.tx_metadata_list, er);
}
@@ -1194,8 +1180,11 @@ namespace tools
crypto::hash payment_id;
if(cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8))
{
- desc.payment_id = epee::string_tools::pod_to_hex(payment_id8);
- has_encrypted_payment_id = true;
+ if (payment_id8 != crypto::null_hash8)
+ {
+ desc.payment_id = epee::string_tools::pod_to_hex(payment_id8);
+ has_encrypted_payment_id = true;
+ }
}
else if (cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id))
{
@@ -2824,6 +2813,108 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
+ bool wallet_rpc_server::on_edit_address_book(const wallet_rpc::COMMAND_RPC_EDIT_ADDRESS_BOOK_ENTRY::request& req, wallet_rpc::COMMAND_RPC_EDIT_ADDRESS_BOOK_ENTRY::response& res, epee::json_rpc::error& er, const connection_context *ctx)
+ {
+ if (!m_wallet) return not_open(er);
+ if (m_restricted)
+ {
+ er.code = WALLET_RPC_ERROR_CODE_DENIED;
+ er.message = "Command unavailable in restricted mode.";
+ return false;
+ }
+
+ const auto ab = m_wallet->get_address_book();
+ if (req.index >= ab.size())
+ {
+ er.code = WALLET_RPC_ERROR_CODE_WRONG_INDEX;
+ er.message = "Index out of range: " + std::to_string(req.index);
+ return false;
+ }
+
+ tools::wallet2::address_book_row entry = ab[req.index];
+
+ cryptonote::address_parse_info info;
+ crypto::hash payment_id = crypto::null_hash;
+ if (req.set_address)
+ {
+ er.message = "";
+ if(!get_account_address_from_str_or_url(info, m_wallet->nettype(), req.address,
+ [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string {
+ if (!dnssec_valid)
+ {
+ er.message = std::string("Invalid DNSSEC for ") + url;
+ return {};
+ }
+ if (addresses.empty())
+ {
+ er.message = std::string("No Monero address found at ") + url;
+ return {};
+ }
+ return addresses[0];
+ }))
+ {
+ er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS;
+ if (er.message.empty())
+ er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + req.address;
+ return false;
+ }
+ entry.m_address = info.address;
+ entry.m_is_subaddress = info.is_subaddress;
+ if (info.has_payment_id)
+ {
+ memcpy(entry.m_payment_id.data, info.payment_id.data, 8);
+ memset(entry.m_payment_id.data + 8, 0, 24);
+ }
+ }
+
+ if (req.set_payment_id)
+ {
+ if (req.payment_id.empty())
+ {
+ payment_id = crypto::null_hash;
+ }
+ else
+ {
+ if (req.set_address && info.has_payment_id)
+ {
+ er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID;
+ er.message = "Separate payment ID given with integrated address";
+ return false;
+ }
+
+ if (!wallet2::parse_long_payment_id(req.payment_id, payment_id))
+ {
+ crypto::hash8 spid;
+ if (!wallet2::parse_short_payment_id(req.payment_id, spid))
+ {
+ er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID;
+ er.message = "Payment id has invalid format: \"" + req.payment_id + "\", expected 64 character string";
+ return false;
+ }
+ else
+ {
+ er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID;
+ er.message = "Payment id has invalid format: standalone short payment IDs are forbidden, they must be part of an integrated address";
+ return false;
+ }
+ }
+ }
+
+ entry.m_payment_id = payment_id;
+ }
+
+ if (req.set_description)
+ entry.m_description = req.description;
+
+ if (!m_wallet->set_address_book_row(req.index, entry.m_address, entry.m_payment_id, entry.m_description, entry.m_is_subaddress))
+ {
+ er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
+ er.message = "Failed to edit address book entry";
+ return false;
+ }
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_delete_address_book(const wallet_rpc::COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY::request& req, wallet_rpc::COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY::response& res, epee::json_rpc::error& er, const connection_context *ctx)
{
if (!m_wallet) return not_open(er);
diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h
index a327ed908..b2b5e7116 100644
--- a/src/wallet/wallet_rpc_server.h
+++ b/src/wallet/wallet_rpc_server.h
@@ -127,6 +127,7 @@ namespace tools
MAP_JON_RPC_WE("parse_uri", on_parse_uri, wallet_rpc::COMMAND_RPC_PARSE_URI)
MAP_JON_RPC_WE("get_address_book", on_get_address_book, wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY)
MAP_JON_RPC_WE("add_address_book", on_add_address_book, wallet_rpc::COMMAND_RPC_ADD_ADDRESS_BOOK_ENTRY)
+ MAP_JON_RPC_WE("edit_address_book", on_edit_address_book, wallet_rpc::COMMAND_RPC_EDIT_ADDRESS_BOOK_ENTRY)
MAP_JON_RPC_WE("delete_address_book",on_delete_address_book,wallet_rpc::COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY)
MAP_JON_RPC_WE("refresh", on_refresh, wallet_rpc::COMMAND_RPC_REFRESH)
MAP_JON_RPC_WE("auto_refresh", on_auto_refresh, wallet_rpc::COMMAND_RPC_AUTO_REFRESH)
@@ -212,6 +213,7 @@ namespace tools
bool on_parse_uri(const wallet_rpc::COMMAND_RPC_PARSE_URI::request& req, wallet_rpc::COMMAND_RPC_PARSE_URI::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_get_address_book(const wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_add_address_book(const wallet_rpc::COMMAND_RPC_ADD_ADDRESS_BOOK_ENTRY::request& req, wallet_rpc::COMMAND_RPC_ADD_ADDRESS_BOOK_ENTRY::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
+ bool on_edit_address_book(const wallet_rpc::COMMAND_RPC_EDIT_ADDRESS_BOOK_ENTRY::request& req, wallet_rpc::COMMAND_RPC_EDIT_ADDRESS_BOOK_ENTRY::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_delete_address_book(const wallet_rpc::COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY::request& req, wallet_rpc::COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_refresh(const wallet_rpc::COMMAND_RPC_REFRESH::request& req, wallet_rpc::COMMAND_RPC_REFRESH::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_auto_refresh(const wallet_rpc::COMMAND_RPC_AUTO_REFRESH::request& req, wallet_rpc::COMMAND_RPC_AUTO_REFRESH::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h
index d70de68be..0c86f404d 100644
--- a/src/wallet/wallet_rpc_server_commands_defs.h
+++ b/src/wallet/wallet_rpc_server_commands_defs.h
@@ -47,7 +47,7 @@
// advance which version they will stop working with
// Don't go over 32767 for any of these
#define WALLET_RPC_VERSION_MAJOR 1
-#define WALLET_RPC_VERSION_MINOR 15
+#define WALLET_RPC_VERSION_MINOR 16
#define MAKE_WALLET_RPC_VERSION(major,minor) (((major)<<16)|(minor))
#define WALLET_RPC_VERSION MAKE_WALLET_RPC_VERSION(WALLET_RPC_VERSION_MAJOR, WALLET_RPC_VERSION_MINOR)
namespace tools
@@ -1845,6 +1845,38 @@ namespace wallet_rpc
typedef epee::misc_utils::struct_init<response_t> response;
};
+ struct COMMAND_RPC_EDIT_ADDRESS_BOOK_ENTRY
+ {
+ struct request_t
+ {
+ uint64_t index;
+ bool set_address;
+ std::string address;
+ bool set_payment_id;
+ std::string payment_id;
+ bool set_description;
+ std::string description;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(index)
+ KV_SERIALIZE(set_address)
+ KV_SERIALIZE(address)
+ KV_SERIALIZE(set_payment_id)
+ KV_SERIALIZE(payment_id)
+ KV_SERIALIZE(set_description)
+ KV_SERIALIZE(description)
+ END_KV_SERIALIZE_MAP()
+ };
+ typedef epee::misc_utils::struct_init<request_t> request;
+
+ struct response_t
+ {
+ BEGIN_KV_SERIALIZE_MAP()
+ END_KV_SERIALIZE_MAP()
+ };
+ typedef epee::misc_utils::struct_init<response_t> response;
+ };
+
struct COMMAND_RPC_GET_ADDRESS_BOOK_ENTRY
{
struct request_t