2018-01-07 05:05:16 +00:00
|
|
|
// Copyright (c) 2014-2018, The Monero Project
|
2015-12-14 04:54:39 +00:00
|
|
|
//
|
2014-07-23 13:03:52 +00:00
|
|
|
// All rights reserved.
|
2015-12-14 04:54:39 +00:00
|
|
|
//
|
2014-07-23 13:03:52 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
2015-12-14 04:54:39 +00:00
|
|
|
//
|
2014-07-23 13:03:52 +00:00
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
2015-12-14 04:54:39 +00:00
|
|
|
//
|
2014-07-23 13:03:52 +00:00
|
|
|
// 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.
|
2015-12-14 04:54:39 +00:00
|
|
|
//
|
2014-07-23 13:03:52 +00:00
|
|
|
// 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.
|
2015-12-14 04:54:39 +00:00
|
|
|
//
|
2014-07-23 13:03:52 +00:00
|
|
|
// 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.
|
2015-12-14 04:54:39 +00:00
|
|
|
//
|
2014-07-23 13:03:52 +00:00
|
|
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2014-04-02 16:00:17 +00:00
|
|
|
#include <algorithm>
|
2014-03-03 22:07:58 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <unordered_set>
|
2014-04-02 16:00:17 +00:00
|
|
|
#include <vector>
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
#include "tx_pool.h"
|
2017-01-26 15:07:23 +00:00
|
|
|
#include "cryptonote_tx_utils.h"
|
|
|
|
#include "cryptonote_basic/cryptonote_boost_serialization.h"
|
2014-03-03 22:07:58 +00:00
|
|
|
#include "cryptonote_config.h"
|
2014-10-28 03:44:45 +00:00
|
|
|
#include "blockchain.h"
|
2017-05-14 13:06:55 +00:00
|
|
|
#include "blockchain_db/blockchain_db.h"
|
2014-03-03 22:07:58 +00:00
|
|
|
#include "common/boost_serialization_helper.h"
|
2018-11-18 10:17:24 +00:00
|
|
|
#include "int-util.h"
|
2014-03-03 22:07:58 +00:00
|
|
|
#include "misc_language.h"
|
|
|
|
#include "warnings.h"
|
2016-10-10 19:47:52 +00:00
|
|
|
#include "common/perf_timer.h"
|
2014-03-03 22:07:58 +00:00
|
|
|
#include "crypto/hash.h"
|
|
|
|
|
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 16:34:23 +00:00
|
|
|
#undef MONERO_DEFAULT_LOG_CATEGORY
|
|
|
|
#define MONERO_DEFAULT_LOG_CATEGORY "txpool"
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
DISABLE_VS_WARNINGS(4244 4345 4503) //'boost::foreach_detail_::or_' : decorated name length exceeded, name was truncated
|
|
|
|
|
2017-10-10 14:47:08 +00:00
|
|
|
using namespace crypto;
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
namespace cryptonote
|
|
|
|
{
|
2014-07-17 15:02:05 +00:00
|
|
|
namespace
|
|
|
|
{
|
2016-03-30 15:55:54 +00:00
|
|
|
//TODO: constants such as these should at least be in the header,
|
|
|
|
// but probably somewhere more accessible to the rest of the
|
|
|
|
// codebase. As it stands, it is at best nontrivial to test
|
|
|
|
// whether or not changing these parameters (or adding new)
|
|
|
|
// will work correctly.
|
2015-11-21 00:26:48 +00:00
|
|
|
time_t const MIN_RELAY_TIME = (60 * 5); // only start re-relaying transactions after that many seconds
|
|
|
|
time_t const MAX_RELAY_TIME = (60 * 60 * 4); // at most that many seconds between resends
|
2017-01-12 23:39:24 +00:00
|
|
|
float const ACCEPT_THRESHOLD = 1.0f;
|
2015-11-21 00:26:48 +00:00
|
|
|
|
|
|
|
// a kind of increasing backoff within min/max bounds
|
2017-05-14 13:06:55 +00:00
|
|
|
uint64_t get_relay_delay(time_t now, time_t received)
|
2015-11-21 00:26:48 +00:00
|
|
|
{
|
|
|
|
time_t d = (now - received + MIN_RELAY_TIME) / MIN_RELAY_TIME * MIN_RELAY_TIME;
|
|
|
|
if (d > MAX_RELAY_TIME)
|
|
|
|
d = MAX_RELAY_TIME;
|
|
|
|
return d;
|
|
|
|
}
|
2017-01-06 20:34:37 +00:00
|
|
|
|
|
|
|
uint64_t template_accept_threshold(uint64_t amount)
|
|
|
|
{
|
|
|
|
return amount * ACCEPT_THRESHOLD;
|
|
|
|
}
|
2017-03-14 19:28:38 +00:00
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
uint64_t get_transaction_weight_limit(uint8_t version)
|
2017-03-14 19:28:38 +00:00
|
|
|
{
|
2018-07-18 21:24:53 +00:00
|
|
|
// from v8, limit a tx to 50% of the minimum block weight
|
|
|
|
if (version >= 8)
|
|
|
|
return get_min_block_weight(version) / 2 - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
|
|
|
|
else
|
|
|
|
return get_min_block_weight(version) - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
|
2017-03-14 19:28:38 +00:00
|
|
|
}
|
2017-05-14 13:06:55 +00:00
|
|
|
|
|
|
|
// This class is meant to create a batch when none currently exists.
|
|
|
|
// If a batch exists, it can't be from another thread, since we can
|
|
|
|
// only be called with the txpool lock taken, and it is held during
|
|
|
|
// the whole prepare/handle/cleanup incoming block sequence.
|
|
|
|
class LockedTXN {
|
|
|
|
public:
|
|
|
|
LockedTXN(Blockchain &b): m_blockchain(b), m_batch(false) {
|
|
|
|
m_batch = m_blockchain.get_db().batch_start();
|
|
|
|
}
|
2017-09-04 08:23:38 +00:00
|
|
|
~LockedTXN() { try { if (m_batch) { m_blockchain.get_db().batch_stop(); } } catch (const std::exception &e) { MWARNING("LockedTXN dtor filtering exception: " << e.what()); } }
|
2017-05-14 13:06:55 +00:00
|
|
|
private:
|
|
|
|
Blockchain &m_blockchain;
|
|
|
|
bool m_batch;
|
|
|
|
};
|
2014-07-17 15:02:05 +00:00
|
|
|
}
|
2015-01-26 05:36:09 +00:00
|
|
|
//---------------------------------------------------------------------------------
|
2014-03-03 22:07:58 +00:00
|
|
|
//---------------------------------------------------------------------------------
|
2018-07-18 21:24:53 +00:00
|
|
|
tx_memory_pool::tx_memory_pool(Blockchain& bchs): m_blockchain(bchs), m_txpool_max_weight(DEFAULT_TXPOOL_MAX_WEIGHT), m_txpool_weight(0), m_cookie(0)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-10-31 08:31:13 +00:00
|
|
|
bool tx_memory_pool::add_tx(transaction &tx, /*const crypto::hash& tx_prefix_hash,*/ const crypto::hash &id, const cryptonote::blobdata &blob, size_t tx_weight, tx_verification_context& tvc, bool kept_by_block, bool relayed, bool do_not_relay, uint8_t version)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2017-05-31 21:50:12 +00:00
|
|
|
// this should already be called with that lock, but let's make it explicit for clarity
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
|
2016-10-10 19:47:52 +00:00
|
|
|
PERF_TIMER(add_tx);
|
2016-06-15 22:37:13 +00:00
|
|
|
if (tx.version == 0)
|
|
|
|
{
|
|
|
|
// v0 never accepted
|
2016-07-27 09:10:38 +00:00
|
|
|
LOG_PRINT_L1("transaction version 0 is invalid");
|
2016-06-15 22:37:13 +00:00
|
|
|
tvc.m_verifivation_failed = true;
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-29 22:00:20 +00:00
|
|
|
|
2016-01-29 17:15:09 +00:00
|
|
|
// we do not accept transactions that timed out before, unless they're
|
|
|
|
// kept_by_block
|
|
|
|
if (!kept_by_block && m_timed_out_transactions.find(id) != m_timed_out_transactions.end())
|
|
|
|
{
|
|
|
|
// not clear if we should set that, since verifivation (sic) did not fail before, since
|
|
|
|
// the tx was accepted before timing out.
|
|
|
|
tvc.m_verifivation_failed = true;
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
if(!check_inputs_types_supported(tx))
|
|
|
|
{
|
|
|
|
tvc.m_verifivation_failed = true;
|
2016-03-27 11:35:36 +00:00
|
|
|
tvc.m_invalid_input = true;
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-15 22:37:13 +00:00
|
|
|
// fee per kilobyte, size rounded up.
|
|
|
|
uint64_t fee;
|
|
|
|
|
|
|
|
if (tx.version == 1)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2016-06-15 22:37:13 +00:00
|
|
|
uint64_t inputs_amount = 0;
|
|
|
|
if(!get_inputs_money_amount(tx, inputs_amount))
|
|
|
|
{
|
|
|
|
tvc.m_verifivation_failed = true;
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2016-06-15 22:37:13 +00:00
|
|
|
uint64_t outputs_amount = get_outs_money_amount(tx);
|
2018-01-30 15:16:20 +00:00
|
|
|
if(outputs_amount > inputs_amount)
|
2016-06-15 22:37:13 +00:00
|
|
|
{
|
2018-03-01 11:36:19 +00:00
|
|
|
LOG_PRINT_L1("transaction use more money than it has: use " << print_money(outputs_amount) << ", have " << print_money(inputs_amount));
|
2016-06-15 22:37:13 +00:00
|
|
|
tvc.m_verifivation_failed = true;
|
|
|
|
tvc.m_overspend = true;
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-30 15:16:20 +00:00
|
|
|
else if(outputs_amount == inputs_amount)
|
|
|
|
{
|
|
|
|
LOG_PRINT_L1("transaction fee is zero: outputs_amount == inputs_amount, rejecting.");
|
|
|
|
tvc.m_verifivation_failed = true;
|
|
|
|
tvc.m_fee_too_low = true;
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2016-06-15 22:37:13 +00:00
|
|
|
fee = inputs_amount - outputs_amount;
|
|
|
|
}
|
|
|
|
else
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2016-06-15 22:37:13 +00:00
|
|
|
fee = tx.rct_signatures.txnFee;
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
if (!kept_by_block && !m_blockchain.check_fee(tx_weight, fee))
|
2014-07-17 15:05:46 +00:00
|
|
|
{
|
|
|
|
tvc.m_verifivation_failed = true;
|
2016-03-27 11:35:36 +00:00
|
|
|
tvc.m_fee_too_low = true;
|
2014-07-17 15:05:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
size_t tx_weight_limit = get_transaction_weight_limit(version);
|
2018-09-13 09:01:05 +00:00
|
|
|
if ((!kept_by_block || version >= HF_VERSION_PER_BYTE_FEE) && tx_weight > tx_weight_limit)
|
2014-07-17 15:02:05 +00:00
|
|
|
{
|
2018-07-18 21:24:53 +00:00
|
|
|
LOG_PRINT_L1("transaction is too heavy: " << tx_weight << " bytes, maximum weight: " << tx_weight_limit);
|
2014-03-03 22:07:58 +00:00
|
|
|
tvc.m_verifivation_failed = true;
|
2016-03-27 11:35:36 +00:00
|
|
|
tvc.m_too_big = true;
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-25 00:03:02 +00:00
|
|
|
// if the transaction came from a block popped from the chain,
|
|
|
|
// don't check if we have its key images as spent.
|
|
|
|
// TODO: Investigate why not?
|
2014-03-03 22:07:58 +00:00
|
|
|
if(!kept_by_block)
|
|
|
|
{
|
|
|
|
if(have_tx_keyimges_as_spent(tx))
|
|
|
|
{
|
2017-09-22 12:57:20 +00:00
|
|
|
mark_double_spend(tx);
|
2014-09-09 10:28:16 +00:00
|
|
|
LOG_PRINT_L1("Transaction with id= "<< id << " used already spent key images");
|
2014-03-03 22:07:58 +00:00
|
|
|
tvc.m_verifivation_failed = true;
|
2016-03-27 11:35:36 +00:00
|
|
|
tvc.m_double_spend = true;
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-27 11:35:36 +00:00
|
|
|
if (!m_blockchain.check_tx_outputs(tx, tvc))
|
2015-10-06 15:22:19 +00:00
|
|
|
{
|
2016-06-15 22:37:13 +00:00
|
|
|
LOG_PRINT_L1("Transaction with id= "<< id << " has at least one invalid output");
|
2015-10-06 15:22:19 +00:00
|
|
|
tvc.m_verifivation_failed = true;
|
2016-03-27 11:35:36 +00:00
|
|
|
tvc.m_invalid_output = true;
|
2015-10-06 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2017-09-10 11:42:36 +00:00
|
|
|
// assume failure during verification steps until success is certain
|
|
|
|
tvc.m_verifivation_failed = true;
|
|
|
|
|
2017-01-13 15:08:37 +00:00
|
|
|
time_t receive_time = time(nullptr);
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
crypto::hash max_used_block_id = null_hash;
|
|
|
|
uint64_t max_used_block_height = 0;
|
2017-05-14 13:06:55 +00:00
|
|
|
cryptonote::txpool_tx_meta_t meta;
|
2018-06-24 09:46:57 +00:00
|
|
|
bool ch_inp_res = check_tx_inputs([&tx]()->cryptonote::transaction&{ return tx; }, id, max_used_block_height, max_used_block_id, tvc, kept_by_block);
|
2014-03-03 22:07:58 +00:00
|
|
|
if(!ch_inp_res)
|
|
|
|
{
|
2016-03-25 00:03:02 +00:00
|
|
|
// if the transaction was valid before (kept_by_block), then it
|
|
|
|
// may become valid again, so ignore the failed inputs check.
|
2014-03-03 22:07:58 +00:00
|
|
|
if(kept_by_block)
|
|
|
|
{
|
2018-07-18 21:24:53 +00:00
|
|
|
meta.weight = tx_weight;
|
2017-05-14 13:06:55 +00:00
|
|
|
meta.fee = fee;
|
|
|
|
meta.max_used_block_id = null_hash;
|
|
|
|
meta.max_used_block_height = 0;
|
|
|
|
meta.last_failed_height = 0;
|
|
|
|
meta.last_failed_id = null_hash;
|
|
|
|
meta.kept_by_block = kept_by_block;
|
|
|
|
meta.receive_time = receive_time;
|
|
|
|
meta.last_relayed_time = time(NULL);
|
|
|
|
meta.relayed = relayed;
|
|
|
|
meta.do_not_relay = do_not_relay;
|
2017-09-22 12:57:20 +00:00
|
|
|
meta.double_spend_seen = have_tx_keyimges_as_spent(tx);
|
2018-06-05 18:59:45 +00:00
|
|
|
meta.bf_padding = 0;
|
2017-07-02 17:43:23 +00:00
|
|
|
memset(meta.padding, 0, sizeof(meta.padding));
|
2017-05-14 13:06:55 +00:00
|
|
|
try
|
|
|
|
{
|
2018-10-31 13:52:16 +00:00
|
|
|
if (kept_by_block)
|
|
|
|
m_parsed_tx_cache.insert(std::make_pair(id, tx));
|
2017-05-31 21:50:12 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2017-05-14 13:06:55 +00:00
|
|
|
LockedTXN lock(m_blockchain);
|
2018-10-31 08:31:13 +00:00
|
|
|
m_blockchain.add_txpool_tx(id, blob, meta);
|
2018-09-16 18:30:39 +00:00
|
|
|
if (!insert_key_images(tx, id, kept_by_block))
|
2017-05-14 13:06:55 +00:00
|
|
|
return false;
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txs_by_fee_and_receive_time.emplace(std::pair<double, std::time_t>(fee / (double)tx_weight, receive_time), id);
|
2017-05-14 13:06:55 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MERROR("transaction already exists at inserting in memory pool: " << e.what());
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
tvc.m_verifivation_impossible = true;
|
|
|
|
tvc.m_added_to_pool = true;
|
|
|
|
}else
|
|
|
|
{
|
2014-09-09 09:32:00 +00:00
|
|
|
LOG_PRINT_L1("tx used wrong inputs, rejected");
|
2014-03-03 22:07:58 +00:00
|
|
|
tvc.m_verifivation_failed = true;
|
2017-09-16 10:27:26 +00:00
|
|
|
tvc.m_invalid_input = true;
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
//update transactions container
|
2018-07-18 21:24:53 +00:00
|
|
|
meta.weight = tx_weight;
|
2017-05-14 13:06:55 +00:00
|
|
|
meta.kept_by_block = kept_by_block;
|
|
|
|
meta.fee = fee;
|
|
|
|
meta.max_used_block_id = max_used_block_id;
|
|
|
|
meta.max_used_block_height = max_used_block_height;
|
|
|
|
meta.last_failed_height = 0;
|
|
|
|
meta.last_failed_id = null_hash;
|
|
|
|
meta.receive_time = receive_time;
|
|
|
|
meta.last_relayed_time = time(NULL);
|
|
|
|
meta.relayed = relayed;
|
|
|
|
meta.do_not_relay = do_not_relay;
|
2017-09-22 12:57:20 +00:00
|
|
|
meta.double_spend_seen = false;
|
2018-06-05 18:59:45 +00:00
|
|
|
meta.bf_padding = 0;
|
2017-07-02 17:43:23 +00:00
|
|
|
memset(meta.padding, 0, sizeof(meta.padding));
|
2017-05-14 13:06:55 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-10-31 13:52:16 +00:00
|
|
|
if (kept_by_block)
|
|
|
|
m_parsed_tx_cache.insert(std::make_pair(id, tx));
|
2017-05-31 21:50:12 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2017-05-14 13:06:55 +00:00
|
|
|
LockedTXN lock(m_blockchain);
|
2018-10-31 13:51:48 +00:00
|
|
|
m_blockchain.remove_txpool_tx(id);
|
|
|
|
m_blockchain.add_txpool_tx(id, blob, meta);
|
|
|
|
if (!insert_key_images(tx, id, kept_by_block))
|
2017-05-14 13:06:55 +00:00
|
|
|
return false;
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txs_by_fee_and_receive_time.emplace(std::pair<double, std::time_t>(fee / (double)tx_weight, receive_time), id);
|
2017-05-14 13:06:55 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2018-03-01 11:36:19 +00:00
|
|
|
MERROR("internal error: transaction already exists at inserting in memory pool: " << e.what());
|
2017-05-14 13:06:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
tvc.m_added_to_pool = true;
|
|
|
|
|
2017-05-14 13:06:55 +00:00
|
|
|
if(meta.fee > 0 && !do_not_relay)
|
2014-03-03 22:07:58 +00:00
|
|
|
tvc.m_should_be_relayed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
tvc.m_verifivation_failed = false;
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txpool_weight += tx_weight;
|
2015-04-30 05:02:12 +00:00
|
|
|
|
2016-04-17 10:04:01 +00:00
|
|
|
++m_cookie;
|
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
MINFO("Transaction added to pool: txid " << id << " weight: " << tx_weight << " fee/byte: " << (fee / (double)tx_weight));
|
2018-01-29 21:20:24 +00:00
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
prune(m_txpool_max_weight);
|
2018-01-29 21:20:24 +00:00
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2017-05-14 13:06:55 +00:00
|
|
|
bool tx_memory_pool::add_tx(transaction &tx, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay, uint8_t version)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
crypto::hash h = null_hash;
|
2014-09-25 06:24:42 +00:00
|
|
|
size_t blob_size = 0;
|
2018-10-31 08:31:13 +00:00
|
|
|
cryptonote::blobdata bl;
|
|
|
|
t_serializable_object_to_blob(tx, bl);
|
|
|
|
if (bl.size() == 0 || !get_transaction_hash(tx, h))
|
2017-09-10 11:26:24 +00:00
|
|
|
return false;
|
2018-10-31 08:31:13 +00:00
|
|
|
return add_tx(tx, h, bl, get_transaction_weight(tx, bl.size()), tvc, keeped_by_block, relayed, do_not_relay, version);
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-07-18 21:24:53 +00:00
|
|
|
size_t tx_memory_pool::get_txpool_weight() const
|
2018-01-29 21:20:24 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2018-07-18 21:24:53 +00:00
|
|
|
return m_txpool_weight;
|
2018-01-29 21:20:24 +00:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-07-18 21:24:53 +00:00
|
|
|
void tx_memory_pool::set_txpool_max_weight(size_t bytes)
|
2018-01-29 21:20:24 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txpool_max_weight = bytes;
|
2018-01-29 21:20:24 +00:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
void tx_memory_pool::prune(size_t bytes)
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
if (bytes == 0)
|
2018-07-18 21:24:53 +00:00
|
|
|
bytes = m_txpool_max_weight;
|
2018-01-29 21:20:24 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
|
|
|
LockedTXN lock(m_blockchain);
|
2016-04-17 10:04:01 +00:00
|
|
|
bool changed = false;
|
2018-01-29 21:20:24 +00:00
|
|
|
|
|
|
|
// this will never remove the first one, but we don't care
|
|
|
|
auto it = --m_txs_by_fee_and_receive_time.end();
|
|
|
|
while (it != m_txs_by_fee_and_receive_time.begin())
|
|
|
|
{
|
2018-07-18 21:24:53 +00:00
|
|
|
if (m_txpool_weight <= bytes)
|
2018-01-29 21:20:24 +00:00
|
|
|
break;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const crypto::hash &txid = it->second;
|
|
|
|
txpool_tx_meta_t meta;
|
|
|
|
if (!m_blockchain.get_txpool_tx_meta(txid, meta))
|
|
|
|
{
|
|
|
|
MERROR("Failed to find tx in txpool");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// don't prune the kept_by_block ones, they're likely added because we're adding a block with those
|
|
|
|
if (meta.kept_by_block)
|
|
|
|
{
|
|
|
|
--it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(txid);
|
2018-09-16 18:30:39 +00:00
|
|
|
cryptonote::transaction_prefix tx;
|
|
|
|
if (!parse_and_validate_tx_prefix_from_blob(txblob, tx))
|
2018-01-29 21:20:24 +00:00
|
|
|
{
|
|
|
|
MERROR("Failed to parse tx from txpool");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// remove first, in case this throws, so key images aren't removed
|
2018-10-22 03:02:44 +00:00
|
|
|
MINFO("Pruning tx " << txid << " from txpool: weight: " << meta.weight << ", fee/byte: " << it->first.first);
|
2018-01-29 21:20:24 +00:00
|
|
|
m_blockchain.remove_txpool_tx(txid);
|
2018-10-22 03:02:44 +00:00
|
|
|
m_txpool_weight -= meta.weight;
|
2018-09-16 18:30:39 +00:00
|
|
|
remove_transaction_keyimages(tx, txid);
|
2018-10-22 03:02:44 +00:00
|
|
|
MINFO("Pruned tx " << txid << " from txpool: weight: " << meta.weight << ", fee/byte: " << it->first.first);
|
2018-01-29 21:20:24 +00:00
|
|
|
m_txs_by_fee_and_receive_time.erase(it--);
|
2016-04-17 10:04:01 +00:00
|
|
|
changed = true;
|
2018-01-29 21:20:24 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MERROR("Error while pruning txpool: " << e.what());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 10:04:01 +00:00
|
|
|
if (changed)
|
|
|
|
++m_cookie;
|
2018-07-18 21:24:53 +00:00
|
|
|
if (m_txpool_weight > bytes)
|
|
|
|
MINFO("Pool weight after pruning is larger than limit: " << m_txpool_weight << "/" << bytes);
|
2018-01-29 21:20:24 +00:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-09-16 18:30:39 +00:00
|
|
|
bool tx_memory_pool::insert_key_images(const transaction_prefix &tx, const crypto::hash &id, bool kept_by_block)
|
2017-05-14 13:06:55 +00:00
|
|
|
{
|
|
|
|
for(const auto& in: tx.vin)
|
|
|
|
{
|
|
|
|
CHECKED_GET_SPECIFIC_VARIANT(in, const txin_to_key, txin, false);
|
|
|
|
std::unordered_set<crypto::hash>& kei_image_set = m_spent_key_images[txin.k_image];
|
|
|
|
CHECK_AND_ASSERT_MES(kept_by_block || kei_image_set.size() == 0, false, "internal error: kept_by_block=" << kept_by_block
|
|
|
|
<< ", kei_image_set.size()=" << kei_image_set.size() << ENDL << "txin.k_image=" << txin.k_image << ENDL
|
|
|
|
<< "tx_id=" << id );
|
|
|
|
auto ins_res = kei_image_set.insert(id);
|
|
|
|
CHECK_AND_ASSERT_MES(ins_res.second, false, "internal error: try to insert duplicate iterator in key_image set");
|
|
|
|
}
|
2016-04-17 10:04:01 +00:00
|
|
|
++m_cookie;
|
2017-05-14 13:06:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2016-03-25 00:03:02 +00:00
|
|
|
//FIXME: Can return early before removal of all of the key images.
|
|
|
|
// At the least, need to make sure that a false return here
|
|
|
|
// is treated properly. Should probably not return early, however.
|
2018-09-16 18:30:39 +00:00
|
|
|
bool tx_memory_pool::remove_transaction_keyimages(const transaction_prefix& tx, const crypto::hash &actual_hash)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2015-12-14 04:54:39 +00:00
|
|
|
// ND: Speedup
|
2017-01-22 20:38:10 +00:00
|
|
|
for(const txin_v& vi: tx.vin)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CHECKED_GET_SPECIFIC_VARIANT(vi, const txin_to_key, txin, false);
|
|
|
|
auto it = m_spent_key_images.find(txin.k_image);
|
|
|
|
CHECK_AND_ASSERT_MES(it != m_spent_key_images.end(), false, "failed to find transaction input in key images. img=" << txin.k_image << ENDL
|
2018-09-16 18:30:39 +00:00
|
|
|
<< "transaction id = " << actual_hash);
|
2014-03-03 22:07:58 +00:00
|
|
|
std::unordered_set<crypto::hash>& key_image_set = it->second;
|
|
|
|
CHECK_AND_ASSERT_MES(key_image_set.size(), false, "empty key_image set, img=" << txin.k_image << ENDL
|
** CHANGES ARE EXPERIMENTAL (FOR TESTING ONLY)
Bockchain:
1. Optim: Multi-thread long-hash computation when encountering groups of blocks.
2. Optim: Cache verified txs and return result from cache instead of re-checking whenever possible.
3. Optim: Preload output-keys when encoutering groups of blocks. Sort by amount and global-index before bulk querying database and multi-thread when possible.
4. Optim: Disable double spend check on block verification, double spend is already detected when trying to add blocks.
5. Optim: Multi-thread signature computation whenever possible.
6. Patch: Disable locking (recursive mutex) on called functions from check_tx_inputs which causes slowdowns (only seems to happen on ubuntu/VMs??? Reason: TBD)
7. Optim: Removed looped full-tx hash computation when retrieving transactions from pool (???).
8. Optim: Cache difficulty/timestamps (735 blocks) for next-difficulty calculations so that only 2 db reads per new block is needed when a new block arrives (instead of 1470 reads).
Berkeley-DB:
1. Fix: 32-bit data errors causing wrong output global indices and failure to send blocks to peers (etc).
2. Fix: Unable to pop blocks on reorganize due to transaction errors.
3. Patch: Large number of transaction aborts when running multi-threaded bulk queries.
4. Patch: Insufficient locks error when running full sync.
5. Patch: Incorrect db stats when returning from an immediate exit from "pop block" operation.
6. Optim: Add bulk queries to get output global indices.
7. Optim: Modified output_keys table to store public_key+unlock_time+height for single transaction lookup (vs 3)
8. Optim: Used output_keys table retrieve public_keys instead of going through output_amounts->output_txs+output_indices->txs->output:public_key
9. Optim: Added thread-safe buffers used when multi-threading bulk queries.
10. Optim: Added support for nosync/write_nosync options for improved performance (*see --db-sync-mode option for details)
11. Mod: Added checkpoint thread and auto-remove-logs option.
12. *Now usable on 32-bit systems like RPI2.
LMDB:
1. Optim: Added custom comparison for 256-bit key tables (minor speed-up, TBD: get actual effect)
2. Optim: Modified output_keys table to store public_key+unlock_time+height for single transaction lookup (vs 3)
3. Optim: Used output_keys table retrieve public_keys instead of going through output_amounts->output_txs+output_indices->txs->output:public_key
4. Optim: Added support for sync/writemap options for improved performance (*see --db-sync-mode option for details)
5. Mod: Auto resize to +1GB instead of multiplier x1.5
ETC:
1. Minor optimizations for slow-hash for ARM (RPI2). Incomplete.
2. Fix: 32-bit saturation bug when computing next difficulty on large blocks.
[PENDING ISSUES]
1. Berkely db has a very slow "pop-block" operation. This is very noticeable on the RPI2 as it sometimes takes > 10 MINUTES to pop a block during reorganization.
This does not happen very often however, most reorgs seem to take a few seconds but it possibly depends on the number of outputs present. TBD.
2. Berkeley db, possible bug "unable to allocate memory". TBD.
[NEW OPTIONS] (*Currently all enabled for testing purposes)
1. --fast-block-sync arg=[0:1] (default: 1)
a. 0 = Compute long hash per block (may take a while depending on CPU)
b. 1 = Skip long-hash and verify blocks based on embedded known good block hashes (faster, minimal CPU dependence)
2. --db-sync-mode arg=[[safe|fast|fastest]:[sync|async]:[nblocks_per_sync]] (default: fastest:async:1000)
a. safe = fdatasync/fsync (or equivalent) per stored block. Very slow, but safest option to protect against power-out/crash conditions.
b. fast/fastest = Enables asynchronous fdatasync/fsync (or equivalent). Useful for battery operated devices or STABLE systems with UPS and/or systems with battery backed write cache/solid state cache.
Fast - Write meta-data but defer data flush.
Fastest - Defer meta-data and data flush.
Sync - Flush data after nblocks_per_sync and wait.
Async - Flush data after nblocks_per_sync but do not wait for the operation to finish.
3. --prep-blocks-threads arg=[n] (default: 4 or system max threads, whichever is lower)
Max number of threads to use when computing long-hash in groups.
4. --show-time-stats arg=[0:1] (default: 1)
Show benchmark related time stats.
5. --db-auto-remove-logs arg=[0:1] (default: 1)
For berkeley-db only. Auto remove logs if enabled.
**Note: lmdb and berkeley-db have changes to the tables and are not compatible with official git head version.
At the moment, you need a full resync to use this optimized version.
[PERFORMANCE COMPARISON]
**Some figures are approximations only.
Using a baseline machine of an i7-2600K+SSD+(with full pow computation):
1. The optimized lmdb/blockhain core can process blocks up to 585K for ~1.25 hours + download time, so it usually takes 2.5 hours to sync the full chain.
2. The current head with memory can process blocks up to 585K for ~4.2 hours + download time, so it usually takes 5.5 hours to sync the full chain.
3. The current head with lmdb can process blocks up to 585K for ~32 hours + download time and usually takes 36 hours to sync the full chain.
Averate procesing times (with full pow computation):
lmdb-optimized:
1. tx_ave = 2.5 ms / tx
2. block_ave = 5.87 ms / block
memory-official-repo:
1. tx_ave = 8.85 ms / tx
2. block_ave = 19.68 ms / block
lmdb-official-repo (0f4a036437fd41a5498ee5e74e2422ea6177aa3e)
1. tx_ave = 47.8 ms / tx
2. block_ave = 64.2 ms / block
**Note: The following data denotes processing times only (does not include p2p download time)
lmdb-optimized processing times (with full pow computation):
1. Desktop, Quad-core / 8-threads 2600k (8Mb) - 1.25 hours processing time (--db-sync-mode=fastest:async:1000).
2. Laptop, Dual-core / 4-threads U4200 (3Mb) - 4.90 hours processing time (--db-sync-mode=fastest:async:1000).
3. Embedded, Quad-core / 4-threads Z3735F (2x1Mb) - 12.0 hours processing time (--db-sync-mode=fastest:async:1000).
lmdb-optimized processing times (with per-block-checkpoint)
1. Desktop, Quad-core / 8-threads 2600k (8Mb) - 10 minutes processing time (--db-sync-mode=fastest:async:1000).
berkeley-db optimized processing times (with full pow computation)
1. Desktop, Quad-core / 8-threads 2600k (8Mb) - 1.8 hours processing time (--db-sync-mode=fastest:async:1000).
2. RPI2. Improved from estimated 3 months(???) into 2.5 days (*Need 2AMP supply + Clock:1Ghz + [usb+ssd] to achieve this speed) (--db-sync-mode=fastest:async:1000).
berkeley-db optimized processing times (with per-block-checkpoint)
1. RPI2. 12-15 hours (*Need 2AMP supply + Clock:1Ghz + [usb+ssd] to achieve this speed) (--db-sync-mode=fastest:async:1000).
2015-07-10 20:09:32 +00:00
|
|
|
<< "transaction id = " << actual_hash);
|
2014-03-03 22:07:58 +00:00
|
|
|
|
** CHANGES ARE EXPERIMENTAL (FOR TESTING ONLY)
Bockchain:
1. Optim: Multi-thread long-hash computation when encountering groups of blocks.
2. Optim: Cache verified txs and return result from cache instead of re-checking whenever possible.
3. Optim: Preload output-keys when encoutering groups of blocks. Sort by amount and global-index before bulk querying database and multi-thread when possible.
4. Optim: Disable double spend check on block verification, double spend is already detected when trying to add blocks.
5. Optim: Multi-thread signature computation whenever possible.
6. Patch: Disable locking (recursive mutex) on called functions from check_tx_inputs which causes slowdowns (only seems to happen on ubuntu/VMs??? Reason: TBD)
7. Optim: Removed looped full-tx hash computation when retrieving transactions from pool (???).
8. Optim: Cache difficulty/timestamps (735 blocks) for next-difficulty calculations so that only 2 db reads per new block is needed when a new block arrives (instead of 1470 reads).
Berkeley-DB:
1. Fix: 32-bit data errors causing wrong output global indices and failure to send blocks to peers (etc).
2. Fix: Unable to pop blocks on reorganize due to transaction errors.
3. Patch: Large number of transaction aborts when running multi-threaded bulk queries.
4. Patch: Insufficient locks error when running full sync.
5. Patch: Incorrect db stats when returning from an immediate exit from "pop block" operation.
6. Optim: Add bulk queries to get output global indices.
7. Optim: Modified output_keys table to store public_key+unlock_time+height for single transaction lookup (vs 3)
8. Optim: Used output_keys table retrieve public_keys instead of going through output_amounts->output_txs+output_indices->txs->output:public_key
9. Optim: Added thread-safe buffers used when multi-threading bulk queries.
10. Optim: Added support for nosync/write_nosync options for improved performance (*see --db-sync-mode option for details)
11. Mod: Added checkpoint thread and auto-remove-logs option.
12. *Now usable on 32-bit systems like RPI2.
LMDB:
1. Optim: Added custom comparison for 256-bit key tables (minor speed-up, TBD: get actual effect)
2. Optim: Modified output_keys table to store public_key+unlock_time+height for single transaction lookup (vs 3)
3. Optim: Used output_keys table retrieve public_keys instead of going through output_amounts->output_txs+output_indices->txs->output:public_key
4. Optim: Added support for sync/writemap options for improved performance (*see --db-sync-mode option for details)
5. Mod: Auto resize to +1GB instead of multiplier x1.5
ETC:
1. Minor optimizations for slow-hash for ARM (RPI2). Incomplete.
2. Fix: 32-bit saturation bug when computing next difficulty on large blocks.
[PENDING ISSUES]
1. Berkely db has a very slow "pop-block" operation. This is very noticeable on the RPI2 as it sometimes takes > 10 MINUTES to pop a block during reorganization.
This does not happen very often however, most reorgs seem to take a few seconds but it possibly depends on the number of outputs present. TBD.
2. Berkeley db, possible bug "unable to allocate memory". TBD.
[NEW OPTIONS] (*Currently all enabled for testing purposes)
1. --fast-block-sync arg=[0:1] (default: 1)
a. 0 = Compute long hash per block (may take a while depending on CPU)
b. 1 = Skip long-hash and verify blocks based on embedded known good block hashes (faster, minimal CPU dependence)
2. --db-sync-mode arg=[[safe|fast|fastest]:[sync|async]:[nblocks_per_sync]] (default: fastest:async:1000)
a. safe = fdatasync/fsync (or equivalent) per stored block. Very slow, but safest option to protect against power-out/crash conditions.
b. fast/fastest = Enables asynchronous fdatasync/fsync (or equivalent). Useful for battery operated devices or STABLE systems with UPS and/or systems with battery backed write cache/solid state cache.
Fast - Write meta-data but defer data flush.
Fastest - Defer meta-data and data flush.
Sync - Flush data after nblocks_per_sync and wait.
Async - Flush data after nblocks_per_sync but do not wait for the operation to finish.
3. --prep-blocks-threads arg=[n] (default: 4 or system max threads, whichever is lower)
Max number of threads to use when computing long-hash in groups.
4. --show-time-stats arg=[0:1] (default: 1)
Show benchmark related time stats.
5. --db-auto-remove-logs arg=[0:1] (default: 1)
For berkeley-db only. Auto remove logs if enabled.
**Note: lmdb and berkeley-db have changes to the tables and are not compatible with official git head version.
At the moment, you need a full resync to use this optimized version.
[PERFORMANCE COMPARISON]
**Some figures are approximations only.
Using a baseline machine of an i7-2600K+SSD+(with full pow computation):
1. The optimized lmdb/blockhain core can process blocks up to 585K for ~1.25 hours + download time, so it usually takes 2.5 hours to sync the full chain.
2. The current head with memory can process blocks up to 585K for ~4.2 hours + download time, so it usually takes 5.5 hours to sync the full chain.
3. The current head with lmdb can process blocks up to 585K for ~32 hours + download time and usually takes 36 hours to sync the full chain.
Averate procesing times (with full pow computation):
lmdb-optimized:
1. tx_ave = 2.5 ms / tx
2. block_ave = 5.87 ms / block
memory-official-repo:
1. tx_ave = 8.85 ms / tx
2. block_ave = 19.68 ms / block
lmdb-official-repo (0f4a036437fd41a5498ee5e74e2422ea6177aa3e)
1. tx_ave = 47.8 ms / tx
2. block_ave = 64.2 ms / block
**Note: The following data denotes processing times only (does not include p2p download time)
lmdb-optimized processing times (with full pow computation):
1. Desktop, Quad-core / 8-threads 2600k (8Mb) - 1.25 hours processing time (--db-sync-mode=fastest:async:1000).
2. Laptop, Dual-core / 4-threads U4200 (3Mb) - 4.90 hours processing time (--db-sync-mode=fastest:async:1000).
3. Embedded, Quad-core / 4-threads Z3735F (2x1Mb) - 12.0 hours processing time (--db-sync-mode=fastest:async:1000).
lmdb-optimized processing times (with per-block-checkpoint)
1. Desktop, Quad-core / 8-threads 2600k (8Mb) - 10 minutes processing time (--db-sync-mode=fastest:async:1000).
berkeley-db optimized processing times (with full pow computation)
1. Desktop, Quad-core / 8-threads 2600k (8Mb) - 1.8 hours processing time (--db-sync-mode=fastest:async:1000).
2. RPI2. Improved from estimated 3 months(???) into 2.5 days (*Need 2AMP supply + Clock:1Ghz + [usb+ssd] to achieve this speed) (--db-sync-mode=fastest:async:1000).
berkeley-db optimized processing times (with per-block-checkpoint)
1. RPI2. 12-15 hours (*Need 2AMP supply + Clock:1Ghz + [usb+ssd] to achieve this speed) (--db-sync-mode=fastest:async:1000).
2015-07-10 20:09:32 +00:00
|
|
|
auto it_in_set = key_image_set.find(actual_hash);
|
2014-06-24 17:30:41 +00:00
|
|
|
CHECK_AND_ASSERT_MES(it_in_set != key_image_set.end(), false, "transaction id not found in key_image set, img=" << txin.k_image << ENDL
|
** CHANGES ARE EXPERIMENTAL (FOR TESTING ONLY)
Bockchain:
1. Optim: Multi-thread long-hash computation when encountering groups of blocks.
2. Optim: Cache verified txs and return result from cache instead of re-checking whenever possible.
3. Optim: Preload output-keys when encoutering groups of blocks. Sort by amount and global-index before bulk querying database and multi-thread when possible.
4. Optim: Disable double spend check on block verification, double spend is already detected when trying to add blocks.
5. Optim: Multi-thread signature computation whenever possible.
6. Patch: Disable locking (recursive mutex) on called functions from check_tx_inputs which causes slowdowns (only seems to happen on ubuntu/VMs??? Reason: TBD)
7. Optim: Removed looped full-tx hash computation when retrieving transactions from pool (???).
8. Optim: Cache difficulty/timestamps (735 blocks) for next-difficulty calculations so that only 2 db reads per new block is needed when a new block arrives (instead of 1470 reads).
Berkeley-DB:
1. Fix: 32-bit data errors causing wrong output global indices and failure to send blocks to peers (etc).
2. Fix: Unable to pop blocks on reorganize due to transaction errors.
3. Patch: Large number of transaction aborts when running multi-threaded bulk queries.
4. Patch: Insufficient locks error when running full sync.
5. Patch: Incorrect db stats when returning from an immediate exit from "pop block" operation.
6. Optim: Add bulk queries to get output global indices.
7. Optim: Modified output_keys table to store public_key+unlock_time+height for single transaction lookup (vs 3)
8. Optim: Used output_keys table retrieve public_keys instead of going through output_amounts->output_txs+output_indices->txs->output:public_key
9. Optim: Added thread-safe buffers used when multi-threading bulk queries.
10. Optim: Added support for nosync/write_nosync options for improved performance (*see --db-sync-mode option for details)
11. Mod: Added checkpoint thread and auto-remove-logs option.
12. *Now usable on 32-bit systems like RPI2.
LMDB:
1. Optim: Added custom comparison for 256-bit key tables (minor speed-up, TBD: get actual effect)
2. Optim: Modified output_keys table to store public_key+unlock_time+height for single transaction lookup (vs 3)
3. Optim: Used output_keys table retrieve public_keys instead of going through output_amounts->output_txs+output_indices->txs->output:public_key
4. Optim: Added support for sync/writemap options for improved performance (*see --db-sync-mode option for details)
5. Mod: Auto resize to +1GB instead of multiplier x1.5
ETC:
1. Minor optimizations for slow-hash for ARM (RPI2). Incomplete.
2. Fix: 32-bit saturation bug when computing next difficulty on large blocks.
[PENDING ISSUES]
1. Berkely db has a very slow "pop-block" operation. This is very noticeable on the RPI2 as it sometimes takes > 10 MINUTES to pop a block during reorganization.
This does not happen very often however, most reorgs seem to take a few seconds but it possibly depends on the number of outputs present. TBD.
2. Berkeley db, possible bug "unable to allocate memory". TBD.
[NEW OPTIONS] (*Currently all enabled for testing purposes)
1. --fast-block-sync arg=[0:1] (default: 1)
a. 0 = Compute long hash per block (may take a while depending on CPU)
b. 1 = Skip long-hash and verify blocks based on embedded known good block hashes (faster, minimal CPU dependence)
2. --db-sync-mode arg=[[safe|fast|fastest]:[sync|async]:[nblocks_per_sync]] (default: fastest:async:1000)
a. safe = fdatasync/fsync (or equivalent) per stored block. Very slow, but safest option to protect against power-out/crash conditions.
b. fast/fastest = Enables asynchronous fdatasync/fsync (or equivalent). Useful for battery operated devices or STABLE systems with UPS and/or systems with battery backed write cache/solid state cache.
Fast - Write meta-data but defer data flush.
Fastest - Defer meta-data and data flush.
Sync - Flush data after nblocks_per_sync and wait.
Async - Flush data after nblocks_per_sync but do not wait for the operation to finish.
3. --prep-blocks-threads arg=[n] (default: 4 or system max threads, whichever is lower)
Max number of threads to use when computing long-hash in groups.
4. --show-time-stats arg=[0:1] (default: 1)
Show benchmark related time stats.
5. --db-auto-remove-logs arg=[0:1] (default: 1)
For berkeley-db only. Auto remove logs if enabled.
**Note: lmdb and berkeley-db have changes to the tables and are not compatible with official git head version.
At the moment, you need a full resync to use this optimized version.
[PERFORMANCE COMPARISON]
**Some figures are approximations only.
Using a baseline machine of an i7-2600K+SSD+(with full pow computation):
1. The optimized lmdb/blockhain core can process blocks up to 585K for ~1.25 hours + download time, so it usually takes 2.5 hours to sync the full chain.
2. The current head with memory can process blocks up to 585K for ~4.2 hours + download time, so it usually takes 5.5 hours to sync the full chain.
3. The current head with lmdb can process blocks up to 585K for ~32 hours + download time and usually takes 36 hours to sync the full chain.
Averate procesing times (with full pow computation):
lmdb-optimized:
1. tx_ave = 2.5 ms / tx
2. block_ave = 5.87 ms / block
memory-official-repo:
1. tx_ave = 8.85 ms / tx
2. block_ave = 19.68 ms / block
lmdb-official-repo (0f4a036437fd41a5498ee5e74e2422ea6177aa3e)
1. tx_ave = 47.8 ms / tx
2. block_ave = 64.2 ms / block
**Note: The following data denotes processing times only (does not include p2p download time)
lmdb-optimized processing times (with full pow computation):
1. Desktop, Quad-core / 8-threads 2600k (8Mb) - 1.25 hours processing time (--db-sync-mode=fastest:async:1000).
2. Laptop, Dual-core / 4-threads U4200 (3Mb) - 4.90 hours processing time (--db-sync-mode=fastest:async:1000).
3. Embedded, Quad-core / 4-threads Z3735F (2x1Mb) - 12.0 hours processing time (--db-sync-mode=fastest:async:1000).
lmdb-optimized processing times (with per-block-checkpoint)
1. Desktop, Quad-core / 8-threads 2600k (8Mb) - 10 minutes processing time (--db-sync-mode=fastest:async:1000).
berkeley-db optimized processing times (with full pow computation)
1. Desktop, Quad-core / 8-threads 2600k (8Mb) - 1.8 hours processing time (--db-sync-mode=fastest:async:1000).
2. RPI2. Improved from estimated 3 months(???) into 2.5 days (*Need 2AMP supply + Clock:1Ghz + [usb+ssd] to achieve this speed) (--db-sync-mode=fastest:async:1000).
berkeley-db optimized processing times (with per-block-checkpoint)
1. RPI2. 12-15 hours (*Need 2AMP supply + Clock:1Ghz + [usb+ssd] to achieve this speed) (--db-sync-mode=fastest:async:1000).
2015-07-10 20:09:32 +00:00
|
|
|
<< "transaction id = " << actual_hash);
|
2014-03-03 22:07:58 +00:00
|
|
|
key_image_set.erase(it_in_set);
|
|
|
|
if(!key_image_set.size())
|
|
|
|
{
|
|
|
|
//it is now empty hash container for this key_image
|
|
|
|
m_spent_key_images.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-04-17 10:04:01 +00:00
|
|
|
++m_cookie;
|
2014-03-03 22:07:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-07-18 21:24:53 +00:00
|
|
|
bool tx_memory_pool::take_tx(const crypto::hash &id, transaction &tx, size_t& tx_weight, uint64_t& fee, bool &relayed, bool &do_not_relay, bool &double_spend_seen)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2015-05-14 00:27:06 +00:00
|
|
|
auto sorted_it = find_tx_in_sorted_container(id);
|
2015-04-30 05:02:12 +00:00
|
|
|
|
2017-05-14 13:06:55 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
LockedTXN lock(m_blockchain);
|
2017-12-14 17:09:30 +00:00
|
|
|
txpool_tx_meta_t meta;
|
|
|
|
if (!m_blockchain.get_txpool_tx_meta(id, meta))
|
|
|
|
{
|
|
|
|
MERROR("Failed to find tx in txpool");
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-14 13:06:55 +00:00
|
|
|
cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(id);
|
2018-10-31 13:52:16 +00:00
|
|
|
auto ci = m_parsed_tx_cache.find(id);
|
|
|
|
if (ci != m_parsed_tx_cache.end())
|
|
|
|
{
|
|
|
|
tx = ci->second;
|
|
|
|
}
|
|
|
|
else if (!parse_and_validate_tx_from_blob(txblob, tx))
|
2017-05-14 13:06:55 +00:00
|
|
|
{
|
|
|
|
MERROR("Failed to parse tx from txpool");
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-05 20:34:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tx.set_hash(id);
|
|
|
|
}
|
2018-07-18 21:24:53 +00:00
|
|
|
tx_weight = meta.weight;
|
2017-05-14 13:06:55 +00:00
|
|
|
fee = meta.fee;
|
|
|
|
relayed = meta.relayed;
|
|
|
|
do_not_relay = meta.do_not_relay;
|
2017-09-22 12:57:20 +00:00
|
|
|
double_spend_seen = meta.double_spend_seen;
|
2017-05-14 13:06:55 +00:00
|
|
|
|
|
|
|
// remove first, in case this throws, so key images aren't removed
|
|
|
|
m_blockchain.remove_txpool_tx(id);
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txpool_weight -= tx_weight;
|
2018-09-16 18:30:39 +00:00
|
|
|
remove_transaction_keyimages(tx, id);
|
2017-05-14 13:06:55 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MERROR("Failed to remove tx from txpool: " << e.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-31 14:43:13 +00:00
|
|
|
if (sorted_it != m_txs_by_fee_and_receive_time.end())
|
|
|
|
m_txs_by_fee_and_receive_time.erase(sorted_it);
|
2016-04-17 10:04:01 +00:00
|
|
|
++m_cookie;
|
2014-03-03 22:07:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2014-06-15 07:48:13 +00:00
|
|
|
void tx_memory_pool::on_idle()
|
|
|
|
{
|
|
|
|
m_remove_stuck_tx_interval.do_call([this](){return remove_stuck_transactions();});
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2015-05-14 00:27:06 +00:00
|
|
|
sorted_tx_container::iterator tx_memory_pool::find_tx_in_sorted_container(const crypto::hash& id) const
|
|
|
|
{
|
2017-01-13 15:08:37 +00:00
|
|
|
return std::find_if( m_txs_by_fee_and_receive_time.begin(), m_txs_by_fee_and_receive_time.end()
|
2015-05-14 00:27:06 +00:00
|
|
|
, [&](const sorted_tx_container::value_type& a){
|
|
|
|
return a.second == id;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2016-03-25 00:03:02 +00:00
|
|
|
//TODO: investigate whether boolean return is appropriate
|
2014-06-15 07:48:13 +00:00
|
|
|
bool tx_memory_pool::remove_stuck_transactions()
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2018-09-16 18:30:39 +00:00
|
|
|
std::list<std::pair<crypto::hash, uint64_t>> remove;
|
2017-05-14 13:06:55 +00:00
|
|
|
m_blockchain.for_all_txpool_txes([this, &remove](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata*) {
|
|
|
|
uint64_t tx_age = time(nullptr) - meta.receive_time;
|
2014-06-15 07:48:13 +00:00
|
|
|
|
2017-05-14 13:06:55 +00:00
|
|
|
if((tx_age > CRYPTONOTE_MEMPOOL_TX_LIVETIME && !meta.kept_by_block) ||
|
|
|
|
(tx_age > CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME && meta.kept_by_block) )
|
2014-06-15 07:48:13 +00:00
|
|
|
{
|
2017-05-14 13:06:55 +00:00
|
|
|
LOG_PRINT_L1("Tx " << txid << " removed from tx pool due to outdated, age: " << tx_age );
|
|
|
|
auto sorted_it = find_tx_in_sorted_container(txid);
|
2017-01-13 15:08:37 +00:00
|
|
|
if (sorted_it == m_txs_by_fee_and_receive_time.end())
|
2015-05-14 00:27:06 +00:00
|
|
|
{
|
2017-05-14 13:06:55 +00:00
|
|
|
LOG_PRINT_L1("Removing tx " << txid << " from tx pool, but it was not found in the sorted txs container!");
|
2015-05-14 00:27:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-13 15:08:37 +00:00
|
|
|
m_txs_by_fee_and_receive_time.erase(sorted_it);
|
2015-05-14 00:27:06 +00:00
|
|
|
}
|
2017-05-14 13:06:55 +00:00
|
|
|
m_timed_out_transactions.insert(txid);
|
2018-09-16 18:30:39 +00:00
|
|
|
remove.push_back(std::make_pair(txid, meta.weight));
|
2017-05-14 13:06:55 +00:00
|
|
|
}
|
|
|
|
return true;
|
2017-11-08 12:06:41 +00:00
|
|
|
}, false);
|
2017-05-14 13:06:55 +00:00
|
|
|
|
|
|
|
if (!remove.empty())
|
|
|
|
{
|
|
|
|
LockedTXN lock(m_blockchain);
|
2018-09-16 18:30:39 +00:00
|
|
|
for (const std::pair<crypto::hash, uint64_t> &entry: remove)
|
2017-05-14 13:06:55 +00:00
|
|
|
{
|
2018-09-16 18:30:39 +00:00
|
|
|
const crypto::hash &txid = entry.first;
|
2017-05-14 13:06:55 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
cryptonote::blobdata bd = m_blockchain.get_txpool_tx_blob(txid);
|
2018-09-16 18:30:39 +00:00
|
|
|
cryptonote::transaction_prefix tx;
|
|
|
|
if (!parse_and_validate_tx_prefix_from_blob(bd, tx))
|
2017-05-14 13:06:55 +00:00
|
|
|
{
|
|
|
|
MERROR("Failed to parse tx from txpool");
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// remove first, so we only remove key images if the tx removal succeeds
|
|
|
|
m_blockchain.remove_txpool_tx(txid);
|
2018-09-16 18:30:39 +00:00
|
|
|
m_txpool_weight -= entry.second;
|
|
|
|
remove_transaction_keyimages(tx, txid);
|
2017-05-14 13:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MWARNING("Failed to remove stuck transaction: " << txid);
|
|
|
|
// ignore error
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 10:04:01 +00:00
|
|
|
++m_cookie;
|
2014-06-15 07:48:13 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2016-03-25 00:03:02 +00:00
|
|
|
//TODO: investigate whether boolean return is appropriate
|
2018-04-15 23:16:02 +00:00
|
|
|
bool tx_memory_pool::get_relayable_transactions(std::vector<std::pair<crypto::hash, cryptonote::blobdata>> &txs) const
|
2015-11-21 00:26:48 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
|
|
|
const uint64_t now = time(NULL);
|
2018-04-15 23:16:02 +00:00
|
|
|
txs.reserve(m_blockchain.get_txpool_tx_count());
|
2017-05-14 13:06:55 +00:00
|
|
|
m_blockchain.for_all_txpool_txes([this, now, &txs](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *){
|
2015-11-21 00:26:48 +00:00
|
|
|
// 0 fee transactions are never relayed
|
2017-05-14 13:06:55 +00:00
|
|
|
if(meta.fee > 0 && !meta.do_not_relay && now - meta.last_relayed_time > get_relay_delay(now, meta.receive_time))
|
2015-11-21 00:26:48 +00:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
// flushed txes to be re-added when received from a node which was just about to flush it
|
2017-05-14 13:06:55 +00:00
|
|
|
uint64_t max_age = meta.kept_by_block ? CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME : CRYPTONOTE_MEMPOOL_TX_LIVETIME;
|
|
|
|
if (now - meta.receive_time <= max_age / 2)
|
2015-11-21 00:26:48 +00:00
|
|
|
{
|
2017-05-14 13:06:55 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
cryptonote::blobdata bd = m_blockchain.get_txpool_tx_blob(txid);
|
|
|
|
txs.push_back(std::make_pair(txid, bd));
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MERROR("Failed to get transaction blob from db");
|
|
|
|
// ignore error
|
|
|
|
}
|
2015-11-21 00:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-14 13:06:55 +00:00
|
|
|
return true;
|
2017-11-08 12:06:41 +00:00
|
|
|
}, false);
|
2015-11-21 00:26:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-04-15 23:16:02 +00:00
|
|
|
void tx_memory_pool::set_relayed(const std::vector<std::pair<crypto::hash, cryptonote::blobdata>> &txs)
|
2015-11-21 00:26:48 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2015-11-21 00:26:48 +00:00
|
|
|
const time_t now = time(NULL);
|
2017-05-14 13:06:55 +00:00
|
|
|
LockedTXN lock(m_blockchain);
|
2015-11-21 00:26:48 +00:00
|
|
|
for (auto it = txs.begin(); it != txs.end(); ++it)
|
|
|
|
{
|
2017-05-14 13:06:55 +00:00
|
|
|
try
|
2016-10-22 19:46:19 +00:00
|
|
|
{
|
2017-12-14 17:09:30 +00:00
|
|
|
txpool_tx_meta_t meta;
|
|
|
|
if (m_blockchain.get_txpool_tx_meta(it->first, meta))
|
|
|
|
{
|
|
|
|
meta.relayed = true;
|
|
|
|
meta.last_relayed_time = now;
|
|
|
|
m_blockchain.update_txpool_tx(it->first, meta);
|
|
|
|
}
|
2017-05-14 13:06:55 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MERROR("Failed to update txpool transaction metadata: " << e.what());
|
|
|
|
// continue
|
2016-10-22 19:46:19 +00:00
|
|
|
}
|
2015-11-21 00:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2017-11-08 12:06:41 +00:00
|
|
|
size_t tx_memory_pool::get_transactions_count(bool include_unrelayed_txes) const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2017-11-08 12:06:41 +00:00
|
|
|
return m_blockchain.get_txpool_tx_count(include_unrelayed_txes);
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-04-15 23:16:02 +00:00
|
|
|
void tx_memory_pool::get_transactions(std::vector<transaction>& txs, bool include_unrelayed_txes) const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2018-04-15 23:16:02 +00:00
|
|
|
txs.reserve(m_blockchain.get_txpool_tx_count(include_unrelayed_txes));
|
2017-05-14 13:06:55 +00:00
|
|
|
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))
|
|
|
|
{
|
|
|
|
MERROR("Failed to parse tx from txpool");
|
|
|
|
// continue
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-05 20:34:35 +00:00
|
|
|
tx.set_hash(txid);
|
|
|
|
txs.push_back(std::move(tx));
|
2017-05-14 13:06:55 +00:00
|
|
|
return true;
|
2017-11-08 12:06:41 +00:00
|
|
|
}, true, include_unrelayed_txes);
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
2015-04-23 12:13:07 +00:00
|
|
|
//------------------------------------------------------------------
|
2017-11-08 12:06:41 +00:00
|
|
|
void tx_memory_pool::get_transaction_hashes(std::vector<crypto::hash>& txs, bool include_unrelayed_txes) const
|
2017-03-22 18:03:23 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2018-04-15 23:16:02 +00:00
|
|
|
txs.reserve(m_blockchain.get_txpool_tx_count(include_unrelayed_txes));
|
2017-05-14 13:06:55 +00:00
|
|
|
m_blockchain.for_all_txpool_txes([&txs](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
|
|
|
|
txs.push_back(txid);
|
|
|
|
return true;
|
2017-11-08 12:06:41 +00:00
|
|
|
}, false, include_unrelayed_txes);
|
2017-03-22 18:03:23 +00:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------
|
2017-11-08 12:06:41 +00:00
|
|
|
void tx_memory_pool::get_transaction_backlog(std::vector<tx_backlog_entry>& backlog, bool include_unrelayed_txes) const
|
2017-08-26 15:23:31 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
|
|
|
const uint64_t now = time(NULL);
|
2018-04-15 23:16:02 +00:00
|
|
|
backlog.reserve(m_blockchain.get_txpool_tx_count(include_unrelayed_txes));
|
2017-08-26 15:23:31 +00:00
|
|
|
m_blockchain.for_all_txpool_txes([&backlog, now](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
|
2018-07-18 21:24:53 +00:00
|
|
|
backlog.push_back({meta.weight, meta.fee, meta.receive_time - now});
|
2017-08-26 15:23:31 +00:00
|
|
|
return true;
|
2017-11-08 12:06:41 +00:00
|
|
|
}, false, include_unrelayed_txes);
|
2017-08-26 15:23:31 +00:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------
|
2017-11-08 12:06:41 +00:00
|
|
|
void tx_memory_pool::get_transaction_stats(struct txpool_stats& stats, bool include_unrelayed_txes) const
|
2017-05-31 18:11:56 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
|
|
|
const uint64_t now = time(NULL);
|
2017-06-02 02:30:26 +00:00
|
|
|
std::map<uint64_t, txpool_histo> agebytes;
|
2017-11-08 12:06:41 +00:00
|
|
|
stats.txs_total = m_blockchain.get_txpool_tx_count(include_unrelayed_txes);
|
2018-07-18 21:24:53 +00:00
|
|
|
std::vector<uint32_t> weights;
|
|
|
|
weights.reserve(stats.txs_total);
|
|
|
|
m_blockchain.for_all_txpool_txes([&stats, &weights, now, &agebytes](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
|
|
|
|
weights.push_back(meta.weight);
|
|
|
|
stats.bytes_total += meta.weight;
|
|
|
|
if (!stats.bytes_min || meta.weight < stats.bytes_min)
|
|
|
|
stats.bytes_min = meta.weight;
|
|
|
|
if (meta.weight > stats.bytes_max)
|
|
|
|
stats.bytes_max = meta.weight;
|
2017-05-31 18:11:56 +00:00
|
|
|
if (!meta.relayed)
|
|
|
|
stats.num_not_relayed++;
|
|
|
|
stats.fee_total += meta.fee;
|
|
|
|
if (!stats.oldest || meta.receive_time < stats.oldest)
|
|
|
|
stats.oldest = meta.receive_time;
|
|
|
|
if (meta.receive_time < now - 600)
|
|
|
|
stats.num_10m++;
|
|
|
|
if (meta.last_failed_height)
|
|
|
|
stats.num_failing++;
|
2017-08-23 13:19:38 +00:00
|
|
|
uint64_t age = now - meta.receive_time + (now == meta.receive_time);
|
2017-06-02 02:30:26 +00:00
|
|
|
agebytes[age].txs++;
|
2018-07-18 21:24:53 +00:00
|
|
|
agebytes[age].bytes += meta.weight;
|
2017-09-22 12:57:20 +00:00
|
|
|
if (meta.double_spend_seen)
|
|
|
|
++stats.num_double_spends;
|
2017-05-31 18:11:56 +00:00
|
|
|
return true;
|
2017-11-08 12:06:41 +00:00
|
|
|
}, false, include_unrelayed_txes);
|
2018-07-18 21:24:53 +00:00
|
|
|
stats.bytes_med = epee::misc_utils::median(weights);
|
2017-06-02 02:30:26 +00:00
|
|
|
if (stats.txs_total > 1)
|
|
|
|
{
|
|
|
|
/* looking for 98th percentile */
|
|
|
|
size_t end = stats.txs_total * 0.02;
|
|
|
|
uint64_t delta, factor;
|
|
|
|
std::map<uint64_t, txpool_histo>::iterator it, i2;
|
|
|
|
if (end)
|
|
|
|
{
|
|
|
|
/* 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--);
|
|
|
|
stats.histo_98pc = it->first;
|
|
|
|
factor = 9;
|
|
|
|
delta = it->first;
|
|
|
|
stats.histo.resize(10);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/* If not enough txs, don't reserve the last slot;
|
|
|
|
* spread evenly across all 10 bins.
|
|
|
|
*/
|
|
|
|
stats.histo_98pc = 0;
|
|
|
|
it = agebytes.end();
|
|
|
|
factor = stats.txs_total > 9 ? 10 : stats.txs_total;
|
|
|
|
delta = now - stats.oldest;
|
|
|
|
stats.histo.resize(factor);
|
|
|
|
}
|
|
|
|
if (!delta)
|
|
|
|
delta = 1;
|
|
|
|
for (i2 = agebytes.begin(); i2 != it; i2++)
|
|
|
|
{
|
|
|
|
size_t i = (i2->first * factor - 1) / delta;
|
|
|
|
stats.histo[i].txs += i2->second.txs;
|
|
|
|
stats.histo[i].bytes += i2->second.bytes;
|
|
|
|
}
|
|
|
|
for (; i2 != agebytes.end(); i2++)
|
|
|
|
{
|
|
|
|
stats.histo[factor].txs += i2->second.txs;
|
|
|
|
stats.histo[factor].bytes += i2->second.bytes;
|
|
|
|
}
|
|
|
|
}
|
2017-05-31 18:11:56 +00:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------
|
2016-03-25 00:03:02 +00:00
|
|
|
//TODO: investigate whether boolean return is appropriate
|
2017-11-08 12:06:41 +00:00
|
|
|
bool tx_memory_pool::get_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos, bool include_sensitive_data) const
|
2015-04-23 12:13:07 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2018-04-15 23:16:02 +00:00
|
|
|
tx_infos.reserve(m_blockchain.get_txpool_tx_count());
|
|
|
|
key_image_infos.reserve(m_blockchain.get_txpool_tx_count());
|
2017-11-08 12:06:41 +00:00
|
|
|
m_blockchain.for_all_txpool_txes([&tx_infos, key_image_infos, include_sensitive_data](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
|
2015-04-23 12:13:07 +00:00
|
|
|
tx_info txi;
|
2017-05-14 13:06:55 +00:00
|
|
|
txi.id_hash = epee::string_tools::pod_to_hex(txid);
|
2018-10-17 23:01:56 +00:00
|
|
|
txi.tx_blob = *bd;
|
2017-05-14 13:06:55 +00:00
|
|
|
transaction tx;
|
|
|
|
if (!parse_and_validate_tx_from_blob(*bd, tx))
|
|
|
|
{
|
|
|
|
MERROR("Failed to parse tx from txpool");
|
|
|
|
// continue
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-05 20:34:10 +00:00
|
|
|
tx.set_hash(txid);
|
2017-05-14 13:06:55 +00:00
|
|
|
txi.tx_json = obj_to_json_str(tx);
|
2018-07-18 21:24:53 +00:00
|
|
|
txi.blob_size = bd->size();
|
|
|
|
txi.weight = meta.weight;
|
2017-05-14 13:06:55 +00:00
|
|
|
txi.fee = meta.fee;
|
|
|
|
txi.kept_by_block = meta.kept_by_block;
|
|
|
|
txi.max_used_block_height = meta.max_used_block_height;
|
|
|
|
txi.max_used_block_id_hash = epee::string_tools::pod_to_hex(meta.max_used_block_id);
|
|
|
|
txi.last_failed_height = meta.last_failed_height;
|
|
|
|
txi.last_failed_id_hash = epee::string_tools::pod_to_hex(meta.last_failed_id);
|
2017-11-08 12:06:41 +00:00
|
|
|
// In restricted mode we do not include this data:
|
|
|
|
txi.receive_time = include_sensitive_data ? meta.receive_time : 0;
|
2017-05-14 13:06:55 +00:00
|
|
|
txi.relayed = meta.relayed;
|
2017-11-08 12:06:41 +00:00
|
|
|
// In restricted mode we do not include this data:
|
|
|
|
txi.last_relayed_time = include_sensitive_data ? meta.last_relayed_time : 0;
|
2017-05-14 13:06:55 +00:00
|
|
|
txi.do_not_relay = meta.do_not_relay;
|
2017-09-22 12:57:20 +00:00
|
|
|
txi.double_spend_seen = meta.double_spend_seen;
|
2018-12-05 20:34:35 +00:00
|
|
|
tx_infos.push_back(std::move(txi));
|
2017-05-14 13:06:55 +00:00
|
|
|
return true;
|
2017-11-08 12:06:41 +00:00
|
|
|
}, true, include_sensitive_data);
|
2015-04-23 12:13:07 +00:00
|
|
|
|
2017-11-08 12:06:41 +00:00
|
|
|
txpool_tx_meta_t meta;
|
2015-04-23 12:13:07 +00:00
|
|
|
for (const key_images_container::value_type& kee : m_spent_key_images) {
|
|
|
|
const crypto::key_image& k_image = kee.first;
|
|
|
|
const std::unordered_set<crypto::hash>& kei_image_set = kee.second;
|
|
|
|
spent_key_image_info ki;
|
|
|
|
ki.id_hash = epee::string_tools::pod_to_hex(k_image);
|
|
|
|
for (const crypto::hash& tx_id_hash : kei_image_set)
|
|
|
|
{
|
2017-11-08 12:06:41 +00:00
|
|
|
if (!include_sensitive_data)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-12-14 17:09:30 +00:00
|
|
|
if (!m_blockchain.get_txpool_tx_meta(tx_id_hash, meta))
|
|
|
|
{
|
|
|
|
MERROR("Failed to get tx meta from txpool");
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-08 12:06:41 +00:00
|
|
|
if (!meta.relayed)
|
|
|
|
// Do not include that transaction if in restricted mode and it's not relayed
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MERROR("Failed to get tx meta from txpool: " << e.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-04-23 12:13:07 +00:00
|
|
|
ki.txs_hashes.push_back(epee::string_tools::pod_to_hex(tx_id_hash));
|
|
|
|
}
|
2017-11-08 12:06:41 +00:00
|
|
|
// Only return key images for which we have at least one tx that we can show for them
|
|
|
|
if (!ki.txs_hashes.empty())
|
|
|
|
key_image_infos.push_back(ki);
|
2015-04-23 12:13:07 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
//---------------------------------------------------------------------------------
|
2017-09-05 16:20:27 +00:00
|
|
|
bool tx_memory_pool::get_pool_for_rpc(std::vector<cryptonote::rpc::tx_in_pool>& tx_infos, cryptonote::rpc::key_images_with_tx_hashes& key_image_infos) const
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2018-04-15 23:16:02 +00:00
|
|
|
tx_infos.reserve(m_blockchain.get_txpool_tx_count());
|
|
|
|
key_image_infos.reserve(m_blockchain.get_txpool_tx_count());
|
2017-09-05 16:20:27 +00:00
|
|
|
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;
|
2018-12-05 20:34:10 +00:00
|
|
|
if (!parse_and_validate_tx_from_blob(*bd, txi.tx))
|
2017-09-05 16:20:27 +00:00
|
|
|
{
|
|
|
|
MERROR("Failed to parse tx from txpool");
|
|
|
|
// continue
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-05 20:34:10 +00:00
|
|
|
txi.tx.set_hash(txid);
|
2018-07-18 21:24:53 +00:00
|
|
|
txi.blob_size = bd->size();
|
|
|
|
txi.weight = meta.weight;
|
2017-09-05 16:20:27 +00:00
|
|
|
txi.fee = meta.fee;
|
|
|
|
txi.kept_by_block = meta.kept_by_block;
|
|
|
|
txi.max_used_block_height = meta.max_used_block_height;
|
|
|
|
txi.max_used_block_hash = meta.max_used_block_id;
|
|
|
|
txi.last_failed_block_height = meta.last_failed_height;
|
|
|
|
txi.last_failed_block_hash = meta.last_failed_id;
|
|
|
|
txi.receive_time = meta.receive_time;
|
|
|
|
txi.relayed = meta.relayed;
|
|
|
|
txi.last_relayed_time = meta.last_relayed_time;
|
|
|
|
txi.do_not_relay = meta.do_not_relay;
|
2017-09-22 12:57:20 +00:00
|
|
|
txi.double_spend_seen = meta.double_spend_seen;
|
2017-09-05 16:20:27 +00:00
|
|
|
tx_infos.push_back(txi);
|
|
|
|
return true;
|
2017-11-08 12:06:41 +00:00
|
|
|
}, true, false);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
|
|
|
for (const key_images_container::value_type& kee : m_spent_key_images) {
|
|
|
|
std::vector<crypto::hash> tx_hashes;
|
|
|
|
const std::unordered_set<crypto::hash>& kei_image_set = kee.second;
|
|
|
|
for (const crypto::hash& tx_id_hash : kei_image_set)
|
|
|
|
{
|
|
|
|
tx_hashes.push_back(tx_id_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
const crypto::key_image& k_image = kee.first;
|
2018-12-05 20:34:35 +00:00
|
|
|
key_image_infos[k_image] = std::move(tx_hashes);
|
2017-09-05 16:20:27 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
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);
|
|
|
|
|
|
|
|
spent.clear();
|
|
|
|
|
|
|
|
for (const auto& image : key_images)
|
|
|
|
{
|
|
|
|
spent.push_back(m_spent_key_images.find(image) == m_spent_key_images.end() ? false : true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2017-05-14 13:06:55 +00:00
|
|
|
bool tx_memory_pool::get_transaction(const crypto::hash& id, cryptonote::blobdata& txblob) const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
|
|
|
try
|
|
|
|
{
|
2017-06-11 14:10:18 +00:00
|
|
|
return m_blockchain.get_txpool_tx_blob(id, txblob);
|
2017-05-14 13:06:55 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
2017-05-14 13:06:55 +00:00
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
bool tx_memory_pool::on_blockchain_inc(uint64_t new_block_height, const crypto::hash& top_block_id)
|
|
|
|
{
|
2018-06-24 09:46:57 +00:00
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
m_input_cache.clear();
|
2018-10-31 13:52:16 +00:00
|
|
|
m_parsed_tx_cache.clear();
|
2014-03-03 22:07:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
bool tx_memory_pool::on_blockchain_dec(uint64_t new_block_height, const crypto::hash& top_block_id)
|
|
|
|
{
|
2018-06-24 09:46:57 +00:00
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
m_input_cache.clear();
|
2018-10-31 13:52:16 +00:00
|
|
|
m_parsed_tx_cache.clear();
|
2014-03-03 22:07:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2014-07-17 14:31:44 +00:00
|
|
|
bool tx_memory_pool::have_tx(const crypto::hash &id) const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
|
|
|
return m_blockchain.get_db().txpool_has_tx(id);
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2014-07-17 14:31:44 +00:00
|
|
|
bool tx_memory_pool::have_tx_keyimges_as_spent(const transaction& tx) const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2017-01-22 20:38:10 +00:00
|
|
|
for(const auto& in: tx.vin)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CHECKED_GET_SPECIFIC_VARIANT(in, const txin_to_key, tokey_in, true);//should never fail
|
|
|
|
if(have_tx_keyimg_as_spent(tokey_in.k_image))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2014-07-17 14:31:44 +00:00
|
|
|
bool tx_memory_pool::have_tx_keyimg_as_spent(const crypto::key_image& key_im) const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
return m_spent_key_images.end() != m_spent_key_images.find(key_im);
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2014-07-17 14:31:44 +00:00
|
|
|
void tx_memory_pool::lock() const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
m_transactions_lock.lock();
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2014-07-17 14:31:44 +00:00
|
|
|
void tx_memory_pool::unlock() const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
m_transactions_lock.unlock();
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-06-24 09:46:57 +00:00
|
|
|
bool tx_memory_pool::check_tx_inputs(const std::function<cryptonote::transaction&(void)> &get_tx, const crypto::hash &txid, uint64_t &max_used_block_height, crypto::hash &max_used_block_id, tx_verification_context &tvc, bool kept_by_block) const
|
|
|
|
{
|
|
|
|
if (!kept_by_block)
|
|
|
|
{
|
|
|
|
const std::unordered_map<crypto::hash, std::tuple<bool, tx_verification_context, uint64_t, crypto::hash>>::const_iterator i = m_input_cache.find(txid);
|
|
|
|
if (i != m_input_cache.end())
|
|
|
|
{
|
|
|
|
max_used_block_height = std::get<2>(i->second);
|
|
|
|
max_used_block_id = std::get<3>(i->second);
|
|
|
|
tvc = std::get<1>(i->second);
|
|
|
|
return std::get<0>(i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool ret = m_blockchain.check_tx_inputs(get_tx(), max_used_block_height, max_used_block_id, tvc, kept_by_block);
|
|
|
|
if (!kept_by_block)
|
|
|
|
m_input_cache.insert(std::make_pair(txid, std::make_tuple(ret, tvc, max_used_block_height, max_used_block_id)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
bool tx_memory_pool::is_transaction_ready_to_go(txpool_tx_meta_t& txd, const crypto::hash &txid, const cryptonote::blobdata &txblob, transaction &tx) const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2018-06-01 20:50:46 +00:00
|
|
|
struct transction_parser
|
|
|
|
{
|
2018-12-05 20:34:10 +00:00
|
|
|
transction_parser(const cryptonote::blobdata &txblob, const crypto::hash &txid, transaction &tx): txblob(txblob), txid(txid), tx(tx), parsed(false) {}
|
2018-06-01 20:50:46 +00:00
|
|
|
cryptonote::transaction &operator()()
|
|
|
|
{
|
|
|
|
if (!parsed)
|
|
|
|
{
|
|
|
|
if (!parse_and_validate_tx_from_blob(txblob, tx))
|
|
|
|
throw std::runtime_error("failed to parse transaction blob");
|
2018-12-05 20:34:10 +00:00
|
|
|
tx.set_hash(txid);
|
2018-06-01 20:50:46 +00:00
|
|
|
parsed = true;
|
|
|
|
}
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
const cryptonote::blobdata &txblob;
|
2018-12-05 20:34:10 +00:00
|
|
|
const crypto::hash &txid;
|
2018-06-01 20:50:46 +00:00
|
|
|
transaction &tx;
|
|
|
|
bool parsed;
|
2018-12-05 20:34:10 +00:00
|
|
|
} lazy_tx(txblob, txid, tx);
|
2018-06-01 20:50:46 +00:00
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
//not the best implementation at this time, sorry :(
|
|
|
|
//check is ring_signature already checked ?
|
|
|
|
if(txd.max_used_block_id == null_hash)
|
|
|
|
{//not checked, lets try to check
|
|
|
|
|
|
|
|
if(txd.last_failed_id != null_hash && m_blockchain.get_current_blockchain_height() > txd.last_failed_height && txd.last_failed_id == m_blockchain.get_block_id_by_height(txd.last_failed_height))
|
|
|
|
return false;//we already sure that this tx is broken for this height
|
|
|
|
|
2016-03-27 11:35:36 +00:00
|
|
|
tx_verification_context tvc;
|
2018-06-24 09:46:57 +00:00
|
|
|
if(!check_tx_inputs([&lazy_tx]()->cryptonote::transaction&{ return lazy_tx(); }, txid, txd.max_used_block_height, txd.max_used_block_id, tvc))
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
txd.last_failed_height = m_blockchain.get_current_blockchain_height()-1;
|
|
|
|
txd.last_failed_id = m_blockchain.get_block_id_by_height(txd.last_failed_height);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
if(txd.max_used_block_height >= m_blockchain.get_current_blockchain_height())
|
|
|
|
return false;
|
2016-04-05 12:06:29 +00:00
|
|
|
if(true)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
//if we already failed on this height and id, skip actual ring signature check
|
|
|
|
if(txd.last_failed_id == m_blockchain.get_block_id_by_height(txd.last_failed_height))
|
|
|
|
return false;
|
|
|
|
//check ring signature again, it is possible (with very small chance) that this transaction become again valid
|
2016-03-27 11:35:36 +00:00
|
|
|
tx_verification_context tvc;
|
2018-06-24 09:46:57 +00:00
|
|
|
if(!check_tx_inputs([&lazy_tx]()->cryptonote::transaction&{ return lazy_tx(); }, txid, txd.max_used_block_height, txd.max_used_block_id, tvc))
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
txd.last_failed_height = m_blockchain.get_current_blockchain_height()-1;
|
|
|
|
txd.last_failed_id = m_blockchain.get_block_id_by_height(txd.last_failed_height);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//if we here, transaction seems valid, but, anyway, check for key_images collisions with blockchain, just to be sure
|
2018-06-01 20:50:46 +00:00
|
|
|
if(m_blockchain.have_tx_keyimges_as_spent(lazy_tx()))
|
2017-09-22 12:57:20 +00:00
|
|
|
{
|
|
|
|
txd.double_spend_seen = true;
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
2017-09-22 12:57:20 +00:00
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
//transaction is ok.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-09-16 18:30:39 +00:00
|
|
|
bool tx_memory_pool::have_key_images(const std::unordered_set<crypto::key_image>& k_images, const transaction_prefix& tx)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
for(size_t i = 0; i!= tx.vin.size(); i++)
|
|
|
|
{
|
|
|
|
CHECKED_GET_SPECIFIC_VARIANT(tx.vin[i], const txin_to_key, itk, false);
|
|
|
|
if(k_images.count(itk.k_image))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-09-16 18:30:39 +00:00
|
|
|
bool tx_memory_pool::append_key_images(std::unordered_set<crypto::key_image>& k_images, const transaction_prefix& tx)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
for(size_t i = 0; i!= tx.vin.size(); i++)
|
|
|
|
{
|
|
|
|
CHECKED_GET_SPECIFIC_VARIANT(tx.vin[i], const txin_to_key, itk, false);
|
|
|
|
auto i_res = k_images.insert(itk.k_image);
|
|
|
|
CHECK_AND_ASSERT_MES(i_res.second, false, "internal error: key images pool cache - inserted duplicate image in set: " << itk.k_image);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2017-09-22 12:57:20 +00:00
|
|
|
void tx_memory_pool::mark_double_spend(const transaction &tx)
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2016-04-17 10:04:01 +00:00
|
|
|
bool changed = false;
|
2017-09-22 12:57:20 +00:00
|
|
|
LockedTXN lock(m_blockchain);
|
|
|
|
for(size_t i = 0; i!= tx.vin.size(); i++)
|
|
|
|
{
|
|
|
|
CHECKED_GET_SPECIFIC_VARIANT(tx.vin[i], const txin_to_key, itk, void());
|
|
|
|
const key_images_container::const_iterator it = m_spent_key_images.find(itk.k_image);
|
|
|
|
if (it != m_spent_key_images.end())
|
|
|
|
{
|
|
|
|
for (const crypto::hash &txid: it->second)
|
|
|
|
{
|
2017-12-14 17:09:30 +00:00
|
|
|
txpool_tx_meta_t meta;
|
|
|
|
if (!m_blockchain.get_txpool_tx_meta(txid, meta))
|
|
|
|
{
|
|
|
|
MERROR("Failed to find tx meta in txpool");
|
|
|
|
// continue, not fatal
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-22 12:57:20 +00:00
|
|
|
if (!meta.double_spend_seen)
|
|
|
|
{
|
|
|
|
MDEBUG("Marking " << txid << " as double spending " << itk.k_image);
|
|
|
|
meta.double_spend_seen = true;
|
2016-04-17 10:04:01 +00:00
|
|
|
changed = true;
|
2017-09-22 12:57:20 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_blockchain.update_txpool_tx(txid, meta);
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MERROR("Failed to update tx meta: " << e.what());
|
|
|
|
// continue, not fatal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 10:04:01 +00:00
|
|
|
if (changed)
|
|
|
|
++m_cookie;
|
2017-09-22 12:57:20 +00:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2014-07-17 14:31:44 +00:00
|
|
|
std::string tx_memory_pool::print_pool(bool short_format) const
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
|
|
|
m_blockchain.for_all_txpool_txes([&ss, short_format](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *txblob) {
|
|
|
|
ss << "id: " << txid << std::endl;
|
2014-07-17 14:31:44 +00:00
|
|
|
if (!short_format) {
|
2017-05-14 13:06:55 +00:00
|
|
|
cryptonote::transaction tx;
|
|
|
|
if (!parse_and_validate_tx_from_blob(*txblob, tx))
|
|
|
|
{
|
|
|
|
MERROR("Failed to parse tx from txpool");
|
|
|
|
return true; // continue
|
|
|
|
}
|
|
|
|
ss << obj_to_json_str(tx) << std::endl;
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
2018-07-18 21:24:53 +00:00
|
|
|
ss << "blob_size: " << (short_format ? "-" : std::to_string(txblob->size())) << std::endl
|
|
|
|
<< "weight: " << meta.weight << std::endl
|
2017-05-14 13:06:55 +00:00
|
|
|
<< "fee: " << print_money(meta.fee) << std::endl
|
|
|
|
<< "kept_by_block: " << (meta.kept_by_block ? 'T' : 'F') << std::endl
|
2017-09-22 12:57:20 +00:00
|
|
|
<< "double_spend_seen: " << (meta.double_spend_seen ? 'T' : 'F') << std::endl
|
2017-05-14 13:06:55 +00:00
|
|
|
<< "max_used_block_height: " << meta.max_used_block_height << std::endl
|
|
|
|
<< "max_used_block_id: " << meta.max_used_block_id << std::endl
|
|
|
|
<< "last_failed_height: " << meta.last_failed_height << std::endl
|
|
|
|
<< "last_failed_id: " << meta.last_failed_id << std::endl;
|
|
|
|
return true;
|
|
|
|
}, !short_format);
|
2014-07-17 14:31:44 +00:00
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2016-03-25 00:03:02 +00:00
|
|
|
//TODO: investigate whether boolean return is appropriate
|
2018-07-18 21:24:53 +00:00
|
|
|
bool tx_memory_pool::fill_block_template(block &bl, size_t median_weight, uint64_t already_generated_coins, size_t &total_weight, uint64_t &fee, uint64_t &expected_reward, uint8_t version)
|
2014-04-19 20:53:40 +00:00
|
|
|
{
|
2014-03-03 22:07:58 +00:00
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2017-03-21 12:21:19 +00:00
|
|
|
uint64_t best_coinbase = 0, coinbase = 0;
|
2018-07-18 21:24:53 +00:00
|
|
|
total_weight = 0;
|
2014-03-03 22:07:58 +00:00
|
|
|
fee = 0;
|
2017-01-12 23:39:24 +00:00
|
|
|
|
|
|
|
//baseline empty block
|
2018-07-18 21:24:53 +00:00
|
|
|
get_block_reward(median_weight, total_weight, already_generated_coins, best_coinbase, version);
|
2017-01-12 23:39:24 +00:00
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
size_t max_total_weight_pre_v5 = (130 * median_weight) / 100 - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
|
|
|
|
size_t max_total_weight_v5 = 2 * median_weight - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
|
|
|
|
size_t max_total_weight = version >= 5 ? max_total_weight_v5 : max_total_weight_pre_v5;
|
2014-04-02 16:00:17 +00:00
|
|
|
std::unordered_set<crypto::key_image> k_images;
|
2014-05-26 23:51:22 +00:00
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
LOG_PRINT_L2("Filling block template, median weight " << median_weight << ", " << m_txs_by_fee_and_receive_time.size() << " txes in the pool");
|
2017-08-29 15:35:19 +00:00
|
|
|
|
|
|
|
LockedTXN lock(m_blockchain);
|
|
|
|
|
2017-01-13 15:08:37 +00:00
|
|
|
auto sorted_it = m_txs_by_fee_and_receive_time.begin();
|
2018-08-17 17:24:24 +00:00
|
|
|
for (; sorted_it != m_txs_by_fee_and_receive_time.end(); ++sorted_it)
|
2014-04-19 20:53:40 +00:00
|
|
|
{
|
2017-12-14 17:09:30 +00:00
|
|
|
txpool_tx_meta_t meta;
|
|
|
|
if (!m_blockchain.get_txpool_tx_meta(sorted_it->second, meta))
|
|
|
|
{
|
|
|
|
MERROR(" failed to find tx meta");
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-18 21:24:53 +00:00
|
|
|
LOG_PRINT_L2("Considering " << sorted_it->second << ", weight " << meta.weight << ", current block weight " << total_weight << "/" << max_total_weight << ", current coinbase " << print_money(best_coinbase));
|
2015-04-30 05:02:12 +00:00
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
// Can not exceed maximum block weight
|
|
|
|
if (max_total_weight < total_weight + meta.weight)
|
2015-07-11 05:09:21 +00:00
|
|
|
{
|
2018-07-18 21:24:53 +00:00
|
|
|
LOG_PRINT_L2(" would exceed maximum block weight");
|
2015-07-15 05:47:07 +00:00
|
|
|
continue;
|
2015-07-11 05:09:21 +00:00
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2017-03-20 18:01:46 +00:00
|
|
|
// start using the optimal filling algorithm from v5
|
|
|
|
if (version >= 5)
|
2015-07-11 05:09:21 +00:00
|
|
|
{
|
2017-03-20 18:01:46 +00:00
|
|
|
// If we're getting lower coinbase tx,
|
|
|
|
// stop including more tx
|
|
|
|
uint64_t block_reward;
|
2018-07-18 21:24:53 +00:00
|
|
|
if(!get_block_reward(median_weight, total_weight + meta.weight, already_generated_coins, block_reward, version))
|
2017-03-20 18:01:46 +00:00
|
|
|
{
|
2018-07-18 21:24:53 +00:00
|
|
|
LOG_PRINT_L2(" would exceed maximum block weight");
|
2017-03-20 18:01:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-05-14 13:06:55 +00:00
|
|
|
coinbase = block_reward + fee + meta.fee;
|
2017-03-20 18:01:46 +00:00
|
|
|
if (coinbase < template_accept_threshold(best_coinbase))
|
|
|
|
{
|
|
|
|
LOG_PRINT_L2(" would decrease coinbase to " << print_money(coinbase));
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-06 20:34:37 +00:00
|
|
|
}
|
2017-03-20 18:01:46 +00:00
|
|
|
else
|
2017-01-06 20:34:37 +00:00
|
|
|
{
|
2018-07-18 21:24:53 +00:00
|
|
|
// If we've exceeded the penalty free weight,
|
2017-03-20 18:01:46 +00:00
|
|
|
// stop including more tx
|
2018-07-18 21:24:53 +00:00
|
|
|
if (total_weight > median_weight)
|
2017-03-20 18:01:46 +00:00
|
|
|
{
|
2018-07-18 21:24:53 +00:00
|
|
|
LOG_PRINT_L2(" would exceed median block weight");
|
2017-03-20 18:01:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-07-11 05:09:21 +00:00
|
|
|
}
|
2014-05-26 00:25:37 +00:00
|
|
|
|
2017-05-14 13:06:55 +00:00
|
|
|
cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(sorted_it->second);
|
|
|
|
cryptonote::transaction tx;
|
|
|
|
|
2014-05-26 00:25:37 +00:00
|
|
|
// Skip transactions that are not ready to be
|
|
|
|
// included into the blockchain or that are
|
|
|
|
// missing key images
|
2017-08-23 17:21:14 +00:00
|
|
|
const cryptonote::txpool_tx_meta_t original_meta = meta;
|
2018-06-01 20:50:46 +00:00
|
|
|
bool ready = false;
|
|
|
|
try
|
|
|
|
{
|
2018-06-24 09:46:57 +00:00
|
|
|
ready = is_transaction_ready_to_go(meta, sorted_it->second, txblob, tx);
|
2018-06-01 20:50:46 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MERROR("Failed to check transaction readiness: " << e.what());
|
|
|
|
// continue, not fatal
|
|
|
|
}
|
2017-08-23 17:21:14 +00:00
|
|
|
if (memcmp(&original_meta, &meta, sizeof(meta)))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_blockchain.update_txpool_tx(sorted_it->second, meta);
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MERROR("Failed to update tx meta: " << e.what());
|
|
|
|
// continue, not fatal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ready)
|
2015-07-11 05:09:21 +00:00
|
|
|
{
|
2017-03-03 22:32:51 +00:00
|
|
|
LOG_PRINT_L2(" not ready to go");
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-14 13:06:55 +00:00
|
|
|
if (have_key_images(k_images, tx))
|
2017-03-03 22:32:51 +00:00
|
|
|
{
|
|
|
|
LOG_PRINT_L2(" key images already seen");
|
2015-07-15 05:47:07 +00:00
|
|
|
continue;
|
2015-07-11 05:09:21 +00:00
|
|
|
}
|
2014-04-02 16:00:17 +00:00
|
|
|
|
2017-05-14 13:06:55 +00:00
|
|
|
bl.tx_hashes.push_back(sorted_it->second);
|
2018-07-18 21:24:53 +00:00
|
|
|
total_weight += meta.weight;
|
2017-05-14 13:06:55 +00:00
|
|
|
fee += meta.fee;
|
2017-01-06 20:34:37 +00:00
|
|
|
best_coinbase = coinbase;
|
2017-05-14 13:06:55 +00:00
|
|
|
append_key_images(k_images, tx);
|
2018-07-18 21:24:53 +00:00
|
|
|
LOG_PRINT_L2(" added, new block weight " << total_weight << "/" << max_total_weight << ", coinbase " << print_money(best_coinbase));
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 14:55:38 +00:00
|
|
|
expected_reward = best_coinbase;
|
2018-07-18 21:24:53 +00:00
|
|
|
LOG_PRINT_L2("Block template filled with " << bl.tx_hashes.size() << " txes, weight "
|
|
|
|
<< total_weight << "/" << max_total_weight << ", coinbase " << print_money(best_coinbase)
|
2017-01-06 20:34:37 +00:00
|
|
|
<< " (including " << print_money(fee) << " in fees)");
|
2014-03-03 22:07:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2016-01-29 15:09:17 +00:00
|
|
|
size_t tx_memory_pool::validate(uint8_t version)
|
|
|
|
{
|
2016-07-31 19:09:44 +00:00
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2018-07-18 21:24:53 +00:00
|
|
|
size_t tx_weight_limit = get_transaction_weight_limit(version);
|
2017-05-14 13:06:55 +00:00
|
|
|
std::unordered_set<crypto::hash> remove;
|
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txpool_weight = 0;
|
|
|
|
m_blockchain.for_all_txpool_txes([this, &remove, tx_weight_limit](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata*) {
|
|
|
|
m_txpool_weight += meta.weight;
|
|
|
|
if (meta.weight > tx_weight_limit) {
|
|
|
|
LOG_PRINT_L1("Transaction " << txid << " is too big (" << meta.weight << " bytes), removing it from pool");
|
2017-05-14 13:06:55 +00:00
|
|
|
remove.insert(txid);
|
2017-03-22 20:21:54 +00:00
|
|
|
}
|
2017-03-22 20:26:56 +00:00
|
|
|
else if (m_blockchain.have_tx(txid)) {
|
|
|
|
LOG_PRINT_L1("Transaction " << txid << " is in the blockchain, removing it from pool");
|
2017-05-14 13:06:55 +00:00
|
|
|
remove.insert(txid);
|
2017-03-22 20:21:54 +00:00
|
|
|
}
|
2017-05-14 13:06:55 +00:00
|
|
|
return true;
|
2017-11-08 12:06:41 +00:00
|
|
|
}, false);
|
2017-05-14 13:06:55 +00:00
|
|
|
|
|
|
|
size_t n_removed = 0;
|
|
|
|
if (!remove.empty())
|
|
|
|
{
|
|
|
|
LockedTXN lock(m_blockchain);
|
|
|
|
for (const crypto::hash &txid: remove)
|
|
|
|
{
|
|
|
|
try
|
2016-01-29 15:09:17 +00:00
|
|
|
{
|
2017-05-14 13:06:55 +00:00
|
|
|
cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(txid);
|
|
|
|
cryptonote::transaction tx;
|
|
|
|
if (!parse_and_validate_tx_from_blob(txblob, tx))
|
|
|
|
{
|
|
|
|
MERROR("Failed to parse tx from txpool");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// remove tx from db first
|
|
|
|
m_blockchain.remove_txpool_tx(txid);
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txpool_weight -= get_transaction_weight(tx, txblob.size());
|
2018-09-16 18:30:39 +00:00
|
|
|
remove_transaction_keyimages(tx, txid);
|
2017-05-14 13:06:55 +00:00
|
|
|
auto sorted_it = find_tx_in_sorted_container(txid);
|
|
|
|
if (sorted_it == m_txs_by_fee_and_receive_time.end())
|
|
|
|
{
|
|
|
|
LOG_PRINT_L1("Removing tx " << txid << " from tx pool, but it was not found in the sorted txs container!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_txs_by_fee_and_receive_time.erase(sorted_it);
|
|
|
|
}
|
|
|
|
++n_removed;
|
2016-01-29 15:09:17 +00:00
|
|
|
}
|
2017-05-14 13:06:55 +00:00
|
|
|
catch (const std::exception &e)
|
2016-01-29 15:09:17 +00:00
|
|
|
{
|
2017-05-14 13:06:55 +00:00
|
|
|
MERROR("Failed to remove invalid tx from pool");
|
|
|
|
// continue
|
2016-01-29 15:09:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 10:04:01 +00:00
|
|
|
if (n_removed > 0)
|
|
|
|
++m_cookie;
|
2016-01-29 15:09:17 +00:00
|
|
|
return n_removed;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------
|
2018-07-18 21:24:53 +00:00
|
|
|
bool tx_memory_pool::init(size_t max_txpool_weight)
|
2014-03-03 22:07:58 +00:00
|
|
|
{
|
2014-07-17 15:02:05 +00:00
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
2017-05-14 13:06:55 +00:00
|
|
|
CRITICAL_REGION_LOCAL1(m_blockchain);
|
2014-07-17 15:02:05 +00:00
|
|
|
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txpool_max_weight = max_txpool_weight ? max_txpool_weight : DEFAULT_TXPOOL_MAX_WEIGHT;
|
2017-05-14 13:06:55 +00:00
|
|
|
m_txs_by_fee_and_receive_time.clear();
|
|
|
|
m_spent_key_images.clear();
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txpool_weight = 0;
|
2017-09-20 17:55:16 +00:00
|
|
|
std::vector<crypto::hash> remove;
|
2018-04-11 14:27:56 +00:00
|
|
|
|
|
|
|
// first add the not kept by block, then the kept by block,
|
|
|
|
// to avoid rejection due to key image collision
|
|
|
|
for (int pass = 0; pass < 2; ++pass)
|
|
|
|
{
|
|
|
|
const bool kept = pass == 1;
|
|
|
|
bool r = m_blockchain.for_all_txpool_txes([this, &remove, kept](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd) {
|
|
|
|
if (!!kept != !!meta.kept_by_block)
|
|
|
|
return true;
|
2018-09-16 18:30:39 +00:00
|
|
|
cryptonote::transaction_prefix tx;
|
|
|
|
if (!parse_and_validate_tx_prefix_from_blob(*bd, tx))
|
2018-04-11 14:27:56 +00:00
|
|
|
{
|
|
|
|
MWARNING("Failed to parse tx from txpool, removing");
|
|
|
|
remove.push_back(txid);
|
2018-09-25 09:57:24 +00:00
|
|
|
return true;
|
2018-04-11 14:27:56 +00:00
|
|
|
}
|
2018-09-16 18:30:39 +00:00
|
|
|
if (!insert_key_images(tx, txid, meta.kept_by_block))
|
2018-04-11 14:27:56 +00:00
|
|
|
{
|
|
|
|
MFATAL("Failed to insert key images from txpool tx");
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-18 21:24:53 +00:00
|
|
|
m_txs_by_fee_and_receive_time.emplace(std::pair<double, time_t>(meta.fee / (double)meta.weight, meta.receive_time), txid);
|
|
|
|
m_txpool_weight += meta.weight;
|
2018-04-11 14:27:56 +00:00
|
|
|
return true;
|
|
|
|
}, true);
|
|
|
|
if (!r)
|
2017-05-14 13:06:55 +00:00
|
|
|
return false;
|
2018-04-11 14:27:56 +00:00
|
|
|
}
|
2017-09-20 17:55:16 +00:00
|
|
|
if (!remove.empty())
|
|
|
|
{
|
|
|
|
LockedTXN lock(m_blockchain);
|
|
|
|
for (const auto &txid: remove)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_blockchain.remove_txpool_tx(txid);
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
MWARNING("Failed to remove corrupt transaction: " << txid);
|
|
|
|
// ignore error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 10:04:01 +00:00
|
|
|
|
|
|
|
m_cookie = 0;
|
|
|
|
|
|
|
|
// Ignore deserialization error
|
2017-09-20 17:55:16 +00:00
|
|
|
return true;
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
bool tx_memory_pool::deinit()
|
|
|
|
{
|
2017-05-14 13:06:55 +00:00
|
|
|
return true;
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
}
|