Merge pull request #3322
pull/95/headeac3a11e
wallet: more user friendly print_ring (moneromooo-monero)79853514
wallet2_api: add key reuse mitigations API (moneromooo-monero)b057a21d
wallet2_api: add ring api (moneromooo-monero)d32ef7b0
ringdb: factor ring addition code (moneromooo-monero)a7da8208
wallet2_api: add blackball api (moneromooo-monero)2ab66ff1
liblmdb: install lmdb library for wallet2_api usage (stoffu)504428ab
ringdb: use the genesis block as a db name (moneromooo-monero)b09e5181
wallet: add a set_ring command (moneromooo-monero)0590f62a
new blockchain_usage tool, reports on output usage (moneromooo-monero)db10dd6d
wallet: make ringdb an object with database state (moneromooo-monero)df6fad4c
blockchain_utilities: new blockchain_blackball tool (moneromooo-monero)d29ea045
wallet: add an output blackball list to avoid using those in rings (moneromooo-monero)18eaf194
wallet: key reuse mitigation options (moneromooo-monero)5f146873
wallet: add shared ring database (moneromooo-monero)41f727ce
add RPC to get a histogram of outputs of a given amount (moneromooo-monero)
commit
7d928be240
@ -0,0 +1,427 @@
|
||||
// Copyright (c) 2014-2018, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <boost/range/adaptor/transformed.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include "common/command_line.h"
|
||||
#include "common/varint.h"
|
||||
#include "cryptonote_core/tx_pool.h"
|
||||
#include "cryptonote_core/cryptonote_core.h"
|
||||
#include "cryptonote_core/blockchain.h"
|
||||
#include "blockchain_db/blockchain_db.h"
|
||||
#include "blockchain_db/db_types.h"
|
||||
#include "wallet/ringdb.h"
|
||||
#include "version.h"
|
||||
|
||||
#undef MONERO_DEFAULT_LOG_CATEGORY
|
||||
#define MONERO_DEFAULT_LOG_CATEGORY "bcutil"
|
||||
|
||||
namespace po = boost::program_options;
|
||||
using namespace epee;
|
||||
using namespace cryptonote;
|
||||
|
||||
struct output_data
|
||||
{
|
||||
uint64_t amount;
|
||||
uint64_t index;
|
||||
output_data(uint64_t a, uint64_t i): amount(a), index(i) {}
|
||||
bool operator==(const output_data &other) const { return other.amount == amount && other.index == index; }
|
||||
};
|
||||
namespace std
|
||||
{
|
||||
template<> struct hash<output_data>
|
||||
{
|
||||
size_t operator()(const output_data &od) const
|
||||
{
|
||||
const uint64_t data[2] = {od.amount, od.index};
|
||||
crypto::hash h;
|
||||
crypto::cn_fast_hash(data, 2 * sizeof(uint64_t), h);
|
||||
return reinterpret_cast<const std::size_t &>(h);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static std::string get_default_db_path()
|
||||
{
|
||||
boost::filesystem::path dir = tools::get_default_data_dir();
|
||||
// remove .bitmonero, replace with .shared-ringdb
|
||||
dir = dir.remove_filename();
|
||||
dir /= ".shared-ringdb";
|
||||
return dir.string();
|
||||
}
|
||||
|
||||
static bool for_all_transactions(const std::string &filename, const std::function<bool(const cryptonote::transaction_prefix&)> &f)
|
||||
{
|
||||
MDB_env *env;
|
||||
MDB_dbi dbi;
|
||||
MDB_txn *txn;
|
||||
MDB_cursor *cur;
|
||||
int dbr;
|
||||
bool tx_active = false;
|
||||
|
||||
dbr = mdb_env_create(&env);
|
||||
if (dbr) throw std::runtime_error("Failed to create LDMB environment: " + std::string(mdb_strerror(dbr)));
|
||||
dbr = mdb_env_set_maxdbs(env, 2);
|
||||
if (dbr) throw std::runtime_error("Failed to set max env dbs: " + std::string(mdb_strerror(dbr)));
|
||||
const std::string actual_filename = filename;
|
||||
dbr = mdb_env_open(env, actual_filename.c_str(), 0, 0664);
|
||||
if (dbr) throw std::runtime_error("Failed to open rings database file '"
|
||||
+ actual_filename + "': " + std::string(mdb_strerror(dbr)));
|
||||
|
||||
dbr = mdb_txn_begin(env, NULL, 0, &txn);
|
||||
if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
|
||||
epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);});
|
||||
tx_active = true;
|
||||
|
||||
dbr = mdb_dbi_open(txn, "txs_pruned", MDB_INTEGERKEY, &dbi);
|
||||
if (dbr)
|
||||
dbr = mdb_dbi_open(txn, "txs", MDB_INTEGERKEY, &dbi);
|
||||
if (dbr) throw std::runtime_error("Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr)));
|
||||
dbr = mdb_cursor_open(txn, dbi, &cur);
|
||||
if (dbr) throw std::runtime_error("Failed to create LMDB cursor: " + std::string(mdb_strerror(dbr)));
|
||||
|
||||
MDB_val k;
|
||||
MDB_val v;
|
||||
bool fret = true;
|
||||
|
||||
MDB_cursor_op op = MDB_FIRST;
|
||||
while (1)
|
||||
{
|
||||
int ret = mdb_cursor_get(cur, &k, &v, op);
|
||||
op = MDB_NEXT;
|
||||
if (ret == MDB_NOTFOUND)
|
||||
break;
|
||||
if (ret)
|
||||
throw std::runtime_error("Failed to enumerate transactions: " + std::string(mdb_strerror(ret)));
|
||||
|
||||
cryptonote::transaction_prefix tx;
|
||||
blobdata bd;
|
||||
bd.assign(reinterpret_cast<char*>(v.mv_data), v.mv_size);
|
||||
std::stringstream ss;
|
||||
ss << bd;
|
||||
binary_archive<false> ba(ss);
|
||||
bool r = do_serialize(ba, tx);
|
||||
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob");
|
||||
|
||||
if (!f(tx)) {
|
||||
fret = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mdb_cursor_close(cur);
|
||||
mdb_txn_commit(txn);
|
||||
tx_active = false;
|
||||
mdb_dbi_close(env, dbi);
|
||||
mdb_env_close(env);
|
||||
return fret;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
TRY_ENTRY();
|
||||
|
||||
epee::string_tools::set_module_name_and_folder(argv[0]);
|
||||
|
||||
std::string default_db_type = "lmdb";
|
||||
|
||||
std::string available_dbs = cryptonote::blockchain_db_types(", ");
|
||||
available_dbs = "available: " + available_dbs;
|
||||
|
||||
uint32_t log_level = 0;
|
||||
|
||||
tools::on_startup();
|
||||
|
||||
boost::filesystem::path output_file_path;
|
||||
|
||||
po::options_description desc_cmd_only("Command line options");
|
||||
po::options_description desc_cmd_sett("Command line options and settings options");
|
||||
const command_line::arg_descriptor<std::string, false, true, 2> arg_blackball_db_dir = {
|
||||
"blackball-db-dir", "Specify blackball database directory",
|
||||
get_default_db_path(),
|
||||
{{ &arg_testnet_on, &arg_stagenet_on }},
|
||||
[](std::array<bool, 2> testnet_stagenet, bool defaulted, std::string val) {
|
||||
if (testnet_stagenet[0])
|
||||
return (boost::filesystem::path(val) / "testnet").string();
|
||||
else if (testnet_stagenet[1])
|
||||
return (boost::filesystem::path(val) / "stagenet").string();
|
||||
return val;
|
||||
}
|
||||
};
|
||||
const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
|
||||
const command_line::arg_descriptor<std::string> arg_database = {
|
||||
"database", available_dbs.c_str(), default_db_type
|
||||
};
|
||||
const command_line::arg_descriptor<bool> arg_rct_only = {"rct-only", "Only work on ringCT outputs", false};
|
||||
const command_line::arg_descriptor<std::vector<std::string> > arg_inputs = {"inputs", "Path to Monero DB, and path to any fork DBs"};
|
||||
|
||||
command_line::add_arg(desc_cmd_sett, arg_blackball_db_dir);
|
||||
command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
|
||||
command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
|
||||
command_line::add_arg(desc_cmd_sett, arg_log_level);
|
||||
command_line::add_arg(desc_cmd_sett, arg_database);
|
||||
command_line::add_arg(desc_cmd_sett, arg_rct_only);
|
||||
command_line::add_arg(desc_cmd_sett, arg_inputs);
|
||||
command_line::add_arg(desc_cmd_only, command_line::arg_help);
|
||||
|
||||
po::options_description desc_options("Allowed options");
|
||||
desc_options.add(desc_cmd_only).add(desc_cmd_sett);
|
||||
|
||||
po::positional_options_description positional_options;
|
||||
positional_options.add(arg_inputs.name, -1);
|
||||
|
||||
po::variables_map vm;
|
||||
bool r = command_line::handle_error_helper(desc_options, [&]()
|
||||
{
|
||||
auto parser = po::command_line_parser(argc, argv).options(desc_options).positional(positional_options);
|
||||
po::store(parser.run(), vm);
|
||||
po::notify(vm);
|
||||
return true;
|
||||
});
|
||||
if (! r)
|
||||
return 1;
|
||||
|
||||
if (command_line::get_arg(vm, command_line::arg_help))
|
||||
{
|
||||
std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL;
|
||||
std::cout << desc_options << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
mlog_configure(mlog_get_default_log_path("monero-blockchain-blackball.log"), true);
|
||||
if (!command_line::is_arg_defaulted(vm, arg_log_level))
|
||||
mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str());
|
||||
else
|
||||
mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str());
|
||||
|
||||
LOG_PRINT_L0("Starting...");
|
||||
|
||||
bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on);
|
||||
bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on);
|
||||
network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET;
|
||||
output_file_path = command_line::get_arg(vm, arg_blackball_db_dir);
|
||||
bool opt_rct_only = command_line::get_arg(vm, arg_rct_only);
|
||||
|
||||
std::string db_type = command_line::get_arg(vm, arg_database);
|
||||
if (!cryptonote::blockchain_valid_db_type(db_type))
|
||||
{
|
||||
std::cerr << "Invalid database type: " << db_type << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// If we wanted to use the memory pool, we would set up a fake_core.
|
||||
|
||||
// Use Blockchain instead of lower-level BlockchainDB for two reasons:
|
||||
// 1. Blockchain has the init() method for easy setup
|
||||
// 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash()
|
||||
//
|
||||
// cannot match blockchain_storage setup above with just one line,
|
||||
// e.g.
|
||||
// Blockchain* core_storage = new Blockchain(NULL);
|
||||
// because unlike blockchain_storage constructor, which takes a pointer to
|
||||
// tx_memory_pool, Blockchain's constructor takes tx_memory_pool object.
|
||||
LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
|
||||
const std::vector<std::string> inputs = command_line::get_arg(vm, arg_inputs);
|
||||
if (inputs.empty())
|
||||
{
|
||||
LOG_PRINT_L0("No inputs given");
|
||||
return 1;
|
||||
}
|
||||
std::vector<std::unique_ptr<Blockchain>> core_storage(inputs.size());
|
||||
tx_memory_pool m_mempool(*(Blockchain*)NULL);
|
||||
for (size_t n = 0; n < inputs.size(); ++n)
|
||||
{
|
||||
core_storage[n].reset(new Blockchain(m_mempool));
|
||||
|
||||
BlockchainDB* db = new_db(db_type);
|
||||
if (db == NULL)
|
||||
{
|
||||
LOG_ERROR("Attempted to use non-existent database type: " << db_type);
|
||||
throw std::runtime_error("Attempting to use non-existent database type");
|
||||
}
|
||||
LOG_PRINT_L0("database: " << db_type);
|
||||
|
||||
std::string filename = inputs[n];
|
||||
while (boost::ends_with(filename, "/") || boost::ends_with(filename, "\\"))
|
||||
filename.pop_back();
|
||||
LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
|
||||
|
||||
try
|
||||
{
|
||||
db->open(filename, DBF_RDONLY);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
LOG_PRINT_L0("Error opening database: " << e.what());
|
||||
return 1;
|
||||
}
|
||||
r = core_storage[n]->init(db, net_type);
|
||||
|
||||
CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage");
|
||||
LOG_PRINT_L0("Source blockchain storage initialized OK");
|
||||
}
|
||||
|
||||
boost::filesystem::path direc(output_file_path.string());
|
||||
if (boost::filesystem::exists(direc))
|
||||
{
|
||||
if (!boost::filesystem::is_directory(direc))
|
||||
{
|
||||
MERROR("LMDB needs a directory path, but a file was passed: " << output_file_path.string());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!boost::filesystem::create_directories(direc))
|
||||
{
|
||||
MERROR("Failed to create directory: " << output_file_path.string());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_PRINT_L0("Scanning for blackballable outputs...");
|
||||
|
||||
size_t done = 0;
|
||||
std::unordered_map<crypto::key_image, std::vector<uint64_t>> relative_rings;
|
||||
std::unordered_map<output_data, std::unordered_set<crypto::key_image>> outputs;
|
||||
std::unordered_set<output_data> spent, newly_spent;
|
||||
|
||||
cryptonote::block b = core_storage[0]->get_db().get_block_from_height(0);
|
||||
tools::ringdb ringdb(output_file_path.string(), epee::string_tools::pod_to_hex(get_block_hash(b)));
|
||||
|
||||
for (size_t n = 0; n < inputs.size(); ++n)
|
||||
{
|
||||
LOG_PRINT_L0("Reading blockchain from " << inputs[n]);
|
||||
for_all_transactions(inputs[n], [&](const cryptonote::transaction_prefix &tx)->bool
|
||||
{
|
||||
for (const auto &in: tx.vin)
|
||||
{
|
||||
if (in.type() != typeid(txin_to_key))
|
||||
continue;
|
||||
const auto &txin = boost::get<txin_to_key>(in);
|
||||
if (opt_rct_only && txin.amount != 0)
|
||||
continue;
|
||||
|
||||
const std::vector<uint64_t> absolute = cryptonote::relative_output_offsets_to_absolute(txin.key_offsets);
|
||||
if (n == 0)
|
||||
for (uint64_t out: absolute)
|
||||
outputs[output_data(txin.amount, out)].insert(txin.k_image);
|
||||
|
||||
std::vector<uint64_t> new_ring = txin.key_offsets;
|
||||
const uint32_t ring_size = txin.key_offsets.size();
|
||||
if (ring_size == 1)
|
||||
{
|
||||
const crypto::public_key pkey = core_storage[n]->get_output_key(txin.amount, txin.key_offsets[0]);
|
||||
MINFO("Blackballing output " << pkey << ", due to being used in a 1-ring");
|
||||
ringdb.blackball(pkey);
|
||||
newly_spent.insert(output_data(txin.amount, txin.key_offsets[0]));
|
||||
spent.insert(output_data(txin.amount, txin.key_offsets[0]));
|
||||
}
|
||||
else if (relative_rings.find(txin.k_image) != relative_rings.end())
|
||||
{
|
||||
MINFO("Key image " << txin.k_image << " already seen: rings " <<
|
||||
boost::join(relative_rings[txin.k_image] | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " ") <<
|
||||
", " << boost::join(txin.key_offsets | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " "));
|
||||
if (relative_rings[txin.k_image] != txin.key_offsets)
|
||||
{
|
||||
MINFO("Rings are different");
|
||||
const std::vector<uint64_t> r0 = cryptonote::relative_output_offsets_to_absolute(relative_rings[txin.k_image]);
|
||||
const std::vector<uint64_t> r1 = cryptonote::relative_output_offsets_to_absolute(txin.key_offsets);
|
||||
std::vector<uint64_t> common;
|
||||
for (uint64_t out: r0)
|
||||
{
|
||||
if (std::find(r1.begin(), r1.end(), out) != r1.end())
|
||||
common.push_back(out);
|
||||
}
|
||||
if (common.empty())
|
||||
{
|
||||
MERROR("Rings for the same key image are disjoint");
|
||||
}
|
||||
else if (common.size() == 1)
|
||||
{
|
||||
const crypto::public_key pkey = core_storage[n]->get_output_key(txin.amount, common[0]);
|
||||
MINFO("Blackballing output " << pkey << ", due to being used in rings with a single common element");
|
||||
ringdb.blackball(pkey);
|
||||
newly_spent.insert(output_data(txin.amount, common[0]));
|
||||
spent.insert(output_data(txin.amount, common[0]));
|
||||
}
|
||||
else
|
||||
{
|
||||
MINFO("The intersection has more than one element, it's still ok");
|
||||
for (const auto &out: r0)
|
||||
if (std::find(common.begin(), common.end(), out) != common.end())
|
||||
new_ring.push_back(out);
|
||||
new_ring = cryptonote::absolute_output_offsets_to_relative(new_ring);
|
||||
}
|
||||
}
|
||||
}
|
||||
relative_rings[txin.k_image] = new_ring;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
while (!newly_spent.empty())
|
||||
{
|
||||
LOG_PRINT_L0("Secondary pass due to " << newly_spent.size() << " newly found spent outputs");
|
||||
std::unordered_set<output_data> work_spent = std::move(newly_spent);
|
||||
newly_spent.clear();
|
||||
|
||||
for (const output_data &od: work_spent)
|
||||
{
|
||||
for (const crypto::key_image &ki: outputs[od])
|
||||
{
|
||||
std::vector<uint64_t> absolute = cryptonote::relative_output_offsets_to_absolute(relative_rings[ki]);
|
||||
size_t known = 0;
|
||||
uint64_t last_unknown = 0;
|
||||
for (uint64_t out: absolute)
|
||||
{
|
||||
output_data new_od(od.amount, out);
|
||||
if (spent.find(new_od) != spent.end())
|
||||
++known;
|
||||
else
|
||||
last_unknown = out;
|
||||
}
|
||||
if (known == absolute.size() - 1)
|
||||
{
|
||||
const crypto::public_key pkey = core_storage[0]->get_output_key(od.amount, last_unknown);
|
||||
MINFO("Blackballing output " << pkey << ", due to being used in a " <<
|
||||
absolute.size() << "-ring where all other outputs are known to be spent");
|
||||
ringdb.blackball(pkey);
|
||||
newly_spent.insert(output_data(od.amount, last_unknown));
|
||||
spent.insert(output_data(od.amount, last_unknown));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOG_PRINT_L0("Blockchain blackball data exported OK");
|
||||
return 0;
|
||||
|
||||
CATCH_ENTRY("Export error", 1);
|
||||
}
|
@ -0,0 +1,256 @@
|
||||
// Copyright (c) 2014-2018, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <boost/range/adaptor/transformed.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include "common/command_line.h"
|
||||
#include "common/varint.h"
|
||||
#include "cryptonote_core/tx_pool.h"
|
||||
#include "cryptonote_core/cryptonote_core.h"
|
||||
#include "cryptonote_core/blockchain.h"
|
||||
#include "blockchain_db/blockchain_db.h"
|
||||
#include "blockchain_db/db_types.h"
|
||||
#include "version.h"
|
||||
|
||||
#undef MONERO_DEFAULT_LOG_CATEGORY
|
||||
#define MONERO_DEFAULT_LOG_CATEGORY "bcutil"
|
||||
|
||||
namespace po = boost::program_options;
|
||||
using namespace epee;
|
||||
using namespace cryptonote;
|
||||
|
||||
struct output_data
|
||||
{
|
||||
uint64_t amount;
|
||||
uint64_t index;
|
||||
mutable bool coinbase;
|
||||
mutable uint64_t height;
|
||||
output_data(uint64_t a, uint64_t i, bool cb, uint64_t h): amount(a), index(i), coinbase(cb), height(h) {}
|
||||
bool operator==(const output_data &other) const { return other.amount == amount && other.index == index; }
|
||||
void info(bool c, uint64_t h) const { coinbase = c; height =h; }
|
||||
};
|
||||
namespace std
|
||||
{
|
||||
template<> struct hash<output_data>
|
||||
{
|
||||
size_t operator()(const output_data &od) const
|
||||
{
|
||||
const uint64_t data[2] = {od.amount, od.index};
|
||||
crypto::hash h;
|
||||
crypto::cn_fast_hash(data, 2 * sizeof(uint64_t), h);
|
||||
return reinterpret_cast<const std::size_t &>(h);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
struct reference
|
||||
{
|
||||
uint64_t height;
|
||||
uint64_t ring_size;
|
||||
uint64_t position;
|
||||
reference(uint64_t h, uint64_t rs, uint64_t p): height(h), ring_size(rs), position(p) {}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
TRY_ENTRY();
|
||||
|
||||
epee::string_tools::set_module_name_and_folder(argv[0]);
|
||||
|
||||
std::string default_db_type = "lmdb";
|
||||
|
||||
std::string available_dbs = cryptonote::blockchain_db_types(", ");
|
||||
available_dbs = "available: " + available_dbs;
|
||||
|
||||
uint32_t log_level = 0;
|
||||
|
||||
tools::on_startup();
|
||||
|
||||
boost::filesystem::path output_file_path;
|
||||
|
||||
po::options_description desc_cmd_only("Command line options");
|
||||
po::options_description desc_cmd_sett("Command line options and settings options");
|
||||
const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
|
||||
const command_line::arg_descriptor<std::string> arg_database = {
|
||||
"database", available_dbs.c_str(), default_db_type
|
||||
};
|
||||
const command_line::arg_descriptor<bool> arg_rct_only = {"rct-only", "Only work on ringCT outputs", false};
|
||||
const command_line::arg_descriptor<std::string> arg_input = {"input", ""};
|
||||
|
||||
command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on);
|
||||
command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on);
|
||||
command_line::add_arg(desc_cmd_sett, arg_log_level);
|
||||
command_line::add_arg(desc_cmd_sett, arg_database);
|
||||
command_line::add_arg(desc_cmd_sett, arg_rct_only);
|
||||
command_line::add_arg(desc_cmd_sett, arg_input);
|
||||
command_line::add_arg(desc_cmd_only, command_line::arg_help);
|
||||
|
||||
po::options_description desc_options("Allowed options");
|
||||
desc_options.add(desc_cmd_only).add(desc_cmd_sett);
|
||||
|
||||
po::positional_options_description positional_options;
|
||||
positional_options.add(arg_input.name, -1);
|
||||
|
||||
po::variables_map vm;
|
||||
bool r = command_line::handle_error_helper(desc_options, [&]()
|
||||
{
|
||||
auto parser = po::command_line_parser(argc, argv).options(desc_options).positional(positional_options);
|
||||
po::store(parser.run(), vm);
|
||||
po::notify(vm);
|
||||
return true;
|
||||
});
|
||||
if (! r)
|
||||
return 1;
|
||||
|
||||
if (command_line::get_arg(vm, command_line::arg_help))
|
||||
{
|
||||
std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL;
|
||||
std::cout << desc_options << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
mlog_configure(mlog_get_default_log_path("monero-blockchain-usage.log"), true);
|
||||
if (!command_line::is_arg_defaulted(vm, arg_log_level))
|
||||
mlog_set_log(command_line::get_arg(vm, arg_log_level).c_str());
|
||||
else
|
||||
mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str());
|
||||
|
||||
LOG_PRINT_L0("Starting...");
|
||||
|
||||
bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on);
|
||||
bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on);
|
||||
network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET;
|
||||
bool opt_rct_only = command_line::get_arg(vm, arg_rct_only);
|
||||
|
||||
std::string db_type = command_line::get_arg(vm, arg_database);
|
||||
if (!cryptonote::blockchain_valid_db_type(db_type))
|
||||
{
|
||||
std::cerr << "Invalid database type: " << db_type << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// If we wanted to use the memory pool, we would set up a fake_core.
|
||||
|
||||
// Use Blockchain instead of lower-level BlockchainDB for two reasons:
|
||||
// 1. Blockchain has the init() method for easy setup
|
||||
// 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash()
|
||||
//
|
||||
// cannot match blockchain_storage setup above with just one line,
|
||||
// e.g.
|
||||
// Blockchain* core_storage = new Blockchain(NULL);
|
||||
// because unlike blockchain_storage constructor, which takes a pointer to
|
||||
// tx_memory_pool, Blockchain's constructor takes tx_memory_pool object.
|
||||
LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
|
||||
const std::string input = command_line::get_arg(vm, arg_input);
|
||||
std::unique_ptr<Blockchain> core_storage;
|
||||
tx_memory_pool m_mempool(*core_storage);
|
||||
core_storage.reset(new Blockchain(m_mempool));
|
||||
BlockchainDB* db = new_db(db_type);
|
||||
if (db == NULL)
|
||||
{
|
||||
LOG_ERROR("Attempted to use non-existent database type: " << db_type);
|
||||
throw std::runtime_error("Attempting to use non-existent database type");
|
||||
}
|
||||
LOG_PRINT_L0("database: " << db_type);
|
||||
|
||||
const std::string filename = input;
|
||||
LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
|
||||
|
||||
try
|
||||
{
|
||||
db->open(filename, DBF_RDONLY);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
LOG_PRINT_L0("Error opening database: " << e.what());
|
||||
return 1;
|
||||
}
|
||||
r = core_storage->init(db, net_type);
|
||||
|
||||
CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage");
|
||||
LOG_PRINT_L0("Source blockchain storage initialized OK");
|
||||
|
||||
LOG_PRINT_L0("Building usage patterns...");
|
||||
|
||||
size_t done = 0;
|
||||
std::unordered_map<output_data, std::list<reference>> outputs;
|
||||
std::unordered_map<uint64_t,uint64_t> indices;
|
||||
|
||||
LOG_PRINT_L0("Reading blockchain from " << input);
|
||||
core_storage->for_all_transactions([&](const crypto::hash &hash, const cryptonote::transaction &tx)->bool
|
||||
{
|
||||
const bool coinbase = tx.vin.size() == 1 && tx.vin[0].type() == typeid(txin_gen);
|
||||
const uint64_t height = core_storage->get_db().get_tx_block_height(hash);
|
||||
|
||||
// create new outputs
|
||||
for (const auto &out: tx.vout)
|
||||
{
|
||||
if (opt_rct_only && out.amount)
|
||||
continue;
|
||||
uint64_t index = indices[out.amount]++;
|
||||
output_data od(out.amount, indices[out.amount], coinbase, height);
|
||||
auto itb = outputs.emplace(od, std::list<reference>());
|
||||
itb.first->first.info(coinbase, height);
|
||||
}
|
||||
|
||||
for (const auto &in: tx.vin)
|
||||
{
|
||||
if (in.type() != typeid(txin_to_key))
|
||||
continue;
|
||||
const auto &txin = boost::get<txin_to_key>(in);
|
||||
if (opt_rct_only && txin.amount != 0)
|
||||
continue;
|
||||
|
||||
const std::vector<uint64_t> absolute = cryptonote::relative_output_offsets_to_absolute(txin.key_offsets);
|
||||
for (size_t n = 0; n < txin.key_offsets.size(); ++n)
|
||||
{
|
||||
output_data od(txin.amount, absolute[n], coinbase, height);
|
||||
outputs[od].push_back(reference(height, txin.key_offsets.size(), n));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
std::unordered_map<uint64_t, uint64_t> counts;
|
||||
size_t total = 0;
|
||||
for (const auto &out: outputs)
|
||||
{
|
||||
counts[out.second.size()]++;
|
||||
total++;
|
||||
}
|
||||
for (const auto &c: counts)
|
||||
{
|
||||
float percent = 100.f * c.second / total;
|
||||
MINFO(std::to_string(c.second) << " outputs used " << c.first << " times (" << percent << "%)");
|
||||
}
|
||||
|
||||
LOG_PRINT_L0("Blockchain usage exported OK");
|
||||
return 0;
|
||||
|
||||
CATCH_ENTRY("Export error", 1);
|
||||
}
|
@ -0,0 +1,445 @@
|
||||
// Copyright (c) 2018, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <lmdb.h>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/range/adaptor/transformed.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include "misc_log_ex.h"
|
||||
#include "misc_language.h"
|
||||
#include "wallet_errors.h"
|
||||
#include "ringdb.h"
|
||||
|
||||
#undef MONERO_DEFAULT_LOG_CATEGORY
|
||||
#define MONERO_DEFAULT_LOG_CATEGORY "wallet.ringdb"
|
||||
|
||||
static const char zerokey[8] = {0};
|
||||
static const MDB_val zerokeyval = { sizeof(zerokey), (void *)zerokey };
|
||||
|
||||
static int compare_hash32(const MDB_val *a, const MDB_val *b)
|
||||
{
|
||||
uint32_t *va = (uint32_t*) a->mv_data;
|
||||
uint32_t *vb = (uint32_t*) b->mv_data;
|
||||
for (int n = 7; n >= 0; n--)
|
||||
{
|
||||
if (va[n] == vb[n])
|
||||
continue;
|
||||
return va[n] < vb[n] ? -1 : 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static std::string compress_ring(const std::vector<uint64_t> &ring)
|
||||
{
|
||||
std::string s;
|
||||
for (uint64_t out: ring)
|
||||
s += tools::get_varint_data(out);
|
||||
return s;
|
||||
}
|
||||
|
||||
static std::vector<uint64_t> decompress_ring(const std::string &s)
|
||||
{
|
||||
std::vector<uint64_t> ring;
|
||||
int read = 0;
|
||||
for (std::string::const_iterator i = s.begin(); i != s.cend(); std::advance(i, read))
|
||||
{
|
||||
uint64_t out;
|
||||
std::string tmp(i, s.cend());
|
||||
read = tools::read_varint(tmp.begin(), tmp.end(), out);
|
||||
THROW_WALLET_EXCEPTION_IF(read <= 0 || read > 256, tools::error::wallet_internal_error, "Internal error decompressing ring");
|
||||
ring.push_back(out);
|
||||
}
|
||||
return ring;
|
||||
}
|
||||
|
||||
std::string get_rings_filename(boost::filesystem::path filename)
|
||||
{
|
||||
if (!boost::filesystem::is_directory(filename))
|
||||
filename.remove_filename();
|
||||
return filename.string();
|
||||
}
|
||||
|
||||
static crypto::chacha_iv make_iv(const crypto::key_image &key_image, const crypto::chacha_key &key)
|
||||
{
|
||||
static const char salt[] = "ringdsb";
|
||||
|
||||
uint8_t buffer[sizeof(key_image) + sizeof(key) + sizeof(salt)];
|
||||
memcpy(buffer, &key_image, sizeof(key_image));
|
||||
memcpy(buffer + sizeof(key_image), &key, sizeof(key));
|
||||
memcpy(buffer + sizeof(key_image) + sizeof(key), salt, sizeof(salt));
|
||||
crypto::hash hash;
|
||||
crypto::cn_fast_hash(buffer, sizeof(buffer), hash.data);
|
||||
static_assert(sizeof(hash) >= CHACHA_IV_SIZE, "Incompatible hash and chacha IV sizes");
|
||||
crypto::chacha_iv iv;
|
||||
memcpy(&iv, &hash, CHACHA_IV_SIZE);
|
||||
return iv;
|
||||
}
|
||||
|
||||
static std::string encrypt(const std::string &plaintext, const crypto::key_image &key_image, const crypto::chacha_key &key)
|
||||
{
|
||||
const crypto::chacha_iv iv = make_iv(key_image, key);
|
||||
std::string ciphertext;
|
||||
ciphertext.resize(plaintext.size() + sizeof(iv));
|
||||
crypto::chacha20(plaintext.data(), plaintext.size(), key, iv, &ciphertext[sizeof(iv)]);
|
||||
memcpy(&ciphertext[0], &iv, sizeof(iv));
|
||||
return ciphertext;
|
||||
}
|
||||
|
||||
static std::string encrypt(const crypto::key_image &key_image, const crypto::chacha_key &key)
|
||||
{
|
||||
return encrypt(std::string((const char*)&key_image, sizeof(key_image)), key_image, key);
|
||||
}
|
||||
|
||||
static std::string decrypt(const std::string &ciphertext, const crypto::key_image &key_image, const crypto::chacha_key &key)
|
||||
{
|
||||
const crypto::chacha_iv iv = make_iv(key_image, key);
|
||||
std::string plaintext;
|
||||
THROW_WALLET_EXCEPTION_IF(ciphertext.size() < sizeof(iv), tools::error::wallet_internal_error, "Bad ciphertext text");
|
||||
plaintext.resize(ciphertext.size() - sizeof(iv));
|
||||
crypto::chacha20(ciphertext.data() + sizeof(iv), ciphertext.size() - sizeof(iv), key, iv, &plaintext[0]);
|
||||
return plaintext;
|
||||
}
|
||||
|
||||
static void store_relative_ring(MDB_txn *txn, MDB_dbi &dbi, const crypto::key_image &key_image, const std::vector<uint64_t> &relative_ring, const crypto::chacha_key &chacha_key)
|
||||
{
|
||||
MDB_val key, data;
|
||||
std::string key_ciphertext = encrypt(key_image, chacha_key);
|
||||
key.mv_data = (void*)key_ciphertext.data();
|
||||
key.mv_size = key_ciphertext.size();
|
||||
std::string compressed_ring = compress_ring(relative_ring);
|
||||
std::string data_ciphertext = encrypt(compressed_ring, key_image, chacha_key);
|
||||
data.mv_size = data_ciphertext.size();
|
||||
data.mv_data = (void*)data_ciphertext.c_str();
|
||||
int dbr = mdb_put(txn, dbi, &key, &data, 0);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set ring for key image in LMDB table: " + std::string(mdb_strerror(dbr)));
|
||||
}
|
||||
|
||||
static int resize_env(MDB_env *env, const char *db_path, size_t needed)
|
||||
{
|
||||
MDB_envinfo mei;
|
||||
MDB_stat mst;
|
||||
int ret;
|
||||
|
||||
needed = std::max(needed, (size_t)(2ul * 1024 * 1024)); // at least 2 MB
|
||||
|
||||
ret = mdb_env_info(env, &mei);
|
||||
if (ret)
|
||||
return ret;
|
||||
ret = mdb_env_stat(env, &mst);
|
||||
if (ret)
|
||||
return ret;
|
||||
uint64_t size_used = mst.ms_psize * mei.me_last_pgno;
|
||||
uint64_t mapsize = mei.me_mapsize;
|
||||
if (size_used + needed > mei.me_mapsize)
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::filesystem::path path(db_path);
|
||||
boost::filesystem::space_info si = boost::filesystem::space(path);
|
||||
if(si.available < needed)
|
||||
{
|
||||
MERROR("!! WARNING: Insufficient free space to extend database !!: " << (si.available >> 20L) << " MB available");
|
||||
return ENOSPC;
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
// print something but proceed.
|
||||
MWARNING("Unable to query free disk space.");
|
||||
}
|
||||
|
||||
mapsize += needed;
|
||||
}
|
||||
return mdb_env_set_mapsize(env, mapsize);
|
||||
}
|
||||
|
||||
static size_t get_ring_data_size(size_t n_entries)
|
||||
{
|
||||
return n_entries * (32 + 1024); // highball 1kB for the ring data to make sure
|
||||
}
|
||||
|
||||
enum { BLACKBALL_BLACKBALL, BLACKBALL_UNBLACKBALL, BLACKBALL_QUERY, BLACKBALL_CLEAR};
|
||||
|
||||
namespace tools
|
||||
{
|
||||
|
||||
ringdb::ringdb(std::string filename, const std::string &genesis):
|
||||
filename(filename)
|
||||
{
|
||||
MDB_txn *txn;
|
||||
bool tx_active = false;
|
||||
int dbr;
|
||||
|
||||
tools::create_directories_if_necessary(filename);
|
||||
|
||||
dbr = mdb_env_create(&env);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LDMB environment: " + std::string(mdb_strerror(dbr)));
|
||||
dbr = mdb_env_set_maxdbs(env, 2);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set max env dbs: " + std::string(mdb_strerror(dbr)));
|
||||
const std::string actual_filename = get_rings_filename(filename);
|
||||
dbr = mdb_env_open(env, actual_filename.c_str(), 0, 0664);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to open rings database file '"
|
||||
+ actual_filename + "': " + std::string(mdb_strerror(dbr)));
|
||||
|
||||
dbr = mdb_txn_begin(env, NULL, 0, &txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
|
||||
epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);});
|
||||
tx_active = true;
|
||||
|
||||
dbr = mdb_dbi_open(txn, ("rings-" + genesis).c_str(), MDB_CREATE, &dbi_rings);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr)));
|
||||
mdb_set_compare(txn, dbi_rings, compare_hash32);
|
||||
|
||||
dbr = mdb_dbi_open(txn, ("blackballs-" + genesis).c_str(), MDB_CREATE | MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED, &dbi_blackballs);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to open LMDB dbi: " + std::string(mdb_strerror(dbr)));
|
||||
mdb_set_dupsort(txn, dbi_blackballs, compare_hash32);
|
||||
|
||||
dbr = mdb_txn_commit(txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn creating/opening database: " + std::string(mdb_strerror(dbr)));
|
||||
tx_active = false;
|
||||
}
|
||||
|
||||
ringdb::~ringdb()
|
||||
{
|
||||
mdb_dbi_close(env, dbi_rings);
|
||||
mdb_dbi_close(env, dbi_blackballs);
|
||||
mdb_env_close(env);
|
||||
}
|
||||
|
||||
bool ringdb::add_rings(const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx)
|
||||
{
|
||||
MDB_txn *txn;
|
||||
int dbr;
|
||||
bool tx_active = false;
|
||||
|
||||
dbr = resize_env(env, filename.c_str(), get_ring_data_size(tx.vin.size()));
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size");
|
||||
dbr = mdb_txn_begin(env, NULL, 0, &txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
|
||||
epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);});
|
||||
tx_active = true;
|
||||
|
||||
for (const auto &in: tx.vin)
|
||||
{
|
||||
if (in.type() != typeid(cryptonote::txin_to_key))
|
||||
continue;
|
||||
const auto &txin = boost::get<cryptonote::txin_to_key>(in);
|
||||
const uint32_t ring_size = txin.key_offsets.size();
|
||||
if (ring_size == 1)
|
||||
continue;
|
||||
|
||||
store_relative_ring(txn, dbi_rings, txin.k_image, txin.key_offsets, chacha_key);
|
||||
}
|
||||
|
||||
dbr = mdb_txn_commit(txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn adding ring to database: " + std::string(mdb_strerror(dbr)));
|
||||
tx_active = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ringdb::remove_rings(const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx)
|
||||
{
|
||||
MDB_txn *txn;
|
||||
int dbr;
|
||||
bool tx_active = false;
|
||||
|
||||
dbr = resize_env(env, filename.c_str(), 0);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size");
|
||||
dbr = mdb_txn_begin(env, NULL, 0, &txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
|
||||
epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);});
|
||||
tx_active = true;
|
||||
|
||||
for (const auto &in: tx.vin)
|
||||
{
|
||||
if (in.type() != typeid(cryptonote::txin_to_key))
|
||||
continue;
|
||||
const auto &txin = boost::get<cryptonote::txin_to_key>(in);
|
||||
const uint32_t ring_size = txin.key_offsets.size();
|
||||
if (ring_size == 1)
|
||||
continue;
|
||||
|
||||
MDB_val key, data;
|
||||
std::string key_ciphertext = encrypt(txin.k_image, chacha_key);
|
||||
key.mv_data = (void*)key_ciphertext.data();
|
||||
key.mv_size = key_ciphertext.size();
|
||||
|
||||
dbr = mdb_get(txn, dbi_rings, &key, &data);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr && dbr != MDB_NOTFOUND, tools::error::wallet_internal_error, "Failed to look for key image in LMDB table: " + std::string(mdb_strerror(dbr)));
|
||||
if (dbr == MDB_NOTFOUND)
|
||||
continue;
|
||||
THROW_WALLET_EXCEPTION_IF(data.mv_size <= 0, tools::error::wallet_internal_error, "Invalid ring data size");
|
||||
|
||||
MDEBUG("Removing ring data for key image " << txin.k_image);
|
||||
dbr = mdb_del(txn, dbi_rings, &key, NULL);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to remove ring to database: " + std::string(mdb_strerror(dbr)));
|
||||
}
|
||||
|
||||
dbr = mdb_txn_commit(txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn removing ring to database: " + std::string(mdb_strerror(dbr)));
|
||||
tx_active = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ringdb::get_ring(const crypto::chacha_key &chacha_key, const crypto::key_image &key_image, std::vector<uint64_t> &outs)
|
||||
{
|
||||
MDB_txn *txn;
|
||||
int dbr;
|
||||
bool tx_active = false;
|
||||
|
||||
dbr = resize_env(env, filename.c_str(), 0);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size: " + std::string(mdb_strerror(dbr)));
|
||||
dbr = mdb_txn_begin(env, NULL, 0, &txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
|
||||
epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);});
|
||||
tx_active = true;
|
||||
|
||||
MDB_val key, data;
|
||||
std::string key_ciphertext = encrypt(key_image, chacha_key);
|
||||
key.mv_data = (void*)key_ciphertext.data();
|
||||
key.mv_size = key_ciphertext.size();
|
||||
dbr = mdb_get(txn, dbi_rings, &key, &data);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr && dbr != MDB_NOTFOUND, tools::error::wallet_internal_error, "Failed to look for key image in LMDB table: " + std::string(mdb_strerror(dbr)));
|
||||
if (dbr == MDB_NOTFOUND)
|
||||
return false;
|
||||
THROW_WALLET_EXCEPTION_IF(data.mv_size <= 0, tools::error::wallet_internal_error, "Invalid ring data size");
|
||||
|
||||
std::string data_plaintext = decrypt(std::string((const char*)data.mv_data, data.mv_size), key_image, chacha_key);
|
||||
outs = decompress_ring(data_plaintext);
|
||||
MDEBUG("Found ring for key image " << key_image << ":");
|
||||
MDEBUG("Relative: " << boost::join(outs | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " "));
|
||||
outs = cryptonote::relative_output_offsets_to_absolute(outs);
|
||||
MDEBUG("Absolute: " << boost::join(outs | boost::adaptors::transformed([](uint64_t out){return std::to_string(out);}), " "));
|
||||
|
||||
dbr = mdb_txn_commit(txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn getting ring from database: " + std::string(mdb_strerror(dbr)));
|
||||
tx_active = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ringdb::set_ring(const crypto::chacha_key &chacha_key, const crypto::key_image &key_image, const std::vector<uint64_t> &outs, bool relative)
|
||||
{
|
||||
MDB_txn *txn;
|
||||
int dbr;
|
||||
bool tx_active = false;
|
||||
|
||||
dbr = resize_env(env, filename.c_str(), outs.size() * 64);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size: " + std::string(mdb_strerror(dbr)));
|
||||
dbr = mdb_txn_begin(env, NULL, 0, &txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
|
||||
epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);});
|
||||
tx_active = true;
|
||||
|
||||
store_relative_ring(txn, dbi_rings, key_image, relative ? outs : cryptonote::absolute_output_offsets_to_relative(outs), chacha_key);
|
||||
|
||||
dbr = mdb_txn_commit(txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn setting ring to database: " + std::string(mdb_strerror(dbr)));
|
||||
tx_active = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ringdb::blackball_worker(const crypto::public_key &output, int op)
|
||||
{
|
||||
MDB_txn *txn;
|
||||
MDB_cursor *cursor;
|
||||
int dbr;
|
||||
bool tx_active = false;
|
||||
bool ret = true;
|
||||
|
||||
dbr = resize_env(env, filename.c_str(), 32 * 2); // a pubkey, and some slack
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to set env map size: " + std::string(mdb_strerror(dbr)));
|
||||
dbr = mdb_txn_begin(env, NULL, 0, &txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
|
||||
epee::misc_utils::auto_scope_leave_caller txn_dtor = epee::misc_utils::create_scope_leave_handler([&](){if (tx_active) mdb_txn_abort(txn);});
|
||||
tx_active = true;
|
||||
|
||||
MDB_val key = zerokeyval;
|
||||
MDB_val data;
|
||||
data.mv_data = (void*)&output;
|
||||
data.mv_size = sizeof(output);
|
||||
|
||||
switch (op)
|
||||
{
|
||||
case BLACKBALL_BLACKBALL:
|
||||
MDEBUG("Blackballing output " << output);
|
||||
dbr = mdb_put(txn, dbi_blackballs, &key, &data, MDB_NODUPDATA);
|
||||
if (dbr == MDB_KEYEXIST)
|
||||
dbr = 0;
|
||||
break;
|
||||
case BLACKBALL_UNBLACKBALL:
|
||||
MDEBUG("Unblackballing output " << output);
|
||||
dbr = mdb_del(txn, dbi_blackballs, &key, &data);
|
||||
if (dbr == MDB_NOTFOUND)
|
||||
dbr = 0;
|
||||
break;
|
||||
case BLACKBALL_QUERY:
|
||||
dbr = mdb_cursor_open(txn, dbi_blackballs, &cursor);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to create cursor for blackballs table: " + std::string(mdb_strerror(dbr)));
|
||||
dbr = mdb_cursor_get(cursor, &key, &data, MDB_GET_BOTH);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr && dbr != MDB_NOTFOUND, tools::error::wallet_internal_error, "Failed to lookup in blackballs table: " + std::string(mdb_strerror(dbr)));
|
||||
ret = dbr != MDB_NOTFOUND;
|
||||
if (dbr == MDB_NOTFOUND)
|
||||
dbr = 0;
|
||||
mdb_cursor_close(cursor);
|
||||
break;
|
||||
case BLACKBALL_CLEAR:
|
||||
dbr = mdb_drop(txn, dbi_blackballs, 0);
|
||||
break;
|
||||
default:
|
||||
THROW_WALLET_EXCEPTION(tools::error::wallet_internal_error, "Invalid blackball op");
|
||||
}
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to query blackballs table: " + std::string(mdb_strerror(dbr)));
|
||||
|
||||
dbr = mdb_txn_commit(txn);
|
||||
THROW_WALLET_EXCEPTION_IF(dbr, tools::error::wallet_internal_error, "Failed to commit txn blackballing output to database: " + std::string(mdb_strerror(dbr)));
|
||||
tx_active = false;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ringdb::blackball(const crypto::public_key &output)
|
||||
{
|
||||
return blackball_worker(output, BLACKBALL_BLACKBALL);
|
||||
}
|
||||
|
||||
bool ringdb::unblackball(const crypto::public_key &output)
|
||||
{
|
||||
return blackball_worker(output, BLACKBALL_UNBLACKBALL);
|
||||
}
|
||||
|
||||
bool ringdb::blackballed(const crypto::public_key &output)
|
||||
{
|
||||
return blackball_worker(output, BLACKBALL_QUERY);
|
||||
}
|
||||
|
||||
bool ringdb::clear_blackballs()
|
||||
{
|
||||
return blackball_worker(crypto::public_key(), BLACKBALL_CLEAR);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
// Copyright (c) 2018, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <lmdb.h>
|
||||
#include "wipeable_string.h"
|
||||
#include "crypto/crypto.h"
|
||||
#include "cryptonote_basic/cryptonote_basic.h"
|
||||
|
||||
namespace tools
|
||||
{
|
||||
class ringdb
|
||||
{
|
||||
public:
|
||||
ringdb(std::string filename, const std::string &genesis);
|
||||
~ringdb();
|
||||
|
||||
bool add_rings(const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx);
|
||||
bool remove_rings(const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx);
|
||||
bool get_ring(const crypto::chacha_key &chacha_key, const crypto::key_image &key_image, std::vector<uint64_t> &outs);
|
||||
bool set_ring(const crypto::chacha_key &chacha_key, const crypto::key_image &key_image, const std::vector<uint64_t> &outs, bool relative);
|
||||
|
||||
bool blackball(const crypto::public_key &output);
|
||||
bool unblackball(const crypto::public_key &output);
|
||||
bool blackballed(const crypto::public_key &output);
|
||||
bool clear_blackballs();
|
||||
|
||||
private:
|
||||
bool blackball_worker(const crypto::public_key &output, int op);
|
||||
|
||||
private:
|
||||
std::string filename;
|
||||
MDB_env *env;
|
||||
MDB_dbi dbi_rings;
|
||||
MDB_dbi dbi_blackballs;
|
||||
};
|
||||
}
|
Loading…
Reference in new issue