2019-04-12 06:36:43 +02:00
|
|
|
// Copyright (c) 2014-2019, The Monero Project
|
2018-04-10 06:49:20 +02:00
|
|
|
// Copyright (c) 2018, The Loki Project
|
2014-10-07 01:46:25 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-08-13 07:33:20 +02:00
|
|
|
#include "cryptonote_core/service_node_rules.h"
|
|
|
|
#include "checkpoints/checkpoints.h"
|
2017-11-25 23:25:05 +01:00
|
|
|
#include "string_tools.h"
|
2015-03-06 21:20:45 +01:00
|
|
|
#include "blockchain_db.h"
|
2017-01-26 16:07:23 +01:00
|
|
|
#include "cryptonote_basic/cryptonote_format_utils.h"
|
2015-02-12 00:55:53 +01:00
|
|
|
#include "profile_tools.h"
|
2017-07-31 17:36:52 +02:00
|
|
|
#include "ringct/rctOps.h"
|
2020-10-23 22:32:28 +02:00
|
|
|
#include "common/hex.h"
|
2014-10-07 01:46:25 +02:00
|
|
|
|
2017-08-19 16:27:13 +02:00
|
|
|
#include "lmdb/db_lmdb.h"
|
2017-08-20 14:37:19 +02:00
|
|
|
|
2018-04-10 06:49:20 +02:00
|
|
|
#undef LOKI_DEFAULT_LOG_CATEGORY
|
|
|
|
#define LOKI_DEFAULT_LOG_CATEGORY "blockchain.db"
|
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 17:34:23 +01:00
|
|
|
|
2014-10-07 01:46:25 +02:00
|
|
|
namespace cryptonote
|
|
|
|
{
|
|
|
|
|
2017-08-29 12:14:42 +02:00
|
|
|
const command_line::arg_descriptor<std::string> arg_db_sync_mode = {
|
|
|
|
"db-sync-mode"
|
2018-08-01 12:09:35 +02:00
|
|
|
, "Specify sync option, using format [safe|fast|fastest]:[sync|async]:[<nblocks_per_sync>[blocks]|<nbytes_per_sync>[bytes]]."
|
|
|
|
, "fast:async:250000000bytes"
|
2017-08-29 12:14:42 +02:00
|
|
|
};
|
|
|
|
const command_line::arg_descriptor<bool> arg_db_salvage = {
|
|
|
|
"db-salvage"
|
|
|
|
, "Try to salvage a blockchain database if it seems corrupted"
|
|
|
|
, false
|
|
|
|
};
|
|
|
|
|
2019-06-26 16:39:41 +02:00
|
|
|
BlockchainDB *new_db()
|
2017-08-19 16:27:13 +02:00
|
|
|
{
|
2019-06-26 16:39:41 +02:00
|
|
|
return new BlockchainLMDB();
|
2017-08-19 16:27:13 +02:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:14:42 +02:00
|
|
|
void BlockchainDB::init_options(boost::program_options::options_description& desc)
|
|
|
|
{
|
|
|
|
command_line::add_arg(desc, arg_db_sync_mode);
|
|
|
|
command_line::add_arg(desc, arg_db_salvage);
|
|
|
|
}
|
|
|
|
|
2014-10-07 01:46:25 +02:00
|
|
|
void BlockchainDB::pop_block()
|
|
|
|
{
|
|
|
|
block blk;
|
|
|
|
std::vector<transaction> txs;
|
|
|
|
pop_block(blk, txs);
|
|
|
|
}
|
|
|
|
|
2018-11-11 15:51:03 +01:00
|
|
|
void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const std::pair<transaction, blobdata>& txp, const crypto::hash* tx_hash_ptr, const crypto::hash* tx_prunable_hash_ptr)
|
2014-10-07 01:46:25 +02:00
|
|
|
{
|
2018-11-11 15:51:03 +01:00
|
|
|
const transaction &tx = txp.first;
|
|
|
|
|
2016-08-12 19:45:07 +02:00
|
|
|
bool miner_tx = false;
|
2017-10-01 12:24:33 +02:00
|
|
|
crypto::hash tx_hash, tx_prunable_hash;
|
2015-02-12 00:55:53 +01:00
|
|
|
if (!tx_hash_ptr)
|
|
|
|
{
|
|
|
|
// should only need to compute hash for miner transactions
|
|
|
|
tx_hash = get_transaction_hash(tx);
|
|
|
|
LOG_PRINT_L3("null tx_hash_ptr - needed to compute: " << tx_hash);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tx_hash = *tx_hash_ptr;
|
|
|
|
}
|
Infinite Staking Part 2 (#406)
* Cleanup and undoing some protocol breakages
* Simplify expiration of nodes
* Request unlock schedules entire node for expiration
* Fix off by one in expiring nodes
* Undo expiring code for pre v10 nodes
* Fix RPC returning register as unlock height and not checking 0
* Rename key image unlock height const
* Undo testnet hardfork debug changes
* Remove is_type for get_type, fix missing var rename
* Move serialisable data into public namespace
* Serialise tx types properly
* Fix typo in no service node known msg
* Code review
* Fix == to >= on serialising tx type
* Code review 2
* Fix tests and key image unlock
* Add command to print locked key images
* Update ui to display lock stakes, query in print cmd blacklist
* Modify print stakes to be less slow
* Remove autostaking code
* Refactor staking into sweep functions
It appears staking was derived off stake_main written separately at
implementation at the beginning. This merges them back into a common
code path, after removing autostake there's only some minor differences.
It also makes sure that any changes to sweeping upstream are going to be
considered in the staking process which we want.
* Display unlock height for stakes
* Begin creating output blacklist
* Make blacklist output a migration step
* Implement get_output_blacklist for lmdb
* In wallet output selection ignore blacklisted outputs
* Apply blacklisted outputs to output selection
* Fix broken tests, switch key image unlock
* Fix broken unit_tests
* Begin change to limit locked key images to 4 globally
* Revamp prepare registration for new min contribution rules
* Fix up old back case in prepare registration
* Remove debug code
* Cleanup debug code and some unecessary changes
* Fix migration step on mainnet db
* Fix blacklist outputs for pre-existing DB's
* Remove irrelevant note
* Tweak scanning addresses for locked stakes
Since we only now allow contributions from the primary address we can
skip checking all subaddress + lookahead to speed up wallet scanning
* Define macro for SCNu64 for Mingw
* Fix failure on empty DB
* Add missing error msg, remove contributor from stake
* Improve staking messages
* Flush prompt to always display
* Return the msg from stake failure and fix stake parsing error
* Tweak fork rules for smaller bulletproofs
* Tweak pooled nodes minimum amounts
* Fix crash on exit, there's no need to store on destructor
Since all information about service nodes is derived from the blockchain
and we store state every time we receive a block, storing in the
destructor is redundant as there is no new information to store.
* Make prompt be consistent with CLI
* Check max number of key images from per user to node
* Implement error message on get_output_blacklist failure
* Remove resolved TODO's/comments
* Handle infinite staking in print_sn
* Atoi->strtol, fix prepare_registration, virtual override, stale msgs
2019-02-14 02:12:57 +01:00
|
|
|
|
|
|
|
bool has_blacklisted_outputs = false;
|
2019-06-11 20:53:46 +02:00
|
|
|
if (tx.version >= cryptonote::txversion::v2_ringct)
|
2017-10-01 12:24:33 +02:00
|
|
|
{
|
|
|
|
if (!tx_prunable_hash_ptr)
|
2018-11-11 15:51:03 +01:00
|
|
|
tx_prunable_hash = get_transaction_prunable_hash(tx, &txp.second);
|
2017-10-01 12:24:33 +02:00
|
|
|
else
|
|
|
|
tx_prunable_hash = *tx_prunable_hash_ptr;
|
Infinite Staking Part 2 (#406)
* Cleanup and undoing some protocol breakages
* Simplify expiration of nodes
* Request unlock schedules entire node for expiration
* Fix off by one in expiring nodes
* Undo expiring code for pre v10 nodes
* Fix RPC returning register as unlock height and not checking 0
* Rename key image unlock height const
* Undo testnet hardfork debug changes
* Remove is_type for get_type, fix missing var rename
* Move serialisable data into public namespace
* Serialise tx types properly
* Fix typo in no service node known msg
* Code review
* Fix == to >= on serialising tx type
* Code review 2
* Fix tests and key image unlock
* Add command to print locked key images
* Update ui to display lock stakes, query in print cmd blacklist
* Modify print stakes to be less slow
* Remove autostaking code
* Refactor staking into sweep functions
It appears staking was derived off stake_main written separately at
implementation at the beginning. This merges them back into a common
code path, after removing autostake there's only some minor differences.
It also makes sure that any changes to sweeping upstream are going to be
considered in the staking process which we want.
* Display unlock height for stakes
* Begin creating output blacklist
* Make blacklist output a migration step
* Implement get_output_blacklist for lmdb
* In wallet output selection ignore blacklisted outputs
* Apply blacklisted outputs to output selection
* Fix broken tests, switch key image unlock
* Fix broken unit_tests
* Begin change to limit locked key images to 4 globally
* Revamp prepare registration for new min contribution rules
* Fix up old back case in prepare registration
* Remove debug code
* Cleanup debug code and some unecessary changes
* Fix migration step on mainnet db
* Fix blacklist outputs for pre-existing DB's
* Remove irrelevant note
* Tweak scanning addresses for locked stakes
Since we only now allow contributions from the primary address we can
skip checking all subaddress + lookahead to speed up wallet scanning
* Define macro for SCNu64 for Mingw
* Fix failure on empty DB
* Add missing error msg, remove contributor from stake
* Improve staking messages
* Flush prompt to always display
* Return the msg from stake failure and fix stake parsing error
* Tweak fork rules for smaller bulletproofs
* Tweak pooled nodes minimum amounts
* Fix crash on exit, there's no need to store on destructor
Since all information about service nodes is derived from the blockchain
and we store state every time we receive a block, storing in the
destructor is redundant as there is no new information to store.
* Make prompt be consistent with CLI
* Check max number of key images from per user to node
* Implement error message on get_output_blacklist failure
* Remove resolved TODO's/comments
* Handle infinite staking in print_sn
* Atoi->strtol, fix prepare_registration, virtual override, stale msgs
2019-02-14 02:12:57 +01:00
|
|
|
|
|
|
|
crypto::secret_key secret_tx_key;
|
|
|
|
cryptonote::account_public_address address;
|
|
|
|
if (get_tx_secret_key_from_tx_extra(tx.extra, secret_tx_key) && get_service_node_contributor_from_tx_extra(tx.extra, address))
|
|
|
|
has_blacklisted_outputs = true;
|
2017-10-01 12:24:33 +02:00
|
|
|
}
|
2014-10-07 01:46:25 +02:00
|
|
|
|
2015-08-11 12:09:09 +02:00
|
|
|
for (const txin_v& tx_input : tx.vin)
|
|
|
|
{
|
2020-06-02 05:47:20 +02:00
|
|
|
if (std::holds_alternative<txin_to_key>(tx_input))
|
2015-08-11 12:09:09 +02:00
|
|
|
{
|
2020-10-16 00:44:36 +02:00
|
|
|
add_spent_key(var::get<txin_to_key>(tx_input).k_image);
|
2015-08-11 12:09:09 +02:00
|
|
|
}
|
2020-06-02 05:47:20 +02:00
|
|
|
else if (std::holds_alternative<txin_gen>(tx_input))
|
2015-08-11 12:09:09 +02:00
|
|
|
{
|
|
|
|
/* nothing to do here */
|
2016-08-12 19:45:07 +02:00
|
|
|
miner_tx = true;
|
2015-08-11 12:09:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_PRINT_L1("Unsupported input type, removing key images and aborting transaction addition");
|
|
|
|
for (const txin_v& tx_input : tx.vin)
|
|
|
|
{
|
2020-06-02 05:47:20 +02:00
|
|
|
if (std::holds_alternative<txin_to_key>(tx_input))
|
2015-08-11 12:09:09 +02:00
|
|
|
{
|
2020-10-16 00:44:36 +02:00
|
|
|
remove_spent_key(var::get<txin_to_key>(tx_input).k_image);
|
2015-08-11 12:09:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 15:51:03 +01:00
|
|
|
uint64_t tx_id = add_transaction_data(blk_hash, txp, tx_hash, tx_prunable_hash);
|
2014-10-07 01:46:25 +02:00
|
|
|
|
2018-12-05 20:21:43 +01:00
|
|
|
std::vector<uint64_t> amount_output_indices(tx.vout.size());
|
2016-01-31 14:10:14 +01:00
|
|
|
|
2014-10-07 01:46:25 +02:00
|
|
|
// iterate tx.vout using indices instead of C++11 foreach syntax because
|
|
|
|
// we need the index
|
2015-08-09 20:26:36 +02:00
|
|
|
for (uint64_t i = 0; i < tx.vout.size(); ++i)
|
2014-10-07 01:46:25 +02:00
|
|
|
{
|
2018-08-03 03:39:28 +02:00
|
|
|
uint64_t unlock_time = 0;
|
2019-06-11 20:53:46 +02:00
|
|
|
if (tx.version >= cryptonote::txversion::v3_per_output_unlock_times)
|
2018-08-03 03:39:28 +02:00
|
|
|
{
|
|
|
|
unlock_time = tx.output_unlock_times[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unlock_time = tx.unlock_time;
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:45:07 +02:00
|
|
|
// miner v2 txes have their coinbase output in one single out to save space,
|
|
|
|
// and we store them as rct outputs with an identity mask
|
2019-06-11 20:53:46 +02:00
|
|
|
if (miner_tx && tx.version >= cryptonote::txversion::v2_ringct)
|
2016-08-12 19:45:07 +02:00
|
|
|
{
|
|
|
|
cryptonote::tx_out vout = tx.vout[i];
|
2018-08-18 14:21:42 +02:00
|
|
|
const rct::key commitment = rct::zeroCommit(vout.amount);
|
2016-08-12 19:45:07 +02:00
|
|
|
vout.amount = 0;
|
2019-01-17 05:57:30 +01:00
|
|
|
amount_output_indices[i] = add_output(tx_hash, vout, i, unlock_time,
|
2018-12-05 20:21:43 +01:00
|
|
|
&commitment);
|
2016-08-12 19:45:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-17 05:57:30 +01:00
|
|
|
amount_output_indices[i] = add_output(tx_hash, tx.vout[i], i, unlock_time,
|
2019-06-11 20:53:46 +02:00
|
|
|
tx.version >= cryptonote::txversion::v2_ringct ? &tx.rct_signatures.outPk[i].mask : NULL);
|
2016-08-12 19:45:07 +02:00
|
|
|
}
|
2015-08-09 20:26:36 +02:00
|
|
|
}
|
Infinite Staking Part 2 (#406)
* Cleanup and undoing some protocol breakages
* Simplify expiration of nodes
* Request unlock schedules entire node for expiration
* Fix off by one in expiring nodes
* Undo expiring code for pre v10 nodes
* Fix RPC returning register as unlock height and not checking 0
* Rename key image unlock height const
* Undo testnet hardfork debug changes
* Remove is_type for get_type, fix missing var rename
* Move serialisable data into public namespace
* Serialise tx types properly
* Fix typo in no service node known msg
* Code review
* Fix == to >= on serialising tx type
* Code review 2
* Fix tests and key image unlock
* Add command to print locked key images
* Update ui to display lock stakes, query in print cmd blacklist
* Modify print stakes to be less slow
* Remove autostaking code
* Refactor staking into sweep functions
It appears staking was derived off stake_main written separately at
implementation at the beginning. This merges them back into a common
code path, after removing autostake there's only some minor differences.
It also makes sure that any changes to sweeping upstream are going to be
considered in the staking process which we want.
* Display unlock height for stakes
* Begin creating output blacklist
* Make blacklist output a migration step
* Implement get_output_blacklist for lmdb
* In wallet output selection ignore blacklisted outputs
* Apply blacklisted outputs to output selection
* Fix broken tests, switch key image unlock
* Fix broken unit_tests
* Begin change to limit locked key images to 4 globally
* Revamp prepare registration for new min contribution rules
* Fix up old back case in prepare registration
* Remove debug code
* Cleanup debug code and some unecessary changes
* Fix migration step on mainnet db
* Fix blacklist outputs for pre-existing DB's
* Remove irrelevant note
* Tweak scanning addresses for locked stakes
Since we only now allow contributions from the primary address we can
skip checking all subaddress + lookahead to speed up wallet scanning
* Define macro for SCNu64 for Mingw
* Fix failure on empty DB
* Add missing error msg, remove contributor from stake
* Improve staking messages
* Flush prompt to always display
* Return the msg from stake failure and fix stake parsing error
* Tweak fork rules for smaller bulletproofs
* Tweak pooled nodes minimum amounts
* Fix crash on exit, there's no need to store on destructor
Since all information about service nodes is derived from the blockchain
and we store state every time we receive a block, storing in the
destructor is redundant as there is no new information to store.
* Make prompt be consistent with CLI
* Check max number of key images from per user to node
* Implement error message on get_output_blacklist failure
* Remove resolved TODO's/comments
* Handle infinite staking in print_sn
* Atoi->strtol, fix prepare_registration, virtual override, stale msgs
2019-02-14 02:12:57 +01:00
|
|
|
|
|
|
|
if (has_blacklisted_outputs)
|
|
|
|
add_output_blacklist(amount_output_indices);
|
|
|
|
|
2016-04-04 03:10:58 +02:00
|
|
|
add_tx_amount_output_indices(tx_id, amount_output_indices);
|
2014-10-07 01:46:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-11 15:51:03 +01:00
|
|
|
uint64_t BlockchainDB::add_block( const std::pair<block, blobdata>& blck
|
2018-07-18 23:24:53 +02:00
|
|
|
, size_t block_weight
|
2019-01-21 18:18:50 +01:00
|
|
|
, uint64_t long_term_block_weight
|
2014-10-23 21:37:10 +02:00
|
|
|
, const difficulty_type& cumulative_difficulty
|
|
|
|
, const uint64_t& coins_generated
|
2018-11-11 15:51:03 +01:00
|
|
|
, const std::vector<std::pair<transaction, blobdata>>& txs
|
2014-10-23 21:37:10 +02:00
|
|
|
)
|
2014-10-07 01:46:25 +02:00
|
|
|
{
|
2018-11-11 15:51:03 +01:00
|
|
|
const block &blk = blck.first;
|
|
|
|
|
2017-11-15 11:02:23 +01:00
|
|
|
// sanity
|
|
|
|
if (blk.tx_hashes.size() != txs.size())
|
2017-12-09 13:56:27 +01:00
|
|
|
throw std::runtime_error("Inconsistent tx/hashes sizes");
|
2017-11-15 11:02:23 +01:00
|
|
|
|
2015-02-12 00:55:53 +01:00
|
|
|
TIME_MEASURE_START(time1);
|
2014-10-29 03:25:03 +01:00
|
|
|
crypto::hash blk_hash = get_block_hash(blk);
|
2015-02-12 00:55:53 +01:00
|
|
|
TIME_MEASURE_FINISH(time1);
|
|
|
|
time_blk_hash += time1;
|
2014-10-29 03:25:03 +01:00
|
|
|
|
2017-01-16 11:00:06 +01:00
|
|
|
uint64_t prev_height = height();
|
|
|
|
|
2014-10-23 21:37:10 +02:00
|
|
|
// call out to add the transactions
|
|
|
|
|
2015-02-12 00:55:53 +01:00
|
|
|
time1 = epee::misc_utils::get_tick_count();
|
2018-05-22 15:46:30 +02:00
|
|
|
|
|
|
|
uint64_t num_rct_outs = 0;
|
2018-11-11 15:51:03 +01:00
|
|
|
add_transaction(blk_hash, std::make_pair(blk.miner_tx, tx_to_blob(blk.miner_tx)));
|
2019-06-11 20:53:46 +02:00
|
|
|
if (blk.miner_tx.version >= cryptonote::txversion::v2_ringct)
|
2018-05-22 15:46:30 +02:00
|
|
|
num_rct_outs += blk.miner_tx.vout.size();
|
2018-09-19 11:15:03 +02:00
|
|
|
|
2015-02-12 00:55:53 +01:00
|
|
|
int tx_i = 0;
|
2017-10-10 16:47:08 +02:00
|
|
|
crypto::hash tx_hash = crypto::null_hash;
|
2018-11-11 15:51:03 +01:00
|
|
|
for (const std::pair<transaction, blobdata>& tx : txs)
|
2014-10-07 01:46:25 +02:00
|
|
|
{
|
2015-02-12 00:55:53 +01:00
|
|
|
tx_hash = blk.tx_hashes[tx_i];
|
|
|
|
add_transaction(blk_hash, tx, &tx_hash);
|
2018-11-11 15:51:03 +01:00
|
|
|
for (const auto &vout: tx.first.vout)
|
2018-05-22 15:46:30 +02:00
|
|
|
{
|
|
|
|
if (vout.amount == 0)
|
|
|
|
++num_rct_outs;
|
|
|
|
}
|
2015-02-12 00:55:53 +01:00
|
|
|
++tx_i;
|
2014-10-07 01:46:25 +02:00
|
|
|
}
|
2015-02-12 00:55:53 +01:00
|
|
|
TIME_MEASURE_FINISH(time1);
|
|
|
|
time_add_transaction += time1;
|
|
|
|
|
2017-02-11 11:16:18 +01:00
|
|
|
// call out to subclass implementation to add the block & metadata
|
|
|
|
time1 = epee::misc_utils::get_tick_count();
|
2019-01-21 18:18:50 +01:00
|
|
|
add_block(blk, block_weight, long_term_block_weight, cumulative_difficulty, coins_generated, num_rct_outs, blk_hash);
|
2017-02-11 11:16:18 +01:00
|
|
|
TIME_MEASURE_FINISH(time1);
|
|
|
|
time_add_block1 += time1;
|
|
|
|
|
2016-02-08 17:09:07 +01:00
|
|
|
m_hardfork->add(blk, prev_height);
|
2016-02-08 17:32:36 +01:00
|
|
|
|
2015-02-12 00:55:53 +01:00
|
|
|
++num_calls;
|
2014-10-07 01:46:25 +02:00
|
|
|
|
2016-02-08 17:32:36 +01:00
|
|
|
return prev_height;
|
2014-10-07 01:46:25 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 21:56:31 +01:00
|
|
|
void BlockchainDB::set_hard_fork(HardFork* hf)
|
2016-02-08 16:51:57 +01:00
|
|
|
{
|
|
|
|
m_hardfork = hf;
|
|
|
|
}
|
|
|
|
|
2014-10-07 01:46:25 +02:00
|
|
|
void BlockchainDB::pop_block(block& blk, std::vector<transaction>& txs)
|
|
|
|
{
|
|
|
|
blk = get_top_block();
|
2014-10-23 21:37:10 +02:00
|
|
|
|
|
|
|
remove_block();
|
2015-12-25 00:01:00 +01:00
|
|
|
|
2020-06-22 21:44:56 +02:00
|
|
|
for (auto it = blk.tx_hashes.rbegin(); it != blk.tx_hashes.rend(); ++it)
|
2014-10-07 01:46:25 +02:00
|
|
|
{
|
2020-06-22 21:44:56 +02:00
|
|
|
auto& h = *it;
|
Pruning
The blockchain prunes seven eighths of prunable tx data.
This saves about two thirds of the blockchain size, while
keeping the node useful as a sync source for an eighth
of the blockchain.
No other data is currently pruned.
There are three ways to prune a blockchain:
- run monerod with --prune-blockchain
- run "prune_blockchain" in the monerod console
- run the monero-blockchain-prune utility
The first two will prune in place. Due to how LMDB works, this
will not reduce the blockchain size on disk. Instead, it will
mark parts of the file as free, so that future data will use
that free space, causing the file to not grow until free space
grows scarce.
The third way will create a second database, a pruned copy of
the original one. Since this is a new file, this one will be
smaller than the original one.
Once the database is pruned, it will stay pruned as it syncs.
That is, there is no need to use --prune-blockchain again, etc.
2018-04-30 00:30:51 +02:00
|
|
|
cryptonote::transaction tx;
|
|
|
|
if (!get_tx(h, tx) && !get_pruned_tx(h, tx))
|
|
|
|
throw DB_ERROR("Failed to get pruned or unpruned transaction from the db");
|
|
|
|
txs.push_back(std::move(tx));
|
2014-10-07 01:46:25 +02:00
|
|
|
remove_transaction(h);
|
|
|
|
}
|
2015-12-25 00:01:00 +01:00
|
|
|
remove_transaction(get_transaction_hash(blk.miner_tx));
|
2014-10-07 01:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockchainDB::remove_transaction(const crypto::hash& tx_hash)
|
|
|
|
{
|
Pruning
The blockchain prunes seven eighths of prunable tx data.
This saves about two thirds of the blockchain size, while
keeping the node useful as a sync source for an eighth
of the blockchain.
No other data is currently pruned.
There are three ways to prune a blockchain:
- run monerod with --prune-blockchain
- run "prune_blockchain" in the monerod console
- run the monero-blockchain-prune utility
The first two will prune in place. Due to how LMDB works, this
will not reduce the blockchain size on disk. Instead, it will
mark parts of the file as free, so that future data will use
that free space, causing the file to not grow until free space
grows scarce.
The third way will create a second database, a pruned copy of
the original one. Since this is a new file, this one will be
smaller than the original one.
Once the database is pruned, it will stay pruned as it syncs.
That is, there is no need to use --prune-blockchain again, etc.
2018-04-30 00:30:51 +02:00
|
|
|
transaction tx = get_pruned_tx(tx_hash);
|
2014-10-07 01:46:25 +02:00
|
|
|
|
|
|
|
for (const txin_v& tx_input : tx.vin)
|
|
|
|
{
|
2020-06-02 05:47:20 +02:00
|
|
|
if (std::holds_alternative<txin_to_key>(tx_input))
|
2014-10-07 01:46:25 +02:00
|
|
|
{
|
2020-10-16 00:44:36 +02:00
|
|
|
remove_spent_key(var::get<txin_to_key>(tx_input).k_image);
|
2014-10-07 01:46:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 03:04:04 +01:00
|
|
|
// need tx as tx.vout has the tx outputs, and the output amounts are needed
|
|
|
|
remove_transaction_data(tx_hash, tx);
|
2014-10-07 01:46:25 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 06:21:44 +02:00
|
|
|
block_header BlockchainDB::get_block_header(const crypto::hash& h) const
|
2017-01-15 17:05:55 +01:00
|
|
|
{
|
2020-09-02 06:21:44 +02:00
|
|
|
block_header b = get_block_header_from_height(get_block_height(h));
|
2017-01-15 17:05:55 +01:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
block BlockchainDB::get_block(const crypto::hash& h) const
|
|
|
|
{
|
2020-09-02 06:21:44 +02:00
|
|
|
block b = get_block_from_height(get_block_height(h));
|
2017-01-15 17:05:55 +01:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockchainDB::get_tx(const crypto::hash& h, cryptonote::transaction &tx) const
|
|
|
|
{
|
|
|
|
blobdata bd;
|
|
|
|
if (!get_tx_blob(h, bd))
|
|
|
|
return false;
|
|
|
|
if (!parse_and_validate_tx_from_blob(bd, tx))
|
2017-12-09 13:56:27 +01:00
|
|
|
throw DB_ERROR("Failed to parse transaction from blob retrieved from the db");
|
2017-01-15 17:05:55 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Pruning
The blockchain prunes seven eighths of prunable tx data.
This saves about two thirds of the blockchain size, while
keeping the node useful as a sync source for an eighth
of the blockchain.
No other data is currently pruned.
There are three ways to prune a blockchain:
- run monerod with --prune-blockchain
- run "prune_blockchain" in the monerod console
- run the monero-blockchain-prune utility
The first two will prune in place. Due to how LMDB works, this
will not reduce the blockchain size on disk. Instead, it will
mark parts of the file as free, so that future data will use
that free space, causing the file to not grow until free space
grows scarce.
The third way will create a second database, a pruned copy of
the original one. Since this is a new file, this one will be
smaller than the original one.
Once the database is pruned, it will stay pruned as it syncs.
That is, there is no need to use --prune-blockchain again, etc.
2018-04-30 00:30:51 +02:00
|
|
|
bool BlockchainDB::get_pruned_tx(const crypto::hash& h, cryptonote::transaction &tx) const
|
|
|
|
{
|
|
|
|
blobdata bd;
|
|
|
|
if (!get_pruned_tx_blob(h, bd))
|
|
|
|
return false;
|
|
|
|
if (!parse_and_validate_tx_base_from_blob(bd, tx))
|
|
|
|
throw DB_ERROR("Failed to parse transaction base from blob retrieved from the db");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-15 17:05:55 +01:00
|
|
|
transaction BlockchainDB::get_tx(const crypto::hash& h) const
|
|
|
|
{
|
|
|
|
transaction tx;
|
|
|
|
if (!get_tx(h, tx))
|
2020-10-23 22:32:28 +02:00
|
|
|
throw TX_DNE("tx with hash " + tools::type_to_hex(h) + " not found in db");
|
2017-01-15 17:05:55 +01:00
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
2018-08-03 03:39:28 +02:00
|
|
|
uint64_t BlockchainDB::get_output_unlock_time(const uint64_t amount, const uint64_t amount_index) const
|
|
|
|
{
|
|
|
|
output_data_t odata = get_output_key(amount, amount_index);
|
|
|
|
return odata.unlock_time;
|
|
|
|
}
|
|
|
|
|
Pruning
The blockchain prunes seven eighths of prunable tx data.
This saves about two thirds of the blockchain size, while
keeping the node useful as a sync source for an eighth
of the blockchain.
No other data is currently pruned.
There are three ways to prune a blockchain:
- run monerod with --prune-blockchain
- run "prune_blockchain" in the monerod console
- run the monero-blockchain-prune utility
The first two will prune in place. Due to how LMDB works, this
will not reduce the blockchain size on disk. Instead, it will
mark parts of the file as free, so that future data will use
that free space, causing the file to not grow until free space
grows scarce.
The third way will create a second database, a pruned copy of
the original one. Since this is a new file, this one will be
smaller than the original one.
Once the database is pruned, it will stay pruned as it syncs.
That is, there is no need to use --prune-blockchain again, etc.
2018-04-30 00:30:51 +02:00
|
|
|
transaction BlockchainDB::get_pruned_tx(const crypto::hash& h) const
|
|
|
|
{
|
|
|
|
transaction tx;
|
|
|
|
if (!get_pruned_tx(h, tx))
|
2020-10-23 22:32:28 +02:00
|
|
|
throw TX_DNE("pruned tx with hash " + tools::type_to_hex(h) + " not found in db");
|
Pruning
The blockchain prunes seven eighths of prunable tx data.
This saves about two thirds of the blockchain size, while
keeping the node useful as a sync source for an eighth
of the blockchain.
No other data is currently pruned.
There are three ways to prune a blockchain:
- run monerod with --prune-blockchain
- run "prune_blockchain" in the monerod console
- run the monero-blockchain-prune utility
The first two will prune in place. Due to how LMDB works, this
will not reduce the blockchain size on disk. Instead, it will
mark parts of the file as free, so that future data will use
that free space, causing the file to not grow until free space
grows scarce.
The third way will create a second database, a pruned copy of
the original one. Since this is a new file, this one will be
smaller than the original one.
Once the database is pruned, it will stay pruned as it syncs.
That is, there is no need to use --prune-blockchain again, etc.
2018-04-30 00:30:51 +02:00
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:55:53 +01:00
|
|
|
void BlockchainDB::reset_stats()
|
|
|
|
{
|
|
|
|
num_calls = 0;
|
|
|
|
time_blk_hash = 0;
|
2015-02-11 05:01:02 +01:00
|
|
|
time_tx_exists = 0;
|
2015-02-12 00:55:53 +01:00
|
|
|
time_add_block1 = 0;
|
|
|
|
time_add_transaction = 0;
|
2015-02-12 00:55:53 +01:00
|
|
|
time_commit1 = 0;
|
2015-02-12 00:55:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockchainDB::show_stats()
|
|
|
|
{
|
2020-04-03 18:30:11 +02:00
|
|
|
LOG_PRINT_L1("\n"
|
|
|
|
<< "*********************************\n"
|
|
|
|
<< "num_calls: " << num_calls << "\n"
|
|
|
|
<< "time_blk_hash: " << time_blk_hash << "ms\n"
|
|
|
|
<< "time_tx_exists: " << time_tx_exists << "ms\n"
|
|
|
|
<< "time_add_block1: " << time_add_block1 << "ms\n"
|
|
|
|
<< "time_add_transaction: " << time_add_transaction << "ms\n"
|
|
|
|
<< "time_commit1: " << time_commit1 << "ms\n"
|
|
|
|
<< "*********************************\n"
|
2015-02-12 00:55:53 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-29 02:48:29 +02:00
|
|
|
void BlockchainDB::fixup(cryptonote::network_type)
|
2015-12-06 21:48:17 +01:00
|
|
|
{
|
2015-12-26 23:27:35 +01:00
|
|
|
if (is_read_only()) {
|
|
|
|
LOG_PRINT_L1("Database is opened read only - skipping fixup check");
|
|
|
|
return;
|
|
|
|
}
|
2018-07-03 03:28:22 +02:00
|
|
|
|
|
|
|
set_batch_transactions(true);
|
2015-12-06 21:48:17 +01:00
|
|
|
}
|
|
|
|
|
2019-08-13 07:33:20 +02:00
|
|
|
bool BlockchainDB::get_immutable_checkpoint(checkpoint_t *immutable_checkpoint, uint64_t block_height) const
|
|
|
|
{
|
|
|
|
size_t constexpr NUM_CHECKPOINTS = service_nodes::CHECKPOINT_NUM_CHECKPOINTS_FOR_CHAIN_FINALITY;
|
|
|
|
static_assert(NUM_CHECKPOINTS == 2,
|
|
|
|
"Expect checkpoint finality to be 2, otherwise the immutable logic needs to check for any hardcoded "
|
|
|
|
"checkpoints inbetween");
|
|
|
|
|
|
|
|
std::vector<checkpoint_t> checkpoints = get_checkpoints_range(block_height, 0, NUM_CHECKPOINTS);
|
|
|
|
|
|
|
|
if (checkpoints.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
checkpoint_t *checkpoint_ptr = nullptr;
|
|
|
|
if (checkpoints[0].type != checkpoint_type::service_node) // checkpoint[0] is the first closest checkpoint that is <= my height
|
|
|
|
{
|
|
|
|
checkpoint_ptr = &checkpoints[0]; // Must be hard-coded then, always immutable
|
|
|
|
}
|
|
|
|
else if (checkpoints.size() == NUM_CHECKPOINTS)
|
|
|
|
{
|
|
|
|
// NOTE: The first checkpoint is a service node checkpoint. Go back
|
|
|
|
// 1 checkpoint, which will either be another service node checkpoint or
|
|
|
|
// a predefined one.
|
|
|
|
checkpoint_ptr = &checkpoints[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false; // NOTE: Only one service node checkpoint recorded, we can override this checkpoint.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (immutable_checkpoint)
|
|
|
|
*immutable_checkpoint = std::move(*checkpoint_ptr);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-19 02:18:26 +01:00
|
|
|
uint64_t BlockchainDB::get_tx_block_height(const crypto::hash &h) const
|
|
|
|
{
|
|
|
|
auto result = get_tx_block_heights({{h}}).front();
|
|
|
|
if (result == std::numeric_limits<uint64_t>::max())
|
|
|
|
{
|
2020-10-23 22:32:28 +02:00
|
|
|
std::string err = "tx_data_t with hash " + tools::type_to_hex(h) + " not found in db";
|
2019-11-19 02:18:26 +01:00
|
|
|
LOG_PRINT_L1(err);
|
|
|
|
throw TX_DNE(std::move(err));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-09 09:04:25 +02:00
|
|
|
bool BlockchainDB::get_alt_block_header(const crypto::hash &blkid, alt_block_data_t *data, cryptonote::block_header *header, cryptonote::blobdata *checkpoint) const
|
|
|
|
{
|
|
|
|
cryptonote::blobdata blob;
|
|
|
|
if (!get_alt_block(blkid, data, &blob, checkpoint))
|
|
|
|
{
|
2020-10-23 22:32:28 +02:00
|
|
|
throw BLOCK_DNE("Alt-block with hash " + tools::type_to_hex(blkid) + " not found in db");
|
2020-09-09 09:04:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
serialization::binary_string_unarchiver ba{blob};
|
|
|
|
serialization::value(ba, *header);
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-31 09:45:33 +02:00
|
|
|
void BlockchainDB::fill_timestamps_and_difficulties_for_pow(cryptonote::network_type nettype,
|
|
|
|
std::vector<uint64_t> ×tamps,
|
|
|
|
std::vector<uint64_t> &difficulties,
|
|
|
|
uint64_t chain_height,
|
|
|
|
uint64_t timestamps_difficulty_height) const
|
|
|
|
{
|
2020-09-29 02:48:29 +02:00
|
|
|
constexpr uint64_t MIN_CHAIN_HEIGHT = 2;
|
|
|
|
if (chain_height < MIN_CHAIN_HEIGHT)
|
2020-08-31 09:45:33 +02:00
|
|
|
return;
|
|
|
|
|
2020-09-02 02:22:51 +02:00
|
|
|
uint64_t const top_block_height = chain_height - 1;
|
2020-09-18 21:39:50 +02:00
|
|
|
static const uint64_t hf16_height = HardFork::get_hardcoded_hard_fork_height(nettype, cryptonote::network_version_16_pulse);
|
2020-09-02 02:22:51 +02:00
|
|
|
bool const before_hf16 = chain_height < hf16_height;
|
|
|
|
uint64_t const block_count = DIFFICULTY_BLOCKS_COUNT(before_hf16);
|
2020-08-31 09:45:33 +02:00
|
|
|
|
|
|
|
timestamps.reserve(block_count);
|
|
|
|
difficulties.reserve(block_count);
|
|
|
|
|
|
|
|
if (timestamps_difficulty_height == 0 ||
|
|
|
|
(chain_height - timestamps_difficulty_height) != 1 ||
|
2020-09-02 02:22:51 +02:00
|
|
|
timestamps.size() > block_count ||
|
|
|
|
difficulties.size() > block_count)
|
2020-08-31 09:45:33 +02:00
|
|
|
{
|
|
|
|
// Cache invalidated.
|
|
|
|
timestamps.clear();
|
|
|
|
difficulties.clear();
|
|
|
|
|
|
|
|
// Fill missing timestamps/difficulties, up to one before the latest (latest is added below).
|
|
|
|
uint64_t start_height = chain_height - std::min<size_t>(chain_height, block_count);
|
2020-09-02 02:22:51 +02:00
|
|
|
start_height = std::max<uint64_t>(start_height, 1);
|
2020-08-31 09:45:33 +02:00
|
|
|
|
2020-09-29 02:48:29 +02:00
|
|
|
for (uint64_t block_height = start_height; block_height < (chain_height - 1) /*skip latest block*/; block_height++)
|
2020-08-31 09:45:33 +02:00
|
|
|
{
|
|
|
|
timestamps.push_back(get_block_timestamp(block_height));
|
|
|
|
difficulties.push_back(get_block_cumulative_difficulty(block_height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add latest timestamp/difficulty
|
2020-09-02 02:22:51 +02:00
|
|
|
add_timestamp_and_difficulty(nettype,
|
|
|
|
chain_height,
|
|
|
|
timestamps,
|
|
|
|
difficulties,
|
|
|
|
get_block_timestamp(top_block_height),
|
|
|
|
get_block_cumulative_difficulty(top_block_height));
|
|
|
|
|
2020-08-31 09:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-07 01:46:25 +02:00
|
|
|
} // namespace cryptonote
|